Files
image-workbench-desktop/src/ui-splash.js
T
qiqi 9210bc3d0b
Verify (Gitea) / gate (build · unit · facts) (push) Failing after 1m8s
fix(csp): 去掉 index.html 里的内联 style/script —— 插件样式失效的真因(同 v3.26.9)
现象:风格库 / 浏览器 / 提示词库的面板与画布上的样式节点全部丢样式(DOM 正常、宿主外链
style.css 正常,只有插件动态注入的 <style> 不生效)。

根因:Tauri 会为 index.html 里的内联块加 CSP nonce;CSP3 规定 nonce 一出现 'unsafe-inline'
即被忽略。插件样式是运行时 document.createElement('style') 注入的、没有 nonce,于是被整段丢弃。
这正是 v3.26.9 修过、v3.26.10 回退掉的那条链(当时靠给动态 style 补 nonce 解决)。
我上两版把启动屏做成了内联 <style>+<script>,等于把这个坑重新踩了一遍。

- scripts/boot-splash.js:只导出标记(复用宿主已有的 .iwb-splash* 类),不再有内联样式/脚本
- scripts/build.js:index.html 只内联启动屏标记,样式走外链 style.css
- src/ui-splash.js:React 启动屏挂载时直接移除 #iwb-boot-static 节点
- scripts/check-dist.js:script 数回到 3,并新增断言「index.html 不得含内联 <style>/<script>」
  并写明原因,防止下次再踩
2026-11-27 22:01:37 +08:00

46 lines
2.3 KiB
JavaScript

/* ui-splash.js —— 启动加载屏(v3.26.109 从 app.js 的 JSX 里搬出来)
*
* 设计:图标轻浮动 + 标题 + 细进度条(真实百分比,带流光)+ 阶段文字 + 右侧百分比。
* 数据来自 src/boot-progress.js(读取阶段上报真实进度),未知总量时退回不定长流光。
*/
;(function (global) {
'use strict'
const React = global.React
const el = React.createElement
const NOOP = function () {}
function Splash (props) {
const p = props || {}
const bp = global.IWBBootProgress
const [s, setS] = React.useState(() => (bp ? bp.snapshot() : { pct: 0, label: '正在启动…', detail: '', indeterminate: true }))
React.useEffect(() => {
// React 启动屏一挂载就顶掉 index.html 里的静态启动屏(同款外观,切换无感)
try { const n = document.getElementById('iwb-boot-static'); if (n && n.parentNode) n.parentNode.removeChild(n) } catch (e) { /* ignore */ }
const un = bp ? bp.subscribe(setS) : NOOP
return function () { try { un() } catch (e) { /* ignore */ } }
}, [])
// 进入淡出即视为加载完成:进度收口到 100%,避免最后停在 96%
React.useEffect(() => { if (p.fade && bp) bp.finish() }, [p.fade])
const pct = Math.max(0, Math.min(100, Number(s.pct) || 0))
const indet = !!s.indeterminate
const label = s.detail ? s.label + ' · ' + s.detail : s.label
return el('div', { className: 'iwb-splash' + (p.fade ? ' iwb-splash-fade' : '') },
el('div', { className: 'iwb-splash-logo' }, p.icon || null),
el('div', { className: 'iwb-splash-title' }, '生图工作台'),
el('div', {
className: 'iwb-splash-bar' + (indet ? ' is-indeterminate' : ''),
role: 'progressbar',
'aria-valuemin': 0,
'aria-valuemax': 100,
'aria-valuenow': indet ? undefined : pct,
}, el('div', { className: 'iwb-splash-bar-fill', style: indet ? null : { width: pct + '%' } }, null)),
el('div', { className: 'iwb-splash-meta' },
el('span', { className: 'iwb-splash-sub' }, p.fade ? '加载完成' : label),
el('span', { className: 'iwb-splash-pct' }, p.fade || indet ? '' : pct + '%'),
),
)
}
global.IWBUISplash = { Splash }
})(typeof window !== 'undefined' ? window : globalThis)