Skip to content

Commit bd4086a

Browse files
committed
pdf-server: derive dirty from diff — undoing all changes clears save button
persistAnnotations() was unconditionally setDirty(true). It already computes the diff vs baseline — use isDiffEmpty(diff) to decide instead. Now undoing all the way back to the original state marks the viewer clean again (save button disables, title loses asterisk).
1 parent a027d17 commit bd4086a

1 file changed

Lines changed: 16 additions & 11 deletions

File tree

examples/pdf-server/src/mcp-app.ts

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import {
3131
deserializeDiff,
3232
mergeAnnotations,
3333
computeDiff,
34+
isDiffEmpty,
3435
buildAnnotatedPdfBytes,
3536
importPdfjsAnnotation,
3637
uint8ArrayToBase64,
@@ -3151,20 +3152,24 @@ async function loadBaselineAnnotations(
31513152
}
31523153

31533154
function persistAnnotations(): void {
3154-
if (!isRestoring) setDirty(true);
3155+
// Compute diff relative to PDF baseline
3156+
const currentAnnotations: PdfAnnotationDef[] = [];
3157+
for (const tracked of annotationMap.values()) {
3158+
currentAnnotations.push(tracked.def);
3159+
}
3160+
const diff = computeDiff(
3161+
pdfBaselineAnnotations,
3162+
currentAnnotations,
3163+
formFieldValues,
3164+
);
3165+
3166+
// Dirty tracks whether there are unsaved changes. Undoing back to baseline
3167+
// yields an empty diff → clean again → save button disables.
3168+
if (!isRestoring) setDirty(!isDiffEmpty(diff));
3169+
31553170
const key = annotationStorageKey();
31563171
if (!key) return;
31573172
try {
3158-
// Compute diff relative to PDF baseline
3159-
const currentAnnotations: PdfAnnotationDef[] = [];
3160-
for (const tracked of annotationMap.values()) {
3161-
currentAnnotations.push(tracked.def);
3162-
}
3163-
const diff = computeDiff(
3164-
pdfBaselineAnnotations,
3165-
currentAnnotations,
3166-
formFieldValues,
3167-
);
31683173
localStorage.setItem(key, serializeDiff(diff));
31693174
} catch {
31703175
// localStorage may be full or unavailable

0 commit comments

Comments
 (0)