Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
client/text editor: fix an issue
Adorian Doran48443c74dae0
diff --git a/apps/client/src/widgets/type_widgets/code/CodeEmbed.spec.tsx b/apps/client/src/widgets/type_widgets/code/CodeEmbed.spec.tsxindex 5a56585957..5b178fc4ca 100644--- a/apps/client/src/widgets/type_widgets/code/CodeEmbed.spec.tsx+++ b/apps/client/src/widgets/type_widgets/code/CodeEmbed.spec.tsx@@ -147,6 +147,34 @@ describe("CodeEmbed", () => {await vi.waitFor(() => expect(attachmentView.state.doc.toString()).toBe("b"));});+ it("keeps the latest attachment content when an older fetch resolves last", async () => {+ const attachment = buildAttachment("script.py");+ const { figure } = await mount(attachment, buildEditor(), {+ content: "a",+ isEditable: true+ });+ const view = await findView(figure);+ const resolvers: ((blob: FBlob) => void)[] = [];+ attachment.getBlob = () => new Promise((resolve) => resolvers.push(resolve));+ const saved = new LoadResults([]);+ saved.addAttachmentRow({ attachmentId: attachment.attachmentId } as never, "any");+ await act(async () => {+ await parent.handleEvent("entitiesReloaded", { loadResults: saved });+ await parent.handleEvent("entitiesReloaded", { loadResults: saved });+ });+ expect(resolvers).toHaveLength(2);++ await act(async () => {+ resolvers[1](buildBlob("new"));+ });+ await vi.waitFor(() => expect(view.state.doc.toString()).toBe("new"));+ await act(async () => {+ resolvers[0](buildBlob("old"));+ await new Promise((resolve) => setTimeout(resolve, 50));+ });+ expect(view.state.doc.toString()).toBe("new");+ });+it("loads the note as saved elsewhere, unless it has unsaved changes", async () => {const { figure } = await mount(buildCodeNote("saved"), buildEditor(), {content: "stale",diff --git a/apps/client/src/widgets/type_widgets/code/CodeEmbed.tsx b/apps/client/src/widgets/type_widgets/code/CodeEmbed.tsxindex 82f85e1750..e3911d65c6 100644--- a/apps/client/src/widgets/type_widgets/code/CodeEmbed.tsx+++ b/apps/client/src/widgets/type_widgets/code/CodeEmbed.tsx@@ -74,13 +74,19 @@ function useSavedContent() {const noteBlob = useNoteBlob(entity instanceof FNote ? entity : null);const [ attachmentContent, setAttachmentContent ] = useState<string>();+ const requestIdRef = useRef(0);const content = entity instanceof FNote ? noteBlob?.content : attachmentContent;useTriliumEvent("entitiesReloaded", ({ loadResults }) => {if (entity instanceof FNote) return;const rows = loadResults.getAttachmentRows();if (rows.some((row) => row.attachmentId === entity.attachmentId)) {- void entity.getBlob().then((blob) => setAttachmentContent(blob?.content));+ const requestId = ++requestIdRef.current;+ void entity.getBlob().then((blob) => {+ if (requestId === requestIdRef.current) {+ setAttachmentContent(blob?.content);+ }+ });}});