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