mirror of
https://github.com/zadam/trilium.git
synced 2025-12-14 03:14:24 +01:00
feat(ckeditor/watchdog): use data stored in the spaced update
This commit is contained in:
parent
f662b95dc9
commit
1bbf86fbeb
@ -78,12 +78,23 @@ export function useSpacedUpdate(callback: () => void | Promise<void>, interval =
|
|||||||
return spacedUpdateRef.current;
|
return spacedUpdateRef.current;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface SavedData {
|
||||||
|
content: string;
|
||||||
|
attachments?: {
|
||||||
|
role: string;
|
||||||
|
title: string;
|
||||||
|
mime: string;
|
||||||
|
content: string;
|
||||||
|
position: number;
|
||||||
|
}[];
|
||||||
|
}
|
||||||
|
|
||||||
export function useEditorSpacedUpdate({ note, noteContext, getData, onContentChange, dataSaved, updateInterval }: {
|
export function useEditorSpacedUpdate({ note, noteContext, getData, onContentChange, dataSaved, updateInterval }: {
|
||||||
note: FNote,
|
note: FNote,
|
||||||
noteContext: NoteContext | null | undefined,
|
noteContext: NoteContext | null | undefined,
|
||||||
getData: () => Promise<object | undefined> | object | undefined,
|
getData: () => Promise<SavedData | undefined> | SavedData | undefined,
|
||||||
onContentChange: (newContent: string) => void,
|
onContentChange: (newContent: string) => void,
|
||||||
dataSaved?: () => void,
|
dataSaved?: (savedData: SavedData) => void,
|
||||||
updateInterval?: number;
|
updateInterval?: number;
|
||||||
}) {
|
}) {
|
||||||
const parentComponent = useContext(ParentComponent);
|
const parentComponent = useContext(ParentComponent);
|
||||||
@ -99,7 +110,7 @@ export function useEditorSpacedUpdate({ note, noteContext, getData, onContentCha
|
|||||||
protected_session_holder.touchProtectedSessionIfNecessary(note);
|
protected_session_holder.touchProtectedSessionIfNecessary(note);
|
||||||
await server.put(`notes/${note.noteId}/data`, data, parentComponent?.componentId);
|
await server.put(`notes/${note.noteId}/data`, data, parentComponent?.componentId);
|
||||||
|
|
||||||
dataSaved?.();
|
dataSaved?.(data);
|
||||||
}
|
}
|
||||||
}, [ note, getData, dataSaved ])
|
}, [ note, getData, dataSaved ])
|
||||||
const spacedUpdate = useSpacedUpdate(callback);
|
const spacedUpdate = useSpacedUpdate(callback);
|
||||||
|
|||||||
@ -3,7 +3,7 @@ import NoteContext from "../../../components/note_context";
|
|||||||
import FNote from "../../../entities/fnote";
|
import FNote from "../../../entities/fnote";
|
||||||
import { AppState, BinaryFileData, ExcalidrawImperativeAPI, ExcalidrawProps, LibraryItem } from "@excalidraw/excalidraw/types";
|
import { AppState, BinaryFileData, ExcalidrawImperativeAPI, ExcalidrawProps, LibraryItem } from "@excalidraw/excalidraw/types";
|
||||||
import { useRef } from "preact/hooks";
|
import { useRef } from "preact/hooks";
|
||||||
import { useEditorSpacedUpdate } from "../../react/hooks";
|
import { SavedData, useEditorSpacedUpdate } from "../../react/hooks";
|
||||||
import { ExcalidrawElement, NonDeletedExcalidrawElement } from "@excalidraw/excalidraw/element/types";
|
import { ExcalidrawElement, NonDeletedExcalidrawElement } from "@excalidraw/excalidraw/element/types";
|
||||||
import { exportToSvg, getSceneVersion } from "@excalidraw/excalidraw";
|
import { exportToSvg, getSceneVersion } from "@excalidraw/excalidraw";
|
||||||
import server from "../../../services/server";
|
import server from "../../../services/server";
|
||||||
@ -77,7 +77,7 @@ export default function useCanvasPersistence(note: FNote, noteContext: NoteConte
|
|||||||
const api = apiRef.current;
|
const api = apiRef.current;
|
||||||
if (!api) return;
|
if (!api) return;
|
||||||
const { content, svg } = await getData(api);
|
const { content, svg } = await getData(api);
|
||||||
const attachments = [{ role: "image", title: "canvas-export.svg", mime: "image/svg+xml", content: svg, position: 0 }];
|
const attachments: SavedData["attachments"] = [{ role: "image", title: "canvas-export.svg", mime: "image/svg+xml", content: svg, position: 0 }];
|
||||||
|
|
||||||
// libraryChanged is unset in dataSaved()
|
// libraryChanged is unset in dataSaved()
|
||||||
if (libraryChanged.current) {
|
if (libraryChanged.current) {
|
||||||
@ -124,7 +124,7 @@ export default function useCanvasPersistence(note: FNote, noteContext: NoteConte
|
|||||||
title: libraryItem.id + libraryItem.name,
|
title: libraryItem.id + libraryItem.name,
|
||||||
mime: "application/json",
|
mime: "application/json",
|
||||||
content: JSON.stringify(libraryItem),
|
content: JSON.stringify(libraryItem),
|
||||||
position: position
|
position
|
||||||
});
|
});
|
||||||
|
|
||||||
position += 10;
|
position += 10;
|
||||||
|
|||||||
@ -57,6 +57,10 @@ export default function EditableText({ note, parentComponent, ntxId, noteContext
|
|||||||
onContentChange(newContent) {
|
onContentChange(newContent) {
|
||||||
contentRef.current = newContent;
|
contentRef.current = newContent;
|
||||||
watchdogRef.current?.editor?.setData(newContent);
|
watchdogRef.current?.editor?.setData(newContent);
|
||||||
|
},
|
||||||
|
dataSaved(savedData) {
|
||||||
|
// Store back the saved data in order to retrieve it in case the CKEditor crashes.
|
||||||
|
contentRef.current = savedData.content;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
const templates = useTemplates();
|
const templates = useTemplates();
|
||||||
@ -245,7 +249,9 @@ export default function EditableText({ note, parentComponent, ntxId, noteContext
|
|||||||
}
|
}
|
||||||
|
|
||||||
initialized.current.resolve();
|
initialized.current.resolve();
|
||||||
editor.setData(contentRef.current ?? "");
|
// Restore the data, either on the first render or if the editor crashes.
|
||||||
|
// We are not using CKEditor's built-in watch dog content, instead we are using the data we store regularly in the spaced update (see `dataSaved`).
|
||||||
|
editor.setData(contentRef.current);
|
||||||
parentComponent?.triggerEvent("textEditorRefreshed", { ntxId, editor });
|
parentComponent?.triggerEvent("textEditorRefreshed", { ntxId, editor });
|
||||||
}}
|
}}
|
||||||
/>}
|
/>}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user