From 0413c6a10a00188adb4c8c40a1bf9b7d8efcbea4 Mon Sep 17 00:00:00 2001 From: Pabloader Date: Mon, 13 Apr 2026 06:39:17 +0000 Subject: [PATCH] Maybe fixed contenteditable --- src/common/components/ContentEditable.tsx | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/src/common/components/ContentEditable.tsx b/src/common/components/ContentEditable.tsx index 9ae5834..a4c4c6d 100644 --- a/src/common/components/ContentEditable.tsx +++ b/src/common/components/ContentEditable.tsx @@ -12,12 +12,19 @@ type Props = Omit, 'value' | 'onInput'> & { onInput?: JSX.EventHandler>; }; -const parseLines = (html: string): string => ( - html +const parseLines = (html: string, keepSpan: boolean = false): string => { + html = html .replace(/\n?
<\/div>/g, '\n\n') .replace(/\n?]*>/, '\n') - .replace(/<[^>]+>/g, '') -); + .replace(/<\/?div[^>]*>/g, '') + .replace(/]*>/g, ''); + + if (!keepSpan) { + html = html.replace(/<[^>]+>/g, ''); + } + + return html; +}; function getCaretOffset(el: HTMLElement): number { const sel = window.getSelection(); @@ -86,8 +93,12 @@ export const ContentEditable = ({ const el = ref.current; if (!el) return; - const offset = document.activeElement === el ? getCaretOffset(el) : null; const newValue = enableHighlight ? highlight(value) : value; + const oldValue = parseLines(el.innerHTML, true); + + if (oldValue === newValue) return; + + const offset = document.activeElement === el ? getCaretOffset(el) : null; el.innerHTML = newValue; (el as any).value = value; if (offset !== null) setCaretOffset(el, offset);