Spaces:
Build error
Build error
File size: 8,268 Bytes
3b623f5 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 |
/*
Here we initialize the plugin with keyword mapping.
Intended to handle user interactions seamlessly.
Observe the keydown events for proactive suggestions.
Provide a mechanism for accepting AI suggestions.
Evaluate each input change with debounce logic.
Next, we implement touch and mouse interactions.
Anchor the user experience to intuitive behavior.
Intelligently reset suggestions on new input.
*/
import { Extension } from '@tiptap/core';
import { Plugin, PluginKey } from 'prosemirror-state';
export const AIAutocompletion = Extension.create({
name: 'aiAutocompletion',
addOptions() {
return {
generateCompletion: () => Promise.resolve(''),
debounceTime: 1000
};
},
addGlobalAttributes() {
return [
{
types: ['paragraph'],
attributes: {
class: {
default: null,
parseHTML: (element) => element.getAttribute('class'),
renderHTML: (attributes) => {
if (!attributes.class) return {};
return { class: attributes.class };
}
},
'data-prompt': {
default: null,
parseHTML: (element) => element.getAttribute('data-prompt'),
renderHTML: (attributes) => {
if (!attributes['data-prompt']) return {};
return { 'data-prompt': attributes['data-prompt'] };
}
},
'data-suggestion': {
default: null,
parseHTML: (element) => element.getAttribute('data-suggestion'),
renderHTML: (attributes) => {
if (!attributes['data-suggestion']) return {};
return { 'data-suggestion': attributes['data-suggestion'] };
}
}
}
}
];
},
addProseMirrorPlugins() {
let debounceTimer = null;
let loading = false;
let touchStartX = 0;
let touchStartY = 0;
return [
new Plugin({
key: new PluginKey('aiAutocompletion'),
props: {
handleKeyDown: (view, event) => {
const { state, dispatch } = view;
const { selection } = state;
const { $head } = selection;
if ($head.parent.type.name !== 'paragraph') return false;
const node = $head.parent;
if (event.key === 'Tab') {
// if (!node.attrs['data-suggestion']) {
// // Generate completion
// if (loading) return true
// loading = true
// 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
// }).finally(() => {
// loading = false
// })
// }
if (node.attrs['data-suggestion']) {
// 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
})
);
}
// Start debounce logic for AI generation only if the cursor is at the end of the paragraph
if (selection.empty && $head.pos === $head.end()) {
// Set up debounce for AI generation
if (this.options.debounceTime !== null) {
clearTimeout(debounceTimer);
// Capture current position
const currentPos = $head.before();
debounceTimer = setTimeout(() => {
const newState = view.state;
const newSelection = newState.selection;
const newNode = newState.doc.nodeAt(currentPos);
// Check if the node still exists and is still a paragraph
if (
newNode &&
newNode.type.name === 'paragraph' &&
newSelection.$head.pos === newSelection.$head.end() &&
newSelection.$head.pos === currentPos + newNode.nodeSize - 1
) {
const prompt = newNode.textContent;
if (prompt.trim() !== '') {
if (loading) return true;
loading = true;
this.options
.generateCompletion(prompt)
.then((suggestion) => {
if (suggestion && suggestion.trim() !== '') {
if (
view.state.selection.$head.pos === view.state.selection.$head.end()
) {
view.dispatch(
newState.tr.setNodeMarkup(currentPos, null, {
...newNode.attrs,
class: 'ai-autocompletion',
'data-prompt': prompt,
'data-suggestion': suggestion
})
);
}
}
})
.finally(() => {
loading = false;
});
}
}
}, this.options.debounceTime);
}
}
}
return false;
},
handleDOMEvents: {
touchstart: (view, event) => {
touchStartX = event.touches[0].clientX;
touchStartY = event.touches[0].clientY;
return false;
},
touchend: (view, event) => {
const touchEndX = event.changedTouches[0].clientX;
const touchEndY = event.changedTouches[0].clientY;
const deltaX = touchEndX - touchStartX;
const deltaY = touchEndY - touchStartY;
// Check if the swipe was primarily horizontal and to the right
if (Math.abs(deltaX) > Math.abs(deltaY) && deltaX > 50) {
const { state, dispatch } = view;
const { selection } = state;
const { $head } = selection;
const node = $head.parent;
if (node.type.name === 'paragraph' && node.attrs['data-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;
}
}
return false;
},
// Add mousedown behavior
// mouseup: (view, event) => {
// const { state, dispatch } = view;
// const { selection } = state;
// const { $head } = selection;
// const node = $head.parent;
// // Reset debounce timer on mouse click
// clearTimeout(debounceTimer);
// // If a suggestion exists and the cursor moves, remove the suggestion
// if (
// node.type.name === 'paragraph' &&
// node.attrs['data-suggestion'] &&
// view.state.selection.$head.pos !== view.state.selection.$head.end()
// ) {
// dispatch(
// state.tr.setNodeMarkup($head.before(), null, {
// ...node.attrs,
// class: null,
// 'data-prompt': null,
// 'data-suggestion': null
// })
// );
// }
// return false;
// }
mouseup: (view, event) => {
const { state, dispatch } = view;
// Reset debounce timer on mouse click
clearTimeout(debounceTimer);
// Iterate over all nodes in the document
const tr = state.tr;
state.doc.descendants((node, pos) => {
if (node.type.name === 'paragraph' && node.attrs['data-suggestion']) {
// Remove suggestion from this paragraph
tr.setNodeMarkup(pos, null, {
...node.attrs,
class: null,
'data-prompt': null,
'data-suggestion': null
});
}
});
// Apply the transaction if any changes were made
if (tr.docChanged) {
dispatch(tr);
}
return false;
}
}
}
})
];
}
});
|