import { computed, defineComponent, inject, onMounted, 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 useLink from '../../utils/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'
import UploadAndCut from '@/utils/UploadAndCut'
import { AddToToken } from './AdderPage'
import DefaultImg from '/tab/icons/bgGameCloud.png'
import useAdderPageStore, { type EditBlockItemType } from './useAdderPageStore'
import useRouterStore from '@/useRouterStore'
import NativeColorPicker from '@/utils/NativeColorPicker'
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) {
const layout = useLayoutStore()
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)
}}
> */}
{
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 文字
} as EditBlockItemType)
const isGame = computed(() => layout.state.current === 0)
const debounced = ref('')
const store = useAdderPageStore()
onMounted(() => {
if (store.editBlockItem !== null) {
form.link = store.editBlockItem.link
form.name = store.editBlockItem.name
form.icon = store.editBlockItem.icon
form.text = store.editBlockItem.text
form.background = store.editBlockItem.background
form.color = store.editBlockItem.color
form.type = store.editBlockItem.type
}
})
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
})
watch(
() => form.type,
(cur, pre) => {
if (cur === 1) {
if (!form.name) {
globalToast.error('文字图标请至少填写文字或者名称')
form.type = pre
return
} else {
form.text = form.name.substring(0, 2).toLocaleUpperCase()
}
}
}
)
const addTo = inject(AddToToken)
return () => (
{
debounced.value = debounced.value + ' '
}}
>
获取地址
{form.type === 1 && (
<>
{
form.background = e
}}
>
>
)}
}
onClick={() => {
if (form.type === 1 && !form.text && !form.name) {
globalToast.error('文字图标请至少填写文字或者名称')
return
}
if (!form.link) {
globalToast.warning('请输入网站链接')
return
}
if (!form.name) {
globalToast.warning('请输入网站名称')
return
}
if (store.editBlockItem !== null) {
console.log(123)
layout.changeBlock(form, store.editBlockItem.id)
useRouterStore().back()
store.editBlockItem = null
} else {
const id = uuid()
const data: Block = {
id,
link: form.link,
name: '',
icon: form.type === 0 ? form.icon || info.icon : '',
background: form.type === 0 ? '' : form.background,
color: form.type === 0 ? '' : form.color,
w: 1,
h: 1,
text:
form.type === 0
? ''
: form.text || form.name.substring(0, 2).toLocaleUpperCase(),
label: form.name
}
layout.addBlock(data, addTo?.value)
}
}}
>
确定
)
})