-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: add plugin background * chore: update demo with background * chore: fix ci * test: add test case for background plugin * chore: fix cr
- Loading branch information
Showing
25 changed files
with
262 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { Graph } from '@/src'; | ||
import data from '@@/dataset/cluster.json'; | ||
|
||
export const pluginBackground: TestCase = async (context) => { | ||
const graph = new Graph({ | ||
...context, | ||
autoResize: true, | ||
data, | ||
layout: { type: 'd3force' }, | ||
behaviors: ['drag-canvas', 'drag-element'], | ||
plugins: [ | ||
{ | ||
type: 'background', | ||
key: 'background', | ||
backgroundImage: | ||
'url(https://mdn.alipayobjects.com/huamei_qa8qxu/afts/img/A*0Qq0ToQm1rEAAAAAAAAAAAAADmJ7AQ/original)', | ||
}, | ||
], | ||
}); | ||
|
||
await graph.render(); | ||
|
||
pluginBackground.form = (panel) => { | ||
const config = { | ||
backgroundSize: 'cover', | ||
}; | ||
return [ | ||
panel | ||
.add(config, 'backgroundSize', { | ||
Cover: 'cover', | ||
Contain: 'contain', | ||
}) | ||
.name('backgroundSize') | ||
.onChange((backgroundSize: string) => { | ||
graph.updatePlugin({ | ||
key: 'background', | ||
backgroundSize, | ||
}); | ||
}), | ||
]; | ||
}; | ||
|
||
return graph; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { pluginBackground } from '@/__tests__/demos'; | ||
import { createDemoGraph } from '@@/utils'; | ||
|
||
describe('plugin background', () => { | ||
it('background', async () => { | ||
const graph = await createDemoGraph(pluginBackground); | ||
const container = graph.getCanvas().getContainer()!; | ||
|
||
const el = container.querySelector('.g6-background') as HTMLDivElement; | ||
|
||
expect(graph.getPlugins()).toEqual([ | ||
{ | ||
type: 'background', | ||
key: 'background', | ||
backgroundImage: | ||
'url(https://mdn.alipayobjects.com/huamei_qa8qxu/afts/img/A*0Qq0ToQm1rEAAAAAAAAAAAAADmJ7AQ/original)', | ||
}, | ||
]); | ||
expect(el.style.backgroundImage).toContain( | ||
'url(https://mdn.alipayobjects.com/huamei_qa8qxu/afts/img/A*0Qq0ToQm1rEAAAAAAAAAAAAADmJ7AQ/original)', | ||
); | ||
|
||
await graph.destroy(); | ||
expect(container.querySelector('.g6-background')).toBeFalsy(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { omit } from '@antv/util'; | ||
import type { RuntimeContext } from '../../runtime/types'; | ||
import { createPluginContainer } from '../../utils/dom'; | ||
import type { BasePluginOptions } from '../base-plugin'; | ||
import { BasePlugin } from '../base-plugin'; | ||
|
||
/** | ||
* <zh/> 背景配置项 | ||
* | ||
* <en/> Background options | ||
*/ | ||
export interface BackgroundOptions extends BasePluginOptions, CSSStyleDeclaration {} | ||
|
||
/** | ||
* <zh/> 背景图 | ||
* | ||
* <en/> Background image | ||
* @remarks | ||
* <zh/> 支持为图画布设置一个背景图片,让画布更有层次感、叙事性。 | ||
* | ||
* <en/> Support setting a background image for the canvas to make the canvas more hierarchical and narrative. | ||
*/ | ||
export class Background extends BasePlugin<BackgroundOptions> { | ||
static defaultOptions: Partial<BackgroundOptions> = { | ||
transition: 'background 0.5s', | ||
backgroundSize: 'cover', | ||
opacity: '0.35', | ||
}; | ||
|
||
private $element: HTMLElement = createPluginContainer('background'); | ||
|
||
constructor(context: RuntimeContext, options: BackgroundOptions) { | ||
super(context, Object.assign({}, Background.defaultOptions, options)); | ||
|
||
const $container = this.context.canvas.getContainer(); | ||
$container!.appendChild(this.$element); | ||
|
||
this.update(options); | ||
} | ||
|
||
/** | ||
* <zh/> 更新背景图配置 | ||
* | ||
* <en/> Update the background image configuration | ||
* @param options - <zh/> 配置项 | <en/> Options | ||
* @internal | ||
*/ | ||
public async update(options: Partial<BackgroundOptions>) { | ||
super.update(options); | ||
|
||
// Set the background style. | ||
Object.assign(this.$element.style, omit(this.options, ['key', 'type'])); | ||
} | ||
|
||
/** | ||
* <zh/> 销毁背景图 | ||
* | ||
* <en/> Destroy the background image | ||
*/ | ||
public destroy(): void { | ||
super.destroy(); | ||
// Remove the background dom. | ||
this.$element.remove(); | ||
} | ||
} |
14 changes: 7 additions & 7 deletions
14
packages/g6/src/plugins/contextmenu.ts → packages/g6/src/plugins/contextmenu/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 5 additions & 5 deletions
10
packages/g6/src/plugins/watermark.ts → packages/g6/src/plugins/watermark/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
```js | ob { pin: false } | ||
createGraph( | ||
{ | ||
data: { | ||
nodes: [ | ||
{ id: 'node-0' }, | ||
{ id: 'node-1' }, | ||
{ id: 'node-2' }, | ||
{ id: 'node-3' }, | ||
{ id: 'node-4' }, | ||
{ id: 'node-5' }, | ||
], | ||
edges: [ | ||
{ source: 'node-0', target: 'node-1' }, | ||
{ source: 'node-0', target: 'node-2' }, | ||
{ source: 'node-0', target: 'node-3' }, | ||
{ source: 'node-0', target: 'node-4' }, | ||
{ source: 'node-1', target: 'node-0' }, | ||
{ source: 'node-2', target: 'node-0' }, | ||
{ source: 'node-3', target: 'node-0' }, | ||
{ source: 'node-4', target: 'node-0' }, | ||
{ source: 'node-5', target: 'node-0' }, | ||
], | ||
}, | ||
layout: { type: 'grid' }, | ||
behaviors: ['drag-canvas'], | ||
plugins: ['grid-line', { | ||
type: 'background', | ||
key: 'background', | ||
backgroundImage: 'url(https://mdn.alipayobjects.com/huamei_qa8qxu/afts/img/A*0Qq0ToQm1rEAAAAAAAAAAAAADmJ7AQ/original)', | ||
backgrounfRepeat: 'no-repeat', | ||
backgroundSize: 'contain', | ||
}], | ||
}, | ||
{ width: 600, height: 300 }, | ||
(gui, graph) => { | ||
const options = { | ||
type: 'background', | ||
backgroundSize: 'contain', | ||
}; | ||
const optionFolder = gui.addFolder('Background Options'); | ||
optionFolder.add(options, 'type').disable(true); | ||
optionFolder.add(options, 'backgroundSize', ['cover', 'contain', 'auto', '50%']); | ||
|
||
optionFolder.onChange(({ property, value }) => { | ||
graph.updatePlugin({ | ||
key: 'background', | ||
[property]: value, | ||
}); | ||
graph.render(); | ||
}); | ||
}, | ||
); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.