@libresign/pdf-elements 0.3.1 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (39) hide show
  1. package/README.md +24 -1
  2. package/dist/components/DraggableElement.vue.d.ts +240 -0
  3. package/dist/components/PDFElements.vue.d.ts +552 -0
  4. package/dist/components/PDFPage.vue.d.ts +40 -0
  5. package/dist/index.css +1 -0
  6. package/dist/index.d.ts +5 -0
  7. package/dist/index.mjs +1293 -0
  8. package/dist/pdf-uHvE5neP.mjs +17593 -0
  9. package/dist/pdf.worker.min-DmO9Xdfo.mjs +4 -0
  10. package/dist/types.d.ts +30 -0
  11. package/dist/utils/asyncReader.d.ts +10 -0
  12. package/dist/utils/geometry.d.ts +5 -0
  13. package/dist/utils/measurements.d.ts +12 -0
  14. package/dist/utils/objectStore.d.ts +5 -0
  15. package/dist/utils/pageBounds.d.ts +5 -0
  16. package/dist/utils/zoom.d.ts +2 -0
  17. package/package.json +46 -21
  18. package/src/components/DraggableElement.vue +18 -15
  19. package/src/components/PDFElements.vue +135 -60
  20. package/src/components/PDFPage.vue +11 -7
  21. package/src/index.ts +18 -0
  22. package/src/types.ts +35 -0
  23. package/src/utils/asyncReader.ts +103 -0
  24. package/src/utils/{geometry.js → geometry.ts} +16 -2
  25. package/src/utils/{measurements.js → measurements.ts} +20 -1
  26. package/src/utils/{objectStore.js → objectStore.ts} +15 -4
  27. package/src/utils/{pageBounds.js → pageBounds.ts} +2 -2
  28. package/src/utils/{zoom.js → zoom.ts} +3 -1
  29. package/dist/demo.html +0 -1
  30. package/dist/pdf-elements.common.js +0 -31698
  31. package/dist/pdf-elements.common.js.map +0 -1
  32. package/dist/pdf-elements.css +0 -1
  33. package/dist/pdf-elements.umd.js +0 -31710
  34. package/dist/pdf-elements.umd.js.map +0 -1
  35. package/dist/pdf-elements.umd.min.js +0 -2
  36. package/dist/pdf-elements.umd.min.js.map +0 -1
  37. package/dist/pdf.worker.min.mjs +0 -22
  38. package/src/index.js +0 -17
  39. package/src/utils/asyncReader.js +0 -44
package/dist/index.mjs ADDED
@@ -0,0 +1,1293 @@
1
+ import { defineComponent as I, openBlock as m, createElementBlock as v, normalizeStyle as E, renderSlot as M, createElementVNode as P, withModifiers as R, createCommentVNode as $, normalizeClass as Y, Fragment as z, renderList as B, markRaw as y, resolveComponent as Z, createVNode as G, createBlock as J, withCtx as N, toDisplayString as U } from "vue";
2
+ const Q = I({
3
+ name: "PDFPage",
4
+ emits: ["onMeasure"],
5
+ props: {
6
+ page: {
7
+ type: Object,
8
+ required: !0
9
+ },
10
+ scale: {
11
+ type: Number,
12
+ default: 1
13
+ }
14
+ },
15
+ data() {
16
+ return {
17
+ dynamicScale: this.scale,
18
+ isRendering: !1,
19
+ pendingRender: !1,
20
+ renderTask: null
21
+ };
22
+ },
23
+ watch: {
24
+ scale(e) {
25
+ this.dynamicScale = e, this.render();
26
+ }
27
+ },
28
+ mounted() {
29
+ this.render();
30
+ },
31
+ beforeUnmount() {
32
+ if (this.renderTask) {
33
+ try {
34
+ this.renderTask.cancel();
35
+ } catch {
36
+ }
37
+ this.renderTask = null;
38
+ }
39
+ },
40
+ methods: {
41
+ getCanvasMeasurement() {
42
+ return {
43
+ canvasWidth: this.$refs.canvas.width,
44
+ canvasHeight: this.$refs.canvas.height
45
+ };
46
+ },
47
+ measure() {
48
+ this.$emit("onMeasure", {
49
+ scale: this.dynamicScale
50
+ });
51
+ },
52
+ async render() {
53
+ if (this.isRendering) {
54
+ this.pendingRender = !0;
55
+ return;
56
+ }
57
+ this.isRendering = !0, this.pendingRender = !1;
58
+ try {
59
+ const e = await this.page, t = this.$refs.canvas;
60
+ if (!t) return;
61
+ if (this.renderTask) {
62
+ try {
63
+ this.renderTask.cancel();
64
+ } catch {
65
+ }
66
+ this.renderTask = null;
67
+ }
68
+ const i = t.getContext("2d");
69
+ if (!i) return;
70
+ const s = e.getViewport({
71
+ scale: this.dynamicScale
72
+ });
73
+ t.width = s.width, t.height = s.height, this.renderTask = e.render({
74
+ canvasContext: i,
75
+ viewport: s
76
+ }), await this.renderTask.promise, this.measure();
77
+ } finally {
78
+ this.isRendering = !1, this.renderTask = null, this.pendingRender && (this.pendingRender = !1, this.render());
79
+ }
80
+ }
81
+ }
82
+ }), V = (e, t) => {
83
+ const i = e.__vccOpts || e;
84
+ for (const [s, o] of t)
85
+ i[s] = o;
86
+ return i;
87
+ }, _ = { ref: "canvas" };
88
+ function ee(e, t, i, s, o, a) {
89
+ return m(), v("canvas", _, null, 512);
90
+ }
91
+ const te = /* @__PURE__ */ V(Q, [["render", ee], ["__scopeId", "data-v-2a60d679"]]), ie = I({
92
+ name: "DraggableElement",
93
+ props: {
94
+ object: {
95
+ type: Object,
96
+ required: !0
97
+ },
98
+ pagesScale: {
99
+ type: Number,
100
+ default: 1
101
+ },
102
+ pageWidth: {
103
+ type: Number,
104
+ required: !0
105
+ },
106
+ pageHeight: {
107
+ type: Number,
108
+ required: !0
109
+ },
110
+ onUpdate: {
111
+ type: Function,
112
+ default: () => {
113
+ }
114
+ },
115
+ onDelete: {
116
+ type: Function,
117
+ default: () => {
118
+ }
119
+ },
120
+ onDuplicate: {
121
+ type: Function,
122
+ default: () => {
123
+ }
124
+ },
125
+ onDragStart: {
126
+ type: Function,
127
+ default: () => {
128
+ }
129
+ },
130
+ onDragMove: {
131
+ type: Function,
132
+ default: () => {
133
+ }
134
+ },
135
+ onDragEnd: {
136
+ type: Function,
137
+ default: () => {
138
+ }
139
+ },
140
+ isBeingDraggedGlobally: {
141
+ type: Boolean,
142
+ default: !1
143
+ },
144
+ draggingClientPos: {
145
+ type: Object,
146
+ default: () => ({ x: 0, y: 0 })
147
+ },
148
+ currentDocIndex: {
149
+ type: Number,
150
+ default: -1
151
+ },
152
+ currentPageIndex: {
153
+ type: Number,
154
+ default: -1
155
+ },
156
+ globalDragDocIndex: {
157
+ type: Number,
158
+ default: -1
159
+ },
160
+ globalDragPageIndex: {
161
+ type: Number,
162
+ default: -1
163
+ },
164
+ showSelectionUi: {
165
+ type: Boolean,
166
+ default: !0
167
+ },
168
+ showDefaultActions: {
169
+ type: Boolean,
170
+ default: !0
171
+ },
172
+ readOnly: {
173
+ type: Boolean,
174
+ default: !1
175
+ },
176
+ ignoreClickOutsideSelectors: {
177
+ type: Array,
178
+ default: () => []
179
+ }
180
+ },
181
+ data() {
182
+ return {
183
+ isSelected: !1,
184
+ mode: "idle",
185
+ direction: "",
186
+ startX: 0,
187
+ startY: 0,
188
+ startLeft: 0,
189
+ startTop: 0,
190
+ startWidth: 0,
191
+ startHeight: 0,
192
+ offsetX: 0,
193
+ offsetY: 0,
194
+ resizeOffsetX: 0,
195
+ resizeOffsetY: 0,
196
+ resizeOffsetW: 0,
197
+ resizeOffsetH: 0,
198
+ aspectRatio: 1,
199
+ lastMouseX: 0,
200
+ lastMouseY: 0,
201
+ pointerOffsetDoc: { x: 0, y: 0 },
202
+ currentPageRect: null,
203
+ rafId: null
204
+ };
205
+ },
206
+ computed: {
207
+ resizeDirections() {
208
+ return ["top-left", "top-right", "bottom-left", "bottom-right"];
209
+ },
210
+ elementStyle() {
211
+ const e = this.pagesScale || 1, t = this.mode === "drag", i = this.mode === "resize", s = t ? this.offsetX : 0, o = t ? this.offsetY : 0, a = i ? this.resizeOffsetX : 0, n = i ? this.resizeOffsetY : 0, h = i ? this.resizeOffsetW : 0, r = i ? this.resizeOffsetH : 0, l = this.object.x + s + a, u = this.object.y + o + n, c = this.object.width + h, d = this.object.height + r;
212
+ return {
213
+ left: `${l * e}px`,
214
+ top: `${u * e}px`,
215
+ width: `${c * e}px`,
216
+ height: `${d * e}px`,
217
+ pointerEvents: this.readOnly ? "none" : "auto"
218
+ };
219
+ },
220
+ toolbarStyle() {
221
+ const e = this.pagesScale || 1, t = this.mode === "drag", i = this.mode === "resize", s = t ? this.offsetX : 0, o = t ? this.offsetY : 0, a = i ? this.resizeOffsetX : 0, n = i ? this.resizeOffsetY : 0, h = i ? this.resizeOffsetW : 0, r = this.object.x + s + a, l = this.object.y + o + n, u = this.object.width + h, d = l - 60, g = d < 0 ? l + 8 : d;
222
+ return {
223
+ left: `${(r + u / 2) * e}px`,
224
+ top: `${g * e}px`,
225
+ transform: "translateX(-50%)"
226
+ };
227
+ },
228
+ dragElementStyle() {
229
+ return !this.isBeingDraggedGlobally || !this.draggingClientPos ? {} : {
230
+ opacity: 0,
231
+ pointerEvents: "none"
232
+ };
233
+ }
234
+ },
235
+ mounted() {
236
+ this.handleClickOutside = this.handleClickOutside.bind(this), this.boundHandleMove = this.handleMove.bind(this), this.boundStopInteraction = this.stopInteraction.bind(this), document.addEventListener("mousedown", this.handleClickOutside), document.addEventListener("touchstart", this.handleClickOutside);
237
+ },
238
+ beforeUnmount() {
239
+ document.removeEventListener("mousedown", this.handleClickOutside), document.removeEventListener("touchstart", this.handleClickOutside), window.removeEventListener("mousemove", this.boundHandleMove), window.removeEventListener("mouseup", this.boundStopInteraction), window.removeEventListener("touchmove", this.boundHandleMove), window.removeEventListener("touchend", this.boundStopInteraction), this.rafId && cancelAnimationFrame(this.rafId);
240
+ },
241
+ methods: {
242
+ handleElementClick(e) {
243
+ this.readOnly || e.target.closest(".delete-handle") || e.target.closest("[data-stop-drag]") || e.target.closest(".actions-toolbar") || (e.preventDefault(), this.isSelected = !0, this.startDrag(e));
244
+ },
245
+ handleClickOutside(e) {
246
+ const t = Array.isArray(this.ignoreClickOutsideSelectors) ? this.ignoreClickOutsideSelectors : [];
247
+ for (const i of t)
248
+ if (i && e?.target?.closest?.(i))
249
+ return;
250
+ this.$el && !this.$el.contains(e.target) && (this.isSelected = !1);
251
+ },
252
+ startResizeFromSlot(e, t) {
253
+ !e || !t || this.startResize(e, t);
254
+ },
255
+ startDrag(e) {
256
+ if (this.readOnly || e.target.classList.contains("delete") || e.target.classList.contains("resize-handle")) return;
257
+ this.mode = "drag", this.startX = e.type.includes("touch") ? e.touches[0].clientX : e.clientX, this.startY = e.type.includes("touch") ? e.touches[0].clientY : e.clientY, this.startLeft = this.object.x, this.startTop = this.object.y, this.offsetX = 0, this.offsetY = 0, this.resetResizeOffsets();
258
+ const t = this.$el.querySelector(".draggable-element").getBoundingClientRect();
259
+ this.pointerOffsetDoc.x = this.startX - t.left, this.pointerOffsetDoc.y = this.startY - t.top;
260
+ const i = this.capturePageRect();
261
+ this.currentPageRect = i;
262
+ let s = { x: 0, y: 0 };
263
+ if (i) {
264
+ const o = i.left + this.object.x * this.pagesScale, a = i.top + this.object.y * this.pagesScale;
265
+ s = {
266
+ x: t.left - o,
267
+ y: t.top - a
268
+ };
269
+ }
270
+ this.onDragStart(this.startX, this.startY, { ...this.pointerOffsetDoc }, s), window.addEventListener("mousemove", this.boundHandleMove), window.addEventListener("mouseup", this.boundStopInteraction), window.addEventListener("touchmove", this.boundHandleMove), window.addEventListener("touchend", this.boundStopInteraction);
271
+ },
272
+ startResize(e, t) {
273
+ this.readOnly || (this.mode = "resize", this.direction = e, this.startX = t.type.includes("touch") ? t.touches[0].clientX : t.clientX, this.startY = t.type.includes("touch") ? t.touches[0].clientY : t.clientY, this.startLeft = this.object.x, this.startTop = this.object.y, this.startWidth = this.object.width, this.startHeight = this.object.height, this.aspectRatio = this.startWidth / this.startHeight, this.offsetX = 0, this.offsetY = 0, this.resetResizeOffsets(), window.addEventListener("mousemove", this.boundHandleMove), window.addEventListener("mouseup", this.boundStopInteraction), window.addEventListener("touchmove", this.boundHandleMove), window.addEventListener("touchend", this.boundStopInteraction));
274
+ },
275
+ handleMove(e) {
276
+ this.mode !== "idle" && (e.preventDefault(), !this.rafId && (this.rafId = requestAnimationFrame(() => {
277
+ const t = e.type.includes("touch") ? e.touches[0]?.clientX : e.clientX, i = e.type.includes("touch") ? e.touches[0]?.clientY : e.clientY;
278
+ if (t === void 0 || i === void 0) return;
279
+ this.lastMouseX = t, this.lastMouseY = i;
280
+ const s = (t - this.startX) / this.pagesScale, o = (i - this.startY) / this.pagesScale;
281
+ if (this.mode === "drag") {
282
+ const c = this.currentPageRect;
283
+ if (c) {
284
+ const d = t - this.pointerOffsetDoc.x, g = i - this.pointerOffsetDoc.y, f = (d - c.left) / this.pagesScale, b = (g - c.top) / this.pagesScale;
285
+ this.offsetX = f - this.object.x, this.offsetY = b - this.object.y;
286
+ } else
287
+ this.offsetX = s, this.offsetY = o;
288
+ this.onDragMove(t, i), this.isBeingDraggedGlobally && this.onUpdate({
289
+ _globalDrag: !0,
290
+ _mouseX: t,
291
+ _mouseY: i
292
+ }), this.rafId = null;
293
+ return;
294
+ }
295
+ const a = 16 / (this.pagesScale || 1);
296
+ let n = this.startWidth, h = this.startHeight, r = this.startLeft, l = this.startTop;
297
+ const u = this.direction.includes("right") ? s : this.direction.includes("left") ? -s : 0;
298
+ if (n = this.startWidth + u, n < a && (n = a), h = n / this.aspectRatio, this.direction.includes("left") && (r = this.startLeft + (this.startWidth - n), r < 0)) {
299
+ const c = -r;
300
+ r = 0, n = n - c, h = n / this.aspectRatio;
301
+ }
302
+ if (this.direction.includes("top") && (l = this.startTop + (this.startHeight - h), l < 0)) {
303
+ const c = -l;
304
+ l = 0, h = h - c, n = h * this.aspectRatio, this.direction.includes("left") && (r = this.startLeft + (this.startWidth - n));
305
+ }
306
+ if (r + n > this.pageWidth) {
307
+ const c = r + n - this.pageWidth;
308
+ n -= c, h = n / this.aspectRatio;
309
+ }
310
+ if (l + h > this.pageHeight) {
311
+ const c = l + h - this.pageHeight;
312
+ h -= c, n = h * this.aspectRatio, this.direction.includes("left") && (r = this.startLeft + (this.startWidth - n));
313
+ }
314
+ this.resizeOffsetX = r - this.object.x, this.resizeOffsetY = l - this.object.y, this.resizeOffsetW = n - this.object.width, this.resizeOffsetH = h - this.object.height, this.rafId = null;
315
+ })));
316
+ },
317
+ stopInteraction() {
318
+ if (this.mode !== "idle") {
319
+ if (this.mode === "drag" && (this.offsetX !== 0 || this.offsetY !== 0))
320
+ if (this.isBeingDraggedGlobally)
321
+ this.onUpdate({
322
+ _globalDrag: !0,
323
+ _mouseX: this.lastMouseX,
324
+ _mouseY: this.lastMouseY
325
+ });
326
+ else {
327
+ const e = Math.max(0, Math.min(this.object.x + this.offsetX, this.pageWidth - this.object.width)), t = Math.max(0, Math.min(this.object.y + this.offsetY, this.pageHeight - this.object.height));
328
+ this.onUpdate({ x: e, y: t });
329
+ }
330
+ if (this.mode === "resize" && (this.resizeOffsetW !== 0 || this.resizeOffsetH !== 0 || this.resizeOffsetX !== 0 || this.resizeOffsetY !== 0)) {
331
+ const e = this.object.x + this.resizeOffsetX, t = this.object.y + this.resizeOffsetY, i = this.object.width + this.resizeOffsetW, s = this.object.height + this.resizeOffsetH;
332
+ this.onUpdate({ x: e, y: t, width: i, height: s });
333
+ }
334
+ this.resetOffsets(), this.onDragEnd(), window.removeEventListener("mousemove", this.boundHandleMove), window.removeEventListener("mouseup", this.boundStopInteraction), window.removeEventListener("touchmove", this.boundHandleMove), window.removeEventListener("touchend", this.boundStopInteraction);
335
+ }
336
+ },
337
+ capturePageRect() {
338
+ const e = this.$el.closest(".page-wrapper");
339
+ if (!e) return null;
340
+ const t = e.querySelector("canvas");
341
+ return t ? t.getBoundingClientRect() : null;
342
+ },
343
+ resetResizeOffsets() {
344
+ this.resizeOffsetX = 0, this.resizeOffsetY = 0, this.resizeOffsetW = 0, this.resizeOffsetH = 0;
345
+ },
346
+ resetOffsets() {
347
+ this.mode = "idle", this.offsetX = 0, this.offsetY = 0, this.resetResizeOffsets(), this.pointerOffsetDoc = { x: 0, y: 0 }, this.currentPageRect = null;
348
+ }
349
+ }
350
+ }), se = { class: "draggable-wrapper" }, ne = ["onMousedown", "onTouchstart"];
351
+ function oe(e, t, i, s, o, a) {
352
+ return m(), v("div", se, [
353
+ e.isSelected && !e.isBeingDraggedGlobally && e.showSelectionUi && e.showDefaultActions ? (m(), v("div", {
354
+ key: 0,
355
+ class: "actions-toolbar",
356
+ style: E(e.toolbarStyle)
357
+ }, [
358
+ M(e.$slots, "actions", {
359
+ object: e.object,
360
+ onDelete: e.onDelete,
361
+ onDuplicate: e.onDuplicate
362
+ }, () => [
363
+ P("button", {
364
+ class: "action-btn",
365
+ type: "button",
366
+ title: "Duplicate",
367
+ onClick: t[0] || (t[0] = R((...n) => e.onDuplicate && e.onDuplicate(...n), ["stop"]))
368
+ }, [...t[5] || (t[5] = [
369
+ P("svg", {
370
+ width: "16",
371
+ height: "16",
372
+ viewBox: "0 0 24 24",
373
+ fill: "currentColor"
374
+ }, [
375
+ P("path", { d: "M16 1H6a2 2 0 0 0-2 2v12h2V3h10V1zm3 4H10a2 2 0 0 0-2 2v14a2 2 0 0 0 2 2h9a2 2 0 0 0 2-2V7a2 2 0 0 0-2-2zm0 16H10V7h9v14z" })
376
+ ], -1)
377
+ ])]),
378
+ P("button", {
379
+ class: "action-btn",
380
+ type: "button",
381
+ title: "Delete",
382
+ onClick: t[1] || (t[1] = R((...n) => e.onDelete && e.onDelete(...n), ["stop"]))
383
+ }, [...t[6] || (t[6] = [
384
+ P("svg", {
385
+ width: "16",
386
+ height: "16",
387
+ viewBox: "0 0 16 16",
388
+ fill: "currentColor"
389
+ }, [
390
+ P("path", { d: "M6.5 1h3a.5.5 0 0 1 .5.5v1H6v-1a.5.5 0 0 1 .5-.5ZM11 2.5v-1A1.5 1.5 0 0 0 9.5 0h-3A1.5 1.5 0 0 0 5 1.5v1H2.5a.5.5 0 0 0 0 1h.5v10.5A1.5 1.5 0 0 0 4.5 15h7a1.5 1.5 0 0 0 1.5-1.5V3.5h.5a.5.5 0 0 0 0-1H11Zm1 1v10.5a.5.5 0 0 1-.5.5h-7a.5.5 0 0 1-.5-.5V3.5h8Z" })
391
+ ], -1)
392
+ ])])
393
+ ], !0)
394
+ ], 4)) : $("", !0),
395
+ P("div", {
396
+ class: Y(["draggable-element", { selected: e.isSelected && e.showSelectionUi }]),
397
+ draggable: "false",
398
+ onDragstart: t[2] || (t[2] = R(() => {
399
+ }, ["prevent"])),
400
+ style: E([e.elementStyle, e.dragElementStyle]),
401
+ onMousedown: t[3] || (t[3] = (...n) => e.handleElementClick && e.handleElementClick(...n)),
402
+ onTouchstart: t[4] || (t[4] = (...n) => e.handleElementClick && e.handleElementClick(...n))
403
+ }, [
404
+ M(e.$slots, "default", {
405
+ object: e.object,
406
+ isSelected: e.isSelected,
407
+ onDelete: e.onDelete,
408
+ onResize: e.startResizeFromSlot
409
+ }, void 0, !0),
410
+ e.isSelected && e.showSelectionUi ? (m(!0), v(z, { key: 0 }, B(e.resizeDirections, (n) => (m(), v("button", {
411
+ key: n,
412
+ class: Y(["resize-handle", `handle-${n}`]),
413
+ type: "button",
414
+ onMousedown: R((h) => e.startResize(n, h), ["stop", "prevent"]),
415
+ onTouchstart: R((h) => e.startResize(n, h), ["stop", "prevent"])
416
+ }, null, 42, ne))), 128)) : $("", !0)
417
+ ], 38)
418
+ ]);
419
+ }
420
+ const ae = /* @__PURE__ */ V(ie, [["render", oe], ["__scopeId", "data-v-c700849e"]]);
421
+ let H = null, A = null, W = null, X = null;
422
+ function re() {
423
+ return A || (A = import("./pdf-uHvE5neP.mjs")), A;
424
+ }
425
+ function he() {
426
+ return W || (W = import("./pdf.worker.min-DmO9Xdfo.mjs").then(
427
+ (e) => e.default
428
+ )), W;
429
+ }
430
+ async function le(e) {
431
+ if (X) {
432
+ e.GlobalWorkerOptions.workerSrc = X;
433
+ return;
434
+ }
435
+ e.GlobalWorkerOptions.workerSrc || (e.GlobalWorkerOptions.workerSrc = await he());
436
+ }
437
+ async function ce(e) {
438
+ return H || (await le(e), H = new e.PDFWorker({})), H;
439
+ }
440
+ function je(e) {
441
+ X = e, A && A.then((t) => {
442
+ t.GlobalWorkerOptions.workerSrc = e;
443
+ });
444
+ }
445
+ function K(e) {
446
+ return new Promise((t, i) => {
447
+ const s = new FileReader();
448
+ s.onload = () => t(s.result), s.onerror = i, s.readAsArrayBuffer(e);
449
+ });
450
+ }
451
+ async function x(e, t = {}) {
452
+ const i = await re(), s = await ce(i), o = e instanceof ArrayBuffer, a = ArrayBuffer.isView(e), n = typeof Blob < "u" && e instanceof Blob;
453
+ if (e && typeof e == "object" && !o && !a && !n)
454
+ return i.getDocument({ ...e, ...t, worker: s }).promise;
455
+ if (typeof e == "string")
456
+ return i.getDocument({ url: e, ...t, worker: s }).promise;
457
+ if (n) {
458
+ const r = await K(e);
459
+ return i.getDocument({ data: r, ...t, worker: s }).promise;
460
+ }
461
+ const h = o ? e : new Uint8Array(
462
+ e.buffer,
463
+ e.byteOffset,
464
+ e.byteLength
465
+ );
466
+ return i.getDocument({ data: h, ...t, worker: s }).promise;
467
+ }
468
+ function F(e, t, i, s, o, a) {
469
+ return {
470
+ x: Math.max(0, Math.min(e, o - i)),
471
+ y: Math.max(0, Math.min(t, a - s))
472
+ };
473
+ }
474
+ function de(e, t, i, s, o, a) {
475
+ const n = Math.max(0, e), h = Math.max(0, t), r = Math.min(o, e + i), l = Math.min(a, t + s);
476
+ return r <= n || l <= h ? 0 : (r - n) * (l - h);
477
+ }
478
+ function ge(e, t, i = 300) {
479
+ return {
480
+ minY: Math.max(0, e - i),
481
+ maxY: e + t + i
482
+ };
483
+ }
484
+ function ue(e, t, i, s) {
485
+ return !(e + t < i || e > s);
486
+ }
487
+ function q(e, t) {
488
+ e.forEach((i) => {
489
+ i.pagesScale = i.pagesScale.map(() => t);
490
+ });
491
+ }
492
+ function fe(e, t) {
493
+ return !e || !t ? !1 : e.allObjects.some((i) => i.some((s) => s.id === t));
494
+ }
495
+ function L(e, t) {
496
+ if (!(!e || !t)) {
497
+ for (let i = 0; i < e.allObjects.length; i++)
498
+ if (e.allObjects[i].some((s) => s.id === t))
499
+ return i;
500
+ }
501
+ }
502
+ function pe(e, t, i, s) {
503
+ const o = e?.allObjects?.[t];
504
+ if (!o) return !1;
505
+ const a = o.findIndex((n) => n.id === i);
506
+ return a === -1 ? !1 : (o.splice(a, 1, { ...o[a], ...s }), !0);
507
+ }
508
+ function me(e, t, i) {
509
+ const s = e?.allObjects?.[t];
510
+ if (!s) return !1;
511
+ const o = s.findIndex((a) => a.id === i);
512
+ return o === -1 ? !1 : (s.splice(o, 1), !0);
513
+ }
514
+ function ve(e, t, i, s) {
515
+ const o = e[t];
516
+ if (o)
517
+ return o;
518
+ const a = i.getCanvasMeasurement(), n = {
519
+ width: a.canvasWidth / s,
520
+ height: a.canvasHeight / s
521
+ };
522
+ return e[t] = n, n;
523
+ }
524
+ const we = I({
525
+ name: "PDFElements",
526
+ emits: ["pdf-elements:end-init", "pdf-elements:delete-object", "pdf-elements:object-click"],
527
+ components: {
528
+ PDFPage: te,
529
+ DraggableElement: ae
530
+ },
531
+ props: {
532
+ width: {
533
+ type: String,
534
+ default: "100%"
535
+ },
536
+ height: {
537
+ type: String,
538
+ default: "100%"
539
+ },
540
+ initFiles: {
541
+ type: Array,
542
+ default: () => []
543
+ },
544
+ initFileNames: {
545
+ type: Array,
546
+ default: () => []
547
+ },
548
+ initialScale: {
549
+ type: Number,
550
+ default: 1
551
+ },
552
+ showPageFooter: {
553
+ type: Boolean,
554
+ default: !0
555
+ },
556
+ hideSelectionUI: {
557
+ type: Boolean,
558
+ default: !1
559
+ },
560
+ showSelectionHandles: {
561
+ type: Boolean,
562
+ default: !0
563
+ },
564
+ showElementActions: {
565
+ type: Boolean,
566
+ default: !0
567
+ },
568
+ readOnly: {
569
+ type: Boolean,
570
+ default: !1
571
+ },
572
+ emitObjectClick: {
573
+ type: Boolean,
574
+ default: !1
575
+ },
576
+ ignoreClickOutsideSelectors: {
577
+ type: Array,
578
+ default: () => []
579
+ },
580
+ pageCountFormat: {
581
+ type: String,
582
+ default: "{currentPage} of {totalPages}"
583
+ },
584
+ autoFitZoom: {
585
+ type: Boolean,
586
+ default: !1
587
+ },
588
+ pdfjsOptions: {
589
+ type: Object,
590
+ default: () => ({})
591
+ }
592
+ },
593
+ data() {
594
+ return {
595
+ scale: this.initialScale,
596
+ pdfDocuments: [],
597
+ selectedDocIndex: -1,
598
+ selectedPageIndex: -1,
599
+ isAddingMode: !1,
600
+ previewElement: null,
601
+ previewPosition: { x: 0, y: 0 },
602
+ previewScale: { x: 1, y: 1 },
603
+ previewPageDocIndex: -1,
604
+ previewPageIndex: -1,
605
+ previewVisible: !1,
606
+ hoverRafId: 0,
607
+ pendingHoverClientPos: null,
608
+ lastHoverRect: null,
609
+ addingListenersAttached: !1,
610
+ dragRafId: 0,
611
+ pendingDragClientPos: null,
612
+ pageBoundsVersion: 0,
613
+ lastScrollTop: 0,
614
+ lastClientWidth: 0,
615
+ nextObjectCounter: 0,
616
+ isDraggingElement: !1,
617
+ draggingObject: null,
618
+ draggingDocIndex: -1,
619
+ draggingPageIndex: -1,
620
+ draggingClientPosition: { x: 0, y: 0 },
621
+ draggingScale: 1,
622
+ draggingInitialMouseOffset: { x: 0, y: 0 },
623
+ draggingElementShift: { x: 0, y: 0 },
624
+ lastMouseClientPos: { x: 0, y: 0 },
625
+ viewportRafId: 0,
626
+ objectIndexCache: y({}),
627
+ zoomRafId: null,
628
+ wheelZoomRafId: null,
629
+ boundHandleWheel: null,
630
+ visualScale: this.initialScale,
631
+ autoFitApplied: !1,
632
+ lastContainerWidth: 0,
633
+ _pagesBoundingRects: y({}),
634
+ _pagesBoundingRectsList: y([]),
635
+ _pageMeasurementCache: y({}),
636
+ _lastPageBoundsScrollTop: 0,
637
+ _lastPageBoundsClientHeight: 0
638
+ };
639
+ },
640
+ mounted() {
641
+ this.boundHandleWheel = this.handleWheel.bind(this), this.init(), window.addEventListener("scroll", this.onViewportScroll, { passive: !0 }), window.addEventListener("resize", this.onViewportScroll), this.$el?.addEventListener("scroll", this.onViewportScroll, { passive: !0 }), this.$el?.addEventListener("wheel", this.boundHandleWheel, { passive: !1 });
642
+ },
643
+ beforeUnmount() {
644
+ this.zoomRafId && cancelAnimationFrame(this.zoomRafId), this.wheelZoomRafId && (cancelAnimationFrame(this.wheelZoomRafId), this.wheelZoomRafId = null), this.boundHandleWheel && this.$el?.removeEventListener("wheel", this.boundHandleWheel), this.detachAddingListeners(), window.removeEventListener("scroll", this.onViewportScroll), window.removeEventListener("resize", this.onViewportScroll), this.$el?.removeEventListener("scroll", this.onViewportScroll), this.viewportRafId && (window.cancelAnimationFrame(this.viewportRafId), this.viewportRafId = 0), this.hoverRafId && (window.cancelAnimationFrame(this.hoverRafId), this.hoverRafId = 0), this.dragRafId && (window.cancelAnimationFrame(this.dragRafId), this.dragRafId = 0);
645
+ },
646
+ methods: {
647
+ async init() {
648
+ if (!this.initFiles || this.initFiles.length === 0) return;
649
+ const e = [];
650
+ this.autoFitApplied = !1;
651
+ for (let t = 0; t < this.initFiles.length; t++) {
652
+ const i = this.initFiles[t], s = this.initFileNames[t] || `document-${t + 1}.pdf`;
653
+ let o;
654
+ if (i instanceof Blob) {
655
+ const r = await K(i);
656
+ o = await x({ data: r }, this.pdfjsOptions);
657
+ } else
658
+ o = await x(i, this.pdfjsOptions);
659
+ const a = [], n = Array(o.numPages).fill(0);
660
+ for (let r = 1; r <= o.numPages; r++) {
661
+ const l = o.getPage(r);
662
+ l.then((u) => {
663
+ n.splice(r - 1, 1, u.getViewport({ scale: 1 }).width), this.autoFitZoom && this.scheduleAutoFitZoom();
664
+ }), a.push(l);
665
+ }
666
+ const h = y(a);
667
+ e.push({
668
+ name: s,
669
+ file: i,
670
+ pdfDoc: y(o),
671
+ numPages: o.numPages,
672
+ pages: h,
673
+ pageWidths: n,
674
+ pagesScale: Array(o.numPages).fill(this.scale),
675
+ allObjects: Array(o.numPages).fill(0).map(() => [])
676
+ });
677
+ }
678
+ this.pdfDocuments = e, this._pageMeasurementCache = y({}), e.length && (this.selectedDocIndex = 0, this.selectedPageIndex = 0, this.$emit("pdf-elements:end-init", { docsCount: e.length }), this.$nextTick(() => {
679
+ this.autoFitZoom && this.scheduleAutoFitZoom();
680
+ }));
681
+ },
682
+ selectPage(e, t) {
683
+ this.selectedDocIndex = e, this.selectedPageIndex = t;
684
+ },
685
+ startDraggingElement(e, t, i, s, o, a, n) {
686
+ this.isDraggingElement = !0, this.draggingObject = { ...i }, this.draggingDocIndex = e, this.draggingPageIndex = t, this.draggingScale = this.getDisplayedPageScale(e, t), this.lastMouseClientPos.x = s, this.lastMouseClientPos.y = o, this.draggingElementShift = n && typeof n.x == "number" && typeof n.y == "number" ? n : { x: 0, y: 0 }, this.cachePageBounds();
687
+ const h = this.getPageRect(e, t);
688
+ if (a && typeof a.x == "number" && typeof a.y == "number")
689
+ this.draggingInitialMouseOffset.x = a.x, this.draggingInitialMouseOffset.y = a.y, this.draggingClientPosition.x = s - this.draggingInitialMouseOffset.x, this.draggingClientPosition.y = o - this.draggingInitialMouseOffset.y;
690
+ else if (h) {
691
+ const r = h.left + i.x * this.draggingScale, l = h.top + i.y * this.draggingScale;
692
+ this.draggingInitialMouseOffset.x = s - r, this.draggingInitialMouseOffset.y = o - l, this.draggingClientPosition.x = s - this.draggingInitialMouseOffset.x, this.draggingClientPosition.y = o - this.draggingInitialMouseOffset.y;
693
+ }
694
+ },
695
+ updateDraggingPosition(e, t) {
696
+ this.isDraggingElement && (this.pendingDragClientPos ? (this.pendingDragClientPos.x = e, this.pendingDragClientPos.y = t) : this.pendingDragClientPos = { x: e, y: t }, !this.dragRafId && (this.dragRafId = window.requestAnimationFrame(() => {
697
+ this.dragRafId = 0;
698
+ const i = this.pendingDragClientPos;
699
+ i && (this.lastMouseClientPos.x = i.x, this.lastMouseClientPos.y = i.y, this.draggingClientPosition.x = i.x - this.draggingInitialMouseOffset.x, this.draggingClientPosition.y = i.y - this.draggingInitialMouseOffset.y);
700
+ })));
701
+ },
702
+ stopDraggingElement() {
703
+ if (this.draggingObject) {
704
+ const e = this.draggingObject.id, t = this.draggingDocIndex, i = this.checkAndMoveObjectPage(
705
+ this.draggingDocIndex,
706
+ e,
707
+ this.lastMouseClientPos.x,
708
+ this.lastMouseClientPos.y
709
+ );
710
+ i !== void 0 && this.$nextTick(() => {
711
+ this.selectPage(t, i);
712
+ const s = `draggable${t}-${i}-${e}`, o = this.$refs[s];
713
+ o && Array.isArray(o) && o[0] && (o[0].isSelected = !0);
714
+ });
715
+ }
716
+ this.isDraggingElement = !1, this.draggingObject = null, this.draggingDocIndex = -1, this.draggingPageIndex = -1, this.draggingElementShift = { x: 0, y: 0 }, this.pendingDragClientPos = null;
717
+ },
718
+ startAddingElement(e) {
719
+ this.pdfDocuments.length && (this.attachAddingListeners(), this.isAddingMode = !0, this.previewElement = { ...e }, this.previewPageDocIndex = 0, this.previewPageIndex = 0, this.previewVisible = !1, this.previewScale = { x: 1, y: 1 }, this.cachePageBounds());
720
+ },
721
+ cachePageBounds() {
722
+ const e = {}, t = [], i = this.$el, s = i?.scrollTop || 0, o = i?.clientHeight || 0;
723
+ if (!this.isAddingMode && !this.isDraggingElement && s === this._lastPageBoundsScrollTop && o === this._lastPageBoundsClientHeight)
724
+ return;
725
+ this._lastPageBoundsScrollTop = s, this._lastPageBoundsClientHeight = o;
726
+ const { minY: a, maxY: n } = ge(s, o);
727
+ for (let h = 0; h < this.pdfDocuments.length; h++)
728
+ for (let r = 0; r < this.pdfDocuments[h].pages.length; r++) {
729
+ const l = this.getPageCanvasElement(h, r);
730
+ if (!l) continue;
731
+ if (o) {
732
+ const d = l.closest(".page-wrapper") || l, g = d.offsetTop || 0, f = d.offsetHeight || 0;
733
+ if (!ue(g, f, a, n))
734
+ continue;
735
+ }
736
+ const u = l.getBoundingClientRect(), c = {
737
+ docIndex: h,
738
+ pageIndex: r,
739
+ rect: u
740
+ };
741
+ e[`${h}-${r}`] = c, t.push(c);
742
+ }
743
+ this._pagesBoundingRects = y(e), this._pagesBoundingRectsList = y(t), this.pageBoundsVersion++;
744
+ },
745
+ cachePageBoundsForPage(e, t) {
746
+ const i = this.getPageCanvasElement(e, t);
747
+ if (!i) return;
748
+ const s = i.getBoundingClientRect(), o = {
749
+ ...this._pagesBoundingRects,
750
+ [`${e}-${t}`]: {
751
+ docIndex: e,
752
+ pageIndex: t,
753
+ rect: s
754
+ }
755
+ };
756
+ this._pagesBoundingRects = y(o), this._pagesBoundingRectsList = y(Object.values(o)), this.pageBoundsVersion++;
757
+ },
758
+ getPageBoundsMap() {
759
+ return this._pagesBoundingRects || {};
760
+ },
761
+ getPageBoundsList() {
762
+ return this._pagesBoundingRectsList || [];
763
+ },
764
+ getPageRect(e, t) {
765
+ return this.getPageBoundsMap()[`${e}-${t}`]?.rect || null;
766
+ },
767
+ getPointerPosition(e) {
768
+ return e?.type?.includes?.("touch") ? {
769
+ x: e.touches?.[0]?.clientX,
770
+ y: e.touches?.[0]?.clientY
771
+ } : {
772
+ x: e?.clientX,
773
+ y: e?.clientY
774
+ };
775
+ },
776
+ getDisplayedPageScale(e, t) {
777
+ this.pageBoundsVersion;
778
+ const i = this.pdfDocuments[e];
779
+ if (!i) return 1;
780
+ const s = i.pageWidths?.[t] || 0;
781
+ this.getPageBoundsMap()[`${e}-${t}`] || this.cachePageBoundsForPage(e, t);
782
+ const a = this.getPageBoundsMap()[`${e}-${t}`]?.rect?.width || 0;
783
+ if (a && s)
784
+ return a / s;
785
+ if (this.isAddingMode || this.isDraggingElement) {
786
+ const l = this.getPageCanvasElement(e, t)?.getBoundingClientRect?.().width || 0;
787
+ if (l && s)
788
+ return l / s;
789
+ }
790
+ const n = i.pagesScale[t] || 1, h = this.visualScale && this.scale ? this.visualScale / this.scale : 1;
791
+ return n * h;
792
+ },
793
+ getPageComponent(e, t) {
794
+ const i = this.$refs[`page${e}-${t}`];
795
+ return i && Array.isArray(i) && i[0] ? i[0] : null;
796
+ },
797
+ getPageCanvasElement(e, t) {
798
+ const i = this.getPageComponent(e, t);
799
+ return i ? i.$el || i : null;
800
+ },
801
+ onViewportScroll() {
802
+ this.viewportRafId || (this.viewportRafId = window.requestAnimationFrame(() => {
803
+ const e = this.$el, t = e?.scrollTop || 0, i = e?.clientWidth || 0, s = t !== this.lastScrollTop, o = i !== this.lastClientWidth;
804
+ this.lastScrollTop = t, this.lastClientWidth = i, (this.isAddingMode || this.isDraggingElement) && (s || o) && this.cachePageBounds(), this.autoFitZoom && !this.isAddingMode && !this.isDraggingElement && i && o && (this.lastContainerWidth = i, this.autoFitApplied = !1, this.scheduleAutoFitZoom()), this.viewportRafId = 0;
805
+ }));
806
+ },
807
+ handleMouseMove(e) {
808
+ if (!this.isAddingMode || !this.previewElement) return;
809
+ const { x: t, y: i } = this.getPointerPosition(e);
810
+ t === void 0 || i === void 0 || (this.pendingHoverClientPos ? (this.pendingHoverClientPos.x = t, this.pendingHoverClientPos.y = i) : this.pendingHoverClientPos = { x: t, y: i }, !this.hoverRafId && (this.hoverRafId = window.requestAnimationFrame(() => {
811
+ this.hoverRafId = 0;
812
+ const s = this.pendingHoverClientPos;
813
+ if (!s) return;
814
+ const o = s.x, a = s.y;
815
+ let n = null;
816
+ if (this.lastHoverRect && o >= this.lastHoverRect.left && o <= this.lastHoverRect.right && a >= this.lastHoverRect.top && a <= this.lastHoverRect.bottom)
817
+ n = {
818
+ docIndex: this.previewPageDocIndex,
819
+ pageIndex: this.previewPageIndex,
820
+ rect: this.lastHoverRect
821
+ };
822
+ else {
823
+ const S = this.getPageBoundsList().length ? this.getPageBoundsList() : Object.values(this.getPageBoundsMap());
824
+ for (let w = 0; w < S.length; w++) {
825
+ const j = S[w], C = j.rect;
826
+ if (o >= C.left && o <= C.right && a >= C.top && a <= C.bottom) {
827
+ n = j;
828
+ break;
829
+ }
830
+ }
831
+ }
832
+ if (!n) {
833
+ this.previewVisible = !1, this.previewScale = { x: 1, y: 1 }, this.lastHoverRect = null;
834
+ return;
835
+ }
836
+ this.previewPageDocIndex = n.docIndex, this.previewPageIndex = n.pageIndex, this.lastHoverRect = n.rect;
837
+ const h = this.getPageCanvasElement(n.docIndex, n.pageIndex), r = this.pdfDocuments[n.docIndex]?.pagesScale?.[n.pageIndex] || 1, l = h?.width || n.rect.width, u = h?.height || n.rect.height, c = l ? n.rect.width / l : 1, d = u ? n.rect.height / u : 1, g = (o - n.rect.left) / c / r, f = (a - n.rect.top) / d / r, b = l / r, D = u / r;
838
+ this.previewScale.x = r, this.previewScale.y = r;
839
+ let p = g - this.previewElement.width / 2, O = f - this.previewElement.height / 2;
840
+ p = Math.max(0, Math.min(p, b - this.previewElement.width)), O = Math.max(0, Math.min(O, D - this.previewElement.height)), this.previewPosition.x = p, this.previewPosition.y = O, this.previewVisible = !0;
841
+ })));
842
+ },
843
+ handleOverlayClick(e, t, i) {
844
+ if (!this.emitObjectClick) return;
845
+ const { x: s, y: o } = this.getPointerPosition(i);
846
+ if (!Number.isFinite(s) || !Number.isFinite(o)) return;
847
+ this.cachePageBoundsForPage(e, t);
848
+ const a = this.getPageRect(e, t);
849
+ if (!a) return;
850
+ const n = this.getDisplayedPageScale(e, t) || 1, h = (s - a.left) / n, r = (o - a.top) / n, u = this.pdfDocuments?.[e]?.allObjects?.[t] || [];
851
+ if (u.length === 0) return;
852
+ let c = null;
853
+ for (let d = u.length - 1; d >= 0; d--) {
854
+ const g = u[d], f = Number(g.x), b = Number(g.y), D = Number(g.width), p = Number(g.height);
855
+ if ([f, b, D, p].every(Number.isFinite) && h >= f && h <= f + D && r >= b && r <= b + p) {
856
+ c = g;
857
+ break;
858
+ }
859
+ }
860
+ c && this.$emit("pdf-elements:object-click", {
861
+ docIndex: e,
862
+ pageIndex: t,
863
+ object: c,
864
+ event: i
865
+ });
866
+ },
867
+ handleKeyDown(e) {
868
+ e.key === "Escape" && this.isAddingMode && this.cancelAdding();
869
+ },
870
+ handleWheel(e) {
871
+ if (!e.ctrlKey) return;
872
+ e.preventDefault();
873
+ const t = 1 - e.deltaY * 2e-3, i = Math.max(0.5, Math.min(3, this.visualScale * t));
874
+ this.visualScale = i, !this.wheelZoomRafId && (this.wheelZoomRafId = window.requestAnimationFrame(() => {
875
+ this.wheelZoomRafId = null, this.commitZoom();
876
+ }));
877
+ },
878
+ commitZoom() {
879
+ const e = this.visualScale;
880
+ this.scale = e, q(this.pdfDocuments, this.scale), this._pageMeasurementCache = y({}), this.cachePageBounds();
881
+ },
882
+ finishAdding() {
883
+ if (!this.isAddingMode || !this.previewElement || !this.previewVisible) return;
884
+ const e = {
885
+ ...this.previewElement,
886
+ id: this.generateObjectId(),
887
+ x: Math.round(this.previewPosition.x),
888
+ y: Math.round(this.previewPosition.y)
889
+ }, t = this.pdfDocuments[this.previewPageDocIndex], i = this.getPageWidth(this.previewPageDocIndex, this.previewPageIndex), s = this.getPageHeight(this.previewPageDocIndex, this.previewPageIndex);
890
+ if (e.x < 0 || e.y < 0 || e.x + e.width > i || e.y + e.height > s) {
891
+ this.cancelAdding();
892
+ return;
893
+ }
894
+ t.allObjects[this.previewPageIndex].push(e);
895
+ const o = this.previewPageIndex, a = this.previewPageDocIndex, n = e.id;
896
+ this.cancelAdding(), this.$nextTick(() => {
897
+ const h = `draggable${a}-${o}-${n}`, r = this.$refs[h];
898
+ r && Array.isArray(r) && r[0] && (r[0].isSelected = !0);
899
+ });
900
+ },
901
+ cancelAdding() {
902
+ this.isAddingMode = !1, this.previewElement = null, this.previewVisible = !1, this.detachAddingListeners();
903
+ },
904
+ generateObjectId() {
905
+ const e = this.nextObjectCounter++, t = Math.random().toString(36).slice(2, 8);
906
+ return `obj-${Date.now()}-${e}-${t}`;
907
+ },
908
+ attachAddingListeners() {
909
+ this.addingListenersAttached || (this.addingListenersAttached = !0, document.addEventListener("mousemove", this.handleMouseMove), document.addEventListener("touchmove", this.handleMouseMove, { passive: !0 }), document.addEventListener("mouseup", this.finishAdding), document.addEventListener("touchend", this.finishAdding), document.addEventListener("keydown", this.handleKeyDown));
910
+ },
911
+ detachAddingListeners() {
912
+ this.addingListenersAttached && (this.addingListenersAttached = !1, document.removeEventListener("mousemove", this.handleMouseMove), document.removeEventListener("touchmove", this.handleMouseMove), document.removeEventListener("mouseup", this.finishAdding), document.removeEventListener("touchend", this.finishAdding), document.removeEventListener("keydown", this.handleKeyDown));
913
+ },
914
+ addObjectToPage(e, t = this.selectedPageIndex, i = this.selectedDocIndex) {
915
+ if (i < 0 || i >= this.pdfDocuments.length || t < 0 || t >= this.pdfDocuments[i].pages.length) return !1;
916
+ const s = this.pdfDocuments[i], o = this.getPageComponent(i, t);
917
+ if (!o) return !1;
918
+ let a = e;
919
+ (!a.id || this.objectIdExists(i, a.id)) && (a = { ...a, id: this.generateObjectId() });
920
+ const n = this.getCachedMeasurement(i, t, o), h = n.width, r = n.height;
921
+ return a.x < 0 || a.y < 0 || a.x + a.width > h || a.y + a.height > r ? !1 : (s.allObjects[t].push(a), this.objectIndexCache[`${i}-${a.id}`] = t, !0);
922
+ },
923
+ objectIdExists(e, t) {
924
+ if (!t) return !1;
925
+ const i = `${e}-${t}`;
926
+ if (this.objectIndexCache[i] !== void 0) return !0;
927
+ const s = this.pdfDocuments[e];
928
+ return fe(s, t);
929
+ },
930
+ updateObjectInPage(e, t, i, s) {
931
+ const o = this.pdfDocuments[e];
932
+ pe(o, t, i, s);
933
+ },
934
+ removeObjectFromPage(e, t, i) {
935
+ const s = this.pdfDocuments[e];
936
+ me(s, t, i);
937
+ },
938
+ getAllObjects(e = this.selectedDocIndex) {
939
+ if (e < 0 || e >= this.pdfDocuments.length) return [];
940
+ const t = this.pdfDocuments[e], i = [];
941
+ return t.allObjects.forEach((s, o) => {
942
+ const a = this.getPageComponent(e, o);
943
+ if (!a) return;
944
+ const h = this.getCachedMeasurement(e, o, a).height, r = t.pagesScale[o] || 1;
945
+ s.forEach((l) => {
946
+ i.push({
947
+ ...l,
948
+ pageIndex: o,
949
+ pageNumber: o + 1,
950
+ scale: r,
951
+ normalizedCoordinates: {
952
+ llx: Math.round(l.x),
953
+ lly: Math.round(h - l.y),
954
+ ury: Math.round(h - l.y - l.height),
955
+ width: Math.round(l.width),
956
+ height: Math.round(l.height)
957
+ }
958
+ });
959
+ });
960
+ }), i;
961
+ },
962
+ updateObject(e, t, i) {
963
+ if (e < 0 || e >= this.pdfDocuments.length) return;
964
+ const s = this.pdfDocuments[e], o = `${e}-${t}`;
965
+ let a = this.objectIndexCache[o];
966
+ if (a === void 0 && (a = L(s, t), a !== void 0 && (this.objectIndexCache[o] = a)), a === void 0) return;
967
+ const n = s.allObjects[a]?.find((h) => h.id === t);
968
+ if (n) {
969
+ if (i._globalDrag && i._mouseX !== void 0 && i._mouseY !== void 0) {
970
+ const h = i._mouseX, r = i._mouseY, l = this.getPageBoundsMap();
971
+ (!l || Object.keys(l).length === 0) && this.cachePageBounds();
972
+ const u = this.getPageRect(e, a);
973
+ if (u) {
974
+ const c = this.getDisplayedPageScale(e, a), d = (h - u.left - this.draggingElementShift.x) / c - this.draggingInitialMouseOffset.x / c, g = (r - u.top - this.draggingElementShift.y) / c - this.draggingInitialMouseOffset.y / c;
975
+ this.updateObjectInPage(e, a, t, { x: d, y: g });
976
+ }
977
+ return;
978
+ }
979
+ if (i.x !== void 0 || i.y !== void 0) {
980
+ const h = i.x !== void 0 ? i.x : n.x, r = i.y !== void 0 ? i.y : n.y, l = i.width !== void 0 ? i.width : n.width, u = i.height !== void 0 ? i.height : n.height, { width: c, height: d } = this.getPageSize(e, a);
981
+ if (h >= 0 && r >= 0 && h + l <= c && r + u <= d) {
982
+ this.updateObjectInPage(e, a, t, i);
983
+ return;
984
+ }
985
+ let g = a, f = 0;
986
+ for (let p = 0; p < s.pages.length; p++) {
987
+ const O = this.getPageWidth(e, p), S = this.getPageHeight(e, p), w = de(h, r, l, u, O, S);
988
+ w > f && (f = w, g = p);
989
+ }
990
+ if (g !== a) {
991
+ const { width: p, height: O } = this.getPageSize(e, g), { x: S, y: w } = F(h, r, l, u, p, O);
992
+ this.removeObjectFromPage(e, a, t);
993
+ const j = {
994
+ ...n,
995
+ ...i,
996
+ x: S,
997
+ y: w
998
+ };
999
+ s.allObjects[g].push(j), this.objectIndexCache[`${e}-${t}`] = g;
1000
+ return;
1001
+ }
1002
+ const { width: b, height: D } = this.getPageSize(e, a);
1003
+ if (h < 0 || r < 0 || h + l > b || r + u > D)
1004
+ return;
1005
+ }
1006
+ this.updateObjectInPage(e, a, t, i);
1007
+ }
1008
+ },
1009
+ deleteObject(e, t) {
1010
+ if (e < 0 || e >= this.pdfDocuments.length) return;
1011
+ const i = this.pdfDocuments[e];
1012
+ let s = null, o = -1;
1013
+ i.allObjects.some((a, n) => {
1014
+ const h = a.findIndex((r) => r.id === t);
1015
+ return h === -1 ? !1 : (s = a[h], o = n, a.splice(h, 1), !0);
1016
+ }), delete this.objectIndexCache[`${e}-${t}`], s && this.$emit("pdf-elements:delete-object", {
1017
+ object: s,
1018
+ docIndex: e,
1019
+ pageIndex: o
1020
+ });
1021
+ },
1022
+ duplicateObject(e, t) {
1023
+ if (e < 0 || e >= this.pdfDocuments.length) return;
1024
+ const i = this.pdfDocuments[e], s = `${e}-${t}`;
1025
+ let o = this.objectIndexCache[s];
1026
+ if (o === void 0 && (o = L(i, t), o !== void 0 && (this.objectIndexCache[s] = o)), o === void 0) return;
1027
+ const a = i.allObjects[o]?.find((g) => g.id === t);
1028
+ if (!a) return;
1029
+ const { width: n, height: h } = this.getPageSize(e, o), r = 12, { x: l, y: u } = F(
1030
+ a.x + r,
1031
+ a.y + r,
1032
+ a.width,
1033
+ a.height,
1034
+ n,
1035
+ h
1036
+ );
1037
+ let c = a.signer;
1038
+ c?.element && Object.prototype.hasOwnProperty.call(c.element, "elementId") && (c = {
1039
+ ...c,
1040
+ element: { ...c.element }
1041
+ }, delete c.element.elementId);
1042
+ const d = {
1043
+ ...a,
1044
+ id: this.generateObjectId(),
1045
+ x: l,
1046
+ y: u,
1047
+ signer: c
1048
+ };
1049
+ i.allObjects[o].push(d), this.objectIndexCache[`${e}-${d.id}`] = o, this.$nextTick(() => {
1050
+ const g = `draggable${e}-${o}-${d.id}`, f = this.$refs[g];
1051
+ f && Array.isArray(f) && f[0] && (f[0].isSelected = !0);
1052
+ });
1053
+ },
1054
+ checkAndMoveObjectPage(e, t, i, s) {
1055
+ if (e < 0 || e >= this.pdfDocuments.length) return;
1056
+ const o = this.pdfDocuments[e], a = `${e}-${t}`;
1057
+ let n = this.objectIndexCache[a];
1058
+ if (n === void 0 && (n = L(o, t), n !== void 0 && (this.objectIndexCache[a] = n)), n === void 0) return;
1059
+ const h = o.allObjects[n]?.find((w) => w.id === t);
1060
+ if (!h) return n;
1061
+ let r = n;
1062
+ const l = this.getPageBoundsList(), u = this.getPageBoundsMap(), c = l.length ? l : Object.values(u);
1063
+ for (let w = 0; w < c.length; w++) {
1064
+ const { docIndex: j, pageIndex: C, rect: k } = c[w];
1065
+ if (j === e && i >= k.left && i <= k.right && s >= k.top && s <= k.bottom) {
1066
+ r = C;
1067
+ break;
1068
+ }
1069
+ }
1070
+ const d = this.getPageRect(e, r);
1071
+ if (!d) return n;
1072
+ const g = this.getDisplayedPageScale(e, r), f = (i - d.left - this.draggingElementShift.x) / g - this.draggingInitialMouseOffset.x / g, b = (s - d.top - this.draggingElementShift.y) / g - this.draggingInitialMouseOffset.y / g, { width: D, height: p } = this.getPageSize(e, r), { x: O, y: S } = F(
1073
+ f,
1074
+ b,
1075
+ h.width,
1076
+ h.height,
1077
+ D,
1078
+ p
1079
+ );
1080
+ return r !== n ? (this.removeObjectFromPage(e, n, t), o.allObjects[r].push({
1081
+ ...h,
1082
+ x: O,
1083
+ y: S
1084
+ }), this.objectIndexCache[a] = r) : (O !== h.x || S !== h.y) && this.updateObjectInPage(e, n, t, { x: O, y: S }), r;
1085
+ },
1086
+ onMeasure(e, t, i) {
1087
+ t < 0 || t >= this.pdfDocuments.length || (this.pdfDocuments[t].pagesScale.splice(i, 1, e.scale), this._pageMeasurementCache[`${t}-${i}`] = null, this.cachePageBoundsForPage(t, i), this.autoFitZoom && this.scheduleAutoFitZoom());
1088
+ },
1089
+ formatPageNumber(e, t) {
1090
+ return this.pageCountFormat.replace("{currentPage}", e).replace("{totalPages}", t);
1091
+ },
1092
+ getPageWidth(e, t) {
1093
+ const i = this.getPageComponent(e, t);
1094
+ return i ? this.getCachedMeasurement(e, t, i).width : 0;
1095
+ },
1096
+ getPageHeight(e, t) {
1097
+ const i = this.getPageComponent(e, t);
1098
+ return i ? this.getCachedMeasurement(e, t, i).height : 0;
1099
+ },
1100
+ getPageSize(e, t) {
1101
+ return {
1102
+ width: this.getPageWidth(e, t),
1103
+ height: this.getPageHeight(e, t)
1104
+ };
1105
+ },
1106
+ getCachedMeasurement(e, t, i) {
1107
+ const s = `${e}-${t}`, a = this.pdfDocuments[e].pagesScale[t] || 1;
1108
+ return ve(this._pageMeasurementCache, s, i, a);
1109
+ },
1110
+ calculateOptimalScale(e) {
1111
+ const t = this.$el?.clientWidth || 0;
1112
+ if (!t || !e) return 1;
1113
+ const i = t - 40;
1114
+ return Math.max(0.1, Math.min(2, i / e));
1115
+ },
1116
+ scheduleAutoFitZoom() {
1117
+ this.autoFitApplied || this.zoomRafId || (this.zoomRafId = window.requestAnimationFrame(() => {
1118
+ this.zoomRafId = 0, this.adjustZoomToFit();
1119
+ }));
1120
+ },
1121
+ adjustZoomToFit() {
1122
+ if (!this.autoFitZoom || this.autoFitApplied || !this.pdfDocuments.length) return;
1123
+ const e = this.pdfDocuments.flatMap((s) => s.pageWidths || []).filter((s) => s > 0);
1124
+ let t = 0;
1125
+ if (e.length)
1126
+ t = Math.max(...e);
1127
+ else {
1128
+ if (this.autoFitZoom) {
1129
+ this.scheduleAutoFitZoom();
1130
+ return;
1131
+ }
1132
+ const s = this.$el?.querySelectorAll("canvas");
1133
+ if (!s?.length) return;
1134
+ t = Math.max(...Array.from(s).map(
1135
+ (o) => o.width / (this.scale || 1)
1136
+ ));
1137
+ }
1138
+ const i = this.calculateOptimalScale(t);
1139
+ this.autoFitApplied = !0, Math.abs(i - this.scale) > 0.01 && (this.scale = i, this.visualScale = i, q(this.pdfDocuments, this.scale), this._pageMeasurementCache = y({}), this.cachePageBounds());
1140
+ }
1141
+ }
1142
+ }), be = {
1143
+ key: 0,
1144
+ class: "pages-container"
1145
+ }, ye = ["onMousedown", "onTouchstart"], Pe = ["onClick", "onTouchend"], Oe = {
1146
+ key: 0,
1147
+ class: "page-footer"
1148
+ };
1149
+ function De(e, t, i, s, o, a) {
1150
+ const n = Z("PDFPage"), h = Z("DraggableElement");
1151
+ return m(), v("div", {
1152
+ style: E({ width: e.width, height: e.height }),
1153
+ class: "pdf-elements-root"
1154
+ }, [
1155
+ e.pdfDocuments.length ? (m(), v("div", be, [
1156
+ (m(!0), v(z, null, B(e.pdfDocuments, (r, l) => (m(), v("div", { key: l }, [
1157
+ (m(!0), v(z, null, B(r.pages, (u, c) => (m(), v("div", {
1158
+ key: `${l}-${c}`,
1159
+ class: "page-slot"
1160
+ }, [
1161
+ P("div", {
1162
+ class: "page-wrapper",
1163
+ onMousedown: (d) => e.selectPage(l, c),
1164
+ onTouchstart: (d) => e.selectPage(l, c)
1165
+ }, [
1166
+ P("div", {
1167
+ class: Y(["page-canvas", { "shadow-outline": l === e.selectedDocIndex && c === e.selectedPageIndex }])
1168
+ }, [
1169
+ G(n, {
1170
+ ref_for: !0,
1171
+ ref: `page${l}-${c}`,
1172
+ page: u,
1173
+ scale: e.scale,
1174
+ onOnMeasure: (d) => e.onMeasure(d, l, c)
1175
+ }, null, 8, ["page", "scale", "onOnMeasure"]),
1176
+ P("div", {
1177
+ class: "overlay",
1178
+ onMousemove: t[0] || (t[0] = (...d) => e.handleMouseMove && e.handleMouseMove(...d)),
1179
+ onTouchmove: t[1] || (t[1] = (...d) => e.handleMouseMove && e.handleMouseMove(...d)),
1180
+ onClick: (d) => e.handleOverlayClick(l, c, d),
1181
+ onTouchend: (d) => e.handleOverlayClick(l, c, d)
1182
+ }, [
1183
+ e.isAddingMode && e.previewPageDocIndex === l && e.previewPageIndex === c && e.previewElement && e.previewVisible ? (m(), v("div", {
1184
+ key: 0,
1185
+ class: "preview-element",
1186
+ style: E({
1187
+ left: `${e.previewPosition.x * e.previewScale.x}px`,
1188
+ top: `${e.previewPosition.y * e.previewScale.y}px`,
1189
+ width: `${e.previewElement.width * e.previewScale.x}px`,
1190
+ height: `${e.previewElement.height * e.previewScale.y}px`
1191
+ })
1192
+ }, [
1193
+ M(e.$slots, e.previewElement.type ? `element-${e.previewElement.type}` : "custom", {
1194
+ object: e.previewElement,
1195
+ isSelected: !1
1196
+ }, () => [
1197
+ M(e.$slots, "custom", {
1198
+ object: e.previewElement,
1199
+ isSelected: !1
1200
+ }, void 0, !0)
1201
+ ], !0)
1202
+ ], 4)) : $("", !0),
1203
+ (m(!0), v(z, null, B(r.allObjects[c], (d) => (m(), J(h, {
1204
+ key: d.id,
1205
+ ref_for: !0,
1206
+ ref: `draggable${l}-${c}-${d.id}`,
1207
+ object: d,
1208
+ "pages-scale": e.getDisplayedPageScale(l, c),
1209
+ "page-width": e.getPageWidth(l, c),
1210
+ "page-height": e.getPageHeight(l, c),
1211
+ "read-only": e.readOnly,
1212
+ "on-update": (g) => e.updateObject(l, d.id, g),
1213
+ "on-delete": () => e.deleteObject(l, d.id),
1214
+ "on-duplicate": () => e.duplicateObject(l, d.id),
1215
+ "on-drag-start": (g, f, b, D) => e.startDraggingElement(l, c, d, g, f, b, D),
1216
+ "on-drag-move": e.updateDraggingPosition,
1217
+ "on-drag-end": e.stopDraggingElement,
1218
+ "is-being-dragged-globally": e.isDraggingElement && e.draggingObject && e.draggingObject.id === d.id,
1219
+ "dragging-client-pos": e.draggingClientPosition,
1220
+ "current-doc-index": l,
1221
+ "current-page-index": c,
1222
+ "global-drag-doc-index": e.draggingDocIndex,
1223
+ "global-drag-page-index": e.draggingPageIndex,
1224
+ "show-selection-ui": e.showSelectionHandles && !e.hideSelectionUI && d.resizable !== !1,
1225
+ "show-default-actions": e.showElementActions && !e.hideSelectionUI,
1226
+ "ignore-click-outside-selectors": e.ignoreClickOutsideSelectors
1227
+ }, {
1228
+ default: N((g) => [
1229
+ M(e.$slots, g.object.type ? `element-${g.object.type}` : "custom", {
1230
+ object: g.object,
1231
+ onDelete: g.onDelete,
1232
+ onResize: g.onResize
1233
+ }, () => [
1234
+ M(e.$slots, "custom", {
1235
+ object: g.object,
1236
+ onDelete: g.onDelete,
1237
+ onResize: g.onResize
1238
+ }, void 0, !0)
1239
+ ], !0)
1240
+ ]),
1241
+ actions: N((g) => [
1242
+ M(e.$slots, "actions", {
1243
+ object: g.object,
1244
+ onDelete: g.onDelete,
1245
+ onDuplicate: g.onDuplicate
1246
+ }, void 0, !0)
1247
+ ]),
1248
+ _: 2
1249
+ }, 1032, ["object", "pages-scale", "page-width", "page-height", "read-only", "on-update", "on-delete", "on-duplicate", "on-drag-start", "on-drag-move", "on-drag-end", "is-being-dragged-globally", "dragging-client-pos", "current-doc-index", "current-page-index", "global-drag-doc-index", "global-drag-page-index", "show-selection-ui", "show-default-actions", "ignore-click-outside-selectors"]))), 128))
1250
+ ], 40, Pe)
1251
+ ], 2),
1252
+ e.showPageFooter ? (m(), v("div", Oe, [
1253
+ P("span", null, U(r.name), 1),
1254
+ P("span", null, U(e.formatPageNumber(c + 1, r.numPages)), 1)
1255
+ ])) : $("", !0)
1256
+ ], 40, ye)
1257
+ ]))), 128))
1258
+ ]))), 128))
1259
+ ])) : $("", !0),
1260
+ e.isDraggingElement && e.draggingObject ? (m(), v("div", {
1261
+ key: 1,
1262
+ class: "drag-portal",
1263
+ style: E({
1264
+ position: "fixed",
1265
+ left: `${e.draggingClientPosition.x}px`,
1266
+ top: `${e.draggingClientPosition.y}px`,
1267
+ width: `${e.draggingObject.width * e.draggingScale}px`,
1268
+ height: `${e.draggingObject.height * e.draggingScale}px`,
1269
+ pointerEvents: "none"
1270
+ })
1271
+ }, [
1272
+ M(e.$slots, e.draggingObject.type ? `element-${e.draggingObject.type}` : "custom", {
1273
+ object: e.draggingObject,
1274
+ isSelected: !1
1275
+ }, () => [
1276
+ M(e.$slots, "custom", {
1277
+ object: e.draggingObject,
1278
+ isSelected: !1
1279
+ }, void 0, !0)
1280
+ ], !0)
1281
+ ], 4)) : $("", !0)
1282
+ ], 4);
1283
+ }
1284
+ const T = /* @__PURE__ */ V(we, [["render", De], ["__scopeId", "data-v-eb792162"]]), Se = (e) => {
1285
+ const t = T.name || "PDFElements";
1286
+ e.component(t, T);
1287
+ };
1288
+ T.install = Se;
1289
+ export {
1290
+ T as PDFElements,
1291
+ T as default,
1292
+ je as setWorkerPath
1293
+ };