dsh-design-studio 0.22.0 → 0.23.1

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/lib/core.js CHANGED
@@ -35,7 +35,8 @@ const MAX_SCREENS = 60 // 右侧最多显示几屏,再多的收起来并提示
35
35
  const MAX_ROUTES = 300 // 开发服务器的路由先都记着(只为算出收起了几屏);再多就是写飞了
36
36
 
37
37
  // 设计类型:决定右侧用什么画幅显示(网页框、手机、小程序、平板、桌面软件窗口、海报、幻灯片、邮件、图表)
38
- export const KINDS = ['web', 'app', 'miniprogram', 'tablet', 'desktop', 'poster', 'slides', 'email', 'diagram', 'dshplugin']
38
+ import { KINDS } from './kinds.js'
39
+ export { KINDS }
39
40
  const KIND_ALIAS = {
40
41
  website: 'web', site: 'web', page: 'web', landing: 'web', dashboard: 'web',
41
42
  phone: 'app', mobile: 'app', ios: 'app', android: 'app',
@@ -13,6 +13,7 @@
13
13
  import crypto from 'node:crypto'
14
14
  import { tokenize, scanTags, decodeEntities, lineAt, visibleText, rawEnd } from './html.js'
15
15
  import { scanCss, cssTokens, URL_FUNCS } from './css.js'
16
+ import { KINDS, META_ALIAS } from '../kinds.js'
16
17
 
17
18
  export const FORMAT = 1
18
19
  export const LIMITS = {
@@ -38,10 +39,9 @@ export const capsOf = (mode) => (mode === 'share' || mode === 'ci'
38
39
  : { files: LIMITS.localFiles, screens: LIMITS.localScreens, total: LIMITS.localTotal })
39
40
  export const TYPES = ['template', 'system', 'style']
40
41
  export const LICENSES = ['CC0-1.0', 'CC-BY-4.0']
41
- export const KINDS = ['web', 'app', 'miniprogram', 'tablet', 'desktop', 'poster', 'slides', 'email', 'diagram', 'dshplugin']
42
+ export { KINDS }
42
43
  export const STYLE_IDS = ['minimal', 'warm', 'editorial', 'dark', 'playful', 'fresh', 'tech', 'glass', 'soft', 'guochao', 'ink', 'cyber', 'cute', 'retro', 'sale', 'japandi', 'swiss', 'brutal', 'cream', 'sport']
43
44
  export const HEADS = ['SANS', 'SERIF', 'ROUND', 'MONO', 'GROTESK'] // 风格卡的字体只能是这几种系统字体栈
44
- const META_ALIAS = { phone: 'app', mobile: 'app', ios: 'app', android: 'app', mini: 'miniprogram', miniapp: 'miniprogram', weapp: 'miniprogram', wechat: 'miniprogram', 小程序: 'miniprogram', pad: 'tablet', ipad: 'tablet', pc: 'desktop', electron: 'desktop', tauri: 'desktop', mac: 'desktop', macos: 'desktop', windows: 'desktop', win: 'desktop', 桌面: 'desktop', 桌面软件: 'desktop', social: 'poster', image: 'poster', slide: 'slides', deck: 'slides', ppt: 'slides', mail: 'email', mindmap: 'diagram', roadmap: 'diagram', architecture: 'diagram', flowchart: 'diagram', 思维导图: 'diagram', 导图: 'diagram', 路线图: 'diagram', 架构图: 'diagram', 图表: 'diagram', dsh: 'dshplugin', 'dsh-plugin': 'dshplugin', dsh插件: 'dshplugin' }
45
45
 
46
46
  const EXT = { html: 'html', css: 'css', svg: 'svg', png: 'image', jpg: 'image', jpeg: 'image', webp: 'image', gif: 'image', avif: 'image', json: 'json', md: 'md' }
47
47
  const DATA_IMAGE = /^data:image\/(png|jpe?g|webp|gif|avif)(;[a-z0-9=._-]+)*(;base64)?,/i
package/lib/handoff.js CHANGED
@@ -677,7 +677,7 @@ export function localOnly(u) {
677
677
 
678
678
  /* eslint-disable no-undef */
679
679
  /** 在空白页里比两张无损截图:逐像素,每个点三个通道差加起来超过 24 就算不一样 → 差异比例、差得多的几块、拼图 */
680
- async function diffIn(a, b, labels) {
680
+ export async function diffIn(a, b, labels) {
681
681
  const load = (src) => new Promise((res, rej) => { const i = new Image(); i.onload = () => res(i); i.onerror = () => rej(new Error('图片载不出来')); i.src = 'data:image/png;base64,' + src })
682
682
  const [A, B] = await Promise.all([load(a), load(b)])
683
683
  const w = A.width, h = A.height
@@ -693,19 +693,21 @@ async function diffIn(a, b, labels) {
693
693
  const CELL = 16
694
694
  const cw = Math.ceil(w / CELL), ch = Math.ceil(h / CELL)
695
695
  const cells = new Float32Array(cw * ch)
696
- const heat = g.createImageData(w, h)
696
+ // 差异照样逐点算(1px 的线也不能漏);热力图大图按 st×st 一格画(#21:省掉一张全尺寸的图),格子里有一点不一样就标红
697
+ const st = Math.max(1, Math.round(Math.sqrt((w * h) / 1e6)))
698
+ const hw = Math.ceil(w / st), hh = Math.ceil(h / st)
699
+ const heat = g.createImageData(hw, hh)
697
700
  let bad = 0
698
701
  for (let y = 0; y < h; y++) {
702
+ const hy = (y / st) | 0
699
703
  for (let x = 0; x < w; x++) {
700
- const i = (y * w + x) * 4
704
+ const i = (y * w + x) * 4, j = (hy * hw + ((x / st) | 0)) * 4
701
705
  const d = Math.abs(da[i] - db[i]) + Math.abs(da[i + 1] - db[i + 1]) + Math.abs(da[i + 2] - db[i + 2])
702
706
  const hit = d > 24
703
707
  const l = (da[i] * 0.3 + da[i + 1] * 0.59 + da[i + 2] * 0.11) * 0.3 + 175
704
708
  if (hit) { bad++; cells[((y / CELL) | 0) * cw + ((x / CELL) | 0)]++ }
705
- heat.data[i] = hit ? 232 : l
706
- heat.data[i + 1] = hit ? 56 : l
707
- heat.data[i + 2] = hit ? 48 : l
708
- heat.data[i + 3] = 255
709
+ if (hit) { heat.data[j] = 232; heat.data[j + 1] = 56; heat.data[j + 2] = 48; heat.data[j + 3] = 255 }
710
+ else if (y % st === 0 && x % st === 0 && !heat.data[j + 3]) { heat.data[j] = l; heat.data[j + 1] = l; heat.data[j + 2] = l; heat.data[j + 3] = 255 }
709
711
  }
710
712
  }
711
713
  // 差得多的格子连成块,按差的像素数排
@@ -743,8 +745,8 @@ async function diffIn(a, b, labels) {
743
745
  o.fillStyle = '#eef0f3'
744
746
  o.fillRect(0, 0, w * 3 + PAD * 4, h + HEAD + PAD)
745
747
  const hc = document.createElement('canvas')
746
- hc.width = w
747
- hc.height = h
748
+ hc.width = hw
749
+ hc.height = hh
748
750
  hc.getContext('2d').putImageData(heat, 0, 0)
749
751
  const pct = (bad / (w * h)) * 100
750
752
  ;[[A, labels[0]], [B, labels[1]], [hc, labels[2].replace('{pct}', pct.toFixed(1))]].forEach(([img, label], k) => {
@@ -752,7 +754,7 @@ async function diffIn(a, b, labels) {
752
754
  o.fillStyle = '#1f2329'
753
755
  o.font = '600 22px -apple-system, "PingFang SC", "Microsoft YaHei", sans-serif'
754
756
  o.fillText(label, x, 30)
755
- o.drawImage(img, x, HEAD)
757
+ o.drawImage(img, x, HEAD, w, h) // 热力图是取样大小、做出来的截图可能不一样大:都画成设计稿那么大
756
758
  o.strokeStyle = 'rgba(0,0,0,.12)'
757
759
  o.lineWidth = 1
758
760
  o.strokeRect(x - 0.5, HEAD - 0.5, w + 1, h + 1)
@@ -848,8 +850,3 @@ export async function compareScreens(browser, { design, impl, size, at = 'top',
848
850
  } else lines.push('没有成片的差异。')
849
851
  return { pct: r.pct, boxes, jpg: Buffer.from(r.jpg, 'base64'), text: lines.join('\n') }
850
852
  }
851
-
852
- /** 设计目录里的几屏(交接、对比时按文件名找) */
853
- export function screensOf(root) {
854
- return allScreens(root)
855
- }
package/lib/index.js CHANGED
@@ -612,7 +612,7 @@ export const styleTool = {
612
612
  '用户点了风格卡(消息里有〔风格〕)、自己定了具体的风格或配色、或有默认的品牌设计系统时不用调;只说「你定」「帮我推荐一个」「出几个风格挑」算没定,要调。',
613
613
  parameters: specToJsonSchema({
614
614
  subject: { type: 'string', required: true, description: '这次要做什么,直接写用户的原话' },
615
- kind: { type: 'string', enum: ['web', 'app', 'miniprogram', 'tablet', 'desktop', 'poster', 'slides', 'email', 'diagram', 'dshplugin'], description: '设计类型' },
615
+ kind: { type: 'string', enum: KINDS, description: '设计类型' },
616
616
  count: { type: 'number', description: '要几个方向,1–3,默认 1;要先出三个方向时填 3,每个方向一张不同的卡' },
617
617
  }),
618
618
  output: {
package/lib/kinds.js ADDED
@@ -0,0 +1,11 @@
1
+ // 设计类型的唯一一份表(服务端用):加新类型改这里,core / look / gallery/policy 都从这里取。
2
+ // client.js 是单独打包进页面的,有自己的一份,靠 test-client 里的对照测试看住。
3
+
4
+ /** 设计类型和默认画幅(宽, 高) */
5
+ export const KIND_SIZE = { web: [1280, 800], app: [390, 844], miniprogram: [375, 812], tablet: [820, 1180], desktop: [1280, 800], poster: [1080, 1350], slides: [1280, 720], email: [640, 960], diagram: [1600, 1000], dshplugin: [1280, 800] }
6
+
7
+ /** 全部类型(顺序 = 界面里列出的顺序) */
8
+ export const KINDS = Object.keys(KIND_SIZE)
9
+
10
+ /** 页面 <meta name="dsh-design"> 里认的类型别名(比 core 的 parseKind 少,故意不合并) */
11
+ export const META_ALIAS = { phone: 'app', mobile: 'app', ios: 'app', android: 'app', mini: 'miniprogram', miniapp: 'miniprogram', weapp: 'miniprogram', wechat: 'miniprogram', 小程序: 'miniprogram', pad: 'tablet', ipad: 'tablet', pc: 'desktop', electron: 'desktop', tauri: 'desktop', mac: 'desktop', macos: 'desktop', windows: 'desktop', win: 'desktop', 桌面: 'desktop', 桌面软件: 'desktop', social: 'poster', image: 'poster', slide: 'slides', deck: 'slides', ppt: 'slides', mail: 'email', mindmap: 'diagram', roadmap: 'diagram', architecture: 'diagram', flowchart: 'diagram', 思维导图: 'diagram', 导图: 'diagram', 路线图: 'diagram', 架构图: 'diagram', 图表: 'diagram', dsh: 'dshplugin', 'dsh-plugin': 'dshplugin', dsh插件: 'dshplugin' }
package/lib/look.js CHANGED
@@ -8,10 +8,20 @@
8
8
 
9
9
  import fs from 'node:fs'
10
10
  import path from 'node:path'
11
+ import { KIND_SIZE, KINDS as KIND_LIST, META_ALIAS } from './kinds.js'
12
+ export { KIND_SIZE }
13
+
14
+ // 体检一次最多几个页面任务一起跑(#21:12 屏 × 4 尺寸一下全铺开会挤满事件循环;浏览器那边本来也只开 3 个页面)
15
+ const LOOK_PAR = 4
16
+ /** 最多 n 个一起跑,结果按原来的顺序 */
17
+ async function mapLimit(list, n, fn) {
18
+ const out = new Array(list.length)
19
+ let next = 0
20
+ const run = async () => { while (next < list.length) { const i = next++; out[i] = await fn(list[i], i) } }
21
+ await Promise.all(Array.from({ length: Math.min(n, list.length) }, run))
22
+ return out
23
+ }
11
24
 
12
- export const KIND_SIZE = { web: [1280, 800], app: [390, 844], miniprogram: [375, 812], tablet: [820, 1180], desktop: [1280, 800], poster: [1080, 1350], slides: [1280, 720], email: [640, 960], diagram: [1600, 1000], dshplugin: [1280, 800] }
13
- const KIND_LIST = Object.keys(KIND_SIZE)
14
- const META_ALIAS = { phone: 'app', mobile: 'app', ios: 'app', android: 'app', mini: 'miniprogram', miniapp: 'miniprogram', weapp: 'miniprogram', wechat: 'miniprogram', 小程序: 'miniprogram', pad: 'tablet', ipad: 'tablet', pc: 'desktop', electron: 'desktop', tauri: 'desktop', mac: 'desktop', macos: 'desktop', windows: 'desktop', win: 'desktop', 桌面: 'desktop', 桌面软件: 'desktop', social: 'poster', image: 'poster', slide: 'slides', deck: 'slides', ppt: 'slides', mail: 'email', mindmap: 'diagram', roadmap: 'diagram', architecture: 'diagram', flowchart: 'diagram', 思维导图: 'diagram', 导图: 'diagram', 路线图: 'diagram', 架构图: 'diagram', 图表: 'diagram', dsh: 'dshplugin', 'dsh-plugin': 'dshplugin', dsh插件: 'dshplugin' }
15
25
  // 只给宽度时的高:常见真机的屏幕高;别的宽度按这一类默认的宽高比
16
26
  const PHONE_H = { 320: 568, 360: 780, 375: 667, 390: 844, 393: 852, 402: 874, 412: 915, 414: 896, 428: 926, 430: 932, 440: 956 }
17
27
  const TABLET_H = { 744: 1133, 768: 1024, 810: 1080, 820: 1180, 834: 1194, 1024: 1366, 1032: 1376 }
@@ -135,7 +145,7 @@ export async function prepare(page, dark) {
135
145
  /** 没写尺寸的幻灯片 / 海报:开页面量一下,比默认画幅大就按页面实际大小(和右侧栏的画幅一样)。量不到就还是默认 */
136
146
  export async function fitSize(browser, screens, kind, size) {
137
147
  const [w, h] = size
138
- const seen = await Promise.all(screens.map((sc) => browser.withPage({ url: sc.url, w, h }, async (page) => (await prepare(page, false)).info.natural).catch(() => null)))
148
+ const seen = await mapLimit(screens, LOOK_PAR, (sc) => browser.withPage({ url: sc.url, w, h }, async (page) => (await prepare(page, false)).info.natural).catch(() => null))
139
149
  return sizeOf(autoSize(seen, kind), kind) || size
140
150
  }
141
151
 
@@ -163,7 +173,7 @@ export async function measureAll(browser, { screens, sizes, kind, dark, fetcher
163
173
  }
164
174
  return gone.get(url)
165
175
  }
166
- const rows = await Promise.all(jobs.map(async ({ sc, w, h }, i) => {
176
+ const rows = await mapLimit(jobs, LOOK_PAR, async ({ sc, w, h }, i) => {
167
177
  const row = { key: sc.key, label: sc.label || sc.key, w, h, issues: [], metrics: null, error: '' }
168
178
  try {
169
179
  await browser.withPage({ url: sc.url, w, h, dark }, async (page) => {
@@ -193,7 +203,7 @@ export async function measureAll(browser, { screens, sizes, kind, dark, fetcher
193
203
  row.error = String((e && e.message) || e).slice(0, 160)
194
204
  }
195
205
  return row
196
- }))
206
+ })
197
207
  // 规则 → 文件和行号(一次问完)
198
208
  if (locate) {
199
209
  const withRule = rows.flatMap((r) => r.issues.filter((it) => it.rule && it.rule.sel))
@@ -232,7 +242,7 @@ export async function shootAll(browser, { screens, size, at, selector, dark, kin
232
242
  const scale = scaleFor(w, h)
233
243
  const out = []
234
244
  const perScreen = at === 'full' ? Math.max(1, Math.floor(max / Math.max(1, screens.length))) : 1
235
- await Promise.all(screens.map(async (sc, si) => {
245
+ await mapLimit(screens, LOOK_PAR, async (sc, si) => {
236
246
  const shots = []
237
247
  const base = { key: sc.key, label: sc.label || sc.key, w, h }
238
248
  try {
@@ -282,7 +292,7 @@ export async function shootAll(browser, { screens, size, at, selector, dark, kin
282
292
  shots.push({ ...base, at, error: String((e && e.message) || e).slice(0, 160) })
283
293
  }
284
294
  out[si] = shots
285
- }))
295
+ })
286
296
  return out.flat().filter(Boolean)
287
297
  }
288
298
 
package/lib/system.js CHANGED
@@ -533,7 +533,6 @@ function hinted(vars) {
533
533
  return out
534
534
  }
535
535
 
536
- const roundTo = (v, step = 1) => Math.round(v / step) * step
537
536
  const familyOf = (ff) => String(ff || '').split(',')[0].trim().replace(/^["']|["']$/g, '')
538
537
  const isSerif = (ff) => /(^|,)\s*serif\s*$/i.test(String(ff || '')) || /serif|song|ming|georgia|times|garamond|playfair|lora|merriweather|noto serif/i.test(familyOf(ff))
539
538
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-design-studio",
3
- "version": "0.22.0",
3
+ "version": "0.23.1",
4
4
  "type": "module",
5
5
  "scripts": {
6
6
  "test": "node --test --test-concurrency=1 test/*.mjs"