Skip to content

Commit f4efdda

Browse files
committed
String and List option
1 parent a9ace87 commit f4efdda

File tree

6 files changed

+173
-1
lines changed

6 files changed

+173
-1
lines changed

src/BasicConfig.vue

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@
22
import { computed } from 'vue'
33
import { NAlert, NForm, NFormItem } from 'naive-ui'
44
import type { Config } from 'fcitx5-js'
5+
import TooltipButton from './TooltipButton.vue'
56
import IntegerOption from './option/IntegerOption.vue'
67
import BooleanOption from './option/BooleanOption.vue'
78
import EnumOption from './option/EnumOption.vue'
89
import KeyOption from './option/KeyOption.vue'
10+
import StringOption from './option/StringOption.vue'
11+
import ListOption from './option/ListOption.vue'
912
import GroupOption from './option/GroupOption.vue'
1013
import UnknownOption from './option/UnknownOption.vue'
1114
import { isMobile } from './util'
@@ -29,7 +32,12 @@ function toComponent(child: { Type: string, Children: any[] | null }) {
2932
return EnumOption
3033
case 'Key':
3134
return KeyOption
35+
case 'String':
36+
return StringOption
3237
default: {
38+
if (child.Type.startsWith('List|')) {
39+
return ListOption
40+
}
3341
if (child.Children) {
3442
return GroupOption
3543
}
@@ -51,8 +59,14 @@ function toComponent(child: { Type: string, Children: any[] | null }) {
5159
<NFormItem
5260
v-for="child in config.Children"
5361
:key="`${path}/${child.Option}`"
54-
:label="child.Description"
5562
>
63+
<template #label>
64+
{{ child.Description }}
65+
<TooltipButton
66+
v-if="child.Tooltip"
67+
:text="child.Tooltip"
68+
/>
69+
</template>
5670
<component
5771
:is="toComponent(child)"
5872
:config="child"

src/TooltipButton.vue

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<script setup lang="ts">
2+
import { NButton, NTooltip } from 'naive-ui'
3+
4+
defineProps<{
5+
text: string
6+
}>()
7+
</script>
8+
9+
<template>
10+
<NTooltip trigger="hover">
11+
<template #trigger>
12+
<NButton
13+
secondary
14+
circle
15+
size="tiny"
16+
>
17+
?
18+
</NButton>
19+
</template>
20+
{{ text }}
21+
</NTooltip>
22+
</template>

src/UpButton.vue

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<script setup lang="ts">
2+
import { NButton, NIcon } from 'naive-ui'
3+
import UpIcon from './UpIcon.vue'
4+
</script>
5+
6+
<template>
7+
<NButton quaternary>
8+
<template #icon>
9+
<NIcon>
10+
<UpIcon />
11+
</NIcon>
12+
</template>
13+
</NButton>
14+
</template>

src/UpIcon.vue

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<script lang="ts">
2+
export default {
3+
name: 'IcBaselineArrowUpward',
4+
}
5+
</script>
6+
7+
<template>
8+
<svg xmlns="http://www.w3.org/2000/svg" width="1em" height="1em" viewBox="0 0 24 24"><path fill="currentColor" d="m4 12l1.41 1.41L11 7.83V20h2V7.83l5.58 5.59L20 12l-8-8z" /></svg>
9+
</template>

src/option/ListOption.vue

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
<script setup lang="ts">
2+
import { NButtonGroup, NList, NListItem } from 'naive-ui'
3+
import UpButton from '../UpButton.vue'
4+
import MinusButton from '../MinusButton.vue'
5+
import PlusButton from '../PlusButton.vue'
6+
import KeyOption from './KeyOption.vue'
7+
import StringOption from './StringOption.vue'
8+
import UnknownOption from './UnknownOption.vue'
9+
10+
const props = defineProps<{
11+
config: {
12+
Type: string
13+
}
14+
value: { [key: string]: string }
15+
onUpdate: (value: { [key: string]: string }) => void
16+
}>()
17+
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+
29+
function move(index: number) {
30+
props.onUpdate({ ...props.value, [index - 1]: props.value[index], [index]: props.value[index - 1] })
31+
}
32+
33+
function remove(index: number) {
34+
const { length } = Object.keys(props.value)
35+
const value: { [key: string]: string } = {}
36+
for (let i = 0; i < index; ++i) {
37+
value[i] = props.value[i]
38+
}
39+
for (let i = index; i < length - 1; ++i) {
40+
value[i] = props.value[i + 1]
41+
}
42+
props.onUpdate(value)
43+
}
44+
45+
function add(index: number) {
46+
const { length } = Object.keys(props.value)
47+
const value: { [key: string]: string } = {}
48+
for (let i = 0; i < index; ++i) {
49+
value[i] = props.value[i]
50+
}
51+
value[index] = ''
52+
for (let i = index; i < length; ++i) {
53+
value[i + 1] = props.value[i]
54+
}
55+
props.onUpdate(value)
56+
}
57+
</script>
58+
59+
<template>
60+
<NList style="width: 100%; padding: 0 12px">
61+
<NListItem
62+
v-for="[i, item] of Object.entries(value)"
63+
:key="i"
64+
>
65+
<component
66+
:is="toComponent(config.Type)"
67+
:value="item"
68+
@update="v => onUpdate({ ...value, [i]: v })"
69+
/>
70+
<template #suffix>
71+
<NButtonGroup>
72+
<UpButton
73+
:disabled="Number(i) === 0"
74+
size="small"
75+
@click="move(Number(i))"
76+
/>
77+
<MinusButton
78+
size="small"
79+
@click="remove(Number(i))"
80+
/>
81+
<PlusButton
82+
size="small"
83+
@click="add(Number(i))"
84+
/>
85+
</NButtonGroup>
86+
</template>
87+
</NListItem>
88+
<NListItem>
89+
<div style="width: 100%" />
90+
<template #suffix>
91+
<PlusButton
92+
size="small"
93+
@click="add(Object.keys(value).length)"
94+
/>
95+
</template>
96+
</NListItem>
97+
</NList>
98+
</template>

src/option/StringOption.vue

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<script setup lang="ts">
2+
import { NInput } from 'naive-ui'
3+
4+
defineProps<{
5+
value: string
6+
onUpdate: (value: string) => void
7+
}>()
8+
</script>
9+
10+
<template>
11+
<NInput
12+
:value="value"
13+
@update:value="onUpdate"
14+
/>
15+
</template>

0 commit comments

Comments
 (0)