dsh-design-studio 0.16.0 → 0.17.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 +3 -1
- package/README.md +3 -1
- package/cordis.patch.yml +4 -0
- package/lib/annotations-row.js +14 -0
- package/lib/annotations.js +214 -0
- package/lib/browser.js +14 -3
- package/lib/client.js +353 -40
- package/lib/core.js +9 -2
- package/lib/handoff.js +16 -4
- package/lib/index.js +81 -10
- package/lib/look.js +19 -7
- package/lib/photos.js +9 -0
- package/lib/picker.js +337 -8
- package/lib/serve.js +3 -2
- package/lib/styles.js +203 -0
- package/lib/tool.js +3 -2
- package/locale/annotations/en.json +6 -0
- package/locale/annotations/zh.json +6 -0
- package/package.json +4 -1
- package/skills/ui-design/SKILL.md +5 -3
- package/skills/ui-design/references/annotations.md +64 -0
package/lib/picker.js
CHANGED
|
@@ -318,6 +318,7 @@
|
|
|
318
318
|
if (fx) fx.host.style.setProperty('--k', K)
|
|
319
319
|
if (editUi) editUi.host.style.setProperty('--k', K)
|
|
320
320
|
placePin()
|
|
321
|
+
if (anMarks.length) placeAnnots()
|
|
321
322
|
}
|
|
322
323
|
|
|
323
324
|
function mount(hint) {
|
|
@@ -593,6 +594,14 @@
|
|
|
593
594
|
transform:scale(var(--k, 1));transform-origin:0 0;box-shadow:0 2px 6px rgba(0,0,0,.25)}
|
|
594
595
|
.ck.hi b{background:#e5484d}
|
|
595
596
|
.ck.on{animation:ck-on 1.2s ease-out}
|
|
597
|
+
.an{position:absolute;pointer-events:none;border-radius:4px}
|
|
598
|
+
.an.sel{outline:calc(2px * var(--k, 1)) solid #4d6bfe;outline-offset:calc(1px * var(--k, 1));background:rgba(77,107,254,.07)}
|
|
599
|
+
.an b{position:absolute;left:calc((var(--i, 0) * 21px - 9px) * var(--k, 1));top:calc(-9px * var(--k, 1));min-width:18px;height:18px;padding:0 4px;box-sizing:border-box;border-radius:9px;
|
|
600
|
+
border:1.5px solid #4d6bfe;background:#fff;color:#4d6bfe;font:700 11px/15px -apple-system,BlinkMacSystemFont,system-ui,sans-serif;text-align:center;
|
|
601
|
+
transform:scale(var(--k, 1));transform-origin:0 0;box-shadow:0 2px 6px rgba(0,0,0,.25);pointer-events:auto;cursor:pointer}
|
|
602
|
+
.an.sel b{background:#4d6bfe;color:#fff}
|
|
603
|
+
.an.fl{animation:an-fl 1.2s ease-out}
|
|
604
|
+
@keyframes an-fl{0%,40%{box-shadow:0 0 0 calc(6px * var(--k, 1)) rgba(77,107,254,.35)}100%{box-shadow:0 0 0 0 rgba(77,107,254,0)}}
|
|
596
605
|
.cut{position:absolute;pointer-events:none;border:calc(2px * var(--k, 1)) solid #e5484d;border-radius:4px;box-shadow:0 0 0 1px rgba(255,255,255,.85)}
|
|
597
606
|
.cut.alt{border-color:#f59e0b;border-style:dashed}
|
|
598
607
|
.cut b{position:absolute;left:calc(-2px * var(--k, 1));bottom:100%;margin-bottom:calc(3px * var(--k, 1));padding:0 6px;border-radius:4px;background:#e5484d;color:#fff;
|
|
@@ -681,12 +690,53 @@
|
|
|
681
690
|
return r.bottom > 0 && r.right > 0 && r.top < innerHeight && r.left < innerWidth
|
|
682
691
|
}
|
|
683
692
|
|
|
684
|
-
|
|
693
|
+
// 为了闪改动自动滚走之前的位置(页面和外面几层滚动区)。闪完滚回去,不然预览一直停在那儿,「一屏」类导出截的就是被滚走的画面。
|
|
694
|
+
// 用户自己动了页面就作废:滚到哪算哪
|
|
695
|
+
let scrollHome = null
|
|
696
|
+
let scrollHomeTimer = 0
|
|
697
|
+
const FLASH_MS = 420 + 2900
|
|
698
|
+
const RETURN_MS = 1000 // 平滑滚回去最多要这么久;这段时间里截图、测量还能立刻跳回原位
|
|
699
|
+
const dropScrollHome = () => { clearTimeout(scrollHomeTimer); scrollHome = null }
|
|
700
|
+
for (const ev of ['wheel', 'touchmove', 'pointerdown']) addEventListener(ev, () => { if (scrollHome) dropScrollHome() }, { capture: true, passive: true })
|
|
701
|
+
addEventListener('keydown', (e) => { if (scrollHome && /^(Arrow|Page|Home$|End$| $|Tab$)/.test(e.key)) dropScrollHome() }, true)
|
|
702
|
+
const scrollersOf = (el) => {
|
|
703
|
+
const out = []
|
|
704
|
+
for (let a = el.parentElement; a && a !== document.documentElement; a = a.parentElement) {
|
|
705
|
+
if (a !== document.scrollingElement && (a.scrollHeight > a.clientHeight + 1 || a.scrollWidth > a.clientWidth + 1)) out.push({ el: a, x: a.scrollLeft, y: a.scrollTop })
|
|
706
|
+
}
|
|
707
|
+
return out
|
|
708
|
+
}
|
|
709
|
+
/** 滚回自动滚走之前的位置(导出前不等动画) */
|
|
710
|
+
function goScrollHome(behavior) {
|
|
711
|
+
const h = scrollHome
|
|
712
|
+
if (!h) return
|
|
713
|
+
clearTimeout(scrollHomeTimer)
|
|
714
|
+
if (behavior === 'smooth') scrollHomeTimer = setTimeout(dropScrollHome, RETURN_MS)
|
|
715
|
+
else scrollHome = null
|
|
716
|
+
try { scrollTo({ left: h.x, top: h.y, behavior }) } catch { scrollTo(h.x, h.y) }
|
|
717
|
+
for (const s of h.inner) { try { s.el.scrollTo({ left: s.x, top: s.y, behavior }) } catch { /* 元素没了 */ } }
|
|
718
|
+
}
|
|
719
|
+
|
|
720
|
+
/** 用户要在现在这个位置上操作(画笔标注):别滚回去,正在滚回去也停下 */
|
|
721
|
+
function holdScroll() {
|
|
722
|
+
const h = scrollHome
|
|
723
|
+
if (!h) return
|
|
724
|
+
dropScrollHome()
|
|
725
|
+
try { scrollTo({ left: scrollX, top: scrollY, behavior: 'instant' }) } catch { /* 停不住就算了 */ }
|
|
726
|
+
for (const s of h.inner) { try { s.el.scrollTo({ left: s.el.scrollLeft, top: s.el.scrollTop, behavior: 'instant' }) } catch { /* 元素没了 */ } }
|
|
727
|
+
}
|
|
728
|
+
|
|
729
|
+
/** 闪一下;改的地方都不在屏幕里就先滚过去,闪完滚回来。窗口在后台时没人看,不滚 */
|
|
685
730
|
function play(els) {
|
|
686
731
|
if (!els.length) return
|
|
687
|
-
if (els.some(inView)) return flash(els)
|
|
732
|
+
if (els.some(inView) || document.hidden) return flash(els)
|
|
733
|
+
const inner = scrollersOf(els[0])
|
|
734
|
+
if (!scrollHome) scrollHome = { x: scrollX, y: scrollY, inner }
|
|
735
|
+
else for (const s of inner) if (!scrollHome.inner.some((o) => o.el === s.el)) scrollHome.inner.push(s)
|
|
688
736
|
try { els[0].scrollIntoView({ behavior: 'smooth', block: 'center' }) } catch { els[0].scrollIntoView() }
|
|
689
737
|
setTimeout(() => flash(els), 420)
|
|
738
|
+
clearTimeout(scrollHomeTimer)
|
|
739
|
+
scrollHomeTimer = setTimeout(() => goScrollHome('smooth'), FLASH_MS)
|
|
690
740
|
}
|
|
691
741
|
|
|
692
742
|
// ── 生成动画:元素按从上到下的顺序浮现,扫描光带一起往下走 ─────────────────
|
|
@@ -991,7 +1041,7 @@
|
|
|
991
1041
|
let pending = null
|
|
992
1042
|
let busy = false
|
|
993
1043
|
|
|
994
|
-
const seedNow = () => ({ href: here(), scroll: { x: scrollX, y: scrollY }, sig: snapshot().map })
|
|
1044
|
+
const seedNow = () => ({ href: here(), scroll: scrollHome ? { x: scrollHome.x, y: scrollHome.y } : { x: scrollX, y: scrollY }, sig: snapshot().map })
|
|
995
1045
|
|
|
996
1046
|
/** 选择器列表按顶层逗号拆开(括号、方括号里的逗号不算) */
|
|
997
1047
|
function splitSelectors(text) {
|
|
@@ -1686,7 +1736,16 @@ background:rgba(20,20,22,.86);color:#e8e8ea;font:12px/18px -apple-system,system-
|
|
|
1686
1736
|
|
|
1687
1737
|
/** 页面里跳到别的屏的地方:路径 + 在视口里的位置(画布上从这里画一条线到那一屏)
|
|
1688
1738
|
* shown:只要这会儿露在外面的(画布连线用;导出、交接要全部) */
|
|
1689
|
-
|
|
1739
|
+
/** 这个元素跟着哪条挪到底部的栏走:往上找,先碰到别的 fixed 就是它自己定位,不跟栏走 */
|
|
1740
|
+
function barDyOf(el, bars) {
|
|
1741
|
+
for (let a = el; a && a.nodeType === 1; a = a.parentElement) {
|
|
1742
|
+
if (bars.has(a)) return bars.get(a)
|
|
1743
|
+
if (getComputedStyle(a).position === 'fixed') return undefined
|
|
1744
|
+
}
|
|
1745
|
+
return undefined
|
|
1746
|
+
}
|
|
1747
|
+
|
|
1748
|
+
function linksOf(shown, bars) {
|
|
1690
1749
|
const out = []
|
|
1691
1750
|
const seen = new Set()
|
|
1692
1751
|
for (const el of document.querySelectorAll(LINK_SEL)) {
|
|
@@ -1707,7 +1766,8 @@ background:rgba(20,20,22,.86);color:#e8e8ea;font:12px/18px -apple-system,system-
|
|
|
1707
1766
|
if (seen.has(k)) continue
|
|
1708
1767
|
seen.add(k)
|
|
1709
1768
|
const text = clip(String(el.innerText || el.getAttribute('aria-label') || el.getAttribute('title') || '').replace(/\s+/g, ' ').trim(), 24)
|
|
1710
|
-
|
|
1769
|
+
const barDy = bars && bars.size ? barDyOf(el, bars) : undefined
|
|
1770
|
+
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
1771
|
}
|
|
1712
1772
|
return out
|
|
1713
1773
|
}
|
|
@@ -2029,10 +2089,12 @@ background:rgba(20,20,22,.86);color:#e8e8ea;font:12px/18px -apple-system,system-
|
|
|
2029
2089
|
}
|
|
2030
2090
|
|
|
2031
2091
|
async function capture(opts) {
|
|
2092
|
+
goScrollHome('instant') // 正在为闪改动滚走:按原来的位置截
|
|
2032
2093
|
const full = !!opts.full
|
|
2033
2094
|
const scale = Math.min(3, Math.max(1, Number(opts.scale) || 2))
|
|
2034
2095
|
const de = document.documentElement
|
|
2035
2096
|
const W = Math.ceil(de.clientWidth || innerWidth)
|
|
2097
|
+
const bars = new Map() // 整页图里挪到底部的栏 → 往下挪了多少
|
|
2036
2098
|
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
2099
|
const box = makeSandbox()
|
|
2038
2100
|
const inline = makeInliner()
|
|
@@ -2144,6 +2206,15 @@ background:rgba(20,20,22,.86);color:#e8e8ea;font:12px/18px -apple-system,system-
|
|
|
2144
2206
|
const y = parseFloat(cs.top)
|
|
2145
2207
|
if (Number.isFinite(x) && Number.isFinite(y)) st += `left:${x - bodyAt.l}px;top:${y - bodyAt.t}px;right:auto;bottom:auto;`
|
|
2146
2208
|
}
|
|
2209
|
+
// 整页:贴着窗口底部的固定元素(标签栏、操作条、悬浮按钮)放到整张图的底部。内联的 top 是按窗口算好的像素,上下都写死时 top 说了算,不挪就钉在第一屏底部、盖住中间的内容
|
|
2210
|
+
if (full && H > innerHeight && cs.position === 'fixed' && el.namespaceURI !== SVGNS && el.ownerDocument === document && document.body.contains(el) && !fixedHeld(el)) {
|
|
2211
|
+
const r = el.getBoundingClientRect()
|
|
2212
|
+
const y = parseFloat(cs.top)
|
|
2213
|
+
if (Number.isFinite(y) && r.height > 0 && r.height < innerHeight / 2 && r.top >= innerHeight / 2 && r.bottom <= innerHeight + 1 && bottomPinned(el)) {
|
|
2214
|
+
st += `top:${y + H - innerHeight}px;bottom:auto;`
|
|
2215
|
+
bars.set(el, H - innerHeight)
|
|
2216
|
+
}
|
|
2217
|
+
}
|
|
2147
2218
|
// 原来一行放得下的字,画成图时也别折行(宽度是定死的,字体渲染差一丁点就会挤到第二行)
|
|
2148
2219
|
if (el.namespaceURI !== SVGNS && cs.whiteSpace !== 'nowrap' && oneLine(el)) st += 'white-space:nowrap;'
|
|
2149
2220
|
c.setAttribute('style', st)
|
|
@@ -2349,7 +2420,7 @@ background:rgba(20,20,22,.86);color:#e8e8ea;font:12px/18px -apple-system,system-
|
|
|
2349
2420
|
ctx.fillRect(0, 0, canvas.width, canvas.height)
|
|
2350
2421
|
ctx.drawImage(img, 0, 0, canvas.width, canvas.height)
|
|
2351
2422
|
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 }
|
|
2423
|
+
return { png, w: W, h: H, scale, top, bars }
|
|
2353
2424
|
} finally {
|
|
2354
2425
|
box.remove()
|
|
2355
2426
|
}
|
|
@@ -2384,6 +2455,16 @@ background:rgba(20,20,22,.86);color:#e8e8ea;font:12px/18px -apple-system,system-
|
|
|
2384
2455
|
}
|
|
2385
2456
|
|
|
2386
2457
|
/** 固定元素是不是被某个祖先(transform、filter 之类)接管了位置:接管了就跟着那个祖先走,不用挪 */
|
|
2458
|
+
/** 是靠 bottom 贴着窗口底部的(标签栏、悬浮按钮),不是写了 top 碰巧在下半屏的浮层。认不出来就当不是,留在原位 */
|
|
2459
|
+
function bottomPinned(el) {
|
|
2460
|
+
try {
|
|
2461
|
+
const m = el.computedStyleMap()
|
|
2462
|
+
return String(m.get('top')) === 'auto' && String(m.get('bottom')) !== 'auto'
|
|
2463
|
+
} catch {
|
|
2464
|
+
return false
|
|
2465
|
+
}
|
|
2466
|
+
}
|
|
2467
|
+
|
|
2387
2468
|
function fixedHeld(el) {
|
|
2388
2469
|
for (let a = el.parentElement; a; a = a.parentElement) if (holds(getComputedStyle(a), true)) return true
|
|
2389
2470
|
return false
|
|
@@ -2524,7 +2605,8 @@ background:rgba(20,20,22,.86);color:#e8e8ea;font:12px/18px -apple-system,system-
|
|
|
2524
2605
|
try {
|
|
2525
2606
|
const r = await capture(d)
|
|
2526
2607
|
// 整页图从文档顶上画起,链接加上滚动距离;只截视口时就是视口里的位置
|
|
2527
|
-
|
|
2608
|
+
// 挪到整张图底部的栏:里面的链接跟着栏走,它本来就不随页面滚
|
|
2609
|
+
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 }))
|
|
2528
2610
|
.filter((l) => l.x < r.w && l.y < r.h && l.x + l.w > 0 && l.y + l.h > 0)
|
|
2529
2611
|
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() })
|
|
2530
2612
|
} catch (e) {
|
|
@@ -2534,6 +2616,7 @@ background:rgba(20,20,22,.86);color:#e8e8ea;font:12px/18px -apple-system,system-
|
|
|
2534
2616
|
|
|
2535
2617
|
// 导出前预检:这一页实际多大、有几个点了没反应的链接(href="#"、指向不存在的文件)
|
|
2536
2618
|
async function onMeasure(d) {
|
|
2619
|
+
goScrollHome('instant') // 正在为闪改动滚走:按原来的位置量
|
|
2537
2620
|
try {
|
|
2538
2621
|
if (document.readyState !== 'complete') await new Promise((r) => { addEventListener('load', r, { once: true }); setTimeout(r, 1500) })
|
|
2539
2622
|
const de = document.documentElement
|
|
@@ -4145,7 +4228,12 @@ background:rgba(20,20,22,.86);color:#e8e8ea;font:12px/18px -apple-system,system-
|
|
|
4145
4228
|
async function onCheck(d) {
|
|
4146
4229
|
let r
|
|
4147
4230
|
try {
|
|
4231
|
+
goScrollHome('instant') // 量的时候要滚动,别把闪改动的回位打断、停在半路
|
|
4148
4232
|
await settle()
|
|
4233
|
+
// 刚换上来的帧,ready 可能早于第一次排版(视口还是 0×0,量出来是空清单):等排好版再量,等不到就照实说没量成,别当成「没问题」
|
|
4234
|
+
const laidOut = () => innerWidth > 0 && innerHeight > 0 && document.documentElement.clientWidth > 0 && document.documentElement.getClientRects().length > 0
|
|
4235
|
+
for (let i = 0; i < 60 && !laidOut(); i++) await new Promise((res) => setTimeout(res, 50))
|
|
4236
|
+
if (!laidOut()) throw new Error('layout')
|
|
4149
4237
|
r = runCheck(String(d.kind || 'web'))
|
|
4150
4238
|
} catch (e) {
|
|
4151
4239
|
// 量的时候出错了:照实说没量成,别当成「没问题」
|
|
@@ -4161,6 +4249,233 @@ background:rgba(20,20,22,.86);color:#e8e8ea;font:12px/18px -apple-system,system-
|
|
|
4161
4249
|
post({ type: 'checked', id: d.id, issues: r.issues.map(({ el, ...rest }) => rest), metrics: r.metrics, width: r.metrics.w })
|
|
4162
4250
|
}
|
|
4163
4251
|
|
|
4252
|
+
// ── 业务标注:编号圆点、选中才描框、点编号回报。内容(annotations.json)由右侧栏发来,全当数据:选择器只进 querySelector,文字只用 textContent ──
|
|
4253
|
+
let anMarks = [] // { a, el, node, dot }
|
|
4254
|
+
let anSel = ''
|
|
4255
|
+
let anRaf = 0
|
|
4256
|
+
let anMo = null
|
|
4257
|
+
let anRo = null
|
|
4258
|
+
let anReT = 0
|
|
4259
|
+
let anReGap = 250
|
|
4260
|
+
let anLast = ''
|
|
4261
|
+
|
|
4262
|
+
const flatText = (s) => String(s || '').replace(/\s+/g, '').toLowerCase()
|
|
4263
|
+
const shownBox = (el) => {
|
|
4264
|
+
const r = el.getBoundingClientRect()
|
|
4265
|
+
if (r.width <= 0 && r.height <= 0) return false
|
|
4266
|
+
return !hiddenChain(el) // 自己或祖先 display:none、visibility:hidden、完全透明
|
|
4267
|
+
}
|
|
4268
|
+
|
|
4269
|
+
/** el 里看得见的字:藏着的、完全透明的文字节点不算 */
|
|
4270
|
+
function seenText(el) {
|
|
4271
|
+
let s = ''
|
|
4272
|
+
const w = document.createTreeWalker(el, NodeFilter.SHOW_TEXT)
|
|
4273
|
+
for (let n = w.nextNode(); n; n = w.nextNode()) if (n.parentElement && !hiddenChain(n.parentElement)) s += n.nodeValue
|
|
4274
|
+
return s
|
|
4275
|
+
}
|
|
4276
|
+
|
|
4277
|
+
/** 文字里含这段字、看得见、最小的那个元素(一段字被 <a> 切开也认) */
|
|
4278
|
+
function annotByText(text) {
|
|
4279
|
+
const want = flatText(text)
|
|
4280
|
+
if (!want || !document.body) return null
|
|
4281
|
+
let best = null
|
|
4282
|
+
let bestLen = Infinity
|
|
4283
|
+
for (const el of document.body.querySelectorAll('*')) {
|
|
4284
|
+
if (SKIP_TAGS.has(el.tagName) || el.tagName === 'STYLE' || el.tagName === 'DSH-DESIGN-EDIT') continue
|
|
4285
|
+
const tx = flatText(el.textContent)
|
|
4286
|
+
if (tx.length > bestLen || !tx.includes(want)) continue
|
|
4287
|
+
if (tx.length === bestLen && !(best && best.contains(el))) continue // 一样长:子元素比父元素准,别的地方的同样文字不抢第一处
|
|
4288
|
+
if (!shownBox(el) || !flatText(el.innerText ?? el.textContent).includes(want) || !flatText(seenText(el)).includes(want)) continue // 容器看得见、字在藏着、折叠或透明的子元素里:不算
|
|
4289
|
+
best = el
|
|
4290
|
+
bestLen = tx.length
|
|
4291
|
+
}
|
|
4292
|
+
return best
|
|
4293
|
+
}
|
|
4294
|
+
|
|
4295
|
+
/** 一条标注对应页面上哪个元素 → { el, how: selector / text / page(整屏的说明)/ ''(定位不到), count: 选择器匹配到几个 } */
|
|
4296
|
+
function findAnnot(a) {
|
|
4297
|
+
const selector = String((a && a.selector) || '')
|
|
4298
|
+
const text = String((a && a.fallbackText) || '')
|
|
4299
|
+
if (!selector && !text) return { el: document.body, how: 'page', count: 0 } // 整屏的说明:编号放在页面左上角
|
|
4300
|
+
let count = 0
|
|
4301
|
+
let el = null
|
|
4302
|
+
if (selector) {
|
|
4303
|
+
try {
|
|
4304
|
+
const all = document.querySelectorAll(selector)
|
|
4305
|
+
count = all.length
|
|
4306
|
+
el = all[0] || null
|
|
4307
|
+
} catch { /* 选择器写法不对:当作没找到 */ }
|
|
4308
|
+
if (el) return { el, how: 'selector', count }
|
|
4309
|
+
}
|
|
4310
|
+
el = annotByText(text)
|
|
4311
|
+
return { el, how: el ? 'text' : '', count }
|
|
4312
|
+
}
|
|
4313
|
+
|
|
4314
|
+
/** 元素没被能滚、能裁的容器挡住的那块(视口坐标);全挡住了是 null:编号别飘在外面 */
|
|
4315
|
+
function annotVisible(el) {
|
|
4316
|
+
const r = el.getBoundingClientRect()
|
|
4317
|
+
const box = { left: r.left, top: r.top, right: r.right, bottom: r.bottom }
|
|
4318
|
+
let mode = getComputedStyle(el).position
|
|
4319
|
+
for (let p = el.parentElement; p && p !== document.body && p !== document.documentElement; p = p.parentElement) {
|
|
4320
|
+
const o = getComputedStyle(p)
|
|
4321
|
+
if ((mode === 'absolute' && !holds(o, false)) || (mode === 'fixed' && !holds(o, true))) continue // 这层不是它的包含块,裁不到它
|
|
4322
|
+
mode = o.position
|
|
4323
|
+
if (o.overflowX === 'visible' && o.overflowY === 'visible') continue
|
|
4324
|
+
const c = p.getBoundingClientRect()
|
|
4325
|
+
if (o.overflowX !== 'visible') { box.left = Math.max(box.left, c.left); box.right = Math.min(box.right, c.right) }
|
|
4326
|
+
if (o.overflowY !== 'visible') { box.top = Math.max(box.top, c.top); box.bottom = Math.min(box.bottom, c.bottom) }
|
|
4327
|
+
if ((r.width > 0 && box.right <= box.left) || (r.height > 0 && box.bottom <= box.top)) return null
|
|
4328
|
+
}
|
|
4329
|
+
// 再和视口取交集(整个在视口外就按版面位置算,滚到了自然对)
|
|
4330
|
+
const vp = { left: Math.max(box.left, 0), top: Math.max(box.top, 0), right: Math.min(box.right, innerWidth), bottom: Math.min(box.bottom, innerHeight) }
|
|
4331
|
+
const seen = vp.right > vp.left && vp.bottom > vp.top ? vp : box
|
|
4332
|
+
return { ...seen, partial: seen.left > r.left + 1 || seen.top > r.top + 1 || seen.right < r.right - 1 || seen.bottom < r.bottom - 1 }
|
|
4333
|
+
}
|
|
4334
|
+
|
|
4335
|
+
function placeAnnots() {
|
|
4336
|
+
for (const m of anMarks) {
|
|
4337
|
+
const vis = m.el && m.el.isConnected && shownBox(m.el) ? annotVisible(m.el) : null
|
|
4338
|
+
if (!vis) { m.node.style.display = 'none'; continue }
|
|
4339
|
+
const r = docRect(m.el)
|
|
4340
|
+
Object.assign(m.node.style, { display: '', left: r.left + 'px', top: r.top + 'px', width: r.width + 'px', height: r.height + 'px' })
|
|
4341
|
+
// 编号放在露出来的那块的左上角;贴着页面边的往里挪,描框位置不变
|
|
4342
|
+
const b = m.el.getBoundingClientRect()
|
|
4343
|
+
const inset = (hid, at) => (hid > 1 ? hid + 9 * K : Math.max(0, 9 * K - at))
|
|
4344
|
+
const ml = inset(vis.left - b.left, r.left)
|
|
4345
|
+
const mt = inset(vis.top - b.top, r.top)
|
|
4346
|
+
m.dot.style.marginLeft = ml ? `${ml}px` : ''
|
|
4347
|
+
m.dot.style.marginTop = mt ? `${mt}px` : ''
|
|
4348
|
+
}
|
|
4349
|
+
}
|
|
4350
|
+
const queueAnnots = () => { if (!anRaf) anRaf = requestAnimationFrame(() => { anRaf = 0; placeAnnots() }) }
|
|
4351
|
+
|
|
4352
|
+
// 同一个元素上的几条:编号往右排开
|
|
4353
|
+
function spreadDots() {
|
|
4354
|
+
const perEl = new Map()
|
|
4355
|
+
for (const m of anMarks) {
|
|
4356
|
+
if (!m.el) continue
|
|
4357
|
+
const i = perEl.get(m.el) || 0
|
|
4358
|
+
perEl.set(m.el, i + 1)
|
|
4359
|
+
m.dot.style.setProperty('--i', i)
|
|
4360
|
+
}
|
|
4361
|
+
}
|
|
4362
|
+
const postAnnotState = () => post({ type: 'annotState', list: anMarks.map((m) => ({ id: m.a.id, how: m.how, count: m.count })) })
|
|
4363
|
+
|
|
4364
|
+
/** 页面内容变了(切标签、脚本重画):每条重新对一遍,后冒出来的补上编号、选择器换了对象跟过去、状态报回右侧栏。这一轮太费时就放慢 */
|
|
4365
|
+
function relocateAnnots() {
|
|
4366
|
+
anReT = 0
|
|
4367
|
+
const t0 = performance.now()
|
|
4368
|
+
let moved = false
|
|
4369
|
+
let state = false
|
|
4370
|
+
for (const m of anMarks) {
|
|
4371
|
+
const f = findAnnot(m.a)
|
|
4372
|
+
if (f.el !== m.el) { m.el = f.el; moved = true }
|
|
4373
|
+
if (f.how !== m.how || f.count !== m.count) { m.how = f.how; m.count = f.count; state = true }
|
|
4374
|
+
}
|
|
4375
|
+
if (moved) spreadDots()
|
|
4376
|
+
if (moved || state) placeAnnots()
|
|
4377
|
+
if (state) postAnnotState()
|
|
4378
|
+
anReGap = Math.min(3000, Math.max(250, (performance.now() - t0) * 20))
|
|
4379
|
+
}
|
|
4380
|
+
const onAnnotDom = () => { queueAnnots(); if (!anReT) anReT = setTimeout(relocateAnnots, anReGap) }
|
|
4381
|
+
|
|
4382
|
+
function watchAnnots(on) {
|
|
4383
|
+
if (on && !anMo) {
|
|
4384
|
+
addEventListener('scroll', queueAnnots, true) // 捕获:里面能滚的区域也算
|
|
4385
|
+
addEventListener('resize', queueAnnots)
|
|
4386
|
+
document.addEventListener('load', onAnnotDom, true) // 图片晚到把版面推走(load 到不了 window,捕获挂在 document)
|
|
4387
|
+
addEventListener('change', onAnnotDom, true) // 纯 CSS 的切换(:checked 换标签内容)没有 DOM 变化
|
|
4388
|
+
addEventListener('input', onAnnotDom, true)
|
|
4389
|
+
addEventListener('transitionend', queueAnnots, true)
|
|
4390
|
+
addEventListener('animationend', queueAnnots, true)
|
|
4391
|
+
anMo = new MutationObserver(onAnnotDom)
|
|
4392
|
+
if (document.body) anMo.observe(document.body, { subtree: true, childList: true, characterData: true, attributes: true })
|
|
4393
|
+
anMo.observe(document.documentElement, { attributes: true }) // 根节点上切 class / data-view 换内容
|
|
4394
|
+
if (typeof ResizeObserver === 'function') {
|
|
4395
|
+
anRo = new ResizeObserver(queueAnnots) // 区域展开、字体换行,页面整体变高
|
|
4396
|
+
anRo.observe(document.documentElement)
|
|
4397
|
+
if (document.body) anRo.observe(document.body)
|
|
4398
|
+
}
|
|
4399
|
+
} else if (!on && anMo) {
|
|
4400
|
+
removeEventListener('scroll', queueAnnots, true)
|
|
4401
|
+
removeEventListener('resize', queueAnnots)
|
|
4402
|
+
document.removeEventListener('load', onAnnotDom, true)
|
|
4403
|
+
removeEventListener('change', onAnnotDom, true)
|
|
4404
|
+
removeEventListener('input', onAnnotDom, true)
|
|
4405
|
+
removeEventListener('transitionend', queueAnnots, true)
|
|
4406
|
+
removeEventListener('animationend', queueAnnots, true)
|
|
4407
|
+
anMo.disconnect()
|
|
4408
|
+
anMo = null
|
|
4409
|
+
if (anRo) anRo.disconnect()
|
|
4410
|
+
anRo = null
|
|
4411
|
+
}
|
|
4412
|
+
}
|
|
4413
|
+
|
|
4414
|
+
function clearAnnots() {
|
|
4415
|
+
for (const m of anMarks) m.node.remove()
|
|
4416
|
+
anMarks = []
|
|
4417
|
+
clearTimeout(anReT)
|
|
4418
|
+
anReT = 0
|
|
4419
|
+
watchAnnots(false)
|
|
4420
|
+
}
|
|
4421
|
+
|
|
4422
|
+
function setAnnotSel(id) {
|
|
4423
|
+
anSel = String(id || '')
|
|
4424
|
+
for (const m of anMarks) m.node.classList.toggle('sel', m.a.id === anSel)
|
|
4425
|
+
}
|
|
4426
|
+
|
|
4427
|
+
const pickAnnot = (m) => { setAnnotSel(m.a.id); post({ type: 'annotPick', id: m.a.id }) }
|
|
4428
|
+
|
|
4429
|
+
function showAnnots(list, sel) {
|
|
4430
|
+
clearAnnots()
|
|
4431
|
+
const items = Array.isArray(list) ? list.slice(0, 200) : []
|
|
4432
|
+
if (!items.length) { anSel = ''; return }
|
|
4433
|
+
const { root } = fxRoot()
|
|
4434
|
+
for (const a of items) {
|
|
4435
|
+
const f = findAnnot(a) // 这会儿定位不到的也留着:页面后面长出来时补上编号
|
|
4436
|
+
const node = document.createElement('div')
|
|
4437
|
+
node.className = 'an'
|
|
4438
|
+
const dot = document.createElement('b')
|
|
4439
|
+
dot.textContent = String(a.n)
|
|
4440
|
+
const m = { a, el: f.el, how: f.how, count: f.count, node, dot }
|
|
4441
|
+
dot.addEventListener('click', (e) => { e.stopPropagation(); e.preventDefault(); pickAnnot(m) })
|
|
4442
|
+
node.append(dot)
|
|
4443
|
+
root.append(node)
|
|
4444
|
+
anMarks.push(m)
|
|
4445
|
+
}
|
|
4446
|
+
spreadDots()
|
|
4447
|
+
setAnnotSel(sel)
|
|
4448
|
+
placeAnnots()
|
|
4449
|
+
watchAnnots(true)
|
|
4450
|
+
postAnnotState()
|
|
4451
|
+
// 字体、图片晚到会把版面推走
|
|
4452
|
+
setTimeout(placeAnnots, 400)
|
|
4453
|
+
if (document.fonts && document.fonts.ready) document.fonts.ready.then(placeAnnots).catch(() => {})
|
|
4454
|
+
}
|
|
4455
|
+
|
|
4456
|
+
/** 右侧栏点了这张卡片:滚过去,描框,闪一下 */
|
|
4457
|
+
function focusAnnot(id) {
|
|
4458
|
+
anLast = String(id || '')
|
|
4459
|
+
setAnnotSel(id)
|
|
4460
|
+
const m = anMarks.find((x) => x.a.id === id)
|
|
4461
|
+
if (!m || !m.el || !m.el.isConnected) return
|
|
4462
|
+
const go = () => { m.node.classList.remove('fl'); void m.node.offsetWidth; m.node.classList.add('fl') }
|
|
4463
|
+
if (m.how === 'page') { scrollAll(scrollAreas(), false); placeAnnots(); go(); return }
|
|
4464
|
+
const vis = annotVisible(m.el)
|
|
4465
|
+
if (inView(m.el) && vis && !vis.partial) { go(); return }
|
|
4466
|
+
try { m.el.scrollIntoView({ behavior: 'smooth', block: 'center' }) } catch { m.el.scrollIntoView() }
|
|
4467
|
+
setTimeout(() => { placeAnnots(); go() }, 450)
|
|
4468
|
+
}
|
|
4469
|
+
|
|
4470
|
+
/** 画布上每屏盖着一层挡板,点不到页面:右侧栏把点的位置告诉页面,点中编号就算点了它 */
|
|
4471
|
+
function hitAnnot(x, y) {
|
|
4472
|
+
for (const m of [...anMarks].reverse()) {
|
|
4473
|
+
if (m.node.style.display === 'none') continue
|
|
4474
|
+
const r = m.dot.getBoundingClientRect()
|
|
4475
|
+
if (x >= r.left - 3 && x <= r.right + 3 && y >= r.top - 3 && y <= r.bottom + 3) { pickAnnot(m); return }
|
|
4476
|
+
}
|
|
4477
|
+
}
|
|
4478
|
+
|
|
4164
4479
|
// ── 深色模式:页面带了深色样式([data-theme="dark"] / .dark / prefers-color-scheme: dark)就能切 ──
|
|
4165
4480
|
|
|
4166
4481
|
let darkOn = false
|
|
@@ -4262,12 +4577,17 @@ background:rgba(20,20,22,.86);color:#e8e8ea;font:12px/18px -apple-system,system-
|
|
|
4262
4577
|
else if (d.type === 'snapshot') post({ type: 'snap', seed: seedNow() })
|
|
4263
4578
|
else if (d.type === 'busy') { busy = !!d.on; watchHmr() }
|
|
4264
4579
|
else if (d.type === 'unpin') unpin()
|
|
4580
|
+
else if (d.type === 'holdScroll') holdScroll()
|
|
4265
4581
|
else if (d.type === 'reload') selfReload()
|
|
4266
4582
|
else if (d.type === 'capture') onCapture(d)
|
|
4267
4583
|
else if (d.type === 'measure') onMeasure(d)
|
|
4268
4584
|
else if (d.type === 'check') onCheck(d)
|
|
4269
4585
|
else if (d.type === 'checkClear') { clearMarks(); lastIssues = [] }
|
|
4270
|
-
else if (d.type === 'checkFocus') focusIssue(Number(d.i))
|
|
4586
|
+
else if (d.type === 'checkFocus') { holdScroll(); focusIssue(Number(d.i)) } // 用户点了别的地方要看,别再滚回去
|
|
4587
|
+
else if (d.type === 'annots') showAnnots(d.list, d.sel)
|
|
4588
|
+
else if (d.type === 'annotSel') setAnnotSel(d.id)
|
|
4589
|
+
else if (d.type === 'annotFocus') { holdScroll(); focusAnnot(String(d.id || '')) }
|
|
4590
|
+
else if (d.type === 'annotHit') hitAnnot(Number(d.x), Number(d.y))
|
|
4271
4591
|
else if (d.type === 'theme') { setDark(d.dark); postTop(); setTimeout(postTop, 400) }
|
|
4272
4592
|
else if (d.type === 'demo') demo = !!d.on
|
|
4273
4593
|
else if (d.type === 'edit') setTextEdit(!!d.on, d.hint !== false)
|
|
@@ -4298,6 +4618,15 @@ background:rgba(20,20,22,.86);color:#e8e8ea;font:12px/18px -apple-system,system-
|
|
|
4298
4618
|
/** 调节滑块的值套到页面上(和右侧栏拖的时候一样) */
|
|
4299
4619
|
tweak: (vars, attrs) => { applyTweak(vars, attrs); return true },
|
|
4300
4620
|
settle: () => settle().then(() => true),
|
|
4621
|
+
/** 业务标注定位得到吗(只找,不画):[{ id, how, count }] */
|
|
4622
|
+
annots: async (list) => {
|
|
4623
|
+
await settle()
|
|
4624
|
+
return (Array.isArray(list) ? list.slice(0, 200) : []).map((a) => { const f = findAnnot(a); return { id: a.id, how: f.how, count: f.count } })
|
|
4625
|
+
},
|
|
4626
|
+
/** 页面上现在画着哪几条业务标注的编号(id) */
|
|
4627
|
+
annotMarks: () => anMarks.filter((m) => m.node.isConnected && m.node.style.display !== 'none').map((m) => m.a.id),
|
|
4628
|
+
/** 最后被右侧栏定位的是哪条 */
|
|
4629
|
+
annotLast: () => anLast,
|
|
4301
4630
|
/** 跳到别的屏的地方(路径和按钮上的字),量完查一下目标在不在 */
|
|
4302
4631
|
links: () => linksOf().map(({ path, text }) => ({ path, text })),
|
|
4303
4632
|
info: () => {
|
package/lib/serve.js
CHANGED
|
@@ -68,6 +68,7 @@ export const MIME = {
|
|
|
68
68
|
// 改动通知里不算的:版本库、依赖、系统垃圾
|
|
69
69
|
const EXPORTS = /^(exports([\\/][^\\/]+\.(png|pdf|pptx))?|refs([\\/][^\\/]+\.(png|jpe?g|gif|webp))?)$/i // 导出的图片 / PDF / PPT、参考图(和第一次建的 exports / refs 目录):不算设计改了
|
|
70
70
|
const IGNORED = /(^|[\\/])(\.git|node_modules|\.DS_Store|\.next|\.cache|\.turbo)([\\/]|$)/
|
|
71
|
+
const ANNOTATIONS = /^annotations\.json(\.[0-9a-z]+\.tmp)?$/i // 业务标注(DSH 写文件时先写 .tmp 再改名):右侧栏靠轮询跟上,页面用不到它,别一改就整屏刷新
|
|
71
72
|
const HANDOFF = /^handoff([\\/]|$)/i // 转成代码的交接包(说明和截图):不算设计改了,也别在截图时把页面刷掉
|
|
72
73
|
|
|
73
74
|
/** 只认本机名 + 自己的端口(防 DNS 重绑定:外站把域名解析到 127.0.0.1 也进不来) */
|
|
@@ -305,7 +306,7 @@ export function draftInjector(tag) {
|
|
|
305
306
|
}
|
|
306
307
|
}
|
|
307
308
|
|
|
308
|
-
export function createHub({ log = () => {}, onChange = () => {}, history = null, parents = () => [], strict = null, drafts = null } = {}) {
|
|
309
|
+
export function createHub({ log = () => {}, onChange = () => {}, history = null, parents = () => [], strict = null, drafts = null, skipAnnots = () => false } = {}) {
|
|
309
310
|
const strictHtml = (buf) => { try { return !!(strict && strict.html && strict.html(buf)) } catch (e) { log(e); return true } }
|
|
310
311
|
const strictRoot = (dir) => { try { return !!(strict && strict.root && strict.root(dir)) } catch (e) { log(e); return true } }
|
|
311
312
|
const strictVer = (dir) => { try { return !!(strict && strict.versions && strict.versions(dir)) } catch (e) { log(e); return true } }
|
|
@@ -521,7 +522,7 @@ export function createHub({ log = () => {}, onChange = () => {}, history = null,
|
|
|
521
522
|
try {
|
|
522
523
|
entry.watcher = fs.watch(entry.root, { recursive: true }, (_type, name) => {
|
|
523
524
|
const rel = String(name || '')
|
|
524
|
-
if (IGNORED.test(rel) || /(^|[\\/])\./.test(rel) || EXPORTS.test(rel) || HANDOFF.test(rel)) return
|
|
525
|
+
if (IGNORED.test(rel) || /(^|[\\/])\./.test(rel) || EXPORTS.test(rel) || HANDOFF.test(rel) || (skipAnnots() && ANNOTATIONS.test(rel))) return
|
|
525
526
|
last = rel
|
|
526
527
|
batch.add(rel.split(path.sep).join('/'))
|
|
527
528
|
clearTimeout(timer)
|