Skip to content

Commit

Permalink
feat(client): support action.hidden
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Dec 18, 2023
1 parent 2de0714 commit a3ce12d
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 21 deletions.
4 changes: 2 additions & 2 deletions packages/client/app/layout/layout.vue
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@
<template v-for="item in ctx.internal.menus[menu]" :key="menu">
<layout-menu-item
v-if="item.id !== '@separator'"
v-bind="{ ...item, ...ctx.internal.actions[item.id.startsWith('.') ? menu + item.id : item.id] }"
:item="{ ...item, ...ctx.internal.actions[item.id.startsWith('.') ? menu + item.id : item.id] }"
:menu-key="menu" :menu-data="menuData"
/>
</template>
</template>
<template v-else>
<layout-menu-item v-for="item in menu" v-bind="item" />
<layout-menu-item v-for="item in menu" :item="item" />
</template>
</slot>
</template>
Expand Down
22 changes: 14 additions & 8 deletions packages/client/app/layout/menu-item.vue
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<template>
<el-tooltip :disabled="disabled" :content="toValue(label)" placement="bottom">
<span class="menu-item" :class="[toValue(type), { disabled }]" @click="trigger">
<k-icon class="menu-icon" :name="toValue(icon)"></k-icon>
<el-tooltip v-if="!hidden" :disabled="disabled" :content="toValue(item.label)" placement="bottom">
<span class="menu-item" :class="[toValue(item.type), { disabled }]" @click="trigger">
<k-icon class="menu-icon" :name="toValue(item.icon)"></k-icon>
</span>
</el-tooltip>
</template>
Expand All @@ -11,17 +11,23 @@
import { LegacyMenuItem, MaybeGetter, useContext } from '@koishijs/client'
import { computed } from 'vue'
const props = defineProps<LegacyMenuItem & {
const props = defineProps<{
item: LegacyMenuItem
menuKey?: string
menuData?: any
}>()
const ctx = useContext()
const hidden = computed(() => {
if (!props.item.hidden) return false
return toValue(props.item.hidden)
})
const disabled = computed(() => {
if (!props.action) return true
if (!props.disabled) return false
return toValue(props.disabled)
if (!props.item.action) return true
if (!props.item.disabled) return false
return toValue(props.item.disabled)
})
function toValue<T>(getter: MaybeGetter<T>): T {
Expand All @@ -30,7 +36,7 @@ function toValue<T>(getter: MaybeGetter<T>): T {
}
function trigger() {
return props.action(ctx.internal.createScope({
return props.item.action(ctx.internal.createScope({
[props.menuKey]: props.menuData,
}))
}
Expand Down
2 changes: 1 addition & 1 deletion packages/client/app/theme/menu/index.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<template>
<template v-for="menu of ctx.internal.activeMenus" :key="id">
<template v-for="menu of ctx.internal.activeMenus" :key="menu.id">
<k-menu v-bind="menu"></k-menu>
</template>
</template>
Expand Down
10 changes: 7 additions & 3 deletions packages/client/app/theme/menu/menu-item.vue
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<template>
<div
class="k-menu-item"
v-if="forced || !disabled"
v-if="!hidden"
:class="[toValue(type), { disabled }]"
@click.prevent="item?.action(ctx.internal.createScope())"
>
Expand All @@ -19,14 +19,18 @@ const props = defineProps<MenuItem & { prefix: string }>()
const ctx = useContext()
const forced = computed(() => props.id.startsWith('!'))
const item = computed(() => {
let id = props.id.replace(/^!/, '')
if (id.startsWith('.')) id = props.prefix + id
return ctx.internal.actions[id]
})
const hidden = computed(() => {
if (!item.value) return true
if (!item.value.hidden) return false
return toValue(item.value.hidden)
})
const disabled = computed(() => {
if (!item.value) return true
if (!item.value.disabled) return false
Expand Down
2 changes: 1 addition & 1 deletion packages/client/client/components/slot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export const KSlot = defineComponent({
const external = [...ctx.internal.views[props.name] || []]
.filter(item => !item.disabled?.())
.map(item => ({
node: h(item.component, { data: props.data, ...props.data }, slots),
node: h(item.component, { ...props.data }, slots),
order: item.order,
layer: 1,
}))
Expand Down
1 change: 1 addition & 0 deletions packages/client/client/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export function useContext() {

export interface ActionOptions {
shortcut?: string
hidden?: (scope: Flatten<ActionContext>) => boolean
disabled?: (scope: Flatten<ActionContext>) => boolean
action: (scope: Flatten<ActionContext>) => any
}
Expand Down
10 changes: 5 additions & 5 deletions plugins/config/client/components/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -108,17 +108,17 @@ const ctx = useContext()
ctx.define('config.tree', current)
ctx.action('config.tree.add-plugin', {
disabled: ({ config }) => config.tree.path && !config.tree.children,
hidden: ({ config }) => config.tree.path && !config.tree.children,
action: ({ config }) => dialogSelect.value = config.tree,
})
ctx.action('config.tree.add-group', {
disabled: ({ config }) => config.tree.path && !config.tree.children,
hidden: ({ config }) => config.tree.path && !config.tree.children,
action: ({ config }) => addItem(config.tree.path, 'reload', 'group'),
})
ctx.action('config.tree.clone', {
disabled: ({ config }) => !config.tree.path || !!config.tree.children,
hidden: ({ config }) => !config.tree.path || !!config.tree.children,
action: async ({ config }) => {
const ident = Math.random().toString(36).slice(2, 8)
send('manager/unload', config.tree.parent?.path ?? '', `${config.tree.name}:${ident}`, config.tree.config)
Expand All @@ -127,7 +127,7 @@ ctx.action('config.tree.clone', {
})
ctx.action('config.tree.manage', {
disabled: ({ config }) => !config.tree.path || !!config.tree.children,
hidden: ({ config }) => !config.tree.path || !!config.tree.children,
action: async ({ config }) => {
dialogFork.value = config.tree.name
},
Expand All @@ -142,7 +142,7 @@ ctx.action('config.tree.rename', {
})
ctx.action('config.tree.remove', {
disabled: ({ config }) => !config.tree.path,
disabled: ({ config }) => !config.tree.path || hasCoreDeps(config.tree),
action: ({ config }) => remove.value = config.tree,
})
Expand Down
2 changes: 1 addition & 1 deletion plugins/config/client/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export default (ctx: Context) => {
})

ctx.menu('config.tree', [{
id: '!config.tree.toggle',
id: 'config.tree.toggle',
type: ({ config }) => config.tree?.disabled ? '' : type.value,
icon: ({ config }) => config.tree?.disabled ? 'play' : 'stop',
label: ({ config }) => (config.tree?.disabled ? '启用' : '停用')
Expand Down

0 comments on commit a3ce12d

Please sign in to comment.