编辑添加一体化,异步加载添加搜索引擎功能

This commit is contained in:
plightfield 2024-09-09 18:51:35 +08:00
parent ece577a0ae
commit 226c2a92c7
2 changed files with 97 additions and 39 deletions

View File

@ -0,0 +1,65 @@
import { defineComponent, reactive, watch } from 'vue'
import { Button, Form, Input } from 'ant-design-vue'
import asyncLoader from '@/utils/asyncLoader'
import { CheckOutlined } from '@ant-design/icons-vue'
const ImageUploader = asyncLoader(() => import('@/utils/ImageUploader'))
export default defineComponent({
props: {
selected: {
type: Object,
default: () => ({})
}
},
setup(props) {
const addState = reactive({
name: '',
url: '',
icon: ''
})
watch(
() => props.selected,
(obj) => {
if (!obj) {
addState.name = ''
addState.url = ''
addState.icon = ''
return
}
addState.name = obj.name
addState.url = obj.url
addState.icon = obj.icon
},
{ immediate: true }
)
return () => (
<Form
layout="vertical"
model={addState}
onFinish={(res) => {
console.log(res)
}}
>
<Form.Item label="名称" name="name" rules={[{ required: true, message: '引擎名必填' }]}>
<Input v-model:value={addState.name} />
</Form.Item>
<Form.Item
label="链接"
name="url"
rules={[{ required: true, message: '链接必填' }]}
help="以 %s 代替查询内容"
>
<Input v-model:value={addState.url} />
</Form.Item>
<Form.Item label="图标" name="icon">
<ImageUploader v-model:value={addState.icon} />
</Form.Item>
<Form.Item class="flex justify-end mb-0">
<Button type="primary" htmlType="submit" icon={<CheckOutlined />}>
</Button>
</Form.Item>
</Form>
)
}
})

View File

@ -10,10 +10,10 @@ import {
message, message,
Modal Modal
} from 'ant-design-vue' } from 'ant-design-vue'
import useSearchConfigStore from './useSearchConfigStore' import useSearchConfigStore, { type SearchInfo } from './useSearchConfigStore'
import { EditOutlined, DeleteOutlined, PlusOutlined, CheckOutlined } from '@ant-design/icons-vue' import { EditOutlined, DeleteOutlined, PlusOutlined, CheckOutlined } from '@ant-design/icons-vue'
import asyncLoader from '@/utils/asyncLoader' import asyncLoader from '@/utils/asyncLoader'
const ImageUploader = asyncLoader(() => import('@/utils/ImageUploader')) const SearchAdder = asyncLoader(() => import('./SearchAdder'))
const SearchItem = defineComponent({ const SearchItem = defineComponent({
props: { props: {
icon: { icon: {
@ -37,7 +37,7 @@ const SearchItem = defineComponent({
default: false default: false
} }
}, },
emits: ['update:modelValue'], emits: ['update:modelValue', 'edit'],
setup(props, ctx) { setup(props, ctx) {
const searchConfig = useSearchConfigStore() const searchConfig = useSearchConfigStore()
return () => ( return () => (
@ -84,7 +84,19 @@ const SearchItem = defineComponent({
/> />
{props.editable && ( {props.editable && (
<> <>
<Button size="small" type="link" class="ml-4" icon={<EditOutlined />}></Button> <Button
size="small"
type="link"
class="ml-4"
icon={<EditOutlined />}
onClick={() => {
ctx.emit('edit', {
icon: props.icon,
name: props.name,
url: props.url
})
}}
></Button>
<Button <Button
size="small" size="small"
type="text" type="text"
@ -110,12 +122,7 @@ const SearchItem = defineComponent({
export default defineComponent(() => { export default defineComponent(() => {
const searchConfig = useSearchConfigStore() const searchConfig = useSearchConfigStore()
const showAdder = ref(false) const showAdder = ref<{ [key: string]: any } | null | undefined>(undefined)
const addState = reactive({
name: '',
url: '',
icon: ''
})
return () => ( return () => (
<div <div
class="w-full h-full bg-white/90 backdrop-blur p-4 flex flex-col select-text" class="w-full h-full bg-white/90 backdrop-blur p-4 flex flex-col select-text"
@ -149,6 +156,9 @@ export default defineComponent(() => {
name={el.name} name={el.name}
editable editable
v-model={el.show} v-model={el.show}
onEdit={(obj) => {
showAdder.value = obj
}}
/> />
))} ))}
</div> </div>
@ -158,41 +168,24 @@ export default defineComponent(() => {
block block
icon={<PlusOutlined />} icon={<PlusOutlined />}
onClick={() => { onClick={() => {
showAdder.value = true showAdder.value = null
}} }}
> >
</Button> </Button>
</div> </div>
</div> </div>
<Modal v-model:open={showAdder.value} title="添加搜索引擎" footer={false}> <Modal
<Form open={showAdder.value !== undefined}
layout="vertical" onUpdate:open={(e) => {
model={addState} if (!e) {
onFinish={(res) => { showAdder.value = undefined
console.log(res) }
}} }}
title="添加搜索引擎"
footer={false}
> >
<Form.Item label="名称" name="name" rules={[{ required: true, message: '引擎名必填' }]}> <SearchAdder selected={showAdder.value as any} />
<Input v-model:value={addState.name} />
</Form.Item>
<Form.Item
label="链接"
name="url"
rules={[{ required: true, message: '链接必填' }]}
help="以 %s 代替查询内容"
>
<Input v-model:value={addState.url} />
</Form.Item>
<Form.Item label="图标" name="icon">
<ImageUploader v-model:value={addState.icon} />
</Form.Item>
<Form.Item class="flex justify-end mb-0">
<Button type="primary" htmlType="submit" icon={<CheckOutlined />}>
</Button>
</Form.Item>
</Form>
</Modal> </Modal>
</ConfigProvider> </ConfigProvider>
</div> </div>