Elffuss BraidHub

kikocis/continue public

578c9023eefa · 1.5 KiBRawPermalink
1// Adapted from SlashCommand extension (@tiptap/extension-mention/src/mention.ts)
2
3import { Node } from "@tiptap/core";
4import { PluginKey } from "@tiptap/pm/state";
5import Suggestion, { SuggestionOptions } from "@tiptap/suggestion";
6import { ComboBoxItem } from "../../types";
7
8export type SlashCommandOptions = {
9 suggestion: Omit<SuggestionOptions<ComboBoxItem, ComboBoxItem>, "editor">;
10};
11
12export const SlashCommand = Node.create<SlashCommandOptions>({
13 name: "slash-command",
14
15 addOptions() {
16 return {
17 suggestion: {
18 allow: ({ editor }) =>
19 // the first character is "/". we want to avoid slash commands when there is a space after it
20 editor.getText().at(1) !== " ",
21 char: "/",
22 pluginKey: new PluginKey(this.name),
23 startOfLine: true,
24 command: ({ editor, range, props }) => {
25 // First delete the slash character and any text after it
26 editor.chain().focus().deleteRange(range).run();
27
28 editor.commands.insertPrompt(props);
29 },
30 },
31 };
32 },
33
34 group: "inline",
35 inline: true,
36 selectable: false,
37 atom: true,
38
39 // We don't need attributes since we won't be rendering any node
40 addAttributes() {
41 return {};
42 },
43
44 // No need to parse HTML since we're not rendering anything
45 parseHTML() {
46 return [];
47 },
48
49 addProseMirrorPlugins() {
50 return [
51 Suggestion({
52 editor: this.editor,
53 ...this.options.suggestion,
54 }),
55 ];
56 },
57});