diff --git a/src/hooks/useLocalFonts.ts b/src/hooks/useLocalFonts.ts new file mode 100644 index 0000000..961696f --- /dev/null +++ b/src/hooks/useLocalFonts.ts @@ -0,0 +1,51 @@ +import { onMounted, ref } from 'vue' + +export function useLocalFonts() { + const fonts = ref>(new Map()) + const disabled = ref(false) + + const formatFonts = (list: FontData[]): Map => { + const res: Map = new Map() + for (const item of list) { + if (!res.has(item.family)) { + res.set(item.family, []) + } + + const fontArray = res.get(item.family) + if (!Array.isArray(fontArray)) { + continue + } + if (item.family === item.fullName) { + fontArray.push({ name: item.style, value: item.fullName }) + continue + } + const name = item.fullName.replace(item.family, '').trim() + const value = item.fullName + fontArray.push({ name, value }) + } + return res + } + const getFonts = async () => { + if (!window.queryLocalFonts) { + return + } + const list = await window.queryLocalFonts() + console.log('list', list) + const res = formatFonts(list) + console.log('fontlist', res) + fonts.value = res + // 对比family,相同则移入第一个元素的children里面 + } + + onMounted(() => { + if (!window.queryLocalFonts) { + disabled.value = true + } + }) + + return { + disabled, + fonts, + getFonts, + } +} diff --git a/src/store/globalConfig.ts b/src/store/globalConfig.ts index de55b4c..5f02dd9 100644 --- a/src/store/globalConfig.ts +++ b/src/store/globalConfig.ts @@ -24,6 +24,7 @@ export const useGlobalConfig = defineStore('global', { patternColor: '#1b66c9', patternList: defaultPatternList as number[], background: {}, // 背景颜色或图片 + font: '', }, musicList: defaultMusicList as IMusic[], imageList: defaultImageList as IImage[], @@ -106,6 +107,10 @@ export const useGlobalConfig = defineStore('global', { getBackground(state) { return state.globalConfig.theme.background }, + // 获取字体 + getFont(state) { + return state.globalConfig.theme.font + }, // 获取是否显示头像 getIsShowAvatar(state) { return state.globalConfig.isShowAvatar @@ -232,6 +237,10 @@ export const useGlobalConfig = defineStore('global', { setBackground(background: any) { this.globalConfig.theme.background = background }, + // 设置字体 + setFont(font: any) { + this.globalConfig.theme.font = font + }, // 设置是否显示头像 setIsShowAvatar(isShowAvatar: boolean) { this.globalConfig.isShowAvatar = isShowAvatar @@ -256,6 +265,7 @@ export const useGlobalConfig = defineStore('global', { patternColor: '#1b66c9', patternList: defaultPatternList as number[], background: {}, // 背景颜色或图片 + font: '', }, musicList: defaultMusicList as IMusic[], imageList: defaultImageList as IImage[], diff --git a/src/utils/format/tree.ts b/src/utils/format/tree.ts new file mode 100644 index 0000000..be17e6f --- /dev/null +++ b/src/utils/format/tree.ts @@ -0,0 +1,3 @@ +/** + * @description 用于将平铺的数组组合到树形数组中 + */ \ No newline at end of file diff --git a/src/views/Config/Global/FaceConfig/index.vue b/src/views/Config/Global/FaceConfig/index.vue index 0fef3a1..d0ee4a2 100644 --- a/src/views/Config/Global/FaceConfig/index.vue +++ b/src/views/Config/Global/FaceConfig/index.vue @@ -6,6 +6,7 @@ import { useI18n } from 'vue-i18n' import { useRouter } from 'vue-router' import { z as zod } from 'zod' import { daisyuiThemes } from '@/constant/theme' +import { useLocalFonts } from '@/hooks/useLocalFonts' import i18n, { languageList } from '@/locales/i18n' import useStore from '@/store' import { themeChange } from '@/utils' @@ -18,9 +19,10 @@ const { t } = useI18n() const globalConfig = useStore().globalConfig const personConfig = useStore().personConfig const prizeConfig = useStore().prizeConfig -const { getTopTitle: topTitle, getTheme: localTheme, getPatterColor: patternColor, getPatternList: patternList, getCardColor: cardColor, getLuckyColor: luckyCardColor, getTextColor: textColor, getCardSize: cardSize, getTextSize: textSize, getRowCount: rowCount, getIsShowPrizeList: isShowPrizeList, getLanguage: userLanguage, getBackground: backgroundImage, getImageList: imageList, getIsShowAvatar: isShowAvatar, +const { getTopTitle: topTitle, getTheme: localTheme, getPatterColor: patternColor, getPatternList: patternList, getCardColor: cardColor, getLuckyColor: luckyCardColor, getTextColor: textColor, getCardSize: cardSize, getTextSize: textSize, getRowCount: rowCount, getIsShowPrizeList: isShowPrizeList, getLanguage: userLanguage, getBackground: backgroundImage, getFont: currentFont, getImageList: imageList, getIsShowAvatar: isShowAvatar, } = storeToRefs(globalConfig) const { getAlreadyPersonList: alreadyPersonList, getNotPersonList: notPersonList } = storeToRefs(personConfig) +const { getFonts, disabled: disabledFonts, fonts } = useLocalFonts() const colorPickerRef = ref() const resetDataDialogRef = ref() interface ThemeDaType { @@ -42,6 +44,7 @@ const patternColorValue = ref(structuredClone(patternColor.value)) const themeList = ref(daisyuiThemes) const daisyuiThemeList = ref(daisyuiThemes) const backgroundImageValue = ref(backgroundImage.value) +const currentFontValue = ref(currentFont.value) const formData = ref({ rowCount: rowCountValue, }) @@ -150,6 +153,10 @@ watch(isShowPrizeListValue, () => { watch(backgroundImageValue, (val) => { globalConfig.setBackground(val) }) +watch(currentFontValue, (val) => { + console.log('currentFontValue', val) + globalConfig.setFont(val) +}) watch(languageValue, (val: string) => { globalConfig.setLanguage(val) }) @@ -225,6 +232,16 @@ onMounted(() => { class="w-full max-w-xs input input-bordered" > + +
diff --git a/src/views/Demo/index.vue b/src/views/Demo/index.vue index a0acc4b..ec9335b 100644 --- a/src/views/Demo/index.vue +++ b/src/views/Demo/index.vue @@ -1,51 +1,76 @@ diff --git a/src/window.d.ts b/src/window.d.ts new file mode 100644 index 0000000..af39de7 --- /dev/null +++ b/src/window.d.ts @@ -0,0 +1,12 @@ +// src/types/window.d.ts +interface FontData { + family: string + fullName: string + postscriptName: string + style: string + blob: () => Promise +} + +interface Window { + queryLocalFonts?: (options?) => Promise +}