logo

X6

  • 文档
  • API
  • 图表示例
  • 常见问题
  • 更新日志
  • XFlow
  • 所有产品antv logo arrow
  • 2.x
  • 简介
  • 快速上手
  • 基础
    • 画布
    • 节点
    • 边
    • 连接桩
    • 交互
    • 事件
    • 数据
  • 进阶
    • 连接点
    • 工具
    • 群组
    • React 节点
    • Vue 节点
    • Angular 节点
    • HTML 节点
  • 插件
    • 图形变换
    • 对齐线
    • 复制粘贴
    • 快捷键
    • 撤销重做
    • 框选
    • 滚动画布
    • 小地图
    • Dnd
    • Stencil
    • 导出
  • 升级到 2.x 版本
  • 开发者工具

Stencil

上一篇
Dnd
下一篇
导出

Resources

Ant Design
Galacea Effects
Umi-React 应用开发框架
Dumi-组件/文档研发工具
ahooks-React Hooks 库

社区

体验科技专栏
seeconfSEE Conf-蚂蚁体验科技大会

帮助

GitHub
StackOverflow

more products更多产品

Ant DesignAnt Design-企业级 UI 设计语言
yuque语雀-知识创作与分享工具
EggEgg-企业级 Node 开发框架
kitchenKitchen-Sketch 工具集
GalaceanGalacean-互动图形解决方案
xtech蚂蚁体验科技
© Copyright 2025 Ant Group Co., Ltd..备案号:京ICP备15032932号-38

Loading...

通过阅读本章节你可以了解到

  • 如何通过 stencil 插件,进一步增强 dnd 能力

使用

Stencil 是在 Dnd 基础上的进一步封装,提供了一个类似侧边栏的 UI 组件,并支持分组、折叠、搜索等能力。我们提供了一个独立的插件包 @antv/x6-plugin-stencil 来使用这个功能。

# npm
$ npm install @antv/x6-plugin-stencil --save
# yarn
$ yarn add @antv/x6-plugin-stencil

然后我们在代码中这样使用:

import { Stencil } from '@antv/x6-plugin-stencil'
const graph = new Graph({
background: {
color: '#F2F7FA',
},
})
const stencil = new Stencil({
target: graph,
groups: [
{
name: 'group1',
},
{
name: 'group2',
},
],
})
const rect1 = graph.createNode({
shape: 'rect',
width: 100,
height: 40,
})
const rect2 = rect1.clone()
// 需要一个容纳 stencil 的 Dom 容器 stencilContainer
stencilContainer.appendChild(stencil.container)
stencil.load([rect1, rect2], 'group1')

演示

配置

选项类型必选默认值说明
titlestring'Stencil'标题。
groupsGroup[]✓️-分组信息。
searchFilterfalse搜索选项。
placeholderstring'Search'搜索文本框的 placeholder 文本。
notFoundTextstring'No matches found'未匹配到搜索结果时的提示文本。
collapsablebooleanfalse是否显示全局折叠/展开按钮。
layout(this: Stencil, model: Model, group?: Group | null) => any网格布局模板画布中节点的布局方法。
layoutOptionsany-布局选项。
stencilGraphWidthnumber200模板画布宽度。
stencilGraphHeightnumber800模板画布高度。设置为 0 时高度会自适应。
stencilGraphPaddingnumber10模板画布边距。
stencilGraphOptionsGraph.Options-模板画布选项。

提示

除了上面的配置,Stencil 还继承了 Dnd 的所有配置。

分组

初始化时,按照 groups 提供的分组,在每个分组中会渲染一个模板画布。分组的类型定义如下:

export interface Group {
name: string // 分组名称
title?: string // 分组标题,缺省时使用 `name`
collapsable?: boolean // 分组是否可折叠,默认为 true
collapsed?: boolean // 初始状态是否为折叠状态
nodeMovable?: boolean // 模板画布中的节点能否拖动
graphWidth?: number // 模板画布宽度
graphHeight?: number // 模板画布高度
graphPadding?: number // 模板画布边距
graphOptions?: Graph.Options // 模板画布选项
layout?: (this: Stencil, model: Model, group?: Group | null) => any
layoutOptions?: any // 布局选项
}

可以看到分组内的一些配置和外层的配置有重合,比如 graphWidth 和 stencilGraphHeight,分组内的配置优先级比较高。

布局

添加节点时,使用分组或全局的 layout 和 layoutOptions 来对节点进行自动布局,默认使用网格布局方法来布局模板节点,支持的布局选项有:

选项类型默认值说明
columnsnumber2网格布局的列数,默认为 2。行数根据节点数自动计算。
columnWidthnumber | 'auto' | 'compact''auto'列宽。auto: 所有节点中最宽节点的宽度作为列宽,compact: 该列中最宽节点的宽度作为列宽。
rowHeightnumber | 'auto' | 'compact''auto'行高。auto: 所有节点中最高节点的高度作为行高,compact: 该行中最高节点的高度作为行高。
dxnumber10单元格在 X 轴的偏移量,默认为 10。
dynumber10单元格在 Y 轴的偏移量,默认为 10。
marginXnumber0单元格在 X 轴的边距,默认为 0。
marginYnumber0单元格在 Y 轴的边距,默认为 0。
centerbooleantrue节点是否与网格居中对齐,默认为 true。
resizeToFitbooleanfalse是否自动调整节点的大小来适应网格大小,默认为 false。

也可以按照 (this: Stencil, model: Model, group?: Group | null) => any 签名进行自定义布局。

搜索

Stencil 还提供了搜索能力。

// 只搜索 rect 节点
const stencil = new Stencil({
search: (cell, keyword, groupName, stencil) => {
if (keyword) {
return cell.shape === 'rect'
}
return true
},
})
// 搜索 text 包含关键字的 rect 节点
const stencil = new Stencil({
search: (cell, keyword, groupName, stencil) => {
if (keyword) {
return cell.shape === 'rect' && cell.attr('text/text').includes(keyword)
}
return true
},
})

API

stencil.load(...)

load(nodes: (Node | Node.Metadata)[], groupName?: string): this

加载节点。参数如下:

名称类型必选默认值描述
nodes(Node | Node.Metadata)[]√-加载的节点。
groupNamestring-加载节点的分组名称。

stencil.unload(...)

unload(nodes: (Node | Node.Metadata)[], groupName?: string): this

卸载节点。参数如下:

名称类型必选默认值描述
nodes(Node | Node.Metadata)[]√-卸载的节点。
groupNamestring-卸载节点的分组名称。

stencil.addGroup(...)

addGroup(group: Stencil.Group | Stencil.Group[])

新增分组。

stencil.addGroup({
name: 'group1',
graphHeight: 100,
})

stencil.removeGroup(...)

removeGroup(groupName: string | string[])

删除分组。

stencil.resizeGroup(...)

resizeGroup(groupName: string, size: { width: number; height: number })

修改分组的尺寸。