@embedpdf/plugin-annotation 1.0.10 → 1.0.12
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.cjs +2 -693
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +1 -266
- package/dist/index.js +180 -158
- package/dist/index.js.map +1 -1
- package/dist/lib/actions.d.ts +104 -0
- package/dist/lib/annotation-plugin.d.ts +33 -0
- package/dist/lib/index.d.ts +10 -0
- package/dist/lib/manifest.d.ts +4 -0
- package/dist/lib/reducer.d.ts +5 -0
- package/dist/lib/selectors.d.ts +13 -0
- package/dist/lib/types.d.ts +121 -0
- package/dist/lib/utils.d.ts +11 -0
- package/dist/lib/variant-key.d.ts +8 -0
- package/dist/preact/adapter.d.ts +8 -0
- package/dist/preact/core.d.ts +1 -0
- package/dist/preact/index.cjs +2 -1038
- package/dist/preact/index.cjs.map +1 -1
- package/dist/preact/index.d.ts +1 -28
- package/dist/preact/index.js +126 -156
- package/dist/preact/index.js.map +1 -1
- package/dist/preact/interaction-manager.d.ts +1 -0
- package/dist/preact/selection.d.ts +1 -0
- package/dist/react/adapter.d.ts +2 -0
- package/dist/react/core.d.ts +1 -0
- package/dist/react/index.cjs +2 -0
- package/dist/react/index.cjs.map +1 -0
- package/dist/react/index.d.ts +1 -0
- package/dist/react/index.js +983 -0
- package/dist/react/index.js.map +1 -0
- package/dist/react/interaction-manager.d.ts +1 -0
- package/dist/react/selection.d.ts +1 -0
- package/dist/shared-preact/components/annotation-container.d.ts +19 -0
- package/dist/shared-preact/components/annotation-layer.d.ts +11 -0
- package/dist/shared-preact/components/annotations/ink-highlight-paint.d.ts +0 -0
- package/dist/shared-preact/components/annotations/ink-paint.d.ts +17 -0
- package/dist/shared-preact/components/annotations/ink.d.ts +23 -0
- package/dist/shared-preact/components/annotations.d.ts +7 -0
- package/dist/shared-preact/components/index.d.ts +1 -0
- package/dist/shared-preact/components/render-annotation.d.ts +11 -0
- package/dist/shared-preact/components/text-markup/highlight.d.ts +13 -0
- package/dist/shared-preact/components/text-markup/squiggly.d.ts +13 -0
- package/dist/shared-preact/components/text-markup/strikeout.d.ts +13 -0
- package/dist/shared-preact/components/text-markup/underline.d.ts +13 -0
- package/dist/shared-preact/components/text-markup.d.ts +6 -0
- package/dist/shared-preact/hooks/index.d.ts +1 -0
- package/dist/shared-preact/hooks/use-annotation.d.ts +11 -0
- package/dist/shared-preact/index.d.ts +2 -0
- package/dist/shared-preact/resize-ink.d.ts +17 -0
- package/dist/shared-preact/types.d.ts +1 -0
- package/dist/shared-react/components/annotation-container.d.ts +19 -0
- package/dist/shared-react/components/annotation-layer.d.ts +11 -0
- package/dist/shared-react/components/annotations/ink-highlight-paint.d.ts +0 -0
- package/dist/shared-react/components/annotations/ink-paint.d.ts +17 -0
- package/dist/shared-react/components/annotations/ink.d.ts +23 -0
- package/dist/shared-react/components/annotations.d.ts +7 -0
- package/dist/shared-react/components/index.d.ts +1 -0
- package/dist/shared-react/components/render-annotation.d.ts +11 -0
- package/dist/shared-react/components/text-markup/highlight.d.ts +13 -0
- package/dist/shared-react/components/text-markup/squiggly.d.ts +13 -0
- package/dist/shared-react/components/text-markup/strikeout.d.ts +13 -0
- package/dist/shared-react/components/text-markup/underline.d.ts +13 -0
- package/dist/shared-react/components/text-markup.d.ts +6 -0
- package/dist/shared-react/hooks/index.d.ts +1 -0
- package/dist/shared-react/hooks/use-annotation.d.ts +11 -0
- package/dist/shared-react/index.d.ts +2 -0
- package/dist/shared-react/resize-ink.d.ts +17 -0
- package/dist/shared-react/types.d.ts +1 -0
- package/package.json +17 -15
- package/dist/index.d.cts +0 -266
- package/dist/preact/index.d.cts +0 -28
package/dist/index.cjs
CHANGED
|
@@ -1,693 +1,2 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
-
var __export = (target, all) => {
|
|
7
|
-
for (var name in all)
|
|
8
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
-
};
|
|
10
|
-
var __copyProps = (to, from, except, desc) => {
|
|
11
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
-
for (let key of __getOwnPropNames(from))
|
|
13
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
-
}
|
|
16
|
-
return to;
|
|
17
|
-
};
|
|
18
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
-
|
|
20
|
-
// src/index.ts
|
|
21
|
-
var index_exports = {};
|
|
22
|
-
__export(index_exports, {
|
|
23
|
-
ANNOTATION_PLUGIN_ID: () => ANNOTATION_PLUGIN_ID,
|
|
24
|
-
AnnotationPlugin: () => AnnotationPlugin,
|
|
25
|
-
AnnotationPluginPackage: () => AnnotationPluginPackage,
|
|
26
|
-
getAnnotations: () => getAnnotations,
|
|
27
|
-
getAnnotationsByPageIndex: () => getAnnotationsByPageIndex,
|
|
28
|
-
getSelectedAnnotation: () => getSelectedAnnotation,
|
|
29
|
-
getSelectedAnnotationByPageIndex: () => getSelectedAnnotationByPageIndex,
|
|
30
|
-
getSelectedAnnotationMode: () => getSelectedAnnotationMode,
|
|
31
|
-
getSelectedAnnotationWithPageIndex: () => getSelectedAnnotationWithPageIndex,
|
|
32
|
-
isAnnotationSelected: () => isAnnotationSelected,
|
|
33
|
-
isInAnnotationMode: () => isInAnnotationMode,
|
|
34
|
-
manifest: () => manifest
|
|
35
|
-
});
|
|
36
|
-
module.exports = __toCommonJS(index_exports);
|
|
37
|
-
|
|
38
|
-
// src/lib/manifest.ts
|
|
39
|
-
var ANNOTATION_PLUGIN_ID = "annotation";
|
|
40
|
-
var manifest = {
|
|
41
|
-
id: ANNOTATION_PLUGIN_ID,
|
|
42
|
-
name: "Annotation Plugin",
|
|
43
|
-
version: "1.0.0",
|
|
44
|
-
provides: ["annotation"],
|
|
45
|
-
requires: ["interaction-manager", "selection"],
|
|
46
|
-
optional: ["history"],
|
|
47
|
-
defaultConfig: {
|
|
48
|
-
enabled: true,
|
|
49
|
-
autoCommit: true
|
|
50
|
-
}
|
|
51
|
-
};
|
|
52
|
-
|
|
53
|
-
// src/lib/annotation-plugin.ts
|
|
54
|
-
var import_core = require("@embedpdf/core");
|
|
55
|
-
var import_models = require("@embedpdf/models");
|
|
56
|
-
|
|
57
|
-
// src/lib/actions.ts
|
|
58
|
-
var SET_ANNOTATIONS = "ANNOTATION/SET_ANNOTATIONS";
|
|
59
|
-
var REINDEX_PAGE_ANNOTATIONS = "ANNOTATION/REINDEX_PAGE";
|
|
60
|
-
var SELECT_ANNOTATION = "ANNOTATION/SELECT_ANNOTATION";
|
|
61
|
-
var DESELECT_ANNOTATION = "ANNOTATION/DESELECT_ANNOTATION";
|
|
62
|
-
var SET_ANNOTATION_MODE = "ANNOTATION/SET_ANNOTATION_MODE";
|
|
63
|
-
var UPDATE_TOOL_DEFAULTS = "ANNOTATION/UPDATE_TOOL_DEFAULTS";
|
|
64
|
-
var ADD_COLOR_PRESET = "ANNOTATION/ADD_COLOR_PRESET";
|
|
65
|
-
var CREATE_ANNOTATION = "ANNOTATION/CREATE_ANNOTATION";
|
|
66
|
-
var PATCH_ANNOTATION = "ANNOTATION/PATCH_ANNOTATION";
|
|
67
|
-
var DELETE_ANNOTATION = "ANNOTATION/DELETE_ANNOTATION";
|
|
68
|
-
var COMMIT_PENDING_CHANGES = "ANNOTATION/COMMIT";
|
|
69
|
-
var STORE_PDF_ID = "ANNOTATION/STORE_PDF_ID";
|
|
70
|
-
var PURGE_ANNOTATION = "ANNOTATION/PURGE_ANNOTATION";
|
|
71
|
-
var setAnnotations = (p) => ({
|
|
72
|
-
type: SET_ANNOTATIONS,
|
|
73
|
-
payload: p
|
|
74
|
-
});
|
|
75
|
-
var reindexPageAnnotations = (pageIndex) => ({
|
|
76
|
-
type: REINDEX_PAGE_ANNOTATIONS,
|
|
77
|
-
payload: { pageIndex }
|
|
78
|
-
});
|
|
79
|
-
var selectAnnotation = (pageIndex, localId) => ({
|
|
80
|
-
type: SELECT_ANNOTATION,
|
|
81
|
-
payload: { pageIndex, localId }
|
|
82
|
-
});
|
|
83
|
-
var deselectAnnotation = () => ({ type: DESELECT_ANNOTATION });
|
|
84
|
-
var setAnnotationMode = (m) => ({
|
|
85
|
-
type: SET_ANNOTATION_MODE,
|
|
86
|
-
payload: m
|
|
87
|
-
});
|
|
88
|
-
var updateToolDefaults = (subtype, patch) => ({ type: UPDATE_TOOL_DEFAULTS, payload: { subtype, patch } });
|
|
89
|
-
var addColorPreset = (c) => ({
|
|
90
|
-
type: ADD_COLOR_PRESET,
|
|
91
|
-
payload: c
|
|
92
|
-
});
|
|
93
|
-
var createAnnotation = (pageIndex, localId, annotation) => ({
|
|
94
|
-
type: CREATE_ANNOTATION,
|
|
95
|
-
payload: { pageIndex, localId, annotation }
|
|
96
|
-
});
|
|
97
|
-
var patchAnnotation = (pageIndex, localId, patch) => ({
|
|
98
|
-
type: PATCH_ANNOTATION,
|
|
99
|
-
payload: { pageIndex, localId, patch }
|
|
100
|
-
});
|
|
101
|
-
var deleteAnnotation = (pageIndex, localId) => ({
|
|
102
|
-
type: DELETE_ANNOTATION,
|
|
103
|
-
payload: { pageIndex, localId }
|
|
104
|
-
});
|
|
105
|
-
var commitPendingChanges = () => ({ type: COMMIT_PENDING_CHANGES });
|
|
106
|
-
var storePdfId = (uid, pdfId) => ({
|
|
107
|
-
type: STORE_PDF_ID,
|
|
108
|
-
payload: { uid, pdfId }
|
|
109
|
-
});
|
|
110
|
-
var purgeAnnotation = (uid) => ({
|
|
111
|
-
type: PURGE_ANNOTATION,
|
|
112
|
-
payload: { uid }
|
|
113
|
-
});
|
|
114
|
-
|
|
115
|
-
// src/lib/utils.ts
|
|
116
|
-
var makeUid = (pageIndex, localId) => `p${pageIndex}#${localId}`;
|
|
117
|
-
var parseUid = (uid) => {
|
|
118
|
-
const [pg, rest] = uid.slice(1).split("#");
|
|
119
|
-
return { pageIndex: Number(pg), localId: Number(rest) };
|
|
120
|
-
};
|
|
121
|
-
|
|
122
|
-
// src/lib/selectors.ts
|
|
123
|
-
var makeUid2 = (page, id) => `p${page}#${id}`;
|
|
124
|
-
var getAnnotationsByPageIndex = (s, page) => (s.pages[page] ?? []).map((uid) => s.byUid[uid]);
|
|
125
|
-
var getAnnotations = (s) => {
|
|
126
|
-
const out = {};
|
|
127
|
-
for (const p of Object.keys(s.pages).map(Number)) out[p] = getAnnotationsByPageIndex(s, p);
|
|
128
|
-
return out;
|
|
129
|
-
};
|
|
130
|
-
var getSelectedAnnotation = (s) => s.selectedUid ? s.byUid[s.selectedUid] : null;
|
|
131
|
-
var getSelectedAnnotationWithPageIndex = (s) => {
|
|
132
|
-
if (!s.selectedUid) return null;
|
|
133
|
-
const { pageIndex, localId } = parseUid(s.selectedUid);
|
|
134
|
-
return { pageIndex, localId, annotation: s.byUid[s.selectedUid].object };
|
|
135
|
-
};
|
|
136
|
-
var getSelectedAnnotationByPageIndex = (s, pageIndex) => {
|
|
137
|
-
if (!s.selectedUid) return null;
|
|
138
|
-
const pageUids = s.pages[pageIndex] ?? [];
|
|
139
|
-
if (pageUids.includes(s.selectedUid)) {
|
|
140
|
-
return s.byUid[s.selectedUid];
|
|
141
|
-
}
|
|
142
|
-
return null;
|
|
143
|
-
};
|
|
144
|
-
var isInAnnotationMode = (s) => s.annotationMode !== null;
|
|
145
|
-
var getSelectedAnnotationMode = (s) => s.annotationMode;
|
|
146
|
-
var isAnnotationSelected = (s, page, id) => s.selectedUid === makeUid2(page, id);
|
|
147
|
-
|
|
148
|
-
// src/lib/annotation-plugin.ts
|
|
149
|
-
var AnnotationPlugin = class extends import_core.BasePlugin {
|
|
150
|
-
constructor(id, registry, engine, config) {
|
|
151
|
-
super(id, registry);
|
|
152
|
-
this.ANNOTATION_HISTORY_TOPIC = "annotations";
|
|
153
|
-
this.state$ = (0, import_core.createBehaviorEmitter)();
|
|
154
|
-
/** Map <subtype> → <modeId>. Filled once in `initialize()`. */
|
|
155
|
-
this.modeBySubtype = /* @__PURE__ */ new Map();
|
|
156
|
-
/** The inverse map for quick lookup in onModeChange(). */
|
|
157
|
-
this.subtypeByMode = /* @__PURE__ */ new Map();
|
|
158
|
-
this.modeChange$ = (0, import_core.createBehaviorEmitter)();
|
|
159
|
-
this.activeTool$ = (0, import_core.createBehaviorEmitter)({
|
|
160
|
-
mode: null,
|
|
161
|
-
defaults: null
|
|
162
|
-
});
|
|
163
|
-
this.engine = engine;
|
|
164
|
-
this.config = config;
|
|
165
|
-
const selection = registry.getPlugin("selection");
|
|
166
|
-
this.selection = selection?.provides() ?? null;
|
|
167
|
-
const history = registry.getPlugin("history");
|
|
168
|
-
this.history = history?.provides() ?? null;
|
|
169
|
-
const interactionManager = registry.getPlugin("interaction-manager");
|
|
170
|
-
this.interactionManager = interactionManager?.provides() ?? null;
|
|
171
|
-
this.coreStore.onAction(import_core.SET_DOCUMENT, (_action, state) => {
|
|
172
|
-
const doc = state.core.document;
|
|
173
|
-
if (doc) {
|
|
174
|
-
this.getAllAnnotations(doc);
|
|
175
|
-
}
|
|
176
|
-
});
|
|
177
|
-
}
|
|
178
|
-
async initialize() {
|
|
179
|
-
for (const [subtype, defaults] of (0, import_core.enumEntries)(this.state.toolDefaults)) {
|
|
180
|
-
this.registerTool(subtype, defaults);
|
|
181
|
-
}
|
|
182
|
-
this.history?.onHistoryChange((topic) => {
|
|
183
|
-
if (topic === this.ANNOTATION_HISTORY_TOPIC && this.config.autoCommit !== false) {
|
|
184
|
-
this.commit();
|
|
185
|
-
}
|
|
186
|
-
});
|
|
187
|
-
this.interactionManager?.onModeChange((s) => {
|
|
188
|
-
const newSubtype = this.subtypeByMode.get(s.activeMode) ?? null;
|
|
189
|
-
if (newSubtype !== this.state.annotationMode) {
|
|
190
|
-
this.dispatch(setAnnotationMode(newSubtype));
|
|
191
|
-
this.modeChange$.emit(newSubtype);
|
|
192
|
-
}
|
|
193
|
-
});
|
|
194
|
-
this.selection?.onEndSelection(() => {
|
|
195
|
-
if (!this.state.annotationMode) return;
|
|
196
|
-
if (!(this.state.annotationMode === import_models.PdfAnnotationSubtype.HIGHLIGHT || this.state.annotationMode === import_models.PdfAnnotationSubtype.UNDERLINE || this.state.annotationMode === import_models.PdfAnnotationSubtype.STRIKEOUT || this.state.annotationMode === import_models.PdfAnnotationSubtype.SQUIGGLY)) {
|
|
197
|
-
return;
|
|
198
|
-
}
|
|
199
|
-
const formattedSelection = this.selection?.getFormattedSelection();
|
|
200
|
-
if (!formattedSelection) return;
|
|
201
|
-
for (const selection of formattedSelection) {
|
|
202
|
-
const rect = selection.rect;
|
|
203
|
-
const segmentRects = selection.segmentRects;
|
|
204
|
-
const type = this.state.annotationMode;
|
|
205
|
-
const color = this.state.toolDefaults[type].color;
|
|
206
|
-
const opacity = this.state.toolDefaults[type].opacity;
|
|
207
|
-
this.createAnnotation(selection.pageIndex, {
|
|
208
|
-
type,
|
|
209
|
-
rect,
|
|
210
|
-
segmentRects,
|
|
211
|
-
color,
|
|
212
|
-
opacity,
|
|
213
|
-
pageIndex: selection.pageIndex,
|
|
214
|
-
id: Date.now() + Math.random()
|
|
215
|
-
});
|
|
216
|
-
}
|
|
217
|
-
this.selection?.clear();
|
|
218
|
-
});
|
|
219
|
-
}
|
|
220
|
-
registerTool(subtype, defaults) {
|
|
221
|
-
const modeId = defaults.interaction.mode;
|
|
222
|
-
const interactionMode = {
|
|
223
|
-
id: modeId,
|
|
224
|
-
scope: "page",
|
|
225
|
-
exclusive: defaults.interaction.exclusive,
|
|
226
|
-
cursor: defaults.interaction.cursor
|
|
227
|
-
};
|
|
228
|
-
this.interactionManager?.registerMode(interactionMode);
|
|
229
|
-
if (defaults.textSelection) {
|
|
230
|
-
this.selection?.enableForMode(modeId);
|
|
231
|
-
}
|
|
232
|
-
this.modeBySubtype.set(subtype, modeId);
|
|
233
|
-
this.subtypeByMode.set(modeId, subtype);
|
|
234
|
-
}
|
|
235
|
-
buildCapability() {
|
|
236
|
-
return {
|
|
237
|
-
getPageAnnotations: (options) => {
|
|
238
|
-
return this.getPageAnnotations(options);
|
|
239
|
-
},
|
|
240
|
-
getSelectedAnnotation: () => {
|
|
241
|
-
return getSelectedAnnotation(this.state);
|
|
242
|
-
},
|
|
243
|
-
selectAnnotation: (pageIndex, annotationId) => {
|
|
244
|
-
this.selectAnnotation(pageIndex, annotationId);
|
|
245
|
-
},
|
|
246
|
-
deselectAnnotation: () => {
|
|
247
|
-
this.dispatch(deselectAnnotation());
|
|
248
|
-
},
|
|
249
|
-
getAnnotationMode: () => {
|
|
250
|
-
return this.state.annotationMode;
|
|
251
|
-
},
|
|
252
|
-
setAnnotationMode: (subtype) => {
|
|
253
|
-
if (subtype === this.state.annotationMode) return;
|
|
254
|
-
if (subtype) {
|
|
255
|
-
const mode = this.modeBySubtype.get(subtype);
|
|
256
|
-
if (!mode) throw new Error(`Mode missing for subtype ${subtype}`);
|
|
257
|
-
this.interactionManager?.activate(mode);
|
|
258
|
-
} else {
|
|
259
|
-
this.interactionManager?.activate("default");
|
|
260
|
-
}
|
|
261
|
-
},
|
|
262
|
-
getToolDefaults: (subtype) => {
|
|
263
|
-
const defaults = this.state.toolDefaults[subtype];
|
|
264
|
-
if (!defaults) {
|
|
265
|
-
throw new Error(`No defaults found for subtype: ${subtype}`);
|
|
266
|
-
}
|
|
267
|
-
return defaults;
|
|
268
|
-
},
|
|
269
|
-
setToolDefaults: (subtype, patch) => {
|
|
270
|
-
this.dispatch(updateToolDefaults(subtype, patch));
|
|
271
|
-
},
|
|
272
|
-
getColorPresets: () => [...this.state.colorPresets],
|
|
273
|
-
addColorPreset: (color) => this.dispatch(addColorPreset(color)),
|
|
274
|
-
createAnnotation: (pageIndex, annotation) => this.createAnnotation(pageIndex, annotation),
|
|
275
|
-
updateAnnotation: (pageIndex, localId, patch) => this.updateAnnotation(pageIndex, localId, patch),
|
|
276
|
-
deleteAnnotation: (pageIndex, localId) => this.deleteAnnotation(pageIndex, localId),
|
|
277
|
-
renderAnnotation: (options) => this.renderAnnotation(options),
|
|
278
|
-
onStateChange: this.state$.on,
|
|
279
|
-
onModeChange: this.modeChange$.on,
|
|
280
|
-
onActiveToolChange: this.activeTool$.on,
|
|
281
|
-
commit: () => this.commit()
|
|
282
|
-
};
|
|
283
|
-
}
|
|
284
|
-
createActiveTool(mode, toolDefaults) {
|
|
285
|
-
if (mode === null) {
|
|
286
|
-
return { mode: null, defaults: null };
|
|
287
|
-
}
|
|
288
|
-
return { mode, defaults: toolDefaults[mode] };
|
|
289
|
-
}
|
|
290
|
-
emitActiveTool(state) {
|
|
291
|
-
const activeTool = this.createActiveTool(state.annotationMode, state.toolDefaults);
|
|
292
|
-
this.activeTool$.emit(activeTool);
|
|
293
|
-
}
|
|
294
|
-
onStoreUpdated(prev, next) {
|
|
295
|
-
this.state$.emit(next);
|
|
296
|
-
if (prev.annotationMode !== next.annotationMode || prev.toolDefaults[prev.annotationMode ?? import_models.PdfAnnotationSubtype.HIGHLIGHT] !== next.toolDefaults[next.annotationMode ?? import_models.PdfAnnotationSubtype.HIGHLIGHT]) {
|
|
297
|
-
this.emitActiveTool(next);
|
|
298
|
-
}
|
|
299
|
-
}
|
|
300
|
-
getAllAnnotations(doc) {
|
|
301
|
-
const task = this.engine.getAllAnnotations(doc);
|
|
302
|
-
task.wait((annotations) => this.dispatch(setAnnotations(annotations)), import_models.ignore);
|
|
303
|
-
}
|
|
304
|
-
getPageAnnotations(options) {
|
|
305
|
-
const { pageIndex } = options;
|
|
306
|
-
const doc = this.coreState.core.document;
|
|
307
|
-
if (!doc) {
|
|
308
|
-
return import_models.PdfTaskHelper.reject({ code: import_models.PdfErrorCode.NotFound, message: "Document not found" });
|
|
309
|
-
}
|
|
310
|
-
const page = doc.pages.find((p) => p.index === pageIndex);
|
|
311
|
-
if (!page) {
|
|
312
|
-
return import_models.PdfTaskHelper.reject({ code: import_models.PdfErrorCode.NotFound, message: "Page not found" });
|
|
313
|
-
}
|
|
314
|
-
return this.engine.getPageAnnotations(doc, page);
|
|
315
|
-
}
|
|
316
|
-
renderAnnotation({
|
|
317
|
-
pageIndex,
|
|
318
|
-
annotation,
|
|
319
|
-
scaleFactor = 1,
|
|
320
|
-
rotation = import_models.Rotation.Degree0,
|
|
321
|
-
dpr = 1,
|
|
322
|
-
mode = import_models.AppearanceMode.Normal,
|
|
323
|
-
imageType = "image/webp"
|
|
324
|
-
}) {
|
|
325
|
-
const coreState = this.coreState.core;
|
|
326
|
-
if (!coreState.document) {
|
|
327
|
-
throw new Error("document does not open");
|
|
328
|
-
}
|
|
329
|
-
const page = coreState.document.pages.find((page2) => page2.index === pageIndex);
|
|
330
|
-
if (!page) {
|
|
331
|
-
throw new Error("page does not exist");
|
|
332
|
-
}
|
|
333
|
-
return this.engine.renderAnnotation(
|
|
334
|
-
coreState.document,
|
|
335
|
-
page,
|
|
336
|
-
annotation,
|
|
337
|
-
scaleFactor,
|
|
338
|
-
rotation,
|
|
339
|
-
dpr,
|
|
340
|
-
mode,
|
|
341
|
-
imageType
|
|
342
|
-
);
|
|
343
|
-
}
|
|
344
|
-
selectAnnotation(pageIndex, annotationId) {
|
|
345
|
-
this.dispatch(selectAnnotation(pageIndex, annotationId));
|
|
346
|
-
}
|
|
347
|
-
createAnnotation(pageIndex, annotation) {
|
|
348
|
-
const localId = annotation.id;
|
|
349
|
-
const execute = () => this.dispatch(createAnnotation(pageIndex, localId, annotation));
|
|
350
|
-
if (!this.history) {
|
|
351
|
-
execute();
|
|
352
|
-
if (this.config.autoCommit) this.commit();
|
|
353
|
-
return;
|
|
354
|
-
}
|
|
355
|
-
const command = {
|
|
356
|
-
execute,
|
|
357
|
-
undo: () => {
|
|
358
|
-
this.dispatch(deselectAnnotation());
|
|
359
|
-
this.dispatch(deleteAnnotation(pageIndex, localId));
|
|
360
|
-
}
|
|
361
|
-
};
|
|
362
|
-
this.history.register(command, this.ANNOTATION_HISTORY_TOPIC);
|
|
363
|
-
}
|
|
364
|
-
updateAnnotation(pageIndex, localId, patch) {
|
|
365
|
-
if (!this.history) {
|
|
366
|
-
this.dispatch(patchAnnotation(pageIndex, localId, patch));
|
|
367
|
-
if (this.config.autoCommit !== false) {
|
|
368
|
-
this.commit();
|
|
369
|
-
}
|
|
370
|
-
return;
|
|
371
|
-
}
|
|
372
|
-
const originalObject = this.state.byUid[makeUid(pageIndex, localId)].object;
|
|
373
|
-
const originalPatch = Object.fromEntries(
|
|
374
|
-
Object.keys(patch).map((key) => [key, originalObject[key]])
|
|
375
|
-
);
|
|
376
|
-
const command = {
|
|
377
|
-
execute: () => this.dispatch(patchAnnotation(pageIndex, localId, patch)),
|
|
378
|
-
undo: () => this.dispatch(patchAnnotation(pageIndex, localId, originalPatch))
|
|
379
|
-
};
|
|
380
|
-
this.history.register(command, this.ANNOTATION_HISTORY_TOPIC);
|
|
381
|
-
}
|
|
382
|
-
deleteAnnotation(pageIndex, localId) {
|
|
383
|
-
if (!this.history) {
|
|
384
|
-
this.dispatch(deselectAnnotation());
|
|
385
|
-
this.dispatch(deleteAnnotation(pageIndex, localId));
|
|
386
|
-
if (this.config.autoCommit !== false) {
|
|
387
|
-
this.commit();
|
|
388
|
-
}
|
|
389
|
-
return;
|
|
390
|
-
}
|
|
391
|
-
const originalAnnotation = this.state.byUid[makeUid(pageIndex, localId)].object;
|
|
392
|
-
const command = {
|
|
393
|
-
execute: () => {
|
|
394
|
-
this.dispatch(deselectAnnotation());
|
|
395
|
-
this.dispatch(deleteAnnotation(pageIndex, localId));
|
|
396
|
-
},
|
|
397
|
-
undo: () => this.dispatch(createAnnotation(pageIndex, localId, originalAnnotation))
|
|
398
|
-
};
|
|
399
|
-
this.history.register(command, this.ANNOTATION_HISTORY_TOPIC);
|
|
400
|
-
}
|
|
401
|
-
commit() {
|
|
402
|
-
const task = new import_models.Task();
|
|
403
|
-
if (!this.state.hasPendingChanges) return import_models.PdfTaskHelper.resolve(true);
|
|
404
|
-
const doc = this.coreState.core.document;
|
|
405
|
-
if (!doc)
|
|
406
|
-
return import_models.PdfTaskHelper.reject({ code: import_models.PdfErrorCode.NotFound, message: "Document not found" });
|
|
407
|
-
const creations = [];
|
|
408
|
-
const updates = [];
|
|
409
|
-
const deletionsByPage = /* @__PURE__ */ new Map();
|
|
410
|
-
const affectedPages = /* @__PURE__ */ new Set();
|
|
411
|
-
for (const [uid, ta] of Object.entries(this.state.byUid)) {
|
|
412
|
-
if (ta.commitState === "synced") continue;
|
|
413
|
-
const { pageIndex } = parseUid(uid);
|
|
414
|
-
const page = doc.pages.find((p) => p.index === pageIndex);
|
|
415
|
-
if (!page) continue;
|
|
416
|
-
affectedPages.add(pageIndex);
|
|
417
|
-
switch (ta.commitState) {
|
|
418
|
-
case "new":
|
|
419
|
-
const task2 = this.engine.createPageAnnotation(doc, page, ta.object);
|
|
420
|
-
task2.wait((annoId) => this.dispatch(storePdfId(uid, annoId)), import_models.ignore);
|
|
421
|
-
creations.push(task2);
|
|
422
|
-
break;
|
|
423
|
-
case "dirty":
|
|
424
|
-
updates.push(
|
|
425
|
-
this.engine.updatePageAnnotation(doc, page, { ...ta.object, id: ta.pdfId })
|
|
426
|
-
);
|
|
427
|
-
break;
|
|
428
|
-
case "deleted":
|
|
429
|
-
if (!deletionsByPage.has(pageIndex)) {
|
|
430
|
-
deletionsByPage.set(pageIndex, []);
|
|
431
|
-
}
|
|
432
|
-
deletionsByPage.get(pageIndex).push({ ta, uid });
|
|
433
|
-
break;
|
|
434
|
-
}
|
|
435
|
-
}
|
|
436
|
-
const deletionTasks = [];
|
|
437
|
-
for (const [pageIndex, deletions] of deletionsByPage.entries()) {
|
|
438
|
-
const page = doc.pages.find((p) => p.index === pageIndex);
|
|
439
|
-
deletions.sort((a, b) => (b.ta.pdfId ?? -1) - (a.ta.pdfId ?? -1));
|
|
440
|
-
for (const { ta, uid } of deletions) {
|
|
441
|
-
if (ta.pdfId !== void 0) {
|
|
442
|
-
const task2 = new import_models.Task();
|
|
443
|
-
const removeTask = this.engine.removePageAnnotation(doc, page, {
|
|
444
|
-
...ta.object,
|
|
445
|
-
id: ta.pdfId
|
|
446
|
-
});
|
|
447
|
-
removeTask.wait(() => {
|
|
448
|
-
this.dispatch(purgeAnnotation(uid));
|
|
449
|
-
task2.resolve(true);
|
|
450
|
-
}, task2.fail);
|
|
451
|
-
deletionTasks.push(task2);
|
|
452
|
-
} else {
|
|
453
|
-
this.dispatch(purgeAnnotation(uid));
|
|
454
|
-
}
|
|
455
|
-
}
|
|
456
|
-
}
|
|
457
|
-
const allWriteTasks = [...creations, ...updates, ...deletionTasks];
|
|
458
|
-
import_models.Task.allSettled(allWriteTasks).wait(() => {
|
|
459
|
-
for (const pageIndex of affectedPages) {
|
|
460
|
-
this.dispatch(reindexPageAnnotations(pageIndex));
|
|
461
|
-
}
|
|
462
|
-
this.dispatch(commitPendingChanges());
|
|
463
|
-
task.resolve(true);
|
|
464
|
-
}, task.fail);
|
|
465
|
-
return task;
|
|
466
|
-
}
|
|
467
|
-
};
|
|
468
|
-
AnnotationPlugin.id = "annotation";
|
|
469
|
-
|
|
470
|
-
// src/lib/reducer.ts
|
|
471
|
-
var import_models2 = require("@embedpdf/models");
|
|
472
|
-
var DEFAULT_COLORS = [
|
|
473
|
-
"#E44234",
|
|
474
|
-
"#FF8D00",
|
|
475
|
-
"#FFCD45",
|
|
476
|
-
"#5CC96E",
|
|
477
|
-
"#25D2D1",
|
|
478
|
-
"#597CE2",
|
|
479
|
-
"#C544CE",
|
|
480
|
-
"#7D2E25"
|
|
481
|
-
];
|
|
482
|
-
var patchAnno = (state, uid, patch) => {
|
|
483
|
-
const prev = state.byUid[uid];
|
|
484
|
-
if (!prev) return state;
|
|
485
|
-
return {
|
|
486
|
-
...state,
|
|
487
|
-
byUid: {
|
|
488
|
-
...state.byUid,
|
|
489
|
-
[uid]: {
|
|
490
|
-
...prev,
|
|
491
|
-
commitState: prev.commitState === "synced" ? "dirty" : prev.commitState,
|
|
492
|
-
object: { ...prev.object, ...patch }
|
|
493
|
-
}
|
|
494
|
-
},
|
|
495
|
-
hasPendingChanges: true
|
|
496
|
-
};
|
|
497
|
-
};
|
|
498
|
-
var initialState = (cfg) => ({
|
|
499
|
-
pages: {},
|
|
500
|
-
byUid: {},
|
|
501
|
-
selectedUid: null,
|
|
502
|
-
annotationMode: null,
|
|
503
|
-
toolDefaults: {
|
|
504
|
-
[import_models2.PdfAnnotationSubtype.HIGHLIGHT]: {
|
|
505
|
-
name: "Highlight",
|
|
506
|
-
color: "#FFCD45",
|
|
507
|
-
opacity: 1,
|
|
508
|
-
interaction: { mode: "highlight", exclusive: false },
|
|
509
|
-
textSelection: true
|
|
510
|
-
},
|
|
511
|
-
[import_models2.PdfAnnotationSubtype.UNDERLINE]: {
|
|
512
|
-
name: "Underline",
|
|
513
|
-
color: "#E44234",
|
|
514
|
-
opacity: 1,
|
|
515
|
-
interaction: { mode: "underline", exclusive: false },
|
|
516
|
-
textSelection: true
|
|
517
|
-
},
|
|
518
|
-
[import_models2.PdfAnnotationSubtype.STRIKEOUT]: {
|
|
519
|
-
name: "Strikeout",
|
|
520
|
-
color: "#E44234",
|
|
521
|
-
opacity: 1,
|
|
522
|
-
interaction: { mode: "strikeout", exclusive: false },
|
|
523
|
-
textSelection: true
|
|
524
|
-
},
|
|
525
|
-
[import_models2.PdfAnnotationSubtype.SQUIGGLY]: {
|
|
526
|
-
name: "Squiggly",
|
|
527
|
-
color: "#E44234",
|
|
528
|
-
opacity: 1,
|
|
529
|
-
interaction: { mode: "squiggly", exclusive: false },
|
|
530
|
-
textSelection: true
|
|
531
|
-
},
|
|
532
|
-
[import_models2.PdfAnnotationSubtype.INK]: {
|
|
533
|
-
name: "Ink",
|
|
534
|
-
color: "#E44234",
|
|
535
|
-
opacity: 1,
|
|
536
|
-
strokeWidth: 11,
|
|
537
|
-
interaction: { mode: "ink", exclusive: true, cursor: "crosshair" },
|
|
538
|
-
textSelection: false
|
|
539
|
-
},
|
|
540
|
-
...cfg.toolDefaults
|
|
541
|
-
},
|
|
542
|
-
colorPresets: cfg.colorPresets ?? DEFAULT_COLORS,
|
|
543
|
-
hasPendingChanges: false
|
|
544
|
-
});
|
|
545
|
-
var reducer = (state, action) => {
|
|
546
|
-
switch (action.type) {
|
|
547
|
-
/* ───── bulk load from engine ───── */
|
|
548
|
-
case SET_ANNOTATIONS: {
|
|
549
|
-
const newPages = { ...state.pages };
|
|
550
|
-
const newByUid = { ...state.byUid };
|
|
551
|
-
for (const [pgStr, list] of Object.entries(action.payload)) {
|
|
552
|
-
const pageIndex = Number(pgStr);
|
|
553
|
-
const oldUidsOnPage = state.pages[pageIndex] || [];
|
|
554
|
-
for (const uid of oldUidsOnPage) {
|
|
555
|
-
delete newByUid[uid];
|
|
556
|
-
}
|
|
557
|
-
const newUidsOnPage = list.map((a, index) => {
|
|
558
|
-
const localId = Date.now() + Math.random() + index;
|
|
559
|
-
const uid = makeUid(pageIndex, localId);
|
|
560
|
-
newByUid[uid] = { localId, pdfId: a.id, commitState: "synced", object: a };
|
|
561
|
-
return uid;
|
|
562
|
-
});
|
|
563
|
-
newPages[pageIndex] = newUidsOnPage;
|
|
564
|
-
}
|
|
565
|
-
return { ...state, pages: newPages, byUid: newByUid };
|
|
566
|
-
}
|
|
567
|
-
/* ───── GUI bits ───── */
|
|
568
|
-
case SET_ANNOTATION_MODE:
|
|
569
|
-
return { ...state, annotationMode: action.payload };
|
|
570
|
-
case SELECT_ANNOTATION:
|
|
571
|
-
return {
|
|
572
|
-
...state,
|
|
573
|
-
selectedUid: makeUid(action.payload.pageIndex, action.payload.localId)
|
|
574
|
-
};
|
|
575
|
-
case DESELECT_ANNOTATION:
|
|
576
|
-
return { ...state, selectedUid: null };
|
|
577
|
-
case ADD_COLOR_PRESET:
|
|
578
|
-
return state.colorPresets.includes(action.payload) ? state : { ...state, colorPresets: [...state.colorPresets, action.payload] };
|
|
579
|
-
case UPDATE_TOOL_DEFAULTS: {
|
|
580
|
-
const { subtype, patch } = action.payload;
|
|
581
|
-
return {
|
|
582
|
-
...state,
|
|
583
|
-
toolDefaults: {
|
|
584
|
-
...state.toolDefaults,
|
|
585
|
-
[subtype]: { ...state.toolDefaults[subtype], ...patch }
|
|
586
|
-
}
|
|
587
|
-
};
|
|
588
|
-
}
|
|
589
|
-
/* ───── create ───── */
|
|
590
|
-
case CREATE_ANNOTATION: {
|
|
591
|
-
const { pageIndex, localId, annotation } = action.payload;
|
|
592
|
-
const uid = makeUid(pageIndex, localId);
|
|
593
|
-
return {
|
|
594
|
-
...state,
|
|
595
|
-
pages: { ...state.pages, [pageIndex]: [...state.pages[pageIndex] ?? [], uid] },
|
|
596
|
-
byUid: {
|
|
597
|
-
...state.byUid,
|
|
598
|
-
[uid]: { localId, pdfId: void 0, commitState: "new", object: annotation }
|
|
599
|
-
},
|
|
600
|
-
hasPendingChanges: true
|
|
601
|
-
};
|
|
602
|
-
}
|
|
603
|
-
/* ───── delete ───── */
|
|
604
|
-
case DELETE_ANNOTATION: {
|
|
605
|
-
const { pageIndex, localId } = action.payload;
|
|
606
|
-
const uid = makeUid(pageIndex, localId);
|
|
607
|
-
if (!state.byUid[uid]) return state;
|
|
608
|
-
return {
|
|
609
|
-
...state,
|
|
610
|
-
pages: {
|
|
611
|
-
...state.pages,
|
|
612
|
-
[pageIndex]: (state.pages[pageIndex] ?? []).filter((u) => u !== uid)
|
|
613
|
-
},
|
|
614
|
-
byUid: {
|
|
615
|
-
...state.byUid,
|
|
616
|
-
[uid]: { ...state.byUid[uid], commitState: "deleted" }
|
|
617
|
-
},
|
|
618
|
-
hasPendingChanges: true
|
|
619
|
-
};
|
|
620
|
-
}
|
|
621
|
-
/* ───── field edits ───── */
|
|
622
|
-
case PATCH_ANNOTATION: {
|
|
623
|
-
const uid = makeUid(action.payload.pageIndex, action.payload.localId);
|
|
624
|
-
return patchAnno(state, uid, action.payload.patch);
|
|
625
|
-
}
|
|
626
|
-
/* ───── commit bookkeeping ───── */
|
|
627
|
-
case COMMIT_PENDING_CHANGES: {
|
|
628
|
-
const cleaned = {};
|
|
629
|
-
for (const [uid, ta] of Object.entries(state.byUid)) {
|
|
630
|
-
cleaned[uid] = {
|
|
631
|
-
...ta,
|
|
632
|
-
commitState: ta.commitState === "dirty" || ta.commitState === "new" ? "synced" : ta.commitState
|
|
633
|
-
};
|
|
634
|
-
}
|
|
635
|
-
return { ...state, byUid: cleaned, hasPendingChanges: false };
|
|
636
|
-
}
|
|
637
|
-
case REINDEX_PAGE_ANNOTATIONS: {
|
|
638
|
-
const { pageIndex } = action.payload;
|
|
639
|
-
const newByUid = { ...state.byUid };
|
|
640
|
-
const uidsOnPage = state.pages[pageIndex] || [];
|
|
641
|
-
const annosOnPage = uidsOnPage.map((uid) => state.byUid[uid]).filter((ta) => ta && ta.commitState !== "deleted");
|
|
642
|
-
annosOnPage.sort((a, b) => (a.pdfId ?? Infinity) - (b.pdfId ?? Infinity));
|
|
643
|
-
annosOnPage.forEach((ta, newPdfId) => {
|
|
644
|
-
const uid = makeUid(pageIndex, ta.localId);
|
|
645
|
-
newByUid[uid] = { ...newByUid[uid], pdfId: newPdfId };
|
|
646
|
-
});
|
|
647
|
-
return { ...state, byUid: newByUid };
|
|
648
|
-
}
|
|
649
|
-
case STORE_PDF_ID: {
|
|
650
|
-
const { uid, pdfId } = action.payload;
|
|
651
|
-
const ta = state.byUid[uid];
|
|
652
|
-
if (!ta) return state;
|
|
653
|
-
return {
|
|
654
|
-
...state,
|
|
655
|
-
byUid: {
|
|
656
|
-
...state.byUid,
|
|
657
|
-
[uid]: { ...ta, pdfId, commitState: "synced" }
|
|
658
|
-
}
|
|
659
|
-
};
|
|
660
|
-
}
|
|
661
|
-
case PURGE_ANNOTATION: {
|
|
662
|
-
const { uid } = action.payload;
|
|
663
|
-
const { [uid]: _gone, ...rest } = state.byUid;
|
|
664
|
-
return { ...state, byUid: rest };
|
|
665
|
-
}
|
|
666
|
-
default:
|
|
667
|
-
return state;
|
|
668
|
-
}
|
|
669
|
-
};
|
|
670
|
-
|
|
671
|
-
// src/lib/index.ts
|
|
672
|
-
var AnnotationPluginPackage = {
|
|
673
|
-
manifest,
|
|
674
|
-
create: (registry, engine, config) => new AnnotationPlugin(ANNOTATION_PLUGIN_ID, registry, engine, config),
|
|
675
|
-
reducer,
|
|
676
|
-
initialState: (_, config) => initialState(config)
|
|
677
|
-
};
|
|
678
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
679
|
-
0 && (module.exports = {
|
|
680
|
-
ANNOTATION_PLUGIN_ID,
|
|
681
|
-
AnnotationPlugin,
|
|
682
|
-
AnnotationPluginPackage,
|
|
683
|
-
getAnnotations,
|
|
684
|
-
getAnnotationsByPageIndex,
|
|
685
|
-
getSelectedAnnotation,
|
|
686
|
-
getSelectedAnnotationByPageIndex,
|
|
687
|
-
getSelectedAnnotationMode,
|
|
688
|
-
getSelectedAnnotationWithPageIndex,
|
|
689
|
-
isAnnotationSelected,
|
|
690
|
-
isInAnnotationMode,
|
|
691
|
-
manifest
|
|
692
|
-
});
|
|
693
|
-
//# sourceMappingURL=index.cjs.map
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const t=require("@embedpdf/core"),e=require("@embedpdf/models"),n="annotation",o={id:n,name:"Annotation Plugin",version:"1.0.0",provides:["annotation"],requires:["interaction-manager","selection"],optional:["history"],defaultConfig:{enabled:!0,autoCommit:!0}},i="ANNOTATION/SET_ANNOTATIONS",a="ANNOTATION/REINDEX_PAGE",s="ANNOTATION/SELECT_ANNOTATION",r="ANNOTATION/DESELECT_ANNOTATION",d="ANNOTATION/UPDATE_TOOL_DEFAULTS",c="ANNOTATION/ADD_COLOR_PRESET",l="ANNOTATION/CREATE_ANNOTATION",p="ANNOTATION/PATCH_ANNOTATION",u="ANNOTATION/DELETE_ANNOTATION",h="ANNOTATION/COMMIT",g="ANNOTATION/STORE_PDF_ID",y="ANNOTATION/PURGE_ANNOTATION",f="ANNOTATION/SET_ACTIVE_VARIANT",A=t=>({type:a,payload:{pageIndex:t}}),m=()=>({type:r}),I=(t,e,n)=>({type:l,payload:{pageIndex:t,localId:e,annotation:n}}),T=(t,e,n)=>({type:p,payload:{pageIndex:t,localId:e,patch:n}}),N=(t,e)=>({type:u,payload:{pageIndex:t,localId:e}}),b=(t,e)=>({type:g,payload:{uid:t,pdfId:e}}),v=t=>({type:y,payload:{uid:t}}),O=(t,e)=>`p${t}#${e}`,S=t=>{const[e,n]=t.slice(1).split("#");return{pageIndex:Number(e),localId:Number(n)}},P=(t,e)=>(t.pages[e]??[]).map((e=>t.byUid[e])),x=t=>t.selectedUid?t.byUid[t.selectedUid]:null,E=(t,e)=>e?`${t}#${e}`:`${t}`,U=class extends t.BasePlugin{constructor(e,n,o,i){super(e,n),this.ANNOTATION_HISTORY_TOPIC="annotations",this.state$=t.createBehaviorEmitter(),this.modeByVariant=new Map,this.variantByMode=new Map,this.activeVariantChange$=t.createBehaviorEmitter(),this.activeTool$=t.createBehaviorEmitter({variantKey:null,defaults:null}),this.engine=o,this.config=i;const a=n.getPlugin("selection");this.selection=(null==a?void 0:a.provides())??null;const s=n.getPlugin("history");this.history=(null==s?void 0:s.provides())??null;const r=n.getPlugin("interaction-manager");this.interactionManager=(null==r?void 0:r.provides())??null,this.coreStore.onAction(t.SET_DOCUMENT,((t,e)=>{const n=e.core.document;n&&this.getAllAnnotations(n)}))}async initialize(){var t,n,o;for(const[e,i]of Object.entries(this.state.toolDefaults))this.registerTool(e,i);null==(t=this.history)||t.onHistoryChange((t=>{t===this.ANNOTATION_HISTORY_TOPIC&&!1!==this.config.autoCommit&&this.commit()})),null==(n=this.interactionManager)||n.onModeChange((t=>{const e=this.variantByMode.get(t.activeMode)??null;console.log(e,this.state.activeVariant),e!==this.state.activeVariant&&(this.dispatch({type:f,payload:e}),this.activeVariantChange$.emit(e))})),null==(o=this.selection)||o.onEndSelection((()=>{var t,n;if(!this.state.activeVariant)return;if(this.state.activeVariant!==E(e.PdfAnnotationSubtype.HIGHLIGHT)&&this.state.activeVariant!==E(e.PdfAnnotationSubtype.UNDERLINE)&&this.state.activeVariant!==E(e.PdfAnnotationSubtype.STRIKEOUT)&&this.state.activeVariant!==E(e.PdfAnnotationSubtype.SQUIGGLY))return;const o=null==(t=this.selection)?void 0:t.getFormattedSelection();if(o){for(const t of o){const n=t.rect,o=t.segmentRects,i=this.state.activeVariant,a=this.state.toolDefaults[i].subtype,s=this.state.toolDefaults[i].color,r=this.state.toolDefaults[i].opacity,d=this.state.toolDefaults[i].blendMode??e.PdfBlendMode.Normal;this.createAnnotation(t.pageIndex,{type:a,rect:n,segmentRects:o,color:s,opacity:r,blendMode:d,pageIndex:t.pageIndex,id:Date.now()+Math.random()})}null==(n=this.selection)||n.clear()}}))}registerTool(t,e){var n,o;const i=e.interaction.mode,a={id:i,scope:"page",exclusive:e.interaction.exclusive,cursor:e.interaction.cursor};null==(n=this.interactionManager)||n.registerMode(a),e.textSelection&&(null==(o=this.selection)||o.enableForMode(i)),this.modeByVariant.set(t,i),this.variantByMode.set(i,t)}buildCapability(){return{getPageAnnotations:t=>this.getPageAnnotations(t),getSelectedAnnotation:()=>x(this.state),selectAnnotation:(t,e)=>{this.selectAnnotation(t,e)},deselectAnnotation:()=>{this.dispatch(m())},getActiveVariant:()=>this.state.activeVariant,setActiveVariant:t=>{var e,n;if(t!==this.state.activeVariant)if(t){const n=this.modeByVariant.get(t);if(!n)throw new Error(`Mode missing for variant ${t}`);null==(e=this.interactionManager)||e.activate(n)}else null==(n=this.interactionManager)||n.activate("default")},getToolDefaults:t=>{const e=this.state.toolDefaults[t];if(!e)throw new Error(`No defaults found for variant: ${t}`);return e},getToolDefaultsBySubtypeAndIntent:(t,e)=>{const n=E(t,e),o=this.state.toolDefaults[n];if(!o)throw new Error(`No defaults found for variant: ${n}`);return o},getToolDefaultsBySubtype:t=>{const e=this.state.toolDefaults[t];if(!e)throw new Error(`No defaults found for subtype: ${t}`);return e},setToolDefaults:(t,e)=>{this.dispatch(((t,e)=>({type:d,payload:{variantKey:t,patch:e}}))(t,e))},getColorPresets:()=>[...this.state.colorPresets],addColorPreset:t=>this.dispatch({type:c,payload:t}),createAnnotation:(t,e)=>this.createAnnotation(t,e),updateAnnotation:(t,e,n)=>this.updateAnnotation(t,e,n),deleteAnnotation:(t,e)=>this.deleteAnnotation(t,e),renderAnnotation:t=>this.renderAnnotation(t),onStateChange:this.state$.on,onActiveVariantChange:this.activeVariantChange$.on,onActiveToolChange:this.activeTool$.on,commit:()=>this.commit()}}createActiveTool(t,e){return null===t?{variantKey:null,defaults:null}:{variantKey:t,defaults:e[t]}}emitActiveTool(t){const e=this.createActiveTool(t.activeVariant,t.toolDefaults);this.activeTool$.emit(e)}onStoreUpdated(t,n){this.state$.emit(n),t.activeVariant===n.activeVariant&&t.toolDefaults[t.activeVariant??e.PdfAnnotationSubtype.HIGHLIGHT]===n.toolDefaults[n.activeVariant??e.PdfAnnotationSubtype.HIGHLIGHT]||this.emitActiveTool(n)}getAllAnnotations(t){this.engine.getAllAnnotations(t).wait((t=>this.dispatch({type:i,payload:t})),e.ignore)}getPageAnnotations(t){const{pageIndex:n}=t,o=this.coreState.core.document;if(!o)return e.PdfTaskHelper.reject({code:e.PdfErrorCode.NotFound,message:"Document not found"});const i=o.pages.find((t=>t.index===n));return i?this.engine.getPageAnnotations(o,i):e.PdfTaskHelper.reject({code:e.PdfErrorCode.NotFound,message:"Page not found"})}renderAnnotation({pageIndex:t,annotation:n,scaleFactor:o=1,rotation:i=e.Rotation.Degree0,dpr:a=1,mode:s=e.AppearanceMode.Normal,imageType:r="image/webp"}){const d=this.coreState.core;if(!d.document)throw new Error("document does not open");const c=d.document.pages.find((e=>e.index===t));if(!c)throw new Error("page does not exist");return this.engine.renderAnnotation(d.document,c,n,o,i,a,s,r)}selectAnnotation(t,e){this.dispatch(((t,e)=>({type:s,payload:{pageIndex:t,localId:e}}))(t,e))}createAnnotation(t,e){const n=e.id,o=()=>this.dispatch(I(t,n,e));if(!this.history)return o(),void(this.config.autoCommit&&this.commit());const i={execute:o,undo:()=>{this.dispatch(m()),this.dispatch(N(t,n))}};this.history.register(i,this.ANNOTATION_HISTORY_TOPIC)}updateAnnotation(t,e,n){if(!this.history)return this.dispatch(T(t,e,n)),void(!1!==this.config.autoCommit&&this.commit());const o=this.state.byUid[O(t,e)].object,i=Object.fromEntries(Object.keys(n).map((t=>[t,o[t]]))),a={execute:()=>this.dispatch(T(t,e,n)),undo:()=>this.dispatch(T(t,e,i))};this.history.register(a,this.ANNOTATION_HISTORY_TOPIC)}deleteAnnotation(t,e){if(!this.history)return this.dispatch(m()),this.dispatch(N(t,e)),void(!1!==this.config.autoCommit&&this.commit());const n=this.state.byUid[O(t,e)].object,o={execute:()=>{this.dispatch(m()),this.dispatch(N(t,e))},undo:()=>this.dispatch(I(t,e,n))};this.history.register(o,this.ANNOTATION_HISTORY_TOPIC)}commit(){const t=new e.Task;if(!this.state.hasPendingChanges)return e.PdfTaskHelper.resolve(!0);const n=this.coreState.core.document;if(!n)return e.PdfTaskHelper.reject({code:e.PdfErrorCode.NotFound,message:"Document not found"});const o=[],i=[],a=new Map,s=new Set;for(const[c,l]of Object.entries(this.state.byUid)){if("synced"===l.commitState)continue;const{pageIndex:t}=S(c),r=n.pages.find((e=>e.index===t));if(r)switch(s.add(t),l.commitState){case"new":const s=this.engine.createPageAnnotation(n,r,l.object);s.wait((t=>this.dispatch(b(c,t))),e.ignore),o.push(s);break;case"dirty":i.push(this.engine.updatePageAnnotation(n,r,{...l.object,id:l.pdfId}));break;case"deleted":a.has(t)||a.set(t,[]),a.get(t).push({ta:l,uid:c})}}const r=[];for(const[c,l]of a.entries()){const t=n.pages.find((t=>t.index===c));l.sort(((t,e)=>(e.ta.pdfId??-1)-(t.ta.pdfId??-1)));for(const{ta:o,uid:i}of l)if(void 0!==o.pdfId){const a=new e.Task;this.engine.removePageAnnotation(n,t,{...o.object,id:o.pdfId}).wait((()=>{this.dispatch(v(i)),a.resolve(!0)}),a.fail),r.push(a)}else this.dispatch(v(i))}const d=[...o,...i,...r];return e.Task.allSettled(d).wait((()=>{for(const t of s)this.dispatch(A(t));this.dispatch({type:h}),t.resolve(!0)}),t.fail),t}};U.id="annotation";let C=U;const D=["#E44234","#FF8D00","#FFCD45","#5CC96E","#25D2D1","#597CE2","#C544CE","#7D2E25"],M={manifest:o,create:(t,e,o)=>new C(n,t,e,o),reducer:(t,e)=>{switch(e.type){case i:{const n={...t.pages},o={...t.byUid};for(const[i,a]of Object.entries(e.payload)){const e=Number(i),s=t.pages[e]||[];for(const t of s)delete o[t];const r=a.map(((t,n)=>{const i=Date.now()+Math.random()+n,a=O(e,i);return o[a]={localId:i,pdfId:t.id,commitState:"synced",object:t},a}));n[e]=r}return{...t,pages:n,byUid:o}}case f:return{...t,activeVariant:e.payload};case s:return{...t,selectedUid:O(e.payload.pageIndex,e.payload.localId)};case r:return{...t,selectedUid:null};case c:return t.colorPresets.includes(e.payload)?t:{...t,colorPresets:[...t.colorPresets,e.payload]};case d:{const{variantKey:n,patch:o}=e.payload,i=t.toolDefaults[n];return i?{...t,toolDefaults:{...t.toolDefaults,[n]:{...i,...o}}}:t}case l:{const{pageIndex:n,localId:o,annotation:i}=e.payload,a=O(n,o);return{...t,pages:{...t.pages,[n]:[...t.pages[n]??[],a]},byUid:{...t.byUid,[a]:{localId:o,pdfId:void 0,commitState:"new",object:i}},hasPendingChanges:!0}}case u:{const{pageIndex:n,localId:o}=e.payload,i=O(n,o);return t.byUid[i]?{...t,pages:{...t.pages,[n]:(t.pages[n]??[]).filter((t=>t!==i))},byUid:{...t.byUid,[i]:{...t.byUid[i],commitState:"deleted"}},hasPendingChanges:!0}:t}case p:{const n=O(e.payload.pageIndex,e.payload.localId);return((t,e,n)=>{const o=t.byUid[e];return o?{...t,byUid:{...t.byUid,[e]:{...o,commitState:"synced"===o.commitState?"dirty":o.commitState,object:{...o.object,...n}}},hasPendingChanges:!0}:t})(t,n,e.payload.patch)}case h:{const e={};for(const[n,o]of Object.entries(t.byUid))e[n]={...o,commitState:"dirty"===o.commitState||"new"===o.commitState?"synced":o.commitState};return{...t,byUid:e,hasPendingChanges:!1}}case a:{const{pageIndex:n}=e.payload,o={...t.byUid},i=(t.pages[n]||[]).map((e=>t.byUid[e])).filter((t=>t&&"deleted"!==t.commitState));return i.sort(((t,e)=>(t.pdfId??1/0)-(e.pdfId??1/0))),i.forEach(((t,e)=>{const i=O(n,t.localId);o[i]={...o[i],pdfId:e}})),{...t,byUid:o}}case g:{const{uid:n,pdfId:o}=e.payload,i=t.byUid[n];return i?{...t,byUid:{...t.byUid,[n]:{...i,pdfId:o,commitState:"synced"}}}:t}case y:{const{uid:n}=e.payload,{[n]:o,...i}=t.byUid;return{...t,byUid:i}}default:return t}},initialState:(t,n)=>{return o=n,{pages:{},byUid:{},selectedUid:null,activeVariant:null,toolDefaults:{[E(e.PdfAnnotationSubtype.HIGHLIGHT)]:{name:"Highlight",subtype:e.PdfAnnotationSubtype.HIGHLIGHT,interaction:{mode:"highlight",exclusive:!1},textSelection:!0,color:"#FFCD45",opacity:1,blendMode:e.PdfBlendMode.Multiply},[E(e.PdfAnnotationSubtype.UNDERLINE)]:{name:"Underline",subtype:e.PdfAnnotationSubtype.UNDERLINE,interaction:{mode:"underline",exclusive:!1},textSelection:!0,color:"#E44234",opacity:1,blendMode:e.PdfBlendMode.Normal},[E(e.PdfAnnotationSubtype.STRIKEOUT)]:{name:"Strikeout",subtype:e.PdfAnnotationSubtype.STRIKEOUT,interaction:{mode:"strikeout",exclusive:!1},textSelection:!0,color:"#E44234",opacity:1,blendMode:e.PdfBlendMode.Normal},[E(e.PdfAnnotationSubtype.SQUIGGLY)]:{name:"Squiggly",subtype:e.PdfAnnotationSubtype.SQUIGGLY,interaction:{mode:"squiggly",exclusive:!1},textSelection:!0,color:"#E44234",opacity:1,blendMode:e.PdfBlendMode.Normal},[E(e.PdfAnnotationSubtype.INK)]:{name:"Ink",subtype:e.PdfAnnotationSubtype.INK,interaction:{mode:"ink",exclusive:!0,cursor:"crosshair"},color:"#E44234",opacity:1,strokeWidth:11,blendMode:e.PdfBlendMode.Normal},[E(e.PdfAnnotationSubtype.INK,"InkHighlight")]:{name:"Ink Highlight",subtype:e.PdfAnnotationSubtype.INK,interaction:{mode:"inkHighlight",exclusive:!0,cursor:"crosshair"},color:"#E44234",opacity:1,strokeWidth:11,blendMode:e.PdfBlendMode.Multiply},...o.toolDefaults},colorPresets:o.colorPresets??D,hasPendingChanges:!1};var o}};exports.ANNOTATION_PLUGIN_ID=n,exports.AnnotationPlugin=C,exports.AnnotationPluginPackage=M,exports.getAnnotations=t=>{const e={};for(const n of Object.keys(t.pages).map(Number))e[n]=P(t,n);return e},exports.getAnnotationsByPageIndex=P,exports.getSelectedAnnotation=x,exports.getSelectedAnnotationByPageIndex=(t,e)=>{if(!t.selectedUid)return null;return(t.pages[e]??[]).includes(t.selectedUid)?t.byUid[t.selectedUid]:null},exports.getSelectedAnnotationVariant=t=>t.activeVariant,exports.getSelectedAnnotationWithPageIndex=t=>{if(!t.selectedUid)return null;const{pageIndex:e,localId:n}=S(t.selectedUid);return{pageIndex:e,localId:n,annotation:t.byUid[t.selectedUid].object}},exports.isAnnotationSelected=(t,e,n)=>t.selectedUid===((t,e)=>`p${t}#${e}`)(e,n),exports.isInAnnotationVariant=t=>null!==t.activeVariant,exports.makeVariantKey=E,exports.manifest=o,exports.parseVariantKey=t=>{const[e,n]=t.split("#");return{subtype:Number(e),intent:n}},exports.variantKeyFromAnnotation=t=>E(t.type,t.intent);
|
|
2
|
+
//# sourceMappingURL=index.cjs.map
|