@plait/core 0.8.0 → 0.10.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.
@@ -1513,17 +1513,15 @@ function clampZoomLevel(zoom, minZoom = 0.2, maxZoom = 4) {
1513
1513
  return zoom < minZoom ? minZoom : zoom > maxZoom ? maxZoom : zoom;
1514
1514
  }
1515
1515
  function getViewBox(board, zoom) {
1516
- const { hideScrollbar } = board.options;
1517
- const scrollBarWidth = hideScrollbar ? 0 : SCROLL_BAR_WIDTH;
1518
- const viewportContainerRect = PlaitBoard.getBoardNativeElement(board).getBoundingClientRect();
1516
+ const boardContainerRectangle = PlaitBoard.getBoardNativeElement(board).getBoundingClientRect();
1519
1517
  const elementHostBBox = getElementHostBBox(board, zoom);
1520
- const horizontalPadding = viewportContainerRect.width / 2;
1521
- const verticalPadding = viewportContainerRect.height / 2;
1518
+ const horizontalPadding = boardContainerRectangle.width / 2;
1519
+ const verticalPadding = boardContainerRectangle.height / 2;
1522
1520
  const viewBox = [
1523
1521
  elementHostBBox.left - horizontalPadding / zoom,
1524
1522
  elementHostBBox.top - verticalPadding / zoom,
1525
- elementHostBBox.right - elementHostBBox.left + (horizontalPadding * 2 - scrollBarWidth) / zoom,
1526
- elementHostBBox.bottom - elementHostBBox.top + (verticalPadding * 2 - scrollBarWidth) / zoom
1523
+ elementHostBBox.right - elementHostBBox.left + (horizontalPadding * 2) / zoom,
1524
+ elementHostBBox.bottom - elementHostBBox.top + (verticalPadding * 2) / zoom
1527
1525
  ];
1528
1526
  return viewBox;
1529
1527
  }
@@ -1636,8 +1634,8 @@ function updateZoom(board, newZoom, isCenter = true) {
1636
1634
  const mousePoint = PlaitBoard.getMovingPoint(board);
1637
1635
  const nativeElement = PlaitBoard.getBoardNativeElement(board);
1638
1636
  const nativeElementRect = nativeElement.getBoundingClientRect();
1639
- const viewportContainerRect = getViewportContainerRect(board);
1640
- let focusPoint = [viewportContainerRect.width / 2, viewportContainerRect.height / 2];
1637
+ const boardContainerRect = PlaitBoard.getBoardNativeElement(board).getBoundingClientRect();
1638
+ let focusPoint = [boardContainerRect.width / 2, boardContainerRect.height / 2];
1641
1639
  if (!isCenter && mousePoint && distanceBetweenPointAndRectangle(mousePoint[0], mousePoint[1], nativeElementRect) === 0) {
1642
1640
  focusPoint = toPoint(mousePoint[0], mousePoint[1], nativeElement);
1643
1641
  }
@@ -1649,13 +1647,18 @@ function updateZoom(board, newZoom, isCenter = true) {
1649
1647
  updateViewport(board, newOrigination, newZoom);
1650
1648
  }
1651
1649
  function fitViewport(board) {
1652
- const nativeElementRect = PlaitBoard.getBoardNativeElement(board).getBoundingClientRect();
1653
- const viewportContainerRect = getViewportContainerRect(board);
1650
+ const { hideScrollbar } = board.options;
1651
+ let scrollBarWidth = 0;
1652
+ if (!hideScrollbar) {
1653
+ const viewportContainer = PlaitBoard.getViewportContainer(board);
1654
+ scrollBarWidth = viewportContainer.offsetWidth - viewportContainer.clientWidth;
1655
+ }
1656
+ const boardContainerRect = PlaitBoard.getBoardNativeElement(board).getBoundingClientRect();
1654
1657
  const elementHostBox = getRectangleByElements(board, board.children, true);
1655
1658
  const zoom = board.viewport.zoom;
1656
1659
  const autoFitPadding = 16;
1657
- const viewportWidth = nativeElementRect.width - 2 * autoFitPadding;
1658
- const viewportHeight = nativeElementRect.height - 2 * autoFitPadding;
1660
+ const viewportWidth = boardContainerRect.width - 2 * autoFitPadding;
1661
+ const viewportHeight = boardContainerRect.height - 2 * autoFitPadding;
1659
1662
  let newZoom = zoom;
1660
1663
  if (viewportWidth < elementHostBox.width || viewportHeight < elementHostBox.height) {
1661
1664
  newZoom = Math.min(viewportWidth / elementHostBox.width, viewportHeight / elementHostBox.height);
@@ -1667,8 +1670,8 @@ function fitViewport(board) {
1667
1670
  const centerX = viewBox[0] + viewBox[2] / 2;
1668
1671
  const centerY = viewBox[1] + viewBox[3] / 2;
1669
1672
  const newOrigination = [
1670
- centerX - viewportContainerRect.width / 2 / newZoom,
1671
- centerY - viewportContainerRect.height / 2 / newZoom
1673
+ centerX - boardContainerRect.width / 2 / newZoom + scrollBarWidth / 2 / zoom,
1674
+ centerY - boardContainerRect.height / 2 / newZoom + scrollBarWidth / 2 / zoom
1672
1675
  ];
1673
1676
  updateViewport(board, newOrigination, newZoom);
1674
1677
  }
@@ -1776,6 +1779,7 @@ function createBoard(children, options) {
1776
1779
  onChange: () => { },
1777
1780
  mousedown: (event) => { },
1778
1781
  mousemove: (event) => { },
1782
+ mouseleave: (event) => { },
1779
1783
  globalMousemove: (event) => { },
1780
1784
  mouseup: (event) => { },
1781
1785
  globalMouseup: (event) => { },
@@ -1973,7 +1977,6 @@ function withSelection(board) {
1973
1977
  const range = { anchor: point, focus: point };
1974
1978
  const hitElements = getHitElements(board, { ranges: [range] });
1975
1979
  if (PlaitBoard.isPointer(board, PlaitPointerType.selection) &&
1976
- getHitElements(board, { ranges: [range] }).length === 0 &&
1977
1980
  hitElements.length === 0 &&
1978
1981
  options.isMultiple &&
1979
1982
  !options.isDisabledSelect) {
@@ -2175,19 +2178,22 @@ function withMoving(board) {
2175
2178
  const endPoint = transformPoint(board, toPoint(event.x, event.y, host));
2176
2179
  offsetX = endPoint[0] - startPoint[0];
2177
2180
  offsetY = endPoint[1] - startPoint[1];
2178
- throttleRAF(() => {
2179
- const currentElements = activeElements.map(activeElement => {
2180
- const [x, y] = activeElement?.points[0];
2181
- const index = board.children.findIndex(item => item.id === activeElement.id);
2182
- Transforms.setNode(board, {
2183
- points: [[x + offsetX, y + offsetY]]
2184
- }, [index]);
2185
- MERGING.set(board, true);
2186
- return PlaitNode.get(board, [index]);
2181
+ const offsetBuffer = 5;
2182
+ if (Math.abs(offsetX) > offsetBuffer || Math.abs(offsetY) > offsetBuffer) {
2183
+ throttleRAF(() => {
2184
+ const currentElements = activeElements.map(activeElement => {
2185
+ const [x, y] = activeElement?.points[0];
2186
+ const index = board.children.findIndex(item => item.id === activeElement.id);
2187
+ Transforms.setNode(board, {
2188
+ points: [[x + offsetX, y + offsetY]]
2189
+ }, [index]);
2190
+ MERGING.set(board, true);
2191
+ return PlaitNode.get(board, [index]);
2192
+ });
2193
+ PlaitBoard.getBoardNativeElement(board).classList.add('element-moving');
2194
+ addMovingElements(board, currentElements);
2187
2195
  });
2188
- PlaitBoard.getBoardNativeElement(board).classList.add('element-moving');
2189
- addMovingElements(board, currentElements);
2190
- });
2196
+ }
2191
2197
  }
2192
2198
  if (isPreventDefault) {
2193
2199
  // 阻止 move 过程中触发画布滚动行为
@@ -2464,7 +2470,6 @@ class PlaitBoardComponent {
2464
2470
  this.initializeHookListener();
2465
2471
  this.viewportScrollListener();
2466
2472
  this.elementResizeListener();
2467
- this.mouseLeaveListener();
2468
2473
  });
2469
2474
  BOARD_TO_COMPONENT.set(this.board, this);
2470
2475
  BOARD_TO_ROUGH_SVG.set(this.board, roughSVG);
@@ -2535,6 +2540,12 @@ class PlaitBoardComponent {
2535
2540
  BOARD_TO_MOVING_POINT.set(this.board, [event.x, event.y]);
2536
2541
  this.board.mousemove(event);
2537
2542
  });
2543
+ fromEvent(this.host, 'mouseleave')
2544
+ .pipe(takeUntil(this.destroy$))
2545
+ .subscribe((event) => {
2546
+ BOARD_TO_MOVING_POINT.delete(this.board);
2547
+ this.board.mouseleave(event);
2548
+ });
2538
2549
  fromEvent(document, 'mousemove')
2539
2550
  .pipe(takeUntil(this.destroy$))
2540
2551
  .subscribe((event) => {
@@ -2640,13 +2651,6 @@ class PlaitBoardComponent {
2640
2651
  });
2641
2652
  this.resizeObserver.observe(this.nativeElement);
2642
2653
  }
2643
- mouseLeaveListener() {
2644
- fromEvent(this.host, 'mouseleave')
2645
- .pipe(takeUntil(this.destroy$))
2646
- .subscribe((event) => {
2647
- BOARD_TO_MOVING_POINT.delete(this.board);
2648
- });
2649
- }
2650
2654
  initializeIslands() {
2651
2655
  this.islands?.forEach(island => {
2652
2656
  island.initialize(this.board);