zhou zhou
2 天以前 33bd4dd1f0e41131cd8e5bbf87204a1f0b72bb08
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import { addCollection } from '@iconify/vue/offline'
import { LOCAL_ICON_COLLECTIONS } from '../../plugins/iconify.collections.js'
 
const FULL_ICON_COLLECTION_LOADERS = Object.freeze({
  fluent: () => import('@iconify-json/fluent').then((module) => module.icons),
  'icon-park-outline': () =>
    import('@iconify-json/icon-park-outline').then((module) => module.icons),
  iconamoon: () => import('@iconify-json/iconamoon').then((module) => module.icons),
  ix: () => import('@iconify-json/ix').then((module) => module.icons),
  'line-md': () => import('@iconify-json/line-md').then((module) => module.icons),
  ri: () => import('@iconify-json/ri').then((module) => module.icons),
  solar: () => import('@iconify-json/solar').then((module) => module.icons),
  'svg-spinners': () => import('@iconify-json/svg-spinners').then((module) => module.icons),
  'system-uicons': () => import('@iconify-json/system-uicons').then((module) => module.icons),
  vaadin: () => import('@iconify-json/vaadin').then((module) => module.icons)
})
 
const fullyRegisteredPrefixes = new Set()
const pendingPrefixLoads = new Map()
 
function parseIconName(icon) {
  if (typeof icon !== 'string') {
    return null
  }
 
  const normalizedIcon = icon.trim()
  if (!normalizedIcon) {
    return null
  }
 
  const separatorIndex = normalizedIcon.indexOf(':')
  if (separatorIndex <= 0 || separatorIndex >= normalizedIcon.length - 1) {
    return null
  }
 
  return {
    prefix: normalizedIcon.slice(0, separatorIndex),
    name: normalizedIcon.slice(separatorIndex + 1)
  }
}
 
function hasBundledIcon(icon) {
  const parsedIcon = parseIconName(icon)
  if (!parsedIcon) {
    return false
  }
 
  const collection = LOCAL_ICON_COLLECTIONS[parsedIcon.prefix]
  if (!collection) {
    return false
  }
 
  return Boolean(collection.icons?.[parsedIcon.name] || collection.aliases?.[parsedIcon.name])
}
 
function loadFullIconCollection(prefix) {
  if (fullyRegisteredPrefixes.has(prefix)) {
    return Promise.resolve(true)
  }
 
  const currentTask = pendingPrefixLoads.get(prefix)
  if (currentTask) {
    return currentTask
  }
 
  const loader = FULL_ICON_COLLECTION_LOADERS[prefix]
  if (!loader) {
    return Promise.resolve(false)
  }
 
  const loadTask = loader()
    .then((collection) => {
      addCollection(collection)
      fullyRegisteredPrefixes.add(prefix)
      pendingPrefixLoads.delete(prefix)
      return true
    })
    .catch((error) => {
      pendingPrefixLoads.delete(prefix)
      throw error
    })
 
  pendingPrefixLoads.set(prefix, loadTask)
  return loadTask
}
 
function collectRuntimeIcons(source, iconNames = new Set()) {
  if (!Array.isArray(source)) {
    return iconNames
  }
 
  source.forEach((item) => {
    if (!item || typeof item !== 'object') {
      return
    }
 
    const icon = item.meta?.icon || item.icon
    if (typeof icon === 'string' && icon.includes(':') && !hasBundledIcon(icon)) {
      iconNames.add(icon)
    }
 
    if (Array.isArray(item.children) && item.children.length > 0) {
      collectRuntimeIcons(item.children, iconNames)
    }
  })
 
  return iconNames
}
 
function scheduleIdleTask(task, delay = 0) {
  const invoke = () => {
    if (globalThis.requestIdleCallback) {
      globalThis.requestIdleCallback(task, { timeout: 1000 })
      return
    }
    setTimeout(task, 0)
  }
 
  if (delay > 0) {
    setTimeout(invoke, delay)
    return
  }
 
  invoke()
}
 
async function ensureIconRegistered(icon) {
  const parsedIcon = parseIconName(icon)
  if (!parsedIcon) {
    return false
  }
 
  if (hasBundledIcon(icon) || fullyRegisteredPrefixes.has(parsedIcon.prefix)) {
    return true
  }
 
  return loadFullIconCollection(parsedIcon.prefix)
}
 
function warmRuntimeIcons(iconNames, delay = 120) {
  const icons = [...new Set(Array.isArray(iconNames) ? iconNames : [])].filter(Boolean)
  if (icons.length === 0) {
    return
  }
 
  scheduleIdleTask(() => {
    icons.forEach((icon) => {
      void ensureIconRegistered(icon)
    })
  }, delay)
}
 
function warmMenuIcons(menuList, delay = 120) {
  warmRuntimeIcons([...collectRuntimeIcons(menuList)], delay)
}
 
export {
  collectRuntimeIcons,
  ensureIconRegistered,
  hasBundledIcon,
  warmMenuIcons,
  warmRuntimeIcons
}