Skip to content

Commit 11556b7

Browse files
committed
refactor reset; GroupOption
1 parent 402877b commit 11556b7

File tree

3 files changed

+69
-41
lines changed

3 files changed

+69
-41
lines changed

src/BasicConfig.vue

Lines changed: 14 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,39 @@
11
<script setup lang="ts">
2-
import { computed, ref, watchEffect } from 'vue'
2+
import { computed } from 'vue'
33
import { NAlert, NForm, NFormItem } from 'naive-ui'
44
import type { Config } from 'fcitx5-js'
55
import IntegerOption from './option/IntegerOption.vue'
66
import BooleanOption from './option/BooleanOption.vue'
77
import EnumOption from './option/EnumOption.vue'
8+
import GroupOption from './option/GroupOption.vue'
89
import UnknownOption from './option/UnknownOption.vue'
910
import { isMobile } from './util'
1011
11-
const props = defineProps<{
12+
defineProps<{
1213
path: string
1314
config: Config
15+
value: any
16+
onUpdate: (value: any) => void
1417
}>()
1518
1619
const labelPlacement = computed(() => isMobile.value ? 'top' : 'left')
1720
18-
function toComponent(type: string) {
19-
switch (type) {
21+
function toComponent(child: { Type: string, Children: any[] | null }) {
22+
switch (child.Type) {
2023
case 'Integer':
2124
return IntegerOption
2225
case 'Boolean':
2326
return BooleanOption
2427
case 'Enum':
2528
return EnumOption
26-
default:
29+
default: {
30+
if (child.Children) {
31+
return GroupOption
32+
}
2733
return UnknownOption
28-
}
29-
}
30-
31-
function extractValue(reset: boolean) {
32-
const value: { [key: string]: any } = {}
33-
if ('Children' in props.config) {
34-
for (const child of props.config.Children) {
35-
value[child.Option] = reset ? child.DefaultValue : child.Value
3634
}
3735
}
38-
return value
39-
}
40-
41-
const form = ref<{ [key: string]: any }>({})
42-
43-
watchEffect(() => {
44-
form.value = extractValue(false)
45-
})
46-
47-
function reset() {
48-
form.value = extractValue(true)
4936
}
50-
51-
function get() {
52-
return form.value
53-
}
54-
55-
defineExpose({
56-
reset,
57-
get,
58-
})
5937
</script>
6038

6139
<template>
@@ -64,7 +42,6 @@ defineExpose({
6442
</NAlert>
6543
<NForm
6644
v-else
67-
:model="form"
6845
:label-placement="labelPlacement"
6946
label-width="200px"
7047
>
@@ -74,10 +51,10 @@ defineExpose({
7451
:label="child.Description"
7552
>
7653
<component
77-
:is="toComponent(child.Type)"
54+
:is="toComponent(child)"
7855
:config="child"
79-
:value="form[child.Option]"
80-
:on-update="(v) => { form[child.Option] = v }"
56+
:value="value[child.Option]"
57+
@update="v => onUpdate({ ...value, [child.Option]: v })"
8158
/>
8259
</NFormItem>
8360
</NForm>

src/InputMethodConfig.vue

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<script lang="ts">
22
import { computed, h, ref, watchEffect } from 'vue'
33
import type { MenuOption } from 'naive-ui'
4+
import type { Config } from 'fcitx5-js'
45
import { NButton, NCheckbox, NCheckboxGroup, NFlex, NLayout, NLayoutFooter, NLayoutSider, NMenu } from 'naive-ui'
56
import MinusButton from './MinusButton.vue'
67
import PlusButton from './PlusButton.vue'
@@ -134,10 +135,32 @@ const filteredLanguageOptions = computed(() => {
134135
return languageOptions.value
135136
})
136137
137-
const basicConfig = ref()
138+
const form = ref({})
139+
140+
function extractValue(config: Config, reset: boolean) {
141+
const value: { [key: string]: any } = {}
142+
if ('Children' in config) {
143+
for (const child of config.Children) {
144+
value[child.Option] = reset
145+
? (
146+
'DefaultValue' in child ? child.DefaultValue : extractValue(child, true)
147+
)
148+
: child.Value
149+
}
150+
}
151+
return value
152+
}
153+
154+
watchEffect(() => {
155+
form.value = extractValue(config.value, false)
156+
})
157+
158+
function reset() {
159+
form.value = extractValue(config.value, true)
160+
}
138161
139162
function apply() {
140-
window.fcitx.setConfig(uri.value, basicConfig.value.get())
163+
window.fcitx.setConfig(uri.value, form.value)
141164
}
142165
143166
function confirm() {
@@ -234,10 +257,11 @@ function confirm() {
234257
<template v-else>
235258
<NLayout position="absolute" style="bottom: 50px">
236259
<BasicConfig
237-
ref="basicConfig"
238260
:path="selectedInputMethod"
239261
:config="config"
262+
:value="form"
240263
style="margin: 16px"
264+
@update="v => form = v"
241265
/>
242266
</NLayout>
243267
<NLayoutFooter position="absolute">
@@ -247,7 +271,7 @@ function confirm() {
247271
<NFlex>
248272
<NButton
249273
secondary
250-
@click="basicConfig.reset()"
274+
@click="reset()"
251275
>
252276
Reset to default
253277
</NButton>

src/option/GroupOption.vue

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<script setup lang="ts">
2+
import { ref } from 'vue'
3+
import { NCard } from 'naive-ui'
4+
import BasicConfig from '../BasicConfig.vue'
5+
6+
defineProps<{
7+
config: {
8+
Children: any[]
9+
}
10+
value: any
11+
onUpdate: (value: any) => void
12+
}>()
13+
14+
const basicConfig = ref()
15+
</script>
16+
17+
<template>
18+
<NCard>
19+
<BasicConfig
20+
ref="basicConfig"
21+
path=""
22+
:config="config"
23+
:value="value"
24+
@update="onUpdate"
25+
/>
26+
</NCard>
27+
</template>

0 commit comments

Comments
 (0)