Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions __tests__/model/cell.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,18 @@ describe('Cell core API', () => {
expect(c.isVisible()).toBe(true)
})

it('data API: getData options reference', () => {
const c = new Cell({ data: { n: 1 } })

expect(c.getData()).toEqual({ n: 1 })

expect(c.getData()).toBe(c.getData())
expect(c.getData({ reference: true })).toBe(c.getData())

expect(c.getData({ reference: false })).toEqual(c.getData())
expect(c.getData({ reference: false })).not.toBe(c.getData())
})

it('data API: setData, replaceData, updateData, removeData', () => {
const c = new Cell({ data: { n: 1 } })
expect(c.getData()).toEqual({ n: 1 })
Expand Down
7 changes: 6 additions & 1 deletion site/docs/api/model/cell.en.md
Original file line number Diff line number Diff line change
Expand Up @@ -1321,11 +1321,16 @@ Set the associated data and trigger the `change:data` event and canvas redraw.
#### getData()

```ts
getData(): any
getData(options?: Cell.GetDataOptions): any
```

Get the associated data.

By default, it returns a reference to the original object.

| Name | Type | Required | Default | Description |
|-------------------|---------|:----:|---------|----------------------------------------------------------------------------------------|
| options.reference | boolean | | `true` | When `true`, it returns a reference to the original object, otherwise returns a deep copy. |
#### setData(...)

```ts
Expand Down
8 changes: 7 additions & 1 deletion site/docs/api/model/cell.zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -1321,11 +1321,17 @@ const rect = new Shape.Rect({
#### getData()

```ts
getData(): any
getData(options?: Cell.GetDataOptions): any
```

获取关联的数据。

默认返回原始对象的引用

| 名称 | 类型 | 必选 | 默认值 | 描述 |
|-------------------|---------|:----:|---------|----------------------------------------------------------------------------------------|
| options.reference | boolean | | `true` | 为 `true` 时返回原始对象的引用,否则返回其深拷贝。 |

#### setData(...)

```ts
Expand Down
13 changes: 11 additions & 2 deletions src/model/cell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -706,8 +706,13 @@ export class Cell<
this.setData(val)
}

getData<T = Properties['data']>(): T {
return this.store.get<T>('data')
getData<T = Properties['data']>(options: Cell.GetDataOptions = {}): T {
const result = this.store.get<T>('data')

if (options.reference === false) {
return ObjectExt.cloneDeep(result)
}
return result
}

setData<T = Properties['data']>(data: T, options: Cell.SetDataOptions = {}) {
Expand Down Expand Up @@ -1569,6 +1574,10 @@ export namespace Cell {
deep?: boolean
keepId?: boolean
}

export interface GetDataOptions {
reference?: boolean
}
}

export namespace Cell {
Expand Down