Skip to content

Commit

Permalink
fix: 使用@antv/x6-plugin-clipboard插件且useLocalStorage开启时,希望支持设置LocalStor…
Browse files Browse the repository at this point in the history
…age的key antvis#4218
  • Loading branch information
qiufeihong2018 committed May 13, 2024
1 parent 23e9aa1 commit e9bc3c7
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 13 deletions.
10 changes: 8 additions & 2 deletions examples/x6-example-features/src/pages/clipboard/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,19 @@ export default class Example extends React.Component {
onCopy = () => {
const cells = this.selection.getSelectedCells()
if (cells && cells.length) {
this.clipboard.copy(cells)
this.clipboard.copy(cells, {
useLocalStorage: true,
localStorageKey: 'dawei',
})
}
}

onPaste = () => {
if (!this.clipboard.isEmpty()) {
this.clipboard.paste()
this.clipboard.paste({
localStorageKey: 'dawei',
useLocalStorage: true,
})
}
}

Expand Down
18 changes: 12 additions & 6 deletions packages/x6-plugin-clipboard/src/clipboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,13 @@ export class ClipboardImpl {

serialize(options: ClipboardImpl.PasteOptions) {
if (options.useLocalStorage !== false) {
Storage.save(this.cells)
Storage.save(this.cells, options.localStorageKey)
}
}

deserialize(options: ClipboardImpl.PasteOptions) {
if (options.useLocalStorage) {
const cells = Storage.fetch()
const cells = Storage.fetch(options.localStorageKey)
if (cells) {
this.cells = cells
}
Expand All @@ -108,6 +108,7 @@ export class ClipboardImpl {
export namespace ClipboardImpl {
export interface Options {
useLocalStorage?: boolean
localStorageKey?: string
}

export interface CopyOptions extends Options {
Expand Down Expand Up @@ -138,16 +139,21 @@ export namespace ClipboardImpl {
namespace Storage {
const LOCAL_STORAGE_KEY = `${Config.prefixCls}.clipboard.cells`

export function save(cells: Cell[]) {
export function save(cells: Cell[], localStorageKey?: string) {
if (window.localStorage) {
const data = cells.map((cell) => cell.toJSON())
localStorage.setItem(LOCAL_STORAGE_KEY, JSON.stringify(data))
localStorage.setItem(
`${localStorageKey}.${LOCAL_STORAGE_KEY}`,
JSON.stringify(data),
)
}
}

export function fetch() {
export function fetch(localStorageKey?: string) {
if (window.localStorage) {
const raw = localStorage.getItem(LOCAL_STORAGE_KEY)
const raw = localStorage.getItem(
`${localStorageKey}.${LOCAL_STORAGE_KEY}`,
)
const cells = raw ? JSON.parse(raw) : []
if (cells) {
return Model.fromJSON(cells)
Expand Down
20 changes: 15 additions & 5 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit e9bc3c7

Please sign in to comment.