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: ✨ add batch for selection #1399

Merged
merged 1 commit into from
Oct 2, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
29 changes: 23 additions & 6 deletions packages/x6/src/addon/selection/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,22 +173,27 @@ export class Selection extends View<Selection.EventArgs> {
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<Cell> = {}
Expand Down Expand Up @@ -226,9 +231,13 @@ export class Selection extends View<Selection.EventArgs> {
return this.clean(options)
}

clean(options: Collection.SetOptions = {}) {
clean(options: Selection.SetOptions = {}) {
if (this.length) {
this.collection.reset([], { ...options, ui: true })
if (options.batch === false) {
this.unselect(this.cells, options)
} else {
this.collection.reset([], { ...options, ui: true })
}
}
return this
}
Expand Down Expand Up @@ -314,7 +323,7 @@ export class Selection extends View<Selection.EventArgs> {
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
}
Expand Down Expand Up @@ -1066,6 +1075,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 {
Expand Down
15 changes: 9 additions & 6 deletions packages/x6/src/graph/graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1757,13 +1757,16 @@ export class Graph extends Basecoat<EventArgs> {
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
}

Expand All @@ -1781,15 +1784,15 @@ export class Graph extends Basecoat<EventArgs> {

select(
cells: Cell | string | (Cell | string)[],
options: Collection.AddOptions = {},
options?: Selection.AddOptions,
) {
this.selection.select(cells, options)
return this
}

unselect(
cells: Cell | string | (Cell | string)[],
options: Collection.RemoveOptions = {},
options?: Selection.RemoveOptions,
) {
this.selection.unselect(cells, options)
return this
Expand Down
15 changes: 9 additions & 6 deletions packages/x6/src/graph/selection.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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) {
Expand All @@ -153,22 +152,22 @@ 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
}

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
}

Expand Down Expand Up @@ -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
}