dsh-design-studio 1.2.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 +1 -0
- package/README.md +1 -0
- package/lib/bundle.js +19 -15
- package/lib/client.js +254 -10
- 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 +7 -0
- package/lib/handoff.js +7 -0
- package/lib/history.js +2 -0
- package/lib/htmlpack.js +30 -5
- package/lib/index.js +191 -9
- package/lib/picker.js +47 -4
- package/lib/serve.js +36 -2
- package/lib/tool.js +3 -2
- package/lib/tweaks.js +135 -4
- package/package.json +1 -1
- package/skills/ui-design/SKILL.md +3 -1
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
|
@@ -100,7 +100,7 @@
|
|
|
100
100
|
|
|
101
101
|
**准备分享**(消息以〔准备分享〕开头):用户要把设计打成 zip 发给别人,打包前的检查查出了这些问题。逐条改,没列出来的地方别动:
|
|
102
102
|
- 有脚本、`on…` 事件、`javascript:` 链接:删掉(设计稿是静态的,交互效果用 CSS 的 `:hover`、`:target`、`<details>` 代替);
|
|
103
|
-
- 连了外网(外链的字体、图片、样式,`@import`):图片下载进目录用相对路径,或者换成 CSS / SVG
|
|
103
|
+
- 连了外网(外链的字体、图片、样式,`@import`):图片下载进目录用相对路径,或者换成 CSS / SVG 画的;字体改用系统字体栈(分享包不带字体文件,别调 `use_fonts`);
|
|
104
104
|
- 像真的手机号、邮箱、身份证号:换成明显是假的(`138 0000 0000`、`hello@example.com`);
|
|
105
105
|
- 文件太大、太多:压小图片、删掉没用到的文件。
|
|
106
106
|
改完调 `check_design` 体检一遍,说一句改了哪几处;用户会再点一次「检查并打包」。
|
|
@@ -148,6 +148,8 @@
|
|
|
148
148
|
|
|
149
149
|
**图片**:用户给了照片就用用户的。靠照片说话的设计(民宿酒店、餐饮、奶茶咖啡、健身、美妆、服饰、教育、旅行风景)需要头图、封面、商品图时,调 `find_photos` 从自带照片库拿,一次要够这几屏用的,别画插画冒充照片;库里没有合适的,或者题材本身不靠照片(工具、后台、数据类),才用 SVG 插画、几何构图或色块。不引用外网图片(预览离线也要好看)。库里的是 AI 生成的示意图:不写「实拍」,做完告诉用户上线前换成真实照片。
|
|
150
150
|
|
|
151
|
+
**字体**:默认用系统字体栈(见「中文排版」)。用户点名要某种字体,或这套设计要系统字体给不了的气质(国风书法标题、潮流海报、手账手写、杂志感衬线),调 `use_fonts` 从自带字体库放进来(免费可商用,工具说明里列着有哪些):先建好 `tokens.css`,再调 `use_fonts`(`font` 写字体名,`var` 写要换的变量,如 `--font-display`),它会复制字体文件、在 tokens.css 开头写好 `@font-face`(「dsh-fonts」那段由设计台管,别改)。之后改 tokens.css 用局部修改,别整个重写,重写了再调一次。一套设计最多两款库里的字体(标题一款、正文一款);书法、手写字体只用在大标题,正文用它很难读。不引用外网字体(Google Fonts 之类)。
|
|
152
|
+
|
|
151
153
|
**图标**:用一套线性 SVG 图标(同样的线宽 1.75–2、圆角端点、24 网格),内联写进 HTML。不要用 emoji 当图标。
|
|
152
154
|
|
|
153
155
|
**状态**:按钮的悬停 / 按下 / 禁用,输入框的聚焦 / 出错,列表的空状态、加载、出错——用户会点的地方都要有。
|