@libresign/pdf-elements 1.0.1 → 1.0.2
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.mjs +51 -43
- package/package.json +1 -1
- package/src/utils/asyncReader.ts +19 -4
package/dist/index.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
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
|
|
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 K, createBlock as J, withCtx as N, toDisplayString as U } from "vue";
|
|
2
2
|
const Q = I({
|
|
3
3
|
name: "PDFPage",
|
|
4
4
|
emits: ["onMeasure"],
|
|
@@ -419,43 +419,51 @@ function oe(e, t, i, s, o, a) {
|
|
|
419
419
|
}
|
|
420
420
|
const ae = /* @__PURE__ */ V(ie, [["render", oe], ["__scopeId", "data-v-c700849e"]]);
|
|
421
421
|
let H = null, A = null, W = null, X = null;
|
|
422
|
-
function re() {
|
|
423
|
-
|
|
422
|
+
function re(e) {
|
|
423
|
+
const t = e;
|
|
424
|
+
return t.default?.PDFWorker ? t.default : t;
|
|
424
425
|
}
|
|
425
426
|
function he() {
|
|
427
|
+
return A || (A = import("./pdf-uHvE5neP.mjs").then(re)), A;
|
|
428
|
+
}
|
|
429
|
+
function le() {
|
|
426
430
|
return W || (W = import("./pdf.worker.min-DmO9Xdfo.mjs").then(
|
|
427
431
|
(e) => e.default
|
|
428
432
|
)), W;
|
|
429
433
|
}
|
|
430
|
-
async function
|
|
431
|
-
if (
|
|
432
|
-
|
|
433
|
-
|
|
434
|
+
async function ce(e) {
|
|
435
|
+
if (e?.GlobalWorkerOptions) {
|
|
436
|
+
if (X) {
|
|
437
|
+
e.GlobalWorkerOptions.workerSrc = X;
|
|
438
|
+
return;
|
|
439
|
+
}
|
|
440
|
+
e.GlobalWorkerOptions.workerSrc || (e.GlobalWorkerOptions.workerSrc = await le());
|
|
434
441
|
}
|
|
435
|
-
e.GlobalWorkerOptions.workerSrc || (e.GlobalWorkerOptions.workerSrc = await he());
|
|
436
442
|
}
|
|
437
|
-
async function
|
|
438
|
-
return H || (await
|
|
443
|
+
async function de(e) {
|
|
444
|
+
return H || (await ce(e), H = new e.PDFWorker({})), H;
|
|
439
445
|
}
|
|
440
|
-
function
|
|
441
|
-
X = e, A && A.then((t) => {
|
|
442
|
-
t.GlobalWorkerOptions.workerSrc = e;
|
|
446
|
+
function Ce(e) {
|
|
447
|
+
X = e, H = null, A && A.then((t) => {
|
|
448
|
+
t?.GlobalWorkerOptions && (t.GlobalWorkerOptions.workerSrc = e);
|
|
449
|
+
}).catch((t) => {
|
|
450
|
+
console.warn("setWorkerPath: failed to update pdfjs workerSrc immediately", t);
|
|
443
451
|
});
|
|
444
452
|
}
|
|
445
|
-
function
|
|
453
|
+
function G(e) {
|
|
446
454
|
return new Promise((t, i) => {
|
|
447
455
|
const s = new FileReader();
|
|
448
456
|
s.onload = () => t(s.result), s.onerror = i, s.readAsArrayBuffer(e);
|
|
449
457
|
});
|
|
450
458
|
}
|
|
451
459
|
async function x(e, t = {}) {
|
|
452
|
-
const i = await
|
|
460
|
+
const i = await he(), s = await de(i), o = e instanceof ArrayBuffer, a = ArrayBuffer.isView(e), n = typeof Blob < "u" && e instanceof Blob;
|
|
453
461
|
if (e && typeof e == "object" && !o && !a && !n)
|
|
454
462
|
return i.getDocument({ ...e, ...t, worker: s }).promise;
|
|
455
463
|
if (typeof e == "string")
|
|
456
464
|
return i.getDocument({ url: e, ...t, worker: s }).promise;
|
|
457
465
|
if (n) {
|
|
458
|
-
const r = await
|
|
466
|
+
const r = await G(e);
|
|
459
467
|
return i.getDocument({ data: r, ...t, worker: s }).promise;
|
|
460
468
|
}
|
|
461
469
|
const h = o ? e : new Uint8Array(
|
|
@@ -471,17 +479,17 @@ function F(e, t, i, s, o, a) {
|
|
|
471
479
|
y: Math.max(0, Math.min(t, a - s))
|
|
472
480
|
};
|
|
473
481
|
}
|
|
474
|
-
function
|
|
482
|
+
function ge(e, t, i, s, o, a) {
|
|
475
483
|
const n = Math.max(0, e), h = Math.max(0, t), r = Math.min(o, e + i), l = Math.min(a, t + s);
|
|
476
484
|
return r <= n || l <= h ? 0 : (r - n) * (l - h);
|
|
477
485
|
}
|
|
478
|
-
function
|
|
486
|
+
function ue(e, t, i = 300) {
|
|
479
487
|
return {
|
|
480
488
|
minY: Math.max(0, e - i),
|
|
481
489
|
maxY: e + t + i
|
|
482
490
|
};
|
|
483
491
|
}
|
|
484
|
-
function
|
|
492
|
+
function fe(e, t, i, s) {
|
|
485
493
|
return !(e + t < i || e > s);
|
|
486
494
|
}
|
|
487
495
|
function q(e, t) {
|
|
@@ -489,7 +497,7 @@ function q(e, t) {
|
|
|
489
497
|
i.pagesScale = i.pagesScale.map(() => t);
|
|
490
498
|
});
|
|
491
499
|
}
|
|
492
|
-
function
|
|
500
|
+
function pe(e, t) {
|
|
493
501
|
return !e || !t ? !1 : e.allObjects.some((i) => i.some((s) => s.id === t));
|
|
494
502
|
}
|
|
495
503
|
function L(e, t) {
|
|
@@ -499,19 +507,19 @@ function L(e, t) {
|
|
|
499
507
|
return i;
|
|
500
508
|
}
|
|
501
509
|
}
|
|
502
|
-
function
|
|
510
|
+
function me(e, t, i, s) {
|
|
503
511
|
const o = e?.allObjects?.[t];
|
|
504
512
|
if (!o) return !1;
|
|
505
513
|
const a = o.findIndex((n) => n.id === i);
|
|
506
514
|
return a === -1 ? !1 : (o.splice(a, 1, { ...o[a], ...s }), !0);
|
|
507
515
|
}
|
|
508
|
-
function
|
|
516
|
+
function ve(e, t, i) {
|
|
509
517
|
const s = e?.allObjects?.[t];
|
|
510
518
|
if (!s) return !1;
|
|
511
519
|
const o = s.findIndex((a) => a.id === i);
|
|
512
520
|
return o === -1 ? !1 : (s.splice(o, 1), !0);
|
|
513
521
|
}
|
|
514
|
-
function
|
|
522
|
+
function we(e, t, i, s) {
|
|
515
523
|
const o = e[t];
|
|
516
524
|
if (o)
|
|
517
525
|
return o;
|
|
@@ -521,7 +529,7 @@ function ve(e, t, i, s) {
|
|
|
521
529
|
};
|
|
522
530
|
return e[t] = n, n;
|
|
523
531
|
}
|
|
524
|
-
const
|
|
532
|
+
const be = I({
|
|
525
533
|
name: "PDFElements",
|
|
526
534
|
emits: ["pdf-elements:end-init", "pdf-elements:delete-object", "pdf-elements:object-click"],
|
|
527
535
|
components: {
|
|
@@ -652,7 +660,7 @@ const we = I({
|
|
|
652
660
|
const i = this.initFiles[t], s = this.initFileNames[t] || `document-${t + 1}.pdf`;
|
|
653
661
|
let o;
|
|
654
662
|
if (i instanceof Blob) {
|
|
655
|
-
const r = await
|
|
663
|
+
const r = await G(i);
|
|
656
664
|
o = await x({ data: r }, this.pdfjsOptions);
|
|
657
665
|
} else
|
|
658
666
|
o = await x(i, this.pdfjsOptions);
|
|
@@ -723,14 +731,14 @@ const we = I({
|
|
|
723
731
|
if (!this.isAddingMode && !this.isDraggingElement && s === this._lastPageBoundsScrollTop && o === this._lastPageBoundsClientHeight)
|
|
724
732
|
return;
|
|
725
733
|
this._lastPageBoundsScrollTop = s, this._lastPageBoundsClientHeight = o;
|
|
726
|
-
const { minY: a, maxY: n } =
|
|
734
|
+
const { minY: a, maxY: n } = ue(s, o);
|
|
727
735
|
for (let h = 0; h < this.pdfDocuments.length; h++)
|
|
728
736
|
for (let r = 0; r < this.pdfDocuments[h].pages.length; r++) {
|
|
729
737
|
const l = this.getPageCanvasElement(h, r);
|
|
730
738
|
if (!l) continue;
|
|
731
739
|
if (o) {
|
|
732
740
|
const d = l.closest(".page-wrapper") || l, g = d.offsetTop || 0, f = d.offsetHeight || 0;
|
|
733
|
-
if (!
|
|
741
|
+
if (!fe(g, f, a, n))
|
|
734
742
|
continue;
|
|
735
743
|
}
|
|
736
744
|
const u = l.getBoundingClientRect(), c = {
|
|
@@ -925,15 +933,15 @@ const we = I({
|
|
|
925
933
|
const i = `${e}-${t}`;
|
|
926
934
|
if (this.objectIndexCache[i] !== void 0) return !0;
|
|
927
935
|
const s = this.pdfDocuments[e];
|
|
928
|
-
return
|
|
936
|
+
return pe(s, t);
|
|
929
937
|
},
|
|
930
938
|
updateObjectInPage(e, t, i, s) {
|
|
931
939
|
const o = this.pdfDocuments[e];
|
|
932
|
-
|
|
940
|
+
me(o, t, i, s);
|
|
933
941
|
},
|
|
934
942
|
removeObjectFromPage(e, t, i) {
|
|
935
943
|
const s = this.pdfDocuments[e];
|
|
936
|
-
|
|
944
|
+
ve(s, t, i);
|
|
937
945
|
},
|
|
938
946
|
getAllObjects(e = this.selectedDocIndex) {
|
|
939
947
|
if (e < 0 || e >= this.pdfDocuments.length) return [];
|
|
@@ -984,7 +992,7 @@ const we = I({
|
|
|
984
992
|
}
|
|
985
993
|
let g = a, f = 0;
|
|
986
994
|
for (let p = 0; p < s.pages.length; p++) {
|
|
987
|
-
const O = this.getPageWidth(e, p), S = this.getPageHeight(e, p), w =
|
|
995
|
+
const O = this.getPageWidth(e, p), S = this.getPageHeight(e, p), w = ge(h, r, l, u, O, S);
|
|
988
996
|
w > f && (f = w, g = p);
|
|
989
997
|
}
|
|
990
998
|
if (g !== a) {
|
|
@@ -1105,7 +1113,7 @@ const we = I({
|
|
|
1105
1113
|
},
|
|
1106
1114
|
getCachedMeasurement(e, t, i) {
|
|
1107
1115
|
const s = `${e}-${t}`, a = this.pdfDocuments[e].pagesScale[t] || 1;
|
|
1108
|
-
return
|
|
1116
|
+
return we(this._pageMeasurementCache, s, i, a);
|
|
1109
1117
|
},
|
|
1110
1118
|
calculateOptimalScale(e) {
|
|
1111
1119
|
const t = this.$el?.clientWidth || 0;
|
|
@@ -1139,20 +1147,20 @@ const we = I({
|
|
|
1139
1147
|
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
1148
|
}
|
|
1141
1149
|
}
|
|
1142
|
-
}),
|
|
1150
|
+
}), ye = {
|
|
1143
1151
|
key: 0,
|
|
1144
1152
|
class: "pages-container"
|
|
1145
|
-
},
|
|
1153
|
+
}, Pe = ["onMousedown", "onTouchstart"], Oe = ["onClick", "onTouchend"], De = {
|
|
1146
1154
|
key: 0,
|
|
1147
1155
|
class: "page-footer"
|
|
1148
1156
|
};
|
|
1149
|
-
function
|
|
1157
|
+
function Se(e, t, i, s, o, a) {
|
|
1150
1158
|
const n = Z("PDFPage"), h = Z("DraggableElement");
|
|
1151
1159
|
return m(), v("div", {
|
|
1152
1160
|
style: E({ width: e.width, height: e.height }),
|
|
1153
1161
|
class: "pdf-elements-root"
|
|
1154
1162
|
}, [
|
|
1155
|
-
e.pdfDocuments.length ? (m(), v("div",
|
|
1163
|
+
e.pdfDocuments.length ? (m(), v("div", ye, [
|
|
1156
1164
|
(m(!0), v(z, null, B(e.pdfDocuments, (r, l) => (m(), v("div", { key: l }, [
|
|
1157
1165
|
(m(!0), v(z, null, B(r.pages, (u, c) => (m(), v("div", {
|
|
1158
1166
|
key: `${l}-${c}`,
|
|
@@ -1166,7 +1174,7 @@ function De(e, t, i, s, o, a) {
|
|
|
1166
1174
|
P("div", {
|
|
1167
1175
|
class: Y(["page-canvas", { "shadow-outline": l === e.selectedDocIndex && c === e.selectedPageIndex }])
|
|
1168
1176
|
}, [
|
|
1169
|
-
|
|
1177
|
+
K(n, {
|
|
1170
1178
|
ref_for: !0,
|
|
1171
1179
|
ref: `page${l}-${c}`,
|
|
1172
1180
|
page: u,
|
|
@@ -1247,13 +1255,13 @@ function De(e, t, i, s, o, a) {
|
|
|
1247
1255
|
]),
|
|
1248
1256
|
_: 2
|
|
1249
1257
|
}, 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,
|
|
1258
|
+
], 40, Oe)
|
|
1251
1259
|
], 2),
|
|
1252
|
-
e.showPageFooter ? (m(), v("div",
|
|
1260
|
+
e.showPageFooter ? (m(), v("div", De, [
|
|
1253
1261
|
P("span", null, U(r.name), 1),
|
|
1254
1262
|
P("span", null, U(e.formatPageNumber(c + 1, r.numPages)), 1)
|
|
1255
1263
|
])) : $("", !0)
|
|
1256
|
-
], 40,
|
|
1264
|
+
], 40, Pe)
|
|
1257
1265
|
]))), 128))
|
|
1258
1266
|
]))), 128))
|
|
1259
1267
|
])) : $("", !0),
|
|
@@ -1281,13 +1289,13 @@ function De(e, t, i, s, o, a) {
|
|
|
1281
1289
|
], 4)) : $("", !0)
|
|
1282
1290
|
], 4);
|
|
1283
1291
|
}
|
|
1284
|
-
const T = /* @__PURE__ */ V(
|
|
1292
|
+
const T = /* @__PURE__ */ V(be, [["render", Se], ["__scopeId", "data-v-eb792162"]]), Me = (e) => {
|
|
1285
1293
|
const t = T.name || "PDFElements";
|
|
1286
1294
|
e.component(t, T);
|
|
1287
1295
|
};
|
|
1288
|
-
T.install =
|
|
1296
|
+
T.install = Me;
|
|
1289
1297
|
export {
|
|
1290
1298
|
T as PDFElements,
|
|
1291
1299
|
T as default,
|
|
1292
|
-
|
|
1300
|
+
Ce as setWorkerPath
|
|
1293
1301
|
};
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@libresign/pdf-elements",
|
|
3
3
|
"description": "PDF viewer with draggable and resizable element overlays for Vue 3",
|
|
4
|
-
"version": "1.0.
|
|
4
|
+
"version": "1.0.2",
|
|
5
5
|
"author": "LibreCode <contact@librecode.coop>",
|
|
6
6
|
"private": false,
|
|
7
7
|
"main": "dist/index.js",
|
package/src/utils/asyncReader.ts
CHANGED
|
@@ -8,9 +8,16 @@ let pdfjsPromise: Promise<typeof import('pdfjs-dist')> | null = null
|
|
|
8
8
|
let workerUrlPromise: Promise<string> | null = null
|
|
9
9
|
let workerSrcOverride: string | null = null
|
|
10
10
|
|
|
11
|
+
type PdfjsModule = typeof import('pdfjs-dist')
|
|
12
|
+
|
|
13
|
+
function normalizePdfjs(mod: PdfjsModule): PdfjsModule {
|
|
14
|
+
const m = mod as PdfjsModule & { default?: PdfjsModule }
|
|
15
|
+
return (m.default?.PDFWorker ? m.default : m) as PdfjsModule
|
|
16
|
+
}
|
|
17
|
+
|
|
11
18
|
function loadPdfjs() {
|
|
12
19
|
if (!pdfjsPromise) {
|
|
13
|
-
pdfjsPromise = import('pdfjs-dist')
|
|
20
|
+
pdfjsPromise = import('pdfjs-dist').then(normalizePdfjs)
|
|
14
21
|
}
|
|
15
22
|
return pdfjsPromise
|
|
16
23
|
}
|
|
@@ -24,7 +31,10 @@ function loadWorkerUrl() {
|
|
|
24
31
|
return workerUrlPromise
|
|
25
32
|
}
|
|
26
33
|
|
|
27
|
-
async function ensureWorkerSrc(pdfjs:
|
|
34
|
+
async function ensureWorkerSrc(pdfjs: PdfjsModule) {
|
|
35
|
+
if (!pdfjs?.GlobalWorkerOptions) {
|
|
36
|
+
return
|
|
37
|
+
}
|
|
28
38
|
if (workerSrcOverride) {
|
|
29
39
|
pdfjs.GlobalWorkerOptions.workerSrc = workerSrcOverride
|
|
30
40
|
return
|
|
@@ -34,7 +44,7 @@ async function ensureWorkerSrc(pdfjs: typeof import('pdfjs-dist')) {
|
|
|
34
44
|
}
|
|
35
45
|
}
|
|
36
46
|
|
|
37
|
-
async function getSharedWorker(pdfjs:
|
|
47
|
+
async function getSharedWorker(pdfjs: PdfjsModule): Promise<PDFWorker> {
|
|
38
48
|
if (!sharedWorker) {
|
|
39
49
|
await ensureWorkerSrc(pdfjs)
|
|
40
50
|
sharedWorker = new pdfjs.PDFWorker({}) as PDFWorker
|
|
@@ -44,9 +54,14 @@ async function getSharedWorker(pdfjs: typeof import('pdfjs-dist')): Promise<PDFW
|
|
|
44
54
|
|
|
45
55
|
export function setWorkerPath(path: string) {
|
|
46
56
|
workerSrcOverride = path
|
|
57
|
+
sharedWorker = null
|
|
47
58
|
if (pdfjsPromise) {
|
|
48
59
|
pdfjsPromise.then((pdfjs) => {
|
|
49
|
-
pdfjs
|
|
60
|
+
if (pdfjs?.GlobalWorkerOptions) {
|
|
61
|
+
pdfjs.GlobalWorkerOptions.workerSrc = path
|
|
62
|
+
}
|
|
63
|
+
}).catch((error) => {
|
|
64
|
+
console.warn('setWorkerPath: failed to update pdfjs workerSrc immediately', error)
|
|
50
65
|
})
|
|
51
66
|
}
|
|
52
67
|
}
|