Skip to content

Commit c7ff969

Browse files
fix(ui): fix commands cursor offset and update commands list
1 parent 48ce4cc commit c7ff969

File tree

2 files changed

+38
-15
lines changed

2 files changed

+38
-15
lines changed

web/src/components/MemoEditor/Editor/CommandSuggestions.tsx

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,15 @@ const CommandSuggestions = observer(({ editorRef, editorActions, commands }: Com
2525
onAutocomplete: (cmd, word, index, actions) => {
2626
// Replace the trigger word with the command output
2727
actions.removeText(index, word.length);
28+
const initialPosition = actions.getCursorPosition();
2829
actions.insertText(cmd.run());
29-
// Position cursor if command specifies an offset
30-
if (cmd.cursorOffset) {
31-
actions.setCursorPosition(actions.getCursorPosition() + cmd.cursorOffset);
32-
}
30+
31+
const offset = cmd.cursorRange?.[0];
32+
if (typeof offset === "undefined") return;
33+
34+
const cursorPosition = initialPosition + offset;
35+
const length = cmd.cursorRange?.[1] || 0;
36+
actions.setCursorPosition(cursorPosition, cursorPosition + length);
3337
},
3438
});
3539

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,47 @@
11
export interface Command {
22
name: string;
33
run: () => string;
4-
cursorOffset?: number;
4+
// Ex.:
5+
// [4] == [4, 0] - cursor offset is 4 chars from initial position
6+
// [7, 2] - cursor offset is 7 chars and 2 next chars selected
7+
// If omitted, cursor stays in the end of the inserted string
8+
cursorRange?: number[];
59
}
610

711
export const editorCommands: Command[] = [
812
{
9-
name: "todo",
10-
run: () => "- [ ] ",
11-
cursorOffset: 6, // Places cursor after "- [ ] " to start typing task
13+
name: "code",
14+
run: () => "```js\n\n```", // JS by default as most popular (at least on github)
15+
cursorRange: [3, 2],
1216
},
1317
{
14-
name: "code",
15-
run: () => "```\n\n```",
16-
cursorOffset: 4, // Places cursor on empty line between code fences
18+
// Template from github, but with summary initially selected for better UX
19+
name: "details",
20+
run: () => "<details><summary>Details</summary>\n<p>\n\n\n</p>\n</details>",
21+
cursorRange: [18, 7],
22+
},
23+
{
24+
name: "image",
25+
run: () => "![alt text]()", // No need in URL placeholder
26+
cursorRange: [2, 8],
1727
},
1828
{
1929
name: "link",
20-
run: () => "[text](url)",
21-
cursorOffset: 1, // Places cursor after "[" to type link text
30+
run: () => "[text]()",
31+
cursorRange: [1, 4],
2232
},
2333
{
2434
name: "table",
25-
run: () => "| Header | Header |\n| ------ | ------ |\n| Cell | Cell |",
26-
cursorOffset: 1, // Places cursor after first "|" to edit first header
35+
run: () => "| Column1 | Column2 |\n| ------ | ------ |\n| Cell1 | Cell2 |",
36+
cursorRange: [2, 7],
37+
},
38+
{
39+
name: "todo",
40+
run: () => "- [ ] ",
41+
},
42+
{
43+
name: "youtube",
44+
run: () => "[![alt text](https://img.youtube.com/vi/VIDEO_ID/0.jpg)](https://www.youtube.com/watch?v=VIDEO_ID)",
45+
cursorRange: [3, 8],
2746
},
2847
];

0 commit comments

Comments
 (0)