dsh-design-studio 0.25.2 → 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/client.js +615 -42
- package/lib/handoff.js +169 -24
- package/lib/impl.js +123 -0
- package/lib/index.js +204 -31
- package/lib/picker.js +136 -0
- package/lib/serve.js +3 -1
- package/lib/slot.js +37 -0
- package/package.json +1 -1
package/lib/index.js
CHANGED
|
@@ -68,7 +68,8 @@ import { SYSTEMS_DIR, extractSystem, listSystems, setDefault, skillSection } fro
|
|
|
68
68
|
import { saveFrame, listFrames, frameSection } from './frames.js'
|
|
69
69
|
import { readTweaks, applyTweaks, lintTweaks } from './tweaks.js'
|
|
70
70
|
import { readAnnotations, lintAnnotations, annotationFindings, annotationSection } from './annotations.js'
|
|
71
|
-
import {
|
|
71
|
+
import { createImpl } from './impl.js'
|
|
72
|
+
import { FRAMEWORKS, detectProject, projectFor, defaultFramework, buildHandoff, compareScreens, implUrl, implServe, defaultDest, pickedDest, within, pinRoots } from './handoff.js'
|
|
72
73
|
|
|
73
74
|
export const name = 'dsh-design-studio'
|
|
74
75
|
export const inject = ['webServer']
|
|
@@ -94,7 +95,7 @@ const SKILL_RANK = 600 // 和 DSH 自带的技能同一档;用户 / 项目里
|
|
|
94
95
|
* 几行之间靠这个互通(同一个包里的模块 Node 只加载一次):工作台起来后放 tracker、无头浏览器(browser);
|
|
95
96
|
* 「自动打开预览」那行开着时 autoOpen > 0
|
|
96
97
|
*/
|
|
97
|
-
export const studio = { tracker: null, autoOpen: 0, offline: 0, annotations: 0, check: null, browser: null, market: null, photos: null, sessionCwd: null, log: () => {} }
|
|
98
|
+
export const studio = { tracker: null, impl: null, openPreview: null, autoOpen: 0, offline: 0, annotations: 0, check: null, browser: null, market: null, photos: null, sessionCwd: null, log: () => {} }
|
|
98
99
|
|
|
99
100
|
/** 设计技能:模型做界面设计时加载(DSH 的 skill 工具),也能让用户手动调 */
|
|
100
101
|
export const designSkill = {
|
|
@@ -676,13 +677,15 @@ export const compareTool = {
|
|
|
676
677
|
name: 'compare_design',
|
|
677
678
|
description:
|
|
678
679
|
'把做出来的页面和右侧「设计工作台」里的设计稿比一比:按同样尺寸在无头浏览器里各截一张,逐像素比,回一张「设计稿 | 做出来的 | 差异」拼图(差得多的地方红框标号)、差了百分之几,和每块差异在两边分别是哪个元素。' +
|
|
679
|
-
'照设计稿写代码时每做完一屏调一次(url 给开发服务器上那一页的地址,如 http://localhost:5173/login),照差异改到 3% 以下。at=bottom 比滚到底的样子。要先用 preview_page 打开过设计。'
|
|
680
|
+
'照设计稿写代码时每做完一屏调一次(url 给开发服务器上那一页的地址,如 http://localhost:5173/login),照差异改到 3% 以下。at=bottom 比滚到底的样子。要先用 preview_page 打开过设计。' +
|
|
681
|
+
'设计稿套了外框(DSH 插件、外框库)时只比外框里的 main 槽位:外框是宿主画的,做出来的页面就是槽位那块本身,会按槽位的尺寸打开。',
|
|
680
682
|
parameters: specToJsonSchema({
|
|
681
683
|
url: { type: 'string', required: true, description: '做出来的页面:本机地址(http://localhost:5173/login,或端口 5173)或本地 HTML 文件' },
|
|
682
684
|
screen: { type: 'string', description: '和设计稿的哪一屏比(文件名,如 login 或 login.html);不填用右侧正在看的那屏' },
|
|
683
685
|
size: { type: 'string', description: '尺寸,如 375x667 或只写宽度;不填按设计的尺寸' },
|
|
684
686
|
at: { type: 'string', enum: ['top', 'bottom'], description: 'top 顶上一屏(默认)、bottom 滚到底' },
|
|
685
687
|
dark: { type: 'boolean', description: '比深色' },
|
|
688
|
+
whole: { type: 'boolean', description: '设计稿套了外框时默认只比 main 槽位;做出来的页面自己也套了同一个外框(整屏和设计稿一样大)才填 true,整屏比' },
|
|
686
689
|
}),
|
|
687
690
|
output: {
|
|
688
691
|
schema: {
|
|
@@ -696,34 +699,93 @@ export const compareTool = {
|
|
|
696
699
|
async execute(args, exec, ctx) {
|
|
697
700
|
const id = toolSession(exec, '比对')
|
|
698
701
|
const a = args || {}
|
|
699
|
-
const
|
|
700
|
-
if (!
|
|
701
|
-
|
|
702
|
-
if (!
|
|
703
|
-
const
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
if (!want) return { text: `认不出尺寸「${String(a.size).slice(0, 20)}」:写成 375x667,或只写宽度 375。` }
|
|
707
|
-
// 指定了哪屏就只认那屏,找不到就说(别悄悄拿第一屏比,结果对不上号);没指定才用正在看的 / 第一屏
|
|
708
|
-
const asked = typeof a.screen === 'string' && a.screen.trim()
|
|
709
|
-
const pick = pickScreens(snap, asked ? [a.screen] : [snap.focus], 1)
|
|
710
|
-
const sc = pick.list[0] || (asked ? null : pickScreens(snap, null, 1).list[0])
|
|
711
|
-
if (!sc) return { text: `没找到设计稿的这一屏:${asked ? a.screen : '(没有屏)'}。现在有:${(snap.screens || []).map((x) => x.key).join('、')}` }
|
|
712
|
-
let impl
|
|
713
|
-
try { impl = implUrl(a.url, studio.tracker.cwdOf(id)) } catch (e) { return { text: e.message } }
|
|
714
|
-
if (snap.origin && impl.startsWith(snap.origin + '/')) return { text: '这个地址是设计稿自己(设计工作台的预览),给做出来的那个项目的地址。' }
|
|
715
|
-
let r
|
|
716
|
-
try {
|
|
717
|
-
r = await compareScreens(b, { design: sc, impl, size: want, at: a.at === 'bottom' ? 'bottom' : 'top', dark: !!a.dark })
|
|
718
|
-
} catch (e) {
|
|
719
|
-
return { text: `没比成:${String((e && e.message) || e).slice(0, 200)}(做出来的页面打得开吗?开发服务器起来了吗?)` }
|
|
720
|
-
}
|
|
721
|
-
const kept = await keepImages(ctx, id, [{ key: `compare-${sc.key}`, jpg: r.jpg }], true)
|
|
722
|
-
if (kept.images) return { text: r.text, images: kept.images }
|
|
723
|
-
return { text: `${r.text}\n(这个 DSH 版本没法把图直接给你,存成了文件:${kept.files.join('、')})` }
|
|
702
|
+
const out = await runCompare(id, a)
|
|
703
|
+
if (!out.r) return { text: out.text }
|
|
704
|
+
keepImpl(id, out, 'model', !!a.size)
|
|
705
|
+
if (!out.r.jpg) return { text: out.r.text } // 没截图就停下了(套了外框但槽位露不出来)
|
|
706
|
+
const kept = await keepImages(ctx, id, [{ key: `compare-${out.sc.key}`, jpg: out.r.jpg }], true)
|
|
707
|
+
if (kept.images) return { text: out.r.text, images: kept.images }
|
|
708
|
+
return { text: `${out.r.text}\n(这个 DSH 版本没法把图直接给你,存成了文件:${kept.files.join('、')})` }
|
|
724
709
|
},
|
|
725
710
|
}
|
|
726
711
|
|
|
712
|
+
/** 设计记在哪个名下:本地设计按目录,开发服务器的按端口 */
|
|
713
|
+
/** 填的本地文件就是设计稿自己的某一屏(设计目录里登记的那几个文件):不算做出来的 */
|
|
714
|
+
export const isDesignFile = (at, u) => {
|
|
715
|
+
if (u.protocol !== 'file:' || !at.snap.target || at.snap.target.kind !== 'file') return false
|
|
716
|
+
try {
|
|
717
|
+
const rel = path.relative(path.resolve(at.root), fileURLToPath(u)).split(path.sep).join('/')
|
|
718
|
+
return !rel.startsWith('..') && (at.snap.screens || []).some((x) => x.key === rel)
|
|
719
|
+
} catch { return false }
|
|
720
|
+
}
|
|
721
|
+
export const implRoot = (snap) => (snap && snap.target ? (snap.target.kind === 'file' ? snap.target.root : `proxy:${snap.target.port}`) : '')
|
|
722
|
+
|
|
723
|
+
/**
|
|
724
|
+
* 比一屏(compare_design 和右侧栏「和做出来的比」里的「重新比」共用)
|
|
725
|
+
* a: { url, screen, size, at, dark, whole }
|
|
726
|
+
* @returns { text } 没比成;{ r, sc, impl, want, opts, root } 比完了(r.jpg 为空 = 套了外框但槽位露不出来,r.text 说了怎么办;
|
|
727
|
+
* root:开始比时在看的设计,比的时候换了设计也记回这一份)
|
|
728
|
+
*/
|
|
729
|
+
async function runCompare(id, a) {
|
|
730
|
+
const snap = studio.tracker.snapshot(id)
|
|
731
|
+
const root = implRoot(snap)
|
|
732
|
+
if (!snap.url) return { text: '还没有在预览的设计稿:先用 preview_page 打开设计文件,再比。' }
|
|
733
|
+
const b = studio.browser
|
|
734
|
+
if (!b || !b.available()) return { text: '这台电脑上没找到 Chrome / Edge(比对要用无头浏览器截图),比不了。' }
|
|
735
|
+
const design = designFor(id)
|
|
736
|
+
const { kind } = design
|
|
737
|
+
const want = Array.isArray(a.size) ? a.size : a.size ? sizeOf(a.size, kind) : await fitted(b, design, typeof a.screen === 'string' && a.screen.trim() ? [a.screen] : null)
|
|
738
|
+
if (!want) return { text: `认不出尺寸「${String(a.size).slice(0, 20)}」:写成 375x667,或只写宽度 375。` }
|
|
739
|
+
// 指定了哪屏就只认那屏,找不到就说(别悄悄拿第一屏比,结果对不上号);没指定才用正在看的 / 第一屏
|
|
740
|
+
const asked = typeof a.screen === 'string' && a.screen.trim()
|
|
741
|
+
const pick = pickScreens(snap, asked ? [a.screen] : [snap.focus], 1)
|
|
742
|
+
const sc = pick.list[0] || (asked ? null : pickScreens(snap, null, 1).list[0])
|
|
743
|
+
if (!sc) return { text: `没找到设计稿的这一屏:${asked ? a.screen : '(没有屏)'}。现在有:${(snap.screens || []).map((x) => x.key).join('、')}` }
|
|
744
|
+
// 相对路径、本地 HTML 的预览根:先问 DSH 会话目录(和右侧栏填地址一样),问不到才用记着的
|
|
745
|
+
let cwd = ''
|
|
746
|
+
try { cwd = studio.sessionCwd ? await studio.sessionCwd(id) : '' } catch { cwd = '' }
|
|
747
|
+
if (!cwd) cwd = studio.tracker.cwdOf(id)
|
|
748
|
+
let impl
|
|
749
|
+
try { impl = implUrl(a.url, cwd) } catch (e) { return { text: e.message } }
|
|
750
|
+
if ((snap.origin && impl.startsWith(snap.origin + '/')) || isDesignFile({ snap, root }, new URL(impl))) return { text: '这个地址是设计稿自己(设计工作台的预览或设计目录里的文件),给做出来的那个项目的地址。' }
|
|
751
|
+
const opts = { at: a.at === 'bottom' ? 'bottom' : 'top', dark: !!a.dark, whole: a.whole === true }
|
|
752
|
+
let r
|
|
753
|
+
try {
|
|
754
|
+
// 本地 HTML 经预览服务器(http)打开再截:直接开 file:// 时 Vite 这类打包出来的模块脚本、/assets 这种根路径都载不出来(和右侧栏里看到的一样)
|
|
755
|
+
let shotUrl = impl
|
|
756
|
+
if (/^file:/i.test(impl) && studio.openPreview) {
|
|
757
|
+
const u = new URL(impl)
|
|
758
|
+
const s = implServe(fileURLToPath(u), cwd)
|
|
759
|
+
const p = await studio.openPreview({ kind: 'file', root: s.root })
|
|
760
|
+
shotUrl = `${p.origin}${s.rel}${u.search}${u.hash}`
|
|
761
|
+
}
|
|
762
|
+
r = await compareScreens(b, { design: sc, impl: shotUrl, shown: impl, size: want, ...opts })
|
|
763
|
+
} catch (e) {
|
|
764
|
+
return { text: `没比成:${String((e && e.message) || e).slice(0, 200)}(做出来的页面打得开吗?开发服务器起来了吗?)` }
|
|
765
|
+
}
|
|
766
|
+
return { r, sc, impl, want, opts, root }
|
|
767
|
+
}
|
|
768
|
+
|
|
769
|
+
/** 比完记一笔(右侧栏「和做出来的比」显示分数、标红框);by:model 模型比的 / user 右侧栏点的 */
|
|
770
|
+
function keepImpl(id, out, by, sizeGiven) {
|
|
771
|
+
const store = studio.impl
|
|
772
|
+
const root = out.root
|
|
773
|
+
if (!store || !root || !out.sc) return null
|
|
774
|
+
const { r } = out
|
|
775
|
+
// 这次没比成分数(套了外框、槽位露不出来):地址没变就不动原来的记录(旧分数别标成这次比的)
|
|
776
|
+
if (typeof r.pct !== 'number') {
|
|
777
|
+
const cur = store.get(root, out.sc.key)
|
|
778
|
+
if (cur && cur.url === out.impl) return cur
|
|
779
|
+
return store.set(root, out.sc.key, { url: out.impl, ...out.opts, by })
|
|
780
|
+
}
|
|
781
|
+
const rec = store.set(root, out.sc.key, {
|
|
782
|
+
url: out.impl, ...out.opts, by, size: sizeGiven ? out.want : undefined,
|
|
783
|
+
...(typeof r.pct === 'number' ? { pct: r.pct, boxes: r.boxes, cmp: r.cmp, slot: r.slot } : {}),
|
|
784
|
+
})
|
|
785
|
+
if (r.jpg) store.shot(root, out.sc.key, r.jpg)
|
|
786
|
+
return rec
|
|
787
|
+
}
|
|
788
|
+
|
|
727
789
|
/** 工具结果显示成:文字 + 一张张图 */
|
|
728
790
|
function withImages(value) {
|
|
729
791
|
const blocks = [{ type: 'text', text: value.text }]
|
|
@@ -885,6 +947,7 @@ export function apply(ctx) {
|
|
|
885
947
|
},
|
|
886
948
|
})
|
|
887
949
|
ctx.effect(() => () => { drafts.clear(); hub.closeAll() }, 'dsh-design-studio: preview servers')
|
|
950
|
+
const implStore = createImpl({ log }) // 「和做出来的比」:每屏对应的页面地址、上次比对的分数
|
|
888
951
|
ctx.effect(() => {
|
|
889
952
|
studio.tracker = tracker
|
|
890
953
|
studio.log = log
|
|
@@ -893,7 +956,11 @@ export function apply(ctx) {
|
|
|
893
956
|
const photos = photoIndex ? createPhotos({ market, index: photoIndex, log }) : null
|
|
894
957
|
studio.photos = photos
|
|
895
958
|
studio.sessionCwd = sessionCwd
|
|
959
|
+
studio.impl = implStore
|
|
960
|
+
studio.openPreview = open
|
|
896
961
|
return () => {
|
|
962
|
+
if (studio.impl === implStore) { implStore.flush(); studio.impl = null }
|
|
963
|
+
if (studio.openPreview === open) studio.openPreview = null
|
|
897
964
|
if (studio.tracker === tracker) { studio.tracker = null; studio.log = () => {} }
|
|
898
965
|
if (studio.market === market) { market.abort(); studio.market = null }
|
|
899
966
|
if (studio.photos === photos) studio.photos = null
|
|
@@ -979,7 +1046,7 @@ export function apply(ctx) {
|
|
|
979
1046
|
let tweaks = null
|
|
980
1047
|
if (t && t.kind === 'file') { try { tweaks = readTweaks(t.root) } catch (e) { log(e) } }
|
|
981
1048
|
const snap = tracker.snapshot(session)
|
|
982
|
-
json(res, 200, { ...snap, strict: strictScreens(snap), phone: { on: ph.on, seen: ph.seen || 0 }, tweaks, ...annotsField(session, new URL(req.url, 'http://x').searchParams.get('an')), ...(c ? { checkReq: c.id } : {}) })
|
|
1049
|
+
json(res, 200, { ...snap, impl: implStore.list(implRoot(snap)), strict: strictScreens(snap), phone: { on: ph.on, seen: ph.seen || 0 }, tweaks, ...annotsField(session, new URL(req.url, 'http://x').searchParams.get('an')), ...(c ? { checkReq: c.id } : {}) })
|
|
983
1050
|
}
|
|
984
1051
|
|
|
985
1052
|
/** 正在看的几屏里,哪些是别人做的、没改过的(地址列表):右侧栏给它们的 iframe 加沙盒、不给剪贴板 */
|
|
@@ -1023,6 +1090,108 @@ export function apply(ctx) {
|
|
|
1023
1090
|
}
|
|
1024
1091
|
}
|
|
1025
1092
|
|
|
1093
|
+
// ── 和做出来的比(#25)────────────────────────────────────────────────────
|
|
1094
|
+
|
|
1095
|
+
/** 这个会话正在看的设计里有这一屏:{ snap, root },没有给 null */
|
|
1096
|
+
const implScreen = (session, key) => {
|
|
1097
|
+
const snap = tracker.snapshot(session)
|
|
1098
|
+
const root = implRoot(snap)
|
|
1099
|
+
if (!root || !key || !(snap.screens || []).some((x) => x.key === key)) return null
|
|
1100
|
+
return { snap, root }
|
|
1101
|
+
}
|
|
1102
|
+
/** 填的本地文件就是设计稿里的某一屏(拿设计稿和自己比) */
|
|
1103
|
+
|
|
1104
|
+
/**
|
|
1105
|
+
* POST { session, screen, url?, whole? } → { url, live, rec }
|
|
1106
|
+
* 填了地址(或改了整屏 / 只比槽位)先记下;再按记着的地址开一个预览服务器(开发服务器走代理、热更新照常;本地 HTML 按目录开),
|
|
1107
|
+
* 页面里注入设计台脚本(两边滚动同步)。地址只认本机的,不能是设计稿自己
|
|
1108
|
+
*/
|
|
1109
|
+
async function implRoute(req, res) {
|
|
1110
|
+
if (!admit(req, res)) return
|
|
1111
|
+
if (req.method !== 'POST') return json(res, 405, { error: 'method not allowed' })
|
|
1112
|
+
let body
|
|
1113
|
+
try { body = await readBody(req) } catch (e) { return json(res, 400, { error: e.message }) }
|
|
1114
|
+
const session = String(body.session || '')
|
|
1115
|
+
const key = String(body.screen || '')
|
|
1116
|
+
const at = implScreen(session, key)
|
|
1117
|
+
if (!at) return json(res, 404, { error: 'no-screen' })
|
|
1118
|
+
let rec = implStore.get(at.root, key)
|
|
1119
|
+
let url = rec ? rec.url : ''
|
|
1120
|
+
// 相对路径按会话目录算(新会话也问 DSH 要,和「打开本地设计」一样);本地 HTML 的预览根也不出会话目录
|
|
1121
|
+
// 用户填的:按 DSH 里这个会话的目录算(模型可能在子目录跑过工具,记着的不一定是会话目录)
|
|
1122
|
+
let cwd = ''
|
|
1123
|
+
try { cwd = await dshCwd(session) } catch { cwd = '' }
|
|
1124
|
+
if (!cwd) cwd = tracker.cwdOf(session)
|
|
1125
|
+
if (typeof body.url === 'string' && body.url.trim()) {
|
|
1126
|
+
try { url = implUrl(body.url.trim().slice(0, 2000), cwd) } catch (e) { return json(res, 400, { error: 'bad-url', msg: e.message }) }
|
|
1127
|
+
}
|
|
1128
|
+
if (!url) return json(res, 404, { error: 'no-url' })
|
|
1129
|
+
let u = null
|
|
1130
|
+
try { u = new URL(url) } catch { /* 记录文件被手改坏了 */ }
|
|
1131
|
+
if (!u || !/^(https?|file):$/.test(u.protocol)) return json(res, 400, { error: 'bad-url', msg: '' })
|
|
1132
|
+
const port = Number(u.port || (u.protocol === 'https:' ? 443 : 80))
|
|
1133
|
+
// 设计稿自己(预览地址、或设计目录里的那几屏文件)、设计台别的预览页、DSH 自己的网页:都不是做出来的项目
|
|
1134
|
+
const own = Number(ownHost(ctx).split(':')[1]) || 0
|
|
1135
|
+
if ((at.snap.origin && url.startsWith(at.snap.origin + '/')) || (u.protocol !== 'file:' && (previewPorts.has(port) || port === own)) || isDesignFile(at, u)) return json(res, 400, { error: 'self' })
|
|
1136
|
+
if (u.protocol === 'https:') return json(res, 400, { error: 'https' }) // 代理只会接 http 的开发服务器
|
|
1137
|
+
if (url !== (rec && rec.url) || (typeof body.whole === 'boolean' && body.whole !== (rec && rec.whole))) {
|
|
1138
|
+
rec = implStore.set(at.root, key, { url, by: 'user', ...(typeof body.whole === 'boolean' ? { whole: body.whole } : {}) })
|
|
1139
|
+
}
|
|
1140
|
+
let live
|
|
1141
|
+
try {
|
|
1142
|
+
if (u.protocol === 'file:') {
|
|
1143
|
+
const s = implServe(fileURLToPath(u), cwd)
|
|
1144
|
+
const r = await open({ kind: 'file', root: s.root })
|
|
1145
|
+
live = `${r.origin}${s.rel}${u.search}${u.hash}`
|
|
1146
|
+
} else {
|
|
1147
|
+
const r = await open({ kind: 'proxy', port })
|
|
1148
|
+
live = r.origin + u.pathname + u.search + u.hash
|
|
1149
|
+
}
|
|
1150
|
+
} catch (e) {
|
|
1151
|
+
log(e)
|
|
1152
|
+
return json(res, 500, { error: 'open', msg: String((e && e.message) || e).slice(0, 200) })
|
|
1153
|
+
}
|
|
1154
|
+
json(res, 200, { url, live, rec })
|
|
1155
|
+
}
|
|
1156
|
+
|
|
1157
|
+
/** POST { session, screen, size, dark } → 照记着的地址在无头浏览器里比一次(和 compare_design 一样),记下分数 → { rec, text } */
|
|
1158
|
+
async function implCmpRoute(req, res) {
|
|
1159
|
+
if (!admit(req, res)) return
|
|
1160
|
+
if (req.method !== 'POST') return json(res, 405, { error: 'method not allowed' })
|
|
1161
|
+
let body
|
|
1162
|
+
try { body = await readBody(req) } catch (e) { return json(res, 400, { error: e.message }) }
|
|
1163
|
+
const session = String(body.session || '')
|
|
1164
|
+
const key = String(body.screen || '')
|
|
1165
|
+
const at = implScreen(session, key)
|
|
1166
|
+
if (!at) return json(res, 404, { error: 'no-screen' })
|
|
1167
|
+
const rec = implStore.get(at.root, key)
|
|
1168
|
+
if (!rec) return json(res, 404, { error: 'no-url' })
|
|
1169
|
+
// 眼前看的不是记录里的地址了(模型刚换了地址、画面还没跟上):不比,等画面换过去
|
|
1170
|
+
if (typeof body.url === 'string' && body.url && body.url !== rec.url) return json(res, 200, { error: 'stale' })
|
|
1171
|
+
// 画幅能设 100–8000(自定义画幅):照眼前的比,不走模型那边 200 起的尺寸规矩
|
|
1172
|
+
const m = typeof body.size === 'string' ? /^(\d{2,5})x(\d{2,5})$/.exec(body.size) : null
|
|
1173
|
+
const size = m && +m[1] >= 100 && +m[2] >= 100 && +m[1] <= 8000 && +m[2] <= 8000 ? [+m[1], +m[2]] : rec.size ? rec.size.join('x') : ''
|
|
1174
|
+
const out = await runCompare(session, { url: rec.url, screen: key, size, at: rec.at, dark: typeof body.dark === 'boolean' ? body.dark : rec.dark, whole: rec.whole })
|
|
1175
|
+
if (!out.r) return json(res, 200, { error: 'failed', text: out.text })
|
|
1176
|
+
// 比的这会儿换了设计、或这屏改填了别的地址:这次的分数不算数,别写回去
|
|
1177
|
+
const now = implStore.get(at.root, key)
|
|
1178
|
+
if (out.root !== at.root || implRoot(tracker.snapshot(session)) !== at.root || !now || now.url !== rec.url) return json(res, 200, { error: 'stale' })
|
|
1179
|
+
const next = keepImpl(session, out, 'user', !!size)
|
|
1180
|
+
json(res, 200, { rec: next, text: out.r.text, ok: !!out.r.jpg })
|
|
1181
|
+
}
|
|
1182
|
+
|
|
1183
|
+
/** GET ?session=&screen= → 最近一次比对的拼图(设计稿 | 做出来的 | 差异) */
|
|
1184
|
+
function implShotRoute(req, res) {
|
|
1185
|
+
if (!admit(req, res)) return
|
|
1186
|
+
const q = new URL(req.url, 'http://x').searchParams
|
|
1187
|
+
const key = q.get('screen') || ''
|
|
1188
|
+
const at = implScreen(q.get('session') || '', key)
|
|
1189
|
+
const jpg = at ? implStore.shot(at.root, key) : null
|
|
1190
|
+
if (!jpg) return json(res, 404, { error: 'none' })
|
|
1191
|
+
res.writeHead(200, { 'Content-Type': 'image/jpeg', 'Content-Length': jpg.length, 'Cache-Control': 'no-store' })
|
|
1192
|
+
res.end(jpg)
|
|
1193
|
+
}
|
|
1194
|
+
|
|
1026
1195
|
/** 外框库:GET 列出来(首页「套外框」下面挑外框用) */
|
|
1027
1196
|
function framesRoute(req, res) {
|
|
1028
1197
|
if (!admit(req, res)) return
|
|
@@ -1560,6 +1729,10 @@ export function apply(ctx) {
|
|
|
1560
1729
|
async function sessionCwd(id) {
|
|
1561
1730
|
const known = tracker.cwdOf(id)
|
|
1562
1731
|
if (known && path.resolve(known) !== path.resolve(os.homedir())) return known
|
|
1732
|
+
return (await dshCwd(id)) || known
|
|
1733
|
+
}
|
|
1734
|
+
/** DSH 里这个会话的目录(会话头里记的);问不到回空 */
|
|
1735
|
+
async function dshCwd(id) {
|
|
1563
1736
|
const ok = (c) => typeof c === 'string' && path.isAbsolute(c) ? c : ''
|
|
1564
1737
|
try {
|
|
1565
1738
|
const ss = ctx && typeof ctx.get === 'function' ? ctx.get('sessions') : null
|
|
@@ -1575,7 +1748,7 @@ export function apply(ctx) {
|
|
|
1575
1748
|
if (c) return c
|
|
1576
1749
|
}
|
|
1577
1750
|
} catch (e) { log(e) }
|
|
1578
|
-
return
|
|
1751
|
+
return ''
|
|
1579
1752
|
}
|
|
1580
1753
|
const lib = createLibraryRoutes({ admit, readBody, json, tracker, library, inspiration, studio, open, storeLoad: fileStore.load, version: VERSION, exported, log, sessionCwd, vocab: () => market.vocab() })
|
|
1581
1754
|
// 画布上睡着的屏的缩略图(issue #17)
|
|
@@ -1591,7 +1764,7 @@ export function apply(ctx) {
|
|
|
1591
1764
|
})
|
|
1592
1765
|
const mk = createMarketRoutes({ admit, readBody, json, tracker, library, market, open, systemsDir: SYSTEMS_DIR, log, sessionCwd })
|
|
1593
1766
|
|
|
1594
|
-
const routes = { works: lib.works, workthumb: lib.workthumb, scrthumb: canvasThumbs.route, pack: lib.pack, usezip: lib.usezip, ghauth: lib.ghauth, submit: lib.submit, inspire: lib.inspire, inspimg: lib.inspimg, ...mk, state, open: openRoute, view: viewRoute, close: closeRoute, history: historyRoute, restore: restoreRoute, edit: editRoute, style: styleRoute, bundle: bundleRoute, ref: refRoute, export: exportRoute, doc: docRoute, reveal: revealRoute, phone: phoneRoute, check: checkRoute, locate: locateRoute, tweak: tweakRoute, systems: systemsRoute, frames: framesRoute, systhumb: systhumbRoute, project: projectRoute }
|
|
1767
|
+
const routes = { works: lib.works, workthumb: lib.workthumb, scrthumb: canvasThumbs.route, pack: lib.pack, usezip: lib.usezip, ghauth: lib.ghauth, submit: lib.submit, inspire: lib.inspire, inspimg: lib.inspimg, ...mk, state, open: openRoute, view: viewRoute, close: closeRoute, history: historyRoute, restore: restoreRoute, edit: editRoute, style: styleRoute, bundle: bundleRoute, ref: refRoute, export: exportRoute, doc: docRoute, reveal: revealRoute, phone: phoneRoute, check: checkRoute, locate: locateRoute, tweak: tweakRoute, systems: systemsRoute, frames: framesRoute, systhumb: systhumbRoute, project: projectRoute, impl: implRoute, implcmp: implCmpRoute, implshot: implShotRoute }
|
|
1595
1768
|
for (const [key, handler] of Object.entries(routes)) {
|
|
1596
1769
|
ctx.effect(() => ctx.webServer.register({
|
|
1597
1770
|
kind: 'exact',
|
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
|
-
|
|
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
|
+
})()`
|