diff --git a/.changeset/chilled-ears-smash.md b/.changeset/chilled-ears-smash.md new file mode 100644 index 000000000..4aaad7af4 --- /dev/null +++ b/.changeset/chilled-ears-smash.md @@ -0,0 +1,5 @@ +--- +'@plait/core': patch +--- + +fix select elements error after move node diff --git a/packages/core/src/transforms/group.spec.ts b/packages/core/src/transforms/group.spec.ts index 0338296cc..bfa841038 100644 --- a/packages/core/src/transforms/group.spec.ts +++ b/packages/core/src/transforms/group.spec.ts @@ -1,6 +1,6 @@ import { PlaitBoard, PlaitElement, PlaitGroupElement } from '../interfaces'; import { clearNodeWeakMap, createTestingBoard, fakeNodeWeakMap } from '../testing'; -import { addSelectedElement } from '../utils'; +import { addSelectedElement, getSelectedElements } from '../utils'; import { GroupTransforms } from './group'; const children: PlaitElement[] = [ @@ -96,7 +96,7 @@ const children: PlaitElement[] = [ groupId: 'JDQpa' }, { - id: 'DPMaD', + id: 'DPMsD', type: 'geometry', shape: 'rectangle', angle: 0, @@ -161,4 +161,15 @@ describe('group transform', () => { const elementIngroup = board.children.filter(item => item.groupId); expect(elementIngroup.length).toBe(0); }); + + it('should correctly moving elements when selecting elements from different layers for grouping.', () => { + addSelectedElement(board, [children[0], children[1], children[4], children[5]]); + GroupTransforms.addGroup(board); + expect(board.children[2].id).toBe(children[0].id); + expect(board.children[3].id).toBe(children[1].id); + expect(board.children[4].id).toBe(children[4].id); + expect(board.children[5].id).toBe(children[5].id); + const selectedElements = getSelectedElements(board); + expect(selectedElements.length).toBe(4); + }); }); diff --git a/packages/core/src/utils/group.ts b/packages/core/src/utils/group.ts index 201222c7c..aa2cdb1e0 100644 --- a/packages/core/src/utils/group.ts +++ b/packages/core/src/utils/group.ts @@ -5,7 +5,7 @@ import { createG, setAngleForG } from './dom'; import { drawRectangle } from './drawing/rectangle'; import { getRectangleByElements } from './element'; import { idCreator } from './id-creator'; -import { getSelectedElements } from './selected-element'; +import { cacheSelectedElements, getSelectedElements } from './selected-element'; import { isSelectionMoving } from './selection'; import { depthFirstRecursion } from './tree'; import { moveElementsToNewPath } from './common'; @@ -250,7 +250,8 @@ export const getEditingGroup = (board: PlaitBoard, element: PlaitElement) => { return editingGroup; }; -export const moveElementsToNewPathAfterAddGroup = (board: PlaitBoard, moveElements: PlaitElement[], newPath: Path) => { +export const moveElementsToNewPathAfterAddGroup = (board: PlaitBoard, selectedElements: PlaitElement[], newPath: Path) => { + const moveElements = [...selectedElements]; sortElements(board, moveElements); moveElements.pop(); moveElementsToNewPath(