Skip to content

Commit

Permalink
refactor(events): add EventArgs definition for custom events
Browse files Browse the repository at this point in the history
fix #47
  • Loading branch information
bubkoo committed Dec 16, 2019
1 parent 1fe72f3 commit 727fee0
Show file tree
Hide file tree
Showing 39 changed files with 390 additions and 230 deletions.
4 changes: 2 additions & 2 deletions examples/x6-example-drawio/src/pages/graph/format.tsx
@@ -1,5 +1,5 @@
import React from 'react'
import { Graph, Cell } from '@antv/x6'
import { Cell } from '@antv/x6'
import { FormatCell } from './format-cell'
import { FormatDiagram } from './fromat-diagram'
import { fetchEditor } from '../'
Expand All @@ -12,7 +12,7 @@ export class Format extends React.PureComponent<Format.Props, Format.State> {

componentDidMount() {
fetchEditor().then(editor => {
editor.graph.on(Graph.events.selectionChanged, () => {
editor.graph.on('selectionChanged', () => {
this.setState({
selectedCell: editor.graph.getSelectedCell(),
})
Expand Down
6 changes: 3 additions & 3 deletions examples/x6-example-drawio/src/pages/graph/toolbar.tsx
Expand Up @@ -24,7 +24,7 @@ export class GraphToolbar extends React.PureComponent<

componentDidMount() {
fetchEditor().then(editor => {
editor.graph.on(Graph.events.selectionChanged, () => {
editor.graph.on('selectionChanged', () => {
this.setState({ selectedCells: editor.graph.getSelectedCells() })
})

Expand All @@ -35,8 +35,8 @@ export class GraphToolbar extends React.PureComponent<
})
}

editor.commands.undoManager.on(UndoManager.events.undo, updateUndoState)
editor.commands.undoManager.on(UndoManager.events.redo, updateUndoState)
editor.commands.undoManager.on('undo', updateUndoState)
editor.commands.undoManager.on('redo', updateUndoState)

updateUndoState()
this.setState({ editor })
Expand Down
Expand Up @@ -8,7 +8,7 @@ export class Setting extends React.Component<Setting.Props, Setting.State> {
super(props)
this.state = this.getNextState(props)

props.graph.on(Graph.events.selectionChanged, () => {
props.graph.on('selectionChanged', () => {
this.setState(this.getNextState())
})
}
Expand Down
12 changes: 6 additions & 6 deletions examples/x6-example-features/src/pages/flowchart/toolbar.tsx
Expand Up @@ -114,12 +114,12 @@ export class GraphToolbar extends React.Component<
}),
)

this.graph.on(Graph.events.selectionChanged, this.updateState)
this.graph.view.on(View.events.scale, this.updateState)
this.graph.view.on(View.events.scaleAndTranslate, this.updateState)
this.graph.model.on(Model.events.change, this.updateState)
this.undoManager.on(UndoManager.events.undo, this.updateState)
this.undoManager.on(UndoManager.events.redo, this.updateState)
this.graph.on('selectionChanged', this.updateState)
this.graph.view.on('scale', this.updateState)
this.graph.view.on('scaleAndTranslate', this.updateState)
this.graph.model.on('change', this.updateState)
this.undoManager.on('undo', this.updateState)
this.undoManager.on('redo', this.updateState)

this.state = this.getNextState()
}
Expand Down
13 changes: 6 additions & 7 deletions packages/x6/src/addon/autosave/index.ts
@@ -1,9 +1,8 @@
import { IChange } from '../../change'
import { Model } from '../../core/model'
import { Graph } from '../../graph'
import { Disablable, Disposable } from '../../common'

export class AutoSave extends Disablable {
export class AutoSave extends Disablable<AutoSave.EventArgs> {
graph: Graph

/**
Expand Down Expand Up @@ -32,7 +31,7 @@ export class AutoSave extends Disablable {

setGraph(graph: Graph | null) {
if (this.graph != null) {
this.graph.model.off(Model.events.change, this.onModelChanged, this)
this.graph.model.off('change', this.onModelChanged, this)
}

if (graph != null) {
Expand All @@ -42,7 +41,7 @@ export class AutoSave extends Disablable {
}

if (this.graph != null) {
this.graph.model.on(Model.events.change, this.onModelChanged, this)
this.graph.model.on('change', this.onModelChanged, this)
}
}

Expand All @@ -66,7 +65,7 @@ export class AutoSave extends Disablable {
}

private save() {
this.trigger(AutoSave.events.save)
this.trigger('save')
}

reset() {
Expand All @@ -81,7 +80,7 @@ export class AutoSave extends Disablable {
}

export namespace AutoSave {
export const events = {
save: 'save',
export interface EventArgs {
save?: null
}
}
4 changes: 2 additions & 2 deletions packages/x6/src/addon/dnd/index.ts
Expand Up @@ -350,7 +350,7 @@ export class Dnd<T> extends Disablable<Dnd.EventArgMap<T>> {
}

// Consume all events in the current graph before they are fired
graph.on(Graph.events.fireMouseEvent, this.eventConsumer)
graph.on('fireMouseEvent', this.eventConsumer)

this.trigger('dragEnter', {
graph,
Expand All @@ -364,7 +364,7 @@ export class Dnd<T> extends Disablable<Dnd.EventArgMap<T>> {
this.currentDropTarget = null

graph.eventloopManager.isMouseDown = false
graph.off(Graph.events.fireMouseEvent, this.eventConsumer)
graph.off('fireMouseEvent', this.eventConsumer)

this.removePreviewElement()

Expand Down
4 changes: 2 additions & 2 deletions packages/x6/src/change/currentroot-change.ts
Expand Up @@ -46,11 +46,11 @@ export class CurrentRootChange implements IChange {
this.view.refresh()
}

const name = this.isUp ? View.events.up : View.events.down
this.view.trigger(name, {
this.view.trigger(this.isUp ? 'up' : 'down', {
previous: this.previous,
currentRoot: this.view.currentRoot,
})

this.isUp = !this.isUp
}
}
3 changes: 1 addition & 2 deletions packages/x6/src/change/selection-change.ts
@@ -1,5 +1,4 @@
import { Cell } from '../core/cell'
import { Graph } from '../graph/graph'
import { Selection } from '../graph/selection'
import { IChange } from './change'

Expand Down Expand Up @@ -31,7 +30,7 @@ export class SelectionChange implements IChange {
this.added = this.removed
this.removed = tmp

this.selection.graph.trigger(Graph.events.selectionChanged, {
this.selection.graph.trigger('selectionChanged', {
added: this.added,
removed: this.removed,
})
Expand Down
13 changes: 6 additions & 7 deletions packages/x6/src/change/undoableedit.ts
@@ -1,5 +1,4 @@
import { Disposable, Events } from '../common'
import { Model } from '../core/model'
import { IChange } from './change'

export class UndoableEdit extends Disposable {
Expand Down Expand Up @@ -50,7 +49,7 @@ export class UndoableEdit extends Disposable {
return
}

this.model.trigger(Model.events.startEdit)
this.model.trigger('startEdit')
const count = this.changes.length

for (let i = count - 1; i >= 0; i -= 1) {
Expand All @@ -59,12 +58,12 @@ export class UndoableEdit extends Disposable {
change.execute()
}

this.model.trigger(Model.events.executed, change)
this.model.trigger('executed', change)
}

this.undone = true
this.redone = false
this.model.trigger(Model.events.endEdit)
this.model.trigger('endEdit')

this.notify()
}
Expand All @@ -77,17 +76,17 @@ export class UndoableEdit extends Disposable {
return
}

this.model.trigger(Model.events.startEdit)
this.model.trigger('startEdit')
this.changes.forEach(change => {
if (change.execute != null) {
change.execute()
}

this.model.trigger(Model.events.executed, change)
this.model.trigger('executed', change)
})
this.undone = false
this.redone = true
this.model.trigger(Model.events.endEdit)
this.model.trigger('endEdit')

this.notify()
}
Expand Down
29 changes: 13 additions & 16 deletions packages/x6/src/change/undomanager.ts
@@ -1,10 +1,8 @@
import { Primer, Disposable } from '../common'
import { Graph } from '../graph'
import { View } from '../core/view'
import { Model } from '../core/model'
import { UndoableEdit } from './undoableedit'

export class UndoManager extends Primer {
export class UndoManager extends Primer<UndoManager.EventArgs> {
private size: number
private cursor: number
private history: UndoableEdit[]
Expand All @@ -18,7 +16,7 @@ export class UndoManager extends Primer {
clear() {
this.cursor = 0
this.history = []
this.trigger(UndoManager.events.clear)
this.trigger('clear')
}

isEmpty() {
Expand All @@ -40,7 +38,7 @@ export class UndoManager extends Primer {
edit.undo()

if (edit.isSignificant()) {
this.trigger(UndoManager.events.undo, edit)
this.trigger('undo', edit)
break
}
}
Expand All @@ -53,7 +51,7 @@ export class UndoManager extends Primer {
this.cursor += 1
edit.redo()
if (edit.isSignificant()) {
this.trigger(UndoManager.events.redo, edit)
this.trigger('redo', edit)
break
}
}
Expand All @@ -68,7 +66,7 @@ export class UndoManager extends Primer {

this.history.push(edit)
this.cursor = this.history.length
this.trigger(UndoManager.events.add, edit)
this.trigger('add', edit)
}

trim() {
Expand All @@ -94,8 +92,7 @@ export namespace UndoManager {
const undoListener = (edit: UndoableEdit) => {
undoManager.add(edit)
}
graph.view.on(View.events.undo, undoListener)
graph.model.on(Model.events.afterUndo, undoListener)
graph.model.on('afterUndo', undoListener)

const undoHandler = (edit: UndoableEdit) => {
const cells = graph.changeManager
Expand All @@ -105,16 +102,16 @@ export namespace UndoManager {
graph.setSelectedCells(cells)
}

undoManager.on(UndoManager.events.undo, undoHandler)
undoManager.on(UndoManager.events.redo, undoHandler)
undoManager.on('undo', undoHandler)
undoManager.on('redo', undoHandler)

return undoManager
}

export const events = {
add: 'add',
undo: 'undo',
redo: 'redo',
clear: 'clear',
export interface EventArgs {
add: UndoableEdit
undo: UndoableEdit
redo: UndoableEdit
clear?: null
}
}
20 changes: 10 additions & 10 deletions packages/x6/src/common/dnd/dnd.ts
@@ -1,7 +1,7 @@
import { Primer } from '../primer'
import { addListeners, removeListeners } from './handler'

export class Dnd extends Primer {
export class Dnd extends Primer<Dnd.EventArgs> {
public readonly options: Dnd.Options
public disabled: boolean

Expand All @@ -26,15 +26,15 @@ export class Dnd extends Primer {

export namespace Dnd {
export const delay = 300
export const events = {
prepare: 'prepare',
dragStart: 'dragStart',
dragging: 'dragging',
dragEnter: 'dragEnter',
dragOver: 'dragOver',
dragLeave: 'dragLeave',
dragEnd: 'dragEnd',
drop: 'drop',
export interface EventArgs {
prepare: State
dragStart: State
dragging: State
dragEnter: State
dragOver: State
dragLeave: State
dragEnd: State
drop: State | null
}

export type HTMLElementOrFunc =
Expand Down
16 changes: 8 additions & 8 deletions packages/x6/src/common/dnd/handler.ts
Expand Up @@ -77,7 +77,7 @@ function process(e: MouseEvent) {
onDrop()
} else if (state.isPreparing) {
state.isPreparing = false
state.instance.trigger(Dnd.events.dragEnd, state)
state.instance.trigger('dragEnd', state)
clear()
}
}
Expand Down Expand Up @@ -133,7 +133,7 @@ function prepare(e: MouseEvent | TouchEvent) {
updatePosition(e)

// 将代理元素插入文档,设置样式等
data.instance.trigger(Dnd.events.prepare, data)
data.instance.trigger('prepare', data)

const offset = getOffset(data.element)
const width = outerWidth(data.element)
Expand Down Expand Up @@ -181,7 +181,7 @@ function onDragStart() {
regionWidth = outerWidth(state.region)
regionHeight = outerHeight(state.region)

state.instance.trigger(Dnd.events.dragStart, state)
state.instance.trigger('dragStart', state)
}

function onDragging() {
Expand Down Expand Up @@ -228,7 +228,7 @@ function onDragging() {
style.top = `${getTop()}px`
}

state.instance.trigger(Dnd.events.dragging, state)
state.instance.trigger('dragging', state)
}

function onDragEnterLeaveOver() {
Expand All @@ -242,25 +242,25 @@ function onDragEnterLeaveOver() {

if (state.activeContainer) {
if (!isContained(state.activeContainer, state.preview, fully)) {
state.instance.trigger(Dnd.events.dragLeave, state)
state.instance.trigger('dragLeave', state)
state.activeContainer = null
} else {
state.instance.trigger(Dnd.events.dragOver, state)
state.instance.trigger('dragOver', state)
}
} else {
for (let i = 0, ii = containers.length; i < ii; i += 1) {
const container = containers[i]
if (isContained(container, state.preview, fully)) {
state.activeContainer = container
state.instance.trigger(Dnd.events.dragEnter, state)
state.instance.trigger('dragEnter', state)
break
}
}
}
}

function onDrop() {
data!.instance.trigger(Dnd.events.drop, data)
data!.instance.trigger('drop', data)
clear()
}

Expand Down

0 comments on commit 727fee0

Please sign in to comment.