@plannotator/ui 0.23.0 → 0.24.0

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.
@@ -1,5 +1,5 @@
1
1
  import React, { useState, useRef, useEffect } from 'react';
2
- import { Annotation, AnnotationType, Block, type CodeAnnotation, type EditorAnnotation } from '../types';
2
+ import { AnnotationType, type Annotation, type Block, type CodeAnnotation, type EditorAnnotation } from '../types';
3
3
  import { isCurrentUser } from '../utils/identity';
4
4
  import { ImageThumbnail } from './ImageThumbnail';
5
5
  import { EditorAnnotationCard } from './EditorAnnotationCard';
@@ -73,12 +73,18 @@ interface PanelProps {
73
73
  /** Committed direct edits to one or more documents. Rendered as pinned cards
74
74
  * above the annotation timeline with expandable unified diffs. */
75
75
  directEdits?: DirectEditsPanelItem[] | null;
76
+ /** Host slot rendered at the foot of each plan-annotation card (e.g. reply/
77
+ * resolve UI). The panel stays presentation-only; clicks inside the slot
78
+ * do not select the card. Default: nothing rendered. */
79
+ renderCardFooter?: (annotation: Annotation) => React.ReactNode;
80
+ /** Hide every mutation affordance (delete/edit buttons on all card kinds).
81
+ * Selection and scrolling still work. Default false — today's behavior. */
82
+ readOnly?: boolean;
76
83
  }
77
84
 
78
85
  export const AnnotationPanel: React.FC<PanelProps> = ({
79
86
  isOpen,
80
87
  annotations,
81
- blocks,
82
88
  onSelect,
83
89
  onDelete,
84
90
  onEdit,
@@ -97,6 +103,8 @@ export const AnnotationPanel: React.FC<PanelProps> = ({
97
103
  otherFileAnnotations,
98
104
  onOtherFileAnnotationsClick,
99
105
  directEdits = null,
106
+ renderCardFooter,
107
+ readOnly = false,
100
108
  }) => {
101
109
  const isMobile = useIsMobile();
102
110
  const [copiedText, setCopiedText] = useState(false);
@@ -195,6 +203,8 @@ export const AnnotationPanel: React.FC<PanelProps> = ({
195
203
  onSelect={() => onSelect(entry.annotation.id)}
196
204
  onDelete={() => onDelete(entry.annotation.id)}
197
205
  onEdit={onEdit ? (updates: Partial<Annotation>) => onEdit(entry.annotation.id, updates) : undefined}
206
+ readOnly={readOnly}
207
+ footer={renderCardFooter?.(entry.annotation)}
198
208
  />
199
209
  ) : (
200
210
  <CodeAnnotationCard
@@ -205,6 +215,7 @@ export const AnnotationPanel: React.FC<PanelProps> = ({
205
215
  onSelect={() => onSelectCodeAnnotation?.(entry.annotation.id)}
206
216
  onDelete={() => onDeleteCodeAnnotation?.(entry.annotation.id)}
207
217
  onEdit={onEditCodeAnnotation ? (updates: Partial<CodeAnnotation>) => onEditCodeAnnotation(entry.annotation.id, updates) : undefined}
218
+ readOnly={readOnly}
208
219
  />
209
220
  )
210
221
  ))}
@@ -221,7 +232,7 @@ export const AnnotationPanel: React.FC<PanelProps> = ({
221
232
  <EditorAnnotationCard
222
233
  key={ann.id}
223
234
  annotation={ann}
224
- onDelete={() => onDeleteEditorAnnotation?.(ann.id)}
235
+ onDelete={readOnly ? undefined : () => onDeleteEditorAnnotation?.(ann.id)}
225
236
  />
226
237
  ))}
227
238
  </>
@@ -418,7 +429,9 @@ const AnnotationCard: React.FC<{
418
429
  onSelect: () => void;
419
430
  onDelete: () => void;
420
431
  onEdit?: (updates: Partial<Annotation>) => void;
421
- }> = ({ annotation, isSelected, isMe, onSelect, onDelete, onEdit }) => {
432
+ readOnly?: boolean;
433
+ footer?: React.ReactNode;
434
+ }> = ({ annotation, isSelected, isMe, onSelect, onDelete, onEdit, readOnly = false, footer }) => {
422
435
  const [isEditing, setIsEditing] = useState(false);
423
436
  const [editText, setEditText] = useState(annotation.text || '');
424
437
  const textareaRef = useRef<HTMLTextAreaElement>(null);
@@ -517,26 +530,28 @@ const AnnotationCard: React.FC<{
517
530
  <span className="text-[10px] text-muted-foreground/50 truncate">
518
531
  {annotation.author ? `${annotation.author}${isMe ? ' (me)' : ''} · ` : ''}{formatTimestamp(annotation.createdA)}
519
532
  </span>
520
- <div className="ml-auto flex items-center gap-0.5 opacity-0 transition-opacity group-hover:opacity-100 [@media(hover:none)]:opacity-100">
521
- {onEdit && annotation.type !== AnnotationType.DELETION && !isEditing && (
533
+ {!readOnly && (
534
+ <div className="ml-auto flex items-center gap-0.5 opacity-0 transition-opacity group-hover:opacity-100 [@media(hover:none)]:opacity-100">
535
+ {onEdit && annotation.type !== AnnotationType.DELETION && !isEditing && (
536
+ <button
537
+ type="button"
538
+ onClick={handleStartEdit}
539
+ className="relative rounded-md p-1.5 text-muted-foreground transition-colors before:absolute before:-inset-1.5 before:content-[''] hover:text-foreground"
540
+ title="Edit annotation"
541
+ >
542
+ <PencilIcon />
543
+ </button>
544
+ )}
522
545
  <button
523
546
  type="button"
524
- onClick={handleStartEdit}
525
- className="relative rounded-md p-1.5 text-muted-foreground transition-colors before:absolute before:-inset-1.5 before:content-[''] hover:text-foreground"
526
- title="Edit annotation"
547
+ onClick={(e: React.MouseEvent<HTMLButtonElement>) => { e.stopPropagation(); onDelete(); }}
548
+ className="relative rounded-md p-1.5 text-muted-foreground transition-colors before:absolute before:-inset-1.5 before:content-[''] hover:text-destructive"
549
+ title="Delete annotation"
527
550
  >
528
- <PencilIcon />
551
+ <TrashCardIcon />
529
552
  </button>
530
- )}
531
- <button
532
- type="button"
533
- onClick={(e: React.MouseEvent<HTMLButtonElement>) => { e.stopPropagation(); onDelete(); }}
534
- className="relative rounded-md p-1.5 text-muted-foreground transition-colors before:absolute before:-inset-1.5 before:content-[''] hover:text-destructive"
535
- title="Delete annotation"
536
- >
537
- <TrashCardIcon />
538
- </button>
539
- </div>
553
+ </div>
554
+ )}
540
555
  </div>
541
556
 
542
557
  {/* Global Comment - show text directly */}
@@ -585,6 +600,19 @@ const AnnotationCard: React.FC<{
585
600
  ))}
586
601
  </div>
587
602
  )}
603
+
604
+ {/* Host footer slot (reply/resolve UI etc.) — interactions inside it
605
+ must not toggle card selection. */}
606
+ {footer != null && footer !== false && (
607
+ <div
608
+ data-annotation-card-footer="true"
609
+ className="mt-2"
610
+ onClick={(e: React.MouseEvent) => e.stopPropagation()}
611
+ onKeyDown={(e: React.KeyboardEvent) => e.stopPropagation()}
612
+ >
613
+ {footer}
614
+ </div>
615
+ )}
588
616
  </div>
589
617
  );
590
618
  };
@@ -596,7 +624,8 @@ const CodeAnnotationCard: React.FC<{
596
624
  onSelect: () => void;
597
625
  onDelete: () => void;
598
626
  onEdit?: (updates: Partial<CodeAnnotation>) => void;
599
- }> = ({ annotation, isSelected, isMe, onSelect, onDelete, onEdit }) => {
627
+ readOnly?: boolean;
628
+ }> = ({ annotation, isSelected, isMe, onSelect, onDelete, onEdit, readOnly = false }) => {
600
629
  const [isEditing, setIsEditing] = useState(false);
601
630
  const [editText, setEditText] = useState(annotation.text || '');
602
631
  const textareaRef = useRef<HTMLTextAreaElement>(null);
@@ -650,26 +679,28 @@ const CodeAnnotationCard: React.FC<{
650
679
  <span className="text-[10px] text-muted-foreground/50 truncate">
651
680
  {annotation.author ? `${annotation.author}${isMe ? ' (me)' : ''} · ` : ''}{formatTimestamp(annotation.createdAt)}
652
681
  </span>
653
- <div className="ml-auto flex items-center gap-0.5 opacity-0 transition-opacity group-hover:opacity-100 [@media(hover:none)]:opacity-100">
654
- {onEdit && !isEditing && (
682
+ {!readOnly && (
683
+ <div className="ml-auto flex items-center gap-0.5 opacity-0 transition-opacity group-hover:opacity-100 [@media(hover:none)]:opacity-100">
684
+ {onEdit && !isEditing && (
685
+ <button
686
+ type="button"
687
+ onClick={(e) => { e.stopPropagation(); setIsEditing(true); }}
688
+ className="relative rounded-md p-1.5 text-muted-foreground transition-colors before:absolute before:-inset-1.5 before:content-[''] hover:text-foreground"
689
+ title="Edit annotation"
690
+ >
691
+ <PencilIcon />
692
+ </button>
693
+ )}
655
694
  <button
656
695
  type="button"
657
- onClick={(e) => { e.stopPropagation(); setIsEditing(true); }}
658
- className="relative rounded-md p-1.5 text-muted-foreground transition-colors before:absolute before:-inset-1.5 before:content-[''] hover:text-foreground"
659
- title="Edit annotation"
696
+ onClick={(e) => { e.stopPropagation(); onDelete(); }}
697
+ className="relative rounded-md p-1.5 text-muted-foreground transition-colors before:absolute before:-inset-1.5 before:content-[''] hover:text-destructive"
698
+ title="Delete annotation"
660
699
  >
661
- <PencilIcon />
700
+ <TrashCardIcon />
662
701
  </button>
663
- )}
664
- <button
665
- type="button"
666
- onClick={(e) => { e.stopPropagation(); onDelete(); }}
667
- className="relative rounded-md p-1.5 text-muted-foreground transition-colors before:absolute before:-inset-1.5 before:content-[''] hover:text-destructive"
668
- title="Delete annotation"
669
- >
670
- <TrashCardIcon />
671
- </button>
672
- </div>
702
+ </div>
703
+ )}
673
704
  </div>
674
705
 
675
706
  {/* File / line meta */}
@@ -1,4 +1,4 @@
1
- import React, { useState, useEffect, useRef, useMemo, useCallback } from "react";
1
+ import React, { useState, useEffect, useRef, useMemo } from "react";
2
2
  import { AnnotationType } from "../types";
3
3
  import { createPortal } from "react-dom";
4
4
  import { useDismissOnOutsideAndEscape } from "../hooks/useDismissOnOutsideAndEscape";
@@ -1,5 +1,5 @@
1
1
  import React from "react";
2
- import { Block } from "../types";
2
+ import type { Block } from "../types";
3
3
  import { InlineMarkdown } from "./InlineMarkdown";
4
4
  import { ListItemBody } from "./ListItemBody";
5
5
  import { CodeBlock } from "./blocks/CodeBlock";
@@ -14,7 +14,8 @@ type EditorAnnotationVariant = 'plan' | 'code-review';
14
14
 
15
15
  interface EditorAnnotationCardProps {
16
16
  annotation: EditorAnnotation;
17
- onDelete: () => void;
17
+ /** Omit to render the card read-only (no delete affordance). */
18
+ onDelete?: () => void;
18
19
  variant?: EditorAnnotationVariant;
19
20
  }
20
21
 
@@ -38,15 +39,17 @@ export const EditorAnnotationCard: React.FC<EditorAnnotationCardProps> = ({ anno
38
39
  <span className="text-[10px] font-mono text-muted-foreground/50 truncate" title={annotation.filePath}>
39
40
  {annotation.filePath}:{lineRange}
40
41
  </span>
41
- <button
42
- onClick={(e) => { e.stopPropagation(); onDelete(); }}
43
- className="ml-auto relative rounded-md p-1.5 text-muted-foreground transition-colors before:absolute before:-inset-1.5 before:content-[''] opacity-0 group-hover:opacity-100 [@media(hover:none)]:opacity-100 hover:text-destructive"
44
- title="Delete annotation"
45
- >
46
- <svg className="w-3 h-3" fill="none" viewBox="0 0 24 24" stroke="currentColor" strokeWidth={2}>
47
- <path strokeLinecap="round" strokeLinejoin="round" d="M6 18L18 6M6 6l12 12" />
48
- </svg>
49
- </button>
42
+ {onDelete && (
43
+ <button
44
+ onClick={(e) => { e.stopPropagation(); onDelete(); }}
45
+ className="ml-auto relative rounded-md p-1.5 text-muted-foreground transition-colors before:absolute before:-inset-1.5 before:content-[''] opacity-0 group-hover:opacity-100 [@media(hover:none)]:opacity-100 hover:text-destructive"
46
+ title="Delete annotation"
47
+ >
48
+ <svg className="w-3 h-3" fill="none" viewBox="0 0 24 24" stroke="currentColor" strokeWidth={2}>
49
+ <path strokeLinecap="round" strokeLinejoin="round" d="M6 18L18 6M6 6l12 12" />
50
+ </svg>
51
+ </button>
52
+ )}
50
53
  </div>
51
54
 
52
55
  {/* Selected text */}
@@ -2,7 +2,7 @@ import React, { useState, useCallback, useEffect, useRef } from 'react';
2
2
  import { Canvas } from './Canvas';
3
3
  import { Toolbar } from './Toolbar';
4
4
  import { renderStroke } from './utils';
5
- import type { Point, Stroke, Tool, AnnotatorState } from './types';
5
+ import type { Point, AnnotatorState } from './types';
6
6
  import { DEFAULT_STATE } from './types';
7
7
 
8
8
  interface ImageAnnotatorProps {
@@ -1,8 +1,8 @@
1
1
  import React, { useRef, useState, useEffect, useMemo, forwardRef, useImperativeHandle, useCallback } from 'react';
2
2
  import { createPortal } from 'react-dom';
3
3
  import hljs from 'highlight.js';
4
- import { Block, Annotation, AnnotationType, EditorMode, type InputMethod, type ImageAttachment, type ActionsLabelMode } from '../types';
5
- import { Frontmatter, computeListIndices, groupBlocks } from '../utils/parser';
4
+ import { AnnotationType, type Block, type Annotation, type EditorMode, type InputMethod, type ImageAttachment, type ActionsLabelMode } from '../types';
5
+ import { computeListIndices, groupBlocks, type Frontmatter } from '../utils/parser';
6
6
  import { buildHeadingSlugMap } from '../utils/slugify';
7
7
  import { BlockRenderer } from './BlockRenderer';
8
8
  import { CodeBlock } from './blocks/CodeBlock';
@@ -11,7 +11,6 @@ import { TableToolbar } from './blocks/TableToolbar';
11
11
  import { TablePopout } from './blocks/TablePopout';
12
12
  import { CodePathValidationContext } from './CodePathValidationContext';
13
13
  import { useValidatedCodePaths } from '../hooks/useValidatedCodePaths';
14
- import { ListMarker } from './ListMarker';
15
14
  import { AnnotationToolbar } from './AnnotationToolbar';
16
15
  import { FloatingQuickLabelPicker } from './FloatingQuickLabelPicker';
17
16
 
@@ -33,13 +32,12 @@ class ToolbarErrorBoundary extends React.Component<
33
32
  }
34
33
  }
35
34
 
36
- import { CommentPopover, type CommentAskAIContext, type CommentAskAIHandler } from './CommentPopover';
35
+ import { CommentPopover, type CommentAskAIHandler } from './CommentPopover';
37
36
  import { TaterSpriteSitting } from './TaterSpriteSitting';
38
37
  import { AttachmentsButton } from './AttachmentsButton';
39
38
  import { MessagesIcon } from './icons/MessagesIcon';
40
39
  import { GraphvizBlock } from './GraphvizBlock';
41
40
  import { MermaidBlock } from './MermaidBlock';
42
- import { getImageSrc } from './ImageThumbnail';
43
41
  import { isGraphvizLanguage, isMermaidLanguage } from './diagramLanguages';
44
42
  import { getIdentity } from '../utils/identity';
45
43
  import { type QuickLabel } from '../utils/quickLabels';
@@ -112,6 +110,15 @@ interface ViewerProps {
112
110
  onToggleCheckbox?: (blockId: string, checked: boolean) => void;
113
111
  checkboxOverrides?: Map<string, boolean>;
114
112
  onAskAI?: CommentAskAIHandler;
113
+ /** Whether comment popovers offer image attachments. Hosts without an
114
+ * uploadTransport pass false so the attach affordance never dead-ends.
115
+ * Default true — today's behavior. */
116
+ allowImages?: boolean;
117
+ /** View-only mode: suppresses every annotation-creation entry point
118
+ * (selection toolbar, comment popovers, quick labels, pinpoint, global
119
+ * comment, attachments, checkbox toggles). Existing annotations still
120
+ * render and remain selectable. Default false — today's behavior. */
121
+ readOnly?: boolean;
115
122
  }
116
123
 
117
124
  export interface ViewerHandle {
@@ -191,6 +198,8 @@ export const Viewer = forwardRef<ViewerHandle, ViewerProps>(({
191
198
  onToggleCheckbox,
192
199
  checkboxOverrides,
193
200
  onAskAI,
201
+ allowImages = true,
202
+ readOnly = false,
194
203
  }, ref) => {
195
204
  const [copied, setCopied] = useState(false);
196
205
  const [lightbox, setLightbox] = useState<{ src: string; alt: string } | null>(null);
@@ -283,6 +292,7 @@ export const Viewer = forwardRef<ViewerHandle, ViewerProps>(({
283
292
  onSelectAnnotation,
284
293
  selectedAnnotationId,
285
294
  mode,
295
+ enabled: !readOnly,
286
296
  });
287
297
 
288
298
  // Refs for code block annotation path
@@ -319,7 +329,7 @@ export const Viewer = forwardRef<ViewerHandle, ViewerProps>(({
319
329
  containerRef,
320
330
  highlighterRef,
321
331
  inputMethod,
322
- enabled: !toolbarState && !hookCommentPopover && !viewerCommentPopover && !hookQuickLabelPicker && !codeBlockQuickLabelPicker && !(isPlanDiffActive ?? false),
332
+ enabled: !readOnly && !toolbarState && !hookCommentPopover && !viewerCommentPopover && !hookQuickLabelPicker && !codeBlockQuickLabelPicker && !(isPlanDiffActive ?? false),
323
333
  onCodeBlockClick: handlePinpointCodeBlockClick,
324
334
  });
325
335
 
@@ -612,7 +622,7 @@ export const Viewer = forwardRef<ViewerHandle, ViewerProps>(({
612
622
  )}
613
623
 
614
624
  {/* Attachments button */}
615
- {onAddGlobalAttachment && onRemoveGlobalAttachment && (
625
+ {!readOnly && onAddGlobalAttachment && onRemoveGlobalAttachment && (
616
626
  <AttachmentsButton
617
627
  images={globalAttachments}
618
628
  onAdd={onAddGlobalAttachment}
@@ -623,6 +633,7 @@ export const Viewer = forwardRef<ViewerHandle, ViewerProps>(({
623
633
  )}
624
634
 
625
635
  {/* <span className="md:hidden">Comment</span><span className="hidden md:inline">Global comment</span> button */}
636
+ {!readOnly && (
626
637
  <button
627
638
  ref={globalCommentButtonRef}
628
639
  onClick={() => {
@@ -641,6 +652,7 @@ export const Viewer = forwardRef<ViewerHandle, ViewerProps>(({
641
652
  {actionsLabelMode === 'full' && <span>Global comment</span>}
642
653
  {actionsLabelMode === 'short' && <span>Comment</span>}
643
654
  </button>
655
+ )}
644
656
 
645
657
  {/* Copy plan/file button */}
646
658
  <button
@@ -683,7 +695,7 @@ export const Viewer = forwardRef<ViewerHandle, ViewerProps>(({
683
695
  orderedIndex={indices[i]}
684
696
  onOpenLinkedDoc={onOpenLinkedDoc}
685
697
  onOpenCodeFile={onOpenCodeFile}
686
- onToggleCheckbox={onToggleCheckbox}
698
+ onToggleCheckbox={readOnly ? undefined : onToggleCheckbox}
687
699
  checkboxOverrides={checkboxOverrides}
688
700
  githubRepo={repoInfo?.display}
689
701
  headingAnchorId={headingSlugMap.get(block.id)}
@@ -758,7 +770,7 @@ export const Viewer = forwardRef<ViewerHandle, ViewerProps>(({
758
770
  isHovered={inputMethod !== 'pinpoint' && hoveredCodeBlock?.block.id === group.block.id}
759
771
  />
760
772
  ) : (
761
- <BlockRenderer imageBaseDir={imageBaseDir} onImageClick={(src, alt) => setLightbox({ src, alt })} key={group.block.id} block={group.block} onOpenLinkedDoc={onOpenLinkedDoc} onOpenCodeFile={onOpenCodeFile} onNavigateAnchor={scrollToAnchor} onToggleCheckbox={onToggleCheckbox} checkboxOverrides={checkboxOverrides} githubRepo={repoInfo?.display} headingAnchorId={headingSlugMap.get(group.block.id)} />
773
+ <BlockRenderer imageBaseDir={imageBaseDir} onImageClick={(src, alt) => setLightbox({ src, alt })} key={group.block.id} block={group.block} onOpenLinkedDoc={onOpenLinkedDoc} onOpenCodeFile={onOpenCodeFile} onNavigateAnchor={scrollToAnchor} onToggleCheckbox={readOnly ? undefined : onToggleCheckbox} checkboxOverrides={checkboxOverrides} githubRepo={repoInfo?.display} headingAnchorId={headingSlugMap.get(group.block.id)} />
762
774
  )
763
775
  )}
764
776
 
@@ -875,6 +887,7 @@ export const Viewer = forwardRef<ViewerHandle, ViewerProps>(({
875
887
  initialText={hookCommentPopover.initialText}
876
888
  onSubmit={hookCommentSubmit}
877
889
  onClose={hookCommentClose}
890
+ allowImages={allowImages}
878
891
  onAskAI={onAskAI}
879
892
  askAIContext={{
880
893
  kind: 'selection',
@@ -892,6 +905,7 @@ export const Viewer = forwardRef<ViewerHandle, ViewerProps>(({
892
905
  initialText={viewerCommentPopover.initialText}
893
906
  onSubmit={handleViewerCommentSubmit}
894
907
  onClose={handleViewerCommentClose}
908
+ allowImages={allowImages}
895
909
  onAskAI={onAskAI}
896
910
  askAIContext={{
897
911
  kind: viewerCommentPopover.isGlobal ? 'general' : 'selection',
@@ -1,7 +1,7 @@
1
1
  import React, { useState, useRef, useEffect, useCallback } from 'react';
2
2
  import hljs from 'highlight.js';
3
3
  import 'highlight.js/styles/github-dark.css';
4
- import { Block } from '../../types';
4
+ import type { Block } from '../../types';
5
5
 
6
6
  interface CodeBlockProps {
7
7
  block: Block;
@@ -10,7 +10,7 @@ interface CodeBlockProps {
10
10
  isHovered: boolean;
11
11
  }
12
12
 
13
- export const CodeBlock: React.FC<CodeBlockProps> = ({ block, onHover, onLeave, isHovered }) => {
13
+ export const CodeBlock: React.FC<CodeBlockProps> = ({ block, onHover, onLeave }) => {
14
14
  const [copied, setCopied] = useState(false);
15
15
  const containerRef = useRef<HTMLDivElement>(null);
16
16
  const codeRef = useRef<HTMLElement>(null);
@@ -1,6 +1,6 @@
1
1
  import React, { useRef, useEffect } from "react";
2
2
  import { isCodeFilePath } from "@plannotator/core/code-file";
3
- import { Block } from "../../types";
3
+ import type { Block } from "../../types";
4
4
  import { sanitizeBlockHtml } from "../../utils/sanitizeHtml";
5
5
  import { getImageSrc } from "../ImageThumbnail";
6
6
 
@@ -1,5 +1,5 @@
1
1
  import React, { useRef } from 'react';
2
- import { Block } from '../../types';
2
+ import type { Block } from '../../types';
3
3
  import { InlineMarkdown } from '../InlineMarkdown';
4
4
 
5
5
  interface TableBlockProps {
@@ -9,7 +9,7 @@ import {
9
9
  type ColumnDef,
10
10
  type SortingState,
11
11
  } from '@tanstack/react-table';
12
- import { Block } from '../../types';
12
+ import type { Block } from '../../types';
13
13
  import { InlineMarkdown } from '../InlineMarkdown';
14
14
  import { PopoutDialog } from '../PopoutDialog';
15
15
  import { parseTableContent, buildCsvFromRows, buildMarkdownTable } from './TableBlock';
@@ -174,6 +174,12 @@ const escapeAttrValue = (value: string): string => {
174
174
  return value.replace(/\\/g, '\\\\').replace(/"/g, '\\"');
175
175
  };
176
176
 
177
+ /** Whitespace-insensitive comparison for restore verification: a highlight
178
+ * spanning element boundaries legitimately differs from `originalText` in
179
+ * whitespace, so only content differences count as a mismatch. */
180
+ const normalizeForRestoreCompare = (value: string): string =>
181
+ value.replace(/\s+/g, ' ').trim();
182
+
177
183
  const applyMathAnnotationClass = (
178
184
  element: HTMLElement,
179
185
  id: string,
@@ -227,6 +233,15 @@ export interface UseAnnotationHighlighterOptions {
227
233
  selectedAnnotationId: string | null;
228
234
  mode: EditorMode;
229
235
  enabled?: boolean;
236
+ /** Opt-in: after a meta-based restore (`fromStore`), verify the painted text
237
+ * matches the annotation's `originalText` (whitespace-normalized). On
238
+ * mismatch the wrong highlight is removed and the text-search fallback
239
+ * runs instead; if that also fails, `onRestoreMismatch` fires and nothing
240
+ * is painted. Default false — today's behavior (positions are trusted). */
241
+ verifyRestoredContent?: boolean;
242
+ /** Fires when a restore was rejected (content mismatch) and the text-search
243
+ * fallback could not re-anchor the annotation either. */
244
+ onRestoreMismatch?: (annotation: Annotation, restoredText: string) => void;
230
245
  }
231
246
 
232
247
  export interface UseAnnotationHighlighterReturn {
@@ -258,6 +273,8 @@ export function useAnnotationHighlighter({
258
273
  selectedAnnotationId,
259
274
  mode,
260
275
  enabled = true,
276
+ verifyRestoredContent = false,
277
+ onRestoreMismatch,
261
278
  }: UseAnnotationHighlighterOptions): UseAnnotationHighlighterReturn {
262
279
  const highlighterRef = useRef<Highlighter | null>(null);
263
280
  const modeRef = useRef<EditorMode>(mode);
@@ -278,6 +295,8 @@ export function useAnnotationHighlighter({
278
295
  useEffect(() => { modeRef.current = mode; }, [mode]);
279
296
  useEffect(() => { onAddAnnotationRef.current = onAddAnnotation; }, [onAddAnnotation]);
280
297
  useEffect(() => { onSelectAnnotationRef.current = onSelectAnnotation; }, [onSelectAnnotation]);
298
+ const onRestoreMismatchRef = useRef(onRestoreMismatch);
299
+ useEffect(() => { onRestoreMismatchRef.current = onRestoreMismatch; }, [onRestoreMismatch]);
281
300
 
282
301
  const clearPendingSelection = useCallback(() => {
283
302
  pendingSourceRef.current = null;
@@ -625,23 +644,42 @@ export function useAnnotationHighlighter({
625
644
  }
626
645
  }
627
646
 
647
+ // When a meta-based restore paints text that no longer matches the
648
+ // anchor's originalText (document drift), remember what it painted so
649
+ // the mismatch can be reported if the text fallback also fails.
650
+ let rejectedRestoreText: string | null = null;
651
+
628
652
  if (ann.startMeta && ann.endMeta) {
629
653
  try {
630
654
  highlighter.fromStore(ann.startMeta, ann.endMeta, ann.originalText, ann.id);
631
655
  const restoredDoms = highlighter.getDoms(ann.id);
632
656
  if (restoredDoms && restoredDoms.length > 0) {
633
- if (ann.type === AnnotationType.DELETION) {
634
- highlighter.addClass('deletion', ann.id);
635
- } else if (ann.type === AnnotationType.COMMENT) {
636
- highlighter.addClass('comment', ann.id);
657
+ const restoredText = restoredDoms.map(dom => dom.textContent ?? '').join('');
658
+ if (
659
+ verifyRestoredContent &&
660
+ normalizeForRestoreCompare(restoredText) !== normalizeForRestoreCompare(ann.originalText)
661
+ ) {
662
+ // Positions resolved, but onto the WRONG text — remove the bad
663
+ // highlight and fall through to the text-search fallback.
664
+ try { highlighter.remove(ann.id); } catch {}
665
+ rejectedRestoreText = restoredText;
666
+ } else {
667
+ if (ann.type === AnnotationType.DELETION) {
668
+ highlighter.addClass('deletion', ann.id);
669
+ } else if (ann.type === AnnotationType.COMMENT) {
670
+ highlighter.addClass('comment', ann.id);
671
+ }
672
+ return;
637
673
  }
638
- return;
639
674
  }
640
675
  } catch {}
641
676
  }
642
677
 
643
678
  const range = findTextInDOM(ann.originalText);
644
679
  if (!range) {
680
+ if (rejectedRestoreText !== null) {
681
+ onRestoreMismatchRef.current?.(ann, rejectedRestoreText);
682
+ }
645
683
  console.warn(`Could not find text for annotation ${ann.id}: "${ann.originalText.slice(0, 50)}..."`);
646
684
  return;
647
685
  }
@@ -720,7 +758,7 @@ export function useAnnotationHighlighter({
720
758
  console.warn(`Failed to apply highlight for annotation ${ann.id}:`, e);
721
759
  }
722
760
  });
723
- }, [findMathElementsForAnnotation, findTextInDOM]);
761
+ }, [findMathElementsForAnnotation, findTextInDOM, verifyRestoredContent]);
724
762
 
725
763
  const removeHighlight = useCallback((id: string) => {
726
764
  highlighterRef.current?.remove(id);
@@ -20,11 +20,6 @@ interface DraftData {
20
20
  ts: number;
21
21
  }
22
22
 
23
- interface MissingDraftData {
24
- found?: false;
25
- draftGeneration?: number;
26
- }
27
-
28
23
  function readDraftGeneration(value: unknown): number | null {
29
24
  return typeof value === 'number' && Number.isInteger(value) && value >= 0 ? value : null;
30
25
  }
@@ -1,7 +1,7 @@
1
- import { useEffect, useState, useCallback, type RefObject } from 'react';
1
+ import { useEffect, useState, type RefObject } from 'react';
2
2
  import type Highlighter from '@plannotator/web-highlighter';
3
3
  import type { InputMethod } from '../types';
4
- import { resolvePinpointTarget, type PinpointTarget } from '../utils/blockTargeting';
4
+ import { resolvePinpointTarget } from '../utils/blockTargeting';
5
5
 
6
6
  export interface UsePinpointOptions {
7
7
  containerRef: RefObject<HTMLElement | null>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@plannotator/ui",
3
- "version": "0.23.0",
3
+ "version": "0.24.0",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  "./components/*": "./components/*.tsx",
package/utils/parser.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { Block, type Annotation, type CodeAnnotation, type EditorAnnotation, type ImageAttachment } from '../types';
1
+ import type { Block, Annotation, CodeAnnotation, EditorAnnotation, ImageAttachment } from '../types';
2
2
  import { planDenyFeedback } from '@plannotator/core/feedback-templates';
3
3
 
4
4
  /**
package/utils/sharing.ts CHANGED
@@ -8,7 +8,7 @@
8
8
  * Inspired by textarea.my's approach.
9
9
  */
10
10
 
11
- import { Annotation, AnnotationType, type ImageAttachment } from '../types';
11
+ import { AnnotationType, type Annotation, type ImageAttachment } from '../types';
12
12
  import { compress, decompress } from '@plannotator/core/compress';
13
13
  import { encrypt, decrypt } from '@plannotator/core/crypto';
14
14