Skip to content

Commit 96e4507

Browse files
committed
EntryOption; unify toComponent
1 parent 0ecf114 commit 96e4507

File tree

5 files changed

+90
-54
lines changed

5 files changed

+90
-54
lines changed

src/BasicConfig.vue

Lines changed: 1 addition & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,7 @@ import { computed } from 'vue'
33
import { NAlert, NDialogProvider, NForm, NFormItem } from 'naive-ui'
44
import type { Config } from 'fcitx5-js'
55
import TooltipButton from './TooltipButton.vue'
6-
import IntegerOption from './option/IntegerOption.vue'
7-
import BooleanOption from './option/BooleanOption.vue'
8-
import EnumOption from './option/EnumOption.vue'
9-
import KeyOption from './option/KeyOption.vue'
10-
import StringOption from './option/StringOption.vue'
11-
import ExternalOption from './option/ExternalOption.vue'
12-
import ListOption from './option/ListOption.vue'
13-
import GroupOption from './option/GroupOption.vue'
14-
import UnknownOption from './option/UnknownOption.vue'
15-
import { isMobile } from './util'
6+
import { isMobile, toComponent } from './util'
167
178
defineProps<{
189
path: string
@@ -22,32 +13,6 @@ defineProps<{
2213
}>()
2314
2415
const labelPlacement = computed(() => isMobile.value ? 'top' : 'left')
25-
26-
function toComponent(child: { Type: string, Children: any[] | null }) {
27-
switch (child.Type) {
28-
case 'Integer':
29-
return IntegerOption
30-
case 'Boolean':
31-
return BooleanOption
32-
case 'Enum':
33-
return EnumOption
34-
case 'Key':
35-
return KeyOption
36-
case 'String':
37-
return StringOption
38-
case 'External':
39-
return ExternalOption
40-
default: {
41-
if (child.Type.startsWith('List|')) {
42-
return ListOption
43-
}
44-
if (child.Children) {
45-
return GroupOption
46-
}
47-
return UnknownOption
48-
}
49-
}
50-
}
5116
</script>
5217

5318
<template>

src/option/EntryOption.vue

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<script setup lang="ts">
2+
import { NFlex } from 'naive-ui'
3+
import { toComponent } from '../util'
4+
5+
defineProps<{
6+
config: {
7+
Children: {
8+
Description: string
9+
Option: string
10+
Type: string
11+
}[]
12+
}
13+
value: any
14+
onUpdate: (value: any) => void
15+
}>()
16+
</script>
17+
18+
<template>
19+
<NFlex>
20+
<component
21+
:is="toComponent(child)"
22+
v-for="child of config.Children"
23+
:key="child.Option"
24+
:placeholder="child.Description"
25+
:config="child"
26+
:value="value[child.Option]"
27+
style="max-width: 200px"
28+
@update="v => { console.log(v); onUpdate({ ...value, [child.Option]: v }) }"
29+
/>
30+
</NFlex>
31+
</template>

src/option/ExternalOption.vue

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<script setup lang="ts">
22
import { h, ref } from 'vue'
3-
import { useDialog } from 'naive-ui'
3+
import { NScrollbar, useDialog } from 'naive-ui'
44
import GearButton from '../GearButton.vue'
55
import BasicConfig from '../BasicConfig.vue'
66
import FooterButtons from '../FooterButtons.vue'
@@ -26,14 +26,18 @@ function click() {
2626
form.value = extractValue(config, false)
2727
const instance = dialog.info({
2828
title: props.config.Description,
29-
content: () => h(BasicConfig, {
29+
content: () => h(NScrollbar, {
30+
style: {
31+
'max-height': 'calc(100vh - 200px)',
32+
},
33+
}, () => h(BasicConfig, {
3034
path: props.config.Option,
3135
config,
3236
value: form.value,
3337
onUpdate(v) {
3438
form.value = v
3539
},
36-
}),
40+
})),
3741
action: () => h(FooterButtons, {
3842
reset() {
3943
form.value = extractValue(config, true)
@@ -45,6 +49,12 @@ function click() {
4549
window.fcitx.setConfig(props.config.External, form.value)
4650
},
4751
}),
52+
actionStyle: {
53+
display: 'block', // separate 4 buttons
54+
},
55+
style: {
56+
width: 'auto', // KeyOption overflows on Desktop
57+
},
4858
})
4959
}
5060
else {

src/option/ListOption.vue

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@ import { NButtonGroup, NList, NListItem } from 'naive-ui'
33
import UpButton from '../UpButton.vue'
44
import MinusButton from '../MinusButton.vue'
55
import PlusButton from '../PlusButton.vue'
6-
import KeyOption from './KeyOption.vue'
7-
import StringOption from './StringOption.vue'
8-
import UnknownOption from './UnknownOption.vue'
6+
import { toComponent } from '../util'
97
108
const props = defineProps<{
119
config: {
@@ -15,17 +13,6 @@ const props = defineProps<{
1513
onUpdate: (value: { [key: string]: string }) => void
1614
}>()
1715
18-
function toComponent(type: string) {
19-
switch (type) {
20-
case 'List|Key':
21-
return KeyOption
22-
case 'List|String':
23-
return StringOption
24-
default:
25-
return UnknownOption
26-
}
27-
}
28-
2916
function move(index: number) {
3017
props.onUpdate({ ...props.value, [index - 1]: props.value[index], [index]: props.value[index - 1] })
3118
}
@@ -63,7 +50,8 @@ function add(index: number) {
6350
:key="i"
6451
>
6552
<component
66-
:is="toComponent(config.Type)"
53+
:is="toComponent({ Type: config.Type.slice('List|'.length) })"
54+
:config="config"
6755
:value="item"
6856
@update="v => onUpdate({ ...value, [i]: v })"
6957
/>

src/util.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
11
import { computed } from 'vue'
22
import { useBreakpoint } from 'vooks'
33
import type { Config } from 'fcitx5-js'
4+
import IntegerOption from './option/IntegerOption.vue'
5+
import BooleanOption from './option/BooleanOption.vue'
6+
import EnumOption from './option/EnumOption.vue'
7+
import KeyOption from './option/KeyOption.vue'
8+
import StringOption from './option/StringOption.vue'
9+
import ExternalOption from './option/ExternalOption.vue'
10+
import ListOption from './option/ListOption.vue'
11+
import EntryOption from './option/EntryOption.vue'
12+
import GroupOption from './option/GroupOption.vue'
13+
import UnknownOption from './option/UnknownOption.vue'
414

515
const breakpoint = useBreakpoint()
616
export const isMobile = computed(() => breakpoint.value === 'xs' || breakpoint.value === 's')
@@ -18,3 +28,35 @@ export function extractValue(config: Config, reset: boolean) {
1828
}
1929
return value
2030
}
31+
32+
export function toComponent(child: { Type: string, Children?: any[] | null } & { [key: string]: string }) {
33+
switch (child.Type) {
34+
case 'Integer':
35+
return IntegerOption
36+
case 'Boolean':
37+
return BooleanOption
38+
case 'Enum':
39+
return EnumOption
40+
case 'Key':
41+
return KeyOption
42+
case 'String':
43+
if (child.IsEnum === 'True') {
44+
return EnumOption
45+
}
46+
return StringOption
47+
case 'External':
48+
return ExternalOption
49+
default: {
50+
if (child.Type.startsWith('List|')) {
51+
return ListOption
52+
}
53+
if (child.Type.startsWith('Entries')) {
54+
return EntryOption
55+
}
56+
if (child.Children) {
57+
return GroupOption
58+
}
59+
return UnknownOption
60+
}
61+
}
62+
}

0 commit comments

Comments
 (0)