refac: styling

This commit is contained in:
Timothy Jaeryang Baek 2024-12-01 17:15:16 -08:00
parent 0055f3dcb6
commit 460992613f
2 changed files with 49 additions and 17 deletions

View File

@ -211,7 +211,8 @@ input[type='number'] {
float: left; float: left;
color: #adb5bd; color: #adb5bd;
pointer-events: none; pointer-events: none;
height: 0;
@apply line-clamp-1 absolute
} }
.ai-autocompletion::after { .ai-autocompletion::after {

View File

@ -136,12 +136,16 @@ export const AIAutocompletion = Extension.create({
debounceTimer = setTimeout(() => { debounceTimer = setTimeout(() => {
const newState = view.state; const newState = view.state;
const newSelection = newState.selection;
const newNode = newState.doc.nodeAt(currentPos); const newNode = newState.doc.nodeAt(currentPos);
const currentIsAtEnd =
newState.selection.$head.pos === newState.selection.$head.end();
// Check if the node still exists and is still a paragraph // Check if the node still exists and is still a paragraph
if (newNode && newNode.type.name === 'paragraph' && currentIsAtEnd) { if (
newNode &&
newNode.type.name === 'paragraph' &&
newSelection.$head.pos === newSelection.$head.end() &&
newSelection.$head.pos === currentPos + newNode.nodeSize - 1
) {
const prompt = newNode.textContent; const prompt = newNode.textContent;
if (prompt.trim() !== '') { if (prompt.trim() !== '') {
@ -212,29 +216,56 @@ export const AIAutocompletion = Extension.create({
return false; return false;
}, },
// Add mousedown behavior // 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) => { mouseup: (view, event) => {
const { state, dispatch } = view; const { state, dispatch } = view;
const { selection } = state;
const { $head } = selection;
const node = $head.parent;
// Reset debounce timer on mouse click // Reset debounce timer on mouse click
clearTimeout(debounceTimer); clearTimeout(debounceTimer);
// If a suggestion exists and the cursor moves, remove the suggestion // Iterate over all nodes in the document
if ( const tr = state.tr;
node.type.name === 'paragraph' && state.doc.descendants((node, pos) => {
node.attrs['data-suggestion'] && if (node.type.name === 'paragraph' && node.attrs['data-suggestion']) {
view.state.selection.$head.pos !== view.state.selection.$head.end() // Remove suggestion from this paragraph
) { tr.setNodeMarkup(pos, null, {
dispatch(
state.tr.setNodeMarkup($head.before(), null, {
...node.attrs, ...node.attrs,
class: null, class: null,
'data-prompt': null, 'data-prompt': null,
'data-suggestion': null 'data-suggestion': null
}) });
); }
});
// Apply the transaction if any changes were made
if (tr.docChanged) {
dispatch(tr);
} }
return false; return false;