From 801cc1e9db6f85c076ca4a3be6524e4f7ccd35cb Mon Sep 17 00:00:00 2001 From: Tobias Waslowski Date: Thu, 7 Aug 2025 09:02:14 +0200 Subject: [PATCH] feat(commands): enable cursor offsetting after running command also add additional commands --- .../MemoEditor/Editor/CommandSuggestions.tsx | 3 +++ .../components/MemoEditor/Editor/commands.ts | 26 +++++++++++++++++++ .../components/MemoEditor/types/command.ts | 1 + 3 files changed, 30 insertions(+) diff --git a/web/src/components/MemoEditor/Editor/CommandSuggestions.tsx b/web/src/components/MemoEditor/Editor/CommandSuggestions.tsx index 9df688b8c..200d7267d 100644 --- a/web/src/components/MemoEditor/Editor/CommandSuggestions.tsx +++ b/web/src/components/MemoEditor/Editor/CommandSuggestions.tsx @@ -49,6 +49,9 @@ const CommandSuggestions = observer(({ editorRef, editorActions, commands }: Pro const [word, index] = getCurrentWord(); editorActions.current.removeText(index, word.length); editorActions.current.insertText(cmd.run()); + if (cmd.cursorOffset) { + editorActions.current.setCursorPosition(cmd.cursorOffset); + } hide(); }; diff --git a/web/src/components/MemoEditor/Editor/commands.ts b/web/src/components/MemoEditor/Editor/commands.ts index 8575c9ef9..766bbf4ee 100644 --- a/web/src/components/MemoEditor/Editor/commands.ts +++ b/web/src/components/MemoEditor/Editor/commands.ts @@ -5,10 +5,36 @@ export const editorCommands: Command[] = [ name: "todo", description: "Insert a task checkbox", run: () => "- [ ] ", + cursorOffset: 6, }, { name: "code", description: "Insert a code block", run: () => "```\n\n```", + cursorOffset: 4, + }, + { + name: "link", + description: "Insert a link", + run: () => "[text](url)", + cursorOffset: 1, + }, + { + name: "table-2", + description: "Insert a table", + run: () => "| Header | Header |\n| ------ | ------ |\n| Cell | Cell |", + cursorOffset: 1, + }, + { + name: "table-3", + description: "Insert a table", + run: () => "| Header | Header | Header |\n| ------ | ------ | ------ |\n| Cell | Cell | Cell |", + cursorOffset: 1, + }, + { + name: "highlight", + description: "Insert highlighted text", + run: () => "==text==", + cursorOffset: 2, }, ]; diff --git a/web/src/components/MemoEditor/types/command.ts b/web/src/components/MemoEditor/types/command.ts index 419313923..8d472506c 100644 --- a/web/src/components/MemoEditor/types/command.ts +++ b/web/src/components/MemoEditor/types/command.ts @@ -2,4 +2,5 @@ export type Command = { name: string; description?: string; run: () => string; + cursorOffset?: number; };