aboutsummaryrefslogtreecommitdiffstats
path: root/desktop/modules/notes/NotesPage.qml
blob: 88382fc7b5ae8870e843d4feffc4e4494eb1b363 (plain)
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
// Copyright (C) 2026 Danilo M. <danix@danix.xyz>
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License version 2 as
// published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

import Quickshell.Io
import QtQuick
import QtQuick.Controls
import "../.."
import "Notes.js" as Notes

// The notes page: a list, and an editor over it.
//
// Save is two clicks and each click does one thing. The first writes the note
// and, when that has finished, arms the button as "Commit & push?"; the second
// commits and pushes and returns to the list. Arming the second click only
// after the write completes is what stops a quick second click from committing
// a note the first click has not written yet.
//
// Nothing is remembered between visits: leaving the editor with text that was
// not saved drops it, which is the clipboard page's own rule.
Item {
    id: page

    // The notes module: the store, the resolved paths, the writes and git.
    required property QtObject owner

    property string view: "list"          // "list" | "editor"
    property var editing: null            // the note being edited, null for new
    property bool preview: false          // editor shows rendered Markdown
    property string bodyError: ""
    // A write or git command is in flight; the buttons stand down meanwhile.
    property bool busy: false
    // "create" | "edit" | "delete" once that write has landed and the commit
    // and push are still owed. It outlives any timer, so a click after a pause
    // pushes the note that was already written instead of writing it again.
    property string awaiting: ""

    // The shared Switch writes `checked` itself, which drops the binding
    // above; StatusRow.qml documents the same and resyncs the same way.
    onPreviewChanged: prevSwitch.checked = page.preview

    function open(note) {
        page.bodyError = "";
        page.preview = note !== null;
        page.awaiting = "";
        titleField.text = note ? note.title : "";
        // Clear before the assignment below: that assignment is what retargets
        // the body FileView, and its load may land synchronously, so clearing
        // after would wipe the body it just read.
        bodyEdit.text = "";
        page.view = "editor";
        page.editing = note;
        Qt.callLater(() => titleField.forceActiveFocus());
    }

    function toList() {
        page.awaiting = "";
        page.busy = false;
        page.editing = null;
        page.bodyError = "";
        page.view = "list";
    }

    function save() {
        if (page.busy) return;
        // The note is already on disk and only the push is owed, so this click
        // pushes rather than writing a second note.
        if (page.awaiting !== "") {
            const id = page.awaiting === "create" ? page.owner.lastCreatedId : page.editing.id;
            page.busy = true;
            page.owner.commitPush(page.awaiting, id, (ok, msg) => {
                page.busy = false;
                if (!ok) { page.owner.lastError = msg; return; }
                page.toList();
            });
            return;
        }
        page.owner.lastError = "";
        page.busy = true;
        const done = (ok, msg) => {
            page.busy = false;
            if (!ok) { page.owner.lastError = msg; return; }
            // Git off: there is no second half, so this was the whole save.
            if (!page.owner.gitEnabled) { page.toList(); return; }
            page.awaiting = page.editing ? "edit" : "create";
        };
        if (page.editing) page.owner.update(page.editing.id, titleField.text, bodyEdit.text, done);
        else page.owner.create(titleField.text, bodyEdit.text, done);
    }

    function remove() {
        if (page.busy || page.editing === null) return;
        const id = page.editing.id;
        if (page.awaiting === "delete") {
            page.busy = true;
            page.owner.commitPush("delete", id, (ok, msg) => {
                page.busy = false;
                if (!ok) { page.owner.lastError = msg; return; }
                page.toList();
            });
            return;
        }
        page.owner.lastError = "";
        page.busy = true;
        page.owner.remove(id, (ok, msg) => {
            page.busy = false;
            if (!ok) { page.owner.lastError = msg; return; }
            if (!page.owner.gitEnabled) { page.toList(); return; }
            page.awaiting = "delete";
        });
    }

    function updateList() {
        if (page.busy) return;
        page.owner.lastError = "";
        page.busy = true;
        page.owner.pull((ok, msg) => {
            page.busy = false;
            if (ok) updated.restart(); else page.owner.lastError = msg;
        });
    }

    // The open note's body. Nothing watches it: it is read when the editor
    // opens and written on save.
    FileView {
        id: bodyRead
        path: (page.view === "editor" && page.editing)
            ? page.owner.bodyPath(page.editing.file) : ""
        printErrors: false
        onLoaded: { bodyEdit.text = text(); page.bodyError = ""; }
        onLoadFailed: {
            bodyEdit.text = "";
            page.bodyError = page.editing ? "the note's file is missing" : "";
        }
    }

    Timer { id: updated; interval: 2000 }

    // --- list --------------------------------------------------------------

    Item {
        id: listView
        anchors.fill: parent
        visible: page.view === "list"

        TextField {
            id: search
            anchors { top: parent.top; left: parent.left; right: newBtn.left; rightMargin: 8 }
            height: 32
            placeholderText: "Search"
            color: Theme.text
            font { family: Theme.fontFamily; pixelSize: Theme.fontSize - 2 }
            background: Rectangle {
                radius: 6
                color: Qt.alpha(Theme.surface, 0.9)
                border.width: 1
                border.color: Qt.alpha(Theme.text, 0.15)
            }
            onTextChanged: list.currentIndex = 0
        }

        Button {
            id: updateBtn
            anchors { top: parent.top; right: parent.right }
            visible: page.owner.gitEnabled
            enabled: !page.busy
            text: updated.running ? "Updated" : "Update list"
            onClicked: page.updateList()
        }

        Button {
            id: newBtn
            anchors { top: parent.top; right: updateBtn.visible ? updateBtn.left : parent.right
                      rightMargin: updateBtn.visible ? 8 : 0 }
            enabled: page.owner.ready
            text: "New note"
            onClicked: page.open(null)
        }

        Text {
            id: listEmpty
            anchors.centerIn: list
            visible: list.count === 0
            text: page.owner.ready ? "No notes" : "Loading…"
            color: Theme.subtext
            font { family: Theme.fontFamily; pixelSize: Theme.fontSize - 2 }
        }

        ListView {
            id: list
            anchors {
                top: search.bottom; topMargin: 10
                left: parent.left; right: parent.right
                bottom: listError.top; bottomMargin: 6
            }
            clip: true
            spacing: 4
            model: Notes.filter(page.owner.notes, search.text)
            boundsBehavior: Flickable.StopAtBounds
            ScrollBar.vertical: ScrollBar {}

            delegate: Rectangle {
                id: row
                required property var modelData
                required property int index

                width: list.width
                height: 36
                radius: 8
                color: rowArea.containsMouse || ListView.isCurrentItem
                     ? Qt.alpha(Theme.accent, 0.18) : Qt.alpha(Theme.surface, 0.35)

                MouseArea {
                    id: rowArea
                    anchors.fill: parent
                    hoverEnabled: true
                    cursorShape: Qt.PointingHandCursor
                    onClicked: page.open(row.modelData)
                }

                Text {
                    anchors {
                        left: parent.left; leftMargin: 10
                        right: when.left; rightMargin: 8
                        verticalCenter: parent.verticalCenter
                    }
                    elide: Text.ElideRight
                    maximumLineCount: 1
                    textFormat: Text.PlainText
                    text: row.modelData.title
                    color: Theme.text
                    font { family: Theme.fontFamily; pixelSize: Theme.fontSize - 2 }
                }

                Text {
                    id: when
                    anchors { right: parent.right; rightMargin: 10; verticalCenter: parent.verticalCenter }
                    text: Notes.formatDate(row.modelData.file)
                    color: Theme.overlay
                    font { family: Theme.fontFamily; pixelSize: Theme.fontSize - 3 }
                }
            }
        }

        Text {
            id: listError
            anchors { left: parent.left; right: parent.right; bottom: parent.bottom }
            height: visible ? implicitHeight : 0
            visible: page.owner.lastError !== ""
            text: page.owner.lastError
            wrapMode: Text.WordWrap
            color: Theme.red
            font { family: Theme.fontFamily; pixelSize: Theme.fontSize - 3 }
        }
    }

    // --- editor ------------------------------------------------------------

    Item {
        id: editorView
        anchors.fill: parent
        visible: page.view === "editor"

        Button {
            id: cancelBtn
            anchors { top: parent.top; left: parent.left }
            text: "Cancel"
            onClicked: page.toList()
        }

        Button {
            id: saveBtn
            anchors { top: parent.top; right: parent.right }
            enabled: !page.busy
            text: page.busy ? "Saving…"
                : page.awaiting !== "" ? "Commit & push?" : "Save"
            onClicked: page.save()
        }

        Button {
            id: delBtn
            anchors { top: parent.top; right: saveBtn.left; rightMargin: 8 }
            visible: page.editing !== null
            danger: true
            enabled: !page.busy
            text: page.awaiting === "delete" ? "Confirm?" : "Delete"
            onClicked: page.remove()
        }

        // Edit (raw Markdown) on the left, Preview (rendered) on the right.
        // The switch that owns the state is Switch.qml, as elsewhere.
        Item {
            id: previewToggle
            anchors {
                top: parent.top; right: delBtn.visible ? delBtn.left : saveBtn.left
                rightMargin: 12
            }
            width: prevLabel.implicitWidth + 8 + prevSwitch.implicitWidth
            height: Math.max(prevLabel.implicitHeight, prevSwitch.implicitHeight)

            Text {
                id: prevLabel
                anchors { left: parent.left; verticalCenter: parent.verticalCenter }
                text: "Preview"
                color: Theme.subtext
                font { family: Theme.fontFamily; pixelSize: Theme.fontSize - 3 }
            }

            Switch {
                id: prevSwitch
                anchors { right: parent.right; verticalCenter: parent.verticalCenter }
                checked: page.preview
                onToggled: page.preview = checked
            }
        }

        TextField {
            id: titleField
            anchors { top: parent.top; topMargin: 44; left: parent.left; right: parent.right }
            height: 32
            placeholderText: "Title"
            color: Theme.text
            font { family: Theme.fontFamily; pixelSize: Theme.fontSize - 1 }
            background: Rectangle {
                radius: 6
                color: Qt.alpha(Theme.surface, 0.6)
                border.width: 1
                border.color: Qt.alpha(Theme.text, 0.15)
            }
        }

        Text {
            id: bodyErr
            anchors { top: titleField.bottom; topMargin: 6; left: parent.left; right: parent.right }
            height: visible ? implicitHeight : 0
            visible: page.bodyError !== ""
            text: page.bodyError
            color: Theme.yellow
            font { family: Theme.fontFamily; pixelSize: Theme.fontSize - 3 }
        }

        TextEdit {
            id: bodyEdit
            anchors {
                top: bodyErr.bottom; topMargin: 6
                left: parent.left; right: parent.right
                bottom: editError.top; bottomMargin: 6
            }
            readOnly: page.preview
            selectByMouse: true
            textFormat: page.preview ? TextEdit.MarkdownText : TextEdit.PlainText
            wrapMode: TextEdit.Wrap
            color: Theme.text
            selectionColor: Qt.alpha(Theme.accent, 0.4)
            font { family: Theme.fontFamily; pixelSize: Theme.fontSize - 2 }
            // Web and mail links only, as Segments.qml does: a note is the
            // user's own, but a file: or custom scheme link would still reach
            // xdg-open.
            onLinkActivated: link => { if (/^(https?|mailto):/i.test(link)) Qt.openUrlExternally(link); }
            HoverHandler { cursorShape: bodyEdit.hoveredLink ? Qt.PointingHandCursor : Qt.IBeamCursor }
        }

        Text {
            id: editError
            anchors { left: parent.left; right: parent.right; bottom: parent.bottom }
            height: visible ? implicitHeight : 0
            visible: page.owner.lastError !== ""
            text: page.owner.lastError
            wrapMode: Text.WordWrap
            color: Theme.red
            font { family: Theme.fontFamily; pixelSize: Theme.fontSize - 3 }
        }
    }
}