Skip to content

Commit

Permalink
feat(core): override isHit method support isStrict param and isHitDra…
Browse files Browse the repository at this point in the history
…wElement support isStrict to match dblClick editing scene
  • Loading branch information
pubuzhixing8 committed Dec 25, 2024
1 parent 03be33a commit 4036ede
Show file tree
Hide file tree
Showing 11 changed files with 47 additions and 34 deletions.
5 changes: 5 additions & 0 deletions .changeset/silver-kings-begin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@plait/draw': minor
---

isHitDrawElement support isStrict mode to match dblClick editing scene(isStrict is false)
5 changes: 5 additions & 0 deletions .changeset/thick-apricots-scream.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@plait/core': minor
---

override isHit method support isStrict param
2 changes: 1 addition & 1 deletion packages/core/src/interfaces/board.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export interface PlaitBoard {
drawElement: (context: PlaitPluginElementContext) => ComponentType<ElementFlavour>;
isRectangleHit: (element: PlaitElement, range: Selection) => boolean;
// When the element has no fill color, it is considered a hit only if it hits the border.
isHit: (element: PlaitElement, point: Point) => boolean;
isHit: (element: PlaitElement, point: Point, isStrict?: boolean) => boolean;
isInsidePoint: (element: PlaitElement, point: Point) => boolean;
// the hit element is determined by the plugin
getOneHitElement: (hitElements: PlaitElement[]) => PlaitElement;
Expand Down
22 changes: 12 additions & 10 deletions packages/core/src/utils/selected-element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export const getHitElementsBySelection = (
}
depthFirstRecursion<Ancestor>(
board,
node => {
(node) => {
if (!PlaitBoard.isBoard(node) && match(node)) {
let isRectangleHit = false;
try {
Expand All @@ -57,18 +57,19 @@ export const getHitElementsBySelection = (
export const getHitElementsByPoint = (
board: PlaitBoard,
point: Point,
match: (element: PlaitElement) => boolean = () => true
match: (element: PlaitElement) => boolean = () => true,
isStrict = true
): PlaitElement[] => {
let hitElements: PlaitElement[] = [];
depthFirstRecursion<Ancestor>(
board,
node => {
(node) => {
if (PlaitBoard.isBoard(node) || !match(node) || !PlaitElement.hasMounted(node)) {
return;
}
let isHit = false;
try {
isHit = board.isHit(node, point);
isHit = board.isHit(node, point, isStrict);
} catch (error) {
if (isDebug()) {
console.error('isHit', error, 'node', node);
Expand All @@ -88,9 +89,10 @@ export const getHitElementsByPoint = (
export const getHitElementByPoint = (
board: PlaitBoard,
point: Point,
match: (element: PlaitElement) => boolean = () => true
match: (element: PlaitElement) => boolean = () => true,
isStrict = true
): undefined | PlaitElement => {
const pointHitElements = getHitElementsByPoint(board, point, match);
const pointHitElements = getHitElementsByPoint(board, point, match, isStrict);
const hitElement = board.getOneHitElement(pointHitElements);
return hitElement;
};
Expand Down Expand Up @@ -133,15 +135,15 @@ export const removeSelectedElement = (board: PlaitBoard, element: PlaitElement,
if (board.isRecursion(element) && isRemoveChildren) {
depthFirstRecursion(
element,
node => {
(node) => {
targetElements.push(node);
},
node => board.isRecursion(node)
(node) => board.isRecursion(node)
);
} else {
targetElements.push(element);
}
const newSelectedElements = selectedElements.filter(value => !targetElements.includes(value));
const newSelectedElements = selectedElements.filter((value) => !targetElements.includes(value));
cacheSelectedElements(board, newSelectedElements);
}
};
Expand All @@ -157,7 +159,7 @@ export const clearSelectedElement = (board: PlaitBoard) => {

export const isSelectedElement = (board: PlaitBoard, element: PlaitElement) => {
const selectedElements = getSelectedElements(board);
return !!selectedElements.find(value => value === element);
return !!selectedElements.find((value) => value === element);
};

export const temporaryDisableSelection = (board: PlaitOptionsBoard) => {
Expand Down
4 changes: 2 additions & 2 deletions packages/draw/src/plugins/with-draw-hotkey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ export const withDrawHotkey = (board: PlaitBoard) => {
event.preventDefault();
if (!PlaitBoard.isReadonly(board)) {
const point = toViewBoxPoint(board, toHostPoint(board, event.x, event.y));
const hitElement = getHitElementByPoint(board, point);
const hitElement = getHitElementByPoint(board, point, undefined, false);
if (hitElement && PlaitDrawElement.isGeometry(hitElement)) {
if (isMultipleTextGeometry(hitElement)) {
const hitText =
getHitMultipleGeometryText(hitElement, point) ||
hitElement.texts.find(item => item.id.includes(GeometryCommonTextKeys.content)) ||
hitElement.texts.find((item) => item.id.includes(GeometryCommonTextKeys.content)) ||
hitElement.texts[0];
editText(board, hitElement, hitText);
} else {
Expand Down
6 changes: 3 additions & 3 deletions packages/draw/src/plugins/with-draw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,12 @@ export const withDraw = (board: PlaitBoard) => {
return isRectangleHit(element, selection);
};

board.isHit = (element, point) => {
const result = isHitDrawElement(board, element, point);
board.isHit = (element, point, isStrict?: boolean) => {
const result = isHitDrawElement(board, element, point, isStrict);
if (result !== null) {
return result;
}
return isHit(element, point);
return isHit(element, point, isStrict);
};

board.getOneHitElement = elements => {
Expand Down
4 changes: 2 additions & 2 deletions packages/draw/src/plugins/with-table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ export const withTable = (board: PlaitBoard) => {
return drawElement(context);
};

tableBoard.isHit = (element, point) => {
tableBoard.isHit = (element, point, isStrict?: boolean) => {
if (PlaitDrawElement.isElementByTable(element)) {
const client = RectangleClient.getRectangleByPoints(element.points);
return RectangleClient.isPointInRectangle(client, point);
}
return isHit(element, point);
return isHit(element, point, isStrict);
};

tableBoard.getRectangle = (element: PlaitElement) => {
Expand Down
21 changes: 11 additions & 10 deletions packages/draw/src/utils/geometry.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
import { PlaitBoard, PlaitElement, Point, RectangleClient, ThemeColorMode, getSelectedElements, idCreator } from '@plait/core';
import {
PlaitBoard,
PlaitElement,
Point,
RectangleClient,
ThemeColorMode,
getSelectedElements,
idCreator
} from '@plait/core';
import { GeometryShapes, BasicShapes, PlaitGeometry, FlowchartSymbols, UMLSymbols } from '../interfaces/geometry';
import { Element } from 'slate';
import {
Expand Down Expand Up @@ -273,7 +281,7 @@ export const getAutoCompletePoints = (element: PlaitShapeElement) => {
};

export const getHitIndexOfAutoCompletePoint = (movingPoint: Point, points: Point[]) => {
return points.findIndex(point => {
return points.findIndex((point) => {
const movingRectangle = RectangleClient.getRectangleByPoints([movingPoint]);
let rectangle = RectangleClient.getRectangleByPoints([point]);
rectangle = RectangleClient.inflate(rectangle, RESIZE_HANDLE_DIAMETER);
Expand Down Expand Up @@ -362,15 +370,8 @@ export const createDefaultGeometry = (board: PlaitBoard, points: [Point, Point],
export const editText = (board: PlaitBoard, element: PlaitGeometry, text?: DrawTextInfo) => {
const textManage = text ? getTextManage(board, element, text) : getFirstTextManage(element);
if (textManage) {
textManage.edit(() => {
// delay to avoid blinking
setTimeout(() => {
rerenderGeometryActive(board, element);
}, 200);
});
textManage.edit(() => {});
}

rerenderGeometryActive(board, element);
};

export const rerenderGeometryActive = (board: PlaitBoard, element: PlaitGeometry) => {
Expand Down
4 changes: 2 additions & 2 deletions packages/draw/src/utils/hit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ export const getFirstTextOrLineElement = (elements: PlaitElement[]) => {
return null;
};

export const isHitDrawElement = (board: PlaitBoard, element: PlaitElement, point: Point) => {
export const isHitDrawElement = (board: PlaitBoard, element: PlaitElement, point: Point, isStrict: boolean = true) => {
const rectangle = board.getRectangle(element);
point = rotateAntiPointsByElement(point, element) || point;
if (PlaitDrawElement.isGeometry(element)) {
Expand All @@ -199,7 +199,7 @@ export const isHitDrawElement = (board: PlaitBoard, element: PlaitElement, point
const textClient = getTextRectangle(element);
return RectangleClient.isPointInRectangle(textClient, point);
}
if (isEmptyTextElement(element) && !isFilledDrawElement(board, element)) {
if (!!isStrict && isEmptyTextElement(element) && !isFilledDrawElement(board, element)) {
return false;
}
const isHitText = isHitElementText(element, point);
Expand Down
4 changes: 2 additions & 2 deletions packages/flow/src/plugins/with-flow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export const withFlow: PlaitPlugin = (board: PlaitBoard) => {
return isRectangleHit(element, range);
};

board.isHit = (element, point) => {
board.isHit = (element, point, isStrict?: boolean) => {
if (!board.options.readonly) {
if (FlowElement.isFlowElement(element) && PlaitElement.hasMounted(element)) {
if (FlowNode.isFlowNodeElement(element)) {
Expand All @@ -60,7 +60,7 @@ export const withFlow: PlaitPlugin = (board: PlaitBoard) => {
}
}
}
return isHit(element, point);
return isHit(element, point, isStrict);
};

board.isMovable = element => {
Expand Down
4 changes: 2 additions & 2 deletions packages/mind/src/plugins/with-mind.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,13 @@ export const withMind = (baseBoard: PlaitBoard) => {
return isRectangleHit(element, selection);
};

board.isHit = (element, point: Point) => {
board.isHit = (element, point: Point, isStrict?: boolean) => {
if (MindElement.isMindElement(board, element)) {
const client = getRectangleByNode(MindElement.getNode(element));
const isHit = RectangleClient.isHit(RectangleClient.getRectangleByPoints([point, point]), client);
return isHit;
}
return isHit(element, point);
return isHit(element, point, isStrict);
};

board.getOneHitElement = elements => {
Expand Down

0 comments on commit 4036ede

Please sign in to comment.