| 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(props); |
| 26 | }, |
| 27 | }, |
| 28 | }; |
| 29 | }, |
| 30 | |
| 31 | group: "inline", |
| 32 | inline: true, |
| 33 | selectable: false, |
| 34 | atom: true, |
| 35 | |
| 36 | // We don't need attributes since we won't be rendering any node |
| 37 | addAttributes() { |
| 38 | return {}; |
| 39 | }, |
| 40 | |
| 41 | // No need to parse HTML since we're not rendering anything |
| 42 | parseHTML() { |
| 43 | return []; |
| 44 | }, |
| 45 | |
| 46 | addProseMirrorPlugins() { |
| 47 | return [ |
| 48 | Suggestion({ |
| 49 | editor: this.editor, |
| 50 | ...this.options.suggestion, |
| 51 | }), |
| 52 | ]; |
| 53 | }, |
| 54 | }); |