|
1 | | -import { onMounted, onUnmounted, ref } from 'vue' |
2 | | -import type { |
3 | | - Sponsor, |
4 | | - SponsorTier, |
5 | | -} from '@voidzero-dev/vitepress-theme/src/types/sponsors' |
6 | | -import voidZeroSvg from './images/voidzero.svg' |
7 | | -import boltSvg from './images/bolt.svg' |
8 | | -import nuxtLabsSvg from './images/nuxtlabs.svg' |
| 1 | +import { onMounted, ref } from 'vue' |
| 2 | +import type { Sponsor, SponsorTier } from '@voidzero-dev/vitepress-theme' |
9 | 3 |
|
10 | 4 | interface Sponsors { |
11 | | - special: Sponsor[] |
| 5 | + main: Sponsor[] |
| 6 | + partnership: Sponsor[] |
12 | 7 | platinum: Sponsor[] |
13 | | - platinum_china: Sponsor[] |
14 | 8 | gold: Sponsor[] |
15 | | - silver: Sponsor[] |
16 | | - bronze: Sponsor[] |
17 | 9 | } |
18 | 10 |
|
19 | 11 | // shared data across instances so we load only once. |
20 | 12 | const data = ref<SponsorTier[]>() |
21 | 13 |
|
22 | | -const dataHost = 'https://sponsors.vuejs.org' |
23 | | -const dataUrl = `${dataHost}/vite.json` |
24 | | - |
25 | | -export const voidZero = { |
26 | | - name: 'VoidZero', |
27 | | - url: 'https://voidzero.dev', |
28 | | - img: voidZeroSvg, |
29 | | -} satisfies Sponsor |
30 | | - |
31 | | -const viteSponsors: Pick<Sponsors, 'special' | 'gold'> = { |
32 | | - special: [ |
33 | | - // sponsors patak-dev |
34 | | - { |
35 | | - name: 'Bolt', |
36 | | - url: 'https://bolt.new', |
37 | | - img: boltSvg, |
38 | | - }, |
39 | | - // sponsors antfu |
40 | | - { |
41 | | - name: 'NuxtLabs', |
42 | | - url: 'https://nuxtlabs.com', |
43 | | - img: nuxtLabsSvg, |
44 | | - }, |
45 | | - ], |
46 | | - gold: [ |
47 | | - // now automated via sponsors.vuejs.org too |
48 | | - ], |
49 | | -} |
50 | | - |
51 | | -function toggleDarkLogos() { |
52 | | - if (data.value) { |
53 | | - const isDark = document.documentElement.classList.contains('dark') |
54 | | - data.value.forEach(({ items }) => { |
55 | | - items.forEach((s: Sponsor) => { |
56 | | - if (s.hasDark) { |
57 | | - s.img = isDark |
58 | | - ? s.img.replace(/(\.\w+)$/, '-dark$1') |
59 | | - : s.img.replace(/-dark(\.\w+)$/, '$1') |
60 | | - } |
61 | | - }) |
62 | | - }) |
63 | | - } |
64 | | -} |
65 | | - |
66 | 14 | export function useSponsor() { |
67 | 15 | onMounted(async () => { |
68 | | - const ob = new MutationObserver((list) => { |
69 | | - for (const m of list) { |
70 | | - if (m.attributeName === 'class') { |
71 | | - toggleDarkLogos() |
72 | | - } |
73 | | - } |
74 | | - }) |
75 | | - ob.observe(document.documentElement, { attributes: true }) |
76 | | - onUnmounted(() => { |
77 | | - ob.disconnect() |
78 | | - }) |
79 | | - |
80 | | - if (data.value) { |
81 | | - return |
82 | | - } |
83 | | - |
84 | | - const result = await fetch(dataUrl) |
85 | | - const json = await result.json() |
86 | | - |
87 | | - data.value = mapSponsors(json) |
88 | | - toggleDarkLogos() |
| 16 | + if (data.value) return |
| 17 | + |
| 18 | + const result = await fetch('https://sponsors.vite.dev/sponsors.json') |
| 19 | + const sponsors: Sponsors = await result.json() |
| 20 | + |
| 21 | + data.value = [ |
| 22 | + { |
| 23 | + tier: '提供', |
| 24 | + size: 'big', |
| 25 | + items: sponsors.main, |
| 26 | + }, |
| 27 | + { |
| 28 | + tier: 'パートナーシップ', |
| 29 | + size: 'big', |
| 30 | + items: sponsors.partnership, |
| 31 | + }, |
| 32 | + { |
| 33 | + tier: 'プラチナスポンサー', |
| 34 | + size: 'big', |
| 35 | + items: sponsors.platinum, |
| 36 | + }, |
| 37 | + { |
| 38 | + tier: 'ゴールドスポンサー', |
| 39 | + size: 'medium', |
| 40 | + items: sponsors.gold, |
| 41 | + }, |
| 42 | + ] |
89 | 43 | }) |
90 | 44 |
|
91 | | - return { |
92 | | - data, |
93 | | - } |
94 | | -} |
95 | | - |
96 | | -function mapSponsors(sponsors: Sponsors): SponsorTier[] { |
97 | | - return [ |
98 | | - { |
99 | | - tier: 'パートナーシップ', |
100 | | - size: 'big' as const, |
101 | | - items: viteSponsors['special'], |
102 | | - }, |
103 | | - { |
104 | | - tier: 'プラチナスポンサー', |
105 | | - size: 'big' as const, |
106 | | - items: mapImgPath(sponsors['platinum']), |
107 | | - }, |
108 | | - { |
109 | | - tier: 'ゴールドスポンサー', |
110 | | - size: 'medium' as const, |
111 | | - items: [...mapImgPath(sponsors['gold']), ...viteSponsors['gold']], |
112 | | - }, |
113 | | - ] |
114 | | -} |
115 | | - |
116 | | -const viteSponsorNames = new Set( |
117 | | - Object.values(viteSponsors).flatMap((sponsors) => |
118 | | - sponsors.map((s) => s.name), |
119 | | - ), |
120 | | -) |
121 | | - |
122 | | -/** |
123 | | - * Map Vue/Vite sponsors data to objects and filter out Vite-specific sponsors |
124 | | - */ |
125 | | -function mapImgPath(sponsors: Sponsor[]) { |
126 | | - return sponsors |
127 | | - .filter((sponsor) => !viteSponsorNames.has(sponsor.name)) |
128 | | - .map((sponsor) => ({ |
129 | | - ...sponsor, |
130 | | - img: `${dataHost}/images/${sponsor.img}`, |
131 | | - })) |
| 45 | + return data |
132 | 46 | } |
0 commit comments