GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — lowcode-engine/packages/utils/src/asset.ts at main · weiplanet/lowcode-engine Expand file tree/ Copy pathasset.tsMore file actions 285 lines (250 loc) · 7.78 KB / Copy pathasset.tsFile metadata and controls285 lines (250 loc) · 7.78 KB 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 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 import { AssetItem, AssetType, AssetLevels, Asset, AssetList, AssetBundle, AssetLevel, AssetsJson } from '@alilc/lowcode-types'; import { isCSSUrl } from './is-css-url'; import { createDefer } from './create-defer'; import { load, evaluate } from './script'; // API 向下兼容 export { AssetItem, AssetType, AssetLevels, Asset, AssetList, AssetBundle, AssetLevel, AssetsJson } from '@alilc/lowcode-types'; export function isAssetItem(obj: any): obj is AssetItem { return obj && obj.type; } export function isAssetBundle(obj: any): obj is AssetBundle { return obj && obj.type === AssetType.Bundle; } export function assetBundle(assets?: Asset | AssetList | null, level?: AssetLevel): AssetBundle | null { if (!assets) { return null; } return { type: AssetType.Bundle, assets, level, }; } /* urls: "view.js,view2 <device selector>, view3 <device selector>", urls: [ "view.js", "view.js *", "view1.js mobile|pc", "view2.js <device selector>" ] */ export function assetItem(type: AssetType, content?: string | null, level?: AssetLevel, id?: string): AssetItem | null { if (!content) { return null; } return { type, content, level, id, }; } export function megreAssets(assets: AssetsJson, incrementalAssets: AssetsJson): AssetsJson { if (incrementalAssets.packages) { assets.packages = [...(assets.packages || []), ...incrementalAssets.packages]; } if (incrementalAssets.components) { assets.components = [...assets.components, ...incrementalAssets.components]; } megreAssetsComponentList(assets, incrementalAssets, 'componentList'); megreAssetsComponentList(assets, incrementalAssets, 'bizComponentList'); return assets; } function megreAssetsComponentList(assets: AssetsJson, incrementalAssets: AssetsJson, listName: keyof AssetsJson): void { if (incrementalAssets[listName]) { if (assets[listName]) { // 根据title进行合并 incrementalAssets[listName]?.map((item) => { let matchFlag = false; assets[listName]?.map((assetItem) => { if (assetItem.title === item.title) { assetItem.children = assetItem.children.concat(item.children); matchFlag = true; } return assetItem; }); !matchFlag && assets[listName]?.push(item); return item; }); } } } export class StylePoint { private lastContent: string | undefined; private lastUrl: string | undefined; private placeholder: Element | Text; readonly level: number; readonly id: string; constructor(level: number, id?: string) { this.level = level; if (id) { this.id = id; } let placeholder: any; if (id) { placeholder = document.head.querySelector(`style[data-id="${id}"]`); } if (!placeholder) { placeholder = document.createTextNode(''); const meta = document.head.querySelector(`meta[level="${level}"]`); if (meta) { document.head.insertBefore(placeholder, meta); } else { document.head.appendChild(placeholder); } } this.placeholder = placeholder; } applyText(content: string) { if (this.lastContent === content) { return; } this.lastContent = content; this.lastUrl = undefined; const element = document.createElement('style'); element.setAttribute('type', 'text/css'); if (this.id) { element.setAttribute('data-id', this.id); } element.appendChild(document.createTextNode(content)); document.head.insertBefore(element, this.placeholder.parentNode === document.head ? this.placeholder.nextSibling : null); document.head.removeChild(this.placeholder); this.placeholder = element; } applyUrl(url: string) { if (this.lastUrl === url) { return; } this.lastContent = undefined; this.lastUrl = url; const element = document.createElement('link'); element.onload = onload; element.onerror = onload; const i = createDefer(); function onload(e: any) { element.onload = null; element.onerror = null; if (e.type === 'load') { i.resolve(); } else { i.reject(); } } element.href = url; element.rel = 'stylesheet'; if (this.id) { element.setAttribute('data-id', this.id); } document.head.insertBefore(element, this.placeholder.parentNode === document.head ? this.placeholder.nextSibling : null); document.head.removeChild(this.placeholder); this.placeholder = element; return i.promise(); } } function parseAssetList(scripts: any, styles: any, assets: AssetList, level?: AssetLevel) { for (const asset of assets) { parseAsset(scripts, styles, asset, level); } } function parseAsset(scripts: any, styles: any, asset: Asset | undefined | null, level?: AssetLevel) { if (!asset) { return; } if (Array.isArray(asset)) { return parseAssetList(scripts, styles, asset, level); } if (isAssetBundle(asset)) { if (asset.assets) { if (Array.isArray(asset.assets)) { parseAssetList(scripts, styles, asset.assets, asset.level || level); } else { parseAsset(scripts, styles, asset.assets, asset.level || level); } return; } return; } if (!isAssetItem(asset)) { asset = assetItem(isCSSUrl(asset) ? AssetType.CSSUrl : AssetType.JSUrl, asset, level)!; } let lv = asset.level || level; if (!lv || AssetLevel[lv] == null) { lv = AssetLevel.App; } asset.level = lv; if (asset.type === AssetType.CSSUrl || asset.type == AssetType.CSSText) { styles[lv].push(asset); } else { scripts[lv].push(asset); } } export class AssetLoader { async load(asset: Asset) { const styles: any = {}; const scripts: any = {}; AssetLevels.forEach(lv => { styles[lv] = []; scripts[lv] = []; }); parseAsset(scripts, styles, asset); const styleQueue: AssetItem[] = styles[AssetLevel.Environment].concat( styles[AssetLevel.Library], styles[AssetLevel.Theme], styles[AssetLevel.Runtime], styles[AssetLevel.App], ); const scriptQueue: AssetItem[] = scripts[AssetLevel.Environment].concat( scripts[AssetLevel.Library], scripts[AssetLevel.Theme], scripts[AssetLevel.Runtime], scripts[AssetLevel.App], ); await Promise.all( styleQueue.map(({ content, level, type, id }) => this.loadStyle(content, level!, type === AssetType.CSSUrl, id)), ); await Promise.all(scriptQueue.map(({ content, type }) => this.loadScript(content, type === AssetType.JSUrl))); } private stylePoints = new Map<string, StylePoint>(); private loadStyle(content: string | undefined | null, level: AssetLevel, isUrl?: boolean, id?: string) { if (!content) { return; } let point: StylePoint | undefined; if (id) { point = this.stylePoints.get(id); if (!point) { point = new StylePoint(level, id); this.stylePoints.set(id, point); } } else { point = new StylePoint(level); } return isUrl ? point.applyUrl(content) : point.applyText(content); } private loadScript(content: string | undefined | null, isUrl?: boolean) { if (!content) { return; } return isUrl ? load(content) : evaluate(content); } // todo 补充类型 async loadAsyncLibrary(asyncLibraryMap: Record<string, any>) { const promiseList: any[] = []; const libraryKeyList: any[] = []; for (const key in asyncLibraryMap) { // 需要异步加载 if (asyncLibraryMap[key].async) { promiseList.push(window[asyncLibraryMap[key].library]); libraryKeyList.push(asyncLibraryMap[key].library); } } await Promise.all(promiseList).then((mods) => { if (mods.length > 0) { mods.map((item, index) => { window[libraryKeyList[index]] = item; return item; }); } }); } } |
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |