@pdfmergy-embedpdf/plugin-ai-manager 2.6.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (49) hide show
  1. package/dist/index.cjs +2 -0
  2. package/dist/index.cjs.map +1 -0
  3. package/dist/index.d.ts +1 -0
  4. package/dist/index.js +897 -0
  5. package/dist/index.js.map +1 -0
  6. package/dist/lib/actions.d.ts +33 -0
  7. package/dist/lib/ai-manager-plugin.d.ts +18 -0
  8. package/dist/lib/index.d.ts +8 -0
  9. package/dist/lib/manifest.d.ts +4 -0
  10. package/dist/lib/reducer.d.ts +5 -0
  11. package/dist/lib/types.d.ts +50 -0
  12. package/dist/preact/adapter.d.ts +1 -0
  13. package/dist/preact/core.d.ts +1 -0
  14. package/dist/preact/index.cjs +2 -0
  15. package/dist/preact/index.cjs.map +1 -0
  16. package/dist/preact/index.d.ts +1 -0
  17. package/dist/preact/index.js +1310 -0
  18. package/dist/preact/index.js.map +1 -0
  19. package/dist/react/adapter.d.ts +1 -0
  20. package/dist/react/core.d.ts +1 -0
  21. package/dist/react/index.cjs +2 -0
  22. package/dist/react/index.cjs.map +1 -0
  23. package/dist/react/index.d.ts +1 -0
  24. package/dist/react/index.js +1309 -0
  25. package/dist/react/index.js.map +1 -0
  26. package/dist/shared/hooks/index.d.ts +1 -0
  27. package/dist/shared/hooks/use-ai-manager.d.ts +11 -0
  28. package/dist/shared/index.d.ts +2 -0
  29. package/dist/shared-preact/hooks/index.d.ts +1 -0
  30. package/dist/shared-preact/hooks/use-ai-manager.d.ts +11 -0
  31. package/dist/shared-preact/index.d.ts +2 -0
  32. package/dist/shared-react/hooks/index.d.ts +1 -0
  33. package/dist/shared-react/hooks/use-ai-manager.d.ts +11 -0
  34. package/dist/shared-react/index.d.ts +2 -0
  35. package/dist/svelte/hooks/index.d.ts +1 -0
  36. package/dist/svelte/hooks/use-ai-manager.svelte.d.ts +11 -0
  37. package/dist/svelte/index.cjs +2 -0
  38. package/dist/svelte/index.cjs.map +1 -0
  39. package/dist/svelte/index.d.ts +2 -0
  40. package/dist/svelte/index.js +1299 -0
  41. package/dist/svelte/index.js.map +1 -0
  42. package/dist/vue/hooks/index.d.ts +1 -0
  43. package/dist/vue/hooks/use-ai-manager.d.ts +3 -0
  44. package/dist/vue/index.cjs +2 -0
  45. package/dist/vue/index.cjs.map +1 -0
  46. package/dist/vue/index.d.ts +2 -0
  47. package/dist/vue/index.js +1282 -0
  48. package/dist/vue/index.js.map +1 -0
  49. package/package.json +81 -0
package/dist/index.js ADDED
@@ -0,0 +1,897 @@
1
+ const PdfSoftHyphenMarker = "­";
2
+ const PdfZeroWidthSpace = "​";
3
+ const PdfWordJoiner = "⁠";
4
+ const PdfBomOrZwnbsp = "\uFEFF";
5
+ const PdfNonCharacterFFFE = "￾";
6
+ const PdfNonCharacterFFFF = "￿";
7
+ const PdfUnwantedTextMarkers = Object.freeze([
8
+ PdfSoftHyphenMarker,
9
+ PdfZeroWidthSpace,
10
+ PdfWordJoiner,
11
+ PdfBomOrZwnbsp,
12
+ PdfNonCharacterFFFE,
13
+ PdfNonCharacterFFFF
14
+ ]);
15
+ new RegExp(`[${PdfUnwantedTextMarkers.join("")}]`, "g");
16
+ var PdfStandardFont = /* @__PURE__ */ ((PdfStandardFont2) => {
17
+ PdfStandardFont2[PdfStandardFont2["Unknown"] = -1] = "Unknown";
18
+ PdfStandardFont2[PdfStandardFont2["Courier"] = 0] = "Courier";
19
+ PdfStandardFont2[PdfStandardFont2["Courier_Bold"] = 1] = "Courier_Bold";
20
+ PdfStandardFont2[PdfStandardFont2["Courier_BoldOblique"] = 2] = "Courier_BoldOblique";
21
+ PdfStandardFont2[PdfStandardFont2["Courier_Oblique"] = 3] = "Courier_Oblique";
22
+ PdfStandardFont2[PdfStandardFont2["Helvetica"] = 4] = "Helvetica";
23
+ PdfStandardFont2[PdfStandardFont2["Helvetica_Bold"] = 5] = "Helvetica_Bold";
24
+ PdfStandardFont2[PdfStandardFont2["Helvetica_BoldOblique"] = 6] = "Helvetica_BoldOblique";
25
+ PdfStandardFont2[PdfStandardFont2["Helvetica_Oblique"] = 7] = "Helvetica_Oblique";
26
+ PdfStandardFont2[PdfStandardFont2["Times_Roman"] = 8] = "Times_Roman";
27
+ PdfStandardFont2[PdfStandardFont2["Times_Bold"] = 9] = "Times_Bold";
28
+ PdfStandardFont2[PdfStandardFont2["Times_BoldItalic"] = 10] = "Times_BoldItalic";
29
+ PdfStandardFont2[PdfStandardFont2["Times_Italic"] = 11] = "Times_Italic";
30
+ PdfStandardFont2[PdfStandardFont2["Symbol"] = 12] = "Symbol";
31
+ PdfStandardFont2[PdfStandardFont2["ZapfDingbats"] = 13] = "ZapfDingbats";
32
+ return PdfStandardFont2;
33
+ })(PdfStandardFont || {});
34
+ var PdfTextAlignment = /* @__PURE__ */ ((PdfTextAlignment2) => {
35
+ PdfTextAlignment2[PdfTextAlignment2["Left"] = 0] = "Left";
36
+ PdfTextAlignment2[PdfTextAlignment2["Center"] = 1] = "Center";
37
+ PdfTextAlignment2[PdfTextAlignment2["Right"] = 2] = "Right";
38
+ return PdfTextAlignment2;
39
+ })(PdfTextAlignment || {});
40
+ var PdfBlendMode = /* @__PURE__ */ ((PdfBlendMode2) => {
41
+ PdfBlendMode2[PdfBlendMode2["Normal"] = 0] = "Normal";
42
+ PdfBlendMode2[PdfBlendMode2["Multiply"] = 1] = "Multiply";
43
+ PdfBlendMode2[PdfBlendMode2["Screen"] = 2] = "Screen";
44
+ PdfBlendMode2[PdfBlendMode2["Overlay"] = 3] = "Overlay";
45
+ PdfBlendMode2[PdfBlendMode2["Darken"] = 4] = "Darken";
46
+ PdfBlendMode2[PdfBlendMode2["Lighten"] = 5] = "Lighten";
47
+ PdfBlendMode2[PdfBlendMode2["ColorDodge"] = 6] = "ColorDodge";
48
+ PdfBlendMode2[PdfBlendMode2["ColorBurn"] = 7] = "ColorBurn";
49
+ PdfBlendMode2[PdfBlendMode2["HardLight"] = 8] = "HardLight";
50
+ PdfBlendMode2[PdfBlendMode2["SoftLight"] = 9] = "SoftLight";
51
+ PdfBlendMode2[PdfBlendMode2["Difference"] = 10] = "Difference";
52
+ PdfBlendMode2[PdfBlendMode2["Exclusion"] = 11] = "Exclusion";
53
+ PdfBlendMode2[PdfBlendMode2["Hue"] = 12] = "Hue";
54
+ PdfBlendMode2[PdfBlendMode2["Saturation"] = 13] = "Saturation";
55
+ PdfBlendMode2[PdfBlendMode2["Color"] = 14] = "Color";
56
+ PdfBlendMode2[PdfBlendMode2["Luminosity"] = 15] = "Luminosity";
57
+ return PdfBlendMode2;
58
+ })(PdfBlendMode || {});
59
+ const PdfAnnotationFlagName = Object.freeze({
60
+ [
61
+ 1
62
+ /* INVISIBLE */
63
+ ]: "invisible",
64
+ [
65
+ 2
66
+ /* HIDDEN */
67
+ ]: "hidden",
68
+ [
69
+ 4
70
+ /* PRINT */
71
+ ]: "print",
72
+ [
73
+ 8
74
+ /* NO_ZOOM */
75
+ ]: "noZoom",
76
+ [
77
+ 16
78
+ /* NO_ROTATE */
79
+ ]: "noRotate",
80
+ [
81
+ 32
82
+ /* NO_VIEW */
83
+ ]: "noView",
84
+ [
85
+ 64
86
+ /* READ_ONLY */
87
+ ]: "readOnly",
88
+ [
89
+ 128
90
+ /* LOCKED */
91
+ ]: "locked",
92
+ [
93
+ 256
94
+ /* TOGGLE_NOVIEW */
95
+ ]: "toggleNoView"
96
+ });
97
+ Object.entries(
98
+ PdfAnnotationFlagName
99
+ ).reduce(
100
+ (acc, [bit, name]) => {
101
+ acc[name] = Number(bit);
102
+ return acc;
103
+ },
104
+ {}
105
+ );
106
+ var PdfPermissionFlag = /* @__PURE__ */ ((PdfPermissionFlag2) => {
107
+ PdfPermissionFlag2[PdfPermissionFlag2["Print"] = 4] = "Print";
108
+ PdfPermissionFlag2[PdfPermissionFlag2["ModifyContents"] = 8] = "ModifyContents";
109
+ PdfPermissionFlag2[PdfPermissionFlag2["CopyContents"] = 16] = "CopyContents";
110
+ PdfPermissionFlag2[PdfPermissionFlag2["ModifyAnnotations"] = 32] = "ModifyAnnotations";
111
+ PdfPermissionFlag2[PdfPermissionFlag2["FillForms"] = 256] = "FillForms";
112
+ PdfPermissionFlag2[PdfPermissionFlag2["ExtractForAccessibility"] = 512] = "ExtractForAccessibility";
113
+ PdfPermissionFlag2[PdfPermissionFlag2["AssembleDocument"] = 1024] = "AssembleDocument";
114
+ PdfPermissionFlag2[PdfPermissionFlag2["PrintHighQuality"] = 2048] = "PrintHighQuality";
115
+ PdfPermissionFlag2[PdfPermissionFlag2["AllowAll"] = 3900] = "AllowAll";
116
+ return PdfPermissionFlag2;
117
+ })(PdfPermissionFlag || {});
118
+ class PermissionDeniedError extends Error {
119
+ constructor(requiredFlags, currentPermissions) {
120
+ const flagNames = requiredFlags.map((f) => PdfPermissionFlag[f]).join(", ");
121
+ super(`Permission denied. Required: ${flagNames}`);
122
+ this.requiredFlags = requiredFlags;
123
+ this.currentPermissions = currentPermissions;
124
+ this.name = "PermissionDeniedError";
125
+ }
126
+ }
127
+ const TEXT_ALIGNMENT_INFOS = Object.freeze([
128
+ { id: PdfTextAlignment.Left, label: "Left", css: "left" },
129
+ { id: PdfTextAlignment.Center, label: "Center", css: "center" },
130
+ { id: PdfTextAlignment.Right, label: "Right", css: "right" }
131
+ ]);
132
+ TEXT_ALIGNMENT_INFOS.reduce(
133
+ (m, info) => {
134
+ m[info.id] = info;
135
+ return m;
136
+ },
137
+ {}
138
+ );
139
+ TEXT_ALIGNMENT_INFOS.reduce(
140
+ (m, info) => {
141
+ m[info.css] = info.id;
142
+ return m;
143
+ },
144
+ {}
145
+ );
146
+ TEXT_ALIGNMENT_INFOS.map((info) => ({
147
+ value: info.id,
148
+ label: info.label
149
+ }));
150
+ var PdfStandardFontFamily = /* @__PURE__ */ ((PdfStandardFontFamily2) => {
151
+ PdfStandardFontFamily2["Courier"] = "Courier";
152
+ PdfStandardFontFamily2["Helvetica"] = "Helvetica";
153
+ PdfStandardFontFamily2["Times"] = "Times";
154
+ PdfStandardFontFamily2["Symbol"] = "Symbol";
155
+ PdfStandardFontFamily2["ZapfDingbats"] = "ZapfDingbats";
156
+ PdfStandardFontFamily2["Unknown"] = "Unknown";
157
+ return PdfStandardFontFamily2;
158
+ })(PdfStandardFontFamily || {});
159
+ PdfStandardFont.Helvetica;
160
+ const HELVETICA_DESC = {
161
+ id: PdfStandardFont.Helvetica,
162
+ family: "Helvetica",
163
+ bold: false,
164
+ italic: false,
165
+ label: "Helvetica",
166
+ css: "Helvetica, Arial, sans-serif"
167
+ };
168
+ const STANDARD_FONT_DESCRIPTORS = Object.freeze([
169
+ {
170
+ id: PdfStandardFont.Courier,
171
+ family: "Courier",
172
+ bold: false,
173
+ italic: false,
174
+ label: "Courier",
175
+ css: "Courier, monospace"
176
+ },
177
+ {
178
+ id: PdfStandardFont.Courier_Bold,
179
+ family: "Courier",
180
+ bold: true,
181
+ italic: false,
182
+ label: "Courier Bold",
183
+ css: "Courier, monospace"
184
+ },
185
+ {
186
+ id: PdfStandardFont.Courier_BoldOblique,
187
+ family: "Courier",
188
+ bold: true,
189
+ italic: true,
190
+ label: "Courier Bold Oblique",
191
+ css: "Courier, monospace"
192
+ },
193
+ {
194
+ id: PdfStandardFont.Courier_Oblique,
195
+ family: "Courier",
196
+ bold: false,
197
+ italic: true,
198
+ label: "Courier Oblique",
199
+ css: "Courier, monospace"
200
+ },
201
+ HELVETICA_DESC,
202
+ {
203
+ id: PdfStandardFont.Helvetica_Bold,
204
+ family: "Helvetica",
205
+ bold: true,
206
+ italic: false,
207
+ label: "Helvetica Bold",
208
+ css: "Helvetica, Arial, sans-serif"
209
+ },
210
+ {
211
+ id: PdfStandardFont.Helvetica_BoldOblique,
212
+ family: "Helvetica",
213
+ bold: true,
214
+ italic: true,
215
+ label: "Helvetica Bold Oblique",
216
+ css: "Helvetica, Arial, sans-serif"
217
+ },
218
+ {
219
+ id: PdfStandardFont.Helvetica_Oblique,
220
+ family: "Helvetica",
221
+ bold: false,
222
+ italic: true,
223
+ label: "Helvetica Oblique",
224
+ css: "Helvetica, Arial, sans-serif"
225
+ },
226
+ {
227
+ id: PdfStandardFont.Times_Roman,
228
+ family: "Times",
229
+ bold: false,
230
+ italic: false,
231
+ label: "Times Roman",
232
+ css: '"Times New Roman", Times, serif'
233
+ },
234
+ {
235
+ id: PdfStandardFont.Times_Bold,
236
+ family: "Times",
237
+ bold: true,
238
+ italic: false,
239
+ label: "Times Bold",
240
+ css: '"Times New Roman", Times, serif'
241
+ },
242
+ {
243
+ id: PdfStandardFont.Times_BoldItalic,
244
+ family: "Times",
245
+ bold: true,
246
+ italic: true,
247
+ label: "Times Bold Italic",
248
+ css: '"Times New Roman", Times, serif'
249
+ },
250
+ {
251
+ id: PdfStandardFont.Times_Italic,
252
+ family: "Times",
253
+ bold: false,
254
+ italic: true,
255
+ label: "Times Italic",
256
+ css: '"Times New Roman", Times, serif'
257
+ },
258
+ {
259
+ id: PdfStandardFont.Symbol,
260
+ family: "Symbol",
261
+ bold: false,
262
+ italic: false,
263
+ label: "Symbol",
264
+ css: "Symbol, serif"
265
+ },
266
+ {
267
+ id: PdfStandardFont.ZapfDingbats,
268
+ family: "ZapfDingbats",
269
+ bold: false,
270
+ italic: false,
271
+ label: "Zapf Dingbats",
272
+ css: "ZapfDingbats, serif"
273
+ }
274
+ ]);
275
+ STANDARD_FONT_DESCRIPTORS.reduce((m, d) => (m[d.id] = d, m), {});
276
+ const familyStyleToId = /* @__PURE__ */ new Map();
277
+ for (const d of STANDARD_FONT_DESCRIPTORS) {
278
+ familyStyleToId.set(`${d.family}_${d.bold}_${d.italic}`, d.id);
279
+ }
280
+ Object.values(PdfStandardFontFamily).filter(
281
+ (f) => f !== "Unknown"
282
+ /* Unknown */
283
+ ).map((family) => ({ value: family, label: family }));
284
+ [
285
+ ...new Set(STANDARD_FONT_DESCRIPTORS.map((d) => d.family))
286
+ ];
287
+ const BLEND_MODE_INFOS = Object.freeze([
288
+ { id: PdfBlendMode.Normal, label: "Normal", css: "normal" },
289
+ { id: PdfBlendMode.Multiply, label: "Multiply", css: "multiply" },
290
+ { id: PdfBlendMode.Screen, label: "Screen", css: "screen" },
291
+ { id: PdfBlendMode.Overlay, label: "Overlay", css: "overlay" },
292
+ { id: PdfBlendMode.Darken, label: "Darken", css: "darken" },
293
+ { id: PdfBlendMode.Lighten, label: "Lighten", css: "lighten" },
294
+ { id: PdfBlendMode.ColorDodge, label: "Color Dodge", css: "color-dodge" },
295
+ { id: PdfBlendMode.ColorBurn, label: "Color Burn", css: "color-burn" },
296
+ { id: PdfBlendMode.HardLight, label: "Hard Light", css: "hard-light" },
297
+ { id: PdfBlendMode.SoftLight, label: "Soft Light", css: "soft-light" },
298
+ { id: PdfBlendMode.Difference, label: "Difference", css: "difference" },
299
+ { id: PdfBlendMode.Exclusion, label: "Exclusion", css: "exclusion" },
300
+ { id: PdfBlendMode.Hue, label: "Hue", css: "hue" },
301
+ { id: PdfBlendMode.Saturation, label: "Saturation", css: "saturation" },
302
+ { id: PdfBlendMode.Color, label: "Color", css: "color" },
303
+ { id: PdfBlendMode.Luminosity, label: "Luminosity", css: "luminosity" }
304
+ ]);
305
+ BLEND_MODE_INFOS.reduce(
306
+ (m, info) => {
307
+ m[info.id] = info;
308
+ return m;
309
+ },
310
+ {}
311
+ );
312
+ BLEND_MODE_INFOS.reduce(
313
+ (m, info) => {
314
+ m[info.css] = info.id;
315
+ return m;
316
+ },
317
+ {}
318
+ );
319
+ BLEND_MODE_INFOS.map((info) => ({
320
+ value: info.id,
321
+ label: info.label
322
+ }));
323
+ BLEND_MODE_INFOS.map((info) => info.id);
324
+ const START_LOADING_DOCUMENT = "START_LOADING_DOCUMENT";
325
+ const SET_DOCUMENT_LOADED = "SET_DOCUMENT_LOADED";
326
+ const CLOSE_DOCUMENT = "CLOSE_DOCUMENT";
327
+ const SET_SCALE = "SET_SCALE";
328
+ const SET_ROTATION = "SET_ROTATION";
329
+ ({
330
+ print: PdfPermissionFlag.Print,
331
+ modifyContents: PdfPermissionFlag.ModifyContents,
332
+ copyContents: PdfPermissionFlag.CopyContents,
333
+ modifyAnnotations: PdfPermissionFlag.ModifyAnnotations,
334
+ fillForms: PdfPermissionFlag.FillForms,
335
+ extractForAccessibility: PdfPermissionFlag.ExtractForAccessibility,
336
+ assembleDocument: PdfPermissionFlag.AssembleDocument,
337
+ printHighQuality: PdfPermissionFlag.PrintHighQuality
338
+ });
339
+ const ALL_PERMISSION_FLAGS = [
340
+ PdfPermissionFlag.Print,
341
+ PdfPermissionFlag.ModifyContents,
342
+ PdfPermissionFlag.CopyContents,
343
+ PdfPermissionFlag.ModifyAnnotations,
344
+ PdfPermissionFlag.FillForms,
345
+ PdfPermissionFlag.ExtractForAccessibility,
346
+ PdfPermissionFlag.AssembleDocument,
347
+ PdfPermissionFlag.PrintHighQuality
348
+ ];
349
+ const PERMISSION_FLAG_TO_NAME = {
350
+ [PdfPermissionFlag.Print]: "print",
351
+ [PdfPermissionFlag.ModifyContents]: "modifyContents",
352
+ [PdfPermissionFlag.CopyContents]: "copyContents",
353
+ [PdfPermissionFlag.ModifyAnnotations]: "modifyAnnotations",
354
+ [PdfPermissionFlag.FillForms]: "fillForms",
355
+ [PdfPermissionFlag.ExtractForAccessibility]: "extractForAccessibility",
356
+ [PdfPermissionFlag.AssembleDocument]: "assembleDocument",
357
+ [PdfPermissionFlag.PrintHighQuality]: "printHighQuality"
358
+ };
359
+ function getPermissionOverride(overrides, flag) {
360
+ if (!overrides) return void 0;
361
+ if (flag in overrides) {
362
+ return overrides[flag];
363
+ }
364
+ const name = PERMISSION_FLAG_TO_NAME[flag];
365
+ if (name && name in overrides) {
366
+ return overrides[name];
367
+ }
368
+ return void 0;
369
+ }
370
+ function getEffectivePermission(state, documentId, flag) {
371
+ var _a;
372
+ const docState = state.documents[documentId];
373
+ const docConfig = docState == null ? void 0 : docState.permissions;
374
+ const globalConfig = state.globalPermissions;
375
+ const pdfPermissions = ((_a = docState == null ? void 0 : docState.document) == null ? void 0 : _a.permissions) ?? PdfPermissionFlag.AllowAll;
376
+ const docOverride = getPermissionOverride(docConfig == null ? void 0 : docConfig.overrides, flag);
377
+ if (docOverride !== void 0) {
378
+ return docOverride;
379
+ }
380
+ const globalOverride = getPermissionOverride(globalConfig == null ? void 0 : globalConfig.overrides, flag);
381
+ if (globalOverride !== void 0) {
382
+ return globalOverride;
383
+ }
384
+ const enforce = (docConfig == null ? void 0 : docConfig.enforceDocumentPermissions) ?? (globalConfig == null ? void 0 : globalConfig.enforceDocumentPermissions) ?? true;
385
+ if (!enforce) return true;
386
+ return (pdfPermissions & flag) !== 0;
387
+ }
388
+ function getEffectivePermissions(state, documentId) {
389
+ return ALL_PERMISSION_FLAGS.reduce((acc, flag) => {
390
+ return getEffectivePermission(state, documentId, flag) ? acc | flag : acc;
391
+ }, 0);
392
+ }
393
+ class BasePlugin {
394
+ constructor(id, registry) {
395
+ this.id = id;
396
+ this.registry = registry;
397
+ this.cooldownActions = {};
398
+ this.debouncedTimeouts = {};
399
+ this.unsubscribeFromState = null;
400
+ this.unsubscribeFromCoreStore = null;
401
+ this.unsubscribeFromStartLoadingDocument = null;
402
+ this.unsubscribeFromSetDocumentLoaded = null;
403
+ this.unsubscribeFromCloseDocument = null;
404
+ this.unsubscribeFromSetScale = null;
405
+ this.unsubscribeFromSetRotation = null;
406
+ if (id !== this.constructor.id) {
407
+ throw new Error(
408
+ `Plugin ID mismatch: ${id} !== ${this.constructor.id}`
409
+ );
410
+ }
411
+ this.engine = this.registry.getEngine();
412
+ this.logger = this.registry.getLogger();
413
+ this.coreStore = this.registry.getStore();
414
+ this.pluginStore = this.coreStore.getPluginStore(this.id);
415
+ this.unsubscribeFromState = this.pluginStore.subscribeToState((action, newState, oldState) => {
416
+ this.onStoreUpdated(oldState, newState);
417
+ });
418
+ this.unsubscribeFromCoreStore = this.coreStore.subscribe((action, newState, oldState) => {
419
+ this.onCoreStoreUpdated(oldState, newState);
420
+ if (newState.core.activeDocumentId !== oldState.core.activeDocumentId) {
421
+ this.onActiveDocumentChanged(
422
+ oldState.core.activeDocumentId,
423
+ newState.core.activeDocumentId
424
+ );
425
+ }
426
+ });
427
+ this.unsubscribeFromStartLoadingDocument = this.coreStore.onAction(
428
+ START_LOADING_DOCUMENT,
429
+ (action) => {
430
+ this.onDocumentLoadingStarted(action.payload.documentId);
431
+ }
432
+ );
433
+ this.unsubscribeFromSetDocumentLoaded = this.coreStore.onAction(
434
+ SET_DOCUMENT_LOADED,
435
+ (action) => {
436
+ this.onDocumentLoaded(action.payload.documentId);
437
+ }
438
+ );
439
+ this.unsubscribeFromCloseDocument = this.coreStore.onAction(CLOSE_DOCUMENT, (action) => {
440
+ this.onDocumentClosed(action.payload.documentId);
441
+ });
442
+ this.unsubscribeFromSetScale = this.coreStore.onAction(SET_SCALE, (action, state) => {
443
+ const targetId = action.payload.documentId ?? state.core.activeDocumentId;
444
+ if (targetId) {
445
+ this.onScaleChanged(targetId, action.payload.scale);
446
+ }
447
+ });
448
+ this.unsubscribeFromSetRotation = this.coreStore.onAction(SET_ROTATION, (action, state) => {
449
+ const targetId = action.payload.documentId ?? state.core.activeDocumentId;
450
+ if (targetId) {
451
+ this.onRotationChanged(targetId, action.payload.rotation);
452
+ }
453
+ });
454
+ this.readyPromise = new Promise((resolve) => {
455
+ this.readyResolve = resolve;
456
+ });
457
+ this.readyResolve();
458
+ }
459
+ provides() {
460
+ if (!this._capability) {
461
+ const cap = this.buildCapability();
462
+ this._capability = Object.freeze(cap);
463
+ }
464
+ return this._capability;
465
+ }
466
+ /**
467
+ * Get a copy of the current state
468
+ */
469
+ get state() {
470
+ return this.pluginStore.getState();
471
+ }
472
+ /**
473
+ * Get a copy of the current core state
474
+ */
475
+ get coreState() {
476
+ return this.coreStore.getState();
477
+ }
478
+ /**
479
+ * @deprecated use `this.state` Get a copy of the current state
480
+ */
481
+ getState() {
482
+ return this.pluginStore.getState();
483
+ }
484
+ /**
485
+ * @deprecated use `this.coreState` Get a copy of the current core state
486
+ */
487
+ getCoreState() {
488
+ return this.coreStore.getState();
489
+ }
490
+ /**
491
+ * Core Dispatch
492
+ */
493
+ dispatchCoreAction(action) {
494
+ return this.coreStore.dispatchToCore(action);
495
+ }
496
+ /**
497
+ * Dispatch an action to all plugins
498
+ */
499
+ dispatchToAllPlugins(action) {
500
+ return this.coreStore.dispatch(action);
501
+ }
502
+ /**
503
+ * Dispatch an action
504
+ */
505
+ dispatch(action) {
506
+ return this.pluginStore.dispatch(action);
507
+ }
508
+ /**
509
+ * Dispatch an action with a cooldown to prevent rapid repeated calls
510
+ * This executes immediately if cooldown has expired, then blocks subsequent calls
511
+ * @param action The action to dispatch
512
+ * @param cooldownTime Time in ms for cooldown (default: 100ms)
513
+ * @returns boolean indicating whether the action was dispatched or blocked
514
+ */
515
+ cooldownDispatch(action, cooldownTime = 100) {
516
+ const now = Date.now();
517
+ const lastActionTime = this.cooldownActions[action.type] || 0;
518
+ if (now - lastActionTime >= cooldownTime) {
519
+ this.cooldownActions[action.type] = now;
520
+ this.dispatch(action);
521
+ return true;
522
+ }
523
+ return false;
524
+ }
525
+ /**
526
+ * Dispatch an action with true debouncing - waits for the delay after the last call
527
+ * Each new call resets the timer. Action only executes after no calls for the specified time.
528
+ * @param action The action to dispatch
529
+ * @param debounceTime Time in ms to wait after the last call
530
+ */
531
+ debouncedDispatch(action, debounceTime = 100) {
532
+ const actionKey = action.type;
533
+ if (this.debouncedTimeouts[actionKey]) {
534
+ clearTimeout(this.debouncedTimeouts[actionKey]);
535
+ }
536
+ this.debouncedTimeouts[actionKey] = setTimeout(() => {
537
+ this.dispatch(action);
538
+ delete this.debouncedTimeouts[actionKey];
539
+ }, debounceTime);
540
+ }
541
+ /**
542
+ * Cancel a pending debounced action
543
+ * @param actionType The action type to cancel
544
+ */
545
+ cancelDebouncedDispatch(actionType) {
546
+ if (this.debouncedTimeouts[actionType]) {
547
+ clearTimeout(this.debouncedTimeouts[actionType]);
548
+ delete this.debouncedTimeouts[actionType];
549
+ }
550
+ }
551
+ /**
552
+ * Subscribe to state changes
553
+ */
554
+ subscribe(listener) {
555
+ return this.pluginStore.subscribeToState(listener);
556
+ }
557
+ /**
558
+ * Subscribe to core store changes
559
+ */
560
+ subscribeToCoreStore(listener) {
561
+ return this.coreStore.subscribe(listener);
562
+ }
563
+ /**
564
+ * Called when the plugin store state is updated
565
+ * @param oldState Previous state
566
+ * @param newState New state
567
+ */
568
+ onStoreUpdated(oldState, newState) {
569
+ }
570
+ /**
571
+ * Called when the core store state is updated
572
+ * @param oldState Previous state
573
+ * @param newState New state
574
+ */
575
+ onCoreStoreUpdated(oldState, newState) {
576
+ }
577
+ /**
578
+ * Called when a document is opened
579
+ * Override to initialize per-document state
580
+ * @param documentId The ID of the document that was opened
581
+ */
582
+ onDocumentLoadingStarted(documentId) {
583
+ }
584
+ /**
585
+ * Called when a document is loaded
586
+ * @param documentId The ID of the document that is loaded
587
+ */
588
+ onDocumentLoaded(documentId) {
589
+ }
590
+ /**
591
+ * Called when a document is closed
592
+ * Override to cleanup per-document state
593
+ * @param documentId The ID of the document that was closed
594
+ */
595
+ onDocumentClosed(documentId) {
596
+ }
597
+ /**
598
+ * Called when the active document changes
599
+ * @param previousId The ID of the previous active document
600
+ * @param currentId The ID of the new active document
601
+ */
602
+ onActiveDocumentChanged(previousId, currentId) {
603
+ }
604
+ onScaleChanged(documentId, scale) {
605
+ }
606
+ onRotationChanged(documentId, rotation) {
607
+ }
608
+ /**
609
+ * Cleanup method to be called when plugin is being destroyed
610
+ */
611
+ destroy() {
612
+ Object.values(this.debouncedTimeouts).forEach((timeout) => {
613
+ clearTimeout(timeout);
614
+ });
615
+ this.debouncedTimeouts = {};
616
+ if (this.unsubscribeFromState) {
617
+ this.unsubscribeFromState();
618
+ this.unsubscribeFromState = null;
619
+ }
620
+ if (this.unsubscribeFromCoreStore) {
621
+ this.unsubscribeFromCoreStore();
622
+ this.unsubscribeFromCoreStore = null;
623
+ }
624
+ if (this.unsubscribeFromStartLoadingDocument) {
625
+ this.unsubscribeFromStartLoadingDocument();
626
+ this.unsubscribeFromStartLoadingDocument = null;
627
+ }
628
+ if (this.unsubscribeFromSetDocumentLoaded) {
629
+ this.unsubscribeFromSetDocumentLoaded();
630
+ this.unsubscribeFromSetDocumentLoaded = null;
631
+ }
632
+ if (this.unsubscribeFromCloseDocument) {
633
+ this.unsubscribeFromCloseDocument();
634
+ this.unsubscribeFromCloseDocument = null;
635
+ }
636
+ if (this.unsubscribeFromSetScale) {
637
+ this.unsubscribeFromSetScale();
638
+ this.unsubscribeFromSetScale = null;
639
+ }
640
+ if (this.unsubscribeFromSetRotation) {
641
+ this.unsubscribeFromSetRotation();
642
+ this.unsubscribeFromSetRotation = null;
643
+ }
644
+ }
645
+ /**
646
+ * Returns a promise that resolves when the plugin is ready
647
+ */
648
+ ready() {
649
+ return this.readyPromise;
650
+ }
651
+ /**
652
+ * Mark the plugin as ready
653
+ */
654
+ markReady() {
655
+ this.readyResolve();
656
+ }
657
+ /**
658
+ * Reset the ready state (useful for plugins that need to reinitialize)
659
+ */
660
+ resetReady() {
661
+ this.readyPromise = new Promise((resolve) => {
662
+ this.readyResolve = resolve;
663
+ });
664
+ }
665
+ /**
666
+ * Get the active document ID
667
+ * @throws Error if no active document exists
668
+ */
669
+ getActiveDocumentId() {
670
+ const id = this.coreState.core.activeDocumentId;
671
+ if (!id) {
672
+ throw new Error("No active document");
673
+ }
674
+ return id;
675
+ }
676
+ /**
677
+ * Get the active document ID or null if none exists
678
+ */
679
+ getActiveDocumentIdOrNull() {
680
+ return this.coreState.core.activeDocumentId;
681
+ }
682
+ /**
683
+ * Get core document state for a specific document
684
+ * @param documentId Document ID (optional, defaults to active document)
685
+ * @returns Document state or null if not found
686
+ */
687
+ getCoreDocument(documentId) {
688
+ const id = documentId ?? this.getActiveDocumentIdOrNull();
689
+ if (!id) return null;
690
+ return this.coreState.core.documents[id] ?? null;
691
+ }
692
+ /**
693
+ * Get core document state for a specific document
694
+ * @param documentId Document ID (optional, defaults to active document)
695
+ * @throws Error if document not found
696
+ */
697
+ getCoreDocumentOrThrow(documentId) {
698
+ const doc = this.getCoreDocument(documentId);
699
+ if (!doc) {
700
+ throw new Error(`Document not found: ${documentId ?? "active"}`);
701
+ }
702
+ return doc;
703
+ }
704
+ // ─────────────────────────────────────────────────────────
705
+ // Permission Helpers
706
+ // ─────────────────────────────────────────────────────────
707
+ /**
708
+ * Get the effective permission flags for a document.
709
+ * Applies layered resolution: per-document override → global override → PDF permission.
710
+ * Returns AllowAll if document not found.
711
+ * @param documentId Document ID (optional, defaults to active document)
712
+ */
713
+ getDocumentPermissions(documentId) {
714
+ const docId = documentId ?? this.coreState.core.activeDocumentId;
715
+ if (!docId) return PdfPermissionFlag.AllowAll;
716
+ return getEffectivePermissions(this.coreState.core, docId);
717
+ }
718
+ /**
719
+ * Check if a document has the required permissions (returns boolean).
720
+ * Applies layered resolution: per-document override → global override → PDF permission.
721
+ * Useful for conditional UI logic.
722
+ * @param documentId Document ID (optional, defaults to active document)
723
+ * @param flags Permission flags to check
724
+ */
725
+ checkPermission(documentId, ...flags) {
726
+ const docId = documentId ?? this.coreState.core.activeDocumentId;
727
+ if (!docId) return true;
728
+ return flags.every((flag) => getEffectivePermission(this.coreState.core, docId, flag));
729
+ }
730
+ /**
731
+ * Assert that a document has the required permissions.
732
+ * Applies layered resolution: per-document override → global override → PDF permission.
733
+ * Throws PermissionDeniedError if any flag is missing.
734
+ * @param documentId Document ID (optional, defaults to active document)
735
+ * @param flags Permission flags to require
736
+ */
737
+ requirePermission(documentId, ...flags) {
738
+ const docId = documentId ?? this.coreState.core.activeDocumentId;
739
+ if (!docId) return;
740
+ const missingFlags = [];
741
+ for (const flag of flags) {
742
+ if (!getEffectivePermission(this.coreState.core, docId, flag)) {
743
+ missingFlags.push(flag);
744
+ }
745
+ }
746
+ if (missingFlags.length > 0) {
747
+ const effectivePermissions = getEffectivePermissions(this.coreState.core, docId);
748
+ throw new PermissionDeniedError(missingFlags, effectivePermissions);
749
+ }
750
+ }
751
+ }
752
+ const SET_BACKEND = "AI_MANAGER/SET_BACKEND";
753
+ const SET_MODEL_LOADED = "AI_MANAGER/SET_MODEL_LOADED";
754
+ const SET_MODEL_UNLOADED = "AI_MANAGER/SET_MODEL_UNLOADED";
755
+ const SET_MODEL_LOADING = "AI_MANAGER/SET_MODEL_LOADING";
756
+ const SET_MODEL_LOADING_DONE = "AI_MANAGER/SET_MODEL_LOADING_DONE";
757
+ function setBackend(backend) {
758
+ return { type: SET_BACKEND, payload: backend };
759
+ }
760
+ function setModelLoaded(modelId) {
761
+ return { type: SET_MODEL_LOADED, payload: modelId };
762
+ }
763
+ function setModelUnloaded(modelId) {
764
+ return { type: SET_MODEL_UNLOADED, payload: modelId };
765
+ }
766
+ function setModelLoading(modelId) {
767
+ return { type: SET_MODEL_LOADING, payload: modelId };
768
+ }
769
+ function setModelLoadingDone(modelId) {
770
+ return { type: SET_MODEL_LOADING_DONE, payload: modelId };
771
+ }
772
+ const _AiManagerPlugin = class _AiManagerPlugin extends BasePlugin {
773
+ constructor(id, registry, config) {
774
+ super(id, registry);
775
+ this.config = config;
776
+ }
777
+ async initialize(config) {
778
+ this.runtime = config.runtime;
779
+ if (!this.runtime) {
780
+ throw new Error(
781
+ "[AiManagerPlugin] A runtime instance is required. Pass it via config: createPluginRegistration(AiManagerPluginPackage, { runtime: aiRuntime })"
782
+ );
783
+ }
784
+ }
785
+ buildCapability() {
786
+ return {
787
+ run: (pipeline, input) => {
788
+ const task = this.runtime.run(pipeline, input);
789
+ if (!this.runtime.isModelLoaded(pipeline.modelId)) {
790
+ this.dispatch(setModelLoading(pipeline.modelId));
791
+ task.wait(
792
+ () => {
793
+ this.dispatch(setModelLoaded(pipeline.modelId));
794
+ this.dispatch(setBackend(this.runtime.getBackend()));
795
+ },
796
+ () => {
797
+ this.dispatch(setModelLoadingDone(pipeline.modelId));
798
+ }
799
+ );
800
+ }
801
+ return task;
802
+ },
803
+ loadModel: (modelId) => {
804
+ this.dispatch(setModelLoading(modelId));
805
+ const task = this.runtime.loadModel(modelId);
806
+ task.wait(
807
+ () => {
808
+ this.dispatch(setModelLoaded(modelId));
809
+ this.dispatch(setBackend(this.runtime.getBackend()));
810
+ },
811
+ () => {
812
+ this.dispatch(setModelLoadingDone(modelId));
813
+ }
814
+ );
815
+ return task;
816
+ },
817
+ unloadModel: (modelId) => {
818
+ this.runtime.unloadModel(modelId);
819
+ this.dispatch(setModelUnloaded(modelId));
820
+ },
821
+ isModelLoaded: (modelId) => {
822
+ return this.runtime.isModelLoaded(modelId);
823
+ },
824
+ getBackend: () => {
825
+ return this.runtime.getBackend();
826
+ },
827
+ getRuntime: () => {
828
+ return this.runtime;
829
+ }
830
+ };
831
+ }
832
+ async destroy() {
833
+ await this.runtime.destroy();
834
+ }
835
+ };
836
+ _AiManagerPlugin.id = "ai-manager";
837
+ let AiManagerPlugin = _AiManagerPlugin;
838
+ const AI_MANAGER_PLUGIN_ID = "ai-manager";
839
+ const manifest = {
840
+ id: AI_MANAGER_PLUGIN_ID,
841
+ name: "AI Manager Plugin",
842
+ version: "1.0.0",
843
+ provides: ["ai-manager"],
844
+ requires: [],
845
+ optional: [],
846
+ defaultConfig: {
847
+ // runtime must be provided by the user
848
+ runtime: null
849
+ }
850
+ };
851
+ const initialState = {
852
+ backend: null,
853
+ loadedModels: [],
854
+ loadingModels: []
855
+ };
856
+ const reducer = (state = initialState, action) => {
857
+ switch (action.type) {
858
+ case SET_BACKEND:
859
+ return { ...state, backend: action.payload };
860
+ case SET_MODEL_LOADING:
861
+ return {
862
+ ...state,
863
+ loadingModels: state.loadingModels.includes(action.payload) ? state.loadingModels : [...state.loadingModels, action.payload]
864
+ };
865
+ case SET_MODEL_LOADING_DONE:
866
+ return {
867
+ ...state,
868
+ loadingModels: state.loadingModels.filter((id) => id !== action.payload)
869
+ };
870
+ case SET_MODEL_LOADED:
871
+ return {
872
+ ...state,
873
+ loadedModels: state.loadedModels.includes(action.payload) ? state.loadedModels : [...state.loadedModels, action.payload],
874
+ loadingModels: state.loadingModels.filter((id) => id !== action.payload)
875
+ };
876
+ case SET_MODEL_UNLOADED:
877
+ return {
878
+ ...state,
879
+ loadedModels: state.loadedModels.filter((id) => id !== action.payload)
880
+ };
881
+ default:
882
+ return state;
883
+ }
884
+ };
885
+ const AiManagerPluginPackage = {
886
+ manifest,
887
+ create: (registry, config) => new AiManagerPlugin(AI_MANAGER_PLUGIN_ID, registry, config),
888
+ reducer,
889
+ initialState
890
+ };
891
+ export {
892
+ AI_MANAGER_PLUGIN_ID,
893
+ AiManagerPlugin,
894
+ AiManagerPluginPackage,
895
+ manifest
896
+ };
897
+ //# sourceMappingURL=index.js.map