|
|
|
|
@ -1,21 +1,26 @@
|
|
|
|
|
import type { SlashCommandsProps } from "../types";
|
|
|
|
|
import type { EditorRefActions } from ".";
|
|
|
|
|
import { SuggestionsPopup } from "./SuggestionsPopup";
|
|
|
|
|
import { useSuggestions } from "./useSuggestions";
|
|
|
|
|
|
|
|
|
|
const SlashCommands = ({ editorRef, editorActions, commands }: SlashCommandsProps) => {
|
|
|
|
|
const handleCommandAutocomplete = (cmd: (typeof commands)[0], word: string, index: number, actions: EditorRefActions) => {
|
|
|
|
|
// Remove trigger char + word, then insert command output
|
|
|
|
|
actions.removeText(index, word.length);
|
|
|
|
|
actions.insertText(cmd.run());
|
|
|
|
|
// Position cursor relative to insertion point, if specified
|
|
|
|
|
if (cmd.cursorOffset) {
|
|
|
|
|
actions.setCursorPosition(index + cmd.cursorOffset);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const { position, suggestions, selectedIndex, isVisible, handleItemSelect } = useSuggestions({
|
|
|
|
|
editorRef,
|
|
|
|
|
editorActions,
|
|
|
|
|
triggerChar: "/",
|
|
|
|
|
items: commands,
|
|
|
|
|
filterItems: (items, query) => (!query ? items : items.filter((cmd) => cmd.name.toLowerCase().startsWith(query))),
|
|
|
|
|
onAutocomplete: (cmd, word, index, actions) => {
|
|
|
|
|
actions.removeText(index, word.length);
|
|
|
|
|
actions.insertText(cmd.run());
|
|
|
|
|
if (cmd.cursorOffset) {
|
|
|
|
|
actions.setCursorPosition(actions.getCursorPosition() + cmd.cursorOffset);
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
onAutocomplete: handleCommandAutocomplete,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (!isVisible || !position) return null;
|
|
|
|
|
|