@component-hook/pdf-canvas 1.2.2-alpha.3 → 1.2.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/README.md CHANGED
@@ -62,24 +62,19 @@ async function uploadFile(event: Event) {
62
62
  </script>
63
63
 
64
64
  <template>
65
- <div class="w-fit flex flex-col items-center gap-3">
65
+ <div>
66
66
  <pdf-canvas
67
67
  v-if="currentPdf"
68
68
  :file="currentPdf"
69
69
  />
70
70
 
71
- <p
72
- v-else
73
- class="font-mono text-sm"
74
- >
75
- Please select a PDF file or image.
76
- </p>
71
+ <p v-else>Please select a PDF file or image.</p>
77
72
 
78
73
  <button class="relative">
79
74
  <input
80
75
  type="file"
81
76
  accept="application/pdf, .jpg, .png"
82
- class="opacity-0 top-0 left-0 absolute w-[94px] h-[36px] cursor-pointer"
77
+ class="absolute top-0 left-0 h-[36px] w-[94px] cursor-pointer opacity-0"
83
78
  @change="uploadFile"
84
79
  />
85
80
  select file
@@ -127,6 +122,33 @@ export function DrawPdf() {
127
122
  }
128
123
  ```
129
124
 
125
+ ## ⚠️ Required Vite Configuration
126
+
127
+ If you're using Vite in development mode, you must exclude this package from Vite's dependency optimization, because it internally uses a Web Worker which doesn't work well when Vite pre-bundles dependencies.
128
+
129
+ ```js
130
+ // vite.config.js or vite.config.ts
131
+ import { defineConfig } from 'vite';
132
+
133
+ export default defineConfig({
134
+ // …other Vite config
135
+ optimizeDeps: {
136
+ exclude: ['@component-hook/pdf-canvas'],
137
+ },
138
+ });
139
+ ```
140
+
141
+ ### Why Is This Necessary?
142
+
143
+ - Vite's dev server does pre-bundling of dependencies (using esbuild) to improve performance.
144
+ - However, modules using Web Workers (like this component) may break or fail to initialize correctly during this pre-bundling stage.
145
+
146
+ ### Web Worker & Vite Known Issue
147
+
148
+ - This is a known issue: the Vite team is aware of difficulties when optimizing dependencies that use Web Workers.
149
+ - For more background, see this GitHub issue: [Vite/Rolldown-Vite #362](https://github.com/vitejs/rolldown-vite/issues/362)
150
+ - Once Vite improves its Web Worker optimization, it may become safe to remove this exclusion — but for now, it's required to ensure correct behavior in dev mode.
151
+
130
152
  ## License
131
153
 
132
154
  This project is licensed under the MIT License.
@@ -22230,26 +22230,18 @@ Y.setLayerDimensions;
22230
22230
  Y.shadow;
22231
22231
  Y.stopEvent;
22232
22232
  var Vg = Y.version;
22233
- function Wg(a) {
22234
- return new Worker(
22235
- "" + new URL("assets/pdfjs.worker-DawqtvLM.js", import.meta.url).href,
22236
- {
22237
- name: a?.name
22238
- }
22239
- );
22240
- }
22241
22233
  let Ja = null;
22242
- function Xg() {
22234
+ function Wg() {
22243
22235
  const a = document.createElement("div");
22244
22236
  a.style.width = "1in", document.body.append(a);
22245
22237
  const t = a.offsetWidth;
22246
22238
  return a.remove(), t;
22247
22239
  }
22248
- function Ug() {
22249
- const a = Ja ?? Xg();
22240
+ function Xg() {
22241
+ const a = Ja ?? Wg();
22250
22242
  return Ja = a, a / 72;
22251
22243
  }
22252
- function Yg(a, t = 500) {
22244
+ function Ug(a, t = 500) {
22253
22245
  if (typeof a != "function") throw new TypeError("The first argument is not a function.");
22254
22246
  let e = null;
22255
22247
  return function(...s) {
@@ -22258,15 +22250,15 @@ function Yg(a, t = 500) {
22258
22250
  }, t);
22259
22251
  };
22260
22252
  }
22261
- function $g(a) {
22253
+ function Yg(a) {
22262
22254
  return new Promise((t, e) => {
22263
22255
  const s = new FileReader();
22264
22256
  s.addEventListener("load", () => t(s.result)), s.addEventListener("error", e), s.readAsDataURL(a);
22265
22257
  });
22266
22258
  }
22267
22259
  Yl() || (zg.workerSrc = `https://cdnjs.cloudflare.com/ajax/libs/pdf.js/${Vg}/pdf.worker.min.mjs`);
22268
- async function qg(a) {
22269
- const t = "data:application/pdf;base64,", e = await $g(a);
22260
+ async function $g(a) {
22261
+ const t = "data:application/pdf;base64,", e = await Yg(a);
22270
22262
  if (typeof e == "string")
22271
22263
  return globalThis.atob(e.slice(t.length));
22272
22264
  }
@@ -22277,10 +22269,14 @@ function Yl() {
22277
22269
  return !1;
22278
22270
  }
22279
22271
  }
22280
- async function Kg({ page: a, PDFBase64: t, scale: e, password: s }, i) {
22281
- const n = Ug() / window.devicePixelRatio;
22272
+ async function qg({ page: a, PDFBase64: t, scale: e, password: s }, i) {
22273
+ const n = Xg() / window.devicePixelRatio;
22282
22274
  if (Yl()) {
22283
- const r = new Wg();
22275
+ const r = new Worker(new URL(
22276
+ /* @vite-ignore */
22277
+ "" + new URL("assets/pdfjs.worker-DawqtvLM.js", import.meta.url).href,
22278
+ import.meta.url
22279
+ ), { type: "module" });
22284
22280
  return r.postMessage({ data: t, password: s, id: i, page: a, units: n, scale: e }), await new Promise((h) => {
22285
22281
  r.addEventListener("message", ({ data: l }) => {
22286
22282
  if (l.status === "success")
@@ -22303,11 +22299,11 @@ async function Kg({ page: a, PDFBase64: t, scale: e, password: s }, i) {
22303
22299
  }
22304
22300
  }
22305
22301
  const jt = /* @__PURE__ */ new Map();
22306
- function Jg(a) {
22302
+ function Kg(a) {
22307
22303
  const t = new Pr(a);
22308
22304
  return jt.set(a, t), t;
22309
22305
  }
22310
- function Zg(a, t) {
22306
+ function Jg(a, t) {
22311
22307
  const e = Date.now();
22312
22308
  return {
22313
22309
  PDFId: `${a.name}-${e}`,
@@ -22316,19 +22312,19 @@ function Zg(a, t) {
22316
22312
  PDFBase64: t
22317
22313
  };
22318
22314
  }
22319
- async function Qg({ src: a }, t, e) {
22315
+ async function Zg({ src: a }, t, e) {
22320
22316
  if (!a || !e) return null;
22321
22317
  const { objects: s, options: i } = await kd(a), n = s.filter((o) => o !== null), r = Xu.groupSVGElements(n, i);
22322
22318
  return n.forEach((o) => {
22323
22319
  o.stroke = e;
22324
22320
  }), r.hoverCursor = "pointer", r.stroke = `${e}-${t}`, r;
22325
22321
  }
22326
- function tp(a, t) {
22322
+ function Qg(a, t) {
22327
22323
  if (!jt.get(a)) return;
22328
- const s = 1 / 3, i = ap(t, s);
22324
+ const s = 1 / 3, i = op(t, s);
22329
22325
  ql(a, i, s);
22330
22326
  }
22331
- async function ep(a, t) {
22327
+ async function tp(a, t) {
22332
22328
  const e = await Bd(a), s = Date.now(), n = {
22333
22329
  PDFId: `${a.name}${s}`,
22334
22330
  name: a.name.replace(/.png|.jpg|.jpeg/, ""),
@@ -22346,30 +22342,30 @@ function ql(a, t, e) {
22346
22342
  const s = jt.get(a);
22347
22343
  s && (s.setWidth(t.width * e), s.setHeight(t.height * e), s.backgroundImage = t, s.renderAll());
22348
22344
  }
22349
- function sp(a, t) {
22345
+ function ep(a, t) {
22350
22346
  if (!t) return;
22351
22347
  const { oCoords: e } = a;
22352
22348
  if (!e) return;
22353
22349
  const { x: s, y: i } = e.tl.touchCorner.tl, { height: n, width: r, scaleY: o, scaleX: h } = t;
22354
22350
  t.top = i - n * o / 2, t.left = s - r * h / 2, t.setCoords();
22355
22351
  }
22356
- function ip(a, t, e) {
22357
- !e || !a || (e.scale(1 / t), sp(a, e));
22352
+ function sp(a, t, e) {
22353
+ !e || !a || (e.scale(1 / t), ep(a, e));
22358
22354
  }
22359
- function np(a, t) {
22355
+ function ip(a, t) {
22360
22356
  if (!a) return;
22361
22357
  const e = a._cornerSize || 6;
22362
22358
  a.borderScaleFactor = 1 / t, a.cornerSize = e * (1 / t), a.touchCornerSize = e * 4 * (1 / t), a.setCoords();
22363
22359
  }
22364
- function rp(a) {
22360
+ function np(a) {
22365
22361
  const t = jt.get(a);
22366
22362
  t && (t.discardActiveObject(), t.renderAll());
22367
22363
  }
22368
- function op(a) {
22364
+ function rp(a) {
22369
22365
  const t = jt.get(a);
22370
22366
  t && (t.clear(), jt.delete(a));
22371
22367
  }
22372
- function ap(a, t) {
22368
+ function op(a, t) {
22373
22369
  return new Pt(a, { scaleX: t, scaleY: t });
22374
22370
  }
22375
22371
  function zr(a, t) {
@@ -22377,27 +22373,27 @@ function zr(a, t) {
22377
22373
  const { left: e, top: s, scaleX: i, scaleY: n, width: r, height: o } = a, h = t.x * r * i, l = t.y * o * n;
22378
22374
  a.set({ left: e - h, top: s - l });
22379
22375
  }
22380
- function hp(a, t, e) {
22376
+ function ap(a, t, e) {
22381
22377
  const s = a.type;
22382
22378
  return {
22383
- "application/pdf": lp,
22379
+ "application/pdf": hp,
22384
22380
  "image/png": Za,
22385
22381
  "image/jpeg": Za
22386
22382
  }[s]?.(a, t, e) ?? Promise.reject(new Error(`Unsupported ${s} file type.`));
22387
22383
  }
22388
- async function lp(a, t, e) {
22389
- const s = await qg(a);
22384
+ async function hp(a, t, e) {
22385
+ const s = await $g(a);
22390
22386
  if (!s) return;
22391
- const i = Zg(a, s), n = await Kl({ page: 1, PDFBase64: s, scale: 0.8, password: t }, e);
22387
+ const i = Jg(a, s), n = await Kl({ page: 1, PDFBase64: s, scale: 0.8, password: t }, e);
22392
22388
  return { ...i, pages: n };
22393
22389
  }
22394
22390
  function Kl(a, t) {
22395
- return Kg(a, t).then(({ canvas: e, pages: s }) => (e && t && tp(t, e), s));
22391
+ return qg(a, t).then(({ canvas: e, pages: s }) => (e && t && Qg(t, e), s));
22396
22392
  }
22397
22393
  async function Za(a, t, e) {
22398
- return await ep(a, e);
22394
+ return await tp(a, e);
22399
22395
  }
22400
- function cp(a, t) {
22396
+ function lp(a, t) {
22401
22397
  const e = jt.get(a);
22402
22398
  if (!e) return;
22403
22399
  const s = e.getActiveObject();
@@ -22405,7 +22401,7 @@ function cp(a, t) {
22405
22401
  i.set(t);
22406
22402
  }) : s.set(t), e.renderAll());
22407
22403
  }
22408
- async function up(a, t) {
22404
+ async function cp(a, t) {
22409
22405
  const e = jt.get(a);
22410
22406
  if (!e) return;
22411
22407
  const s = e?.getActiveObject();
@@ -22415,7 +22411,7 @@ async function up(a, t) {
22415
22411
  e.add(n), t(e, n);
22416
22412
  }) : (e.add(i), t(e, i)), zr(i, { x: -0.1, y: -0.1 }), e.setActiveObject(i);
22417
22413
  }
22418
- function dp(a) {
22414
+ function up(a) {
22419
22415
  const t = jt.get(a);
22420
22416
  if (!t) return;
22421
22417
  const e = t.getActiveObject();
@@ -22423,7 +22419,7 @@ function dp(a) {
22423
22419
  t.remove(s);
22424
22420
  }), t.discardActiveObject()) : t.remove(e));
22425
22421
  }
22426
- function fp(a) {
22422
+ function dp(a) {
22427
22423
  const {
22428
22424
  id: t = "",
22429
22425
  selectionOptions: e,
@@ -22435,7 +22431,7 @@ function fp(a) {
22435
22431
  } = a || {}, h = ws(null), l = ws(null), c = ws(1), u = ws({ ...hc });
22436
22432
  function d() {
22437
22433
  if (!t || jt.has(t)) return;
22438
- const O = Jg(t);
22434
+ const O = Kg(t);
22439
22435
  return O.on("selection:created", (D) => {
22440
22436
  if (r?.(D), D.selected.length <= 1) return;
22441
22437
  const z = O.getActiveObject();
@@ -22445,7 +22441,7 @@ function fp(a) {
22445
22441
  }), s && O.on("mouse:down", s), i && O.on("mouse:move", i), n && O.on("mouse:up", n), O;
22446
22442
  }
22447
22443
  function f(O, D) {
22448
- return hp(O, D, t);
22444
+ return ap(O, D, t);
22449
22445
  }
22450
22446
  function g(O) {
22451
22447
  return Kl(O, t);
@@ -22478,7 +22474,7 @@ function fp(a) {
22478
22474
  uuid: M = Date.now()
22479
22475
  }) {
22480
22476
  if (O.getActiveObjects().length > 1 || h.current?.stroke === `${z}-${M}`) return;
22481
- const W = await Qg(u.current, M, z);
22477
+ const W = await Zg(u.current, M, z);
22482
22478
  T(O), h.current = W, l.current = D, _(O, D, M), S(c.current, !0), W && O.add(W);
22483
22479
  }
22484
22480
  function _(O, D, z) {
@@ -22497,7 +22493,7 @@ function fp(a) {
22497
22493
  }
22498
22494
  function S(O, D = !1) {
22499
22495
  const z = jt.get(t);
22500
- c.current = O, z && (np(l.current, O), ip(l.current, O, h.current), !D && z.renderAll());
22496
+ c.current = O, z && (ip(l.current, O), sp(l.current, O, h.current), !D && z.renderAll());
22501
22497
  }
22502
22498
  function A(O) {
22503
22499
  if (!O) return;
@@ -22509,19 +22505,19 @@ function fp(a) {
22509
22505
  l.current = null, h.current && (O.remove(h.current), h.current = null);
22510
22506
  }
22511
22507
  function k() {
22512
- rp(t);
22508
+ np(t);
22513
22509
  }
22514
22510
  function P() {
22515
- op(t);
22511
+ rp(t);
22516
22512
  }
22517
22513
  function j(O) {
22518
- cp(t, O);
22514
+ lp(t, O);
22519
22515
  }
22520
22516
  function F() {
22521
- return up(t, v);
22517
+ return cp(t, v);
22522
22518
  }
22523
22519
  function I() {
22524
- dp(t);
22520
+ up(t);
22525
22521
  }
22526
22522
  return {
22527
22523
  createCanvas: d,
@@ -22539,14 +22535,14 @@ function fp(a) {
22539
22535
  deleteActiveFabric: I
22540
22536
  };
22541
22537
  }
22542
- function gp(a) {
22543
- const t = ws(0), e = Yg(() => {
22538
+ function fp(a) {
22539
+ const t = ws(0), e = Ug(() => {
22544
22540
  t.current !== window.innerWidth && (t.current = window.innerWidth, a());
22545
22541
  });
22546
22542
  Js(() => (window.addEventListener("resize", e), () => window.removeEventListener("resize", e)), []);
22547
22543
  }
22548
- const vp = Ql(pp);
22549
- function pp({
22544
+ const bp = Ql(gp);
22545
+ function gp({
22550
22546
  file: a,
22551
22547
  page: t = 1,
22552
22548
  canvasId: e = "PDF-canvas",
@@ -22582,7 +22578,7 @@ function pp({
22582
22578
  setCloseSvgOptions: ot,
22583
22579
  copyActiveFabric: lt,
22584
22580
  deleteActiveFabric: E
22585
- } = fp({
22581
+ } = dp({
22586
22582
  id: T,
22587
22583
  selectionOptions: P,
22588
22584
  pointerDown: b,
@@ -22591,7 +22587,7 @@ function pp({
22591
22587
  selectionCreated: y,
22592
22588
  selectionCleared: _
22593
22589
  });
22594
- gp(st);
22590
+ fp(st);
22595
22591
  async function H() {
22596
22592
  globalThis.requestAnimationFrame(async () => {
22597
22593
  if (j(), a.PDFBase64.startsWith("data:image") || a.canvas) {
@@ -22656,7 +22652,7 @@ function pp({
22656
22652
  );
22657
22653
  }
22658
22654
  export {
22659
- vp as default,
22660
- hp as loadFile,
22661
- fp as useFabric
22655
+ bp as default,
22656
+ ap as loadFile,
22657
+ dp as useFabric
22662
22658
  };