Skip to content

Commit

Permalink
feat: export some model and view events to graph
Browse files Browse the repository at this point in the history
fix #54
  • Loading branch information
bubkoo committed Dec 24, 2019
1 parent 3f3a590 commit 31652b4
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 22 deletions.
4 changes: 2 additions & 2 deletions packages/x6/src/core/model.ts
Expand Up @@ -761,15 +761,15 @@ export class Model extends Events<Model.EventArgs> {
this.endUpdate()
}

beginUpdate() {
protected beginUpdate() {
this.updateLevel += 1
this.trigger('beginUpdate')
if (this.updateLevel === 1) {
this.trigger('startEdit')
}
}

endUpdate() {
protected endUpdate() {
this.updateLevel -= 1
if (this.updateLevel === 0) {
this.trigger('endEdit')
Expand Down
48 changes: 28 additions & 20 deletions packages/x6/src/core/view.ts
Expand Up @@ -189,7 +189,9 @@ export class View extends Primer<View.EventArgs> {
if (this.scale !== scale) {
this.scale = scale
this.scaledOrTranslated()
this.trigger('scale', { scale, previousScale })
const args = { scale, previousScale }
this.trigger('scale', args)
this.graph.trigger('scale', args)
}
}

Expand All @@ -205,10 +207,13 @@ export class View extends Primer<View.EventArgs> {
this.translate.y = ty

this.scaledOrTranslated()
this.trigger('translate', {

const args = {
previousTranslate,
translate: this.translate,
})
}
this.trigger('translate', args)
this.graph.trigger('translate', args)
}
}

Expand All @@ -226,12 +231,14 @@ export class View extends Primer<View.EventArgs> {
this.translate.y = ty

this.scaledOrTranslated()
this.trigger('scaleAndTranslate', {
const args = {
scale,
previousScale,
previousTranslate,
scale,
translate: this.translate,
})
}
this.trigger('scaleAndTranslate', args)
this.graph.trigger('scaleAndTranslate', args)
}
}

Expand Down Expand Up @@ -2224,6 +2231,18 @@ export class View extends Primer<View.EventArgs> {
}

export namespace View {
export interface ScaleArgs {
scale: number
previousScale: number
}

export interface TranslateArgs {
translate: Point
previousTranslate: Point
}

export interface ScaleAndTranslateArgs extends ScaleArgs, TranslateArgs {}

export interface EventArgs {
up: {
previous: Cell | null
Expand All @@ -2233,19 +2252,8 @@ export namespace View {
previous: Cell | null
currentRoot: Cell | null
}
scale: {
scale: number
previousScale: number
}
translate: {
translate: Point
previousTranslate: Point
}
scaleAndTranslate: {
scale: number
previousScale: number
translate: Point
previousTranslate: Point
}
scale: ScaleArgs
translate: TranslateArgs
scaleAndTranslate: ScaleAndTranslateArgs
}
}
2 changes: 2 additions & 0 deletions packages/x6/src/graph/change-manager.ts
Expand Up @@ -19,10 +19,12 @@ export class ChangeManager extends BaseManager {
}

protected onModelChanged(changes: IChange[]) {
this.graph.trigger('model:changing', changes)
changes.forEach(change => this.processChange(change))
this.graph.updateSelection()
this.view.validate()
this.graph.sizeDidChange()
this.graph.trigger('model:changed', changes)
}

protected processChange(change: IChange) {
Expand Down
7 changes: 7 additions & 0 deletions packages/x6/src/graph/events.ts
@@ -1,15 +1,22 @@
import { Cell } from '../core/cell'
import { View } from '../core/view'
import { Rectangle, Overlay, Anchor } from '../struct'
import { MouseEventEx } from '../common'
import { Align, VAlign } from '../types'
import { IChange } from '../change'

export interface EventArgs {
refresh?: null
pan: {
panX: number
panY: number
}
scale: View.ScaleArgs
translate: View.TranslateArgs
scaleAndTranslate: View.ScaleAndTranslateArgs

'model:changing': [IChange[]]
'model:changed': [IChange[]]
'root:changed'?: null

'selection:changed': {
Expand Down

0 comments on commit 31652b4

Please sign in to comment.