-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathactions.js
More file actions
executable file
·532 lines (458 loc) · 16.4 KB
/
actions.js
File metadata and controls
executable file
·532 lines (458 loc) · 16.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
#!/usr/bin/gjs
'use strict';
const extPath = '.local/share/gnome-shell/extensions/quicktext@brainstormtrooper.github.io';
imports.gi.versions.Gtk = '4.0';
imports.searchPath.unshift(extPath);
const Adw = imports.gi.Adw;
const GLib = imports.gi.GLib;
const Gio = imports.gi.Gio;
const Gtk = imports.gi.Gtk;
const Gdk = imports.gi.Gdk;
const GObject = imports.gi.GObject;
const Gettext = imports.gettext;
const GioSSS = Gio.SettingsSchemaSource;
const { qWindow } = imports.interface;
const { vevent, vtodo, treated } = imports.events;
const spl = Gio.SubprocessLauncher;
const QuickText = GObject.registerClass( // eslint-disable-line
{
GTypeName: 'QuickText'
},
class QuickText extends Adw.Application {
_init () {
this.items = [];
this.recycle = {};
this.ID = 'com.github.brainstormtrooper.QuickText';
super._init({
application_id: this.ID
});
GLib.set_prgname(this.application_id);
GLib.set_application_name('QuickText');
}
vfunc_shutdown () {
super.vfunc_shutdown();
}
vfunc_activate () {
super.vfunc_activate();
// Create the application window
this.timeFmt = this.getTimeFormat();
try {
let css_provider = Gtk.CssProvider.new();
css_provider.load_from_path(`${extPath}/stylesheet.css`);
// const context = new Gtk.StyleContext();
const display = Gdk.Display.get_default();
Gtk.StyleContext.add_provider_for_display(display, css_provider, Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION);
} catch (error) {
console.error(error);
}
this.window = new qWindow({ application: this });
this.openButton = this.window._openButton;
this.toastOverlay = this.window._toast_overlay;
this.launcher = new spl();
this.openButton.connect('clicked', () => {
const settings = this.getSettings();
this.launcher.spawnv(['xdg-open', settings.get_string('quick-filepath')]);
});
this.window._listBox.append(this.getListUI());
this.window.present();
}
vfunc_startup() {
super.vfunc_startup();
}
getTimeFormat () {
const schemaId = 'org.gnome.desktop.interface';
const gSettings = new Gio.Settings({ schema: schemaId });
const timeFmt = gSettings.get_string('clock-format');
return timeFmt;
}
getSettings () {
// ["quick-filepath", "quick-multiline", "quick-pendlocation", "quick-append", "quick-hotkey", "quick-prepend"]
const schemaId = 'org.gnome.shell.extensions.quicktext';
const schemaSource = GioSSS.new_from_directory(`${extPath}/schemas`, null, true);
const schema = schemaSource.lookup(schemaId, true);
const schemaObj = { settings_schema: schema }
return new Gio.Settings(schemaObj);
}
updateListUI () {
this.window._listBox.remove(this.listBox);
this.window._listBox.append(this.getListUI());
}
leadingZeros (spin_button) {
const adjustment = spin_button.get_adjustment();
spin_button.set_text(String(adjustment.get_value()).padStart(2, '0'));
return true;
}
getPicker (i) {
const now = GLib.DateTime.new_now_local();
const tbtn = Gtk.ToggleButton.new_with_label('Task');
const ebtn = Gtk.ToggleButton.new_with_label('Event');
ebtn.set_group(tbtn);
ebtn.set_active(true);
const rowLabel = new Gtk.Label({label: 'Create new : '});
const rowLimit = new Gtk.Label({label: 'Duration : '});
const dAdjust = new Gtk.Adjustment({
value: 1,
lower: 1,
upper: 24,
step_increment: 1
});
const duration = new Gtk.SpinButton({
adjustment: dAdjust,
climb_rate: 1,
numeric: true,
digits: 0,
value: 1,
hexpand: false
});
duration.set_orientation(Gtk.Orientation.HORIZONTAL);
tbtn.connect('toggled', () => {
rowLimit.set_label('Due :');
duration.set_visible(false);
});
ebtn.connect('toggled', () => {
rowLimit.set_label('Duration :');
duration.set_visible(true);
});
const row1 = new Gtk.Box({
orientation: Gtk.Orientation.HORIZONTAL,
spacing: 6
});
const row2 = new Gtk.Box({
orientation: Gtk.Orientation.HORIZONTAL,
spacing: 6
});
row1.append(rowLabel);
row1.append(tbtn);
row1.append(ebtn);
row2.append(rowLimit);
row2.append(duration);
const row3 = new Gtk.Box({
orientation: Gtk.Orientation.HORIZONTAL,
spacing: 6
});
const calendar = new Gtk.Calendar();
const hAdjust = new Gtk.Adjustment({
value: now.get_hour(),
lower: 1,
upper: (this.timeFmt == '12h' ? 12 : 24),
step_increment: 1
});
const hours = new Gtk.SpinButton({
adjustment: hAdjust,
climb_rate: 1,
numeric: true,
digits: 0,
value: now.get_hour(),
vexpand: false
});
hours.set_orientation(Gtk.Orientation.VERTICAL);
hours.connect('output', this.leadingZeros);
const mAdjust = new Gtk.Adjustment({
value: now.get_minute(),
lower: 0,
upper: 59,
step_increment: 10
});
const minutes = new Gtk.SpinButton({
adjustment: mAdjust,
climb_rate: 10,
numeric: true,
digits: 0,
value: now.get_minute(),
vexpand: false
});
minutes.set_orientation(Gtk.Orientation.VERTICAL);
minutes.connect('output', this.leadingZeros);
const timeSep = new Gtk.Label({ label: ':' });
const am = Gtk.ToggleButton.new_with_label('AM');
const pm = Gtk.ToggleButton.new_with_label('PM');
pm.set_group(am);
if (now.get_hour() >= 12) {
pm.set_active(true);
if (this.timeFmt == '12h' && now.get_hour() > 12) {
hours.set_value(now.get_hour() - 12);
}
} else {
am.set_active(true);
}
const ampmBox = new Gtk.Box({
orientation: Gtk.Orientation.VERTICAL,
spacing: 6,
visible: (this.timeFmt == '12h' ? true : false)
});
ampmBox.append(am);
ampmBox.append(pm);
const timeBox = new Gtk.Box({
orientation: Gtk.Orientation.HORIZONTAL,
spacing: 6,
baseline_position: Gtk.BaselinePosition.CENTER,
vexpand: false
});
timeBox.append(hours);
timeBox.append(timeSep);
timeBox.append(minutes);
timeBox.append(ampmBox);
const times = new Gtk.Box({
orientation: Gtk.Orientation.VERTICAL,
spacing: 6,
homogeneous: true
});
const above = new Gtk.Separator({ orientation: Gtk.Orientation.VERTICAL });
const below = new Gtk.Separator({ orientation: Gtk.Orientation.VERTICAL });
above.add_css_class('spacer');
below.add_css_class('spacer');
times.append(above);
times.append(timeBox);
times.append(below);
row3.append(calendar);
row3.append(times);
const pickBox = new Gtk.Box({
orientation: Gtk.Orientation.VERTICAL,
spacing: 6
});
const save = new Gtk.Button({
label: 'Save'
});
const row4 = new Gtk.Box({
orientation: Gtk.Orientation.HORIZONTAL,
spacing: 6,
halign: Gtk.Align.END
});
row4.append(save);
pickBox.append(row1);
pickBox.append(row2);
pickBox.append(row3);
pickBox.append(row4);
save.connect('clicked', () => {
let start, end, due;
const note = this.items[i];
const isEvent = ebtn.get_active();
// const targetHr = (isEvent ? parseInt(hours.get_value()) + 1 : parseInt(hours.get_value()));
const selDate = calendar.get_date();
const hour = ((pm.get_active() && this.timeFmt == '12h' && parseInt(hours.get_value()) < 12) ? String(parseInt(hours.get_value() + 12)) : hours.get_value());
start = GLib.DateTime.new_local(
selDate.get_year(),
selDate.get_month(),
selDate.get_day_of_month(),
hour,
minutes.get_value(),
'00'
);
due = start;
end = start.add_hours(parseInt(duration.get_value()));
const obj = { note, start, end, due, isEvent };
const tpl = (isEvent ? vevent : vtodo);
const eventstr = this.strRepl(tpl, obj);
const [tmpevent, ] = Gio.File.new_tmp('quick-XXXXXX.ics');
const bytes = GLib.ByteArray.new_take(eventstr);
tmpevent.replace_contents(bytes, null, false, null, null);
this.launcher.spawnv(['xdg-open', tmpevent.get_path()]);
this.items[i] = this.doFlag(this.items[i]);
this.doSave(this.doJoin(this.items));
this.updateListUI();
});
return pickBox;
}
getListUI () {
const settings = this.getSettings();
const hideActed = settings.get_boolean('quick-hideacted');
this.listBox = new Gtk.Box({
orientation: Gtk.Orientation.VERTICAL,
spacing: 6
});
try {
this.doList();
this.items.filter(item => /\S/.test(item)).forEach((item, i) => {
if ((hideActed && !item.match(/Quick treated/m)) || !hideActed) {
const frame = new Gtk.Frame({
label: null
});
const liBox = new Gtk.Box({
orientation: Gtk.Orientation.VERTICAL
});
const undeleteAction = new Gio.SimpleAction({name: `undelete_${i}`});
undeleteAction.connect('activate', () => {
this.items.splice(i, 0, this.recycle[i]);
// items[i] = this.recycle[i];
this.doSave(this.doJoin(this.items));
this.updateListUI();
});
this.add_action(undeleteAction);
const undeleteToast = new Adw.Toast({title: 'Note deleted'});
undeleteToast.set_button_label('Undo');
undeleteToast.set_action_name(`app.undelete_${i}`);
const liTxtView = new Gtk.TextView();
const liBuffer = new Gtk.TextBuffer();
liBuffer.set_text(item, -1);
liTxtView.set_buffer(liBuffer);
liTxtView.set_editable(false);
const ePopover = new Gtk.Popover();
ePopover.set_child(this.getPicker(i));
const liBtns = new Gtk.Box({
orientation: Gtk.Orientation.HORIZONTAL,
halign: Gtk.Align.END
});
const liDeleteBtn = new Gtk.Button({
icon_name: 'user-trash-symbolic',
tooltip_text: 'Delete'
});
const liEditBtn = new Gtk.Button({
icon_name: 'document-edit-symbolic',
tooltip_text: 'Edit'
});
const liSaveBtn = new Gtk.Button({
icon_name: 'document-save-symbolic',
tooltip_text: 'Save',
visible: false
});
const liCancelBtn = new Gtk.Button({
icon_name: 'edit-delete-symbolic',
tooltip_text: 'Cancel',
visible: false
});
const liEventBtn = new Gtk.MenuButton({
icon_name: 'x-office-calendar-symbolic',
tooltip_text: 'New Event',
popover: ePopover
});
liDeleteBtn.connect('clicked', () => {
this.recycle[i] = item;
this.items = this.items.filter(v => v != item);
this.doSave(this.doJoin(this.items));
this.toastOverlay.add_toast(undeleteToast);
this.updateListUI();
});
liEditBtn.connect('clicked', () => {
liEditBtn.set_visible(false);
liSaveBtn.set_visible(true);
liCancelBtn.set_visible(true);
liTxtView.set_editable(true);
});
liCancelBtn.connect('clicked', () => {
liEditBtn.set_visible(true);
liSaveBtn.set_visible(false);
liCancelBtn.set_visible(false);
liTxtView.set_editable(false);
this.updateListUI();
});
liSaveBtn.connect('clicked', () => {
liEditBtn.set_visible(true);
liSaveBtn.set_visible(false);
liCancelBtn.set_visible(false);
liTxtView.set_editable(false);
this.items[i] = `${liBuffer.get_text(liBuffer.get_start_iter(), liBuffer.get_end_iter(), true).trim()}\n`;
this.doSave(this.doJoin(this.items));
// this.toastOverlay.add_toast(undeleteToast);
this.updateListUI();
});
liBtns.append(liEditBtn);
liBtns.append(liSaveBtn);
liBtns.append(liCancelBtn);
liBtns.append(liEventBtn);
// liBtns.append(liTaskBtn);
liBtns.append(liDeleteBtn);
liBtns.add_css_class('toolbar');
liBox.append(liTxtView);
liBox.append(liBtns);
liBox.add_css_class('card');
frame.set_child(liBox);
this.listBox.append(frame);
}
});
} catch(error) {
console.error(error);
};
return this.listBox;
}
doFlag (item) {
const now = GLib.DateTime.new_now_utc();
const stamp = now.format('%Y%m%dT%H%M%SZ');
return `${item}${treated.replace('{{stamp}}', stamp)}\r`;
}
doJoin (items) {
const settings = this.getSettings();
const append = settings.get_string('quick-append');
return items.join(append);
}
doList () {
// let list;
const settings = this.getSettings();
const fpath = settings.get_string('quick-filepath');
const append = settings.get_string('quick-append');
try {
// [\s\S]*?(?<=^---$)
let fileStr = this.fopen(fpath);
this.items = fileStr.split(append);
} catch (error) {
console.error(error);
}
}
getSummary(note) {
let lines = note.trim().split(/\r\n|\r|\n/gm);
const summary = (lines[1].length > 72) ? lines[1].slice(0, n-1) + '...' : lines[1];
// const desc = note.match(/.{1,72}/g).join("\r\n ");
lines = lines.map(l => l.match(/.{1,72}/g).join("\r\n "));
const desc = lines.join('\\n');
return [summary, desc];
}
strRepl (tpl, obj) {
// obj = { note, start, end, due, isEvent }
let myCal = tpl;
const id = this.makeid();
const [summary, note] = this.getSummary(obj.note);
const now = GLib.DateTime.new_now_utc();
const stamp = now.format('%Y%m%dT%H%M%SZ');
const start = obj.start.to_utc();
const startdate = start.format('%Y%m%dT%H%M%SZ');
const due = obj.due.to_utc();
const duedate = due.format('%Y%m%dT%H%M%SZ');
const end = obj.end.to_utc();
const enddate = end.format('%Y%m%dT%H%M%SZ');
myCal = myCal.replace(/{{stamp}}/gm, stamp);
myCal = myCal.replace(/{{duedate}}/gm, duedate);
myCal = myCal.replace(/{{startdate}}/gm, startdate);
myCal = myCal.replace(/{{enddate}}/gm, enddate);
myCal = myCal.replace(/{{uuid}}/gm, id);
myCal = myCal.replace(/{{summary}}/gm, summary);
myCal = myCal.replace(/{{note}}/gm, note);
return myCal;
}
makeid () {
let result = '';
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
const charactersLength = characters.length;
let counter = 0;
while (counter < 6) {
result += characters.charAt(Math.floor(Math.random() * charactersLength));
counter += 1;
}
return result;
}
fopen (path) {
let dataString;
const file = Gio.File.new_for_path(path);
// asynchronous file loading...
const [ok, string, ] = file.load_contents(null);
if (ok) {
const decoder = new TextDecoder('utf-8');
dataString = decoder.decode(string);
} else {
console.error('Failed to open file');
}
return dataString;
}
doSave(str) {
const settings = this.getSettings();
const fpath = settings.get_string('quick-filepath');
const file = Gio.File.new_for_path(fpath);
file.replace_contents(str, null, false,
Gio.FileCreateFlags.REPLACE_DESTINATION, null);
}
}
)
const qtapp = new QuickText();
try {
qtapp.run([imports.system.programInvocationName]);
} catch (error) {
console.error(error);
}