dsh-design-studio 0.25.3 → 0.26.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/lib/picker.js CHANGED
@@ -5352,6 +5352,140 @@ background:rgba(20,20,22,.86);color:#e8e8ea;font:12px/18px -apple-system,system-
5352
5352
  }
5353
5353
  }
5354
5354
 
5355
+ // 「和做出来的比」(#25):右侧栏把设计稿和做出来的页面摆在一起看,两边滚动同步;
5356
+ // 套了外框的设计还要报 main 槽位在哪(真页面盖在槽位上),量法和 compare_design 是同一份(预览服务器发脚本时带上的)
5357
+ const slotBoxOf = typeof window.__DSH_DESIGN_SLOT__ === 'function' ? window.__DSH_DESIGN_SLOT__ : null
5358
+ const slotMainOf = typeof window.__DSH_DESIGN_MAIN__ === 'function' ? window.__DSH_DESIGN_MAIN__ : null
5359
+ let sync = null // { slot, el, quiet, raf, ro, route, timer, gesture }
5360
+ const NAV_KEY = '__ddp_nav' // 用户在页面里点过 / 按过键的时间:整页跳转后新页面也知道是用户点过去的
5361
+ /** 跟着滚的是哪块:套了外框只认槽位里面能滚的(没有就是页面本身),不然是页面主要的那块滚动区 */
5362
+ function syncArea() {
5363
+ const main = sync && sync.slot && slotMainOf ? slotMainOf() : null
5364
+ const areas = scrollAreas()
5365
+ if (main) {
5366
+ const inner = areas.filter((a) => a !== se() && main.contains(a))
5367
+ if (inner.length) return inner.sort((a, b) => (b.scrollHeight - b.clientHeight) - (a.scrollHeight - a.clientHeight))[0]
5368
+ return se() // 槽位滚不动:跟整页,不选外框的菜单
5369
+ }
5370
+ if (areas.includes(se())) return se()
5371
+ // 页面本身不滚:挑看得见的面积最大的那块(正文),不按能滚多远挑(后台页的长侧栏常比正文长)
5372
+ const big = areas.filter((a) => a.clientHeight >= innerHeight * 0.4 && !isOurs(a))
5373
+ return big.sort((a, b) => b.clientWidth * b.clientHeight - a.clientWidth * a.clientHeight)[0] || mainArea()
5374
+ }
5375
+ const syncPos = (el) => (el === se() ? { x: scrollX, y: scrollY } : { x: el.scrollLeft, y: el.scrollTop })
5376
+ function syncSlot() {
5377
+ if (!sync || !sync.slot || sync.raf) return
5378
+ sync.raf = requestAnimationFrame(() => {
5379
+ if (!sync) return
5380
+ sync.raf = 0
5381
+ let box = null
5382
+ try { box = slotBoxOf ? slotBoxOf() : null } catch { box = null }
5383
+ post({ type: 'slot', box })
5384
+ })
5385
+ }
5386
+ function onSyncScroll(e) {
5387
+ if (!sync) return
5388
+ const t = e.target === document || e.target === document.documentElement || e.target === document.body ? se() : e.target
5389
+ if (!(t instanceof Element)) return
5390
+ if (t === se()) syncSlot() // 页面整个滚了:槽位跟着挪
5391
+ if (t !== sync.el) {
5392
+ // 用户滚的是另一块大的滚动区:以后跟它(页面加载完才长出来的主滚动区、单页应用换了页)
5393
+ if (isOurs(t) || t.clientHeight < innerHeight * 0.4) return
5394
+ // 套了外框:只跟槽位里的(或整个页面)滚,左边长菜单滚了不管
5395
+ const main = sync.slot && slotMainOf ? slotMainOf() : null
5396
+ if (main && t !== se() && !main.contains(t)) return
5397
+ // 现在跟的那块还滚得动:新的得差不多大才换(长侧边栏、代码块滚了不换)
5398
+ const cur = sync.el
5399
+ const area = (el) => (el === se() ? innerWidth * innerHeight : el.clientWidth * el.clientHeight)
5400
+ if (cur && cur.isConnected && (cur.scrollHeight - cur.clientHeight >= 2 || cur.scrollWidth - cur.clientWidth >= 2) && area(t) < area(cur) * 0.5) return
5401
+ sync.el = t
5402
+ }
5403
+ if (performance.now() < sync.quiet) return // 是我们替另一边滚的,不再回传
5404
+ post({ type: 'scrolled', ...syncPos(t) })
5405
+ }
5406
+ // 页面换了路由(点了站内链接、单页应用换页):报给右侧栏(比的不是这页了,红框、分数先收起来)
5407
+ function syncRoute() {
5408
+ if (!sync) return
5409
+ const r = location.pathname + location.search + location.hash
5410
+ if (r === sync.route) return
5411
+ const first = sync.route === null
5412
+ sync.route = r
5413
+ // 是不是用户点过去的(刚点过、按过键):页面自己跳的(/ → /login、/ → /#/)右侧栏当成就是这页
5414
+ let user = performance.now() - sync.gesture < 3000
5415
+ if (first) { try { user = Date.now() - Number(sessionStorage.getItem(NAV_KEY) || 0) < 4000 } catch { /* 存不了就不算 */ } }
5416
+ post({ type: 'route', path: r, user })
5417
+ }
5418
+ // 脚本跳转、表单提交到站外:也不在对比框里跳
5419
+ function onSyncNavigate(e) {
5420
+ if (!sync || !e.cancelable || e.navigationType === 'traverse' || e.downloadRequest) return
5421
+ let u
5422
+ try { u = new URL(e.destination.url) } catch { return }
5423
+ if (!/^https?:$/.test(u.protocol) || u.origin === location.origin) return
5424
+ e.preventDefault()
5425
+ post({ type: 'blocked', href: u.href.slice(0, 200), why: 'sync' })
5426
+ }
5427
+ function onSyncGesture(e) {
5428
+ if (!sync) return
5429
+ sync.gesture = performance.now()
5430
+ try { sessionStorage.setItem(NAV_KEY, String(Date.now())) } catch { /* 存不了:只认这一页里的 */ }
5431
+ if (e.type !== 'click') return
5432
+ // 站外链接不在对比框里跳(外网页面盖进来、旧分数红框还挂着):拦下,右侧栏报一声
5433
+ const a = e.target && e.target.closest ? e.target.closest('a[href], area[href]') : null
5434
+ if (!a || a.hasAttribute('download')) return
5435
+ const tg = String(a.getAttribute('target') || '').toLowerCase()
5436
+ if (tg && tg !== '_self') return
5437
+ let u
5438
+ try { u = new URL(a.getAttribute('href'), location.href) } catch { return }
5439
+ if (!/^https?:$/.test(u.protocol) || u.origin === location.origin) return
5440
+ e.preventDefault()
5441
+ e.stopImmediatePropagation()
5442
+ post({ type: 'blocked', href: u.href.slice(0, 200), why: 'sync' })
5443
+ }
5444
+ function setSync(on, slot) {
5445
+ if (sync) {
5446
+ removeEventListener('scroll', onSyncScroll, true)
5447
+ removeEventListener('resize', syncSlot)
5448
+ removeEventListener('popstate', syncRoute)
5449
+ removeEventListener('hashchange', syncRoute)
5450
+ for (const ev of ['pointerdown', 'keydown', 'click']) removeEventListener(ev, onSyncGesture, true)
5451
+ if (window.navigation && typeof navigation.removeEventListener === 'function') navigation.removeEventListener('navigate', onSyncNavigate)
5452
+ if (sync.ro) sync.ro.disconnect()
5453
+ if (sync.raf) cancelAnimationFrame(sync.raf)
5454
+ clearInterval(sync.timer)
5455
+ sync = null
5456
+ }
5457
+ if (!on) return
5458
+ sync = { slot: !!slot, el: null, quiet: 0, raf: 0, ro: null, route: null, timer: 0, gesture: -1e9 }
5459
+ sync.el = syncArea()
5460
+ addEventListener('scroll', onSyncScroll, true)
5461
+ for (const ev of ['pointerdown', 'keydown', 'click']) addEventListener(ev, onSyncGesture, true)
5462
+ if (window.navigation && typeof navigation.addEventListener === 'function') navigation.addEventListener('navigate', onSyncNavigate)
5463
+ addEventListener('popstate', syncRoute)
5464
+ addEventListener('hashchange', syncRoute)
5465
+ sync.timer = setInterval(syncRoute, 500) // pushState 没有事件:隔一会儿看一眼
5466
+ syncRoute()
5467
+ if (sync.slot) {
5468
+ addEventListener('resize', syncSlot)
5469
+ const main = slotMainOf ? slotMainOf() : null
5470
+ if (main && typeof ResizeObserver === 'function') { sync.ro = new ResizeObserver(syncSlot); sync.ro.observe(main) }
5471
+ syncSlot()
5472
+ }
5473
+ post({ type: 'scrolled', ...syncPos(sync.el), first: true }) // 刚开始同步:报一下现在在哪(右侧栏决定谁跟谁)
5474
+ }
5475
+ function syncTo(x, y) {
5476
+ if (!sync) return
5477
+ // 原来那块没了、或滚不动(页面先出加载提示、主内容后长出来):重新找能滚的那块
5478
+ const stuck = (x) => x.scrollHeight - x.clientHeight < 2 && x.scrollWidth - x.clientWidth < 2
5479
+ if (!sync.el || !sync.el.isConnected) sync.el = syncArea()
5480
+ else if (stuck(sync.el) && performance.now() - (sync.scan || 0) > 500) { sync.scan = performance.now(); sync.el = syncArea() } // 找一遍要量整页:别每次滚都找
5481
+ const el = sync.el
5482
+ sync.quiet = performance.now() + 150
5483
+ const to = { left: Math.max(0, Number(x) || 0), top: Math.max(0, Number(y) || 0), behavior: 'instant' }
5484
+ if (el === se()) scrollTo(to)
5485
+ else el.scrollTo(to)
5486
+ post({ type: 'scrolled', ...syncPos(el), set: true }) // 实际滚到哪(滚到头会停住):只给那边记位置,不再转给另一边
5487
+ }
5488
+
5355
5489
  addEventListener('message', (e) => {
5356
5490
  if (!parentWin || e.source !== parentWin || !parentOrigins.includes(e.origin)) return
5357
5491
  const d = e.data
@@ -5383,6 +5517,8 @@ background:rgba(20,20,22,.86);color:#e8e8ea;font:12px/18px -apple-system,system-
5383
5517
  else if (d.type === 'styleSel') onStyleSel(d.selector)
5384
5518
  else if (d.type === 'style') { styleApply(d.css); queueTop() }
5385
5519
  else if (d.type === 'tweak') { applyTweak(d.vars, d.attrs); queueTop() }
5520
+ else if (d.type === 'sync') setSync(!!d.on, !!d.slot)
5521
+ else if (d.type === 'scrollTo') syncTo(d.x, d.y)
5386
5522
  })
5387
5523
 
5388
5524
  // 无头浏览器(check_design / look_design 在电脑上另开的,见 browser.js)直接调这几个:
package/lib/serve.js CHANGED
@@ -19,6 +19,7 @@ import net from 'node:net'
19
19
  import path from 'node:path'
20
20
  import { fileURLToPath } from 'node:url'
21
21
  import { tokenize } from './gallery/html.js'
22
+ import { FRAME_MAIN, SLOT_BOX } from './slot.js'
22
23
 
23
24
  export const INTERNAL = '/__dsh_design__/'
24
25
  const PICKER_FILE = path.join(path.dirname(fileURLToPath(import.meta.url)), 'picker.js')
@@ -436,7 +437,8 @@ export function createHub({ log = () => {}, onChange = () => {}, onWatch = () =>
436
437
  fs.readFile(PICKER_FILE, 'utf8', (err, src) => {
437
438
  if (err) return send(res, 500, 'picker missing')
438
439
  const cfg = JSON.stringify({ live: entry.kind === 'file', parents: parentOrigins() })
439
- send(res, 200, `window.__DSH_DESIGN__=${cfg};\n${src}`, { 'Content-Type': 'text/javascript; charset=utf-8' })
440
+ // 外框槽位在哪(和 compare_design 同一份量法):「和做出来的比」把真页面盖在槽位上
441
+ send(res, 200, `window.__DSH_DESIGN__=${cfg};\nwindow.__DSH_DESIGN_SLOT__=()=>${SLOT_BOX};\nwindow.__DSH_DESIGN_MAIN__=()=>${FRAME_MAIN};\n${src}`, { 'Content-Type': 'text/javascript; charset=utf-8' })
440
442
  })
441
443
  }
442
444
 
package/lib/slot.js ADDED
@@ -0,0 +1,37 @@
1
+ /**
2
+ * 套了外框的设计(DSH 插件、外框库)里 main 槽位在哪:两处用同一份量法(#25)
3
+ * - compare_design(无头浏览器里 page.eval):只截槽位那块比
4
+ * - 预览页面里的 picker.js(预览服务器发脚本时带上 window.__DSH_DESIGN_SLOT__):「和做出来的比」把真页面盖在槽位上
5
+ * 都是页面里跑的表达式源码(字符串),这里不引别的模块
6
+ */
7
+
8
+ // 套了外框的设计(DSH 插件、外框库):外框是宿主画的,做出来的只有槽位 main 那块 → 只截这块比(#25)
9
+ // 返回整页坐标(截图 clip 用)和视口坐标(对元素块用);槽位太小或看不见给 null,照整屏比
10
+ // 量的是内容那块:槽位自己的边框、内边距是宿主留的白(DSH 设置分区左右 24px),真插件的页面从内容那块开始
11
+ // 外框根和体检一样认:第一个带 data-frame、里面有 data-slot 的元素,而且是登记过的(DSH 自带的四个,或页面里有外框库那段 <template id="dsh-frame">);
12
+ // 普通设计里碰巧写了 data-frame 的包裹层(手机示意框之类)不算
13
+ export const FRAME_MAIN = `(() => {
14
+ const f = [...document.querySelectorAll('[data-frame]')].find((x) => x.querySelector('[data-slot]'))
15
+ if (!f) return null
16
+ if (!/^dsh\\/(sidebar|settings|view|panel)$/.test(f.getAttribute('data-frame') || '') && !document.getElementById('dsh-frame')) return null
17
+ return f.querySelector('[data-slot="main"]')
18
+ })()`
19
+ export const SLOT_BOX = `(() => {
20
+ const el = ${FRAME_MAIN}
21
+ if (!el) return null
22
+ const r = el.getBoundingClientRect()
23
+ const s = getComputedStyle(el)
24
+ const n = (k) => parseFloat(s[k]) || 0
25
+ const left = r.left + n('borderLeftWidth') + n('paddingLeft'), top = r.top + n('borderTopWidth') + n('paddingTop')
26
+ const right = r.left + n('borderLeftWidth') + el.clientWidth - n('paddingRight'), bottom = r.top + n('borderTopWidth') + el.clientHeight - n('paddingBottom')
27
+ const vx = Math.max(0, Math.round(left)), vy = Math.max(0, Math.round(top))
28
+ const w = Math.min(Math.round(right), innerWidth) - vx, h = Math.min(Math.round(bottom), innerHeight) - vy
29
+ // 外框自己多大:页面开头类型标记里写的(外框库存的「framed 宽x高」),没写就量外框根元素(自带 DSH 外框固定 1280×800)
30
+ const mm = /(\\d{3,4})\\s*[x×]\\s*(\\d{3,4})/.exec((document.querySelector('meta[name="dsh-design"]') || {}).content || '')
31
+ const fr = el.closest('[data-frame]').getBoundingClientRect()
32
+ const fw = mm ? +mm[1] : Math.round(fr.width), fh = mm ? +mm[2] : Math.round(fr.height)
33
+ if (w < 40 || h < 40) return { hidden: true, fw, fh } // 认出了外框,但这个尺寸下槽位露不出来:别退回整屏比(会拿外框去比)
34
+ // 开的尺寸装不下整个槽位(只露出一部分):照样比露出来的,但要说一声
35
+ const part = w < Math.round(right - left) - 1 || h < Math.round(bottom - top) - 1
36
+ return { x: vx + Math.round(scrollX), y: vy + Math.round(scrollY), vx, vy, w, h, part, fw, fh }
37
+ })()`
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-design-studio",
3
- "version": "0.25.3",
3
+ "version": "0.26.0",
4
4
  "type": "module",
5
5
  "scripts": {
6
6
  "test": "node --test --test-concurrency=1 test/*.mjs"