import { computed, defineComponent, reactive, ref, watch } from 'vue'
import useLayoutStore from '../useLayoutStore'
import { Button, Form, Input, InputGroup } from 'ant-design-vue'
import { OhVueIcon, addIcons } from 'oh-vue-icons'
import { MdUpload, MdImage, MdCheck } from 'oh-vue-icons/icons'
import ImageUploader from '@/utils/ImageUploader'
import useLink from './useLink'
import { CheckOutlined } from '@ant-design/icons-vue'
import { v4 as uuid } from 'uuid'
import type { Block } from '../layout.types'
import { ColorPicker } from 'vue3-colorpicker'
import 'vue3-colorpicker/style.css'
import { globalToast } from '@/main'
addIcons(MdUpload, MdImage, MdCheck)
const TypeSelector = defineComponent({
props: {
value: {
type: Number,
default: 0
},
icon: {
type: String,
default: ''
},
text: {
type: String,
default: ''
},
color: {
type: String,
default: ''
},
background: {
type: String,
default: ''
},
name: {
type: String,
default: ''
}
},
emits: {
'update:value': (() => true) as (val: number) => boolean,
'update:icon': (() => true) as (val: string) => boolean
},
setup(props, ctx) {
return () => (
{
ctx.emit('update:value', 0)
}}
>
{!props.icon &&
}
{props.value === 0 && (
)}
默认
{
ctx.emit('update:value', 1)
}}
>
{props.text
? props.text.substring(0, 2)
: props.name.substring(0, 2).toLocaleUpperCase() || 'A'}
{props.value === 1 && (
)}
文字
{
ctx.emit('update:icon', e)
}}
>
自定义
)
}
})
export default defineComponent(() => {
const layout = useLayoutStore()
const form = reactive({
link: '',
name: '',
text: '',
background: 'rgb(247,169,78)',
color: 'rgb(255,255,255)',
icon: '',
type: 0 // 0 默认,1 文字
})
const isGame = computed(() => layout.state.current === 0)
const debounced = ref('')
watch(
() => form.link,
(val, _, onCleanup) => {
const it = setTimeout(() => {
debounced.value = val
}, 500)
onCleanup(() => {
clearTimeout(it)
})
}
)
const info = useLink(debounced)
watch(info, (val) => {
if (val.name) form.name = val.name
if (val.icon) form.icon = val.icon
})
return () => (
{form.type === 1 && (
<>
>
)}
}
onClick={() => {
if (form.type === 1 && !form.text && !form.name) {
globalToast.error('文字图标请至少填写文字或者名称')
return
}
const id = uuid()
const data: Block = {
link: form.link,
name: '',
icon: form.type === 0 ? info.icon : '',
background: form.type === 0 ? '' : form.background,
color: form.type === 0 ? '' : form.color,
text:
form.type === 0 ? '' : form.text || form.name.substring(0, 2).toLocaleUpperCase(),
label: form.name,
extra: {
id
}
}
layout.addBlock(data)
}}
>
确定
)
})