@pdfme/ui 6.1.5-dev.14 → 6.1.5-dev.15

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.js CHANGED
@@ -54485,10 +54485,10 @@ if (typeof window !== "undefined") {
54485
54485
  window.hotkeys = hotkeys$1;
54486
54486
  }
54487
54487
  //#endregion
54488
- //#region ../converter/dist/img2pdf-CTJv8er1.js
54488
+ //#region ../converter/dist/img2pdf-BWYwe2Xi.js
54489
54489
  async function pdf2img$1(pdf, options = {}, env) {
54490
54490
  try {
54491
- const { scale = 1, imageType = "jpeg", range = {} } = options;
54491
+ const { scale = 1, imageType = "jpeg", range = {}, maxCanvasPixels } = options;
54492
54492
  const { start = 0, end = Infinity } = range;
54493
54493
  const { getDocument, destroyDocument, createCanvas, canvasToArrayBuffer } = env;
54494
54494
  const pdfDoc = await getDocument(pdf);
@@ -54499,7 +54499,13 @@ async function pdf2img$1(pdf, options = {}, env) {
54499
54499
  const results = [];
54500
54500
  for (let pageNum = startPage; pageNum <= endPage; pageNum++) {
54501
54501
  const page = await pdfDoc.getPage(pageNum);
54502
- const viewport = page.getViewport({ scale });
54502
+ let renderScale = scale;
54503
+ if (maxCanvasPixels && maxCanvasPixels > 0) {
54504
+ const baseViewport = page.getViewport({ scale: 1 });
54505
+ const baseArea = baseViewport.width * baseViewport.height;
54506
+ if (baseArea > 0) renderScale = Math.min(scale, Math.sqrt(maxCanvasPixels / baseArea));
54507
+ }
54508
+ const viewport = page.getViewport({ scale: renderScale });
54503
54509
  const canvas = createCanvas(viewport.width, viewport.height);
54504
54510
  if (!canvas) throw new Error("Failed to create canvas");
54505
54511
  const context = canvas.getContext("2d");
@@ -102370,9 +102376,10 @@ function detectMimeType(arrayBuffer) {
102370
102376
  var arrayBufferToBase64 = (arrayBuffer) => {
102371
102377
  const mimeType = detectMimeType(arrayBuffer);
102372
102378
  const bytes = new Uint8Array(arrayBuffer);
102373
- let binary = "";
102374
- for (let i = 0; i < bytes.length; i++) binary += String.fromCharCode(bytes[i]);
102375
- const base64String = btoa(binary);
102379
+ const chunkSize = 32768;
102380
+ const chunks = [];
102381
+ for (let i = 0; i < bytes.length; i += chunkSize) chunks.push(String.fromCharCode.apply(null, bytes.subarray(i, i + chunkSize)));
102382
+ const base64String = btoa(chunks.join(""));
102376
102383
  if (mimeType) return `data:${mimeType};base64,${base64String}`;
102377
102384
  else return `data:application/octet-stream;base64,${base64String}`;
102378
102385
  };
@@ -132426,6 +132433,8 @@ var usePrevious = (value) => {
132426
132433
  return ref.current;
132427
132434
  };
132428
132435
  var getScale = (n, paper) => Math.floor((n / paper > 1 ? 1 : n / paper) * 100) / 100;
132436
+ var MIN_BASE_SCALE = .01;
132437
+ var MAX_BACKGROUND_CANVAS_PIXELS = 4096 * 4096;
132429
132438
  var useUIPreProcessor = ({ template, size, zoomLevel, maxZoom }) => {
132430
132439
  const [backgrounds, setBackgrounds] = (0, import_react$9.useState)([]);
132431
132440
  const [pageSizes, setPageSizes] = (0, import_react$9.useState)([]);
@@ -132456,13 +132465,16 @@ var useUIPreProcessor = ({ template, size, zoomLevel, maxZoom }) => {
132456
132465
  return buffer;
132457
132466
  };
132458
132467
  const [pageSizeBuffer, imageBuffer] = [createPdfArrayBuffer(), createPdfArrayBuffer()];
132459
- const [_pages, imgBuffers] = await Promise.all([pdf2size(pageSizeBuffer), pdf2img(imageBuffer, { scale: maxZoom })]);
132468
+ const [_pages, imgBuffers] = await Promise.all([pdf2size(pageSizeBuffer), pdf2img(imageBuffer, {
132469
+ scale: maxZoom,
132470
+ maxCanvasPixels: MAX_BACKGROUND_CANVAS_PIXELS
132471
+ })]);
132460
132472
  _pageSizes = _pages;
132461
132473
  paperWidth = _pageSizes[0].width * ZOOM;
132462
132474
  paperHeight = _pageSizes[0].height * ZOOM;
132463
132475
  _backgrounds = imgBuffers.map(arrayBufferToBase64);
132464
132476
  }
132465
- const _scale = Math.min(getScale(size.width, paperWidth), getScale(size.height - 30, paperHeight));
132477
+ const _scale = Math.max(MIN_BASE_SCALE, Math.min(getScale(size.width, paperWidth), getScale(size.height - 30, paperHeight)));
132466
132478
  return {
132467
132479
  backgrounds: _backgrounds,
132468
132480
  pageSizes: _pageSizes,
@@ -236634,6 +236646,7 @@ var scaleDragPosAdjustment = (adjustment, scale) => {
236634
236646
  if (scale < 1) return adjustment * -(1 - scale);
236635
236647
  return 0;
236636
236648
  };
236649
+ var MIN_CANVAS_WIDTH = 100;
236637
236650
  var TemplateEditor = ({ template, size, onSaveTemplate, onChangeTemplate, onPageCursorChange, onChangeSelection, onRegisterSchemaSelectionHandler }) => {
236638
236651
  const past = (0, import_react$9.useRef)([]);
236639
236652
  const future = (0, import_react$9.useRef)([]);
@@ -236643,16 +236656,16 @@ var TemplateEditor = ({ template, size, onSaveTemplate, onChangeTemplate, onPage
236643
236656
  const pluginsRegistry = (0, import_react$9.useContext)(PluginsRegistry);
236644
236657
  const options = (0, import_react$9.useContext)(OptionsContext);
236645
236658
  const maxZoom = useMaxZoom();
236659
+ const canvasWidth = size.width - 45;
236646
236660
  const [hoveringSchemaId, setHoveringSchemaId] = (0, import_react$9.useState)(null);
236647
236661
  const [activeElements, setActiveElements] = (0, import_react$9.useState)([]);
236648
236662
  const [schemasList, setSchemasList] = (0, import_react$9.useState)([[]]);
236649
236663
  const [pageCursor, setPageCursor] = (0, import_react$9.useState)(0);
236650
- const [sidebarOpen, setSidebarOpen] = (0, import_react$9.useState)(options.sidebarOpen ?? true);
236664
+ const [sidebarOpen, setSidebarOpen] = (0, import_react$9.useState)(options.sidebarOpen ?? canvasWidth - 400 >= MIN_CANVAS_WIDTH);
236651
236665
  const [canvasHeight, setCanvasHeight] = (0, import_react$9.useState)(0);
236652
236666
  const [prevTemplate, setPrevTemplate] = (0, import_react$9.useState)(null);
236653
- const canvasWidth = size.width - 45;
236654
236667
  const sizeExcSidebars = (0, import_react$9.useMemo)(() => ({
236655
- width: sidebarOpen ? canvasWidth - 400 : canvasWidth,
236668
+ width: Math.max(sidebarOpen ? canvasWidth - 400 : canvasWidth, 0),
236656
236669
  height: size.height
236657
236670
  }), [
236658
236671
  canvasWidth,