refac: rich text input

This commit is contained in:
Timothy Jaeryang Baek 2024-11-20 23:14:06 -08:00
parent aca06f92e8
commit c5cd1e4403

View File

@ -159,13 +159,30 @@
if (messageInput) { if (messageInput) {
if (event.key === 'Enter') { if (event.key === 'Enter') {
// Check if the current selection is inside a code block // Check if the current selection is inside a structured block (like codeBlock or list)
const { state } = view; const { state } = view;
const { $head } = state.selection; const { $head } = state.selection;
const isInCodeBlock = $head.parent.type.name === 'codeBlock';
if (isInCodeBlock) { // Recursive function to check ancestors for specific node types
return false; // Prevent Enter action inside a code block function isInside(nodeTypes: string[]): boolean {
let currentNode = $head;
while (currentNode) {
if (nodeTypes.includes(currentNode.parent.type.name)) {
return true;
}
if (!currentNode.depth) break; // Stop if we reach the top
currentNode = state.doc.resolve(currentNode.before()); // Move to the parent node
}
return false;
}
const isInCodeBlock = isInside(['codeBlock']);
const isInList = isInside(['listItem', 'bulletList', 'orderedList']);
const isInHeading = isInside(['heading']);
if (isInCodeBlock || isInList || isInHeading) {
// Let ProseMirror handle the normal Enter behavior
return false;
} }
} }
@ -183,14 +200,12 @@
return true; return true;
} }
} }
if (event.key === 'Enter') { if (event.key === 'Enter') {
eventDispatch('enter', { event }); eventDispatch('enter', { event });
event.preventDefault(); event.preventDefault();
return true; return true;
} }
} }
eventDispatch('keydown', { event }); eventDispatch('keydown', { event });
return false; return false;
}, },