mirror of
https://github.com/priyanshujain/margin.git
synced 2026-10-02 19:17:03 +00:00
fix(editor): defer render and load content per chapter for WebKit
This commit is contained in:
1 parent
0de442eab0
commit
c4b69f3641
1 file changed
+13
-8
+13
-8
@@ -1,4 +1,4 @@
|
|||||||
import { useEffect } from "react";
|
import { useEffect, useRef } from "react";
|
||||||
import { EditorContent, useEditor, type Editor as TiptapEditor } from "@tiptap/react";
|
import { EditorContent, useEditor, type Editor as TiptapEditor } from "@tiptap/react";
|
||||||
import type { JSONContent } from "@tiptap/core";
|
import type { JSONContent } from "@tiptap/core";
|
||||||
import { editorExtensions } from "./extensions";
|
import { editorExtensions } from "./extensions";
|
||||||
@@ -11,20 +11,25 @@ interface EditorProps {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function Editor({ chapterId, content, onChange, onReady }: EditorProps) {
|
export function Editor({ chapterId, content, onChange, onReady }: EditorProps) {
|
||||||
const editor = useEditor(
|
const onChangeRef = useRef(onChange);
|
||||||
{
|
onChangeRef.current = onChange;
|
||||||
|
|
||||||
|
const editor = useEditor({
|
||||||
extensions: editorExtensions,
|
extensions: editorExtensions,
|
||||||
content,
|
content,
|
||||||
autofocus: "start",
|
immediatelyRender: false,
|
||||||
editorProps: { attributes: { class: "prose" } },
|
editorProps: { attributes: { class: "prose" } },
|
||||||
onUpdate: ({ editor }) => onChange(editor.getJSON()),
|
onUpdate: ({ editor }) => onChangeRef.current(editor.getJSON()),
|
||||||
},
|
});
|
||||||
[chapterId]
|
|
||||||
);
|
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
onReady(editor);
|
onReady(editor);
|
||||||
}, [editor, onReady]);
|
}, [editor, onReady]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (editor) editor.commands.setContent(content, { emitUpdate: false });
|
||||||
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||||
|
}, [chapterId, editor]);
|
||||||
|
|
||||||
return <EditorContent editor={editor} className="editor-host" />;
|
return <EditorContent editor={editor} className="editor-host" />;
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user