From c5cd1e4403edff3d4d69d00d6fa7b7ff988317a2 Mon Sep 17 00:00:00 2001 From: Timothy Jaeryang Baek Date: Wed, 20 Nov 2024 23:14:06 -0800 Subject: [PATCH] refac: rich text input --- .../components/common/RichTextInput.svelte | 27 ++++++++++++++----- 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/src/lib/components/common/RichTextInput.svelte b/src/lib/components/common/RichTextInput.svelte index fdc98349a..232a739c4 100644 --- a/src/lib/components/common/RichTextInput.svelte +++ b/src/lib/components/common/RichTextInput.svelte @@ -159,13 +159,30 @@ if (messageInput) { 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 { $head } = state.selection; - const isInCodeBlock = $head.parent.type.name === 'codeBlock'; - if (isInCodeBlock) { - return false; // Prevent Enter action inside a code block + // Recursive function to check ancestors for specific node types + 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; } } - if (event.key === 'Enter') { eventDispatch('enter', { event }); event.preventDefault(); return true; } } - eventDispatch('keydown', { event }); return false; },