Skip to content

Commit

Permalink
fix(client): support menu data override, fix #260
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Jan 30, 2024
1 parent 275d7c8 commit eafc280
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 17 deletions.
12 changes: 7 additions & 5 deletions packages/client/app/layout/menu-item.vue
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

<script lang="ts" setup>
import { LegacyMenuItem, MaybeGetter, useContext } from '@koishijs/client'
import { MaybeGetter, useContext } from '@koishijs/client'
import { computed } from 'vue'
const props = defineProps<{
Expand All @@ -30,15 +30,17 @@ const disabled = computed(() => {
return toValue(props.item.disabled)
})
const scope = computed(() => ctx.$action.createScope({
[props.menuKey]: props.menuData,
}))
function toValue<T>(getter: MaybeGetter<T>): T {
if (typeof getter !== 'function') return getter
return (getter as any)(ctx.$action.createScope())
return (getter as any)(scope.value)
}
function trigger() {
return props.item.action(ctx.$action.createScope({
[props.menuKey]: props.menuData,
}))
return props.item.action(scope.value)
}
</script>
29 changes: 17 additions & 12 deletions packages/client/client/plugins/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,17 +140,22 @@ export default class ActionService extends Service {
})
}

createScope(scope = this.ctx.internal.scope, prefix = '') {
return new Proxy({}, {
get: (target, key) => {
if (typeof key === 'symbol') return target[key]
key = prefix + key
if (key in scope) return toValue(scope[key])
const _prefix = key + '.'
if (Object.keys(scope).some(k => k.startsWith(_prefix))) {
return this.createScope(scope, key + '.')
}
},
})
createScope(override = {}) {
const scope = { ...this.ctx.internal.scope, ...override }
return createScope(scope)
}
}

function createScope(scope: Store<ActionContext>, prefix = '') {
return new Proxy({}, {
get: (target, key) => {
if (typeof key === 'symbol') return target[key]
key = prefix + key
if (key in scope) return toValue(scope[key])
const _prefix = key + '.'
if (Object.keys(scope).some(k => k.startsWith(_prefix))) {
return createScope(scope, key + '.')
}
},
})
}

0 comments on commit eafc280

Please sign in to comment.