Skip to content

Commit

Permalink
fix: 🐛 remove single tool by name or index (#565)
Browse files Browse the repository at this point in the history
add `removeTool` and `hasTool` method

fix #552
  • Loading branch information
bubkoo committed Jan 20, 2021
1 parent 4626e50 commit f87dc43
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 7 deletions.
45 changes: 45 additions & 0 deletions packages/x6/src/model/cell.ts
Expand Up @@ -1174,6 +1174,51 @@ export class Cell<
return tools.name === name
}

hasTool(name: string) {
const tools = this.getTools()
if (tools == null) {
return false
}
return tools.items.some((item) =>
typeof item === 'string' ? item === name : item.name === name,
)
}

removeTool(name: string, options?: Cell.SetOptions): this
removeTool(index: number, options?: Cell.SetOptions): this
removeTool(nameOrIndex: string | number, options: Cell.SetOptions = {}) {
const tools = this.getTools()
if (tools) {
let updated = false
const items = tools.items.slice()
const remove = (index: number) => {
items.splice(index, 1)
updated = true
}

if (typeof nameOrIndex === 'number') {
remove(nameOrIndex)
} else {
for (let i = items.length - 1; i >= 0; i -= 1) {
const item = items[i]
const exist =
typeof item === 'string'
? item === nameOrIndex
: item.name === nameOrIndex
if (exist) {
remove(i)
}
}
}

if (updated) {
tools.items = items
this.setTools(tools, options)
}
}
return this
}

// #endregion

// #region common
Expand Down
35 changes: 30 additions & 5 deletions sites/x6-sites/docs/api/model/cell.en.md
Expand Up @@ -2,9 +2,9 @@
title: Cell
order: 0
redirect_from:
- /en/docs
- /en/docs/api
- /en/docs/api/model
- /zh/docs
- /zh/docs/api
- /zh/docs/api/model
---

Cell 是 [Node](./node)[Edge](./edge) 的基类,包含节点和边的通用属性和方法定义,如属性样式、可见性、业务数据等,并且在实例化、样式定制、默认选项、自定义选项等方面具有相同的行为。
Expand Down Expand Up @@ -2781,7 +2781,7 @@ getTools(): Cell.Tools | null
#### hasTools(...)

```sign
hasTools(name?: string): boolean
hasTools(name?: string): boolean
```

是否包含小工具或包含指定名称的工具集。
Expand All @@ -2808,7 +2808,32 @@ removeTools(options?: Cell.SetOptions): this
| options...others | object | | | 其他自定义键值对,可以在事件回调中使用。 |


### 动画 Transition
#### hasTool(name: string): boolean

```sign
hasTool(name: string): boolean
```

是否包含指定名称的工具。

#### removeTool(...)

```sign
removeTool(name: string, options?: Cell.SetOptions): this
removeTool(index: number, options?: Cell.SetOptions): this
```

删除指定名称的工具。

<span class="tag-param">参数<span>

| 名称 | 类型 | 必选 | 默认值 | 描述 |
|------------------|------------------|:----:|---------|---------------------------------------------------------|
| nameOrIndex | string \| number || | 工具名称或索引。 |
| options.silent | boolean | | `false` |`true` 时不触不触发 `'change:tools'` 事件和小工具重绘。 |
| options...others | object | | | 其他自定义键值对,可以在事件回调中使用。 |

### 动画 Transition

#### transition(...)

Expand Down
29 changes: 27 additions & 2 deletions sites/x6-sites/docs/api/model/cell.zh.md
Expand Up @@ -2781,7 +2781,7 @@ getTools(): Cell.Tools | null
#### hasTools(...)

```sign
hasTools(name?: string): boolean
hasTools(name?: string): boolean
```

是否包含小工具或包含指定名称的工具集。
Expand All @@ -2808,7 +2808,32 @@ removeTools(options?: Cell.SetOptions): this
| options...others | object | | | 其他自定义键值对,可以在事件回调中使用。 |


### 动画 Transition
#### hasTool(name: string): boolean

```sign
hasTool(name: string): boolean
```

是否包含指定名称的工具。

#### removeTool(...)

```sign
removeTool(name: string, options?: Cell.SetOptions): this
removeTool(index: number, options?: Cell.SetOptions): this
```

删除指定名称的工具。

<span class="tag-param">参数<span>

| 名称 | 类型 | 必选 | 默认值 | 描述 |
|------------------|------------------|:----:|---------|---------------------------------------------------------|
| nameOrIndex | string \| number || | 工具名称或索引。 |
| options.silent | boolean | | `false` |`true` 时不触不触发 `'change:tools'` 事件和小工具重绘。 |
| options...others | object | | | 其他自定义键值对,可以在事件回调中使用。 |

### 动画 Transition

#### transition(...)

Expand Down

0 comments on commit f87dc43

Please sign in to comment.