-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPanel.qml
More file actions
1330 lines (1189 loc) · 48 KB
/
Copy pathPanel.qml
File metadata and controls
1330 lines (1189 loc) · 48 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
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import QtQuick
import QtQuick.Controls
import Quickshell
import Quickshell.Io
import qs.Commons
import qs.Ui
import "Vault.js" as Vault
import "EditorMutations.js" as EditorMutations
Item {
id: root
property var shell: null
property var manifest: null
property var service: null
property var settings: ({})
property bool opened: false
property string moduleName: "harbefas.vault"
property string currentPath: ""
property string currentTitle: ""
property string draft: ""
property bool editing: false
property bool dirty: false
property string status: ""
property bool shortcutHintsActive: false
property int listIndex: -1
property int heldModifierFlags: 0
property string focusRegion: "search"
// One hue, two weights. Anything secondary is the same foreground at lower
// opacity — never a second colour.
readonly property color foreground: Color.foreground
readonly property color secondary: Util.alpha(foreground, 0.55)
readonly property bool hintCtrlHeld: (heldModifierFlags & Qt.ControlModifier) !== 0
readonly property bool hintShiftHeld: (heldModifierFlags & Qt.ShiftModifier) !== 0
readonly property bool hintAltHeld: (heldModifierFlags & Qt.AltModifier) !== 0
component KeyHint: ShortcutHint {
ctrlHeld: root.hintCtrlHeld
shiftHeld: root.hintShiftHeld
altHeld: root.hintAltHeld
active: root.shortcutHintsActive
foreground: root.foreground
}
// Large notes can make Qt's MarkdownText take a moment to lay out. The reader
// still renders the full note; this threshold only decides when to show a
// small performance warning.
readonly property int largeNoteThreshold: 200000
readonly property bool largeNote: draft.length > largeNoteThreshold
readonly property var notes: service ? service.notes : []
readonly property bool vaultConfigured: service && service.vaultConfigured
// Frontmatter is lifted out of the prose and drawn as a header; wikilinks
// become anchors so they can be followed. These are refreshed manually so a
// large note is not reparsed and re-rendered on every editor keystroke.
property var parsed: ({ fields: {}, body: "" })
property var noteTags: []
property string noteFolder: ""
property int noteWords: 0
property int noteMinutes: 0
property string noteMeta: ""
property string rendered: ""
property string pendingRenderBody: ""
property var blocks: []
// ------------------------------------------------------------- lifecycle
function open(payloadJson) {
var payload = ({})
try { if (payloadJson) payload = JSON.parse(payloadJson) } catch (e) {}
var targeted = payload.focus === "reader"
|| (typeof payload.path === "string" && payload.path !== "")
if (service) {
if (payload.settings && typeof payload.settings === "object")
settings = payload.settings
service.applySettings(payload.settings)
service.refresh()
}
shortcutHintsActive = payload.shortcutLatch !== false
opened = true
focusRegion = targeted ? "reader" : "search"
if (typeof payload.path === "string" && payload.path !== "")
selectPath(payload.path)
else if (currentPath === "" && notes.length > 0) selectPath(notes[0].path)
if (payload.action === "new") startCreate()
else if (payload.action === "daily") openDaily()
Qt.callLater(function() {
if (payload.action === "new" || payload.action === "daily") return
if (targeted) focusReader()
else focusSearch()
})
}
function close() {
// A close from the host must not drop an unsaved edit on the floor.
if (dirty) saveDraft()
heldModifierFlags = 0
opened = false
}
function requestClose() {
if (shell && typeof shell.hide === "function") shell.hide("harbefas.vault")
else close()
}
onNotesChanged: {
if (currentPath === "" && notes.length > 0) selectPath(notes[0].path)
ensureListSelection()
}
onCurrentPathChanged: ensureListSelection()
// The editor takes the draft on entry rather than binding to it: the binding
// would be broken by the first keystroke anyway, since typing writes back
// into draft.
onEditingChanged: {
if (editing) {
renderTimer.stop()
editor.text = draft
} else {
refreshReader(true)
Qt.callLater(function() { focusReader() })
}
}
// ------------------------------------------------------------------ notes
function selectPath(path) {
if (!path || path === currentPath) return
if (dirty) saveDraft()
currentPath = path
currentTitle = Vault.noteTitle(path)
externalChanged = false
externalText = ""
editing = false
clearReader()
noteFile.path = path
noteFile.reload()
ensureListSelection()
}
FileView {
id: noteFile
watchChanges: true
printErrors: false
onLoaded: {
// A change on disk while editing would silently discard the draft, so an
// in-progress edit keeps what the user typed and the conflict is handed
// to them instead. The vault commits itself every minute, so this fires
// for real: another editor, a sync, a script.
if (root.editing && root.dirty) {
var onDisk = text()
if (onDisk !== root.draft) {
root.externalText = onDisk
root.externalChanged = true
}
return
}
root.externalChanged = false
root.draft = text()
if (root.editing) editor.text = root.draft
root.dirty = false
root.refreshReader(!root.editing)
if (root.focusRegion === "reader")
Qt.callLater(function() { root.focusReader() })
}
onLoadFailed: {
root.draft = ""
root.clearReader()
root.status = "Could not read the note."
}
}
property bool externalChanged: false
property string externalText: ""
function reloadExternal() {
externalChanged = false
draft = externalText
editor.text = draft
dirty = false
refreshReader(!editing)
status = "Reloaded from disk"
statusTimer.restart()
}
function keepDraft() {
externalChanged = false
externalText = ""
// The draft is still dirty, so the next save wins over the disk copy.
autosave.restart()
}
function saveDraft() {
if (!dirty || currentPath === "") return
// An unresolved conflict must not be overwritten by the autosave timer.
if (externalChanged) return
noteFile.setText(draft)
dirty = false
status = "Saved"
statusTimer.restart()
}
Timer {
id: statusTimer
interval: 2000
onTriggered: root.status = ""
}
// Autosave keeps the vault's own minute-by-minute git commits meaningful
// without needing Ctrl+S after every keystroke.
Timer {
id: autosave
interval: 1500
onTriggered: root.saveDraft()
}
Timer {
id: renderTimer
interval: root.largeNote ? 250 : 20
repeat: false
onTriggered: root.rendered = Vault.renderMarkdown(root.pendingRenderBody,
String(root.foreground))
}
function clearReader() {
renderTimer.stop()
parsed = ({ fields: {}, body: "" })
noteTags = []
noteFolder = ""
noteWords = 0
noteMinutes = 0
noteMeta = ""
pendingRenderBody = ""
rendered = ""
blocks = []
}
function refreshReader(renderBody) {
var nextParsed = Vault.splitFrontmatter(draft)
parsed = nextParsed
noteTags = Vault.frontmatterTags(nextParsed.fields)
noteFolder = service ? Vault.relativePath(currentPath, service.resolvedVault) : ""
noteWords = Vault.wordCount(nextParsed.body)
noteMinutes = Vault.readingMinutes(noteWords)
noteMeta = Vault.noteMeta(noteFolder, noteWords, noteMinutes)
blocks = Vault.parseMarkdownBlocks(nextParsed.body, String(root.foreground))
if (renderBody === false) return
pendingRenderBody = nextParsed.body
renderTimer.restart()
}
// A vault:// link is a wikilink; anything else belongs to the browser. An
// unresolved name is reported instead of failing silently, since a broken
// link usually means the note has not been written yet.
function followLink(link) {
var name = Vault.wikilinkTarget(link)
if (name === "") { Qt.openUrlExternally(link); return }
var path = service ? service.resolveNote(name) : ""
if (path !== "") { selectPath(path); return }
status = "Note not found: " + name
statusTimer.restart()
}
function noteIndex(path) {
for (var i = 0; i < notes.length; i++) {
if (notes[i].path === path) return i
}
return -1
}
function ensureListSelection() {
if (!notes.length) {
listIndex = -1
return
}
if (listIndex >= 0 && listIndex < notes.length) return
var current = noteIndex(currentPath)
if (current >= 0) {
listIndex = current
return
}
if (listIndex < 0) listIndex = 0
if (listIndex >= notes.length) listIndex = notes.length - 1
}
function moveListSelection(delta) {
if (!notes.length) return
ensureListSelection()
listIndex = (listIndex + (delta < 0 ? -1 : 1) + notes.length) % notes.length
noteList.currentIndex = listIndex
noteList.positionViewAtIndex(listIndex, ListView.Contain)
noteList.forceActiveFocus()
}
function focusSearch() {
noteList.focus = false
keyScope.focus = false
focusRegion = "search"
searchField.forceActiveFocus()
}
function focusList() {
ensureListSelection()
searchField.focus = false
editor.focus = false
keyScope.focus = false
focusRegion = "list"
noteList.currentIndex = listIndex
noteList.forceActiveFocus()
}
function focusFirstNote() {
if (!notes.length) {
focusList()
return
}
listIndex = 0
noteList.positionViewAtIndex(0, ListView.Contain)
focusList()
}
function focusReader() {
searchField.focus = false
editor.focus = false
noteList.focus = false
focusRegion = "reader"
keyScope.forceActiveFocus()
}
function readerFlickable() {
return readerScroll
}
function readerScrollTo(value) {
var flickable = readerFlickable()
if (flickable && flickable.contentY !== undefined) {
var minimum = Number(flickable.originY) || 0
var maximum = Math.max(minimum,
minimum + Math.max(0, Number(flickable.contentHeight) || 0)
- Math.max(0, Number(flickable.height) || 0))
var next = Math.max(minimum, Math.min(maximum, Number(value) || 0))
if (typeof flickable.cancelFlick === "function") flickable.cancelFlick()
var changed = Math.abs(flickable.contentY - next) > 0.01
flickable.contentY = next
return changed
}
var scrollbar = readerScroll.ScrollBar.vertical
if (!scrollbar) return false
var barMaximum = Math.max(0, 1 - (Number(scrollbar.size) || 0))
var barNext = Math.max(0, Math.min(barMaximum, Number(value) || 0))
var barChanged = Math.abs(scrollbar.position - barNext) > 0.001
scrollbar.position = barNext
return barChanged
}
function moveReader(delta) {
var flickable = readerFlickable()
if (flickable && flickable.contentY !== undefined)
return readerScrollTo(flickable.contentY + delta)
var scrollbar = readerScroll.ScrollBar.vertical
if (!scrollbar) return false
var step = Math.max(0.025, (Number(scrollbar.size) || 0.1)
* (Math.abs(delta) > 100 ? 0.86 : 0.12))
return readerScrollTo(scrollbar.position + (delta < 0 ? -step : step))
}
function navigateReader(event) {
if (editing || focusRegion !== "reader" || currentPath === "") return false
var plain = (event.modifiers & (Qt.ControlModifier | Qt.ShiftModifier
| Qt.AltModifier | Qt.MetaModifier)) === 0
if (!plain) return false
var flickable = readerFlickable()
if (!flickable) return false
var page = Math.max(1, Number(flickable.height) || 0) * 0.86
var step = Math.max(24, Style.space(5))
if (event.key === Qt.Key_Down || String(event.text || "").toLowerCase() === "j")
return moveReader(step)
if (event.key === Qt.Key_Up || String(event.text || "").toLowerCase() === "k")
return moveReader(-step)
if (event.key === Qt.Key_PageDown) return moveReader(page)
if (event.key === Qt.Key_PageUp) return moveReader(-page)
if (event.key === Qt.Key_Home) return readerScrollTo(0)
if (event.key === Qt.Key_End)
return readerScrollTo((Number(flickable.contentHeight) || 0)
- (Number(flickable.height) || 0))
return false
}
function selectListSelection() {
ensureListSelection()
if (listIndex >= 0 && listIndex < notes.length)
selectPath(notes[listIndex].path)
focusReader()
}
function textInputFocused() {
return searchField.activeFocus || editor.activeFocus || newNoteField.activeFocus
}
function activateSearch() {
focusSearch()
searchField.selectAll()
}
function noteHeldModifiers(event, pressed) {
var reported = Number(event.modifiers) || 0
var changed = hintModifierFlag(event.key)
if (changed !== 0)
heldModifierFlags = pressed ? (reported | changed) : (reported & ~changed)
else if (pressed)
heldModifierFlags = reported
}
function isModifierKey(key) {
return key === Qt.Key_Control || key === Qt.Key_Shift
|| key === Qt.Key_Alt || key === Qt.Key_AltGr || key === Qt.Key_Meta
}
function isHintModifierKey(key) {
return key === Qt.Key_Control || key === Qt.Key_Shift
|| key === Qt.Key_Alt || key === Qt.Key_AltGr
}
function hintModifierFlag(key) {
if (key === Qt.Key_Control) return Qt.ControlModifier
if (key === Qt.Key_Shift) return Qt.ShiftModifier
if (key === Qt.Key_Alt || key === Qt.Key_AltGr) return Qt.AltModifier
return 0
}
function toggleEditing() {
if (currentPath === "") return
if (editing) saveDraft()
editing = !editing
if (editing) Qt.callLater(function() { editor.forceActiveFocus() })
}
function saveVaultPath(path) {
var value = String(path || "").trim()
if (!value || !root.service) return
var entry = { id: root.moduleName }
for (var key in root.settings) if (key !== "id") entry[key] = root.settings[key]
entry.vaultPath = value
root.settings = entry
root.service.applySettings(entry)
if (root.bar && root.bar.shell
&& typeof root.bar.shell.updateEntryInline === "function")
root.bar.shell.updateEntryInline(root.moduleName, entry)
root.service.refresh()
}
// ------------------------------------------------------------ new notes
property bool creating: false
property string pendingCreate: ""
function startCreate() {
if (!vaultConfigured) return
creating = true
}
function cancelCreate() {
creating = false
focusList()
}
function submitCreate(name) {
var path = Vault.newNotePath(service ? service.resolvedVault : "", name)
if (path === "") return
creating = false
createNote(path, Vault.noteTemplate(Vault.noteTitle(path)))
}
function openDaily() {
if (!service || !vaultConfigured) return
createNote(service.dailyPath(), Vault.dailyTemplate(new Date()))
}
// A shell does the work so the parent folders come along, and an existing
// note is opened untouched instead of being overwritten by the template.
function createNote(path, template) {
if (path === "" || createProcess.running) return
pendingCreate = path
createProcess.command = ["sh", "-c",
'mkdir -p "$(dirname "$1")" && { [ -e "$1" ] || printf %s "$2" > "$1"; }',
"sh", path, template]
createProcess.running = true
}
Process {
id: createProcess
running: false
onExited: function(exitCode) {
var path = root.pendingCreate
root.pendingCreate = ""
if (exitCode !== 0 || path === "") {
root.status = "Could not create the note."
statusTimer.restart()
return
}
if (root.service) root.service.refresh()
root.selectPath(path)
root.editing = true
Qt.callLater(function() { editor.forceActiveFocus() })
}
}
function openExternal() {
if (currentPath === "") return
if (dirty) saveDraft()
Quickshell.execDetached(["xdg-open", currentPath])
status = "Opening note"
statusTimer.restart()
}
// ------------------------------------------------------------------- IPC
// ------------------------------------------------------------------- view
FloatingWindow {
id: window
visible: root.opened
title: "Vault"
color: Color.background
implicitWidth: 1100
implicitHeight: 650
minimumSize: Qt.size(720, 480)
onVisibleChanged: {
if (!visible && root.opened) root.requestClose()
else if (visible && root.opened && root.focusRegion === "reader")
Qt.callLater(function() { root.focusReader() })
}
FocusScope {
id: keyScope
anchors.fill: parent
focus: true
Keys.priority: Keys.BeforeItem
Keys.onShortcutOverride: function(event) {
if (root.textInputFocused()) return
if (event.key === Qt.Key_Return || event.key === Qt.Key_Enter
|| event.key === Qt.Key_Tab || event.key === Qt.Key_Backtab
|| event.key === Qt.Key_Escape || event.key === Qt.Key_Up
|| event.key === Qt.Key_Down || event.key === Qt.Key_PageUp
|| event.key === Qt.Key_PageDown || event.key === Qt.Key_Home
|| event.key === Qt.Key_End
|| String(event.text || "").toLowerCase() === "j"
|| String(event.text || "").toLowerCase() === "k")
event.accepted = true
}
Keys.onPressed: function(event) {
root.noteHeldModifiers(event, true)
if (root.isModifierKey(event.key)) {
if (root.isHintModifierKey(event.key)) root.shortcutHintsActive = true
event.accepted = true
return
}
var ctrl = (event.modifiers & Qt.ControlModifier) !== 0
var shift = (event.modifiers & Qt.ShiftModifier) !== 0
var alt = (event.modifiers & Qt.AltModifier) !== 0
var plain = !ctrl && !shift && !alt
var text = String(event.text || "").toLowerCase()
if (event.key === Qt.Key_N && ctrl) {
root.startCreate()
event.accepted = true
} else if (event.key === Qt.Key_D && ctrl) {
root.openDaily()
event.accepted = true
} else if (plain && !root.textInputFocused() && text === "n") {
root.startCreate()
event.accepted = true
} else if (plain && !root.textInputFocused() && text === "d") {
root.openDaily()
event.accepted = true
} else if (event.key === Qt.Key_S && ctrl) {
root.saveDraft()
event.accepted = true
} else if (event.key === Qt.Key_E && ctrl) {
root.toggleEditing()
event.accepted = true
} else if (event.key === Qt.Key_O && ctrl) {
root.openExternal()
event.accepted = true
} else if (plain && !root.textInputFocused() && text === "o") {
root.openExternal()
event.accepted = true
} else if (event.key === Qt.Key_F && ctrl) {
root.activateSearch()
event.accepted = true
} else if (event.key === Qt.Key_Slash && ctrl) {
root.shortcutHintsActive = !root.shortcutHintsActive
event.accepted = true
} else if (plain && !root.textInputFocused()
&& (event.key === Qt.Key_Slash || text === "/")) {
root.activateSearch()
event.accepted = true
} else if (plain && !root.textInputFocused() && text === "e") {
root.toggleEditing()
event.accepted = true
} else if (plain && !root.textInputFocused() && text === "q") {
root.requestClose()
event.accepted = true
} else if (plain && event.key === Qt.Key_Tab
&& root.focusRegion === "reader") {
root.focusList()
event.accepted = true
} else if (root.navigateReader(event)) {
event.accepted = true
} else if (event.key === Qt.Key_Escape) {
if (root.creating) root.cancelCreate()
else if (root.editing) { root.saveDraft(); root.editing = false }
else if (searchField.activeFocus) root.focusList()
else if (root.focusRegion === "reader") root.focusFirstNote()
else root.requestClose()
event.accepted = true
}
}
Keys.onReleased: function(event) {
root.noteHeldModifiers(event, false)
if (root.isHintModifierKey(event.key)) event.accepted = true
}
Rectangle {
anchors.fill: parent
color: Color.background
visible: !root.vaultConfigured
z: 20
VaultSetup {
anchors.centerIn: parent
width: Math.min(parent.width - Style.space(48), Style.space(620))
foreground: root.foreground
onSubmitted: function(path) { root.saveVaultPath(path) }
}
}
Column {
anchors.fill: parent
anchors.margins: Style.space(14)
spacing: Style.space(10)
// ---- header: search and daily note
Row {
id: header
width: parent.width
spacing: Style.space(8)
TextField {
id: searchField
width: parent.width
foreground: root.foreground
placeholderText: "Search vault…"
onTextChanged: if (root.service) root.service.setQuery(text)
Keys.priority: Keys.BeforeItem
Keys.onPressed: function(event) {
root.noteHeldModifiers(event, true)
if (root.isModifierKey(event.key)) {
if (root.isHintModifierKey(event.key)) root.shortcutHintsActive = true
event.accepted = true
return
}
var ctrl = (event.modifiers & Qt.ControlModifier) !== 0
if (event.key === Qt.Key_Escape) {
text = ""
root.focusList()
event.accepted = true
} else if (event.key === Qt.Key_Down) {
root.focusList()
event.accepted = true
} else if (event.key === Qt.Key_Enter
|| event.key === Qt.Key_Return) {
root.selectListSelection()
event.accepted = true
} else if (event.key === Qt.Key_Slash && ctrl) {
root.shortcutHintsActive = !root.shortcutHintsActive
event.accepted = true
}
}
Keys.onReleased: function(event) {
root.noteHeldModifiers(event, false)
if (root.isHintModifierKey(event.key)) event.accepted = true
}
KeyHint {
sequences: ["/"]
showWash: searchField.activeFocus
}
}
}
// ---- body: note list + reader/editor
Row {
width: parent.width
height: parent.height - header.height - footer.height - parent.spacing * 2
spacing: Style.space(12)
BorderSurface {
width: Style.space(280)
height: parent.height
// Rectangle defaults to white; both surfaces take the kit's fill
// and border so they sit on the theme instead of punching a hole.
color: Style.normalFillFor(root.foreground, root.foreground)
borderSpec: Border.controlSpec("normal", root.foreground, root.foreground)
ListView {
id: noteList
anchors.fill: parent
anchors.margins: Style.space(4)
clip: true
model: root.notes
currentIndex: root.listIndex
// Navigation is handled below so Qt cannot advance twice.
keyNavigationEnabled: false
onCurrentIndexChanged: root.listIndex = currentIndex
delegate: NoteRow {
required property var modelData
required property int index
width: noteList.width
foreground: root.foreground
title: modelData.title
folder: modelData.folder
age: Vault.relativeTime(modelData.mtime, Date.now() / 1000)
selected: noteList.activeFocus
? index === root.listIndex
: modelData.path === root.currentPath
onPressed: {
root.listIndex = index
noteList.forceActiveFocus()
}
onActivated: {
root.selectPath(modelData.path)
root.focusReader()
}
KeyHint {
sequences: ["Enter"]
navHint: "J/K"
active: root.shortcutHintsActive
&& noteList.activeFocus && index === root.listIndex
showWash: false
}
}
Keys.onPressed: function(event) {
root.noteHeldModifiers(event, true)
if (root.isModifierKey(event.key)) {
if (root.isHintModifierKey(event.key)) root.shortcutHintsActive = true
event.accepted = true
return
}
var ctrl = (event.modifiers & Qt.ControlModifier) !== 0
var shift = (event.modifiers & Qt.ShiftModifier) !== 0
var alt = (event.modifiers & Qt.AltModifier) !== 0
var plain = !ctrl && !shift && !alt
var text = String(event.text || "").toLowerCase()
if (event.key === Qt.Key_Return || event.key === Qt.Key_Enter) {
root.selectListSelection()
} else if (event.key === Qt.Key_Down || (plain && text === "j")) {
root.moveListSelection(1)
} else if (event.key === Qt.Key_Up || (plain && text === "k")) {
root.moveListSelection(-1)
} else if (event.key === Qt.Key_Tab) {
root.focusReader()
} else if (event.key === Qt.Key_Home) {
root.listIndex = 0
noteList.positionViewAtIndex(0, ListView.Contain)
} else if (event.key === Qt.Key_End) {
root.listIndex = Math.max(0, root.notes.length - 1)
noteList.positionViewAtIndex(root.listIndex, ListView.Contain)
} else if (event.key === Qt.Key_Escape) {
root.requestClose()
} else return
event.accepted = true
}
Keys.onReleased: function(event) {
root.noteHeldModifiers(event, false)
if (root.isHintModifierKey(event.key)) event.accepted = true
}
}
Text {
readonly property string readError:
root.service && root.service.error ? root.service.error : ""
anchors.centerIn: parent
width: parent.width - Style.space(24)
horizontalAlignment: Text.AlignHCenter
wrapMode: Text.WordWrap
visible: root.notes.length === 0
// An empty vault and a vault that could not be read looked
// exactly alike before.
text: readError !== "" ? "Could not read the vault.\n" + readError
: (root.service && root.service.searching ? "Searching…" : "No notes")
textFormat: Text.PlainText
color: root.secondary
font.family: Style.font.family
font.pixelSize: Style.font.bodySmall
}
}
BorderSurface {
width: parent.width - Style.space(280) - parent.spacing
height: parent.height
color: Style.normalFillFor(root.foreground, root.foreground)
borderSpec: Border.controlSpec("normal", root.foreground, root.foreground)
Column {
anchors.fill: parent
anchors.margins: Style.space(12)
spacing: Style.space(8)
Row {
id: noteHeader
width: parent.width
height: Math.max(titleBlock.implicitHeight, openFileButton.implicitHeight,
hintButton.implicitHeight, modeButton.implicitHeight,
newButton.implicitHeight, dailyButton.implicitHeight)
spacing: Style.space(8)
Column {
id: titleBlock
width: parent.width - openFileButton.width - hintButton.width
- modeButton.width - newButton.width - dailyButton.width
- parent.spacing * 5
anchors.verticalCenter: parent.verticalCenter
spacing: Style.space(1)
Text {
width: parent.width
text: root.currentTitle !== "" ? root.currentTitle : "No note open"
textFormat: Text.PlainText
color: root.foreground
font.family: Style.font.family
font.pixelSize: Style.font.title
font.bold: true
elide: Text.ElideRight
}
Text {
width: parent.width
text: root.noteMeta
textFormat: Text.PlainText
visible: root.noteMeta !== ""
color: root.secondary
font.family: Style.font.family
font.pixelSize: Style.font.caption
elide: Text.ElideRight
}
}
Button {
id: newButton
text: "New"
foreground: root.foreground
enabled: root.vaultConfigured
tooltipText: "Create a note · N or Ctrl+N"
onClicked: root.startCreate()
KeyHint { sequences: ["N", "Ctrl+N"] }
}
Button {
id: dailyButton
text: "Today"
foreground: root.foreground
enabled: root.vaultConfigured
tooltipText: "Open today's daily note · D or Ctrl+D"
onClicked: root.openDaily()
KeyHint { sequences: ["D", "Ctrl+D"] }
}
Button {
id: openFileButton
text: "File"
foreground: root.foreground
enabled: root.currentPath !== ""
tooltipText: "Open note in the default app · O or Ctrl+O"
onClicked: root.openExternal()
KeyHint { sequences: ["O", "Ctrl+O"] }
}
Button {
id: hintButton
text: "?"
foreground: root.foreground
tooltipText: "Show shortcuts · Ctrl+/"
onClicked: root.shortcutHintsActive = !root.shortcutHintsActive
KeyHint { sequences: ["Ctrl+/"] }
}
Button {
id: modeButton
text: root.editing ? "Read" : "Edit"
foreground: root.foreground
// Large notes can be slower in TextArea, but editing uses the
// complete draft.
enabled: root.currentPath !== ""
tooltipText: root.editing
? "Return to reading · E or Ctrl+E"
: "Edit the full Markdown note · E or Ctrl+E"
onClicked: root.toggleEditing()
KeyHint { sequences: ["E", "Ctrl+E"] }
}
}
// Name prompt for a new note. A `/` in the name creates the
// folders, so `Pessoal/Ideias` works without leaving the panel.
TextField {
id: newNoteField
width: parent.width
visible: root.creating
placeholderText: "New note name — Folder/Name creates the folder"
foreground: root.foreground
onAccepted: root.submitCreate(text)
onVisibleChanged: {
if (!visible) return
text = ""
Qt.callLater(function() { newNoteField.forceActiveFocus() })
}
Keys.onEscapePressed: root.cancelCreate()
}
// Frontmatter tags, drawn as chips instead of raw YAML.
Flow {
id: tagRow
width: parent.width
visible: root.noteTags.length > 0
spacing: Style.space(6)
Repeater {
model: root.noteTags
BorderSurface {
required property string modelData
implicitWidth: tagText.implicitWidth + Style.space(14)
implicitHeight: tagText.implicitHeight + Style.space(6)
color: Util.alpha(root.foreground, 0.06)
borderSpec: Border.controlSpec("normal", root.foreground, root.foreground)
Text {
id: tagText
anchors.centerIn: parent
text: "#" + parent.modelData
textFormat: Text.PlainText
color: root.secondary
font.family: Style.font.family
font.pixelSize: Style.font.caption
}
}
}
}
PanelSeparator { width: parent.width }
Text {
id: oversizedNotice
width: parent.width
visible: root.largeNote
text: "Large note (" + Math.round(root.draft.length / 1024)
+ " KB). Rendering the full note may take longer to read or edit."
textFormat: Text.PlainText
wrapMode: Text.Wrap
color: root.secondary
font.family: Style.font.family
font.pixelSize: Style.font.caption
}
// The note changed on disk under an unsaved draft. Autosave is
// held until this is answered, so neither copy is lost.
Row {
id: conflictRow
width: parent.width
visible: root.externalChanged
spacing: Style.space(8)
Text {
width: parent.width - reloadButton.width - keepButton.width
- parent.spacing * 2