From addf310ae158577ee80cd07e6daf29129f864c62 Mon Sep 17 00:00:00 2001 From: Jonatan Heyman Date: Fri, 11 Apr 2025 00:10:29 +0200 Subject: [PATCH] Add "command palette" functionality --- src/components/App.vue | 9 +++- src/components/BufferSelector.vue | 76 ++++++++++++++++++++++--------- src/editor/commands.js | 5 ++ src/editor/editor.js | 14 ++++++ src/editor/keymap.js | 1 + src/stores/heynote-store.js | 15 ++++++ 6 files changed, 96 insertions(+), 24 deletions(-) diff --git a/src/components/App.vue b/src/components/App.vue index 5a762e9..4f82b12 100644 --- a/src/components/App.vue +++ b/src/components/App.vue @@ -65,6 +65,7 @@ showCreateBuffer(value) { this.dialogWatcher(value) }, showEditBuffer(value) { this.dialogWatcher(value) }, showMoveToBufferSelector(value) { this.dialogWatcher(value) }, + showCommandPalette(value) { this.dialogWatcher(value) }, currentBufferPath() { this.focusEditor() @@ -85,10 +86,11 @@ "showCreateBuffer", "showEditBuffer", "showMoveToBufferSelector", + "showCommandPalette", ]), dialogVisible() { - return this.showLanguageSelector || this.showBufferSelector || this.showCreateBuffer || this.showEditBuffer || this.showMoveToBufferSelector + return this.showLanguageSelector || this.showBufferSelector || this.showCreateBuffer || this.showEditBuffer || this.showMoveToBufferSelector || this.showCommandPalette }, editorInert() { @@ -176,7 +178,9 @@ @close="closeDialog" /> ({ + name: cmd, + cmd: cmd, + isCommand: true, + })) + }, + orderedItems() { const sortKeys = Object.fromEntries(this.recentBufferPaths.map((item, idx) => [item, idx])) const getSortScore = (item) => sortKeys[item.path] !== undefined ? sortKeys[item.path] : 1000 @@ -74,32 +85,47 @@ }, filteredItems() { - let items - if (this.filter === "") { - items = this.orderedItems - - } else { - const searchResults = fuzzysort.go(this.filter, this.items, { - keys: ["name", "folder"], + if (this.commandsEnabled && this.filter.startsWith(">")) { + // command mode if the first character is ">" + if (this.filter.length < 2) { + return this.commands + } + const searchResults = fuzzysort.go(this.filter.slice(1), this.commands, { + keys: ["name"], }) - items = searchResults.map((result) => { + return searchResults.map((result) => { const obj = {...result.obj} const nameHighlight = result[0].highlight("", "") - const folderHighlight = result[1].highlight("", "") obj.name = nameHighlight !== "" ? nameHighlight : obj.name - obj.folder = folderHighlight !== "" ? folderHighlight : obj.folder return obj }) + } else { + let items + if (this.filter === "") { + items = this.orderedItems + } else { + const searchResults = fuzzysort.go(this.filter, this.items, { + keys: ["name", "folder"], + }) + items = searchResults.map((result) => { + const obj = {...result.obj} + const nameHighlight = result[0].highlight("", "") + const folderHighlight = result[1].highlight("", "") + obj.name = nameHighlight !== "" ? nameHighlight : obj.name + obj.folder = folderHighlight !== "" ? folderHighlight : obj.folder + return obj + }) + } + + const newNoteItem = { + name: "Create new…", + createNew:true, + } + return [ + ...items, + newNoteItem, + ] } - - const newNoteItem = { - name: "Create new…", - createNew:true, - } - return [ - ...items, - newNoteItem, - ] }, }, @@ -108,6 +134,7 @@ "updateBuffers", "editBufferMetadata", "deleteBuffer", + "executeCommand", ]), buildItems() { @@ -187,13 +214,18 @@ } else { this.$emit("openCreateBuffer", "") } + } else if (item.isCommand) { + this.$emit("close") + this.$nextTick(() => { + this.executeCommand(item.cmd) + }) } else { this.$emit("openBuffer", item.path) } }, itemHasActionButtons(item) { - return !item.createNew && item.path !== SCRATCH_FILE_NAME + return !item.createNew && item.path !== SCRATCH_FILE_NAME && !item.isCommand }, onInput(event) { @@ -248,7 +280,7 @@