From c0a2dd139aa66826e8f89f832372d2a7c3ff5daf Mon Sep 17 00:00:00 2001 From: Abdullah Atta Date: Wed, 22 Mar 2023 12:56:50 +0500 Subject: [PATCH] web: only load new media attachments on realtime sync --- apps/web/src/components/editor/index.tsx | 14 ++++++++++---- apps/web/src/components/editor/tiptap.tsx | 8 ++++++++ apps/web/src/components/editor/types.ts | 1 + 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/apps/web/src/components/editor/index.tsx b/apps/web/src/components/editor/index.tsx index 1d4ae33cb..15691897e 100644 --- a/apps/web/src/components/editor/index.tsx +++ b/apps/web/src/components/editor/index.tsx @@ -110,25 +110,31 @@ export default function EditorManager({ const isContent = item.type === "tiptap" && item.id === contentId; const isNote = item.type === "note" && item.id === id; - if (isContent) { + if (isContent && editorInstance.current) { if (locked) { const result = await db.vault?.decryptContent(item).catch(() => {}); if (result) item.data = result.data; else EV.publish(EVENTS.vaultLocked); } + const oldHashes = editorInstance.current.getMediaHashes(); - editorInstance.current?.updateContent(item.data as string); + editorInstance.current.updateContent(item.data as string); + + const newHashes = editorInstance.current.getMediaHashes(); + const hashesToLoad = newHashes.filter( + (hash, index) => hash !== oldHashes[index] + ); if (appstore.get().isSyncing()) { db.eventManager.subscribe( EVENTS.syncCompleted, async () => { - await db.attachments?.downloadMedia(id); + await db.attachments?.downloadMedia(id, hashesToLoad); }, true ); } else { - await db.attachments?.downloadMedia(id); + await db.attachments?.downloadMedia(id, hashesToLoad); } } else if (isNote) { if (!locked && item.locked) return EV.publish(EVENTS.vaultLocked); diff --git a/apps/web/src/components/editor/tiptap.tsx b/apps/web/src/components/editor/tiptap.tsx index f9b64f42b..26dfd443c 100644 --- a/apps/web/src/components/editor/tiptap.tsx +++ b/apps/web/src/components/editor/tiptap.tsx @@ -373,6 +373,14 @@ function toIEditor(editor: Editor): IEditor { focus: () => editor.current?.commands.focus("start"), undo: () => editor.current?.commands.undo(), redo: () => editor.current?.commands.redo(), + getMediaHashes: () => { + if (!editor.current) return []; + const hashes: string[] = []; + editor.current.state.doc.descendants((n) => { + if (typeof n.attrs.hash === "string") hashes.push(n.attrs.hash); + }); + return hashes; + }, updateContent: (content) => { const { from, to } = editor.state.selection; editor.current diff --git a/apps/web/src/components/editor/types.ts b/apps/web/src/components/editor/types.ts index f284c478d..f87c8f815 100644 --- a/apps/web/src/components/editor/types.ts +++ b/apps/web/src/components/editor/types.ts @@ -30,6 +30,7 @@ export interface IEditor { focus: () => void; undo: () => void; redo: () => void; + getMediaHashes: () => string[]; updateContent: (content: string) => void; attachFile: (file: Attachment) => void; loadWebClip: (hash: string, html: string) => void;