From d18679dbe1b65046d188de4414c0a5703f0dd5d2 Mon Sep 17 00:00:00 2001 From: Opportunity Date: Sat, 2 Oct 2021 17:56:36 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E2=9C=A8=20add=20batch=20for=20selecti?= =?UTF-8?q?on?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/x6/src/addon/selection/index.ts | 25 ++++++++++++++++++------ packages/x6/src/graph/graph.ts | 15 ++++++++------ packages/x6/src/graph/selection.ts | 15 ++++++++------ 3 files changed, 37 insertions(+), 18 deletions(-) diff --git a/packages/x6/src/addon/selection/index.ts b/packages/x6/src/addon/selection/index.ts index c20e4a93632..abb452393d8 100644 --- a/packages/x6/src/addon/selection/index.ts +++ b/packages/x6/src/addon/selection/index.ts @@ -173,22 +173,27 @@ export class Selection extends View { return this.collection.toArray() } - select(cells: Cell | Cell[], options: Collection.AddOptions = {}) { + select(cells: Cell | Cell[], options: Selection.AddOptions = {}) { options.dryrun = true const items = this.filter(Array.isArray(cells) ? cells : [cells]) this.collection.add(items, options) return this } - unselect(cells: Cell | Cell[], options: Collection.RemoveOptions = {}) { + unselect(cells: Cell | Cell[], options: Selection.RemoveOptions = {}) { // dryrun to prevent cell be removed from graph options.dryrun = true this.collection.remove(Array.isArray(cells) ? cells : [cells], options) return this } - reset(cells?: Cell | Cell[], options: Collection.SetOptions = {}) { + reset(cells?: Cell | Cell[], options: Selection.SetOptions = {}) { if (cells) { + if (options.batch) { + this.collection.reset(cells, { ...options, ui: true }) + return this + } + const prev = this.cells const next = this.filter(Array.isArray(cells) ? cells : [cells]) const prevMap: KeyValue = {} @@ -226,7 +231,7 @@ export class Selection extends View { return this.clean(options) } - clean(options: Collection.SetOptions = {}) { + clean(options: Selection.SetOptions = {}) { if (this.length) { this.collection.reset([], { ...options, ui: true }) } @@ -314,7 +319,7 @@ export class Selection extends View { height /= scale.sy const rect = new Rectangle(origin.x, origin.y, width, height) const cells = this.getCellViewsInArea(rect).map((view) => view.cell) - this.collection.reset(cells, { ui: true }) + this.reset(cells, { batch: true }) this.hideRubberband() break } @@ -587,7 +592,7 @@ export class Selection extends View { protected notifyBoxEvent< K extends keyof Selection.BoxEventArgs, - T extends JQuery.TriggeredEvent, + T extends JQuery.TriggeredEvent >(name: K, e: T, x: number, y: number) { const data = this.getEventData(e) const view = data.activeView @@ -1066,6 +1071,14 @@ export namespace Selection { | null | (string | { id: string })[] | ((this: Graph, cell: Cell) => boolean) + + export interface SetOptions extends Collection.SetOptions { + batch?: boolean + } + + export interface AddOptions extends Collection.AddOptions {} + + export interface RemoveOptions extends Collection.RemoveOptions {} } export namespace Selection { diff --git a/packages/x6/src/graph/graph.ts b/packages/x6/src/graph/graph.ts index b3a521f06e0..5828a0fb38f 100644 --- a/packages/x6/src/graph/graph.ts +++ b/packages/x6/src/graph/graph.ts @@ -1757,13 +1757,16 @@ export class Graph extends Basecoat { return this.selection.isEmpty() } - cleanSelection() { - this.selection.clean() + cleanSelection(options?: Selection.SetOptions) { + this.selection.clean(options) return this } - resetSelection(cells?: Cell | string | (Cell | string)[]) { - this.selection.reset(cells) + resetSelection( + cells?: Cell | string | (Cell | string)[], + options?: Selection.SetOptions, + ) { + this.selection.reset(cells, options) return this } @@ -1781,7 +1784,7 @@ export class Graph extends Basecoat { select( cells: Cell | string | (Cell | string)[], - options: Collection.AddOptions = {}, + options?: Selection.AddOptions, ) { this.selection.select(cells, options) return this @@ -1789,7 +1792,7 @@ export class Graph extends Basecoat { unselect( cells: Cell | string | (Cell | string)[], - options: Collection.RemoveOptions = {}, + options?: Selection.RemoveOptions, ) { this.selection.unselect(cells, options) return this diff --git a/packages/x6/src/graph/selection.ts b/packages/x6/src/graph/selection.ts index cded0dc040f..a9462db0152 100644 --- a/packages/x6/src/graph/selection.ts +++ b/packages/x6/src/graph/selection.ts @@ -1,6 +1,5 @@ import { ModifierKey } from '../types' import { Selection } from '../addon/selection' -import { Collection } from '../model/collection' import { Cell } from '../model/cell' import { EventArgs } from './events' import { Base } from './base' @@ -138,7 +137,7 @@ export class SelectionManager extends Base { select( cells: Cell | string | (Cell | string)[], - options: Collection.AddOptions = {}, + options: Selection.AddOptions = {}, ) { const selected = this.getCells(cells) if (selected.length) { @@ -153,7 +152,7 @@ export class SelectionManager extends Base { unselect( cells: Cell | string | (Cell | string)[], - options: Collection.RemoveOptions = {}, + options: Selection.RemoveOptions = {}, ) { this.widget.unselect(this.getCells(cells), options) return this @@ -161,14 +160,14 @@ export class SelectionManager extends Base { reset( cells?: Cell | string | (Cell | string)[], - options: Collection.SetOptions = {}, + options: Selection.SetOptions = {}, ) { this.widget.reset(cells ? this.getCells(cells) : [], options) return this } - clean() { - this.widget.clean() + clean(options: Selection.SetOptions = {}) { + this.widget.clean(options) return this } @@ -264,4 +263,8 @@ export namespace SelectionManager { export type Filter = Selection.Filter export type Content = Selection.Content + + export type SetOptions = Selection.SetOptions + export type AddOptions = Selection.AddOptions + export type RemoveOptions = Selection.RemoveOptions }