GG-Editor 是基於 G6-Editor 進行二次封裝的一款可視化操做應用框架,不過目前相關的使用說明文檔太稀少了,沒有一個完整的項目應用案例,還有就是核心代碼 gg-editor-core 沒有開源,那麼今天就教你們如何基於 GG-Editor 開發一款腦圖應用。html
本項目是基於 GG-Editor 進行開發的腦圖應用,基本上是一個較爲完整的應用了,能夠說已經預先給大家把坑踩過了,另外本項目全部代碼基本能夠很快速的實現一個流程圖應用,由於基本的 API 使用上是相同的。 另外,本文不會過多的講述官方文檔有的內容,會注重於使用上。前端
安裝和使用 GG-Editor 可直接經過 npm 或 yarn 安裝依賴node
npm install --save gg-editor
複製代碼
下面咱們快速進入開發,首先上 github 上把項目拉取到本地 github.com/alibaba/GGE…,咱們將它自帶的 Demo 做爲模版,在此基礎上進行開發。react
執行命令 yarn run start 後啓動效果圖git
下面咱們對項目進行改造github
官方文檔: ggeditor.com/docs/api/re…npm
在 components
文件夾下新增 EditorCustomNode/index.jsx
api
import React, { Fragment } from 'react';
import { RegisterNode } from 'gg-editor';
import PLUS_URL from '@/assets/plus.svg';
import MINUS_URL from '@/assets/minus.svg';
import CATE_URL from '@/assets/docs.svg';
import CASE_URL from '@/assets/file.svg';
const ICON_SIZE = 16;
const COLLAPSED_ICON = 'node-inner-icon';
function getRectPath(x: string | number, y: any, w: number, h: number, r: number) {
if (r) {
return [
['M', +x + +r, y],
['l', w - r * 2, 0],
['a', r, r, 0, 0, 1, r, r],
['l', 0, h - r * 2],
['a', r, r, 0, 0, 1, -r, r],
['l', r * 2 - w, 0],
['a', r, r, 0, 0, 1, -r, -r],
['l', 0, r * 2 - h],
['a', r, r, 0, 0, 1, r, -r],
['z'],
];
}
const res = [['M', x, y], ['l', w, 0], ['l', 0, h], ['l', -w, 0], ['z']];
res.toString = toString;
return res;
}
class EditorCustomNode extends React.Component {
render() {
const config = {
// 繪製標籤
// drawLabel(item) {
// },
// 繪製圖標
afterDraw(item) {
const model = item.getModel();
const group = item.getGraphicGroup();
const label = group.findByClass('label')[0];
const labelBox = label.getBBox();
const { width } = labelBox;
const { height } = labelBox;
const x = -width / 2;
const y = -height / 2;
const { type, collapsed, children } = model;
// 摺疊狀態圖標
if (type === 'cate' && children && children.length > 0) {
group.addShape('image', {
className: COLLAPSED_ICON,
attrs: {
img: collapsed ? PLUS_URL : MINUS_URL,
x: x - 24,
y: y - 2,
width: ICON_SIZE,
height: ICON_SIZE,
style: 'cursor: pointer',
},
});
}
// 文件類型圖標
group.addShape('image', {
attrs: {
img: type === 'cate' ? CATE_URL : CASE_URL,
x: children && children.length > 0 ? x + 4 : x - 12,
y: y - 2,
width: ICON_SIZE,
height: ICON_SIZE,
},
});
},
// 對齊標籤
adjustLabelPosition(item, labelShape) {
const size = this.getSize(item);
const padding = this.getPadding(item);
const width = size[0];
const labelBox = labelShape.getBBox();
labelShape.attr({
x: -width / 2 + padding[3],
y: -labelBox.height / 2,
});
},
// 內置邊距
// [上, 右, 下, 左]
getPadding(item) {
const model = item.getModel();
const { children } = model;
if (children && children.length > 0) {
return [12, 8, 12, 60];
}
return [12, 8, 12, 38];
},
// 標籤尺寸
// [寬, 高]
getSize(item) {
const group = item.getGraphicGroup();
const label = group.findByClass('label')[0];
const labelBox = label.getBBox();
const padding = this.getPadding(item);
return [
labelBox.width + padding[1] + padding[3],
labelBox.height + padding[0] + padding[2],
];
},
// 節點路徑
// x, y, w, h, r
getPath(item) {
const size = this.getSize(item);
const style = this.getStyle(item);
return getRectPath(-size[0] / 2, -size[1] / 2, size[0] + 4, size[1], style.radius);
},
// 節點樣式
getStyle(item) {
return {
// stroke: '#d9d9d9',
radius: 2,
lineWidth: 1,
};
},
// 標籤樣式
getLabelStyle(item) {
return {
fill: 'rgba(0,0,0,0.65)',
lineHeight: 18,
fontSize: 16,
};
},
// 激活樣式
getActivedStyle(item) {
return {
stroke: '#096dd9',
};
},
// 選中樣式
getSelectedStyle(item) {
return {
stroke: '#096dd9',
};
},
};
return (
<Fragment>
<RegisterNode name="mind-base" config={config} extend="mind-base" />
<RegisterNode name="custom-node" config={config} extend="mind-base" />
</Fragment>
);
}
}
export default EditorCustomNode;
複製代碼
修改 Mind 文件夾下新增並新增以下代碼微信
import EditorCustomNode from '../components/EditorCustomNode';
<Mind data={data} className={styles.mind} // rootShape 自定義根節點 // rootShape="custom-root" firstSubShape="custom-node" secondSubShape="custom-node" /> {/* 自定義節點 */} <EditorCustomNode /> 複製代碼
官方文檔:ggeditor.com/docs/api/re…antd
在 components
文件夾下新增 EditorCommand/index.jsx
import React from 'react';
import { RegisterCommand } from 'gg-editor';
function addNodeCall(editor, node, id, type) {
const graph = editor.getGraph();
const model = node.getModel();
const sideNode = editor.getFirstChildrenBySide('left');
const currentNode = sideNode[0] && graph.find(sideNode[0].id);
return graph.add('node', {
id,
parent: node.id,
label: '新建節點',
type,
side: model.children.length > 2 ? 'left' : 'right',
nth: currentNode ? graph.getNth(currentNode) : void 0,
});
}
class CustomCommand extends React.Component {
componentDidMount() {
document.addEventListener('keydown', event => {
event.preventDefault();
});
}
render() {
return [
// Enter 添加同級 case
<RegisterCommand
key="customAppendCase"
name="customAppendCase"
config={{
enable(editor) {
const currentPage = editor.getCurrentPage();
const selected = currentPage.getSelected();
return currentPage.isMind && selected.length === 1;
},
getItem(editor) {
const currentPage = editor.getCurrentPage();
const graph = currentPage.getGraph();
return this.selectedItemId
? graph.find(this.selectedItemId)
: currentPage.getSelected()[0];
},
execute(editor) {
let addNode;
const currentPage = editor.getCurrentPage();
const graph = currentPage.getGraph();
const root = currentPage.getRoot();
const node = this.getItem(editor);
const model = node.getModel();
const { hierarchy } = model;
const parent = node.getParent();
if (node.isRoot) addNode = addNodeCall(currentPage, node, this.addItemId, 'case');
else {
const l = graph.getNth(node);
addNode = graph.add('node', {
id: this.addItemId,
parent: parent.id,
side: hierarchy === 2 && root.children.length === 3 ? 'left' : model.side,
label: '新建節點',
type: 'case',
nth: model.side === 'left' && hierarchy === 2 ? l : l + 1,
});
}
currentPage.clearSelected(),
currentPage.clearActived(),
currentPage.setSelected(addNode, true),
this.executeTimes === 1 &&
((this.selectedItemId = node.id),
(this.addItemId = addNode.id),
currentPage.beginEditLabel(addNode));
},
back(editor: any) {
const currentPage = editor.getCurrentPage();
currentPage.getGraph().remove(this.addItemId),
currentPage.clearSelected(),
currentPage.clearActived(),
currentPage.setSelected(this.selectedItemId, true);
},
shortcutCodes: ['Enter'],
}}
/>,
// Tab 添加下級 case
<RegisterCommand
key="customAppendChildCase"
name="customAppendChildCase"
config={{
enable(editor: any) {
const currentPage = editor.getCurrentPage();
const selected = currentPage.getSelected();
const node = this.getItem(editor);
// 不容許建立次級 case
const isCase = node && node.getModel().type === 'case';
return currentPage.isMind && selected.length > 0 && !isCase;
},
getItem(editor: any) {
const currentPage = editor.getCurrentPage();
const graph = currentPage.getGraph();
return this.selectedItemId
? graph.find(this.selectedItemId)
: currentPage.getSelected()[0];
},
execute(editor: any) {
let addNode;
const currentPage = editor.getCurrentPage();
const graph = currentPage.getGraph();
const node = this.getItem(editor);
(addNode = node.isRoot
? addNodeCall(currentPage, node, this.addItemId, 'case')
: graph.add('node', {
id: this.addItemId,
parent: node.id,
label: '新建節點',
type: 'case',
})),
currentPage.clearSelected(),
currentPage.clearActived(),
currentPage.setSelected(addNode, true),
this.executeTimes === 1 &&
((this.selectedItemId = node.id),
(this.addItemId = addNode.id),
currentPage.beginEditLabel(addNode));
},
back(editor: any) {
const currentPage = editor.getCurrentPage();
currentPage.getGraph().remove(this.addItemId),
currentPage.clearSelected(),
currentPage.clearActived(),
currentPage.setSelected(this.selectedItemId, true);
},
shortcutCodes: ['Tab'],
}}
/>,
// ⌘ + N 添加同級 cate
<RegisterCommand
key="customAppendCate"
name="customAppendCate"
config={{
enable(editor: any) {
const currentPage = editor.getCurrentPage();
const selected = currentPage.getSelected();
return currentPage.isMind && selected.length === 1;
},
getItem(editor: any) {
const currentPage = editor.getCurrentPage();
const graph = currentPage.getGraph();
return this.selectedItemId
? graph.find(this.selectedItemId)
: currentPage.getSelected()[0];
},
execute(editor: any) {
let addNode;
const currentPage = editor.getCurrentPage();
const graph = currentPage.getGraph();
const root = currentPage.getRoot();
const node = this.getItem(editor);
const model = node.getModel();
const { hierarchy } = model;
const parent = node.getParent();
if (node.isRoot) addNode = addNodeCall(currentPage, node, this.addItemId, 'cate');
else {
const index = graph.getNth(node);
addNode = graph.add('node', {
id: this.addItemId,
parent: parent.id,
side: hierarchy === 2 && root.children.length === 3 ? 'left' : model.side,
label: '新建節點',
type: 'cate',
nth: model.side === 'left' && hierarchy === 2 ? index : index + 1,
});
}
currentPage.clearSelected(),
currentPage.clearActived(),
currentPage.setSelected(addNode, true),
this.executeTimes === 1 &&
((this.selectedItemId = node.id),
(this.addItemId = addNode.id),
currentPage.beginEditLabel(addNode));
},
back(editor: any) {
const currentPage = editor.getCurrentPage();
currentPage.getGraph().remove(this.addItemId),
currentPage.clearSelected(),
currentPage.clearActived(),
currentPage.setSelected(this.selectedItemId, true);
},
shortcutCodes: ['metaKey', 'n'],
}}
/>,
// ⌘ + ⇧ + N 添加下級 cate
<RegisterCommand
key="customAppendChildCate"
name="customAppendChildCate"
config={{
enable(editor: any) {
const currentPage = editor.getCurrentPage();
const selected = currentPage.getSelected();
const node = this.getItem(editor);
// 不容許建立次級 cate
const isCase = node && node.getModel().type === 'case';
return currentPage.isMind && selected.length > 0 && !isCase;
},
getItem(editor: any) {
const currentPage = editor.getCurrentPage();
const graph = currentPage.getGraph();
return this.selectedItemId
? graph.find(this.selectedItemId)
: currentPage.getSelected()[0];
},
execute(editor: any) {
let addNode;
const currentPage = editor.getCurrentPage();
const graph = currentPage.getGraph();
const node = this.getItem(editor);
(addNode = node.isRoot
? addNodeCall(currentPage, node, this.addItemId, 'cate')
: graph.add('node', {
id: this.addItemId,
parent: node.id,
label: '新建節點',
type: 'cate',
})),
currentPage.clearSelected(),
currentPage.clearActived(),
currentPage.setSelected(addNode, true),
this.executeTimes === 1 &&
((this.selectedItemId = node.id),
(this.addItemId = addNode.id),
currentPage.beginEditLabel(addNode));
},
back(editor: any) {
const currentPage = editor.getCurrentPage();
currentPage.getGraph().remove(this.addItemId),
currentPage.clearSelected(),
currentPage.clearActived(),
currentPage.setSelected(this.selectedItemId, true);
},
shortcutCodes: ['metaKey', 'shiftKey', 'N'],
}}
/>,
// ⌘ + B 摺疊 / 展開
<RegisterCommand
key="customCollapseExpand"
name="customCollapseExpand"
config={{
enable(editor) {
const node = this.getItem(editor);
if (!node) {
return false;
}
const { type } = node.getModel();
const hasChild = node.getChildren().length > 0;
return node && hasChild && type !== 'case';
},
getItem(editor) {
const currentPage = editor.getCurrentPage();
const grapg = currentPage.getGraph();
return this.itemId ? grapg.find(this.itemId) : currentPage.getSelected()[0];
},
execute(editor) {
const currentPage = editor.getCurrentPage();
const graph = currentPage.getGraph();
const node = this.getItem(editor);
node.getModel().collapsed
? (graph.update(node, {
collapsed: false,
}),
node.getInnerEdges &&
node.getInnerEdges().forEach(editor => {
editor.update();
}),
(this.toCollapsed = false))
: (graph.update(node, {
collapsed: true,
}),
(this.toCollapsed = true)),
currentPage.clearSelected(),
currentPage.setSelected(node, true),
this.executeTimes === 1 && (this.itemId = node.id);
},
back(editor) {
const currentPage = editor.getCurrentPage();
const graph = currentPage.getGraph();
const node = this.getItem(editor);
this.toCollapsed
? graph.update(node, {
collapsed: false,
})
: graph.update(node, {
collapsed: true,
}),
currentPage.clearSelected(),
currentPage.setSelected(node, true);
},
shortcutCodes: [
['metaKey', 'b'],
['ctrlKey', 'b'],
],
}}
/>,
// ⌘ + B 摺疊
<RegisterCommand
key="customCollapse"
name="customCollapse"
extend="customCollapseExpand"
config={{
enable(editor) {
const node = this.getItem(editor);
if (!node) {
return false;
}
const { type, collapsed } = node.getModel();
const hasChild = node.getChildren().length > 0;
return node && hasChild && type !== 'case' && !collapsed;
},
}}
/>,
// ⌘ + B 展開
<RegisterCommand
key="customExpand"
name="customExpand"
extend="customCollapseExpand"
config={{
enable(editor) {
const node = this.getItem(editor);
if (!node) {
return false;
}
const { type, collapsed } = node.getModel();
const hasChild = node.getChildren().length > 0;
return node && hasChild && type !== 'case' && collapsed;
},
}}
/>,
];
}
}
export default CustomCommand;
複製代碼
修改 Mind
文件夾下新增並新增以下代碼
import EditorCommand from '@/component/EditorCommand';
<Mind className={styles.mind} data={mindData} // rootShape="custom-root" firstSubShape="custom-node" secondSubShape="custom-node" graph={{ // renderer: 'svg', fitView: 'cc', // 畫布顯示位置,cc爲水平垂直居中顯示 // animate: true, }} // 註冊快捷鍵 shortcut={{ append: false, appendChild: false, collaspeExpand: false, customAppendCase: true, // Enter 添加同級 case customAppendChildCase: true, // Tab 添加下級 case customAppendCate: true, // ⌘ + N 添加同級 cate customAppendChildCate: true, // ⌘ + ⇧ + N 添加下級 cate customCollapseExpand: true, // ⌘ + B 摺疊 / 展開 customExpand: true, // ⌘ + B / Ctrl + B 展開 customCollapse: true, // ⌘ + B / Ctrl + B 摺疊 }} /> {/* 自定義快捷事件 */} <EditorCommand /> 複製代碼
官方文檔:ggeditor.com/docs/api/co…
修改 EditorContextMenu
下的 MindContextMenu.js
文件
import React from 'react';
import { NodeMenu, CanvasMenu, ContextMenu } from 'gg-editor';
import MenuItem from './MenuItem';
import styles from './index.less';
const MindContextMenu = () => (
<ContextMenu className={styles.contextMenu}>
<NodeMenu>
<MenuItem command="customAppendCase" icon="append" text="Enter 添加同級 case" />
<MenuItem command="customAppendChildCase" icon="append-child" text="Tab 添加下級 case" />
<MenuItem command="customAppendCate" icon="append" text="⌘ + N 添加同級 cate" />
<MenuItem
command="customAppendChildCate"
icon="append-child"
text="⌘ + ⇧ + N 添加下級 cate "
/>
<MenuItem command="customCollapse" icon="collapse" text="⌘ + B / Ctrl + B 摺疊" />
<MenuItem command="customExpand" icon="expand" text="⌘ + B / Ctrl + B 展開" />
<MenuItem command="delete" text="Delete / BackSpace 刪除" />
</NodeMenu>
<CanvasMenu>
<MenuItem command="undo" text="撤銷" />
<MenuItem command="redo" text="重作" />
</CanvasMenu>
</ContextMenu>
);
export default MindContextMenu;
複製代碼
官方文檔:ggeditor.com/docs/api/to…
修改 EditorToolbar
下的 MindToolbar.js
文件
import React from 'react';
import { Divider } from 'antd';
import { Toolbar } from 'gg-editor';
import ToolbarButton from './ToolbarButton';
import styles from './index.less';
const MindToolbar = () => (
<Toolbar className={styles.toolbar}>
<ToolbarButton command="undo" text="⌘ + Z 撤銷" />
<ToolbarButton command="redo" text="⇧ + ⌘ + Z 重作" />
<Divider type="vertical" />
<ToolbarButton command="zoomIn" icon="zoom-in" text="放大" />
<ToolbarButton command="zoomOut" icon="zoom-out" text="縮小" />
<ToolbarButton command="autoZoom" icon="fit-map" text="自適應比例" />
<ToolbarButton command="resetZoom" icon="actual-size" text="原始比例" />
<Divider type="vertical" />
<ToolbarButton command="customAppendCase" icon="append" text="Enter 添加同級 case" />
<ToolbarButton command="customAppendChildCase" icon="append-child" text="Tab 添加下級 case" />
<Divider type="vertical" />
<ToolbarButton command="customAppendCate" icon="append" text="⌘ + N 添加同級 cate" />
<ToolbarButton
command="customAppendChildCate"
icon="append-child"
text="⌘ + ⇧ + N 添加下級 cate "
/>
<Divider type="vertical" />
<ToolbarButton command="customCollapse" icon="collapse" text="⌘ + B / Ctrl + B 摺疊" />
<ToolbarButton command="customExpand" icon="expand" text="⌘ + B / Ctrl + B 展開" />
</Toolbar>
);
export default MindToolbar;
複製代碼
• 項目地址:github.com/luchx/ECHI_…
• 常見問題: github.com/gaoli/GGEdi…
微信公衆號:前端雜論