dsh-design-studio 0.15.0 → 0.16.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/picker.js CHANGED
@@ -681,12 +681,53 @@
681
681
  return r.bottom > 0 && r.right > 0 && r.top < innerHeight && r.left < innerWidth
682
682
  }
683
683
 
684
- /** 闪一下;改的地方都不在屏幕里就先滚过去 */
684
+ // 为了闪改动自动滚走之前的位置(页面和外面几层滚动区)。闪完滚回去,不然预览一直停在那儿,「一屏」类导出截的就是被滚走的画面。
685
+ // 用户自己动了页面就作废:滚到哪算哪
686
+ let scrollHome = null
687
+ let scrollHomeTimer = 0
688
+ const FLASH_MS = 420 + 2900
689
+ const RETURN_MS = 1000 // 平滑滚回去最多要这么久;这段时间里截图、测量还能立刻跳回原位
690
+ const dropScrollHome = () => { clearTimeout(scrollHomeTimer); scrollHome = null }
691
+ for (const ev of ['wheel', 'touchmove', 'pointerdown']) addEventListener(ev, () => { if (scrollHome) dropScrollHome() }, { capture: true, passive: true })
692
+ addEventListener('keydown', (e) => { if (scrollHome && /^(Arrow|Page|Home$|End$| $|Tab$)/.test(e.key)) dropScrollHome() }, true)
693
+ const scrollersOf = (el) => {
694
+ const out = []
695
+ for (let a = el.parentElement; a && a !== document.documentElement; a = a.parentElement) {
696
+ if (a !== document.scrollingElement && (a.scrollHeight > a.clientHeight + 1 || a.scrollWidth > a.clientWidth + 1)) out.push({ el: a, x: a.scrollLeft, y: a.scrollTop })
697
+ }
698
+ return out
699
+ }
700
+ /** 滚回自动滚走之前的位置(导出前不等动画) */
701
+ function goScrollHome(behavior) {
702
+ const h = scrollHome
703
+ if (!h) return
704
+ clearTimeout(scrollHomeTimer)
705
+ if (behavior === 'smooth') scrollHomeTimer = setTimeout(dropScrollHome, RETURN_MS)
706
+ else scrollHome = null
707
+ try { scrollTo({ left: h.x, top: h.y, behavior }) } catch { scrollTo(h.x, h.y) }
708
+ for (const s of h.inner) { try { s.el.scrollTo({ left: s.x, top: s.y, behavior }) } catch { /* 元素没了 */ } }
709
+ }
710
+
711
+ /** 用户要在现在这个位置上操作(画笔标注):别滚回去,正在滚回去也停下 */
712
+ function holdScroll() {
713
+ const h = scrollHome
714
+ if (!h) return
715
+ dropScrollHome()
716
+ try { scrollTo({ left: scrollX, top: scrollY, behavior: 'instant' }) } catch { /* 停不住就算了 */ }
717
+ for (const s of h.inner) { try { s.el.scrollTo({ left: s.el.scrollLeft, top: s.el.scrollTop, behavior: 'instant' }) } catch { /* 元素没了 */ } }
718
+ }
719
+
720
+ /** 闪一下;改的地方都不在屏幕里就先滚过去,闪完滚回来。窗口在后台时没人看,不滚 */
685
721
  function play(els) {
686
722
  if (!els.length) return
687
- if (els.some(inView)) return flash(els)
723
+ if (els.some(inView) || document.hidden) return flash(els)
724
+ const inner = scrollersOf(els[0])
725
+ if (!scrollHome) scrollHome = { x: scrollX, y: scrollY, inner }
726
+ else for (const s of inner) if (!scrollHome.inner.some((o) => o.el === s.el)) scrollHome.inner.push(s)
688
727
  try { els[0].scrollIntoView({ behavior: 'smooth', block: 'center' }) } catch { els[0].scrollIntoView() }
689
728
  setTimeout(() => flash(els), 420)
729
+ clearTimeout(scrollHomeTimer)
730
+ scrollHomeTimer = setTimeout(() => goScrollHome('smooth'), FLASH_MS)
690
731
  }
691
732
 
692
733
  // ── 生成动画:元素按从上到下的顺序浮现,扫描光带一起往下走 ─────────────────
@@ -991,7 +1032,7 @@
991
1032
  let pending = null
992
1033
  let busy = false
993
1034
 
994
- const seedNow = () => ({ href: here(), scroll: { x: scrollX, y: scrollY }, sig: snapshot().map })
1035
+ const seedNow = () => ({ href: here(), scroll: scrollHome ? { x: scrollHome.x, y: scrollHome.y } : { x: scrollX, y: scrollY }, sig: snapshot().map })
995
1036
 
996
1037
  /** 选择器列表按顶层逗号拆开(括号、方括号里的逗号不算) */
997
1038
  function splitSelectors(text) {
@@ -1686,7 +1727,16 @@ background:rgba(20,20,22,.86);color:#e8e8ea;font:12px/18px -apple-system,system-
1686
1727
 
1687
1728
  /** 页面里跳到别的屏的地方:路径 + 在视口里的位置(画布上从这里画一条线到那一屏)
1688
1729
  * shown:只要这会儿露在外面的(画布连线用;导出、交接要全部) */
1689
- function linksOf(shown) {
1730
+ /** 这个元素跟着哪条挪到底部的栏走:往上找,先碰到别的 fixed 就是它自己定位,不跟栏走 */
1731
+ function barDyOf(el, bars) {
1732
+ for (let a = el; a && a.nodeType === 1; a = a.parentElement) {
1733
+ if (bars.has(a)) return bars.get(a)
1734
+ if (getComputedStyle(a).position === 'fixed') return undefined
1735
+ }
1736
+ return undefined
1737
+ }
1738
+
1739
+ function linksOf(shown, bars) {
1690
1740
  const out = []
1691
1741
  const seen = new Set()
1692
1742
  for (const el of document.querySelectorAll(LINK_SEL)) {
@@ -1707,7 +1757,8 @@ background:rgba(20,20,22,.86);color:#e8e8ea;font:12px/18px -apple-system,system-
1707
1757
  if (seen.has(k)) continue
1708
1758
  seen.add(k)
1709
1759
  const text = clip(String(el.innerText || el.getAttribute('aria-label') || el.getAttribute('title') || '').replace(/\s+/g, ' ').trim(), 24)
1710
- out.push({ path: p, x: Math.round(r.left), y: Math.round(r.top), w: Math.round(r.width), h: Math.round(r.height), text })
1760
+ const barDy = bars && bars.size ? barDyOf(el, bars) : undefined
1761
+ out.push({ path: p, x: Math.round(r.left), y: Math.round(r.top), w: Math.round(r.width), h: Math.round(r.height), text, ...(barDy === undefined ? null : { barDy }) })
1711
1762
  }
1712
1763
  return out
1713
1764
  }
@@ -2029,10 +2080,12 @@ background:rgba(20,20,22,.86);color:#e8e8ea;font:12px/18px -apple-system,system-
2029
2080
  }
2030
2081
 
2031
2082
  async function capture(opts) {
2083
+ goScrollHome('instant') // 正在为闪改动滚走:按原来的位置截
2032
2084
  const full = !!opts.full
2033
2085
  const scale = Math.min(3, Math.max(1, Number(opts.scale) || 2))
2034
2086
  const de = document.documentElement
2035
2087
  const W = Math.ceil(de.clientWidth || innerWidth)
2088
+ const bars = new Map() // 整页图里挪到底部的栏 → 往下挪了多少
2036
2089
  const H = Math.ceil(full ? Math.min(Number(opts.maxH) || Infinity, Math.max(de.scrollHeight, document.body ? document.body.scrollHeight : 0, innerHeight)) : innerHeight) // maxH:指定高度,别先画一张超长的图
2037
2090
  const box = makeSandbox()
2038
2091
  const inline = makeInliner()
@@ -2144,6 +2197,15 @@ background:rgba(20,20,22,.86);color:#e8e8ea;font:12px/18px -apple-system,system-
2144
2197
  const y = parseFloat(cs.top)
2145
2198
  if (Number.isFinite(x) && Number.isFinite(y)) st += `left:${x - bodyAt.l}px;top:${y - bodyAt.t}px;right:auto;bottom:auto;`
2146
2199
  }
2200
+ // 整页:贴着窗口底部的固定元素(标签栏、操作条、悬浮按钮)放到整张图的底部。内联的 top 是按窗口算好的像素,上下都写死时 top 说了算,不挪就钉在第一屏底部、盖住中间的内容
2201
+ if (full && H > innerHeight && cs.position === 'fixed' && el.namespaceURI !== SVGNS && el.ownerDocument === document && document.body.contains(el) && !fixedHeld(el)) {
2202
+ const r = el.getBoundingClientRect()
2203
+ const y = parseFloat(cs.top)
2204
+ if (Number.isFinite(y) && r.height > 0 && r.height < innerHeight / 2 && r.top >= innerHeight / 2 && r.bottom <= innerHeight + 1 && bottomPinned(el)) {
2205
+ st += `top:${y + H - innerHeight}px;bottom:auto;`
2206
+ bars.set(el, H - innerHeight)
2207
+ }
2208
+ }
2147
2209
  // 原来一行放得下的字,画成图时也别折行(宽度是定死的,字体渲染差一丁点就会挤到第二行)
2148
2210
  if (el.namespaceURI !== SVGNS && cs.whiteSpace !== 'nowrap' && oneLine(el)) st += 'white-space:nowrap;'
2149
2211
  c.setAttribute('style', st)
@@ -2349,7 +2411,7 @@ background:rgba(20,20,22,.86);color:#e8e8ea;font:12px/18px -apple-system,system-
2349
2411
  ctx.fillRect(0, 0, canvas.width, canvas.height)
2350
2412
  ctx.drawImage(img, 0, 0, canvas.width, canvas.height)
2351
2413
  const png = await new Promise((resolve, reject) => canvas.toBlob((b) => (b ? resolve(b) : reject(new Error('toBlob 失败'))), 'image/png'))
2352
- return { png, w: W, h: H, scale, top }
2414
+ return { png, w: W, h: H, scale, top, bars }
2353
2415
  } finally {
2354
2416
  box.remove()
2355
2417
  }
@@ -2384,6 +2446,16 @@ background:rgba(20,20,22,.86);color:#e8e8ea;font:12px/18px -apple-system,system-
2384
2446
  }
2385
2447
 
2386
2448
  /** 固定元素是不是被某个祖先(transform、filter 之类)接管了位置:接管了就跟着那个祖先走,不用挪 */
2449
+ /** 是靠 bottom 贴着窗口底部的(标签栏、悬浮按钮),不是写了 top 碰巧在下半屏的浮层。认不出来就当不是,留在原位 */
2450
+ function bottomPinned(el) {
2451
+ try {
2452
+ const m = el.computedStyleMap()
2453
+ return String(m.get('top')) === 'auto' && String(m.get('bottom')) !== 'auto'
2454
+ } catch {
2455
+ return false
2456
+ }
2457
+ }
2458
+
2387
2459
  function fixedHeld(el) {
2388
2460
  for (let a = el.parentElement; a; a = a.parentElement) if (holds(getComputedStyle(a), true)) return true
2389
2461
  return false
@@ -2501,13 +2573,33 @@ background:rgba(20,20,22,.86);color:#e8e8ea;font:12px/18px -apple-system,system-
2501
2573
  return k
2502
2574
  }
2503
2575
 
2576
+ /** 这一屏用了哪些自带照片库的 AI 示意图(<img>、背景图、伪元素背景都算,按文件名):导出时提醒上线前换真照片 */
2577
+ function aiPhotosNow() {
2578
+ const seen = new Set()
2579
+ const add = (u) => { for (const m of String(u || '').matchAll(/ai-photo-[a-z0-9-]+\.jpg/g)) seen.add(m[0]) }
2580
+ for (const im of document.images) add(im.currentSrc || im.src)
2581
+ for (const im of document.querySelectorAll('image')) add(im.getAttribute('href') || im.getAttribute('xlink:href'))
2582
+ // 打包预览时图片已经内联成 data:,名字记在这个 meta 里
2583
+ const noted = document.querySelector('meta[name="ddp-ai-photos"]')
2584
+ if (noted) add(noted.content)
2585
+ for (const el of [...document.querySelectorAll('*')].slice(0, 4000)) {
2586
+ for (const pseudo of [null, '::before', '::after']) {
2587
+ const cs = getComputedStyle(el, pseudo)
2588
+ if (cs.backgroundImage && cs.backgroundImage !== 'none') add(cs.backgroundImage)
2589
+ if (pseudo && cs.content && cs.content !== 'none' && cs.content !== 'normal') add(cs.content)
2590
+ }
2591
+ }
2592
+ return [...seen]
2593
+ }
2594
+
2504
2595
  async function onCapture(d) {
2505
2596
  try {
2506
2597
  const r = await capture(d)
2507
2598
  // 整页图从文档顶上画起,链接加上滚动距离;只截视口时就是视口里的位置
2508
- const links = linksOf().map((l) => (d.full ? { ...l, x: l.x + scrollX, y: l.y + scrollY } : l))
2599
+ // 挪到整张图底部的栏:里面的链接跟着栏走,它本来就不随页面滚
2600
+ const links = linksOf(false, r.bars).map(({ barDy, ...l }) => (!d.full ? l : barDy === undefined ? { ...l, x: l.x + scrollX, y: l.y + scrollY } : { ...l, y: l.y + barDy }))
2509
2601
  .filter((l) => l.x < r.w && l.y < r.h && l.x + l.w > 0 && l.y + l.h > 0)
2510
- post({ type: 'captured', id: d.id, png: r.png, w: r.w, h: r.h, scale: r.scale, title: document.title, links, top: r.top, href: here() })
2602
+ post({ type: 'captured', id: d.id, png: r.png, w: r.w, h: r.h, scale: r.scale, title: document.title, links, top: r.top, href: here(), aiPhotos: aiPhotosNow() })
2511
2603
  } catch (e) {
2512
2604
  post({ type: 'captured', id: d.id, error: String((e && e.message) || e) })
2513
2605
  }
@@ -2515,6 +2607,7 @@ background:rgba(20,20,22,.86);color:#e8e8ea;font:12px/18px -apple-system,system-
2515
2607
 
2516
2608
  // 导出前预检:这一页实际多大、有几个点了没反应的链接(href="#"、指向不存在的文件)
2517
2609
  async function onMeasure(d) {
2610
+ goScrollHome('instant') // 正在为闪改动滚走:按原来的位置量
2518
2611
  try {
2519
2612
  if (document.readyState !== 'complete') await new Promise((r) => { addEventListener('load', r, { once: true }); setTimeout(r, 1500) })
2520
2613
  const de = document.documentElement
@@ -2537,7 +2630,7 @@ background:rgba(20,20,22,.86);color:#e8e8ea;font:12px/18px -apple-system,system-
2537
2630
  } catch { /* 查不了就不算 */ }
2538
2631
  }))
2539
2632
  for (const l of found) if (gone.get(l.path)) dead.push(l.y + scrollY)
2540
- post({ type: 'measured', id: d.id, sy: scrollY, w: de.clientWidth || innerWidth, h: innerHeight, pageW: Math.max(de.scrollWidth, b ? b.scrollWidth : 0), pageH: Math.max(de.scrollHeight, b ? b.scrollHeight : 0), natural: naturalSize(), dead })
2633
+ post({ type: 'measured', id: d.id, sy: scrollY, aiPhotos: aiPhotosNow(), w: de.clientWidth || innerWidth, h: innerHeight, pageW: Math.max(de.scrollWidth, b ? b.scrollWidth : 0), pageH: Math.max(de.scrollHeight, b ? b.scrollHeight : 0), natural: naturalSize(), dead })
2541
2634
  } catch (e) {
2542
2635
  post({ type: 'measured', id: d.id, error: String((e && e.message) || e) })
2543
2636
  }
@@ -4126,6 +4219,7 @@ background:rgba(20,20,22,.86);color:#e8e8ea;font:12px/18px -apple-system,system-
4126
4219
  async function onCheck(d) {
4127
4220
  let r
4128
4221
  try {
4222
+ goScrollHome('instant') // 量的时候要滚动,别把闪改动的回位打断、停在半路
4129
4223
  await settle()
4130
4224
  r = runCheck(String(d.kind || 'web'))
4131
4225
  } catch (e) {
@@ -4243,12 +4337,13 @@ background:rgba(20,20,22,.86);color:#e8e8ea;font:12px/18px -apple-system,system-
4243
4337
  else if (d.type === 'snapshot') post({ type: 'snap', seed: seedNow() })
4244
4338
  else if (d.type === 'busy') { busy = !!d.on; watchHmr() }
4245
4339
  else if (d.type === 'unpin') unpin()
4340
+ else if (d.type === 'holdScroll') holdScroll()
4246
4341
  else if (d.type === 'reload') selfReload()
4247
4342
  else if (d.type === 'capture') onCapture(d)
4248
4343
  else if (d.type === 'measure') onMeasure(d)
4249
4344
  else if (d.type === 'check') onCheck(d)
4250
4345
  else if (d.type === 'checkClear') { clearMarks(); lastIssues = [] }
4251
- else if (d.type === 'checkFocus') focusIssue(Number(d.i))
4346
+ else if (d.type === 'checkFocus') { holdScroll(); focusIssue(Number(d.i)) } // 用户点了别的地方要看,别再滚回去
4252
4347
  else if (d.type === 'theme') { setDark(d.dark); postTop(); setTimeout(postTop, 400) }
4253
4348
  else if (d.type === 'demo') demo = !!d.on
4254
4349
  else if (d.type === 'edit') setTextEdit(!!d.on, d.hint !== false)
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 挂给模型。
2
+ * dsh-design-studio/tool:把 preview_page、check_design、look_design、extract_design_system、handoff_design、compare_design、find_photos 挂给模型。
3
3
  * 放在 bundle 顶层 → 所有模式都能用;放进设计模式 → 只挂在那个模式里。工具本身见 index.js
4
4
  */
5
- import { previewTool, checkTool, lookTool, systemTool, handoffTool, compareTool } from './index.js'
5
+ import { previewTool, checkTool, lookTool, systemTool, handoffTool, compareTool, photoTool } from './index.js'
6
6
 
7
7
  export const name = 'dsh-design-studio-tool'
8
8
  export const inject = ['tools']
@@ -15,4 +15,5 @@ export function apply(ctx) {
15
15
  ctx.tools.register({ ...systemTool, execute: (args, exec) => systemTool.execute(args, exec, 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
+ ctx.tools.register({ ...photoTool })
18
19
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-design-studio",
3
- "version": "0.15.0",
3
+ "version": "0.16.1",
4
4
  "type": "module",
5
5
  "scripts": {
6
6
  "test": "node --test --test-concurrency=1 test/*.mjs"
@@ -126,7 +126,7 @@
126
126
 
127
127
  **界面语言跟着用户走**:用户用什么语言提需求,界面文案就用什么语言(英文提问做英文界面,中文提问做中文界面);用户点名了语言或市场(「给日本用户」「做英文版」)就照他说的。货币、日期格式、人名地名也跟着那个市场。跟用户说话仍用他的语言。
128
128
 
129
- **图片**:图片位置用 SVG 插画、几何构图或带色块的占位,不引用外网图片(预览离线也要好看)。
129
+ **图片**:用户给了照片就用用户的。靠照片说话的设计(民宿酒店、餐饮、奶茶咖啡、健身、美妆、服饰、教育、旅行风景)需要头图、封面、商品图时,调 `find_photos` 从自带照片库拿,一次要够这几屏用的,别画插画冒充照片;库里没有合适的,或者题材本身不靠照片(工具、后台、数据类),才用 SVG 插画、几何构图或色块。不引用外网图片(预览离线也要好看)。库里的是 AI 生成的示意图:不写「实拍」,做完告诉用户上线前换成真实照片。
130
130
 
131
131
  **图标**:用一套线性 SVG 图标(同样的线宽 1.75–2、圆角端点、24 网格),内联写进 HTML。不要用 emoji 当图标。
132
132