Skip to content

Commit

Permalink
feat: step 1
Browse files Browse the repository at this point in the history
load localStorage from parent component
  • Loading branch information
Henry-Wow committed Jan 12, 2025
1 parent af1ba94 commit cec0fc6
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
16 changes: 15 additions & 1 deletion src/lib/components/editors/CompositeMarkdownEditor.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import { convertSyncStepsToNodes } from './editor-history';
import './CollaborativeEditor.css';
import type { ViewDagNode } from './DagView.svelte';
import { toByteArray } from 'base64-js';
let {
content = $bindable(''),
Expand All @@ -29,14 +30,27 @@
let loroDoc = new LoroDoc();
let idA = loroDoc.peerIdStr;
let awareness = new CursorAwareness(idA);
const savedState = localStorage.getItem('loro-editor-state');
if (savedState) {
try {
const blob = toByteArray(savedState);
loroDoc.import(blob);
console.log("imported saved state")
console.log(loroDoc.toJSON())
dagInfo = convertSyncStepsToNodes(loroDoc);
} catch (e) {
console.error('Failed to load saved state:', e);
}
}
// 初始化时开启时间戳记录
loroDoc.setRecordTimestamp(true);
loroDoc.setChangeMergeInterval(10);
// 监听变化更新历史信息
loroDoc.subscribe((event) => {
if (event.by === "local") {
loroDoc.commit();
dagInfo = convertSyncStepsToNodes(loroDoc);
}
});
Expand Down
5 changes: 1 addition & 4 deletions src/lib/components/editors/RichMarkdownEditor.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,6 @@
initialized= true;
console.log("initLoroDoc")
const savedState = localStorage.getItem('loro-editor-state');
const sessionId = Math.floor(Math.random() * 1000000).toString(16);
loroDoc.setNextCommitMessage(`sessionId: ${sessionId}`);
if (savedState) {
try {
const blob = toByteArray(savedState);
Expand All @@ -57,6 +54,7 @@
// 监听文档变化并保存
unsubscribe = loroDoc.subscribe(() => {
console.log("currentDoc",loroDoc.toJSON())
console.log("currenthistory",loroDoc.exportJsonUpdates(undefined, undefined, false))
if (saveTimeout) {
clearTimeout(saveTimeout);
}
Expand All @@ -65,7 +63,6 @@
localStorage.setItem('loro-editor-state', fromByteArray(state));
saveTimeout = undefined;
}, 1000) as unknown as number;
loroDoc.setNextCommitMessage(`sessionId: ${sessionId}`);
});
// 初始化文档内容
Expand Down

0 comments on commit cec0fc6

Please sign in to comment.