Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(draw): support render snap line when create and refactor snap handle #OSP-707 #810

Merged
merged 9 commits into from
Apr 11, 2024
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/three-emus-buy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@plait/draw': patch
'@plait/core': patch
---

support render snap line when create
refactor snap handle
27 changes: 12 additions & 15 deletions packages/core/src/plugins/with-moving.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {
getRectangleByAngle,
getSelectionAngle
} from '../utils';
import { MovingSnapReaction } from '../utils/moving-snap';
import { getSnapMovingRef } from '../utils/snap/snap-moving';
import { PlaitGroupElement, PlaitPointerType, RectangleClient } from '../interfaces';
import { ACTIVE_MOVING_CLASS_NAME, PRESS_AND_MOVE_BUFFER } from '../constants';
import { addSelectionWithTemporaryElements } from '../transforms/selection';
Expand All @@ -35,7 +35,7 @@ export function withMoving(board: PlaitBoard) {
let isPreventDefault = false;
let startPoint: Point | null;
let activeElements: PlaitElement[] = [];
let alignG: SVGGElement | null = null;
let snapG: SVGGElement | null = null;
let activeElementsRectangle: RectangleClient | null = null;
let selectedTargetElements: PlaitElement[] | null = null;
let hitTargetElement: PlaitElement | undefined = undefined;
Expand Down Expand Up @@ -86,7 +86,7 @@ export function withMoving(board: PlaitBoard) {
if (!isPreventDefault) {
isPreventDefault = true;
}
alignG?.remove();
snapG?.remove();
const endPoint = toViewBoxPoint(board, toHostPoint(board, event.x, event.y));
offsetX = endPoint[0] - startPoint[0];
offsetY = endPoint[1] - startPoint[1];
Expand All @@ -107,17 +107,14 @@ export function withMoving(board: PlaitBoard) {
x: activeElementsRectangle.x + offsetX,
y: activeElementsRectangle.y + offsetY
};
const movingSnapReaction = new MovingSnapReaction(
board,
activeElements,
getRectangleByAngle(newRectangle, getSelectionAngle(activeElements)) || newRectangle
);
const ref = movingSnapReaction.handleSnapping();
offsetX -= ref.deltaX;
offsetY -= ref.deltaY;
alignG = ref.g;
alignG.classList.add(ACTIVE_MOVING_CLASS_NAME);
PlaitBoard.getElementActiveHost(board).append(alignG);

const activeRectangle = getRectangleByAngle(newRectangle, getSelectionAngle(activeElements)) || newRectangle;
const ref = getSnapMovingRef(board, activeRectangle, activeElements);
offsetX += ref.deltaX;
offsetY += ref.deltaY;
snapG = ref.snapG;
snapG.classList.add(ACTIVE_MOVING_CLASS_NAME);
PlaitBoard.getElementActiveHost(board).append(snapG);
handleTouchTarget(board);
const currentElements = updatePoints(board, activeElements, offsetX, offsetY);
PlaitBoard.getBoardContainer(board).classList.add('element-moving');
Expand Down Expand Up @@ -155,7 +152,7 @@ export function withMoving(board: PlaitBoard) {
};

function cancelMove(board: PlaitBoard) {
alignG?.remove();
snapG?.remove();
startPoint = null;
activeElementsRectangle = null;
offsetX = 0;
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,4 @@ export * from './group';
export * from './selection';
export * from './angle';
export * from './fragment';
export * from './snap/snap';
Loading