|
@@ -50,8 +50,6 @@ export const AIAutocompletion = Extension.create({
|
|
|
key: new PluginKey('aiAutocompletion'),
|
|
|
props: {
|
|
|
handleKeyDown: (view, event) => {
|
|
|
- if (event.key !== 'Tab') return false
|
|
|
-
|
|
|
const { state, dispatch } = view
|
|
|
const { selection } = state
|
|
|
const { $head } = selection
|
|
@@ -59,35 +57,47 @@ export const AIAutocompletion = Extension.create({
|
|
|
if ($head.parent.type.name !== 'paragraph') return false
|
|
|
|
|
|
const node = $head.parent
|
|
|
- const prompt = node.textContent
|
|
|
|
|
|
- if (!node.attrs['data-suggestion']) {
|
|
|
- // Generate completion
|
|
|
- this.options.generateCompletion(prompt).then(suggestion => {
|
|
|
- if (suggestion && suggestion.trim() !== '') {
|
|
|
- dispatch(state.tr.setNodeMarkup($head.before(), null, {
|
|
|
- ...node.attrs,
|
|
|
- class: 'ai-autocompletion',
|
|
|
- 'data-prompt': prompt,
|
|
|
- 'data-suggestion': suggestion,
|
|
|
- }))
|
|
|
- }
|
|
|
- })
|
|
|
- } else {
|
|
|
- // Accept suggestion
|
|
|
- const suggestion = node.attrs['data-suggestion']
|
|
|
- dispatch(state.tr
|
|
|
- .insertText(suggestion, $head.pos)
|
|
|
- .setNodeMarkup($head.before(), null, {
|
|
|
- ...node.attrs,
|
|
|
- class: null,
|
|
|
- 'data-prompt': null,
|
|
|
- 'data-suggestion': null,
|
|
|
+ if (event.key === 'Tab') {
|
|
|
+ if (!node.attrs['data-suggestion']) {
|
|
|
+ // Generate completion
|
|
|
+ const prompt = node.textContent
|
|
|
+ this.options.generateCompletion(prompt).then(suggestion => {
|
|
|
+ if (suggestion && suggestion.trim() !== '') {
|
|
|
+ dispatch(state.tr.setNodeMarkup($head.before(), null, {
|
|
|
+ ...node.attrs,
|
|
|
+ class: 'ai-autocompletion',
|
|
|
+ 'data-prompt': prompt,
|
|
|
+ 'data-suggestion': suggestion,
|
|
|
+ }))
|
|
|
+ }
|
|
|
+ // If suggestion is empty or null, do nothing
|
|
|
})
|
|
|
- )
|
|
|
+ } else {
|
|
|
+ // Accept suggestion
|
|
|
+ const suggestion = node.attrs['data-suggestion']
|
|
|
+ dispatch(state.tr
|
|
|
+ .insertText(suggestion, $head.pos)
|
|
|
+ .setNodeMarkup($head.before(), null, {
|
|
|
+ ...node.attrs,
|
|
|
+ class: null,
|
|
|
+ 'data-prompt': null,
|
|
|
+ 'data-suggestion': null,
|
|
|
+ })
|
|
|
+ )
|
|
|
+ }
|
|
|
+ return true
|
|
|
+ } else if (node.attrs['data-suggestion']) {
|
|
|
+ // Reset suggestion on any other key press
|
|
|
+ dispatch(state.tr.setNodeMarkup($head.before(), null, {
|
|
|
+ ...node.attrs,
|
|
|
+ class: null,
|
|
|
+ 'data-prompt': null,
|
|
|
+ 'data-suggestion': null,
|
|
|
+ }))
|
|
|
}
|
|
|
|
|
|
- return true
|
|
|
+ return false
|
|
|
},
|
|
|
},
|
|
|
}),
|