@embedpdf/plugin-annotation 1.0.6 → 1.0.8

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