web: only load new media attachments on realtime sync

This commit is contained in:
Abdullah Atta
2023-03-22 12:56:50 +05:00
committed by Abdullah Atta
parent b354a26f0a
commit c0a2dd139a
3 changed files with 19 additions and 4 deletions

View File

@@ -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);

View File

@@ -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

View File

@@ -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;