Skip to content

Commit

Permalink
feat: 增加节点对比
Browse files Browse the repository at this point in the history
  • Loading branch information
Yan Heng committed Aug 2, 2023
1 parent e9fd695 commit cf44112
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
5 changes: 4 additions & 1 deletion packages/core/src/services/selector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import Konva from 'konva';
import { App } from '../app';
import { Rect } from '../customs/rect';
import { ChildType, EventArgs, Service } from '../types';
import { Point } from '../utils';
import { Point, shapeArrayEqual } from '../utils';

export class Selector extends Service {
public selected: Map<number | string, ChildType>;
Expand Down Expand Up @@ -86,6 +86,9 @@ export class Selector extends Service {
if (!this.enable) {
return;
}
if (shapeArrayEqual(children, [...this.selected.values()])) {
return;
}
this.cancelSelect();
children.forEach((child) => {
child.draggable(true);
Expand Down
27 changes: 26 additions & 1 deletion packages/core/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AppConfig } from '../types';
import { AppConfig, ChildType } from '../types';

export * from './math';

Expand All @@ -7,3 +7,28 @@ export { guid, readeFile, selectFile } from '@pictode/utils';
export const DEFAULT_APP_CONFIG: AppConfig = {
backgroundColor: '#ffffff',
};

export const shapeArrayEqual = (arr1: ChildType[], arr2: ChildType[]): boolean => {
if (arr1.length !== arr2.length) {
return false; // 长度不同,两个数组肯定不相同
}

for (let i = 0; i < arr1.length; i++) {
const obj1 = arr1[i];
const obj2 = arr2.find((item) => item.id === obj1.id);

if (!obj2) {
return false;
}

if (!isShapeEqual(obj1, obj2)) {
return false;
}
}

return true;
};

const isShapeEqual = (obj1: ChildType, obj2: ChildType): boolean => {
return obj1.id() === obj2.id() && obj1._id === obj2._id;
};

0 comments on commit cf44112

Please sign in to comment.