fixed ref preview not updating
This commit is contained in:
@@ -103,6 +103,7 @@ export default function Editor({ editSlug }: Props) {
|
||||
const viewRef = useRef<EditorView | null>(null);
|
||||
const previewRef = useRef<HTMLDivElement>(null);
|
||||
const previewTimerRef = useRef<ReturnType<typeof setTimeout> | null>(null);
|
||||
const updatePreviewRef = useRef<() => void>(() => {});
|
||||
const [slug, setSlug] = useState(editSlug || '');
|
||||
const [summary, setSummary] = useState('');
|
||||
const [originalSlug, setOriginalSlug] = useState(editSlug || '');
|
||||
@@ -140,6 +141,8 @@ export default function Editor({ editSlug }: Props) {
|
||||
});
|
||||
}, [showPreview]);
|
||||
|
||||
useEffect(() => { updatePreviewRef.current = updatePreview; }, [updatePreview]);
|
||||
|
||||
// Initialize CodeMirror once
|
||||
useEffect(() => {
|
||||
if (!editorRef.current || viewRef.current) return;
|
||||
@@ -158,7 +161,7 @@ export default function Editor({ editSlug }: Props) {
|
||||
EditorView.updateListener.of(update => {
|
||||
if (!update.docChanged) return;
|
||||
if (previewTimerRef.current) clearTimeout(previewTimerRef.current);
|
||||
previewTimerRef.current = setTimeout(updatePreview, 300);
|
||||
previewTimerRef.current = setTimeout(() => updatePreviewRef.current(), 300);
|
||||
const pos = update.state.selection.main.head;
|
||||
const line = update.state.doc.lineAt(pos);
|
||||
const textBefore = line.text.slice(0, pos - line.from);
|
||||
|
||||
Reference in New Issue
Block a user