Skip to content

Commit d874a0e

Browse files
author
notcompromised
committed
fix(desktop): stop preview annotations replacing each other after a reload
The picker preload numbered annotations with a counter that restarts on every page load, so the first pick after any reload reused an id the composer draft already held, and addPreviewAnnotation replaced the older annotation. Ids now carry a random per-page-load part.
1 parent 0fcd5f9 commit d874a0e

3 files changed

Lines changed: 45 additions & 5 deletions

File tree

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { describe, expect, it } from "vite-plus/test";
2+
3+
import { createAnnotationIdSource } from "./AnnotationIds.ts";
4+
5+
describe("createAnnotationIdSource", () => {
6+
it("gives annotations picked on separate page loads distinct ids", () => {
7+
// The picker preload runs afresh on every page load, while the composer
8+
// keeps earlier annotations keyed by id.
9+
const firstLoad = createAnnotationIdSource();
10+
firstLoad("element");
11+
const firstAnnotation = firstLoad("annotation");
12+
13+
const secondLoad = createAnnotationIdSource();
14+
secondLoad("element");
15+
const secondAnnotation = secondLoad("annotation");
16+
17+
expect(secondAnnotation).not.toBe(firstAnnotation);
18+
});
19+
20+
it("never repeats an id within one page load", () => {
21+
const nextId = createAnnotationIdSource();
22+
const ids = Array.from({ length: 1_000 }, () => nextId("annotation"));
23+
expect(new Set(ids).size).toBe(ids.length);
24+
});
25+
});
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* Id source for picker annotations and their targets, one per page load.
3+
*
4+
* The composer keeps annotations keyed by id across preview reloads, and this
5+
* preload restarts on every page load, so a bare counter reissues ids that a
6+
* draft already holds. `getRandomValues` rather than `randomUUID`, because
7+
* plain-http previews (LAN, tailnet) are not secure contexts.
8+
*/
9+
export function createAnnotationIdSource(): (prefix: string) => string {
10+
const pageLoad = Array.from(crypto.getRandomValues(new Uint32Array(2)), (part) =>
11+
part.toString(36),
12+
).join("");
13+
let sequence = 0;
14+
return (prefix) => {
15+
sequence += 1;
16+
return `${prefix}_${pageLoad}_${sequence.toString(36)}`;
17+
};
18+
}

‎apps/desktop/src/preview/PickPreload.ts‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import type {
1414
PreviewAnnotationSubmission,
1515
} from "@t3tools/contracts";
1616

17+
import { createAnnotationIdSource } from "./AnnotationIds.ts";
1718
import { resolveAnnotationSubmission } from "./AnnotationKeyboard.ts";
1819
import { previewAnnotationStyles } from "./AnnotationStyles.generated.ts";
1920
import { installRecordingCursor } from "./RecordingCursor.ts";
@@ -132,7 +133,6 @@ interface AnnotationSession {
132133
}
133134

134135
let activeSession: AnnotationSession | null = null;
135-
let idSequence = 0;
136136
let annotationTheme: DesktopPreviewAnnotationTheme | null = null;
137137

138138
const applyAnnotationTheme = (
@@ -220,10 +220,7 @@ window.addEventListener("mousedown", suppressNavigationButton, true);
220220
window.addEventListener("mouseup", requestNavigationForButton, true);
221221
window.addEventListener("auxclick", suppressNavigationButton, true);
222222

223-
const nextId = (prefix: string): string => {
224-
idSequence += 1;
225-
return `${prefix}_${idSequence.toString(36)}`;
226-
};
223+
const nextId = createAnnotationIdSource();
227224

228225
const rectFromDomRect = (rect: DOMRect): PreviewAnnotationRect => ({
229226
x: rect.left,

0 commit comments

Comments
 (0)