@xynolog/react-ui 0.0.0-stage → 0.8.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (146) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +359 -2
  3. package/dist/PdfAnnotationCanvas.client-S64VRLUN.css +193 -0
  4. package/dist/PdfAnnotationCanvas.client-V7R4NXFO.js +799 -0
  5. package/dist/ar-AIV7P4FL.js +594 -0
  6. package/dist/bn-J2OR57PB.js +594 -0
  7. package/dist/chunk-4L3TYOIV.js +172 -0
  8. package/dist/chunk-BM76QBIO.js +594 -0
  9. package/dist/chunk-D2O46PZQ.js +240 -0
  10. package/dist/chunk-O23TLLXW.js +42 -0
  11. package/dist/chunk-WGTUIB6C.js +304 -0
  12. package/dist/cs-JNEAMHFB.js +594 -0
  13. package/dist/da-UY5ZRVTZ.js +594 -0
  14. package/dist/de-EETVXC42.js +594 -0
  15. package/dist/el-S5YKDMTL.js +594 -0
  16. package/dist/en-3YQWF45H.js +61 -0
  17. package/dist/es-GHA7S27R.js +594 -0
  18. package/dist/fa-JTR3GF6I.js +594 -0
  19. package/dist/fi-CQZ7BW57.js +594 -0
  20. package/dist/fr-OJH2WIY7.js +594 -0
  21. package/dist/he-2DBXWA2V.js +594 -0
  22. package/dist/hi-NZP7B55I.js +594 -0
  23. package/dist/id-4KXFKWOI.js +594 -0
  24. package/dist/index.css +4134 -0
  25. package/dist/index.d.ts +4040 -0
  26. package/dist/index.js +29638 -0
  27. package/dist/it-XBMYGKKY.js +594 -0
  28. package/dist/ja-OVHZS3EH.js +594 -0
  29. package/dist/ko-NW2MVM5L.js +594 -0
  30. package/dist/ms-RXLPBZLK.js +594 -0
  31. package/dist/nl-77CTBEDA.js +594 -0
  32. package/dist/no-RAPO56SE.js +594 -0
  33. package/dist/pl-XCFXZCTT.js +594 -0
  34. package/dist/pt-EMBE27IS.js +594 -0
  35. package/dist/ro-M7QLJBYN.js +594 -0
  36. package/dist/sv-ST6CITQY.js +594 -0
  37. package/dist/test-utils.css +112 -0
  38. package/dist/test-utils.d.ts +126 -0
  39. package/dist/test-utils.js +13119 -0
  40. package/dist/th-EEQGL3G7.js +594 -0
  41. package/dist/tr-5FYHZQKD.js +594 -0
  42. package/dist/uk-VTWDC467.js +594 -0
  43. package/dist/vi-ISBKJ66X.js +594 -0
  44. package/dist/zh-2AW2N66Z.js +594 -0
  45. package/package.json +132 -4
  46. package/src/components/ProtectedErrorBoundary.css +121 -0
  47. package/src/components/SessionExpiryBanner.css +67 -0
  48. package/src/components/StatusDisplay.css +439 -0
  49. package/src/components/Toast.css +136 -0
  50. package/src/components/annotation/annotation-entries.css +138 -0
  51. package/src/components/annotation/annotations.css +93 -0
  52. package/src/components/annotation/references.css +300 -0
  53. package/src/components/branding/Branding.css +129 -0
  54. package/src/components/error-states/errors.css +34 -0
  55. package/src/components/layout/Header.css +99 -0
  56. package/src/components/layout/LeftSidebar.css +115 -0
  57. package/src/components/layout/ResizeHandle.css +110 -0
  58. package/src/components/layout/SkipLinks.css +84 -0
  59. package/src/components/loading-states/loading.css +42 -0
  60. package/src/components/modals/SearchModal.css +1019 -0
  61. package/src/components/modals/modals.css +655 -0
  62. package/src/components/navigation/CollapsibleResourceNavigation.css +10 -0
  63. package/src/components/navigation/NavigationMenu.css +59 -0
  64. package/src/components/navigation/NavigationTabs.css +466 -0
  65. package/src/components/navigation/SimpleNavigation.css +9 -0
  66. package/src/components/pdf-annotation/PdfAnnotationCanvas.css +288 -0
  67. package/src/components/resource/panels/CollaborationPanel.css +184 -0
  68. package/src/components/resource/panels/DelegateSection.css +330 -0
  69. package/src/components/resource/panels/JsonLdPanel.css +37 -0
  70. package/src/components/resource/panels/ReferencesPanel.css +108 -0
  71. package/src/components/resource/panels/ResourceInfoPanel.css +183 -0
  72. package/src/components/resource/panels/StatisticsPanel.css +127 -0
  73. package/src/components/resource/panels/UnifiedAnnotationsPanel.css +129 -0
  74. package/src/components/settings/SettingsPanel.css +92 -0
  75. package/src/components/toolbar/Toolbar.css +245 -0
  76. package/src/features/auth/auth.css +120 -0
  77. package/src/styles/base/reset.css +105 -0
  78. package/src/styles/base/utilities.css +35 -0
  79. package/src/styles/core/badges.css +17 -0
  80. package/src/styles/core/buttons.css +772 -0
  81. package/src/styles/core/checkboxes.css +85 -0
  82. package/src/styles/core/forms.css +237 -0
  83. package/src/styles/core/index.css +43 -0
  84. package/src/styles/core/indicators.css +24 -0
  85. package/src/styles/core/inputs.css +164 -0
  86. package/src/styles/core/progress.css +85 -0
  87. package/src/styles/core/selects.css +119 -0
  88. package/src/styles/core/sliders.css +180 -0
  89. package/src/styles/core/tags.css +86 -0
  90. package/src/styles/core/textareas.css +104 -0
  91. package/src/styles/core/toggles.css +82 -0
  92. package/src/styles/features/compose.css +341 -0
  93. package/src/styles/features/entity-tags.css +140 -0
  94. package/src/styles/features/recent-docs.css +97 -0
  95. package/src/styles/features/resource-discovery.css +349 -0
  96. package/src/styles/features/resource-viewer.css +609 -0
  97. package/src/styles/features/resource.css +483 -0
  98. package/src/styles/features/schemas.css +222 -0
  99. package/src/styles/index.css +84 -0
  100. package/src/styles/layout/layout.css +28 -0
  101. package/src/styles/motivations/motivation-assessment.css +179 -0
  102. package/src/styles/motivations/motivation-comment.css +164 -0
  103. package/src/styles/motivations/motivation-highlight.css +132 -0
  104. package/src/styles/motivations/motivation-reference.css +167 -0
  105. package/src/styles/motivations/motivation-tag.css +182 -0
  106. package/src/styles/panels/history-panel.css +212 -0
  107. package/src/styles/panels/user-panel.css +21 -0
  108. package/src/styles/patterns/cards.css +26 -0
  109. package/src/styles/patterns/panel-helpers.css +32 -0
  110. package/src/styles/patterns/panels-base.css +272 -0
  111. package/src/styles/utilities/contrast.css +280 -0
  112. package/src/styles/utilities/focus-extended.css +276 -0
  113. package/src/styles/utilities/focus.css +41 -0
  114. package/src/styles/utilities/motion-overrides.css +286 -0
  115. package/src/styles/utilities/motion.css +184 -0
  116. package/src/styles/utilities/semantic-indicators.css +254 -0
  117. package/src/styles/variables.css +410 -0
  118. package/translations/ar.json +534 -0
  119. package/translations/bn.json +534 -0
  120. package/translations/cs.json +534 -0
  121. package/translations/da.json +534 -0
  122. package/translations/de.json +534 -0
  123. package/translations/el.json +534 -0
  124. package/translations/en.json +534 -0
  125. package/translations/es.json +534 -0
  126. package/translations/fa.json +534 -0
  127. package/translations/fi.json +534 -0
  128. package/translations/fr.json +534 -0
  129. package/translations/he.json +534 -0
  130. package/translations/hi.json +534 -0
  131. package/translations/id.json +534 -0
  132. package/translations/it.json +534 -0
  133. package/translations/ja.json +534 -0
  134. package/translations/ko.json +534 -0
  135. package/translations/ms.json +534 -0
  136. package/translations/nl.json +534 -0
  137. package/translations/no.json +534 -0
  138. package/translations/pl.json +534 -0
  139. package/translations/pt.json +534 -0
  140. package/translations/ro.json +534 -0
  141. package/translations/sv.json +534 -0
  142. package/translations/th.json +534 -0
  143. package/translations/tr.json +534 -0
  144. package/translations/uk.json +534 -0
  145. package/translations/vi.json +534 -0
  146. package/translations/zh.json +534 -0
@@ -0,0 +1,799 @@
1
+ 'use client';
2
+ "use client";
3
+ import {
4
+ AnchoredTextNotice,
5
+ loadPdfDocument,
6
+ renderPdfPageToDataUrl,
7
+ toViewportAnchorRect,
8
+ useAnchoredText,
9
+ useHoverDwell
10
+ } from "./chunk-4L3TYOIV.js";
11
+ import {
12
+ useTranslations
13
+ } from "./chunk-D2O46PZQ.js";
14
+ import "./chunk-O23TLLXW.js";
15
+
16
+ // src/components/pdf-annotation/PdfAnnotationCanvas.tsx
17
+ import { useCallback as useCallback2, useEffect as useEffect2, useMemo, useRef as useRef2, useState as useState2 } from "react";
18
+
19
+ // src/components/pdf-annotation/estimate-slot-height.ts
20
+ function estimateSlotHeight(columnWidth, rasterWidth, aspect) {
21
+ if (!columnWidth || !rasterWidth || !aspect) return null;
22
+ if (columnWidth <= 0 || rasterWidth <= 0 || aspect <= 0) return null;
23
+ return Math.round(Math.min(columnWidth, rasterWidth) * aspect);
24
+ }
25
+
26
+ // src/components/pdf-annotation/PdfPageView.tsx
27
+ import { useCallback, useEffect, useRef, useState } from "react";
28
+ import { createFragmentSelector, textUnder } from "@xynolog/core";
29
+
30
+ // src/components/pdf-annotation/rects-for-page.ts
31
+ import { getTargetSelector, parseFragmentSelector } from "@xynolog/core";
32
+ function fragmentSelectors(target) {
33
+ const selector = getTargetSelector(target);
34
+ if (!selector) return [];
35
+ const selectors = Array.isArray(selector) ? selector : [selector];
36
+ return selectors.filter((s) => s.type === "FragmentSelector").map((s) => s);
37
+ }
38
+ function rectsForPage(annotations, pageNumber) {
39
+ const rects = [];
40
+ for (const annotation of annotations) {
41
+ fragmentSelectors(annotation.target).forEach((sel, selectorIndex) => {
42
+ const coord = parseFragmentSelector(sel.value);
43
+ if (coord && coord.page === pageNumber) {
44
+ rects.push({ annId: annotation.id, selectorIndex, coord, annotation });
45
+ }
46
+ });
47
+ }
48
+ return rects;
49
+ }
50
+
51
+ // src/lib/pdf-coordinates.ts
52
+ function canvasToPdfCoordinates(canvasRect, page, _pageWidth, pageHeight, scale = 1) {
53
+ const x1 = Math.min(canvasRect.startX, canvasRect.endX);
54
+ const y1 = Math.min(canvasRect.startY, canvasRect.endY);
55
+ const x2 = Math.max(canvasRect.startX, canvasRect.endX);
56
+ const y2 = Math.max(canvasRect.startY, canvasRect.endY);
57
+ const pdfX = x1 / scale;
58
+ const pdfWidth = (x2 - x1) / scale;
59
+ const pdfY = pageHeight - y2 / scale;
60
+ const pdfHeight = (y2 - y1) / scale;
61
+ return {
62
+ page,
63
+ x: Math.round(pdfX),
64
+ y: Math.round(pdfY),
65
+ width: Math.round(pdfWidth),
66
+ height: Math.round(pdfHeight)
67
+ };
68
+ }
69
+ function pdfToCanvasCoordinates(pdfCoord, pageHeight, scale = 1) {
70
+ const canvasX = pdfCoord.x * scale;
71
+ const canvasWidth = pdfCoord.width * scale;
72
+ const canvasY = (pageHeight - pdfCoord.y - pdfCoord.height) * scale;
73
+ const canvasHeight = pdfCoord.height * scale;
74
+ return {
75
+ x: canvasX,
76
+ y: canvasY,
77
+ width: canvasWidth,
78
+ height: canvasHeight
79
+ };
80
+ }
81
+
82
+ // src/components/pdf-annotation/PdfPageView.tsx
83
+ import { jsx, jsxs } from "react/jsx-runtime";
84
+ function getMotivationColor(motivation) {
85
+ if (!motivation) {
86
+ return { stroke: "rgb(156, 163, 175)", fill: "rgba(156, 163, 175, 0.2)" };
87
+ }
88
+ switch (motivation) {
89
+ case "highlighting":
90
+ return { stroke: "rgb(250, 204, 21)", fill: "rgba(250, 204, 21, 0.3)" };
91
+ case "linking":
92
+ return { stroke: "rgb(59, 130, 246)", fill: "rgba(59, 130, 246, 0.2)" };
93
+ case "assessing":
94
+ return { stroke: "rgb(239, 68, 68)", fill: "rgba(239, 68, 68, 0.2)" };
95
+ case "commenting":
96
+ return { stroke: "rgb(255, 255, 255)", fill: "rgba(255, 255, 255, 0.2)" };
97
+ default:
98
+ return { stroke: "rgb(156, 163, 175)", fill: "rgba(156, 163, 175, 0.2)" };
99
+ }
100
+ }
101
+ function PdfPageView({
102
+ doc,
103
+ pageNumber,
104
+ scale,
105
+ zoom,
106
+ resourceUri,
107
+ existingAnnotations,
108
+ drawing,
109
+ selectedMotivation,
110
+ session,
111
+ hoveredAnnotationId,
112
+ selectedAnnotationId,
113
+ hoverDelayMs
114
+ }) {
115
+ const [pageImageUrl, setPageImageUrl] = useState(null);
116
+ const [pageDimensions, setPageDimensions] = useState(null);
117
+ const [displayDimensions, setDisplayDimensions] = useState(null);
118
+ const [errorKey, setErrorKey] = useState(null);
119
+ const t = useTranslations("PdfViewer");
120
+ const [isDrawing, setIsDrawing] = useState(false);
121
+ const [selection, setSelection] = useState(null);
122
+ const imageRef = useRef(null);
123
+ useEffect(() => {
124
+ let cancelled = false;
125
+ async function loadPage() {
126
+ try {
127
+ const page = await doc.getPage(pageNumber);
128
+ if (cancelled) return;
129
+ const viewport = page.getViewport({ scale: 1 });
130
+ setPageDimensions({ width: viewport.width, height: viewport.height });
131
+ const { dataUrl } = await renderPdfPageToDataUrl(page, scale);
132
+ if (cancelled) return;
133
+ setPageImageUrl(dataUrl);
134
+ } catch (err) {
135
+ if (cancelled) return;
136
+ console.error("Error loading page:", err);
137
+ setErrorKey("pageLoadFailed");
138
+ }
139
+ }
140
+ loadPage();
141
+ return () => {
142
+ cancelled = true;
143
+ };
144
+ }, [doc, pageNumber, scale]);
145
+ useEffect(() => {
146
+ const updateDisplayDimensions = () => {
147
+ if (imageRef.current) {
148
+ setDisplayDimensions({
149
+ width: imageRef.current.clientWidth,
150
+ height: imageRef.current.clientHeight
151
+ });
152
+ }
153
+ };
154
+ updateDisplayDimensions();
155
+ let resizeObserver = null;
156
+ try {
157
+ resizeObserver = new ResizeObserver(updateDisplayDimensions);
158
+ if (imageRef.current) {
159
+ resizeObserver.observe(imageRef.current);
160
+ }
161
+ } catch (error) {
162
+ console.warn(
163
+ "ResizeObserver not supported, falling back to window resize listener"
164
+ );
165
+ window.addEventListener("resize", updateDisplayDimensions);
166
+ }
167
+ return () => {
168
+ if (resizeObserver) {
169
+ resizeObserver.disconnect();
170
+ } else {
171
+ window.removeEventListener("resize", updateDisplayDimensions);
172
+ }
173
+ };
174
+ }, [pageImageUrl]);
175
+ const handleMouseDown = useCallback(
176
+ (e) => {
177
+ if (!drawing) return;
178
+ if (!imageRef.current) return;
179
+ const rect = imageRef.current.getBoundingClientRect();
180
+ const x = (e.clientX - rect.left) / zoom;
181
+ const y = (e.clientY - rect.top) / zoom;
182
+ setIsDrawing(true);
183
+ setSelection({
184
+ startX: x,
185
+ startY: y,
186
+ endX: x,
187
+ endY: y
188
+ });
189
+ },
190
+ [drawing, zoom]
191
+ );
192
+ const handleMouseMove = useCallback(
193
+ (e) => {
194
+ if (!isDrawing || !selection || !imageRef.current) return;
195
+ const rect = imageRef.current.getBoundingClientRect();
196
+ setSelection({
197
+ ...selection,
198
+ endX: (e.clientX - rect.left) / zoom,
199
+ endY: (e.clientY - rect.top) / zoom
200
+ });
201
+ },
202
+ [isDrawing, selection, zoom]
203
+ );
204
+ const handleMouseUp = useCallback(() => {
205
+ if (!isDrawing || !selection || !pageDimensions || !displayDimensions || !session || !drawing) {
206
+ setIsDrawing(false);
207
+ setSelection(null);
208
+ return;
209
+ }
210
+ const dragDistance = Math.sqrt(
211
+ Math.pow(selection.endX - selection.startX, 2) + Math.pow(selection.endY - selection.startY, 2)
212
+ );
213
+ const MIN_DRAG_DISTANCE = 10;
214
+ if (dragDistance < MIN_DRAG_DISTANCE) {
215
+ if (existingAnnotations.length > 0) {
216
+ let hitRect;
217
+ const hit = rectsForPage(existingAnnotations, pageNumber).find((r) => {
218
+ const rect = pdfToCanvasCoordinates(
219
+ r.coord,
220
+ pageDimensions.height,
221
+ 1
222
+ );
223
+ const scaleX2 = displayDimensions.width / pageDimensions.width;
224
+ const scaleY2 = displayDimensions.height / pageDimensions.height;
225
+ const displayX = rect.x * scaleX2;
226
+ const displayY = rect.y * scaleY2;
227
+ const displayWidth = rect.width * scaleX2;
228
+ const displayHeight = rect.height * scaleY2;
229
+ const inside = selection.endX >= displayX && selection.endX <= displayX + displayWidth && selection.endY >= displayY && selection.endY <= displayY + displayHeight;
230
+ if (inside && imageRef.current) {
231
+ hitRect = toViewportAnchorRect(
232
+ imageRef.current.getBoundingClientRect(),
233
+ displayX * zoom,
234
+ displayY * zoom,
235
+ displayWidth * zoom,
236
+ displayHeight * zoom
237
+ );
238
+ }
239
+ return inside;
240
+ });
241
+ if (hit) {
242
+ session?.client.browse.click(hit.annId, hitRect);
243
+ setIsDrawing(false);
244
+ setSelection(null);
245
+ return;
246
+ }
247
+ }
248
+ setIsDrawing(false);
249
+ setSelection(null);
250
+ return;
251
+ }
252
+ const scaleX = pageDimensions.width / displayDimensions.width;
253
+ const scaleY = pageDimensions.height / displayDimensions.height;
254
+ const nativeSelection = {
255
+ startX: selection.startX * scaleX,
256
+ startY: selection.startY * scaleY,
257
+ endX: selection.endX * scaleX,
258
+ endY: selection.endY * scaleY
259
+ };
260
+ const pdfCoord = canvasToPdfCoordinates(
261
+ nativeSelection,
262
+ pageNumber,
263
+ pageDimensions.width,
264
+ pageDimensions.height,
265
+ 1
266
+ // Use scale 1.0 since we already scaled to native coords
267
+ );
268
+ const fragmentSelector = createFragmentSelector(pdfCoord);
269
+ const quoted = textUnder(drawing.text, pdfCoord);
270
+ if (selectedMotivation) {
271
+ session.client.mark.request(
272
+ resourceUri,
273
+ [
274
+ {
275
+ type: "FragmentSelector",
276
+ conformsTo: "http://tools.ietf.org/rfc/rfc3778",
277
+ value: fragmentSelector
278
+ },
279
+ ...quoted ? [{ type: "TextQuoteSelector", exact: quoted }] : []
280
+ ],
281
+ selectedMotivation
282
+ );
283
+ }
284
+ setIsDrawing(false);
285
+ }, [
286
+ isDrawing,
287
+ selection,
288
+ pageNumber,
289
+ pageDimensions,
290
+ displayDimensions,
291
+ selectedMotivation,
292
+ existingAnnotations,
293
+ session,
294
+ resourceUri,
295
+ drawing,
296
+ zoom
297
+ ]);
298
+ const pageRects = rectsForPage(existingAnnotations, pageNumber);
299
+ const dwell = useHoverDwell(session, hoverDelayMs);
300
+ const { stroke, fill } = getMotivationColor(selectedMotivation ?? null);
301
+ if (errorKey) {
302
+ return /* @__PURE__ */ jsx("div", { className: "xynolog-pdf-annotation-canvas__error", children: t(errorKey) });
303
+ }
304
+ return /* @__PURE__ */ jsxs(
305
+ "div",
306
+ {
307
+ className: "xynolog-pdf-annotation-canvas__container",
308
+ onMouseDown: handleMouseDown,
309
+ onMouseMove: handleMouseMove,
310
+ onMouseUp: handleMouseUp,
311
+ onMouseLeave: () => {
312
+ if (isDrawing) {
313
+ setIsDrawing(false);
314
+ setSelection(null);
315
+ }
316
+ },
317
+ "data-drawing-mode": drawing?.mode ?? "none",
318
+ children: [
319
+ pageImageUrl && /* @__PURE__ */ jsx(
320
+ "img",
321
+ {
322
+ ref: imageRef,
323
+ src: pageImageUrl,
324
+ alt: `PDF page ${pageNumber}`,
325
+ className: "xynolog-pdf-annotation-canvas__image",
326
+ draggable: false,
327
+ style: { pointerEvents: "none" },
328
+ onLoad: () => {
329
+ requestAnimationFrame(() => {
330
+ requestAnimationFrame(() => {
331
+ if (imageRef.current) {
332
+ setDisplayDimensions({
333
+ width: imageRef.current.clientWidth,
334
+ height: imageRef.current.clientHeight
335
+ });
336
+ }
337
+ });
338
+ });
339
+ }
340
+ }
341
+ ),
342
+ displayDimensions && pageDimensions && /* @__PURE__ */ jsx("div", { className: "xynolog-pdf-annotation-canvas__overlay-container", children: /* @__PURE__ */ jsx("div", { className: "xynolog-pdf-annotation-canvas__overlay", children: /* @__PURE__ */ jsxs(
343
+ "svg",
344
+ {
345
+ className: "xynolog-pdf-annotation-canvas__svg",
346
+ width: displayDimensions.width,
347
+ height: displayDimensions.height,
348
+ children: [
349
+ pageRects.map((r) => {
350
+ const rect = pdfToCanvasCoordinates(
351
+ r.coord,
352
+ pageDimensions.height,
353
+ 1
354
+ );
355
+ const scaleX = displayDimensions.width / pageDimensions.width;
356
+ const scaleY = displayDimensions.height / pageDimensions.height;
357
+ const isHovered = r.annId === hoveredAnnotationId;
358
+ const isSelected = r.annId === selectedAnnotationId;
359
+ const annMotivation = r.annotation.motivation;
360
+ const { stroke: annStroke, fill: annFill } = getMotivationColor(annMotivation);
361
+ return /* @__PURE__ */ jsx(
362
+ "rect",
363
+ {
364
+ x: rect.x * scaleX,
365
+ y: rect.y * scaleY,
366
+ width: rect.width * scaleX,
367
+ height: rect.height * scaleY,
368
+ stroke: annStroke,
369
+ strokeWidth: isSelected ? 4 : isHovered ? 3 : 2,
370
+ fill: annFill,
371
+ style: {
372
+ pointerEvents: "auto",
373
+ cursor: "pointer",
374
+ opacity: isSelected ? 1 : isHovered ? 0.9 : 0.7
375
+ },
376
+ onClick: (e) => session?.client.browse.click(
377
+ r.annId,
378
+ e.currentTarget.getBoundingClientRect()
379
+ ),
380
+ onMouseEnter: () => dwell?.enter(r.annId),
381
+ onMouseLeave: () => dwell?.leave()
382
+ },
383
+ `${r.annId}:${r.selectorIndex}`
384
+ );
385
+ }),
386
+ selection && (() => {
387
+ const rectX = Math.min(selection.startX, selection.endX);
388
+ const rectY = Math.min(selection.startY, selection.endY);
389
+ const rectWidth = Math.abs(selection.endX - selection.startX);
390
+ const rectHeight = Math.abs(
391
+ selection.endY - selection.startY
392
+ );
393
+ return /* @__PURE__ */ jsx(
394
+ "rect",
395
+ {
396
+ x: rectX,
397
+ y: rectY,
398
+ width: rectWidth,
399
+ height: rectHeight,
400
+ stroke,
401
+ strokeWidth: 2,
402
+ strokeDasharray: "5,5",
403
+ fill,
404
+ pointerEvents: "none"
405
+ }
406
+ );
407
+ })()
408
+ ]
409
+ }
410
+ ) }) })
411
+ ]
412
+ }
413
+ );
414
+ }
415
+
416
+ // src/components/pdf-annotation/PdfAnnotationCanvas.tsx
417
+ import { HOVER_DELAY_MS } from "@xynolog/sdk";
418
+ import { jsx as jsx2, jsxs as jsxs2 } from "react/jsx-runtime";
419
+ var PRELOAD_MARGIN = "100% 0px";
420
+ var STRIP_PAGE_WIDTH = 30;
421
+ var SCROLL_AXIS = "vertical";
422
+ function scrollWithin(scrollport, target, align) {
423
+ if (!target) return;
424
+ if (!scrollport || typeof scrollport.scrollTo !== "function") {
425
+ target.scrollIntoView?.({ block: align });
426
+ return;
427
+ }
428
+ const portRect = scrollport.getBoundingClientRect();
429
+ const targetRect = target.getBoundingClientRect();
430
+ const top = targetRect.top - (portRect.top + scrollport.clientTop);
431
+ const bottom = targetRect.bottom - (portRect.top + scrollport.clientTop + scrollport.clientHeight);
432
+ if (align === "nearest" && top >= 0 && bottom <= 0) return;
433
+ scrollport.scrollTo({ top: scrollport.scrollTop + top, behavior: "instant" });
434
+ }
435
+ function PdfAnnotationCanvas({
436
+ pdfUrl,
437
+ resourceUri,
438
+ existingAnnotations = [],
439
+ drawingMode,
440
+ selectedMotivation,
441
+ session,
442
+ hoveredAnnotationId,
443
+ selectedAnnotationId,
444
+ hoverDelayMs = HOVER_DELAY_MS,
445
+ zoom = 1,
446
+ ownsScrollport = true,
447
+ pageLayout = "paged"
448
+ }) {
449
+ const [pdfDoc, setPdfDoc] = useState2(null);
450
+ const [numPages, setNumPages] = useState2(0);
451
+ const [pageNumber, setPageNumber] = useState2(1);
452
+ const [isLoading, setIsLoading] = useState2(true);
453
+ const [errorKey, setErrorKey] = useState2(null);
454
+ const t = useTranslations("PdfViewer");
455
+ const [pageShape, setPageShape] = useState2(null);
456
+ const [columnWidth, setColumnWidth] = useState2(null);
457
+ const scrollportRef = useRef2(null);
458
+ const columnRef = useRef2(null);
459
+ const [scale] = useState2(1.5);
460
+ const [visiblePages, setVisiblePages] = useState2(/* @__PURE__ */ new Set());
461
+ const [onscreenPages, setOnscreenPages] = useState2(/* @__PURE__ */ new Set());
462
+ const slotRefs = useRef2(/* @__PURE__ */ new Map());
463
+ const observerRef = useRef2(null);
464
+ const onscreenRef = useRef2(null);
465
+ useEffect2(() => {
466
+ let cancelled = false;
467
+ async function loadPdf() {
468
+ try {
469
+ setIsLoading(true);
470
+ setErrorKey(null);
471
+ const doc = await loadPdfDocument(pdfUrl);
472
+ if (cancelled) return;
473
+ setPdfDoc(doc);
474
+ setNumPages(doc.numPages);
475
+ setIsLoading(false);
476
+ try {
477
+ const first = await doc.getPage(1);
478
+ if (cancelled) return;
479
+ const natural = first.getViewport({ scale: 1 });
480
+ setPageShape({
481
+ aspect: natural.height / natural.width,
482
+ rasterWidth: first.getViewport({ scale }).width
483
+ });
484
+ } catch {
485
+ }
486
+ } catch (err) {
487
+ if (cancelled) return;
488
+ console.error("Error loading PDF:", err);
489
+ setErrorKey("loadFailed");
490
+ setIsLoading(false);
491
+ }
492
+ }
493
+ loadPdf();
494
+ return () => {
495
+ cancelled = true;
496
+ };
497
+ }, [pdfUrl, scale]);
498
+ const anchoredText = useAnchoredText(session, resourceUri);
499
+ const drawing = useMemo(
500
+ () => anchoredText.status === "held" && drawingMode ? { mode: drawingMode, text: anchoredText.text } : null,
501
+ [anchoredText, drawingMode]
502
+ );
503
+ useEffect2(() => {
504
+ if (pageLayout !== "scroll") return;
505
+ const measure = () => {
506
+ if (columnRef.current) setColumnWidth(columnRef.current.clientWidth);
507
+ };
508
+ measure();
509
+ let observer = null;
510
+ try {
511
+ observer = new ResizeObserver(measure);
512
+ if (columnRef.current) observer.observe(columnRef.current);
513
+ } catch {
514
+ window.addEventListener("resize", measure);
515
+ }
516
+ return () => {
517
+ if (observer) observer.disconnect();
518
+ else window.removeEventListener("resize", measure);
519
+ };
520
+ }, [pageLayout]);
521
+ const slotHeight = estimateSlotHeight(
522
+ columnWidth,
523
+ pageShape?.rasterWidth ?? null,
524
+ pageShape?.aspect ?? null
525
+ ) ?? // Before the column has been measured there is still a sane reservation:
526
+ // the raster's own height. Wrong in a narrow column, but every slot is
527
+ // wrong by the SAME amount, so the column is at least internally
528
+ // consistent until the first measurement lands.
529
+ (pageShape ? Math.round(pageShape.rasterWidth * pageShape.aspect) : null);
530
+ useEffect2(() => {
531
+ if (pageLayout !== "scroll" || numPages === 0) return;
532
+ let observer = null;
533
+ try {
534
+ observer = new IntersectionObserver(
535
+ (entries) => {
536
+ setVisiblePages((prev) => {
537
+ const next = new Set(prev);
538
+ for (const entry of entries) {
539
+ const page = Number(entry.target.dataset.page);
540
+ if (!page) continue;
541
+ if (entry.isIntersecting) next.add(page);
542
+ else next.delete(page);
543
+ }
544
+ return next;
545
+ });
546
+ },
547
+ {
548
+ root: ownsScrollport ? scrollportRef.current : null,
549
+ rootMargin: PRELOAD_MARGIN
550
+ }
551
+ );
552
+ } catch {
553
+ setVisiblePages(
554
+ new Set(Array.from({ length: numPages }, (_, i) => i + 1))
555
+ );
556
+ return;
557
+ }
558
+ observerRef.current = observer;
559
+ for (const el of slotRefs.current.values()) observer.observe(el);
560
+ let onscreen = null;
561
+ try {
562
+ onscreen = new IntersectionObserver(
563
+ (entries) => {
564
+ setOnscreenPages((prev) => {
565
+ const next = new Set(prev);
566
+ for (const entry of entries) {
567
+ const page = Number(entry.target.dataset.page);
568
+ if (!page) continue;
569
+ if (entry.isIntersecting) next.add(page);
570
+ else next.delete(page);
571
+ }
572
+ return next;
573
+ });
574
+ },
575
+ { root: ownsScrollport ? scrollportRef.current : null }
576
+ );
577
+ onscreenRef.current = onscreen;
578
+ for (const el of slotRefs.current.values()) onscreen.observe(el);
579
+ } catch {
580
+ setOnscreenPages(/* @__PURE__ */ new Set([1]));
581
+ }
582
+ return () => {
583
+ observer.disconnect();
584
+ observerRef.current = null;
585
+ onscreen?.disconnect();
586
+ onscreenRef.current = null;
587
+ };
588
+ }, [ownsScrollport, pageLayout, numPages]);
589
+ const registerSlot = useCallback2((el) => {
590
+ const page = Number(el.dataset.page);
591
+ slotRefs.current.set(page, el);
592
+ observerRef.current?.observe(el);
593
+ onscreenRef.current?.observe(el);
594
+ return () => {
595
+ observerRef.current?.unobserve(el);
596
+ onscreenRef.current?.unobserve(el);
597
+ slotRefs.current.delete(page);
598
+ };
599
+ }, []);
600
+ const currentTickRef = useRef2(null);
601
+ const scrollToPage = useCallback2(
602
+ (page) => {
603
+ scrollWithin(
604
+ ownsScrollport ? scrollportRef.current : null,
605
+ slotRefs.current.get(page) ?? null,
606
+ "start"
607
+ );
608
+ },
609
+ [ownsScrollport]
610
+ );
611
+ const currentPage = pageLayout === "scroll" ? onscreenPages.size > 0 ? Math.min(...onscreenPages) : 1 : pageNumber;
612
+ useEffect2(() => {
613
+ if (numPages === 0) return;
614
+ const BACK = ["ArrowLeft", "PageUp"];
615
+ const FORWARD = ["ArrowRight", "PageDown"];
616
+ const onKeyDown = (e) => {
617
+ if (!BACK.includes(e.key) && !FORWARD.includes(e.key)) return;
618
+ if (e.metaKey || e.ctrlKey || e.altKey) return;
619
+ const el = e.target;
620
+ const tag = el?.tagName;
621
+ if (tag === "INPUT" || tag === "TEXTAREA" || tag === "SELECT" || el?.isContentEditable)
622
+ return;
623
+ const next = FORWARD.includes(e.key) ? currentPage + 1 : currentPage - 1;
624
+ if (next < 1 || next > numPages) return;
625
+ e.preventDefault();
626
+ if (pageLayout === "scroll") scrollToPage(next);
627
+ else setPageNumber(next);
628
+ };
629
+ window.addEventListener("keydown", onKeyDown);
630
+ return () => window.removeEventListener("keydown", onKeyDown);
631
+ }, [numPages, currentPage, pageLayout, scrollToPage]);
632
+ const stripRef = useRef2(null);
633
+ useEffect2(() => {
634
+ const strip = stripRef.current;
635
+ const clip = scrollportRef.current;
636
+ if (pageLayout !== "scroll" || !ownsScrollport || !strip || !clip) return;
637
+ const size = () => {
638
+ strip.style.maxHeight = `${clip.clientHeight}px`;
639
+ };
640
+ size();
641
+ let observer = null;
642
+ try {
643
+ observer = new ResizeObserver(size);
644
+ observer.observe(clip);
645
+ } catch {
646
+ window.addEventListener("resize", size);
647
+ }
648
+ return () => {
649
+ if (observer) observer.disconnect();
650
+ else window.removeEventListener("resize", size);
651
+ };
652
+ }, [ownsScrollport, pageLayout, numPages, pageShape]);
653
+ useEffect2(() => {
654
+ scrollWithin(stripRef.current, currentTickRef.current, "nearest");
655
+ const active = typeof document !== "undefined" ? document.activeElement : null;
656
+ if (active && stripRef.current?.contains(active) && active !== currentTickRef.current) {
657
+ currentTickRef.current?.focus();
658
+ }
659
+ }, [currentPage]);
660
+ const pageProps = {
661
+ scale,
662
+ zoom,
663
+ resourceUri,
664
+ existingAnnotations,
665
+ drawing,
666
+ selectedMotivation,
667
+ session,
668
+ hoveredAnnotationId,
669
+ selectedAnnotationId,
670
+ hoverDelayMs
671
+ };
672
+ if (errorKey) {
673
+ return /* @__PURE__ */ jsx2("div", { className: "xynolog-pdf-annotation-canvas__error", children: t(errorKey) });
674
+ }
675
+ return /* @__PURE__ */ jsxs2(
676
+ "div",
677
+ {
678
+ ref: scrollportRef,
679
+ className: "xynolog-pdf-annotation-canvas",
680
+ "data-page-layout": pageLayout,
681
+ "data-resource-scrollport": pageLayout === "scroll" && ownsScrollport ? "" : void 0,
682
+ "data-annotate-deferred": anchoredText.status === "held" ? "false" : "true",
683
+ children: [
684
+ isLoading && /* @__PURE__ */ jsx2("div", { className: "xynolog-pdf-annotation-canvas__loading", children: t("loading") }),
685
+ /* @__PURE__ */ jsx2(AnchoredTextNotice, { status: anchoredText.status }),
686
+ pdfDoc && pageLayout === "scroll" ? /* @__PURE__ */ jsxs2(
687
+ "div",
688
+ {
689
+ className: "xynolog-pdf-annotation-canvas__viewport",
690
+ "data-axis": SCROLL_AXIS,
691
+ children: [
692
+ numPages > 0 && pageShape && /* @__PURE__ */ jsx2(
693
+ "div",
694
+ {
695
+ className: "xynolog-pdf-annotation-canvas__strip",
696
+ "data-axis": SCROLL_AXIS,
697
+ "data-scroller": "true",
698
+ "aria-orientation": SCROLL_AXIS,
699
+ ref: stripRef,
700
+ children: Array.from({ length: numPages }, (_, i) => i + 1).map((page) => /* @__PURE__ */ jsx2(
701
+ "button",
702
+ {
703
+ type: "button",
704
+ "data-page": page,
705
+ className: "xynolog-pdf-annotation-canvas__strip-page",
706
+ style: {
707
+ height: `${Math.round(STRIP_PAGE_WIDTH * pageShape.aspect)}px`
708
+ },
709
+ "aria-label": t("pageOf", { page, total: numPages }),
710
+ tabIndex: page === currentPage ? 0 : -1,
711
+ ...page === currentPage ? { "aria-current": "page" } : {},
712
+ ref: page === currentPage ? currentTickRef : void 0,
713
+ onClick: () => scrollToPage(page),
714
+ children: page
715
+ },
716
+ page
717
+ ))
718
+ }
719
+ ),
720
+ /* @__PURE__ */ jsx2(
721
+ "div",
722
+ {
723
+ className: "xynolog-pdf-annotation-canvas__column",
724
+ "data-axis": SCROLL_AXIS,
725
+ ref: columnRef,
726
+ style: { zoom },
727
+ children: Array.from({ length: numPages }, (_, i) => i + 1).map((page) => /* @__PURE__ */ jsx2(
728
+ "div",
729
+ {
730
+ ref: registerSlot,
731
+ "data-page": page,
732
+ className: "xynolog-pdf-annotation-canvas__slot",
733
+ style: slotHeight ? { minHeight: slotHeight } : void 0,
734
+ children: visiblePages.has(page) && /* @__PURE__ */ jsx2(PdfPageView, { doc: pdfDoc, pageNumber: page, ...pageProps })
735
+ },
736
+ page
737
+ ))
738
+ }
739
+ )
740
+ ]
741
+ }
742
+ ) : pdfDoc && !isLoading && // `key` is load-bearing: without it React reuses this instance
743
+ // across page changes, so the previous page's raster and load error
744
+ // survive into the next page — a stale overlay, a rectangle drawn
745
+ // over one page and recorded against another, and an error that
746
+ // never clears. Each page is a different thing; mounting it as one
747
+ // resets all of it.
748
+ /* @__PURE__ */ jsx2("div", { style: { zoom }, children: /* @__PURE__ */ jsx2(
749
+ PdfPageView,
750
+ {
751
+ doc: pdfDoc,
752
+ pageNumber,
753
+ ...pageProps
754
+ },
755
+ pageNumber
756
+ ) }),
757
+ numPages > 0 && /* @__PURE__ */ jsxs2(
758
+ "nav",
759
+ {
760
+ className: "xynolog-pdf-annotation-canvas__controls",
761
+ "aria-label": t("pagination"),
762
+ children: [
763
+ /* @__PURE__ */ jsx2(
764
+ "button",
765
+ {
766
+ disabled: currentPage <= 1,
767
+ onClick: () => pageLayout === "scroll" ? scrollToPage(currentPage - 1) : setPageNumber(pageNumber - 1),
768
+ className: "xynolog-pdf-annotation-canvas__button",
769
+ children: t("previous")
770
+ }
771
+ ),
772
+ /* @__PURE__ */ jsx2(
773
+ "span",
774
+ {
775
+ className: "xynolog-pdf-annotation-canvas__page-info",
776
+ "aria-live": "polite",
777
+ children: t("pageOf", { page: currentPage, total: numPages })
778
+ }
779
+ ),
780
+ /* @__PURE__ */ jsx2(
781
+ "button",
782
+ {
783
+ disabled: currentPage >= numPages,
784
+ onClick: () => pageLayout === "scroll" ? scrollToPage(currentPage + 1) : setPageNumber(pageNumber + 1),
785
+ className: "xynolog-pdf-annotation-canvas__button",
786
+ children: t("next")
787
+ }
788
+ )
789
+ ]
790
+ }
791
+ )
792
+ ]
793
+ }
794
+ );
795
+ }
796
+ export {
797
+ PdfAnnotationCanvas
798
+ };
799
+ //# sourceMappingURL=PdfAnnotationCanvas.client-V7R4NXFO.js.map