Skip to content

Commit

Permalink
feat(console): support dict schema
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Jan 31, 2022
1 parent 4ecaf66 commit d5c2ff3
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 18 deletions.
2 changes: 1 addition & 1 deletion plugins/frontend/console/client/components/form/button.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<template>
<button
@click="onClick"
@click.prevent="onClick"
:title="disabled ? '' : title"
:class="['k-button', type, { disabled, solid, round, frameless }]">
<slot/>
Expand Down
4 changes: 3 additions & 1 deletion plugins/frontend/console/client/components/form/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ import Input from './input.vue'
import TabItem from './tab-item.vue'
import TabGroup from './tab-group.vue'
import Radio from './radio.vue'
import Schema from './schema/index.vue'
import Form from './schema/form.vue'
import Schema from './schema/schema.vue'
import { App } from 'vue'

export default function (app: App) {
app.component('k-button', Button)
app.component('k-checkbox', Checkbox)
app.component('k-form', Form)
app.component('k-input', Input)
app.component('k-radio', Radio)
app.component('k-schema', Schema)
Expand Down
2 changes: 2 additions & 0 deletions plugins/frontend/console/client/components/form/input.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
<slot name="prefix"></slot>
</span>
<input
autocomplete="off"
step="0.00001"
:value="modelValue"
:type="type"
:style="inputStyle"
Expand Down
36 changes: 36 additions & 0 deletions plugins/frontend/console/client/components/form/schema/form.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<template>
<form class="schema-form">
<slot name="header"></slot>
<k-schema :schema="schema" v-model="config"></k-schema>
<slot name="footer"></slot>
</form>
</template>

<script lang="ts" setup>
import { computed, PropType } from 'vue'
import Schema from 'schemastery'
const props = defineProps({
schema: {} as PropType<Schema>,
modelValue: {},
})
const emit = defineEmits(['update:modelValue'])
const config = computed({
get: () => props.modelValue,
set: emit.bind(null, 'update:modelValue'),
})
</script>

<style lang="scss">
.schema-form {
h2 {
font-size: 1.25rem;
}
}
</style>
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<template>
<template v-if="!schema || schema.meta.hidden"/>

<schema-primitive v-else-if="['string', 'number', 'boolean'].includes(schema.type)" :schema="schema" v-model="config">
<schema-primitive v-else-if="isPrimitive(schema)" :schema="schema" v-model="config">
<slot></slot>
</schema-primitive>

<div class="schema-item" v-else-if="schema.type === 'array'">
<div class="schema-item" v-else-if="schema.type === 'array' && isPrimitive(schema.inner)">
<div class="schema-header">
<div class="left">
<slot></slot>
Expand All @@ -22,6 +22,24 @@
</ul>
</div>

<div class="schema-item" v-else-if="schema.type === 'dict' && isPrimitive(schema.inner)">
<div class="schema-header">
<div class="left">
<slot></slot>
</div>
<div class="right">
<k-button solid @click="config[''] = null">添加项</k-button>
</div>
</div>
<ul>
<li v-for="(_, key) in config">
<k-icon name="times-full" class="remove" @click="delete config[key]"></k-icon>
<k-input :model-value="key" @update:model-value="v => (config[v] = config[key], delete config[key])" class="hidden"></k-input>
<k-input v-model="config[key]" class="hidden"></k-input>
</li>
</ul>
</div>

<template v-else-if="schema.type === 'object'">
<h2 v-if="!noDesc && schema.meta.description">{{ schema.meta.description }}</h2>
<k-schema v-for="(item, key) in schema.dict" :key="key" :schema="item" v-model="config[key]" :prefix="prefix + key + '.'">
Expand All @@ -32,8 +50,6 @@
</k-schema>
</template>

<template v-else-if="schema.type === 'const'"></template>

<template v-else-if="schema.type === 'intersect'">
<k-schema v-for="item in schema.list" :schema="item" v-model="config" :prefix="prefix"/>
</template>
Expand Down Expand Up @@ -88,6 +104,10 @@ watch(config, (value) => {
emit('update:modelValue', value)
}, { deep: true })
function isPrimitive(schema: Schema) {
return ['string', 'number', 'boolean'].includes(schema.type)
}
</script>

<style lang="scss">
Expand Down
15 changes: 6 additions & 9 deletions plugins/frontend/manager/client/bots/settings.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,14 @@
配置项
<template v-if="data.config.disabled">
<k-button solid @click="execute('login')">登陆账号</k-button>
<k-button solid @click="execute('logout')">保存配置</k-button>
</template>
<template v-else>
<k-button solid type="error" @click="execute('logout')">登出账号</k-button>
<k-button solid @click="execute('login')">重载配置</k-button>
</template>
</h1>
{{ data }}
<form>
<template v-if="!current">
<k-form :schema="store.protocols[key]" v-model="data.config">
<template #header v-if="!current">
<k-schema :schema="adapterSchema" v-model="data.adapter">
<h3 class="required">adapter</h3>
<p>选择要使用的适配器。</p>
Expand All @@ -22,8 +20,7 @@
<p>选择要使用的协议。</p>
</k-schema>
</template>
<k-schema :schema="store.protocols[key]" v-model="data.config"></k-schema>
</form>
</k-form>
</template>

<script lang="ts" setup>
Expand Down Expand Up @@ -64,16 +61,16 @@ const protocolSchema = computed(() => {
const data = computed<Partial<BotProvider.Data>>(() => store.bots[props.current] || reactive({
adapter: '',
config: {},
config: { disabled: true },
}))
watch(() => data.value.adapter, () => {
data.value.config = { protocol: '' }
data.value.config = { protocol: '', disabled: true }
})
watch(() => data.value.config.protocol, (protocol) => {
if (!protocol) return
data.value.config = { protocol }
data.value.config = { protocol, disabled: true }
}, { flush: 'post' })
const key = computed(() => {
Expand Down
4 changes: 1 addition & 3 deletions plugins/frontend/manager/client/settings/settings.vue
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,7 @@
<k-button solid @click="execute('unload')">保存配置</k-button>
</template>
</h1>
<form>
<k-schema :schema="data.schema" v-model="data.config" prefix=""></k-schema>
</form>
<k-form :schema="data.schema" v-model="data.config"></k-form>
</template>
</k-content>
</template>
Expand Down

0 comments on commit d5c2ff3

Please sign in to comment.