|
| 1 | +import { Extension } from '@tiptap/core' |
| 2 | +import { Decoration, DecorationSet } from '@tiptap/pm/view' |
| 3 | +import { Plugin, PluginKey } from '@tiptap/pm/state' |
| 4 | +import type { Editor } from '@tiptap/vue-3' |
| 5 | +import { useDebounceFn } from '@vueuse/core' |
| 6 | + |
| 7 | +export interface CompletionOptions { |
| 8 | + /** |
| 9 | + * Debounce delay in ms before triggering completion |
| 10 | + * @defaultValue 250 |
| 11 | + */ |
| 12 | + debounce?: number |
| 13 | + /** |
| 14 | + * Characters that should prevent completion from triggering |
| 15 | + * @defaultValue ['/', ':', '@'] |
| 16 | + */ |
| 17 | + triggerCharacters?: string[] |
| 18 | + /** |
| 19 | + * Called when completion should be triggered, receives the text before cursor |
| 20 | + */ |
| 21 | + onTrigger?: (textBefore: string) => void |
| 22 | + /** |
| 23 | + * Called when suggestion is accepted |
| 24 | + */ |
| 25 | + onAccept?: () => void |
| 26 | + /** |
| 27 | + * Called when suggestion is dismissed |
| 28 | + */ |
| 29 | + onDismiss?: () => void |
| 30 | +} |
| 31 | + |
| 32 | +export interface CompletionStorage { |
| 33 | + suggestion: string |
| 34 | + position: number | undefined |
| 35 | + visible: boolean |
| 36 | + debouncedTrigger: ((editor: Editor) => void) | null |
| 37 | + setSuggestion: (text: string) => void |
| 38 | + clearSuggestion: () => void |
| 39 | +} |
| 40 | + |
| 41 | +export const completionPluginKey = new PluginKey('completion') |
| 42 | + |
| 43 | +export const Completion = Extension.create<CompletionOptions, CompletionStorage>({ |
| 44 | + name: 'completion', |
| 45 | + |
| 46 | + addOptions() { |
| 47 | + return { |
| 48 | + debounce: 250, |
| 49 | + triggerCharacters: ['/', ':', '@'], |
| 50 | + onTrigger: undefined, |
| 51 | + onAccept: undefined, |
| 52 | + onDismiss: undefined |
| 53 | + } |
| 54 | + }, |
| 55 | + |
| 56 | + addStorage() { |
| 57 | + return { |
| 58 | + suggestion: '', |
| 59 | + position: undefined as number | undefined, |
| 60 | + visible: false, |
| 61 | + debouncedTrigger: null as ((editor: Editor) => void) | null, |
| 62 | + setSuggestion(text: string) { |
| 63 | + this.suggestion = text |
| 64 | + }, |
| 65 | + clearSuggestion() { |
| 66 | + this.suggestion = '' |
| 67 | + this.position = undefined |
| 68 | + this.visible = false |
| 69 | + } |
| 70 | + } |
| 71 | + }, |
| 72 | + |
| 73 | + addProseMirrorPlugins() { |
| 74 | + const storage = this.storage |
| 75 | + |
| 76 | + return [ |
| 77 | + new Plugin({ |
| 78 | + key: completionPluginKey, |
| 79 | + props: { |
| 80 | + decorations(state) { |
| 81 | + if (!storage.visible || !storage.suggestion || storage.position === undefined) { |
| 82 | + return DecorationSet.empty |
| 83 | + } |
| 84 | + |
| 85 | + const widget = Decoration.widget(storage.position, () => { |
| 86 | + const span = document.createElement('span') |
| 87 | + span.className = 'completion-suggestion' |
| 88 | + span.textContent = storage.suggestion |
| 89 | + span.style.cssText = 'color: var(--ui-text-muted); opacity: 0.6; pointer-events: none;' |
| 90 | + return span |
| 91 | + }, { side: 1 }) |
| 92 | + |
| 93 | + return DecorationSet.create(state.doc, [widget]) |
| 94 | + } |
| 95 | + } |
| 96 | + }) |
| 97 | + ] |
| 98 | + }, |
| 99 | + |
| 100 | + addKeyboardShortcuts() { |
| 101 | + return { |
| 102 | + Tab: ({ editor }) => { |
| 103 | + if (!this.storage.visible || !this.storage.suggestion || this.storage.position === undefined) { |
| 104 | + return false |
| 105 | + } |
| 106 | + |
| 107 | + // Store values before clearing |
| 108 | + const suggestion = this.storage.suggestion |
| 109 | + const position = this.storage.position |
| 110 | + |
| 111 | + // Clear suggestion first |
| 112 | + this.storage.clearSuggestion() |
| 113 | + |
| 114 | + // Force decoration update |
| 115 | + editor.view.dispatch(editor.state.tr.setMeta('completionUpdate', true)) |
| 116 | + |
| 117 | + // Insert the suggestion text |
| 118 | + editor.chain().focus().insertContentAt(position, suggestion).run() |
| 119 | + |
| 120 | + this.options.onAccept?.() |
| 121 | + return true |
| 122 | + }, |
| 123 | + Escape: ({ editor }) => { |
| 124 | + if (this.storage.visible) { |
| 125 | + this.storage.clearSuggestion() |
| 126 | + // Force decoration update |
| 127 | + editor.view.dispatch(editor.state.tr.setMeta('completionUpdate', true)) |
| 128 | + this.options.onDismiss?.() |
| 129 | + return true |
| 130 | + } |
| 131 | + return false |
| 132 | + } |
| 133 | + } |
| 134 | + }, |
| 135 | + |
| 136 | + onUpdate({ editor }) { |
| 137 | + // Clear suggestion on any edit |
| 138 | + if (this.storage.visible) { |
| 139 | + this.storage.clearSuggestion() |
| 140 | + this.options.onDismiss?.() |
| 141 | + } |
| 142 | + |
| 143 | + // Debounced trigger check |
| 144 | + this.storage.debouncedTrigger?.(editor as unknown as Editor) |
| 145 | + }, |
| 146 | + |
| 147 | + onSelectionUpdate() { |
| 148 | + if (this.storage.visible) { |
| 149 | + this.storage.clearSuggestion() |
| 150 | + this.options.onDismiss?.() |
| 151 | + } |
| 152 | + }, |
| 153 | + |
| 154 | + onCreate() { |
| 155 | + const storage = this.storage |
| 156 | + const options = this.options |
| 157 | + |
| 158 | + // Create debounced trigger function for this instance |
| 159 | + this.storage.debouncedTrigger = useDebounceFn((editor: Editor) => { |
| 160 | + if (!options.onTrigger) return |
| 161 | + |
| 162 | + const { state } = editor |
| 163 | + const { selection } = state |
| 164 | + const { $from } = selection |
| 165 | + |
| 166 | + // Only suggest at end of block with content |
| 167 | + const isAtEndOfBlock = $from.parentOffset === $from.parent.content.size |
| 168 | + const hasContent = $from.parent.textContent.trim().length > 0 |
| 169 | + const textContent = $from.parent.textContent |
| 170 | + |
| 171 | + // Don't trigger if sentence is complete (ends with punctuation) |
| 172 | + const endsWithPunctuation = /[.!?]\s*$/.test(textContent) |
| 173 | + |
| 174 | + // Don't trigger if text ends with trigger characters |
| 175 | + const triggerChars = options.triggerCharacters || [] |
| 176 | + const endsWithTrigger = triggerChars.some(char => textContent.endsWith(char)) |
| 177 | + |
| 178 | + if (!isAtEndOfBlock || !hasContent || endsWithPunctuation || endsWithTrigger) { |
| 179 | + return |
| 180 | + } |
| 181 | + |
| 182 | + // Set position and mark as visible |
| 183 | + storage.position = selection.from |
| 184 | + storage.visible = true |
| 185 | + |
| 186 | + // Get text before cursor as context |
| 187 | + const textBefore = state.doc.textBetween(0, selection.from, '\n') |
| 188 | + options.onTrigger(textBefore) |
| 189 | + }, options.debounce || 250) |
| 190 | + }, |
| 191 | + |
| 192 | + onDestroy() { |
| 193 | + this.storage.debouncedTrigger = null |
| 194 | + } |
| 195 | +}) |
| 196 | + |
| 197 | +export default Completion |
0 commit comments