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
fix(pdf): stop reporting a change when only a tool's default colour changes
Elian Doran58b9abce001e
diff --git a/packages/pdfjs-viewer/e2e/annotation-save.spec.ts b/packages/pdfjs-viewer/e2e/annotation-save.spec.tsindex 1a6623c75d..13486dbc4d 100644--- a/packages/pdfjs-viewer/e2e/annotation-save.spec.ts+++ b/packages/pdfjs-viewer/e2e/annotation-save.spec.ts@@ -145,6 +145,26 @@ test("picking a tool on an already-annotated document schedules nothing", asyncexpect(await modifiedCount(reopened)).toBeGreaterThan(0);});+test("changing a tool's colour with nothing selected schedules nothing", async ({ page }) => {+ const viewer = await openHarness(page);+ await enterHighlightMode(viewer);++ // The highlight tool's parameter toolbar opens with the mode. Its picker sets the colour the+ // *next* highlight is drawn in — nothing already in the document changes, so there is nothing+ // to save (#11059).+ const picker = viewer.locator("#editorHighlightColorPicker");+ await picker.locator("button").first().click();+ await picker.locator(".dropdown button").nth(2).click();+ await page.waitForTimeout(1000);++ expect(await modifiedCount(page)).toBe(0);++ // The new colour still applies, and drawing in it is a change like any other.+ await drawStroke(page, await pageBox(viewer), [[200, 300], [330, 340]]);+ await page.waitForTimeout(500);+ expect(await modifiedCount(page)).toBeGreaterThan(0);+});+test("annotations survive reopening the saved document", async ({ page, context }) => {const viewer = await openHarness(page);await enterInkMode(viewer);diff --git a/packages/pdfjs-viewer/src/bootstrap.spec.ts b/packages/pdfjs-viewer/src/bootstrap.spec.tsindex 78ff68478c..0b80aa9824 100644--- a/packages/pdfjs-viewer/src/bootstrap.spec.ts+++ b/packages/pdfjs-viewer/src/bootstrap.spec.ts@@ -158,7 +158,7 @@ describe("boot sequence", () => {});describe("reporting the document as modified", () => {- it("tells the parent on every signal pdf.js gives for a real change", async () => {+ it("tells the parent on the signals pdf.js gives for a real change", async () => {viewer = await installViewerApp(allFeaturesPdf());const storage = viewer.pdfDocument.annotationStorage as any;const resetModified = vi.spyOn(storage, "resetModified");@@ -166,16 +166,16 @@ describe("reporting the document as modified", () => {// The primary hook: pdf.js dirties annotationStorage, as filling in a form field does.storage.setValue("field-1", { value: "typed" });- // A tool's parameters changing (colour, thickness) also mutates the document.- viewer.eventBus.dispatch("switchannotationeditorparams", { source: null });// Deletions and undo/redo only surface here, and only when there is something to undo.+ // Left unconditional: the first stroke of an ink session flips hasSomethingToUndo while+ // the drawing is still outside annotationStorage, so there is nothing to compare against.viewer.eventBus.dispatch("editingstateschanged", { source: null, details: { hasSomethingToUndo: true } });- expect(viewer.messagesOfType("pdfjs-viewer-document-modified")).toHaveLength(3);+ expect(viewer.messagesOfType("pdfjs-viewer-document-modified")).toHaveLength(2);expect(viewer.lastMessageOfType("pdfjs-viewer-document-modified")).toEqual({ type: "pdfjs-viewer-document-modified", noteId: "note-1", ntxId: "ntx-1" });// Without the reset pdf.js keeps reporting the same change on every later edit.- expect(resetModified).toHaveBeenCalledTimes(3);+ expect(resetModified).toHaveBeenCalledTimes(2);});it("stays quiet while pdf.js registers the annotations already in the document", async () => {@@ -201,6 +201,36 @@ describe("reporting the document as modified", () => {expect(viewer.messagesOfType("pdfjs-viewer-document-modified")).toHaveLength(1);});+ it("stays quiet when a tool's parameters leave the document alone", async () => {+ viewer = await installViewerApp(allFeaturesPdf());+ manageSave();++ // With nothing selected, the colour picker in a tool's parameter toolbar only sets what+ // the next annotation will be drawn in.+ viewer.eventBus.dispatch("switchannotationeditorparams", { source: null });++ expect(viewer.messagesOfType("pdfjs-viewer-document-modified")).toHaveLength(0);+ });++ it("reports a parameter change that does alter an annotation", async () => {+ viewer = await installViewerApp(allFeaturesPdf());+ const storage = viewer.pdfDocument.annotationStorage as any;+ manageSave();++ // Recolouring a selected annotation rewrites what the document serializes to, and this+ // event is the only notice of it — pdf.js updates the editor in place rather than going+ // back through onSetModified. Detaching the hook reproduces that: the content changes,+ // nothing else reports it.+ const hook = storage.onSetModified;+ storage.onSetModified = null;+ storage.setValue("18R", { value: "recoloured" });+ storage.onSetModified = hook;++ viewer.eventBus.dispatch("switchannotationeditorparams", { source: null });++ expect(viewer.messagesOfType("pdfjs-viewer-document-modified")).toHaveLength(1);+ });+it("stays quiet when the editing state changes with nothing to undo", async () => {viewer = await installViewerApp(allFeaturesPdf());manageSave();diff --git a/packages/pdfjs-viewer/src/bootstrap.ts b/packages/pdfjs-viewer/src/bootstrap.tsindex af18cd7514..46f3dda6ff 100644--- a/packages/pdfjs-viewer/src/bootstrap.ts+++ b/packages/pdfjs-viewer/src/bootstrap.ts@@ -126,7 +126,7 @@ export function manageSave() {let pointerDownOnPage = false;// What the document held when the parent was last told about it. Compared against, rather- // than trusted, because pdf.js reports far more than actual changes — see onStorageModified.+ // than trusted, because pdf.js reports far more than actual changes — see reportIfChanged.let reportedHash = serializedHash();/**@@ -152,16 +152,23 @@ export function manageSave() {}/**- * pdf.js dirtied `annotationStorage`. That covers real edits, but also every time an editing- * mode turns the annotations already stored on a rendered page into editors — one signal per- * annotation, raised again for each page that scrolls into view. Passing those on had the- * parent re-serialise and re-upload the whole PDF because a toolbar button was pressed- * (#11059), so the content is compared before anything is announced.+ * Reports a modification only when the document really did change.+ *+ * Both signals routed here fire freely on their own: registering the annotations already+ * stored on a page raises the storage hook once per annotation, on every toolbar press and+ * as each annotated page scrolls into view, and a tool's colour picker announces a parameter+ * change even with nothing selected, where it only sets what the *next* annotation will look+ * like. Either one had the parent re-serialise and re-upload the whole PDF for nothing+ * (#11059).+ *+ * Safe for the parameter change because pdf.js applies it before this runs: its own listener+ * was registered at start-up, ours during `documentloaded`, and the event bus calls them in+ * registration order.*/- function onStorageModified() {+ function reportIfChanged() {if (!storage) return;- // Cleared whether or not this turns into an announcement: pdf.js raises the hook once per- // dirtying, so a flag left set would swallow the next real change.+ // Cleared whether or not this turns into an announcement: pdf.js raises the storage hook+ // once per dirtying, so a flag left set would swallow the next real change.storage.resetModified();const hash = serializedHash();@@ -200,10 +207,10 @@ export function manageSave() {});(app.pdfDocument.annotationStorage as any).onSetModified = () => {- onStorageModified();+ reportIfChanged();}; // works great for most cases, including forms.app.eventBus.on("switchannotationeditorparams", () => {- onChange();+ reportIfChanged();});// Catches deletions of existing annotations, undo/redo, and comment deletion// which don't trigger onSetModified or switchannotationeditorparams.