@embedpdf/plugin-annotation 1.0.6 → 1.0.7

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.
package/dist/index.d.ts CHANGED
@@ -1,82 +1,244 @@
1
1
  import { BasePluginConfig, EventHook, Action, BasePlugin, PluginRegistry, PluginManifest, PluginPackage } from '@embedpdf/core';
2
- import { Task, PdfAnnotationObject, PdfErrorReason, PdfAlphaColor, PdfAnnotationSubtype, PdfEngine } from '@embedpdf/models';
2
+ import { PdfAnnotationSubtype, WebAlphaColor, Task, PdfAnnotationObject, PdfErrorReason, PdfEngine } from '@embedpdf/models';
3
3
 
4
- interface AnnotationState {
5
- annotations: Record<number, PdfAnnotationObject[]>;
6
- selectedAnnotation: SelectedAnnotation | null;
7
- annotationMode: PdfAnnotationSubtype | null;
4
+ type CommitState = 'new' | 'dirty' | 'deleted' | 'synced' | 'ignored';
5
+ interface TrackedAnnotation {
6
+ /** A stable, client-side unique identifier for history and state management. */
7
+ localId: number;
8
+ /**
9
+ * If the engine has already created the annotation in the PDF
10
+ * this is the definitive id coming from the engine.
11
+ * It is **never** cleared once set.
12
+ */
13
+ pdfId?: number;
14
+ /** local commit bookkeeping */
15
+ commitState: CommitState;
16
+ /** the actual annotation object */
17
+ object: PdfAnnotationObject;
8
18
  }
9
- interface SelectedAnnotation {
10
- pageIndex: number;
11
- annotationId: number;
12
- annotation: PdfAnnotationObject;
19
+ interface BaseAnnotationDefaults extends WebAlphaColor {
20
+ interaction: {
21
+ mode: string;
22
+ exclusive: boolean;
23
+ cursor?: string;
24
+ };
25
+ textSelection?: boolean;
26
+ }
27
+ interface HighlightDefaults extends BaseAnnotationDefaults {
28
+ name: 'Highlight';
29
+ }
30
+ interface UnderlineDefaults extends BaseAnnotationDefaults {
31
+ name: 'Underline';
32
+ }
33
+ interface StrikeoutDefaults extends BaseAnnotationDefaults {
34
+ name: 'Strikeout';
35
+ }
36
+ interface SquigglyDefaults extends BaseAnnotationDefaults {
37
+ name: 'Squiggly';
38
+ }
39
+ type AnnotationDefaults = HighlightDefaults | UnderlineDefaults | StrikeoutDefaults | SquigglyDefaults;
40
+ type ToolDefaultsBySubtype = {
41
+ [PdfAnnotationSubtype.HIGHLIGHT]: HighlightDefaults;
42
+ [PdfAnnotationSubtype.UNDERLINE]: UnderlineDefaults;
43
+ [PdfAnnotationSubtype.STRIKEOUT]: StrikeoutDefaults;
44
+ [PdfAnnotationSubtype.SQUIGGLY]: SquigglyDefaults;
45
+ };
46
+ type StylableSubtype = keyof ToolDefaultsBySubtype;
47
+ type ToolDefaults<S extends PdfAnnotationSubtype> = ToolDefaultsBySubtype[Extract<S, keyof ToolDefaultsBySubtype>];
48
+ interface ActiveTool {
49
+ mode: StylableSubtype | null;
50
+ defaults: AnnotationDefaults | null;
51
+ }
52
+ interface AnnotationState {
53
+ pages: Record<number, string[]>;
54
+ byUid: Record<string, TrackedAnnotation>;
55
+ selectedUid: string | null;
56
+ annotationMode: StylableSubtype | null;
57
+ toolDefaults: ToolDefaultsBySubtype;
58
+ colorPresets: string[];
59
+ hasPendingChanges: boolean;
13
60
  }
14
61
  interface AnnotationPluginConfig extends BasePluginConfig {
62
+ toolDefaults?: Partial<ToolDefaultsBySubtype>;
63
+ colorPresets?: string[];
64
+ /**
65
+ * When `false` mutations are kept in memory and must be
66
+ * flushed with `commitPendingChanges()`.
67
+ */
68
+ autoCommit?: boolean;
15
69
  }
16
70
  interface AnnotationCapability {
17
71
  getPageAnnotations: (options: GetPageAnnotationsOptions) => Task<PdfAnnotationObject[], PdfErrorReason>;
72
+ getSelectedAnnotation: () => TrackedAnnotation | null;
18
73
  selectAnnotation: (pageIndex: number, annotationId: number) => void;
19
74
  deselectAnnotation: () => void;
20
- updateAnnotationColor: (color: PdfAlphaColor) => Promise<boolean>;
21
- setAnnotationMode: (mode: PdfAnnotationSubtype | null) => void;
75
+ getAnnotationMode: () => StylableSubtype | null;
76
+ setAnnotationMode: (mode: StylableSubtype | null) => void;
77
+ /** strongly typed – only sub-types we have defaults for */
78
+ getToolDefaults: <S extends StylableSubtype>(subtype: S) => ToolDefaultsBySubtype[S];
79
+ /** Partially patch a single tool’s defaults */
80
+ setToolDefaults: <S extends StylableSubtype>(subtype: S, patch: Partial<ToolDefaultsBySubtype[S]>) => void;
81
+ /** current palette – UI just reads this */
82
+ getColorPresets: () => string[];
83
+ /** append a swatch (deduped by RGBA) */
84
+ addColorPreset: (color: string) => void;
85
+ createAnnotation: (pageIndex: number, annotation: PdfAnnotationObject) => void;
86
+ updateAnnotation: (pageIndex: number, annotationId: number, patch: Partial<PdfAnnotationObject>) => void;
87
+ deleteAnnotation: (pageIndex: number, annotationId: number) => void;
88
+ /** undo / redo */
22
89
  onStateChange: EventHook<AnnotationState>;
90
+ onModeChange: EventHook<StylableSubtype | null>;
91
+ onActiveToolChange: EventHook<ActiveTool>;
92
+ commit: () => void;
93
+ }
94
+ interface SelectedAnnotation {
95
+ pageIndex: number;
96
+ localId: number;
97
+ annotation: PdfAnnotationObject;
23
98
  }
24
99
  interface GetPageAnnotationsOptions {
25
100
  pageIndex: number;
26
101
  }
27
- interface UpdateAnnotationColorOptions {
102
+ interface UpdateAnnotationColorOptions extends WebAlphaColor {
28
103
  pageIndex: number;
29
104
  annotationId: number;
30
- color: PdfAlphaColor;
31
105
  }
32
106
 
33
- declare const SET_ANNOTATIONS = "SET_ANNOTATIONS";
34
- declare const SELECT_ANNOTATION = "SELECT_ANNOTATION";
35
- declare const DESELECT_ANNOTATION = "DESELECT_ANNOTATION";
36
- declare const SET_ANNOTATION_MODE = "SET_ANNOTATION_MODE";
37
- declare const UPDATE_ANNOTATION_COLOR = "UPDATE_ANNOTATION_COLOR";
107
+ declare const SET_ANNOTATIONS = "ANNOTATION/SET_ANNOTATIONS";
108
+ declare const REINDEX_PAGE_ANNOTATIONS = "ANNOTATION/REINDEX_PAGE";
109
+ declare const SELECT_ANNOTATION = "ANNOTATION/SELECT_ANNOTATION";
110
+ declare const DESELECT_ANNOTATION = "ANNOTATION/DESELECT_ANNOTATION";
111
+ declare const SET_ANNOTATION_MODE = "ANNOTATION/SET_ANNOTATION_MODE";
112
+ declare const UPDATE_TOOL_DEFAULTS = "ANNOTATION/UPDATE_TOOL_DEFAULTS";
113
+ declare const ADD_COLOR_PRESET = "ANNOTATION/ADD_COLOR_PRESET";
114
+ declare const CREATE_ANNOTATION = "ANNOTATION/CREATE_ANNOTATION";
115
+ declare const PATCH_ANNOTATION = "ANNOTATION/PATCH_ANNOTATION";
116
+ declare const DELETE_ANNOTATION = "ANNOTATION/DELETE_ANNOTATION";
117
+ declare const COMMIT_PENDING_CHANGES = "ANNOTATION/COMMIT";
118
+ declare const STORE_PDF_ID = "ANNOTATION/STORE_PDF_ID";
119
+ declare const PURGE_ANNOTATION = "ANNOTATION/PURGE_ANNOTATION";
38
120
  interface SetAnnotationsAction extends Action {
39
121
  type: typeof SET_ANNOTATIONS;
40
122
  payload: Record<number, PdfAnnotationObject[]>;
41
123
  }
124
+ interface ReindexPageAnnotationsAction extends Action {
125
+ type: typeof REINDEX_PAGE_ANNOTATIONS;
126
+ payload: {
127
+ pageIndex: number;
128
+ };
129
+ }
42
130
  interface SelectAnnotationAction extends Action {
43
131
  type: typeof SELECT_ANNOTATION;
44
- payload: SelectedAnnotation;
132
+ payload: {
133
+ pageIndex: number;
134
+ localId: number;
135
+ };
45
136
  }
46
137
  interface DeselectAnnotationAction extends Action {
47
138
  type: typeof DESELECT_ANNOTATION;
48
139
  }
49
140
  interface SetAnnotationModeAction extends Action {
50
141
  type: typeof SET_ANNOTATION_MODE;
51
- payload: PdfAnnotationSubtype | null;
142
+ payload: StylableSubtype | null;
143
+ }
144
+ interface UpdateToolDefaultsAction extends Action {
145
+ type: typeof UPDATE_TOOL_DEFAULTS;
146
+ payload: {
147
+ subtype: StylableSubtype;
148
+ patch: Partial<ToolDefaultsBySubtype[StylableSubtype]>;
149
+ };
150
+ }
151
+ interface AddColorPresetAction extends Action {
152
+ type: typeof ADD_COLOR_PRESET;
153
+ payload: string;
154
+ }
155
+ interface CreateAnnotationAction extends Action {
156
+ type: typeof CREATE_ANNOTATION;
157
+ payload: {
158
+ pageIndex: number;
159
+ localId: number;
160
+ annotation: PdfAnnotationObject;
161
+ };
162
+ }
163
+ interface PatchAnnotationAction extends Action {
164
+ type: typeof PATCH_ANNOTATION;
165
+ payload: {
166
+ pageIndex: number;
167
+ localId: number;
168
+ patch: Partial<PdfAnnotationObject>;
169
+ };
52
170
  }
53
- interface UpdateAnnotationColorAction extends Action {
54
- type: typeof UPDATE_ANNOTATION_COLOR;
171
+ interface DeleteAnnotationAction extends Action {
172
+ type: typeof DELETE_ANNOTATION;
55
173
  payload: {
56
174
  pageIndex: number;
57
- annotationId: number;
58
- color: PdfAlphaColor;
175
+ localId: number;
59
176
  };
60
177
  }
61
- type AnnotationAction = SetAnnotationsAction | SelectAnnotationAction | DeselectAnnotationAction | SetAnnotationModeAction | UpdateAnnotationColorAction;
178
+ interface CommitAction extends Action {
179
+ type: typeof COMMIT_PENDING_CHANGES;
180
+ }
181
+ interface StorePdfIdAction extends Action {
182
+ type: typeof STORE_PDF_ID;
183
+ payload: {
184
+ uid: string;
185
+ pdfId: number;
186
+ };
187
+ }
188
+ interface PurgeAnnotationAction extends Action {
189
+ type: typeof PURGE_ANNOTATION;
190
+ payload: {
191
+ uid: string;
192
+ };
193
+ }
194
+ type AnnotationAction = SetAnnotationsAction | ReindexPageAnnotationsAction | SelectAnnotationAction | DeselectAnnotationAction | SetAnnotationModeAction | UpdateToolDefaultsAction | AddColorPresetAction | CreateAnnotationAction | PatchAnnotationAction | DeleteAnnotationAction | CommitAction | StorePdfIdAction | PurgeAnnotationAction;
62
195
 
63
196
  declare class AnnotationPlugin extends BasePlugin<AnnotationPluginConfig, AnnotationCapability, AnnotationState, AnnotationAction> {
64
197
  static readonly id: "annotation";
198
+ private readonly ANNOTATION_HISTORY_TOPIC;
199
+ private readonly config;
65
200
  private engine;
66
201
  private readonly state$;
67
- constructor(id: string, registry: PluginRegistry, engine: PdfEngine);
202
+ private readonly interactionManager;
203
+ private readonly selection;
204
+ private readonly history;
205
+ /** Map <subtype> → <modeId>. Filled once in `initialize()`. */
206
+ private readonly modeBySubtype;
207
+ /** The inverse map for quick lookup in onModeChange(). */
208
+ private readonly subtypeByMode;
209
+ private readonly modeChange$;
210
+ private readonly activeTool$;
211
+ constructor(id: string, registry: PluginRegistry, engine: PdfEngine, config: AnnotationPluginConfig);
68
212
  initialize(): Promise<void>;
213
+ private registerTool;
69
214
  protected buildCapability(): AnnotationCapability;
70
- onStoreUpdated(_prevState: AnnotationState, newState: AnnotationState): void;
215
+ private emitActiveTool;
216
+ onStoreUpdated(prev: AnnotationState, next: AnnotationState): void;
71
217
  private getAllAnnotations;
72
218
  private getPageAnnotations;
73
219
  private selectAnnotation;
74
- private updateSelectedAnnotationColor;
220
+ private createAnnotation;
221
+ private updateAnnotation;
222
+ private deleteAnnotation;
223
+ private commit;
75
224
  }
76
225
 
77
226
  declare const ANNOTATION_PLUGIN_ID = "annotation";
78
227
  declare const manifest: PluginManifest<AnnotationPluginConfig>;
79
228
 
229
+ /** All annotations _objects_ on a single page (order preserved). */
230
+ declare const getAnnotationsByPageIndex: (s: AnnotationState, page: number) => TrackedAnnotation[];
231
+ /** Shortcut: every page → list of annotation objects. */
232
+ declare const getAnnotations: (s: AnnotationState) => Record<number, TrackedAnnotation[]>;
233
+ /** The full `TrackedAnnotation` for the current selection. */
234
+ declare const getSelectedAnnotation: (s: AnnotationState) => TrackedAnnotation | null;
235
+ declare const getSelectedAnnotationWithPageIndex: (s: AnnotationState) => SelectedAnnotation | null;
236
+ declare const getSelectedAnnotationByPageIndex: (s: AnnotationState, pageIndex: number) => TrackedAnnotation | null;
237
+ declare const isInAnnotationMode: (s: AnnotationState) => boolean;
238
+ declare const getSelectedAnnotationMode: (s: AnnotationState) => keyof ToolDefaultsBySubtype | null;
239
+ /** Check if a given anno on a page is the current selection. */
240
+ declare const isAnnotationSelected: (s: AnnotationState, page: number, id: number) => boolean;
241
+
80
242
  declare const AnnotationPluginPackage: PluginPackage<AnnotationPlugin, AnnotationPluginConfig, AnnotationState, AnnotationAction>;
81
243
 
82
- export { ANNOTATION_PLUGIN_ID, type AnnotationCapability, AnnotationPlugin, type AnnotationPluginConfig, AnnotationPluginPackage, type AnnotationState, type GetPageAnnotationsOptions, type SelectedAnnotation, type UpdateAnnotationColorOptions, manifest };
244
+ export { ANNOTATION_PLUGIN_ID, type ActiveTool, type AnnotationCapability, type AnnotationDefaults, AnnotationPlugin, type AnnotationPluginConfig, AnnotationPluginPackage, type AnnotationState, type BaseAnnotationDefaults, type CommitState, type GetPageAnnotationsOptions, type HighlightDefaults, type SelectedAnnotation, type SquigglyDefaults, type StrikeoutDefaults, type StylableSubtype, type ToolDefaults, type ToolDefaultsBySubtype, type TrackedAnnotation, type UnderlineDefaults, type UpdateAnnotationColorOptions, getAnnotations, getAnnotationsByPageIndex, getSelectedAnnotation, getSelectedAnnotationByPageIndex, getSelectedAnnotationMode, getSelectedAnnotationWithPageIndex, isAnnotationSelected, isInAnnotationMode, manifest };