Skip to content

Commit

Permalink
feat(devtools-kit): enable editing native Map/Set in input field (#253)
Browse files Browse the repository at this point in the history
  • Loading branch information
Azurewarth0920 authored Mar 1, 2024
1 parent 3a38cfd commit 6ed9dcd
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
22 changes: 21 additions & 1 deletion packages/devtools-kit/src/core/component/state/editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,30 @@ export class RefStateEditor {
ref.value = value
}
else {
// Edit on native type Set
if (ref instanceof Set && Array.isArray(value)) {
ref.clear()
value.forEach((v: unknown) => ref.add(v))
return
}

const currentKeys = Object.keys(value)

// Edit on native type Map
if (ref instanceof Map) {
const previousKeysSet = new Set(ref.keys())
currentKeys.forEach((key) => {
ref.set(key, Reflect.get(value, key))
previousKeysSet.delete(key)
})
previousKeysSet.forEach(key => ref.delete(key))
return
}

// if is reactive, then it must be object
// to prevent loss reactivity, we should assign key by key
const previousKeysSet = new Set(Object.keys(ref))
const currentKeys = Object.keys(value)

// we should check the key diffs, if previous key is the longer
// then remove the needless keys
currentKeys.forEach((key) => {
Expand Down
4 changes: 3 additions & 1 deletion packages/playground/basic/src/stores/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ import { computed, ref } from 'vue'

export const useAppStore = defineStore('app', () => {
const count = ref(120)
const map = ref(new Map([['a', 1], ['b', 2]]))
const set = ref(new Set([1, 2, 3]))
function increment() {
count.value++
}
const doubledCount = computed(() => count.value * 2)

return { count, doubledCount, increment }
return { count, doubledCount, increment, map, set }
})

export const useCounterStore = defineStore('counter', () => {
Expand Down

0 comments on commit 6ed9dcd

Please sign in to comment.