sParentOf(...)
isParentOf(child: Cell | null): boolean
返回当前节点是否是指定 Cell 的父节点。
参数
名称 | 类型 | 必选 | 默认值 | 描述 |
---|---|---|---|---|
child | Cell | null | ✓ |
isChildOf(...)
isChildOf(parent: Cell | null): boolean
返回当前节点/边是否是指定节点的的子节点/边。
参数
名称 | 类型 | 必选 | 默认值 | 描述 |
---|---|---|---|---|
parent | Cell | null | ✓ |
eachChild(...)
eachChild(iterator: (child: Cell, index: number, children: Cell[]) => void, context?: any): this
参数
名称 | 类型 | 必选 | 默认值 | 描述 |
---|---|---|---|---|
iterator | (child: Cell, index: number, children: Cell[]) => void | ✓ | 迭代器函数。 | |
context | any | 迭代器函数的执行上下文。 |
遍历子节点。
filterChild(...)
filterChild(iterator: (child: Cell, index: number, children: Cell[]) => boolean, context?: any): Cell[]
过滤子节点。
参数
名称 | 类型 | 必选 | 默认值 | 描述 |
---|---|---|---|---|
iterator | (child: Cell, index: number, children: Cell[]) => boolean | ✓ | 过滤器函数。 | |
context | any | 过滤器函数的执行上下文。 |
返回值
返回满足过滤条件的子节点/边,或者空数组。
getChildCount()
getChildCount(): number
获取子节点/边的数量。
返回值
返回子节点/边的数量,当没有子节点/边时返回 0
。
getChildIndex(...)
getChildIndex(child: Cell): number
获取子节点/边的索引。
参数
名称 | 类型 | 必选 | 默认值 | 描述 |
---|---|---|---|---|
child | Cell | ✓ |
返回值
返回子节点/边的索引,当子节点不存在时返回 -1
。
getChildAt(...)
getChildAt(index: number): Cell | null
获取指定索引位置的子节点/边。
参数
名称 | 类型 | 必选 | 默认值 | 描述 |
---|---|---|---|---|
index | number | ✓ | 索引位置。 |
返回值
返回指定索引位置的子节点,当该节点不存在子节点/边、索引越界、子节点不存在时返回 null
。
getAncestors(...)
getAncestors(options?: { deep?: boolean }): Cell[]
获取所有祖先节点。
参数
名称 | 类型 | 必选 | 默认值 | 描述 |
---|---|---|---|---|
options.deep | boolean | true | 默认递归获取所有祖先节点,设置为 false 时只返回当前节点/边的父节点。 |
返回值
返回祖先节点数组。
getDescendants(...)
getDescendants(options?: Cell.GetDescendantsOptions): Cell[]
获取所有子孙节点。
参数
名称 | 类型 | 必选 | 默认值 | 描述 |
---|---|---|---|---|
options.deep | boolean | true | 默认递归获取所有子孙节点,设置为 false 时只返回当前节点孩子节点/边。 | |
options.breadthFirst | boolean | false | 默认使用深度优先算法,设置为 true 时使用广度优先搜索算法。 |
返回值
返回子孙节点/边的数组。
isDescendantOf(...)
isDescendantOf(ancestor: Cell | null, options?: { deep?: boolean }): boolean
返回当前节点/边是否是指定节点的子孙节点/边。
参数
名称 | 类型 | 必选 | 默认值 | 描述 |
---|---|---|---|---|
ancestor | Cell | null | ✓ | 指定节点。 | |
options.deep | boolean | true | 默认递归判断指定节点的所有子孙节点/边,设置为 false 时只判断孩子节点/边。 |
isAncestorOf(...)
isAncestorOf(descendant: Cell | null, options?: { deep?: boolean }): boolean
返回当前节点是否是指定节点/边的祖先节点。
参数
名称 | 类型 | 必选 | 默认值 | 描述 |
---|---|---|---|---|
descendant | Cell | null | ✓ | 指定节点/边。 | |
options.deep | boolean | true | 默认递归判断指定节点的所有子孙节点/边,设置为 false 时只判断孩子节点/边。 |
getCommonAncestor(...)
getCommonAncestor(...cells: (Cell | null | undefined)[]): Cell | null
参数
名称 | 类型 | 必选 | 默认值 | 描述 |
---|---|---|---|---|
...cells | (Cell | null | undefined)[] | ✓ | 指定节点/边。 |
获取给定节点/边的共同祖先节点。
返回值
返回找到的共同祖先节点,否则没有共同祖先节点时返回 null
。
addTo(...)
addTo(model: Model, options?: Cell.SetOptions): this
addTo(graph: Graph, options?: Cell.SetOptions): this
addTo(parent: Cell, options?: Cell.SetOptions): this
将当前节点/边添加到画布或指定的父节点。
insertTo(...)
insertTo(parent: Cell, index?: number, options?: Cell.SetOptions): this
将当前节点/边插入到指定父节点的指定位置。
参数
名称 | 类型 | 必选 | 默认值 | 描述 |
---|---|---|---|---|
parent | Cell | ✓ | 父节点。 | |
index | number | 要插入的索引位置,缺省时插入到末尾。 | ||
options.silent | boolean | false | 为 true 时不触发 'change:parent' 事件和画布重绘。 | |
options...others | object | 其他自定义键值对,可以在事件回调中使用。 |
addChild(...)
addChild(child: Cell, options?: Cell.SetOptions): this
将指定的节点/边添加到当前节点的子节点的末尾。
参数
名称 | 类型 | 必选 | 默认值 | 描述 |
---|---|---|---|---|
child | Cell | ✓ | 指定的节点/边。 | |
options.silent | boolean | false | 为 true 时不触发 'change:children' 事件和画布重绘。 | |
options...others | object | 其他自定义键值对,可以在事件回调中使用。 |
insertChild(...)
insertChild(child: Cell, index?: number, options?: Cell.SetOptions): this
将指定的节点/边添加到当前节点的子节点的指定位置。
参数
名称 | 类型 | 必选 | 默认值 | 描述 |
---|---|---|---|---|
child | Cell | ✓ | 指定的节点/边。 | |
index | number | 要插入的索引位置,缺省时插入到末尾。 | ||
options.silent | boolean | false | 为 true 时不触发 'change:children' 事件和画布重绘。 | |
options...others | object | 其他自定义键值对,可以在事件回调中使用。 |
embed(...)
embed(child: Cell, options?: Cell.SetOptions): this
将指定的节点/边嵌入到当前节点,即将指定的节点/边插入到当前节点的末尾。
参数
名称 | 类型 | 必选 | 默认值 | 描述 |
---|---|---|---|---|
child | Cell | ✓ | 指定的节点/边。 | |
options.silent | boolean | false | 为 true 时不触发 'change:children' 事件和画布重绘。 | |
options...others | object | 其他自定义键值对,可以在事件回调中使用。 |
unembed(...)
unembed(child: Cell, options?: Cell.SetOptions): this
将指定的节点/边从当前节点中移除。
参数
名称 | 类型 | 必选 | 默认值 | 描述 |
---|---|---|---|---|
child | Cell | ✓ | 指定的节点/边。 | |
options.silent | boolean | false | 为 true 时不触发 'change:children' 事件和画布重绘。 | |
options...others | object | 其他自定义键值对,可以在事件回调中使用。 |
removeFromParent(...)
removeFromParent(options?: Cell.RemoveOptions): this
将当前节点从父节点中移除。
参数
名称 | 类型 | 必选 | 默认值 | 描述 |
---|---|---|---|---|
options.deep | boolean | true | 默认递归移除所有子节点/边,设置为 false 时只移除当前节点/边。 | |
options.silent | boolean | false | 为 true 时不触发 'change:children' 事件和画布重绘。 | |
options...others | object | 其他自定义键值对,可以在事件回调中使用。 |
removeChild(...)
removeChild(child: Cell, options?: Cell.RemoveOptions): Cell | null
移除指定的子节点/边。
参数
名称 | 类型 | 必选 | 默认值 | 描述 |
---|---|---|---|---|
child | Cell | ✓ | 指定的节点/边。 | |
options.deep | boolean | true | 默认递归移除所有子节点/边,设置为 false 时只移除当前节点/边。 | |
options.silent | boolean | false | 为 true 时不触发 'change:children' 事件和画布重绘。 | |
options...others | object | 其他自定义键值对,可以在事件回调中使用。 |
返回值
返回被移除的节点/边。
removeChildAt(...)
removeChildAt(index: number, options?: Cell.RemoveOptions): Cell | null
移除指定索引位置的子节点/边。
名称 | 类型 | 必选 | 默认值 | 描述 |
---|---|---|---|---|
index | number | ✓ | 指定的索引位置。 | |
options.deep | boolean | true | 默认递归移除所有子节点/边,设置为 false 时只移除当前节点/边。 | |
options.silent | boolean | false | 为 true 时不触发 'change:children' 事件和画布重绘。 | |
options...others | object | 其他自定义键值对,可以在事件回调中使用。 |
返回值
返回被移除的节点/边。
remove(...)
remove(options?: Cell.RemoveOptions): this
先将当前节点/边从父节点,然后将其从画布中移除。
节点和边的属性 Properties
上面介绍的 markup
、attrs
、zIndex
、data
等基础选项,以及节点的 size
、 position
、angle
、ports
等选项,还有边的 source
、target
、labels
等选项,还有创建节点/边时提供的那些额外的键值对,我们都称为属性(Property)。
const rect = new Shape.Rect({
x: 30,
y: 30,
width: 100,
height: 40,
attrs: {...},
data: {...},
zIndex: 10,
sale: {...},
product: {
id: '1234',
name: 'apple',
price: 3.99,
},
})
例如,上面代码中的 attrs
、data
、zIndex
都是标准的属性,其中 x
和 y
是一对自定义选项,节点初始化时被转换为了 position
属性,同样 width
和 height
也是一对自定义选项,节点初始化时被转换为了 size
属性,最后剩余的 sale
和 product
两个对象是非标准的属性。
上面介绍了一些标准属性以及操作(get
/set
)这些标准属性的方法,下面再介绍几个比较通用的方法,这些方法对标准和非标准属性都适用。
getProp(...)
getProp<T>(key: string, defaultValue?: T): T
获取指定的属性值。
参数
名称 | 类型 | 必选 | 默认值 | 描述 |
---|---|---|---|---|
key | string | ✓ | 属性名称。 | |
defaultValue | T | undefined | 默认值,当指定的属性不存在时返回该默认值。 |
用法
// 获取标准属性
const zIndex = rect.getProp<number>('zIndex')
const position = rect.getProp<{x: number; y: number}>('position')
// 获取非标准属性
const product = rect.getProp('product')
setProp(...)
/**
* 设置指定的属性
*/
setProp(key: string, value: any, options?: Cell.SetOptions): this
/**
* 批量设置属性,提供的属性与原属性进行深度 merge
*/
setProp(props: Partial<Properties>, options?: Cell.SetOptions): this
设置属性。
默认情况触发对应的 'change:xxx'
事件和画布重绘,当 options.silent
为 true
时不触发 'change:xxx'
事件和画布重绘。
参数
名称 | 类型 | 必选 | 默认值 | 描述 |
---|---|---|---|---|
key | string | ✓ | 属性名称。 | |
value | any | ✓ | 属性值。 | |
props | Partial | ✓ | 属性键值对,将与现有属性进行深度 merge。 | |
options.silent | boolean | false | 为 true 时不触发 'change:markup' 事件和画布重绘。 | |
options...others | object | 其他自定义键值对,可以在事件回调中使用。 |
用法
设置单个属性:
rect.setProp('size', { width: 100, height: 30 })
rect.setProp('zIndex', 10)
通过键值对设置多个属性,提供的键值对将与现有属性进行深度 merge:
rect.setProp({
size: {
width: 100,
height: 30,
},
zIndex: 10,
})
当 options.silent
为 true
时,不触发对应的 'change:xxx'
事件和画布重绘:
rect.setProp('zIndex', 10, { silent: true })
在选项中支持其他自定义键值对,可以在事件回调用使用:
rect.setProp('zIndex', 10, { otherKey: 'otherValue', ... })
removeProp(...)
removeProp(path: string | string[], options?: Cell.SetOptions): this
删除指定路径的属性。
默认情况触发对应的 'change:xxx'
事件和画布重绘,当 options.silent
为 true
时不触发 'change:xxx'
事件和画布重绘。
参数
名称 | 类型 | 必选 | 默认值 | 描述 |
---|---|---|---|---|
path | string | string[] | ✓ | 属性路径。 | |
options.silent | boolean | false | 为 true 时不触发 'change:markup' 事件和画布重绘。 | |
options...others | object | 其他自定义键值对,可以在事件回调中使用。 |
用法
删除单个属性:
rect.removeProp('zIndex')
删除指定路径的属性:
rect.removeProp('product/id')
// 或
rect.removeProp(['product', 'id'])
当 options.silent
为 true
时,不触发对应的 'change:xxx'
事件和画布重绘:
rect.removeProp('zIndex', { silent: true })
在选项中支持其他自定义键值对,可以在事件回调用使用:
rect.removeProp('zIndex', { otherKey: 'otherValue', ... })
getPropByPath(...)
getPropByPath<T>(path: string | string[]): T
获取指定路径的属性值。
参数
名称 | 类型 | 必选 | 默认值 | 描述 |
---|---|---|---|---|
path | string | string[] | ✓ | 属性路径。 当 path 为 string 类型时,路径是以 '\' 分割的字符串。当 path 为 string[] 类型时,路径是属性对象路径上的 Key 构成的数组。 |
用法
rect.getPropByPath('zIndex')
// 或
rect.getPropByPath(['zIndex'])
rect.getPropByPath('product/id')
// 或
rect.getPropByPath(['product', 'id'])
setPropByPath(...)
setPropByPath(path: string | string[], value: any, options?: Cell.SetByPathOptions): this
根据属性路径设置属性值。
默认情况触发对应的 'change:xxx'
事件和画布重绘,当 options.silent
为 true
时不触发 'change:xxx'
事件和画布重绘。
名称 | 类型 | 必选 | 默认值 | 描述 |
---|---|---|---|---|
path | string | string[] | ✓ | 属性路径。 当 path 为 string 类型时,路径是以 '\' 分割的字符串。当 path 为 string[] 类型时,路径是属性对象路径上的 Key 构成的数组。 | |
value | any | ✓ | 属性值。 | |
options.rewrite | boolean | false | 默认与现路径上的现有属性进行深度 merge,设置为 true 时直接替换路径上的属性值。 | |
options.silent | boolean | false | 为 true 时不触发 'change:attrs' 事件和画布重绘。 | |
options...others | object | 其他自定义键值对,可以在事件回调中使用。 |
用法
默认情况触发对应的 'change:xxx'
事件和画布重绘:
rect.setPropByPath('zIndex', 10)
// 或
rect.setPropByPath(['zIndex'], 10)
rect.setPropByPath('product/price', 5.99)
// 或
rect.setPropByPath(['product', 'price'], 5.99)
当 options.silent
为 true
时不触发 'change:xxx'
事件和画布重绘:
rect.setPropByPath('zIndex', 10, { silent: true })
// 或
rect.setPropByPath(['zIndex'], 10, { silent: true })
rect.setPropByPath('product/price', 5.99, { silent: true })
// 或
rect.setPropByPath(['product', 'price'], 5.99, { silent: true })
默认与现路径上的现有属性进行深度 merge,当 options.rewrite
设置为 true
时直接替换路径上的属性值:
rect.setPropByPath(
['product'],
{ id: '234', name: 'banana', price: 2.99 },
{ rewrite: true },
)
在选项中支持其他自定义键值对,可以在事件回调用使用:
rect.setPropByPath('zIndex', 10, { otherKey: 'otherValue', ... })
removePropByPath(...)
removePropByPath(path: string | string[], options?: Cell.SetOptions): this
删除指定路径的属性值。
参数
名称 | 类型 | 必选 | 默认值 | 描述 |
---|---|---|---|---|
path | string | string[] | ✓ | 属性路径。 当 path 为 string 类型时,路径是以 '\' 分割的字符串。当 path 为 string[] 类型时,路径是属性对象路径上的 Key 构成的数组。 | |
options.silent | boolean | false | 为 true 时不触发 'change:attrs' 事件和画布重绘。 | |
options...others | object | 其他自定义键值对,可以在事件回调中使用。 |
用法
默认情况触发对应的 'change:xxx'
事件和画布重绘:
rect.removePropByPath('zIndex')
// 或
rect.removePropByPath(['zIndex'])
rect.removePropByPath('product/price')
// 或
rect.removePropByPath(['product', 'price'])
当 options.silent
为 true
时不触发 'change:xxx'
事件和画布重绘:
rect.removePropByPath('zIndex', { silent: true })
// 或
rect.removePropByPath(['zIndex'], { silent: true })
rect.removePropByPath('product/price', { silent: true })
// 或
rect.removePropByPath(['product', 'price'], { silent: true })
在选项中支持其他自定义键值对,可以在事件回调用使用:
rect.removePropByPath('zIndex', { otherKey: 'otherValue', ... })
prop(...)
/**
* 获取所有属性。
*/
prop(): Properties
/**
* 获取指定路径的属性值。
*/
prop<T>(path: string | string[]): T
/**
* 设置指定路径的属性值,与路径上的现有属性进行深度 merge。
*/
prop(path: string | string[], value: any, options?: Cell.SetOptions): this
/**
* 设置属性,与现有属性进行深度 merge。
*/
prop(props: Partial<Properties>, options?: Cell.SetOptions): this
该方法是 getProp、setProp、getPropByPath、setPropByPath 和 removePropByPath 几个方法的整合,提供了上面四种函数签名,是一个非常实用的方法。
用法
获取属性:
rect.prop()
rect.prop('zIndex')
rect.prop('product/price')
设置属性:
rect.prop('zIndex', 10)
rect.prop('product/price', 5.99)
rect.prop({
product: {
id: '234',
name: 'banana',
price: 3.99,
},
})
当 options.silent
为 true
时不触发 'change:xxx'
事件和画布重绘:
rect.prop('zIndex', 10, { silent: true })
// 或
rect.prop(['zIndex'], 10, { silent: true })
rect.prop('product/price', 5.99, { silent: true })
// 或
rect.prop(['product', 'price'], 5.99, { silent: true })
在选项中支持其他自定义键值对,可以在事件回调用使用:
rect.prop('zIndex', 10, { otherKey: 'otherValue', ... })
hasChanged(...)
hasChanged(key: string | undefined | null): boolean
返回指定的属性或所有属性是否已经改变。
参数
名称 | 类型 | 必选 | 默认值 | 描述 |
---|---|---|---|---|
key | string | undefined | null | 属性名。缺省时表示检查所有属性。 |
用法
if (rect.hasChanged()) {
// do something
}
if (rect.hasChanged('zIndex')) {
// do something
}
previous(...)
previous<T>(name: string): T | undefined
当指定的属性发生改变后,获取改变前的属性值。
名称 | 类型 | 必选 | 默认值 | 描述 |
---|---|---|---|---|
key | string | ✓ | 属性名。 |
用法
if (rect.hasChanged('zIndex')) {
const old = rect.previous('zIndex')
// do something
}