|
1 | 1 | import { useI18n } from 'vue-i18n'
|
2 |
| -import { computed, ref } from 'vue' |
| 2 | +import { h, computed, ref, type VNode, isVNode, resolveComponent } from 'vue' |
3 | 3 | import { defineStore } from 'pinia'
|
4 | 4 |
|
5 | 5 | import { useEnvStore } from './env'
|
@@ -41,6 +41,42 @@ export const useAppStore = defineStore('app', () => {
|
41 | 41 | y: 0,
|
42 | 42 | })
|
43 | 43 |
|
| 44 | + /* Actions */ |
| 45 | + interface CustomAction { |
| 46 | + component: string |
| 47 | + componentProps?: Recordable |
| 48 | + componentSlots?: Recordable |
| 49 | + } |
| 50 | + type CustomActionSlot = |
| 51 | + | (({ h }: any) => VNode | string | number | boolean) |
| 52 | + | VNode |
| 53 | + | string |
| 54 | + | number |
| 55 | + | boolean |
| 56 | + const customActions: { [key: string]: CustomAction[] } = { |
| 57 | + core_state: [], |
| 58 | + } |
| 59 | + const addCustomActions = (target: string, actions: CustomAction | CustomAction[]) => { |
| 60 | + if (!customActions[target]) throw new Error('Target does not exist: ' + target) |
| 61 | + const _actions = Array.isArray(actions) ? actions : [actions] |
| 62 | + customActions[target].push(..._actions) |
| 63 | + const remove = () => { |
| 64 | + customActions[target] = customActions[target].filter((a) => !_actions.includes(a)) |
| 65 | + } |
| 66 | + return remove |
| 67 | + } |
| 68 | + const renderCustomActionSlot = (slot: CustomActionSlot) => { |
| 69 | + let result: CustomActionSlot = slot |
| 70 | + if (typeof result === 'function') { |
| 71 | + const customH = (type: any, ...args: any[]) => h(resolveComponent(type), ...args) |
| 72 | + result = result({ h: customH }) |
| 73 | + } |
| 74 | + if (isVNode(result)) { |
| 75 | + return result |
| 76 | + } |
| 77 | + return h('div', result) |
| 78 | + } |
| 79 | + |
44 | 80 | const { t } = useI18n()
|
45 | 81 | const envStore = useEnvStore()
|
46 | 82 |
|
@@ -146,5 +182,8 @@ export const useAppStore = defineStore('app', () => {
|
146 | 182 | updatable,
|
147 | 183 | checkForUpdates,
|
148 | 184 | downloadApp,
|
| 185 | + customActions, |
| 186 | + addCustomActions, |
| 187 | + renderCustomActionSlot, |
149 | 188 | }
|
150 | 189 | })
|
0 commit comments