dsh-design-studio 1.1.0 → 1.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.en.md +2 -0
- package/README.md +2 -0
- package/lib/browser.js +4 -1
- package/lib/bundle.js +65 -15
- package/lib/client.js +519 -26
- package/lib/core.js +7 -0
- package/lib/fonts-index.json +320 -0
- package/lib/fonts-preview.json +1 -0
- package/lib/fonts.js +352 -0
- package/lib/gallery/pack.js +11 -2
- package/lib/handoff.js +44 -0
- package/lib/history.js +2 -0
- package/lib/htmlpack.js +85 -19
- package/lib/index.js +227 -11
- package/lib/look.js +2 -1
- package/lib/mock.js +793 -0
- package/lib/picker.js +68 -9
- package/lib/serve.js +70 -11
- package/lib/states.js +11 -1
- package/lib/tool.js +3 -2
- package/lib/tweaks.js +135 -4
- package/package.json +1 -1
- package/skills/ui-design/SKILL.md +18 -1
package/lib/picker.js
CHANGED
|
@@ -1311,7 +1311,8 @@
|
|
|
1311
1311
|
/** 这一屏写了哪几个状态(<meta name="dsh-states">,原样给右侧栏解析)、现在是哪个 */
|
|
1312
1312
|
function statesNow() {
|
|
1313
1313
|
const m = document.querySelector('meta[name="dsh-states"]')
|
|
1314
|
-
|
|
1314
|
+
// 状态跟着模拟数据走的页:前面加一行「#mock」告诉右侧栏(解析状态时这行不算),点名第一个状态不能当成「没写」
|
|
1315
|
+
return m ? { states: ((m.hasAttribute('data-mock') ? '#mock\n' : '') + String(m.getAttribute('content') || '')).slice(0, 400), state: curState() } : {}
|
|
1315
1316
|
}
|
|
1316
1317
|
|
|
1317
1318
|
function boot(seed) {
|
|
@@ -1343,10 +1344,35 @@
|
|
|
1343
1344
|
// ── 设计变量::root / html 上的 CSS 自定义属性(取算出来的值),带出处样式表 ─────
|
|
1344
1345
|
const MAX_TOKENS = 80
|
|
1345
1346
|
|
|
1347
|
+
/** 页面样式表里 @font-face 声明过的字体名(小写;面板试字体时插进去的不算) */
|
|
1348
|
+
function fontFaces() {
|
|
1349
|
+
const out = new Set()
|
|
1350
|
+
const scan = (rules, depth) => {
|
|
1351
|
+
for (const r of rules) {
|
|
1352
|
+
if (r.type === 5 && r.style) out.add(r.style.getPropertyValue('font-family').trim().replace(/^["']|["']$/g, '').toLowerCase())
|
|
1353
|
+
else if (depth < 4 && r.styleSheet) { try { scan(r.styleSheet.cssRules, depth + 1) } catch { /* 别的源 */ } }
|
|
1354
|
+
else if (depth < 4 && r.cssRules) scan(r.cssRules, depth + 1)
|
|
1355
|
+
}
|
|
1356
|
+
}
|
|
1357
|
+
for (const sheet of document.styleSheets) {
|
|
1358
|
+
if (sheet.ownerNode && sheet.ownerNode.getAttribute && sheet.ownerNode.getAttribute('data-dsh-design')) continue
|
|
1359
|
+
try { scan(sheet.cssRules, 0) } catch { /* 别的源的样式表读不了 */ }
|
|
1360
|
+
}
|
|
1361
|
+
return out
|
|
1362
|
+
}
|
|
1363
|
+
|
|
1346
1364
|
function tokensOf() {
|
|
1347
1365
|
const out = []
|
|
1348
1366
|
const seen = new Set()
|
|
1349
1367
|
const cs = getComputedStyle(document.documentElement)
|
|
1368
|
+
let faces = null
|
|
1369
|
+
// 字体变量:打头的字体有没有 @font-face(外观面板据此判断选字体库的同名字体要不要存)
|
|
1370
|
+
const face = (value) => {
|
|
1371
|
+
const m = /^\s*(?:"([^"]+)"|'([^']+)')/.exec(value)
|
|
1372
|
+
if (!m) return false
|
|
1373
|
+
if (!faces) faces = fontFaces()
|
|
1374
|
+
return faces.has((m[1] || m[2]).trim().toLowerCase())
|
|
1375
|
+
}
|
|
1350
1376
|
for (const sheet of document.styleSheets) {
|
|
1351
1377
|
let rules
|
|
1352
1378
|
try { rules = sheet.cssRules } catch { continue } // 别的源的样式表读不了
|
|
@@ -1359,7 +1385,7 @@
|
|
|
1359
1385
|
if (!name.startsWith('--') || seen.has(name)) continue
|
|
1360
1386
|
seen.add(name)
|
|
1361
1387
|
const value = (cs.getPropertyValue(name) || r.style.getPropertyValue(name)).trim()
|
|
1362
|
-
if (value) out.push({ name, value: clip(value, 160), href })
|
|
1388
|
+
if (value) out.push({ name, value: clip(value, 160), href, ...(/^\s*["']/.test(value) && face(value) ? { face: true } : null) })
|
|
1363
1389
|
}
|
|
1364
1390
|
}
|
|
1365
1391
|
}
|
|
@@ -1641,6 +1667,8 @@
|
|
|
1641
1667
|
const normWs = (x) => String(x || '').replace(/[\s\u00a0]+/g, ' ').trim()
|
|
1642
1668
|
const NO_TEXT = /^(SCRIPT|STYLE|TEXTAREA|NOSCRIPT|TEMPLATE|TITLE)$/
|
|
1643
1669
|
let textEdit = false
|
|
1670
|
+
// 模拟数据(mock.js)问:选取、改字、调样式开着时点页面是在选东西,别改数据
|
|
1671
|
+
try { Object.defineProperty(window, '__ddpBusy', { value: () => picking || textEdit || styleOn, configurable: true }) } catch { /* 页面占了 */ }
|
|
1644
1672
|
let editUi = null
|
|
1645
1673
|
let editing = null // { span, node, before, nth }
|
|
1646
1674
|
let editLang = 'zh'
|
|
@@ -1872,7 +1900,9 @@ background:rgba(20,20,22,.86);color:#e8e8ea;font:12px/18px -apple-system,system-
|
|
|
1872
1900
|
// 回默认状态只认什么参数都不带的(?page=2 这种是页面自己的参数,照常当本页链接)
|
|
1873
1901
|
if (u.pathname === me.pathname) {
|
|
1874
1902
|
if (state && !declaredStates().includes(state)) state = ''
|
|
1875
|
-
|
|
1903
|
+
// 状态跟着模拟数据走的页:「回默认」= 去掉点名的状态、回到跟着数据——地址里点名了才算跳,没点名(已经跟着数据)当本页链接
|
|
1904
|
+
const follow = !!document.querySelector('meta[name="dsh-states"][data-mock]')
|
|
1905
|
+
if (state ? state === curState() : u.search || u.hash || /#$/.test(raw) || (follow ? !declaredStates().includes(me.searchParams ? stateOfUrl(me) : '') : !offDefault())) return back ? { path: '', state: '', back } : null
|
|
1876
1906
|
}
|
|
1877
1907
|
let path = u.pathname
|
|
1878
1908
|
try { path = decodeURIComponent(u.pathname) } catch { /* 原样 */ }
|
|
@@ -1995,11 +2025,18 @@ background:rgba(20,20,22,.86);color:#e8e8ea;font:12px/18px -apple-system,system-
|
|
|
1995
2025
|
let me = hereUrl().pathname
|
|
1996
2026
|
try { me = decodeURIComponent(me) } catch { /* 原样 */ }
|
|
1997
2027
|
if (!l || l.back || l.more || l.path !== me || me !== stateNav) return null
|
|
1998
|
-
|
|
2028
|
+
const hash = /^#[^\s#]{1,200}$/.test(l.hash || '') ? l.hash : ''
|
|
1999
2029
|
const d = declaredStates()
|
|
2030
|
+
// 状态跟着模拟数据走的页:不带状态的链接(「首页」)= 回到跟着数据——右侧栏点名过状态就去掉(clear),没点名就随页面自己跳;
|
|
2031
|
+
// 点名第一个状态(?state=第一个)才是点名,不是「回默认」
|
|
2032
|
+
if (document.querySelector('meta[name="dsh-states"][data-mock]')) {
|
|
2033
|
+
if (!l.state) { let had = ''; try { had = hereUrl().searchParams.get('state') || '' } catch { had = '' } return had && d.includes(had) ? { to: '', def: true, clear: true, hash } : null }
|
|
2034
|
+
return d.includes(l.state) ? { to: l.state, def: false, hash } : null
|
|
2035
|
+
}
|
|
2036
|
+
// 换到写了的某个状态;在别的状态里点指着本页、什么都不带的链接 = 回默认状态(顶栏的状态标签也跟着回去)
|
|
2000
2037
|
const to = l.state || (offDefault() ? d[0] : '')
|
|
2001
2038
|
if (!to || !d.includes(to)) return null
|
|
2002
|
-
return { to, def: to === d[0], hash
|
|
2039
|
+
return { to, def: to === d[0], hash }
|
|
2003
2040
|
}
|
|
2004
2041
|
const stateLast = (e) => {
|
|
2005
2042
|
removeEventListener('click', stateLast)
|
|
@@ -2011,7 +2048,7 @@ background:rgba(20,20,22,.86);color:#e8e8ea;font:12px/18px -apple-system,system-
|
|
|
2011
2048
|
if (!g) return
|
|
2012
2049
|
e.preventDefault()
|
|
2013
2050
|
// def:要的是默认状态(右侧栏没记默认那个的名字);hash:链接里的锚点(order.html?state=paid#receipt),换好状态滚过去
|
|
2014
|
-
post({ type: 'stateGo', state: g.to, def: g.def, ...(g.hash ? { hash: g.hash } : null) })
|
|
2051
|
+
post({ type: 'stateGo', state: g.to, def: g.def, ...(g.clear ? { clear: true } : null), ...(g.hash ? { hash: g.hash } : null) })
|
|
2015
2052
|
}
|
|
2016
2053
|
addEventListener('click', (e) => {
|
|
2017
2054
|
stateWant = null
|
|
@@ -2910,6 +2947,7 @@ background:rgba(20,20,22,.86);color:#e8e8ea;font:12px/18px -apple-system,system-
|
|
|
2910
2947
|
// hscroll 超出屏幕(会横向滚动 / 被切掉) · clipped 文字被截断 · overflow 文字超出框 · overlap 文字叠在一起
|
|
2911
2948
|
// contrast 对比度不够 · tap 点击区太小 · tiny 字太小 · img 图片没加载出来 · fonts 字号太杂
|
|
2912
2949
|
// covered 被固定栏压住(滚到底也看不全) · tight 离固定栏太近 · jserr 页面脚本报错 · missing 样式表 / 脚本 / 字体没载出来
|
|
2950
|
+
// mockmiss 页面用到的字段模拟数据里没有
|
|
2913
2951
|
// capsule 小程序右上角的胶囊(微信画的,页面盖不住)挡住了字或按钮
|
|
2914
2952
|
// island 灵动岛挡住了字或按钮 · homebar 底栏里的字、图标伸进底部横条区 · tabbar 标签栏超过 5 项 · navbar 小程序这屏没有导航栏
|
|
2915
2953
|
// winctl 桌面软件的窗口按钮(Mac 红绿灯 / Windows 右上角三个)挡住了字或按钮
|
|
@@ -3709,6 +3747,9 @@ background:rgba(20,20,22,.86);color:#e8e8ea;font:12px/18px -apple-system,system-
|
|
|
3709
3747
|
add('jserr', null, { msg: String(e.msg || '').slice(0, 160), src: relOf(e.src || '') || '', line: e.line || 0 }, 2)
|
|
3710
3748
|
}
|
|
3711
3749
|
}
|
|
3750
|
+
// 12b. 模拟数据(#30):页面里用到、mock.json 里没有的字段(拼错了、漏写了;显示出来是空的)
|
|
3751
|
+
const miss = Array.isArray(window.__dshMockMissing) ? window.__dshMockMissing.filter((x) => typeof x === 'string').slice(0, 6) : []
|
|
3752
|
+
if (miss.length) add('mockmiss', null, { paths: miss.map((x) => x.slice(0, 60)).join('、') }, 1)
|
|
3712
3753
|
try {
|
|
3713
3754
|
for (const f of document.fonts || []) if (f.status === 'error') add('missing', null, { what: 'font', src: String(f.family || '').replace(/['"]/g, '') }, 1)
|
|
3714
3755
|
} catch { /* 老浏览器 */ }
|
|
@@ -5570,7 +5611,25 @@ background:rgba(20,20,22,.86);color:#e8e8ea;font:12px/18px -apple-system,system-
|
|
|
5570
5611
|
// 属性直接改 <html> 上的;原来的行内值(连同优先级)、属性值都记着,撤掉时原样还原。不动文件
|
|
5571
5612
|
const tweakVars = new Map() // 变量 → 原来的行内值 { v, p }(v 为空 = 原来没有)
|
|
5572
5613
|
const tweakAttrs = new Map() // 属性 → 原来的值(null = 原来没有)
|
|
5573
|
-
|
|
5614
|
+
// 字体库(#31):面板里试字体库的字体时(还没存进设计),@font-face 从预览服务器拿,放在这个 <style> 里
|
|
5615
|
+
let tweakFonts = null
|
|
5616
|
+
function applyTweakFonts(list) {
|
|
5617
|
+
const ok = (Array.isArray(list) ? list : []).filter((f) => f && /^[A-Za-z0-9 ]{1,40}$/.test(f.family) && /^[a-z0-9]+(-[a-z0-9]+)*$/.test(f.id)).slice(0, 8)
|
|
5618
|
+
const css = ok.map((f) => {
|
|
5619
|
+
const w = Array.isArray(f.weights) && f.weights.length === 2 && f.weights.every((x) => Number.isFinite(x)) ? `${f.weights[0]} ${f.weights[1]}` : '100 900'
|
|
5620
|
+
return `@font-face{font-family:"${f.family}";src:url("/__dsh_design__/font/${f.id}.woff2") format("woff2");font-weight:${w};font-display:swap}`
|
|
5621
|
+
}).join('\n')
|
|
5622
|
+
if (!css) { if (tweakFonts) { tweakFonts.remove(); tweakFonts = null } return }
|
|
5623
|
+
if (!tweakFonts) {
|
|
5624
|
+
tweakFonts = document.createElement('style')
|
|
5625
|
+
tweakFonts.setAttribute('data-dsh-design', 'fonts')
|
|
5626
|
+
;(document.head || document.documentElement).appendChild(tweakFonts)
|
|
5627
|
+
}
|
|
5628
|
+
if (tweakFonts.textContent !== css) tweakFonts.textContent = css
|
|
5629
|
+
}
|
|
5630
|
+
|
|
5631
|
+
function applyTweak(vars, attrs, fonts) {
|
|
5632
|
+
applyTweakFonts(fonts)
|
|
5574
5633
|
const de = document.documentElement
|
|
5575
5634
|
const nv = vars && typeof vars === 'object' ? vars : {}
|
|
5576
5635
|
const na = attrs && typeof attrs === 'object' ? attrs : {}
|
|
@@ -5581,7 +5640,7 @@ background:rgba(20,20,22,.86);color:#e8e8ea;font:12px/18px -apple-system,system-
|
|
|
5581
5640
|
tweakVars.delete(k)
|
|
5582
5641
|
}
|
|
5583
5642
|
for (const [k, v] of Object.entries(nv)) {
|
|
5584
|
-
if (!/^--[\w-]{1,60}$/.test(k) || v == null || String(v).length >
|
|
5643
|
+
if (!/^--[\w-]{1,60}$/.test(k) || v == null || String(v).length > 200) continue
|
|
5585
5644
|
if (!tweakVars.has(k)) tweakVars.set(k, { v: de.style.getPropertyValue(k), p: de.style.getPropertyPriority(k) })
|
|
5586
5645
|
de.style.setProperty(k, String(v), 'important')
|
|
5587
5646
|
}
|
|
@@ -5762,7 +5821,7 @@ background:rgba(20,20,22,.86);color:#e8e8ea;font:12px/18px -apple-system,system-
|
|
|
5762
5821
|
else if (d.type === 'styleMode') setStyleMode(!!d.on)
|
|
5763
5822
|
else if (d.type === 'styleSel') onStyleSel(d.selector)
|
|
5764
5823
|
else if (d.type === 'style') { styleApply(d.css); queueTop() }
|
|
5765
|
-
else if (d.type === 'tweak') { applyTweak(d.vars, d.attrs); queueTop() }
|
|
5824
|
+
else if (d.type === 'tweak') { applyTweak(d.vars, d.attrs, d.fonts); queueTop() }
|
|
5766
5825
|
else if (d.type === 'sync') setSync(!!d.on, !!d.slot)
|
|
5767
5826
|
else if (d.type === 'scrollTo') syncTo(d.x, d.y)
|
|
5768
5827
|
})
|
package/lib/serve.js
CHANGED
|
@@ -21,6 +21,8 @@ import { fileURLToPath } from 'node:url'
|
|
|
21
21
|
import { tokenize } from './gallery/html.js'
|
|
22
22
|
import { FRAME_MAIN, SLOT_BOX } from './slot.js'
|
|
23
23
|
import { STATE_RE, statesOfHtml } from './states.js'
|
|
24
|
+
import { MOCK_FILE, MOCK_MAX, mockOf, mockScript, mockBridge, parseMock } from './mock.js'
|
|
25
|
+
import { libFontIdOf } from './fonts.js'
|
|
24
26
|
|
|
25
27
|
export const INTERNAL = '/__dsh_design__/'
|
|
26
28
|
const PICKER_FILE = path.join(path.dirname(fileURLToPath(import.meta.url)), 'picker.js')
|
|
@@ -96,7 +98,8 @@ export function stateScript(html, url) {
|
|
|
96
98
|
let s = ''
|
|
97
99
|
try { s = new URL(String(url || ''), 'http://x').searchParams.get('state') || '' } catch { return '' }
|
|
98
100
|
if (!STATE_RE.test(s) || !statesOfHtml(html).some((x) => x.name === s)) return ''
|
|
99
|
-
|
|
101
|
+
// window.__dshState:模拟数据(mock.js)认它——这页状态跟着数据走时,这一屏临时当成这个状态
|
|
102
|
+
return `<script>document.documentElement.setAttribute("data-state","${s}");window.__dshState="${s}"</script>`
|
|
100
103
|
}
|
|
101
104
|
|
|
102
105
|
/** 放进 <head> 最前面;没写 <head> 就放在 <html> / <!doctype> 后面(放到 doctype 前面页面会进怪异模式) */
|
|
@@ -109,11 +112,12 @@ export function headInsert(html, tag) {
|
|
|
109
112
|
* 在 </body> 前(没有就在末尾)插入选取脚本,<head> 最前面放报错记录。
|
|
110
113
|
* nonce:别人做的设计(设计库来的)加了「只准带这个记号的脚本跑」的规矩,我们自己的两段脚本带上它;data-strict 让 picker 拦外链
|
|
111
114
|
*/
|
|
112
|
-
export function injectPicker(html, { nonce = '', url = '' } = {}) {
|
|
115
|
+
export function injectPicker(html, { nonce = '', url = '', mock = null } = {}) {
|
|
113
116
|
const n = nonce ? ` nonce="${nonce}"` : ''
|
|
114
117
|
const tag = `<script src="${INTERNAL}picker.js"${n}${nonce ? ' data-strict="1"' : ''}></script>`
|
|
115
118
|
const i = html.toLowerCase().lastIndexOf('</body>')
|
|
116
|
-
|
|
119
|
+
// 模拟数据(#30):设计目录里有 mock.json 就每屏都带上(数据里的 < 都转义了,替换 nonce 碰不到它)
|
|
120
|
+
const lead = ERR_CATCHER + stateScript(html, url) + mockScript(mock)
|
|
117
121
|
return headInsert(i >= 0 ? html.slice(0, i) + tag + html.slice(i) : html + tag, nonce ? lead.replaceAll('<script>', `<script${n}>`) : lead)
|
|
118
122
|
}
|
|
119
123
|
|
|
@@ -323,18 +327,18 @@ export function draftInjector(tag) {
|
|
|
323
327
|
}
|
|
324
328
|
}
|
|
325
329
|
|
|
326
|
-
export function createHub({ log = () => {}, onChange = () => {}, onWatch = () => {}, onUnwatch = () => {}, history = null, parents = () => [], strict = null, drafts = null, skipAnnots = () => false } = {}) {
|
|
330
|
+
export function createHub({ log = () => {}, onChange = () => {}, onWatch = () => {}, onUnwatch = () => {}, history = null, parents = () => [], strict = null, drafts = null, skipAnnots = () => false, fontFile = () => null } = {}) {
|
|
327
331
|
const strictHtml = (buf) => { try { return !!(strict && strict.html && strict.html(buf)) } catch (e) { log(e); return true } }
|
|
328
332
|
const strictRoot = (dir) => { try { return !!(strict && strict.root && strict.root(dir)) } catch (e) { log(e); return true } }
|
|
329
333
|
const strictVer = (dir) => { try { return !!(strict && strict.versions && strict.versions(dir)) } catch (e) { log(e); return true } }
|
|
330
334
|
|
|
331
|
-
/** 发一份 HTML:别人做的加硬规矩(CSP + nonce
|
|
332
|
-
function sendHtml(res, req, buf, type, force = false) {
|
|
335
|
+
/** 发一份 HTML:别人做的加硬规矩(CSP + nonce),我们的脚本照常注入;mock:这个设计的模拟数据(没有给 null) */
|
|
336
|
+
function sendHtml(res, req, buf, type, force = false, mock = null) {
|
|
333
337
|
if (force || strictHtml(buf)) {
|
|
334
338
|
const nonce = crypto.randomBytes(16).toString('base64')
|
|
335
|
-
return send(res, 200, req.method === 'HEAD' ? '' : injectPicker(defang(buf.toString('utf8')), { nonce, url: req.url }), { 'Content-Type': type, 'Content-Security-Policy': strictCsp(nonce), ...STRICT_HEADERS })
|
|
339
|
+
return send(res, 200, req.method === 'HEAD' ? '' : injectPicker(defang(buf.toString('utf8')), { nonce, url: req.url, mock }), { 'Content-Type': type, 'Content-Security-Policy': strictCsp(nonce), ...STRICT_HEADERS })
|
|
336
340
|
}
|
|
337
|
-
send(res, 200, req.method === 'HEAD' ? '' : injectPicker(buf.toString('utf8'), { url: req.url }), { 'Content-Type': type })
|
|
341
|
+
send(res, 200, req.method === 'HEAD' ? '' : injectPicker(buf.toString('utf8'), { url: req.url, mock }), { 'Content-Type': type })
|
|
338
342
|
}
|
|
339
343
|
const servers = new Map() // key → entry
|
|
340
344
|
|
|
@@ -456,6 +460,16 @@ export function createHub({ log = () => {}, onChange = () => {}, onWatch = () =>
|
|
|
456
460
|
})
|
|
457
461
|
}
|
|
458
462
|
|
|
463
|
+
/** 「数据」面板背后的小页:只有运行时 + 转告右侧栏的那段(设计目录里没有 mock.json 就没有) */
|
|
464
|
+
function mockPage(req, res, entry) {
|
|
465
|
+
const m = entry.kind === 'file' ? mockOf(entry.root) : null
|
|
466
|
+
if (!m) return send(res, 404, 'no mock data')
|
|
467
|
+
const nonce = crypto.randomBytes(16).toString('base64')
|
|
468
|
+
const parents = JSON.stringify(parentOrigins()).replace(/</g, '\\u003c')
|
|
469
|
+
const html = `<!doctype html><html><head><meta charset="utf-8"><title>mock</title>${mockScript(m)}</head><body><script>(${mockBridge})(${parents})</script></body></html>`
|
|
470
|
+
send(res, 200, req.method === 'HEAD' ? '' : html.replaceAll('<script>', `<script nonce="${nonce}">`), { 'Content-Type': MIME['.html'], 'Content-Security-Policy': strictCsp(nonce), ...STRICT_HEADERS })
|
|
471
|
+
}
|
|
472
|
+
|
|
459
473
|
// ── 本地文件 ──────────────────────────────────────────────────────────────
|
|
460
474
|
|
|
461
475
|
function serveFile(req, res, entry, pathname) {
|
|
@@ -471,6 +485,19 @@ export function createHub({ log = () => {}, onChange = () => {}, onWatch = () =>
|
|
|
471
485
|
if (file && !inRoot(entry.root, file)) return send(res, 403, 'forbidden')
|
|
472
486
|
}
|
|
473
487
|
}
|
|
488
|
+
// 字体库的字体(#31):dsh-fonts/<库 id>.woff2 不在了(复制设计时漏了、手动删了),从字体库缓存给,和导出、历史预览一样
|
|
489
|
+
if (!file) {
|
|
490
|
+
const fm = /(?:^|\/)(dsh-fonts\/[a-z0-9-]+\.woff2)$/.exec(String(pathname))
|
|
491
|
+
const id = fm && libFontIdOf(fm[1])
|
|
492
|
+
if (id) { try { file = fontFile(id) } catch (e) { log(e) } }
|
|
493
|
+
if (file) {
|
|
494
|
+
return fs.readFile(file, (err, buf) => {
|
|
495
|
+
if (err) return send(res, 404, 'no such font')
|
|
496
|
+
res.writeHead(200, { 'Content-Type': 'font/woff2', 'Cache-Control': 'no-cache', 'X-Content-Type-Options': 'nosniff' })
|
|
497
|
+
res.end(req.method === 'HEAD' ? undefined : buf)
|
|
498
|
+
})
|
|
499
|
+
}
|
|
500
|
+
}
|
|
474
501
|
if (!file) {
|
|
475
502
|
const html = errorPage('找不到这个文件', String(pathname).replace(/[<>&]/g, ''), false, 'missing')
|
|
476
503
|
return send(res, 404, html, { 'Content-Type': 'text/html; charset=utf-8' })
|
|
@@ -480,7 +507,7 @@ export function createHub({ log = () => {}, onChange = () => {}, onWatch = () =>
|
|
|
480
507
|
if (ext === '.html' || ext === '.htm') {
|
|
481
508
|
fs.readFile(file, (err, buf) => {
|
|
482
509
|
if (err) return send(res, 500, '读不了这个文件')
|
|
483
|
-
sendHtml(res, req, buf, type)
|
|
510
|
+
sendHtml(res, req, buf, type, false, mockOf(entry.root))
|
|
484
511
|
})
|
|
485
512
|
return
|
|
486
513
|
}
|
|
@@ -493,19 +520,49 @@ export function createHub({ log = () => {}, onChange = () => {}, onWatch = () =>
|
|
|
493
520
|
stream.pipe(res)
|
|
494
521
|
}
|
|
495
522
|
|
|
523
|
+
/**
|
|
524
|
+
* 字体库的字体(#31):/__dsh_design__/font/<id>.woff2,面板里试字体、还没存进设计时页面从这里拿。
|
|
525
|
+
* 只给目录里列着、已经下载好的(fontFile 管);同源用,别的源拿不走
|
|
526
|
+
*/
|
|
527
|
+
function libFont(req, res, pathname) {
|
|
528
|
+
if (req.method !== 'GET' && req.method !== 'HEAD') return send(res, 405, 'method not allowed')
|
|
529
|
+
const m = /^\/__dsh_design__\/font\/([a-z0-9]+(?:-[a-z0-9]+)*)\.woff2$/.exec(pathname)
|
|
530
|
+
let file = null
|
|
531
|
+
try { file = m ? fontFile(m[1]) : null } catch (e) { log(e) }
|
|
532
|
+
if (!file) return send(res, 404, 'no such font')
|
|
533
|
+
fs.readFile(file, (err, buf) => {
|
|
534
|
+
if (err) return send(res, 404, 'no such font')
|
|
535
|
+
res.writeHead(200, { 'Content-Type': 'font/woff2', 'Cache-Control': 'max-age=3600', 'Cross-Origin-Resource-Policy': 'same-origin', 'X-Content-Type-Options': 'nosniff' })
|
|
536
|
+
res.end(req.method === 'HEAD' ? undefined : buf)
|
|
537
|
+
})
|
|
538
|
+
}
|
|
539
|
+
|
|
540
|
+
/** 旧版本那时候的 mock.json(那一版没有就没有) */
|
|
541
|
+
function versionMock(root, v) {
|
|
542
|
+
try {
|
|
543
|
+
const f = history && history.fileOf(root, v, MOCK_FILE)
|
|
544
|
+
if (!f) return null
|
|
545
|
+
const st = fs.statSync(f)
|
|
546
|
+
// 旧版本的屏单独一份(scope v):开旧版本不会把现在点出来的数据当成「旧指纹」清掉
|
|
547
|
+
return st.size > MOCK_MAX ? null : { ...parseMock(fs.readFileSync(f, 'utf8')), scope: 'v' }
|
|
548
|
+
} catch { return null }
|
|
549
|
+
}
|
|
550
|
+
|
|
496
551
|
/** 旧版本里的文件(只读;HTML 照样注入 picker,好在画布里显示和对比) */
|
|
497
552
|
function serveVersion(req, res, entry, pathname) {
|
|
498
553
|
const m = /^\/__dsh_design__\/v\/(\d+)\/(.*)$/.exec(pathname)
|
|
499
554
|
let rel = ''
|
|
500
555
|
try { rel = m ? decodeURIComponent(m[2]) : '' } catch { rel = '' }
|
|
501
|
-
|
|
556
|
+
let file = m && history && !rel.split('/').includes('..') ? history.fileOf(entry.root, Number(m[1]), rel) : null
|
|
557
|
+
// 字体库的字体不进版本记录(#31):旧版本要的从字体库缓存拿(目录里列着、下载好了的才给)
|
|
558
|
+
if (!file && m && libFontIdOf(rel)) { try { file = fontFile(libFontIdOf(rel)) } catch (e) { log(e) } }
|
|
502
559
|
if (!file) return send(res, 404, 'no such version file')
|
|
503
560
|
const ext = path.extname(rel || 'index.html').toLowerCase()
|
|
504
561
|
const type = MIME[ext] || 'application/octet-stream'
|
|
505
562
|
fs.readFile(file, (err, buf) => {
|
|
506
563
|
if (err) return send(res, 404, 'no such version file')
|
|
507
564
|
// 装好的别人的设计系统:旧版本里的页面一律加规矩
|
|
508
|
-
if (ext === '.html' || ext === '.htm') return sendHtml(res, req, buf, type, strictVer(entry.root))
|
|
565
|
+
if (ext === '.html' || ext === '.htm') return sendHtml(res, req, buf, type, strictVer(entry.root), versionMock(entry.root, Number(m[1])))
|
|
509
566
|
const extra = strictRoot(entry.root) ? { 'X-Content-Type-Options': 'nosniff', ...(ext === '.svg' ? { 'Content-Security-Policy': STRICT_SVG_CSP } : {}) } : {}
|
|
510
567
|
res.writeHead(200, { 'Content-Type': type, 'Cache-Control': 'no-store', 'Cross-Origin-Resource-Policy': 'same-origin', ...extra })
|
|
511
568
|
res.end(req.method === 'HEAD' ? undefined : buf)
|
|
@@ -706,6 +763,8 @@ export function createHub({ log = () => {}, onChange = () => {}, onWatch = () =>
|
|
|
706
763
|
try { pathname = new URL(req.url, 'http://x').pathname } catch { return send(res, 400, 'bad url') }
|
|
707
764
|
if (pathname === INTERNAL + 'picker.js') return picker(res, entry)
|
|
708
765
|
if (pathname === INTERNAL + 'draft.js') return draftJs(res)
|
|
766
|
+
if (pathname === INTERNAL + 'mock.html') return mockPage(req, res, entry)
|
|
767
|
+
if (pathname.startsWith(INTERNAL + 'font/')) return libFont(req, res, pathname)
|
|
709
768
|
// 草稿就挂在这一屏自己的地址上(/menu.html?__ddp_draft=…),页面里的相对地址(tokens.css、图片)照常对得上
|
|
710
769
|
if (entry.kind === 'file' && /[?&]__ddp_draft=/.test(req.url)) return serveDraft(req, res, entry, pathname)
|
|
711
770
|
if (pathname === INTERNAL + 'events') {
|
package/lib/states.js
CHANGED
|
@@ -28,6 +28,15 @@ export function parseStates(raw) {
|
|
|
28
28
|
}
|
|
29
29
|
|
|
30
30
|
/** HTML(开头那段就够)里写的状态(注释里的示例不算:浏览器里它不是 <meta>) */
|
|
31
|
+
/** 这页的状态跟着模拟数据走(<meta name="dsh-states" … data-mock="…">):不点名就是数据里那个,点名第一个不能省 */
|
|
32
|
+
export function followsMock(html) {
|
|
33
|
+
const s = String(html || '').replace(/<!--[\s\S]*?(?:-->|$)/g, '')
|
|
34
|
+
const re = /<meta\b[^>]*>/gi
|
|
35
|
+
let m
|
|
36
|
+
while ((m = re.exec(s))) if (/\bname\s*=\s*["']?dsh-states["'\s/>]/i.test(m[0])) return /\sdata-mock\s*=/i.test(m[0])
|
|
37
|
+
return false
|
|
38
|
+
}
|
|
39
|
+
|
|
31
40
|
export function statesOfHtml(html) {
|
|
32
41
|
const s = String(html || '').replace(/<!--[\s\S]*?(?:-->|$)/g, '')
|
|
33
42
|
const re = /<meta\b[^>]*>/gi
|
|
@@ -89,8 +98,9 @@ export function expandStates(list, statesOf, { perScreen = 6, extra = 12, only =
|
|
|
89
98
|
const out = []
|
|
90
99
|
let added = 0
|
|
91
100
|
let skipped = 0
|
|
101
|
+
// 默认那行 key 不变;地址也写明第一个状态(状态跟着模拟数据走的屏,不写就是数据里那个状态,会和后面某行重复)
|
|
92
102
|
const row = (sc, x, first) => first
|
|
93
|
-
? { ...sc, base: sc.key, state: x.name, stateLabel: x.label }
|
|
103
|
+
? { ...sc, url: withState(sc.url, x.name), base: sc.key, state: x.name, stateLabel: x.label }
|
|
94
104
|
: { ...sc, key: `${sc.key}?state=${x.name}`, url: withState(sc.url, x.name), base: sc.key, state: x.name, stateLabel: x.label }
|
|
95
105
|
for (const sc of list) {
|
|
96
106
|
const st = statesOf(sc) || []
|
package/lib/tool.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* dsh-design-studio/tool:把 preview_page、check_design、look_design、extract_design_system、handoff_design、compare_design、find_photos、pick_style、save_frame 挂给模型。
|
|
2
|
+
* dsh-design-studio/tool:把 preview_page、check_design、look_design、extract_design_system、handoff_design、compare_design、find_photos、use_fonts、pick_style、save_frame 挂给模型。
|
|
3
3
|
* 放在 bundle 顶层 → 所有模式都能用;放进设计模式 → 只挂在那个模式里。工具本身见 index.js
|
|
4
4
|
*/
|
|
5
|
-
import { previewTool, checkTool, lookTool, systemTool, handoffTool, compareTool, photoTool, styleTool, frameTool } from './index.js'
|
|
5
|
+
import { previewTool, checkTool, lookTool, systemTool, handoffTool, compareTool, photoTool, fontTool, styleTool, frameTool } from './index.js'
|
|
6
6
|
|
|
7
7
|
export const name = 'dsh-design-studio-tool'
|
|
8
8
|
export const inject = ['tools']
|
|
@@ -16,6 +16,7 @@ export function apply(ctx) {
|
|
|
16
16
|
ctx.tools.register({ ...handoffTool, execute: (args, exec) => handoffTool.execute(args, exec, ctx) })
|
|
17
17
|
ctx.tools.register({ ...compareTool, execute: (args, exec) => compareTool.execute(args, exec, ctx) })
|
|
18
18
|
ctx.tools.register({ ...photoTool })
|
|
19
|
+
ctx.tools.register({ ...fontTool })
|
|
19
20
|
ctx.tools.register({ ...styleTool })
|
|
20
21
|
ctx.tools.register({ ...frameTool, execute: (args, exec) => frameTool.execute(args, exec, ctx) })
|
|
21
22
|
}
|
package/lib/tweaks.js
CHANGED
|
@@ -17,6 +17,7 @@ import fs from 'node:fs'
|
|
|
17
17
|
import path from 'node:path'
|
|
18
18
|
import { allScreens } from './core.js'
|
|
19
19
|
import { parseColor, hexOf } from './system.js'
|
|
20
|
+
import { FONT_DIR, fontBlockIds, fontPrefix, firstFamily, libFontOf, libIdsNamed, stripFontBlock, syncFontBlock } from './fonts.js'
|
|
20
21
|
|
|
21
22
|
export const TWEAKS_FILE = 'tweaks.json'
|
|
22
23
|
export const MAX_TWEAKS = 8
|
|
@@ -172,9 +173,9 @@ const fill = (m) => m.replace(/[^\n]/g, '_')
|
|
|
172
173
|
// 一遍从左往右认:先碰到哪个算哪个(字符串里的 /* 不是注释,注释里的引号也不是字符串);
|
|
173
174
|
// 字符串、url(…) 换成非空白:它们是值的一部分,不能被当成空白吃掉
|
|
174
175
|
const mask = (css) => css.replace(/\/\*[\s\S]*?\*\/|"(?:[^"\\\n]|\\[\s\S])*"|'(?:[^'\\\n]|\\[\s\S])*'|\burl\(\s*[^)'"\s][^)]*\)/gi, (m) => (m.startsWith('/*') ? blank(m) : fill(m)))
|
|
175
|
-
/** HTML 里的注释、<script> / <style> 的内容换成等长空格:找 <html>
|
|
176
|
+
/** HTML 里的注释、<script> / <noscript> / <style> 的内容换成等长空格:找 <html> 或生效样式时不会找到这些里面去 */
|
|
176
177
|
const maskHtml = (html, keepStyle = false) => html.replace(/<!--[\s\S]*?-->/g, blank)
|
|
177
|
-
.replace(keepStyle ? /<(script)\b[^>]*>[\s\S]
|
|
178
|
+
.replace(keepStyle ? /<(script|noscript)\b[^>]*>[\s\S]*?<\/\1\s*>/gi : /<(script|style|noscript)\b[^>]*>[\s\S]*?<\/\1\s*>/gi, blank)
|
|
178
179
|
|
|
179
180
|
/** 下标 i 在哪个规则块里:{ selector, depth }(depth 1 = 顶层规则,@media 里面的是 2) */
|
|
180
181
|
function blockAt(css, i) {
|
|
@@ -215,8 +216,8 @@ export function setVarDecl(css, name, value, nested = false) {
|
|
|
215
216
|
}
|
|
216
217
|
|
|
217
218
|
/** HTML 里 <style> 块中的 :root 声明(规则同 setVarDecl;注释、脚本里的不算) */
|
|
218
|
-
function setVarInHtml(html, name, value, nested = false) {
|
|
219
|
-
const re = /(<style\b[^>]*>)([\s\S]*?)(<\/style\s*>)/gi
|
|
219
|
+
export function setVarInHtml(html, name, value, nested = false) {
|
|
220
|
+
const re = /(<style\b(?:[^>"']|"[^"]*"|'[^']*')*>)([\s\S]*?)(<\/style\s*>)/gi // 属性值里的 > 不算结束(media=\"(width >= 900px)\")
|
|
220
221
|
const m0 = maskHtml(html, true)
|
|
221
222
|
let out = html
|
|
222
223
|
let hit = false
|
|
@@ -361,6 +362,136 @@ export function applyTweaks(root, values, vars) {
|
|
|
361
362
|
return { ok: true, changed, files: [...texts.keys()], missed }
|
|
362
363
|
}
|
|
363
364
|
|
|
365
|
+
// ── 字体库:@font-face 那段 ─────────────────────────────────────────────────
|
|
366
|
+
|
|
367
|
+
/**
|
|
368
|
+
* 设计里的样式和页面(.css、.html,往下最多 6 层;软链接、点开头的、node_modules 不看):看字体名写在哪用
|
|
369
|
+
* @returns [[相对路径, 文字]],没看全(文件太大只看了开头、超过 300 个、读不了)时带 partial: true
|
|
370
|
+
*/
|
|
371
|
+
export function designTexts(root) {
|
|
372
|
+
const out = []
|
|
373
|
+
let n = 0
|
|
374
|
+
const walk = (dir, rel, depth) => {
|
|
375
|
+
let list
|
|
376
|
+
try { list = fs.readdirSync(dir, { withFileTypes: true }) } catch { out.partial = true; return }
|
|
377
|
+
for (const d of list) {
|
|
378
|
+
if (d.name.startsWith('.') || d.name === 'node_modules') continue
|
|
379
|
+
const r = rel ? `${rel}/${d.name}` : d.name
|
|
380
|
+
if (d.isDirectory()) { if (d.name === FONT_DIR) continue; if (depth < 6) walk(path.join(dir, d.name), r, depth + 1); else out.partial = true; continue }
|
|
381
|
+
if (!d.isFile() || !/\.(css|html?)$/i.test(d.name)) continue
|
|
382
|
+
if (n >= 300) { out.partial = true; continue }
|
|
383
|
+
n++
|
|
384
|
+
try {
|
|
385
|
+
const t = fs.readFileSync(path.join(dir, d.name), 'utf8')
|
|
386
|
+
if (t.length > MAX_FILE) out.partial = true
|
|
387
|
+
out.push([r, t.slice(0, MAX_FILE)])
|
|
388
|
+
} catch { out.partial = true }
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
walk(root, '', 0)
|
|
392
|
+
return out
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
/**
|
|
396
|
+
* 整套设计里(那段以外)写着名字的字体库 id;over:{ 相对路径: 还没写进去的新文字 }。
|
|
397
|
+
* 没看全(designTexts 的 partial)时结果带 partial: true——调用方这时一款都不去掉(宁可多留一条用不上的 @font-face)
|
|
398
|
+
*/
|
|
399
|
+
export function libFontsUsed(root, index, over = null) {
|
|
400
|
+
const out = new Set()
|
|
401
|
+
if (!index) return out
|
|
402
|
+
const seen = new Set()
|
|
403
|
+
const texts = designTexts(root)
|
|
404
|
+
if (texts.partial) out.partial = true
|
|
405
|
+
for (const [rel, t] of texts) {
|
|
406
|
+
seen.add(rel)
|
|
407
|
+
for (const id of libIdsNamed(over && Object.hasOwn(over, rel) ? over[rel] : t, index)) out.add(id)
|
|
408
|
+
}
|
|
409
|
+
for (const [rel, t] of Object.entries(over || {})) if (!seen.has(rel)) for (const id of libIdsNamed(t, index)) out.add(id)
|
|
410
|
+
return out
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
/**
|
|
414
|
+
* 改过的样式文件里,设计台管的 @font-face 那段跟着变(add:这次选的字体库 id;drop:这次换走了、整套设计都不再写名字的)。
|
|
415
|
+
* rels 只认设计目录自己的 .css / .html(软链接、指到外面的不动)。
|
|
416
|
+
* @returns 改了的文件(相对路径)
|
|
417
|
+
*/
|
|
418
|
+
export function syncFonts(root, rels, add, index, { drop = [] } = {}) {
|
|
419
|
+
if (!index) return []
|
|
420
|
+
let realRoot
|
|
421
|
+
try { realRoot = fs.realpathSync(root) } catch { return [] }
|
|
422
|
+
const out = []
|
|
423
|
+
for (const rel of rels) {
|
|
424
|
+
if (!/\.(css|html?)$/i.test(rel) || !ownFile(root, realRoot, rel)) continue
|
|
425
|
+
let t
|
|
426
|
+
try { t = fs.readFileSync(path.join(root, rel), 'utf8') } catch { continue }
|
|
427
|
+
const next = syncFontBlock(t, add, index, { prefix: fontPrefix(rel), isHtml: /\.html?$/i.test(rel), drop })
|
|
428
|
+
if (next === null) continue
|
|
429
|
+
fs.writeFileSync(path.join(root, rel), next)
|
|
430
|
+
out.push(rel)
|
|
431
|
+
}
|
|
432
|
+
return out
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
// 外链的样式表 / 字体:<link href="https://…">(样式表、预连接、字体)、@import 外链、@font-face 的 src 外链
|
|
436
|
+
const EXTERNAL_FONT_RE = /<link\b(?=[^>]*\b(?:stylesheet|preconnect|preload|font))[^>]*\bhref\s*=\s*["']?(?:https?:)?\/\/|@import\s+(?:url\(\s*)?["']?(?:https?:)?\/\/|\burl\(\s*["']?(?:https?:)?\/\/[^)]*\.(?:woff2?|ttf|otf)\b/i
|
|
437
|
+
|
|
438
|
+
// 这几款常被写在系统字体栈里当后备(没装就跳过),名字出现不等于想用字体库
|
|
439
|
+
const COMMON_FONTS = new Set(['noto-sans-sc', 'noto-serif-sc', 'inter', 'jetbrains-mono'])
|
|
440
|
+
|
|
441
|
+
/**
|
|
442
|
+
* 字体库的字体有没有真放进来(check_design 末尾附上):
|
|
443
|
+
* - 那段写了某款、dsh-fonts/ 里却没有那个文件(复制设计时漏了)
|
|
444
|
+
* - 样式里打头写着字体库的字体,却没有那段:tokens.css 被整个重写时那段会丢;别人电脑上多半没装这款
|
|
445
|
+
* (思源黑体、Inter 这几款常被当系统后备写在最前面,只有 dsh-fonts/ 里放着它们的文件时才算想用字体库;
|
|
446
|
+
* 设计自己写了这个名字的 @font-face——自带的字体文件——也不报;设计从外面加载样式 / 字体(Google Fonts 之类)、
|
|
447
|
+
* dsh-fonts/ 里又没有这款的文件时也不报:多半是从那来的)
|
|
448
|
+
*/
|
|
449
|
+
export function lintFonts(root, index) {
|
|
450
|
+
if (!index) return []
|
|
451
|
+
const out = []
|
|
452
|
+
const inBlock = new Set()
|
|
453
|
+
const own = new Set() // 设计自己 @font-face 声明过的名字(小写)
|
|
454
|
+
const first = new Map() // id → 在哪个文件打头用了
|
|
455
|
+
let external = false // 设计自己从外面加载样式 / 字体(Google Fonts 之类):说不准那款是不是从那来的
|
|
456
|
+
const texts = designTexts(root)
|
|
457
|
+
for (const [rel, t] of texts) {
|
|
458
|
+
for (const id of fontBlockIds(t)) inBlock.add(id)
|
|
459
|
+
const stripped = stripFontBlock(t)
|
|
460
|
+
const isHtml = /\.html?$/i.test(rel)
|
|
461
|
+
let body
|
|
462
|
+
if (isHtml) {
|
|
463
|
+
const clean = maskHtml(stripped, true)
|
|
464
|
+
if (!external && EXTERNAL_FONT_RE.test(clean)) external = true
|
|
465
|
+
const parts = []
|
|
466
|
+
for (const m of clean.matchAll(/<style\b(?:[^>"']|"[^"]*"|'[^']*')*>([\s\S]*?)<\/style\s*>/gi)) parts.push(m[1])
|
|
467
|
+
const noStyle = clean.replace(/<style\b(?:[^>"']|"[^"]*"|'[^']*')*>[\s\S]*?<\/style\s*>/gi, blank)
|
|
468
|
+
for (const m of noStyle.matchAll(/\bstyle\s*=\s*(?:"([^"]*)"|'([^']*)')/gi)) parts.push(m[1] ?? m[2] ?? '')
|
|
469
|
+
body = parts.join('\n;\n').replace(/\/\*[\s\S]*?\*\//g, blank)
|
|
470
|
+
} else {
|
|
471
|
+
body = stripped.replace(/\/\*[\s\S]*?\*\//g, blank)
|
|
472
|
+
if (!external && EXTERNAL_FONT_RE.test(body)) external = true
|
|
473
|
+
}
|
|
474
|
+
for (const m of body.matchAll(/@font-face\s*\{[^}]*?font-family\s*:\s*(?:"([^"]+)"|'([^']+)'|([^;}]+))/gi)) own.add(String(m[1] ?? m[2] ?? m[3]).trim().toLowerCase())
|
|
475
|
+
for (const m of body.matchAll(/(?:^|[{;\s"'])(?:font-family|--[\w-]+)\s*:\s*([^;{}<]+)/g)) {
|
|
476
|
+
const f = libFontOf(index, m[1])
|
|
477
|
+
if (f && firstFamily(m[1]) && !first.has(f.id)) first.set(f.id, rel)
|
|
478
|
+
}
|
|
479
|
+
}
|
|
480
|
+
const has = (id) => { try { return fs.statSync(path.join(root, FONT_DIR, `${id}.woff2`)).isFile() } catch { return false } }
|
|
481
|
+
for (const id of inBlock) {
|
|
482
|
+
const f = index.fonts.find((x) => x.id === id)
|
|
483
|
+
if (f && !has(id)) out.push(`${FONT_DIR}/${id}.woff2 不在了(${f.zh}):调 use_fonts 再放一次`)
|
|
484
|
+
}
|
|
485
|
+
for (const [id, rel] of first) {
|
|
486
|
+
if (inBlock.has(id)) continue
|
|
487
|
+
const f = index.fonts.find((x) => x.id === id)
|
|
488
|
+
if (own.has(f.family.toLowerCase())) continue
|
|
489
|
+
if ((COMMON_FONTS.has(id) || external) && !has(id)) continue
|
|
490
|
+
out.push(`${rel} 里打头用了字体库的「${f.zh}」(${f.family}),但没有它的 @font-face(tokens.css 整个重写时那段会丢):调 use_fonts 放进来,之后改 tokens.css 用局部修改`)
|
|
491
|
+
}
|
|
492
|
+
return out
|
|
493
|
+
}
|
|
494
|
+
|
|
364
495
|
// ── 体检:滑块有没有真接上 ───────────────────────────────────────────────────
|
|
365
496
|
|
|
366
497
|
/**
|
package/package.json
CHANGED
|
@@ -50,6 +50,21 @@
|
|
|
50
50
|
|
|
51
51
|
**同一个页面的几种样子(空列表 / 有数据 / 已付款 / 出错)写成「状态」,别另做一屏**:`<head>` 里写 `<meta name="dsh-states" content="default:有数据, empty:空列表, paid:已付款">`(第一个是默认;冒号前是英文名,后面是给人看的名字;最多 8 个),`<html data-state="default">`,差别全用 CSS 写:`[data-state="empty"] .list { display: none }`。跳到某个状态就链接 `order.html?state=paid`,同一屏里换状态写 `<a href="?state=paid">`。画布上屏名旁边能切状态,`check_design` 每个状态都量。只有一种样子的屏不用写。
|
|
52
52
|
|
|
53
|
+
**点了会变、换页还在的原型,用「模拟数据」**(多角色 / 多阶段、有列表和表单、点按钮后内容要跟着变的 App 原型;只是静态展示的不用):在这套设计的目录里写一个 `mock.json`(一个对象,整套原型共用,用户名、阶段、列表、表单都放这里,写真实的样例内容),页面里不写 JS 就能用:
|
|
54
|
+
|
|
55
|
+
```html
|
|
56
|
+
<span data-mock-text="user.name"></span> <!-- 显示;也可以拼:'¥' + order.total -->
|
|
57
|
+
<div data-mock-show="user.stage == 'onsite'">…</div> <!-- 条件不成立就藏起来;&& || ! 比较 三元都能写 -->
|
|
58
|
+
<button data-mock-set="user.stage = 'onsite'; unread = 0">接受邀约</button> <!-- 点了改数据,几句用 ; 隔开;也可以同时是链接 -->
|
|
59
|
+
<a data-mock-class="on: tab == 'home'" data-mock-set="tab = 'home'">首页</a> <!-- 按条件加 class -->
|
|
60
|
+
<button data-mock-attr="disabled: !form.agree">下一步</button> <!-- 按条件设属性 -->
|
|
61
|
+
<input data-mock-model="form.phone"> <!-- 输入框、勾选框和数据双向,填了换页还在 -->
|
|
62
|
+
<ul data-mock-each="app in applications"><template><li><b data-mock-text="app.title"></b><button data-mock-set="app.status = 'done'">完成</button></li></template></ul>
|
|
63
|
+
<button data-mock-reset>重置演示数据</button>
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
页面的状态可以跟着数据走:`<meta name="dsh-states" content="interviewing:面试中, onsite:在项" data-mock="user.stage">`——数据里 `user.stage` 是几,这页就是哪个状态;角色、阶段这种全局的东西这样写,一处改了所有页一起变,不用每种组合做一屏。改了的数据存在浏览器里,换页、刷新都还在;`mock.json` 一改就从新文件重新开始。复杂的逻辑写脚本:`mock.get('user.stage')`、`mock.set('cart.count', 2)`、`mock.push('cart.items', {…})`、`mock.remove('cart.items', 0)`、`mock.on((data) => …)`、`mock.reset()`——**不要自己用 localStorage 另写一套数据**。mock.json 里有的东西(名字、金额、状态),页面上显示它的地方都绑上,别这屏绑了、那屏写死——用户在右侧栏「数据」里改一处,所有屏要一起变。条件里只能写字段、字面量和运算,不能调函数。写错的条件、找不到的字段 `check_design` 会报出来。
|
|
67
|
+
|
|
53
68
|
4. **写完第一屏就调 `preview_page`**(`target` 填这个文件,`kind` 填类型),用户从第一屏起就能看着它长出来;后面的屏写到同一目录会自动并排出现,不用再调。
|
|
54
69
|
|
|
55
70
|
5. **写完先量、再看,然后收尾**:
|
|
@@ -85,7 +100,7 @@
|
|
|
85
100
|
|
|
86
101
|
**准备分享**(消息以〔准备分享〕开头):用户要把设计打成 zip 发给别人,打包前的检查查出了这些问题。逐条改,没列出来的地方别动:
|
|
87
102
|
- 有脚本、`on…` 事件、`javascript:` 链接:删掉(设计稿是静态的,交互效果用 CSS 的 `:hover`、`:target`、`<details>` 代替);
|
|
88
|
-
- 连了外网(外链的字体、图片、样式,`@import`):图片下载进目录用相对路径,或者换成 CSS / SVG
|
|
103
|
+
- 连了外网(外链的字体、图片、样式,`@import`):图片下载进目录用相对路径,或者换成 CSS / SVG 画的;字体改用系统字体栈(分享包不带字体文件,别调 `use_fonts`);
|
|
89
104
|
- 像真的手机号、邮箱、身份证号:换成明显是假的(`138 0000 0000`、`hello@example.com`);
|
|
90
105
|
- 文件太大、太多:压小图片、删掉没用到的文件。
|
|
91
106
|
改完调 `check_design` 体检一遍,说一句改了哪几处;用户会再点一次「检查并打包」。
|
|
@@ -133,6 +148,8 @@
|
|
|
133
148
|
|
|
134
149
|
**图片**:用户给了照片就用用户的。靠照片说话的设计(民宿酒店、餐饮、奶茶咖啡、健身、美妆、服饰、教育、旅行风景)需要头图、封面、商品图时,调 `find_photos` 从自带照片库拿,一次要够这几屏用的,别画插画冒充照片;库里没有合适的,或者题材本身不靠照片(工具、后台、数据类),才用 SVG 插画、几何构图或色块。不引用外网图片(预览离线也要好看)。库里的是 AI 生成的示意图:不写「实拍」,做完告诉用户上线前换成真实照片。
|
|
135
150
|
|
|
151
|
+
**字体**:默认用系统字体栈(见「中文排版」)。用户点名要某种字体,或这套设计要系统字体给不了的气质(国风书法标题、潮流海报、手账手写、杂志感衬线),调 `use_fonts` 从自带字体库放进来(免费可商用,工具说明里列着有哪些):先建好 `tokens.css`,再调 `use_fonts`(`font` 写字体名,`var` 写要换的变量,如 `--font-display`),它会复制字体文件、在 tokens.css 开头写好 `@font-face`(「dsh-fonts」那段由设计台管,别改)。之后改 tokens.css 用局部修改,别整个重写,重写了再调一次。一套设计最多两款库里的字体(标题一款、正文一款);书法、手写字体只用在大标题,正文用它很难读。不引用外网字体(Google Fonts 之类)。
|
|
152
|
+
|
|
136
153
|
**图标**:用一套线性 SVG 图标(同样的线宽 1.75–2、圆角端点、24 网格),内联写进 HTML。不要用 emoji 当图标。
|
|
137
154
|
|
|
138
155
|
**状态**:按钮的悬停 / 按下 / 禁用,输入框的聚焦 / 出错,列表的空状态、加载、出错——用户会点的地方都要有。
|