From f0e299122f27bc7ba6c109e595d19b33a134bf63 Mon Sep 17 00:00:00 2001 From: Jonatan Heyman Date: Tue, 10 Sep 2024 16:22:30 +0200 Subject: [PATCH] Implement functionality for deleting notes --- electron/main/file-library.js | 12 ++++++++++++ electron/preload/index.ts | 4 ++++ src/components/NoteSelector.vue | 28 +++++++++++++++++++--------- src/editor/block/commands.js | 4 +++- src/stores/notes-store.js | 18 ++++++++++++++++-- 5 files changed, 54 insertions(+), 12 deletions(-) diff --git a/electron/main/file-library.js b/electron/main/file-library.js index 6b3649d..987b8ff 100644 --- a/electron/main/file-library.js +++ b/electron/main/file-library.js @@ -96,6 +96,14 @@ export class FileLibrary { await this.jetpack.moveAsync(fullOldPath, fullNewPath) } + async delete(path) { + if (path === SCRATCH_FILE_NAME) { + throw new Error("Can't delete scratch file") + } + const fullPath = join(this.basePath, path) + await this.jetpack.removeAsync(fullPath) + } + async getList() { //console.log("Listing notes") const notes = {} @@ -262,6 +270,10 @@ export function setupFileLibraryEventHandlers(win) { return await library.move(path, newPath) }); + ipcMain.handle('buffer:delete', async (event, path) => { + return await library.delete(path) + }); + ipcMain.handle("library:selectLocation", async () => { let result = await dialog.showOpenDialog({ title: "Select directory to store buffer", diff --git a/electron/preload/index.ts b/electron/preload/index.ts index 9e5da29..1dd2eb1 100644 --- a/electron/preload/index.ts +++ b/electron/preload/index.ts @@ -77,6 +77,10 @@ contextBridge.exposeInMainWorld("heynote", { return await ipcRenderer.invoke("buffer:save", path, content) }, + async delete(path) { + return await ipcRenderer.invoke("buffer:delete", path) + }, + async move(path, newPath) { return await ipcRenderer.invoke("buffer:move", path, newPath) }, diff --git a/src/components/NoteSelector.vue b/src/components/NoteSelector.vue index a5d6e4b..58058e6 100644 --- a/src/components/NoteSelector.vue +++ b/src/components/NoteSelector.vue @@ -22,14 +22,7 @@ await this.updateNotes() this.$refs.container.focus() this.$refs.input.focus() - this.items = Object.entries(this.notes).map(([path, metadata]) => { - return { - "path": path, - "name": metadata?.name || path, - "folder": path.split("/").slice(0, -1).join("/"), - "scratch": path === SCRATCH_FILE_NAME, - } - }) + this.buildItems() if (this.items.length > 1) { this.selected = 1 } @@ -89,8 +82,21 @@ ...mapActions(useNotesStore, [ "updateNotes", "editNote", + "deleteNote", ]), + buildItems() { + //console.log("buildItems", Object.entries(this.notes)) + this.items = Object.entries(this.notes).map(([path, metadata]) => { + return { + "path": path, + "name": metadata?.name || path, + "folder": path.split("/").slice(0, -1).join("/"), + "scratch": path === SCRATCH_FILE_NAME, + } + }) + }, + onKeydown(event) { if (this.filteredItems.length === 0) { return @@ -185,9 +191,13 @@ this.deleteConfirm = false }, - deleteConfirmNote(path) { + async deleteConfirmNote(path) { if (this.deleteConfirm) { console.log("delete file:", path) + await this.deleteNote(path) + this.hideActionButtons() + this.buildItems() + this.selected = Math.min(this.selected, this.items.length - 1) } else { this.deleteConfirm = true this.actionButton = 2 diff --git a/src/editor/block/commands.js b/src/editor/block/commands.js index 42192c3..c59ce51 100644 --- a/src/editor/block/commands.js +++ b/src/editor/block/commands.js @@ -325,8 +325,10 @@ export const deleteBlock = (editor) => ({state, dispatch}) => { const block = getActiveNoteBlock(state) const blocks = state.facet(blockState) let replace = "" + let newSelection = block.delimiter.from if (blocks.length == 1) { replace = getBlockDelimiter(editor.defaultBlockToken, editor.defaultBlockAutoDetect) + newSelection = replace.length } dispatch(state.update({ changes: { @@ -334,7 +336,7 @@ export const deleteBlock = (editor) => ({state, dispatch}) => { to: block.range.to, insert: replace, }, - selection: EditorSelection.cursor(block.delimiter.from), + selection: EditorSelection.cursor(newSelection), annotations: [heynoteEvent.of(DELETE_BLOCK)], })) } diff --git a/src/stores/notes-store.js b/src/stores/notes-store.js index 5ac1c11..5f815d8 100644 --- a/src/stores/notes-store.js +++ b/src/stores/notes-store.js @@ -109,11 +109,11 @@ export const useNotesStore = defineStore("notes", { if (this.currentEditor.path !== path) { throw new Error(`Can't update note (${path}) since it's not the active one (${this.currentEditor.path})`) } - console.log("currentEditor", this.currentEditor) + //console.log("currentEditor", this.currentEditor) toRaw(this.currentEditor).setName(name) await (toRaw(this.currentEditor)).save() if (newPath && path !== newPath) { - console.log("moving note", path, newPath) + //console.log("moving note", path, newPath) editorCacheStore.freeEditor(path) await window.heynote.buffer.move(path, newPath) this.openNote(newPath) @@ -121,6 +121,20 @@ export const useNotesStore = defineStore("notes", { } }, + async deleteNote(path) { + if (path === SCRATCH_FILE_NAME) { + throw new Error("Can't delete scratch file") + } + const editorCacheStore = useEditorCacheStore() + if (this.currentEditor.path === path) { + this.currentEditor = null + this.currentNotePath = SCRATCH_FILE_NAME + } + editorCacheStore.freeEditor(path) + await window.heynote.buffer.delete(path) + await this.updateNotes() + }, + async reloadLibrary() { const editorCacheStore = useEditorCacheStore() await this.updateNotes()