|
| 1 | +<template> |
| 2 | + <modal |
| 3 | + :show="show" |
| 4 | + :compact-header="true" |
| 5 | + @close="$emit('close')" |
| 6 | + > |
| 7 | + <template #icon> |
| 8 | + <Icon |
| 9 | + name="ci:font" |
| 10 | + class="w-10 h-10 text-blue" |
| 11 | + /> |
| 12 | + </template> |
| 13 | + <template #title> |
| 14 | + Google fonts |
| 15 | + </template> |
| 16 | + |
| 17 | + <div v-if="loading"> |
| 18 | + <Loader class="h-6 w-6 text-nt-blue mx-auto" /> |
| 19 | + </div> |
| 20 | + <div v-else> |
| 21 | + <text-input |
| 22 | + v-model="search" |
| 23 | + |
| 24 | + name="search" |
| 25 | + placeholder="Search fonts" |
| 26 | + /> |
| 27 | + |
| 28 | + <div |
| 29 | + ref="scrollContainer" |
| 30 | + class="grid grid-cols-3 gap-2 p-5 mb-5 overflow-y-scroll max-h-[24rem] border rounded-md" |
| 31 | + > |
| 32 | + <FontCard |
| 33 | + v-for="(fontName, index) in enrichedFonts" |
| 34 | + :key="fontName" |
| 35 | + :ref="el => setFontRef(el, index)" |
| 36 | + :font-name="fontName" |
| 37 | + :is-visible="visible[index]" |
| 38 | + :is-selected="selectedFont === fontName" |
| 39 | + @select-font="selectedFont = fontName" |
| 40 | + /> |
| 41 | + </div> |
| 42 | + |
| 43 | + <div class="flex"> |
| 44 | + <UButton |
| 45 | + size="md" |
| 46 | + color="white" |
| 47 | + class="mr-2" |
| 48 | + @click="$emit('apply', null)" |
| 49 | + > |
| 50 | + Reset |
| 51 | + </UButton> |
| 52 | + <UButton |
| 53 | + size="md" |
| 54 | + :disabled="!selectedFont" |
| 55 | + block |
| 56 | + class="flex-1" |
| 57 | + @click="$emit('apply', selectedFont)" |
| 58 | + > |
| 59 | + Apply |
| 60 | + </UButton> |
| 61 | + </div> |
| 62 | + </div> |
| 63 | + </modal> |
| 64 | +</template> |
| 65 | + |
| 66 | +<script setup> |
| 67 | +import { defineEmits } from "vue" |
| 68 | +import Fuse from "fuse.js" |
| 69 | +import { refDebounced, useElementVisibility } from "@vueuse/core" |
| 70 | +import FontCard from './FontCard.vue' |
| 71 | +
|
| 72 | +const props = defineProps({ |
| 73 | + show: { |
| 74 | + type: Boolean, |
| 75 | + default: false, |
| 76 | + }, |
| 77 | + font: { |
| 78 | + type: String, |
| 79 | + default: null, |
| 80 | + }, |
| 81 | +}) |
| 82 | +
|
| 83 | +const emit = defineEmits(['close','apply']) |
| 84 | +const loading = ref(false) |
| 85 | +const fonts = ref([]) |
| 86 | +const selectedFont = ref(props.font || null) |
| 87 | +const search = ref("") |
| 88 | +const debouncedSearch = refDebounced(search, 500) |
| 89 | +const scrollContainer = ref(null) |
| 90 | +const fontRefs = new Map() |
| 91 | +const visible = ref([]) |
| 92 | +
|
| 93 | +const setFontRef = (el, index) => { |
| 94 | + if (el) fontRefs.set(index, el) |
| 95 | +} |
| 96 | +
|
| 97 | +const initializeVisibilityTracking = async () => { |
| 98 | + await nextTick() // Ensure DOM has been fully updated |
| 99 | + fontRefs.forEach((el, index) => { |
| 100 | + const visibility = useElementVisibility(el, { |
| 101 | + root: scrollContainer.value, |
| 102 | + threshold: 0.1 |
| 103 | + }) |
| 104 | + watch( |
| 105 | + () => visibility.value, |
| 106 | + (isVisible) => { |
| 107 | + if (isVisible) { |
| 108 | + visible.value[index] = true |
| 109 | + } |
| 110 | + }, |
| 111 | + { immediate: true } |
| 112 | + ) |
| 113 | + }) |
| 114 | +} |
| 115 | +
|
| 116 | +const fetchFonts = async () => { |
| 117 | + if (props.show) { |
| 118 | + selectedFont.value = props.font || null |
| 119 | + loading.value = true |
| 120 | + opnFetch('/fonts/').then((data) => { |
| 121 | + fonts.value = data || [] |
| 122 | + loading.value = false |
| 123 | + initializeVisibilityTracking() |
| 124 | + }) |
| 125 | + } |
| 126 | +} |
| 127 | +watch(() => props.show, fetchFonts) |
| 128 | +
|
| 129 | +
|
| 130 | +const enrichedFonts = computed(() => { |
| 131 | + if (search.value === "" || search.value === null) { |
| 132 | + return fonts.value |
| 133 | + } |
| 134 | +
|
| 135 | + // Fuze search |
| 136 | + const fuse = new Fuse(Object.values(fonts.value)) |
| 137 | + return fuse.search(debouncedSearch.value).map((res) => { |
| 138 | + return res.item |
| 139 | + }) |
| 140 | +}) |
| 141 | +</script> |
0 commit comments