Skip to content

Commit

Permalink
feat(core): add moveToCenter utility functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Xwatson authored and pubuzhixing8 committed Jul 11, 2024
1 parent 611b1ba commit 6ec85fe
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/nervous-owls-sip.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@plait/core': minor
---

add moveToCenter utility functions
28 changes: 26 additions & 2 deletions packages/core/src/transforms/board.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
initializeViewportContainer
} from '../utils/viewport';
import { setViewport } from './viewport';
import { depthFirstRecursion, getRealScrollBarWidth } from '../utils';
import { depthFirstRecursion, getRealScrollBarWidth, getSelectedElements } from '../utils';
import { PlaitElement } from '../interfaces/element';
import { setTheme } from './theme';
import { FitViewportOptions } from '../interfaces/viewport';
Expand Down Expand Up @@ -131,11 +131,35 @@ const updatePointerType = <T extends string = PlaitPointerType>(board: PlaitBoar
board.pointer = pointer;
};

function moveToCenter(board: PlaitBoard, centerPoint: Point) {
const plaitElement = getSelectedElements(board)?.[0];
if (plaitElement) {
const boardContainerRect = PlaitBoard.getBoardContainer(board).getBoundingClientRect();
const scrollBarWidth = getRealScrollBarWidth(board);
const oldCenterPoint = getViewBoxCenterPoint(board);
const left = centerPoint[0] - oldCenterPoint[0];
const top = centerPoint[1] - oldCenterPoint[1];
const zoom = board.viewport.zoom;

const origination = [
left - boardContainerRect.width / 2 / zoom + scrollBarWidth / 2 / zoom,
top - boardContainerRect.height / 2 / zoom + scrollBarWidth / 2 / zoom
] as Point;

setViewport(board, {
...board.viewport,
origination
});
clearViewportOrigination(board);
}
}

export const BoardTransforms = {
updatePointerType,
updateViewport,
fitViewport,
updateZoom,
updateThemeColor,
fitViewportWidth
fitViewportWidth,
moveToCenter
};

0 comments on commit 6ec85fe

Please sign in to comment.