@pdfme/ui 6.1.13-dev.1 → 6.1.13-dev.7

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.
@@ -7,5 +7,6 @@ export declare const LEFT_SIDEBAR_WIDTH = 45;
7
7
  export declare const RIGHT_SIDEBAR_WIDTH = 400;
8
8
  export declare const BACKGROUND_COLOR = "rgb(74, 74, 74)";
9
9
  export declare const DEFAULT_MAX_ZOOM = 2;
10
+ export declare const PAGE_SWITCH_REMAINING_RATIO = 0.25;
10
11
  export declare const DESIGNER_CLASSNAME = "pdfme-designer-";
11
12
  export declare const UI_CLASSNAME = "pdfme-ui-";
@@ -0,0 +1,11 @@
1
+ import type { Size } from '@pdfme/common';
2
+ export type ViewportSize = {
3
+ height: number;
4
+ width: number;
5
+ };
6
+ export type ContainerBox = {
7
+ clientHeight: number;
8
+ clientWidth: number;
9
+ getBoundingClientRect: () => Pick<DOMRect, 'bottom' | 'left' | 'right' | 'top'>;
10
+ };
11
+ export declare const measureUiContainerSize: (container: ContainerBox, viewport: ViewportSize) => Size;
package/dist/helper.d.ts CHANGED
@@ -55,6 +55,12 @@ export declare const moveCommandToChangeSchemasArg: (props: {
55
55
  schemaId: string;
56
56
  }[];
57
57
  export declare const getPagesScrollTopByIndex: (pageSizes: Size[], index: number, scale: number) => number;
58
+ export declare const getVisibleOverlap: (containerRect: DOMRect, elementRect: DOMRect) => {
59
+ width: number;
60
+ height: number;
61
+ area: number;
62
+ };
63
+ export declare const getStickyScrollPageIndex: (container: HTMLElement, papers: Array<HTMLElement | null | undefined>, pageCursor: number) => number;
58
64
  export type ZoomMode = 'manual' | 'fit-width' | 'fit-height';
59
65
  export type ZoomAnchor = {
60
66
  pageIndex: number;
package/dist/index.js CHANGED
@@ -56104,9 +56104,29 @@ var import_client = (/* @__PURE__ */ __commonJSMin(((exports, module) => {
56104
56104
  var DESTROYED_ERR_MSG = "[@pdfme/ui] this instance is already destroyed";
56105
56105
  var SELECTABLE_CLASSNAME = "selectable";
56106
56106
  var BACKGROUND_COLOR = "rgb(74, 74, 74)";
56107
+ var PAGE_SWITCH_REMAINING_RATIO = .25;
56107
56108
  var DESIGNER_CLASSNAME = "pdfme-designer-";
56108
56109
  var UI_CLASSNAME = "pdfme-ui-";
56109
56110
  //#endregion
56111
+ //#region src/containerSize.ts
56112
+ var visibleIntersection = (start, end, viewportSize) => Math.max(0, Math.min(end, viewportSize) - Math.max(start, 0));
56113
+ var capOverflowToViewport = (layout, visible, viewportSize) => layout > viewportSize ? Math.min(layout, visible) : layout;
56114
+ var measureUiContainerSize = (container, viewport) => {
56115
+ const layoutWidth = container.clientWidth || viewport.width;
56116
+ const layoutHeight = container.clientHeight || viewport.height;
56117
+ const rect = container.getBoundingClientRect();
56118
+ const visibleWidth = visibleIntersection(rect.left, rect.right, viewport.width);
56119
+ const visibleHeight = visibleIntersection(rect.top, rect.bottom, viewport.height);
56120
+ if (visibleWidth === 0 || visibleHeight === 0) return {
56121
+ height: layoutHeight,
56122
+ width: layoutWidth
56123
+ };
56124
+ return {
56125
+ height: capOverflowToViewport(layoutHeight, visibleHeight, viewport.height),
56126
+ width: capOverflowToViewport(layoutWidth, visibleWidth, viewport.width)
56127
+ };
56128
+ };
56129
+ //#endregion
56110
56130
  //#region ../../node_modules/hotkeys-js/dist/hotkeys-js.js
56111
56131
  /*!
56112
56132
  * hotkeys-js v4.0.7
@@ -77316,6 +77336,44 @@ var moveCommandToChangeSchemasArg = (props) => {
77316
77336
  var getPagesScrollTopByIndex = (pageSizes, index, scale) => {
77317
77337
  return pageSizes.slice(0, index).reduce((acc, cur) => acc + (cur.height * ZOOM + 30 * scale) * scale, 0);
77318
77338
  };
77339
+ var getVisibleOverlap = (containerRect, elementRect) => {
77340
+ const width = Math.max(0, Math.min(containerRect.right, elementRect.right) - Math.max(containerRect.left, elementRect.left));
77341
+ const height = Math.max(0, Math.min(containerRect.bottom, elementRect.bottom) - Math.max(containerRect.top, elementRect.top));
77342
+ return {
77343
+ width,
77344
+ height,
77345
+ area: width * height
77346
+ };
77347
+ };
77348
+ var VERTICAL_SCROLL_EDGE_EPSILON = 1;
77349
+ var isAtVerticalScrollEdge = (container) => {
77350
+ const maxScrollTop = container.scrollHeight - container.clientHeight;
77351
+ if (maxScrollTop <= 0) return false;
77352
+ const { scrollTop } = container;
77353
+ return scrollTop <= VERTICAL_SCROLL_EDGE_EPSILON || scrollTop >= maxScrollTop - VERTICAL_SCROLL_EDGE_EPSILON;
77354
+ };
77355
+ var getStickyScrollPageIndex = (container, papers, pageCursor) => {
77356
+ const containerRect = container.getBoundingClientRect();
77357
+ const stickyHeight = containerRect.height * PAGE_SWITCH_REMAINING_RATIO;
77358
+ let bestPageIndex = pageCursor;
77359
+ let bestVisibleArea = 0;
77360
+ let currentVisibleHeight = 0;
77361
+ let currentVisibleArea = 0;
77362
+ papers.forEach((paper, pageIndex) => {
77363
+ if (!paper) return;
77364
+ const { height, area } = getVisibleOverlap(containerRect, paper.getBoundingClientRect());
77365
+ if (pageIndex === pageCursor) {
77366
+ currentVisibleHeight = height;
77367
+ currentVisibleArea = area;
77368
+ }
77369
+ if (area > bestVisibleArea) {
77370
+ bestVisibleArea = area;
77371
+ bestPageIndex = pageIndex;
77372
+ }
77373
+ });
77374
+ if (bestVisibleArea <= 0) return pageCursor;
77375
+ return !isAtVerticalScrollEdge(container) && currentVisibleArea > 0 && currentVisibleHeight >= stickyHeight ? pageCursor : bestPageIndex;
77376
+ };
77319
77377
  var MIN_ZOOM = .25;
77320
77378
  var FIT_GUTTER = 40;
77321
77379
  var clampZoomLevel = (zoomLevel, maxZoom, minZoom = MIN_ZOOM) => Math.min(Math.max(zoomLevel, minZoom), maxZoom);
@@ -77436,15 +77494,10 @@ var BaseUIClass = class {
77436
77494
  _defineProperty$14(this, "options", {});
77437
77495
  _defineProperty$14(this, "setSize", debounce$2(() => {
77438
77496
  if (!this.domContainer) return;
77439
- const rect = this.domContainer.getBoundingClientRect();
77440
- const vw = window.innerWidth;
77441
- const vh = window.innerHeight;
77442
- const visibleWidth = Math.max(0, Math.min(rect.right, vw) - Math.max(rect.left, 0));
77443
- const visibleHeight = Math.max(0, Math.min(rect.bottom, vh) - Math.max(rect.top, 0));
77444
- this.size = {
77445
- height: visibleHeight,
77446
- width: visibleWidth
77447
- };
77497
+ this.size = measureUiContainerSize(this.domContainer, {
77498
+ height: window.innerHeight,
77499
+ width: window.innerWidth
77500
+ });
77448
77501
  this.render();
77449
77502
  }, 100));
77450
77503
  _defineProperty$14(this, "resizeObserver", new ResizeObserver(this.setSize));
@@ -77454,10 +77507,10 @@ var BaseUIClass = class {
77454
77507
  this.template = cloneDeep$1(template);
77455
77508
  this.options = options;
77456
77509
  const container = this.domContainer;
77457
- this.size = {
77458
- height: container.clientHeight || window.innerHeight,
77459
- width: container.clientWidth || window.innerWidth
77460
- };
77510
+ this.size = measureUiContainerSize(container, {
77511
+ height: window.innerHeight,
77512
+ width: window.innerWidth
77513
+ });
77461
77514
  this.resizeObserver.observe(container);
77462
77515
  const { lang, font } = options;
77463
77516
  if (lang) this.lang = lang;
@@ -88030,7 +88083,7 @@ function getPxValue$5(val) {
88030
88083
  /**
88031
88084
  * Get visible area of element
88032
88085
  */
88033
- function getVisibleArea$6(initArea, scrollerList) {
88086
+ function getVisibleArea$5(initArea, scrollerList) {
88034
88087
  const visibleArea = { ...initArea };
88035
88088
  (scrollerList || []).forEach((ele) => {
88036
88089
  if (ele instanceof HTMLBodyElement || ele instanceof HTMLHtmlElement) return;
@@ -88208,8 +88261,8 @@ function useAlign$5(open, popupEle, target, placement, builtinPlacements, popupA
88208
88261
  const VISIBLE_FIRST = "visibleFirst";
88209
88262
  if (htmlRegion !== "scroll" && htmlRegion !== VISIBLE_FIRST) htmlRegion = VISIBLE;
88210
88263
  const isVisibleFirst = htmlRegion === VISIBLE_FIRST;
88211
- const scrollRegionArea = getVisibleArea$6(scrollRegion, scrollerList);
88212
- const visibleRegionArea = getVisibleArea$6(visibleRegion, scrollerList);
88264
+ const scrollRegionArea = getVisibleArea$5(scrollRegion, scrollerList);
88265
+ const visibleRegionArea = getVisibleArea$5(visibleRegion, scrollerList);
88213
88266
  const visibleArea = htmlRegion === VISIBLE ? visibleRegionArea : scrollRegionArea;
88214
88267
  const adjustCheckVisibleArea = isVisibleFirst ? visibleRegionArea : visibleArea;
88215
88268
  popupElement.style.left = "auto";
@@ -95506,7 +95559,7 @@ function getPxValue$4(val) {
95506
95559
  /**
95507
95560
  * Get visible area of element
95508
95561
  */
95509
- function getVisibleArea$5(initArea, scrollerList) {
95562
+ function getVisibleArea$4(initArea, scrollerList) {
95510
95563
  const visibleArea = { ...initArea };
95511
95564
  (scrollerList || []).forEach((ele) => {
95512
95565
  if (ele instanceof HTMLBodyElement || ele instanceof HTMLHtmlElement) return;
@@ -95684,8 +95737,8 @@ function useAlign$4(open, popupEle, target, placement, builtinPlacements, popupA
95684
95737
  const VISIBLE_FIRST = "visibleFirst";
95685
95738
  if (htmlRegion !== "scroll" && htmlRegion !== VISIBLE_FIRST) htmlRegion = VISIBLE;
95686
95739
  const isVisibleFirst = htmlRegion === VISIBLE_FIRST;
95687
- const scrollRegionArea = getVisibleArea$5(scrollRegion, scrollerList);
95688
- const visibleRegionArea = getVisibleArea$5(visibleRegion, scrollerList);
95740
+ const scrollRegionArea = getVisibleArea$4(scrollRegion, scrollerList);
95741
+ const visibleRegionArea = getVisibleArea$4(visibleRegion, scrollerList);
95689
95742
  const visibleArea = htmlRegion === VISIBLE ? visibleRegionArea : scrollRegionArea;
95690
95743
  const adjustCheckVisibleArea = isVisibleFirst ? visibleRegionArea : visibleArea;
95691
95744
  popupElement.style.left = "auto";
@@ -101632,7 +101685,7 @@ function getPxValue$3(val) {
101632
101685
  /**
101633
101686
  * Get visible area of element
101634
101687
  */
101635
- function getVisibleArea$4(initArea, scrollerList) {
101688
+ function getVisibleArea$3(initArea, scrollerList) {
101636
101689
  const visibleArea = { ...initArea };
101637
101690
  (scrollerList || []).forEach((ele) => {
101638
101691
  if (ele instanceof HTMLBodyElement || ele instanceof HTMLHtmlElement) return;
@@ -101810,8 +101863,8 @@ function useAlign$3(open, popupEle, target, placement, builtinPlacements, popupA
101810
101863
  const VISIBLE_FIRST = "visibleFirst";
101811
101864
  if (htmlRegion !== "scroll" && htmlRegion !== VISIBLE_FIRST) htmlRegion = VISIBLE;
101812
101865
  const isVisibleFirst = htmlRegion === VISIBLE_FIRST;
101813
- const scrollRegionArea = getVisibleArea$4(scrollRegion, scrollerList);
101814
- const visibleRegionArea = getVisibleArea$4(visibleRegion, scrollerList);
101866
+ const scrollRegionArea = getVisibleArea$3(scrollRegion, scrollerList);
101867
+ const visibleRegionArea = getVisibleArea$3(visibleRegion, scrollerList);
101815
101868
  const visibleArea = htmlRegion === VISIBLE ? visibleRegionArea : scrollRegionArea;
101816
101869
  const adjustCheckVisibleArea = isVisibleFirst ? visibleRegionArea : visibleArea;
101817
101870
  popupElement.style.left = "auto";
@@ -103998,7 +104051,7 @@ function getPxValue$2(val) {
103998
104051
  /**
103999
104052
  * Get visible area of element
104000
104053
  */
104001
- function getVisibleArea$3(initArea, scrollerList) {
104054
+ function getVisibleArea$2(initArea, scrollerList) {
104002
104055
  const visibleArea = { ...initArea };
104003
104056
  (scrollerList || []).forEach((ele) => {
104004
104057
  if (ele instanceof HTMLBodyElement || ele instanceof HTMLHtmlElement) return;
@@ -104176,8 +104229,8 @@ function useAlign$2(open, popupEle, target, placement, builtinPlacements, popupA
104176
104229
  const VISIBLE_FIRST = "visibleFirst";
104177
104230
  if (htmlRegion !== "scroll" && htmlRegion !== VISIBLE_FIRST) htmlRegion = VISIBLE;
104178
104231
  const isVisibleFirst = htmlRegion === VISIBLE_FIRST;
104179
- const scrollRegionArea = getVisibleArea$3(scrollRegion, scrollerList);
104180
- const visibleRegionArea = getVisibleArea$3(visibleRegion, scrollerList);
104232
+ const scrollRegionArea = getVisibleArea$2(scrollRegion, scrollerList);
104233
+ const visibleRegionArea = getVisibleArea$2(visibleRegion, scrollerList);
104181
104234
  const visibleArea = htmlRegion === VISIBLE ? visibleRegionArea : scrollRegionArea;
104182
104235
  const adjustCheckVisibleArea = isVisibleFirst ? visibleRegionArea : visibleArea;
104183
104236
  popupElement.style.left = "auto";
@@ -106054,7 +106107,7 @@ function getPxValue$1(val) {
106054
106107
  /**
106055
106108
  * Get visible area of element
106056
106109
  */
106057
- function getVisibleArea$2(initArea, scrollerList) {
106110
+ function getVisibleArea$1(initArea, scrollerList) {
106058
106111
  const visibleArea = { ...initArea };
106059
106112
  (scrollerList || []).forEach((ele) => {
106060
106113
  if (ele instanceof HTMLBodyElement || ele instanceof HTMLHtmlElement) return;
@@ -106232,8 +106285,8 @@ function useAlign$1(open, popupEle, target, placement, builtinPlacements, popupA
106232
106285
  const VISIBLE_FIRST = "visibleFirst";
106233
106286
  if (htmlRegion !== "scroll" && htmlRegion !== VISIBLE_FIRST) htmlRegion = VISIBLE;
106234
106287
  const isVisibleFirst = htmlRegion === VISIBLE_FIRST;
106235
- const scrollRegionArea = getVisibleArea$2(scrollRegion, scrollerList);
106236
- const visibleRegionArea = getVisibleArea$2(visibleRegion, scrollerList);
106288
+ const scrollRegionArea = getVisibleArea$1(scrollRegion, scrollerList);
106289
+ const visibleRegionArea = getVisibleArea$1(visibleRegion, scrollerList);
106237
106290
  const visibleArea = htmlRegion === VISIBLE ? visibleRegionArea : scrollRegionArea;
106238
106291
  const adjustCheckVisibleArea = isVisibleFirst ? visibleRegionArea : visibleArea;
106239
106292
  popupElement.style.left = "auto";
@@ -122369,27 +122422,10 @@ var useZoom = ({ baseScale, maxZoom, pageCursor, pageSizes, containerRef, paperR
122369
122422
  fitHeight: () => fitZoom("fit-height")
122370
122423
  };
122371
122424
  };
122372
- var getVisibleArea$1 = (containerRect, elementRect) => {
122373
- return Math.max(0, Math.min(containerRect.right, elementRect.right) - Math.max(containerRect.left, elementRect.left)) * Math.max(0, Math.min(containerRect.bottom, elementRect.bottom) - Math.max(containerRect.top, elementRect.top));
122374
- };
122375
- var getMostVisiblePageIndex = (container, paperRefs, pageCursor) => {
122376
- const containerRect = container.getBoundingClientRect();
122377
- let bestPageIndex = pageCursor;
122378
- let bestVisibleArea = 0;
122379
- paperRefs.current.forEach((paper, pageIndex) => {
122380
- if (!paper) return;
122381
- const visibleArea = getVisibleArea$1(containerRect, paper.getBoundingClientRect());
122382
- if (visibleArea > bestVisibleArea) {
122383
- bestVisibleArea = visibleArea;
122384
- bestPageIndex = pageIndex;
122385
- }
122386
- });
122387
- return bestVisibleArea > 0 ? bestPageIndex : pageCursor;
122388
- };
122389
122425
  var useScrollPageCursor = ({ ref, paperRefs, pageSizes, scale, pageCursor, onChangePageCursor }) => {
122390
122426
  const onScroll = (0, import_react$9.useCallback)(() => {
122391
122427
  if (!pageSizes[0] || !ref.current) return;
122392
- const _pageCursor = getMostVisiblePageIndex(ref.current, paperRefs, pageCursor);
122428
+ const _pageCursor = getStickyScrollPageIndex(ref.current, paperRefs.current, pageCursor);
122393
122429
  if (_pageCursor !== pageCursor) onChangePageCursor(_pageCursor);
122394
122430
  }, [
122395
122431
  onChangePageCursor,