Skip to content

Commit

Permalink
feat: support dropping cell into a group
Browse files Browse the repository at this point in the history
  • Loading branch information
bubkoo committed Dec 13, 2019
1 parent 809118f commit 2b9348d
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 28 deletions.
3 changes: 2 additions & 1 deletion examples/x6-example-features/src/pages/flowchart/index.tsx
Expand Up @@ -15,6 +15,7 @@ export default class FlowChart extends React.Component {
DomEvent.disableContextMenu(this.container)
this.graph = createGraph(this.container)
demo(this.graph)
this.graph.center()
this.setState({ inited: true })
}

Expand Down Expand Up @@ -50,7 +51,7 @@ export default class FlowChart extends React.Component {
{this.graph && <GraphToolbar graph={this.graph} />}
</div>
<div className="flowchart-graph">
<div className="graph" ref={this.refContainer} />
<div className="graph" ref={this.refContainer} tabIndex={-1} />
</div>
</div>
{this.graph && <Setting graph={this.graph} />}
Expand Down
49 changes: 24 additions & 25 deletions examples/x6-example-features/src/pages/flowchart/toolbar.tsx
@@ -1,7 +1,7 @@
import React from 'react'
import { Radio } from 'antd'
import { Toolbar, Menu } from '@antv/x6-components'
import { Graph, UndoManager, View } from '@antv/x6'
import { Graph, UndoManager, View, Model } from '@antv/x6'

export class GraphToolbar extends React.Component<
GraphToolbar.Props,
Expand All @@ -21,7 +21,7 @@ export class GraphToolbar extends React.Component<
{
name: 'zoomIn',
icon: 'zoom-in',
tooltip: 'Zoom In',
tooltip: '鏀惧ぇ',
handler: () => {
let scale = this.graph.view.scale
if (scale >= 8) {
Expand Down Expand Up @@ -49,7 +49,7 @@ export class GraphToolbar extends React.Component<
{
name: 'zoomOut',
icon: 'zoom-out',
tooltip: 'Zoom Out',
tooltip: '缂╁皬',
handler: () => {
let scale = this.graph.view.scale
if (scale <= 0.15) {
Expand Down Expand Up @@ -79,14 +79,14 @@ export class GraphToolbar extends React.Component<
{
name: 'undo',
icon: 'undo',
tooltip: 'Undo',
tooltip: '鎾ら攢',
shortcut: 'Cmd + Z',
handler: () => this.undoManager.undo(),
},
{
name: 'redo',
icon: 'redo',
tooltip: 'Redo',
tooltip: '閲嶅仛',
shortcut: 'Cmd + Shift + Z',
handler: () => this.undoManager.redo(),
},
Expand All @@ -95,7 +95,7 @@ export class GraphToolbar extends React.Component<
{
name: 'delete',
icon: 'delete',
tooltip: 'Delete',
tooltip: '鍒犻櫎',
shortcut: 'Delete',
handler: () => this.graph.deleteCells(),
},
Expand All @@ -105,18 +105,15 @@ export class GraphToolbar extends React.Component<
this.commands.forEach(items =>
items.forEach(item => {
if (item.shortcut) {
const shortcut = item.shortcut
.replace('Delete', 'backspace')
.replace('Cmd', 'command')
.toLowerCase()
this.graph.bindKey(shortcut, item.handler)
this.graph.bindKey(item.shortcut, item.handler)
}
}),
)

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)

Expand Down Expand Up @@ -146,6 +143,10 @@ export class GraphToolbar extends React.Component<
onClick = (name: string) => {
if (name === 'resetView') {
this.graph.zoomTo(1)
this.graph.center()
} else if (name === 'fitWindow') {
this.graph.fit(8)
// this.graph.center()
} else if (
name === '25' ||
name === '50' ||
Expand All @@ -163,17 +164,8 @@ export class GraphToolbar extends React.Component<

renderZoomDropdown() {
const MenuItem = Menu.Item
const Divider = Menu.Divider

return (
<Menu hasIcon={false}>
<MenuItem name="resetView" hotkey="Cmd+H">
閲嶇疆瑙嗗彛
</MenuItem>
<MenuItem name="fitWindow" hotkey="Cmd+Shift+H">
閫傚簲绐楀彛
</MenuItem>
<Divider />
<MenuItem name="25">25%</MenuItem>
<MenuItem name="50">50%</MenuItem>
<MenuItem name="75">75%</MenuItem>
Expand All @@ -187,6 +179,9 @@ export class GraphToolbar extends React.Component<
}

render() {
const Item = Toolbar.Item
const Group = Toolbar.Group

return (
<Toolbar
onClick={this.onClick}
Expand All @@ -199,13 +194,17 @@ export class GraphToolbar extends React.Component<
</Radio.Group>
}
>
<Toolbar.Item dropdown={this.renderZoomDropdown()}>
<Item dropdown={this.renderZoomDropdown()}>
{(this.graph.view.scale * 100).toFixed(0)}%
</Toolbar.Item>
</Item>
<Group>
<Item name="resetView" tooltip="閲嶇疆瑙嗗彛" icon="retweet" />
<Item name="fitWindow" tooltip="閫傚簲绐楀彛" icon="fullscreen" />
</Group>
{this.commands.map(items => (
<Toolbar.Group key={items.map(i => i.name).join('-')}>
<Group key={items.map(i => i.name).join('-')}>
{items.map(item => (
<Toolbar.Item
<Item
key={item.name}
name={item.name}
icon={item.icon}
Expand All @@ -218,7 +217,7 @@ export class GraphToolbar extends React.Component<
onClick={item.handler}
/>
))}
</Toolbar.Group>
</Group>
))}
</Toolbar>
)
Expand Down
12 changes: 11 additions & 1 deletion examples/x6-example-features/src/pages/flowchart/util.ts
Expand Up @@ -43,7 +43,7 @@ export function createGraph(container: HTMLDivElement) {
keyboard: {
enabled: true,
global: false,
escape: false,
escape: true,
},
selectionPreview: {
dashed: false,
Expand All @@ -61,6 +61,16 @@ export function createGraph(container: HTMLDivElement) {
labelBackgroundColor: '#f8f9fa',
movable: false,
},
dropEnabled: true,
dropTargetHighlight: {
stroke: '#87d068',
opacity: 1,
},
isValidDropTarget(target) {
if (target && target.data) {
return isGroup(target.data.type)
}
},
isLabelMovable() {
return false
},
Expand Down
2 changes: 1 addition & 1 deletion packages/x6/src/handler/connection/handler.ts
Expand Up @@ -245,7 +245,7 @@ export class ConnectionHandler extends MouseHandler {

if (target != null) {
// tslint:disable-next-line
dropTarget = this.graph.getDropTarget([target], evt, dropTarget)!
dropTarget = this.graph.getDropTarget(evt, [target], dropTarget)!

// Disables edges as drop targets if the target cell was created
if (
Expand Down

0 comments on commit 2b9348d

Please sign in to comment.