Skip to content

Commit b99ab5e

Browse files
committed
fix state sync
1 parent 916200d commit b99ab5e

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

src/components/Editor.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,9 @@ const Editor = () => {
5858

5959
const editor = useEditor({
6060
extensions,
61-
content: content,
61+
content: content ? JSON.parse(content) : "",
6262
onUpdate: ({ editor }) => {
63-
setContent(editor.getHTML());
63+
setContent(JSON.stringify(editor.getJSON()));
6464
},
6565
editorProps: {
6666
attributes: {
@@ -73,7 +73,7 @@ const Editor = () => {
7373
useEffect(() => {
7474
const unsubscribe = $storage.subscribe!('st-content', (value) => {
7575
if (editor) {
76-
editor.commands.setContent(value);
76+
editor.commands.setContent(value ? JSON.parse(value) : "");
7777
}
7878
}, null);
7979

src/lib/stores.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,17 @@ export const $storage = getStorage();
7373

7474
const ST_CONTENT_KEY = "st-content";
7575

76+
77+
const defaultContent = {
78+
"type": "doc",
79+
"content": Array(20).fill({
80+
"type": "paragraph"
81+
})
82+
};
83+
7684
export const contentAtom = atomWithStorage<string | null>(
7785
ST_CONTENT_KEY,
78-
"<p></p>".repeat(20),
86+
JSON.stringify(defaultContent),
7987
$storage,
8088
{
8189
getOnInit: true,

0 commit comments

Comments
 (0)