fix(admin-panel): resolve caret jumping and DOM sync issues in RichTextEditor
Deploy Canina / deploy (push) Successful in 49s
Deploy Canina / deploy (push) Successful in 49s
This commit is contained in:
1 parent
ebdc740bc0
commit
8852f62580
3 files changed
+613
-590
No files matched your search
@@ -1,4 +1,4 @@
|
||||
import React, { useRef } from 'react';
|
||||
import React, { useRef, useEffect } from 'react';
|
||||
import {
|
||||
Bold,
|
||||
Italic,
|
||||
@@ -41,6 +41,25 @@ export default function RichTextEditor({
|
||||
className = '',
|
||||
}: RichTextEditorProps) {
|
||||
const editorRef = useRef<HTMLDivElement>(null);
|
||||
const isTypingRef = useRef(false);
|
||||
const isMountedRef = useRef(false);
|
||||
|
||||
// Sync value from props on initial mount or when external change occurs (not while active)
|
||||
useEffect(() => {
|
||||
if (!editorRef.current) return;
|
||||
if (!isMountedRef.current) {
|
||||
editorRef.current.innerHTML = value || '';
|
||||
isMountedRef.current = true;
|
||||
return;
|
||||
}
|
||||
|
||||
// Only update innerHTML if it's different and not currently focused/typing
|
||||
if (editorRef.current.innerHTML !== (value || '')) {
|
||||
if (!isTypingRef.current && document.activeElement !== editorRef.current) {
|
||||
editorRef.current.innerHTML = value || '';
|
||||
}
|
||||
}
|
||||
}, [value]);
|
||||
|
||||
const exec = (command: string, val: string | undefined = undefined) => {
|
||||
document.execCommand(command, false, val);
|
||||
@@ -58,7 +77,11 @@ export default function RichTextEditor({
|
||||
|
||||
const handleInput = () => {
|
||||
if (editorRef.current) {
|
||||
isTypingRef.current = true;
|
||||
onChange(editorRef.current.innerHTML);
|
||||
setTimeout(() => {
|
||||
isTypingRef.current = false;
|
||||
}, 50);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -228,8 +251,8 @@ export default function RichTextEditor({
|
||||
<div
|
||||
ref={editorRef}
|
||||
contentEditable
|
||||
suppressContentEditableWarning
|
||||
onInput={handleInput}
|
||||
dangerouslySetInnerHTML={{ __html: value }}
|
||||
data-placeholder={placeholder}
|
||||
className="p-4 min-h-[140px] max-h-[350px] overflow-y-auto outline-none text-xs leading-relaxed text-gray-800 font-vazir empty:before:content-[attr(data-placeholder)] empty:before:text-gray-400"
|
||||
dir="rtl"
|
||||
|
||||
Reference in new issue
Block a user