Skip to content

Commit

Permalink
feat: add plugin snapline
Browse files Browse the repository at this point in the history
  • Loading branch information
liujiangyu committed Sep 23, 2024
1 parent c9ba7fc commit bb3cd67
Show file tree
Hide file tree
Showing 9 changed files with 382 additions and 54 deletions.
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,6 @@
],
"javascript.preferences.importModuleSpecifier": "relative",
"typescript.preferences.importModuleSpecifier": "relative",
"svg.preview.background": "transparent"
"svg.preview.background": "transparent",
"typescript.tsdk": "node_modules/typescript/lib"
}
6 changes: 6 additions & 0 deletions packages/g6/__tests__/demos/behavior-scroll-canvas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,11 @@ export const behaviorScrollCanvas: TestCase = async (context) => {

await graph.render();

(window as any).__graph = graph;
(window as any).__g_instances__ = [];

const canvas = graph.getCanvas();

(window as any).__g_instances__.push(canvas);
return graph;
};
2 changes: 1 addition & 1 deletion packages/g6/__tests__/demos/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ export { pluginHistory } from './plugin-history';
export { pluginHull } from './plugin-hull';
export { pluginLegend } from './plugin-legend';
export { pluginMinimap } from './plugin-minimap';
export { pluginSnapline } from './plugin-snapline';
export { pluginSnapline, pluginSnapline } from './plugin-snapline';
export { pluginTimebar } from './plugin-timebar';
export { pluginToolbarBuildIn } from './plugin-toolbar-build-in';
export { pluginToolbarIconfont } from './plugin-toolbar-iconfont';
Expand Down
2 changes: 1 addition & 1 deletion packages/g6/__tests__/demos/plugin-grid-line.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export const pluginGridLine: TestCase = async (context) => {
behaviors: ['drag-canvas'],
plugins: [{ type: 'grid-line', follow: false }],
});

(window as any).__graph = graph;
await graph.render();

pluginGridLine.form = (panel) => {
Expand Down
91 changes: 41 additions & 50 deletions packages/g6/__tests__/demos/plugin-snapline.ts
Original file line number Diff line number Diff line change
@@ -1,74 +1,65 @@
import { Graph, Node } from '@antv/g6';
import { Graph } from '@/src';

export const pluginSnapline: TestCase = async (context) => {
const graph = new Graph({
...context,
data: {
nodes: [
{ id: 'node1', style: { x: 100, y: 100 } },
{ id: 'node2', style: { x: 300, y: 300 } },
{ id: 'node3', style: { x: 120, y: 200 } },
{ id: 'node-1', style: { x: 100, y: 100 } },
{ id: 'node-2', combo: 'combo-1', style: { x: 200, y: 100 } },
{ id: 'node-3', style: { x: 100, y: 200 } },
{ id: 'node-4', combo: 'combo-1', style: { x: 200, y: 200 } },
],
edges: [
{ source: 'node-1', target: 'node-2' },
{ source: 'node-2', target: 'node-4' },
{ source: 'node-1', target: 'node-3' },
{ source: 'node-3', target: 'node-4' },
],
combos: [{ id: 'combo-1' }],
},
node: {
type: (datum) => (datum.id === 'node3' ? 'circle' : 'rect'),
style: {
size: (datum) => (datum.id === 'node3' ? 40 : [60, 30]),
fill: 'transparent',
lineWidth: 2,
labelText: (datum) => datum.id,
},
node: { style: { size: 20 } },
edge: {
style: { endArrow: true },
},
behaviors: ['drag-element', 'drag-canvas', 'zoom-canvas'],
behaviors: [
{
type: 'drag-element',
// shadow: true
},
// {
// type: 'scroll-canvas',
// // shadow: true
// },
{
type: 'zoom-canvas',
// shadow: true
},
],
plugins: [
{
type: 'snapline',
key: 'snapline',
verticalLineStyle: { stroke: '#F08F56', lineWidth: 2 },
horizontalLineStyle: { stroke: '#17C76F', lineWidth: 2 },
autoSnap: false,
},
],
});

await graph.render();

const config = {
filter: false,
offset: 20,
autoSnap: false,
};

pluginSnapline.form = (panel) => {
const config = {
enable: true,
hideEdge: 'none',
shadow: false,
};
const handleChange = () => {
graph.setBehaviors([{ type: 'drag-element', ...config }]);
};
return [
panel
.add(config, 'filter')
.name('Add Filter(exclude circle)')
.onChange((filter: boolean) => {
graph.updatePlugin({
key: 'snapline',
filter: (node: Node) => (filter ? node.id !== 'node3' : true),
});
}),
panel
.add(config, 'offset', [0, 20, Infinity])
.name('Offset')
.onChange((offset: string) => {
graph.updatePlugin({
key: 'snapline',
offset,
});
}),
panel
.add(config, 'autoSnap')
.name('Auto Snap')
.onChange((autoSnap: boolean) => {
graph.updatePlugin({
key: 'snapline',
autoSnap,
});
}),
panel.add(config, 'enable').onChange(handleChange),
panel.add(config, 'hideEdge', ['none', 'in', 'out', 'both']).onChange(handleChange),
panel.add(config, 'shadow').onChange(handleChange),
];
};

return graph;
};
1 change: 1 addition & 0 deletions packages/g6/src/behaviors/drag-element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ export class DragElement extends BaseBehavior<DragElementOptions> {
* @internal
*/
protected onDragStart(event: IElementDragEvent) {
// console.log('onDragStart', event, event.target, event.target.id)
this.enable = this.validate(event);
if (!this.enable) return;

Expand Down
2 changes: 1 addition & 1 deletion packages/g6/src/plugins/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export { History } from './history';
export { Hull } from './hull';
export { Legend } from './legend';
export { Minimap } from './minimap';
export { Snapline } from './snapline';
export { SnapLine, Snapline } from './snapline';
export { Timebar } from './timebar';
export { Toolbar } from './toolbar';
export { Tooltip } from './tooltip';
Expand Down
Loading

0 comments on commit bb3cd67

Please sign in to comment.