Skip to content

Commit

Permalink
feat(client): support tagged union
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Jul 11, 2022
1 parent e94763f commit 91aad42
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 18 deletions.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,15 @@
"yakumo": "^0.2.8",
"yakumo-mocha": "^0.2.5",
"yakumo-publish": "^0.2.4",
"yakumo-tsc": "^0.2.4",
"yakumo-upgrade": "^0.2.3",
"yakumo-version": "^0.2.5",
"yml-register": "^1.0.0"
},
"yakumo": {
"require": [
"esbuild-register"
"esbuild-register",
"yml-register"
]
}
}
6 changes: 3 additions & 3 deletions plugins/adapter/kook/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import KaiheilaBot from '@satorijs/adapter-kaiheila'
import KookBot from '@satorijs/adapter-kook'

export default KaiheilaBot
export * from '@satorijs/adapter-kaiheila'
export default KookBot
export * from '@satorijs/adapter-kook'
6 changes: 1 addition & 5 deletions plugins/frontend/client/client/components/form/primitive.vue
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
<template>
<template v-if="schema.type === 'const'">
{{ schema.value }}
</template>

<el-switch v-else-if="schema.type === 'boolean'" v-model="value" :disabled="disabled"></el-switch>
<el-switch v-if="schema.type === 'boolean'" v-model="value" :disabled="disabled"></el-switch>

<template v-else-if="schema.type === 'number'">
<el-slider v-if="schema.meta.role === 'slider'" style="width: 200px"
Expand Down
10 changes: 7 additions & 3 deletions plugins/frontend/client/client/components/form/schema.vue
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@
</k-schema>
</template>

<schema-item v-else-if="prefix || !isComposite" :disabled="disabled" :class="{ changed, required, invalid }" @command="handleCommand">
<template v-else-if="schema.type === 'const'"></template>

<schema-item v-else-if="prefix || (!isComposite && schema?.type !== 'union')" :disabled="disabled" :class="{ changed, required, invalid }" @command="handleCommand">
<template #menu>
<el-dropdown-item command="discard">撤销更改</el-dropdown-item>
<el-dropdown-item command="default">恢复默认值</el-dropdown-item>
Expand Down Expand Up @@ -96,6 +98,7 @@
</schema-group>
</div>

<!-- top level array / dict -->
<template v-else>
<h2>
{{ schema.meta.description || '配置列表' }}
Expand All @@ -107,6 +110,7 @@
</template>
</template>

<!-- union containing object -->
<template v-else-if="schema?.type === 'union' && choices.length > 1 && ['object', 'intersect'].includes(active?.type)">
<k-schema
v-model="config"
Expand Down Expand Up @@ -203,7 +207,7 @@ watch(() => props.modelValue, (value) => {
} catch {}
}
config.value = value ?? getFallback(props.schema)
}, { immediate: true })
}, { immediate: true, deep: true })
watch(config, (value) => {
if (!props.schema) return
Expand All @@ -215,7 +219,7 @@ watch(config, (value) => {
}, { deep: true })
function isPrimitive(schema: Schema) {
return ['string', 'number', 'boolean', 'const'].includes(schema.type)
return ['string', 'number', 'boolean'].includes(schema.type)
}
function handleCommand(action: string) {
Expand Down
9 changes: 3 additions & 6 deletions plugins/frontend/client/client/components/form/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,14 @@ export function isObjectSchema(schema: Schema) {
} else if (schema.type === 'intersect') {
return schema.list.every(isObjectSchema)
} else if (schema.type === 'union') {
const choices = schema.list.filter(item => !dynamic.includes(item.type))
return choices.length === 1 && isObjectSchema(choices[0])
return getChoices(schema).every(isObjectSchema)
} else {
return false
}
}

export function getChoices(schema: Schema) {
return schema.list
.filter(item => !item.meta.hidden && !dynamic.includes(item.type))
.flatMap(item => item.type === 'union' ? getChoices(item) : item, 1)
return schema.list.filter(item => !item.meta.hidden && !dynamic.includes(item.type))
}

export function getFallback(schema: Schema, required = false) {
Expand All @@ -47,7 +44,7 @@ export function validate(schema: Schema): boolean {
return schema.list.every(isObjectSchema)
} else if (schema.type === 'union') {
const choices = getChoices(schema)
return choices.length === 1 || choices.every(item => validate(item) && (item.type === 'const' || item.meta.description))
return choices.length === 1 || choices.every(item => validate(item))
} else if (composite.includes(schema.type)) {
return validate(schema.inner)
} else {
Expand Down

0 comments on commit 91aad42

Please sign in to comment.