dsh-design-studio 0.25.3 → 0.27.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 +1 -1
- package/README.md +1 -1
- package/lib/browser.js +19 -0
- package/lib/client.js +574 -34
- package/lib/handoff.js +73 -42
- package/lib/htmlpack.js +693 -0
- package/lib/impl.js +123 -0
- package/lib/index.js +279 -38
- package/lib/picker.js +136 -0
- package/lib/serve.js +4 -2
- package/lib/slot.js +37 -0
- package/package.json +1 -1
package/lib/impl.js
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 「和做出来的比」(#25):设计稿的每一屏对应做出来的哪个页面、上次比对差了多少
|
|
3
|
+
*
|
|
4
|
+
* - 模型调 compare_design、用户在右侧栏填地址 / 点「重新比」都记一笔:地址、整屏还是只比槽位、深色、尺寸、差异 %、红框
|
|
5
|
+
* - 按设计目录分开记,存在 ~/.dsh/cache/dsh-design-studio/impl.json(重开 DSH 还在);最近的拼图只留在内存里
|
|
6
|
+
* - 右侧栏轮询的状态里带上正在看的设计那份(list),对比视图据此打开真页面、显示分数
|
|
7
|
+
*/
|
|
8
|
+
import fs from 'node:fs'
|
|
9
|
+
import os from 'node:os'
|
|
10
|
+
import path from 'node:path'
|
|
11
|
+
|
|
12
|
+
export const IMPL_FILE = path.join(os.homedir(), '.dsh', 'cache', 'dsh-design-studio', 'impl.json')
|
|
13
|
+
const MAX_ROOTS = 100 // 最多记这么多个设计目录(最久没碰的先丢)
|
|
14
|
+
const MAX_KEYS = 60 // 一个设计最多这么多屏(和屏数上限一样)
|
|
15
|
+
const MAX_SHOTS = 24 // 内存里留几张拼图
|
|
16
|
+
|
|
17
|
+
const num = (v) => (typeof v === 'number' && Number.isFinite(v) ? v : null)
|
|
18
|
+
const box = (b) => (b && typeof b === 'object' ? { x: Math.round(num(b.x) || 0), y: Math.round(num(b.y) || 0), w: Math.round(num(b.w) || 0), h: Math.round(num(b.h) || 0) } : null)
|
|
19
|
+
|
|
20
|
+
/** 一屏的记录:只留认得的字段(文件可能被手改过) */
|
|
21
|
+
export function cleanRec(r) {
|
|
22
|
+
if (!r || typeof r !== 'object' || typeof r.url !== 'string' || !r.url || r.url.length > 2000) return null
|
|
23
|
+
const out = { url: r.url, whole: r.whole === true, dark: r.dark === true, at: r.at === 'bottom' ? 'bottom' : 'top', by: r.by === 'user' ? 'user' : 'model', when: num(r.when) || 0 }
|
|
24
|
+
if (Array.isArray(r.size) && r.size.length === 2 && r.size.every((n) => Number.isInteger(n) && n > 0 && n < 20000)) out.size = [r.size[0], r.size[1]]
|
|
25
|
+
if (num(r.pct) != null) {
|
|
26
|
+
out.pct = Math.round(r.pct * 10) / 10
|
|
27
|
+
out.boxes = (Array.isArray(r.boxes) ? r.boxes : []).slice(0, 6).map(box).filter((b) => b && b.w > 0 && b.h > 0)
|
|
28
|
+
if (Array.isArray(r.cmp) && r.cmp.length === 2 && r.cmp.every((n) => Number.isInteger(n) && n > 0)) out.cmp = [r.cmp[0], r.cmp[1]] // 比的那块多大(套了外框是槽位)
|
|
29
|
+
out.slot = r.slot === true
|
|
30
|
+
}
|
|
31
|
+
return out
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export function createImpl({ file = IMPL_FILE, log = () => {}, now = Date.now } = {}) {
|
|
35
|
+
let data = null // { [设计目录]: { used, screens: { [屏]: 记录 } } }
|
|
36
|
+
const shots = new Map() // `${目录}\n${屏}` → jpg(Buffer)
|
|
37
|
+
let timer = null
|
|
38
|
+
|
|
39
|
+
function load() {
|
|
40
|
+
if (data) return data
|
|
41
|
+
try { data = JSON.parse(fs.readFileSync(file, 'utf8')) } catch { data = {} }
|
|
42
|
+
if (!data || typeof data !== 'object' || Array.isArray(data)) data = {}
|
|
43
|
+
return data
|
|
44
|
+
}
|
|
45
|
+
function flush() {
|
|
46
|
+
clearTimeout(timer)
|
|
47
|
+
timer = null
|
|
48
|
+
if (!data) return
|
|
49
|
+
try {
|
|
50
|
+
fs.mkdirSync(path.dirname(file), { recursive: true })
|
|
51
|
+
const tmp = `${file}.${process.pid}.tmp`
|
|
52
|
+
fs.writeFileSync(tmp, JSON.stringify(data))
|
|
53
|
+
fs.renameSync(tmp, file)
|
|
54
|
+
} catch (e) { log(e) }
|
|
55
|
+
}
|
|
56
|
+
function save() {
|
|
57
|
+
clearTimeout(timer)
|
|
58
|
+
timer = setTimeout(flush, 400)
|
|
59
|
+
if (timer && typeof timer.unref === 'function') timer.unref()
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** 这个设计每屏记着的(只读的拷贝,认不出的不给) */
|
|
63
|
+
function list(root) {
|
|
64
|
+
const d = load()[root]
|
|
65
|
+
if (!d || !d.screens || typeof d.screens !== 'object') return {}
|
|
66
|
+
const out = {}
|
|
67
|
+
for (const [k, r] of Object.entries(d.screens)) {
|
|
68
|
+
const c = cleanRec(r)
|
|
69
|
+
if (c) out[k] = c
|
|
70
|
+
}
|
|
71
|
+
return out
|
|
72
|
+
}
|
|
73
|
+
const get = (root, key) => list(root)[key] || null
|
|
74
|
+
|
|
75
|
+
/** 记一笔:patch 合进去;换了地址就把旧分数清掉(那是别的页面的分) */
|
|
76
|
+
function set(root, key, patch) {
|
|
77
|
+
if (!root || !key || typeof key !== 'string' || key.length > 300) return null
|
|
78
|
+
const all = load()
|
|
79
|
+
const d = all[root] && typeof all[root] === 'object' ? all[root] : { screens: {} }
|
|
80
|
+
if (!d.screens || typeof d.screens !== 'object') d.screens = {}
|
|
81
|
+
const was = cleanRec(d.screens[key])
|
|
82
|
+
let next = { ...(was || {}), ...patch, when: now() }
|
|
83
|
+
if (was && patch.url && patch.url !== was.url && patch.pct === undefined) {
|
|
84
|
+
next = { ...next }
|
|
85
|
+
delete next.pct
|
|
86
|
+
delete next.boxes
|
|
87
|
+
delete next.cmp
|
|
88
|
+
delete next.slot
|
|
89
|
+
// 整屏比、比到底是给原来那个页面定的(比如原来的页面连外框一起做了):换了页面回到默认,除非这次说了
|
|
90
|
+
if (patch.whole === undefined) next.whole = false
|
|
91
|
+
if (patch.at === undefined) next.at = 'top'
|
|
92
|
+
shots.delete(`${root}\n${key}`)
|
|
93
|
+
}
|
|
94
|
+
const rec = cleanRec(next)
|
|
95
|
+
if (!rec) return null
|
|
96
|
+
d.screens[key] = rec
|
|
97
|
+
const keys = Object.keys(d.screens)
|
|
98
|
+
if (keys.length > MAX_KEYS) {
|
|
99
|
+
keys.sort((a, b) => (d.screens[a].when || 0) - (d.screens[b].when || 0))
|
|
100
|
+
for (const k of keys.slice(0, keys.length - MAX_KEYS)) delete d.screens[k]
|
|
101
|
+
}
|
|
102
|
+
d.used = now()
|
|
103
|
+
all[root] = d
|
|
104
|
+
const roots = Object.keys(all)
|
|
105
|
+
if (roots.length > MAX_ROOTS) {
|
|
106
|
+
roots.sort((a, b) => (all[a].used || 0) - (all[b].used || 0))
|
|
107
|
+
for (const r of roots.slice(0, roots.length - MAX_ROOTS)) delete all[r]
|
|
108
|
+
}
|
|
109
|
+
save()
|
|
110
|
+
return rec
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
function shot(root, key, jpg) {
|
|
114
|
+
const k = `${root}\n${key}`
|
|
115
|
+
if (jpg === undefined) return shots.get(k) || null
|
|
116
|
+
shots.delete(k)
|
|
117
|
+
if (jpg) shots.set(k, jpg)
|
|
118
|
+
while (shots.size > MAX_SHOTS) shots.delete(shots.keys().next().value)
|
|
119
|
+
return jpg
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
return { list, get, set, shot, flush }
|
|
123
|
+
}
|
package/lib/index.js
CHANGED
|
@@ -48,6 +48,7 @@ import os from 'node:os'
|
|
|
48
48
|
import { execFile } from 'node:child_process'
|
|
49
49
|
import { createHub, webOrigins, trustedOrigins, resolveInRoot, ERR_CATCHER, defang } from './serve.js'
|
|
50
50
|
import { bundle, inlinePicker, strictMeta, MAX_TOTAL as BUNDLE_MAX } from './bundle.js'
|
|
51
|
+
import { buildPack, PACK_SCREENS } from './htmlpack.js'
|
|
51
52
|
import { createLibrary } from './works.js'
|
|
52
53
|
import { createInspiration } from './inspire.js'
|
|
53
54
|
import { pickStyleText } from './styles.js'
|
|
@@ -68,7 +69,8 @@ import { SYSTEMS_DIR, extractSystem, listSystems, setDefault, skillSection } fro
|
|
|
68
69
|
import { saveFrame, listFrames, frameSection } from './frames.js'
|
|
69
70
|
import { readTweaks, applyTweaks, lintTweaks } from './tweaks.js'
|
|
70
71
|
import { readAnnotations, lintAnnotations, annotationFindings, annotationSection } from './annotations.js'
|
|
71
|
-
import {
|
|
72
|
+
import { createImpl } from './impl.js'
|
|
73
|
+
import { FRAMEWORKS, detectProject, projectFor, defaultFramework, buildHandoff, compareScreens, implUrl, implServe, defaultDest, pickedDest, within, pinRoots } from './handoff.js'
|
|
72
74
|
|
|
73
75
|
export const name = 'dsh-design-studio'
|
|
74
76
|
export const inject = ['webServer']
|
|
@@ -94,7 +96,7 @@ const SKILL_RANK = 600 // 和 DSH 自带的技能同一档;用户 / 项目里
|
|
|
94
96
|
* 几行之间靠这个互通(同一个包里的模块 Node 只加载一次):工作台起来后放 tracker、无头浏览器(browser);
|
|
95
97
|
* 「自动打开预览」那行开着时 autoOpen > 0
|
|
96
98
|
*/
|
|
97
|
-
export const studio = { tracker: null, autoOpen: 0, offline: 0, annotations: 0, check: null, browser: null, market: null, photos: null, sessionCwd: null, log: () => {} }
|
|
99
|
+
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
100
|
|
|
99
101
|
/** 设计技能:模型做界面设计时加载(DSH 的 skill 工具),也能让用户手动调 */
|
|
100
102
|
export const designSkill = {
|
|
@@ -698,35 +700,93 @@ export const compareTool = {
|
|
|
698
700
|
async execute(args, exec, ctx) {
|
|
699
701
|
const id = toolSession(exec, '比对')
|
|
700
702
|
const a = args || {}
|
|
701
|
-
const
|
|
702
|
-
if (!
|
|
703
|
-
|
|
704
|
-
if (!
|
|
705
|
-
const
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
if (!want) return { text: `认不出尺寸「${String(a.size).slice(0, 20)}」:写成 375x667,或只写宽度 375。` }
|
|
709
|
-
// 指定了哪屏就只认那屏,找不到就说(别悄悄拿第一屏比,结果对不上号);没指定才用正在看的 / 第一屏
|
|
710
|
-
const asked = typeof a.screen === 'string' && a.screen.trim()
|
|
711
|
-
const pick = pickScreens(snap, asked ? [a.screen] : [snap.focus], 1)
|
|
712
|
-
const sc = pick.list[0] || (asked ? null : pickScreens(snap, null, 1).list[0])
|
|
713
|
-
if (!sc) return { text: `没找到设计稿的这一屏:${asked ? a.screen : '(没有屏)'}。现在有:${(snap.screens || []).map((x) => x.key).join('、')}` }
|
|
714
|
-
let impl
|
|
715
|
-
try { impl = implUrl(a.url, studio.tracker.cwdOf(id)) } catch (e) { return { text: e.message } }
|
|
716
|
-
if (snap.origin && impl.startsWith(snap.origin + '/')) return { text: '这个地址是设计稿自己(设计工作台的预览),给做出来的那个项目的地址。' }
|
|
717
|
-
let r
|
|
718
|
-
try {
|
|
719
|
-
r = await compareScreens(b, { design: sc, impl, size: want, at: a.at === 'bottom' ? 'bottom' : 'top', dark: !!a.dark, whole: a.whole === true })
|
|
720
|
-
} catch (e) {
|
|
721
|
-
return { text: `没比成:${String((e && e.message) || e).slice(0, 200)}(做出来的页面打得开吗?开发服务器起来了吗?)` }
|
|
722
|
-
}
|
|
723
|
-
if (!r.jpg) return { text: r.text } // 没截图就停下了(套了外框但槽位露不出来)
|
|
724
|
-
const kept = await keepImages(ctx, id, [{ key: `compare-${sc.key}`, jpg: r.jpg }], true)
|
|
725
|
-
if (kept.images) return { text: r.text, images: kept.images }
|
|
726
|
-
return { text: `${r.text}\n(这个 DSH 版本没法把图直接给你,存成了文件:${kept.files.join('、')})` }
|
|
703
|
+
const out = await runCompare(id, a)
|
|
704
|
+
if (!out.r) return { text: out.text }
|
|
705
|
+
keepImpl(id, out, 'model', !!a.size)
|
|
706
|
+
if (!out.r.jpg) return { text: out.r.text } // 没截图就停下了(套了外框但槽位露不出来)
|
|
707
|
+
const kept = await keepImages(ctx, id, [{ key: `compare-${out.sc.key}`, jpg: out.r.jpg }], true)
|
|
708
|
+
if (kept.images) return { text: out.r.text, images: kept.images }
|
|
709
|
+
return { text: `${out.r.text}\n(这个 DSH 版本没法把图直接给你,存成了文件:${kept.files.join('、')})` }
|
|
727
710
|
},
|
|
728
711
|
}
|
|
729
712
|
|
|
713
|
+
/** 设计记在哪个名下:本地设计按目录,开发服务器的按端口 */
|
|
714
|
+
/** 填的本地文件就是设计稿自己的某一屏(设计目录里登记的那几个文件):不算做出来的 */
|
|
715
|
+
export const isDesignFile = (at, u) => {
|
|
716
|
+
if (u.protocol !== 'file:' || !at.snap.target || at.snap.target.kind !== 'file') return false
|
|
717
|
+
try {
|
|
718
|
+
const rel = path.relative(path.resolve(at.root), fileURLToPath(u)).split(path.sep).join('/')
|
|
719
|
+
return !rel.startsWith('..') && (at.snap.screens || []).some((x) => x.key === rel)
|
|
720
|
+
} catch { return false }
|
|
721
|
+
}
|
|
722
|
+
export const implRoot = (snap) => (snap && snap.target ? (snap.target.kind === 'file' ? snap.target.root : `proxy:${snap.target.port}`) : '')
|
|
723
|
+
|
|
724
|
+
/**
|
|
725
|
+
* 比一屏(compare_design 和右侧栏「和做出来的比」里的「重新比」共用)
|
|
726
|
+
* a: { url, screen, size, at, dark, whole }
|
|
727
|
+
* @returns { text } 没比成;{ r, sc, impl, want, opts, root } 比完了(r.jpg 为空 = 套了外框但槽位露不出来,r.text 说了怎么办;
|
|
728
|
+
* root:开始比时在看的设计,比的时候换了设计也记回这一份)
|
|
729
|
+
*/
|
|
730
|
+
async function runCompare(id, a) {
|
|
731
|
+
const snap = studio.tracker.snapshot(id)
|
|
732
|
+
const root = implRoot(snap)
|
|
733
|
+
if (!snap.url) return { text: '还没有在预览的设计稿:先用 preview_page 打开设计文件,再比。' }
|
|
734
|
+
const b = studio.browser
|
|
735
|
+
if (!b || !b.available()) return { text: '这台电脑上没找到 Chrome / Edge(比对要用无头浏览器截图),比不了。' }
|
|
736
|
+
const design = designFor(id)
|
|
737
|
+
const { kind } = design
|
|
738
|
+
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)
|
|
739
|
+
if (!want) return { text: `认不出尺寸「${String(a.size).slice(0, 20)}」:写成 375x667,或只写宽度 375。` }
|
|
740
|
+
// 指定了哪屏就只认那屏,找不到就说(别悄悄拿第一屏比,结果对不上号);没指定才用正在看的 / 第一屏
|
|
741
|
+
const asked = typeof a.screen === 'string' && a.screen.trim()
|
|
742
|
+
const pick = pickScreens(snap, asked ? [a.screen] : [snap.focus], 1)
|
|
743
|
+
const sc = pick.list[0] || (asked ? null : pickScreens(snap, null, 1).list[0])
|
|
744
|
+
if (!sc) return { text: `没找到设计稿的这一屏:${asked ? a.screen : '(没有屏)'}。现在有:${(snap.screens || []).map((x) => x.key).join('、')}` }
|
|
745
|
+
// 相对路径、本地 HTML 的预览根:先问 DSH 会话目录(和右侧栏填地址一样),问不到才用记着的
|
|
746
|
+
let cwd = ''
|
|
747
|
+
try { cwd = studio.sessionCwd ? await studio.sessionCwd(id) : '' } catch { cwd = '' }
|
|
748
|
+
if (!cwd) cwd = studio.tracker.cwdOf(id)
|
|
749
|
+
let impl
|
|
750
|
+
try { impl = implUrl(a.url, cwd) } catch (e) { return { text: e.message } }
|
|
751
|
+
if ((snap.origin && impl.startsWith(snap.origin + '/')) || isDesignFile({ snap, root }, new URL(impl))) return { text: '这个地址是设计稿自己(设计工作台的预览或设计目录里的文件),给做出来的那个项目的地址。' }
|
|
752
|
+
const opts = { at: a.at === 'bottom' ? 'bottom' : 'top', dark: !!a.dark, whole: a.whole === true }
|
|
753
|
+
let r
|
|
754
|
+
try {
|
|
755
|
+
// 本地 HTML 经预览服务器(http)打开再截:直接开 file:// 时 Vite 这类打包出来的模块脚本、/assets 这种根路径都载不出来(和右侧栏里看到的一样)
|
|
756
|
+
let shotUrl = impl
|
|
757
|
+
if (/^file:/i.test(impl) && studio.openPreview) {
|
|
758
|
+
const u = new URL(impl)
|
|
759
|
+
const s = implServe(fileURLToPath(u), cwd)
|
|
760
|
+
const p = await studio.openPreview({ kind: 'file', root: s.root })
|
|
761
|
+
shotUrl = `${p.origin}${s.rel}${u.search}${u.hash}`
|
|
762
|
+
}
|
|
763
|
+
r = await compareScreens(b, { design: sc, impl: shotUrl, shown: impl, size: want, ...opts })
|
|
764
|
+
} catch (e) {
|
|
765
|
+
return { text: `没比成:${String((e && e.message) || e).slice(0, 200)}(做出来的页面打得开吗?开发服务器起来了吗?)` }
|
|
766
|
+
}
|
|
767
|
+
return { r, sc, impl, want, opts, root }
|
|
768
|
+
}
|
|
769
|
+
|
|
770
|
+
/** 比完记一笔(右侧栏「和做出来的比」显示分数、标红框);by:model 模型比的 / user 右侧栏点的 */
|
|
771
|
+
function keepImpl(id, out, by, sizeGiven) {
|
|
772
|
+
const store = studio.impl
|
|
773
|
+
const root = out.root
|
|
774
|
+
if (!store || !root || !out.sc) return null
|
|
775
|
+
const { r } = out
|
|
776
|
+
// 这次没比成分数(套了外框、槽位露不出来):地址没变就不动原来的记录(旧分数别标成这次比的)
|
|
777
|
+
if (typeof r.pct !== 'number') {
|
|
778
|
+
const cur = store.get(root, out.sc.key)
|
|
779
|
+
if (cur && cur.url === out.impl) return cur
|
|
780
|
+
return store.set(root, out.sc.key, { url: out.impl, ...out.opts, by })
|
|
781
|
+
}
|
|
782
|
+
const rec = store.set(root, out.sc.key, {
|
|
783
|
+
url: out.impl, ...out.opts, by, size: sizeGiven ? out.want : undefined,
|
|
784
|
+
...(typeof r.pct === 'number' ? { pct: r.pct, boxes: r.boxes, cmp: r.cmp, slot: r.slot } : {}),
|
|
785
|
+
})
|
|
786
|
+
if (r.jpg) store.shot(root, out.sc.key, r.jpg)
|
|
787
|
+
return rec
|
|
788
|
+
}
|
|
789
|
+
|
|
730
790
|
/** 工具结果显示成:文字 + 一张张图 */
|
|
731
791
|
function withImages(value) {
|
|
732
792
|
const blocks = [{ type: 'text', text: value.text }]
|
|
@@ -888,6 +948,7 @@ export function apply(ctx) {
|
|
|
888
948
|
},
|
|
889
949
|
})
|
|
890
950
|
ctx.effect(() => () => { drafts.clear(); hub.closeAll() }, 'dsh-design-studio: preview servers')
|
|
951
|
+
const implStore = createImpl({ log }) // 「和做出来的比」:每屏对应的页面地址、上次比对的分数
|
|
891
952
|
ctx.effect(() => {
|
|
892
953
|
studio.tracker = tracker
|
|
893
954
|
studio.log = log
|
|
@@ -896,7 +957,11 @@ export function apply(ctx) {
|
|
|
896
957
|
const photos = photoIndex ? createPhotos({ market, index: photoIndex, log }) : null
|
|
897
958
|
studio.photos = photos
|
|
898
959
|
studio.sessionCwd = sessionCwd
|
|
960
|
+
studio.impl = implStore
|
|
961
|
+
studio.openPreview = open
|
|
899
962
|
return () => {
|
|
963
|
+
if (studio.impl === implStore) { implStore.flush(); studio.impl = null }
|
|
964
|
+
if (studio.openPreview === open) studio.openPreview = null
|
|
900
965
|
if (studio.tracker === tracker) { studio.tracker = null; studio.log = () => {} }
|
|
901
966
|
if (studio.market === market) { market.abort(); studio.market = null }
|
|
902
967
|
if (studio.photos === photos) studio.photos = null
|
|
@@ -982,7 +1047,7 @@ export function apply(ctx) {
|
|
|
982
1047
|
let tweaks = null
|
|
983
1048
|
if (t && t.kind === 'file') { try { tweaks = readTweaks(t.root) } catch (e) { log(e) } }
|
|
984
1049
|
const snap = tracker.snapshot(session)
|
|
985
|
-
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 } : {}) })
|
|
1050
|
+
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 } : {}) })
|
|
986
1051
|
}
|
|
987
1052
|
|
|
988
1053
|
/** 正在看的几屏里,哪些是别人做的、没改过的(地址列表):右侧栏给它们的 iframe 加沙盒、不给剪贴板 */
|
|
@@ -1026,6 +1091,108 @@ export function apply(ctx) {
|
|
|
1026
1091
|
}
|
|
1027
1092
|
}
|
|
1028
1093
|
|
|
1094
|
+
// ── 和做出来的比(#25)────────────────────────────────────────────────────
|
|
1095
|
+
|
|
1096
|
+
/** 这个会话正在看的设计里有这一屏:{ snap, root },没有给 null */
|
|
1097
|
+
const implScreen = (session, key) => {
|
|
1098
|
+
const snap = tracker.snapshot(session)
|
|
1099
|
+
const root = implRoot(snap)
|
|
1100
|
+
if (!root || !key || !(snap.screens || []).some((x) => x.key === key)) return null
|
|
1101
|
+
return { snap, root }
|
|
1102
|
+
}
|
|
1103
|
+
/** 填的本地文件就是设计稿里的某一屏(拿设计稿和自己比) */
|
|
1104
|
+
|
|
1105
|
+
/**
|
|
1106
|
+
* POST { session, screen, url?, whole? } → { url, live, rec }
|
|
1107
|
+
* 填了地址(或改了整屏 / 只比槽位)先记下;再按记着的地址开一个预览服务器(开发服务器走代理、热更新照常;本地 HTML 按目录开),
|
|
1108
|
+
* 页面里注入设计台脚本(两边滚动同步)。地址只认本机的,不能是设计稿自己
|
|
1109
|
+
*/
|
|
1110
|
+
async function implRoute(req, res) {
|
|
1111
|
+
if (!admit(req, res)) return
|
|
1112
|
+
if (req.method !== 'POST') return json(res, 405, { error: 'method not allowed' })
|
|
1113
|
+
let body
|
|
1114
|
+
try { body = await readBody(req) } catch (e) { return json(res, 400, { error: e.message }) }
|
|
1115
|
+
const session = String(body.session || '')
|
|
1116
|
+
const key = String(body.screen || '')
|
|
1117
|
+
const at = implScreen(session, key)
|
|
1118
|
+
if (!at) return json(res, 404, { error: 'no-screen' })
|
|
1119
|
+
let rec = implStore.get(at.root, key)
|
|
1120
|
+
let url = rec ? rec.url : ''
|
|
1121
|
+
// 相对路径按会话目录算(新会话也问 DSH 要,和「打开本地设计」一样);本地 HTML 的预览根也不出会话目录
|
|
1122
|
+
// 用户填的:按 DSH 里这个会话的目录算(模型可能在子目录跑过工具,记着的不一定是会话目录)
|
|
1123
|
+
let cwd = ''
|
|
1124
|
+
try { cwd = await dshCwd(session) } catch { cwd = '' }
|
|
1125
|
+
if (!cwd) cwd = tracker.cwdOf(session)
|
|
1126
|
+
if (typeof body.url === 'string' && body.url.trim()) {
|
|
1127
|
+
try { url = implUrl(body.url.trim().slice(0, 2000), cwd) } catch (e) { return json(res, 400, { error: 'bad-url', msg: e.message }) }
|
|
1128
|
+
}
|
|
1129
|
+
if (!url) return json(res, 404, { error: 'no-url' })
|
|
1130
|
+
let u = null
|
|
1131
|
+
try { u = new URL(url) } catch { /* 记录文件被手改坏了 */ }
|
|
1132
|
+
if (!u || !/^(https?|file):$/.test(u.protocol)) return json(res, 400, { error: 'bad-url', msg: '' })
|
|
1133
|
+
const port = Number(u.port || (u.protocol === 'https:' ? 443 : 80))
|
|
1134
|
+
// 设计稿自己(预览地址、或设计目录里的那几屏文件)、设计台别的预览页、DSH 自己的网页:都不是做出来的项目
|
|
1135
|
+
const own = Number(ownHost(ctx).split(':')[1]) || 0
|
|
1136
|
+
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' })
|
|
1137
|
+
if (u.protocol === 'https:') return json(res, 400, { error: 'https' }) // 代理只会接 http 的开发服务器
|
|
1138
|
+
if (url !== (rec && rec.url) || (typeof body.whole === 'boolean' && body.whole !== (rec && rec.whole))) {
|
|
1139
|
+
rec = implStore.set(at.root, key, { url, by: 'user', ...(typeof body.whole === 'boolean' ? { whole: body.whole } : {}) })
|
|
1140
|
+
}
|
|
1141
|
+
let live
|
|
1142
|
+
try {
|
|
1143
|
+
if (u.protocol === 'file:') {
|
|
1144
|
+
const s = implServe(fileURLToPath(u), cwd)
|
|
1145
|
+
const r = await open({ kind: 'file', root: s.root })
|
|
1146
|
+
live = `${r.origin}${s.rel}${u.search}${u.hash}`
|
|
1147
|
+
} else {
|
|
1148
|
+
const r = await open({ kind: 'proxy', port })
|
|
1149
|
+
live = r.origin + u.pathname + u.search + u.hash
|
|
1150
|
+
}
|
|
1151
|
+
} catch (e) {
|
|
1152
|
+
log(e)
|
|
1153
|
+
return json(res, 500, { error: 'open', msg: String((e && e.message) || e).slice(0, 200) })
|
|
1154
|
+
}
|
|
1155
|
+
json(res, 200, { url, live, rec })
|
|
1156
|
+
}
|
|
1157
|
+
|
|
1158
|
+
/** POST { session, screen, size, dark } → 照记着的地址在无头浏览器里比一次(和 compare_design 一样),记下分数 → { rec, text } */
|
|
1159
|
+
async function implCmpRoute(req, res) {
|
|
1160
|
+
if (!admit(req, res)) return
|
|
1161
|
+
if (req.method !== 'POST') return json(res, 405, { error: 'method not allowed' })
|
|
1162
|
+
let body
|
|
1163
|
+
try { body = await readBody(req) } catch (e) { return json(res, 400, { error: e.message }) }
|
|
1164
|
+
const session = String(body.session || '')
|
|
1165
|
+
const key = String(body.screen || '')
|
|
1166
|
+
const at = implScreen(session, key)
|
|
1167
|
+
if (!at) return json(res, 404, { error: 'no-screen' })
|
|
1168
|
+
const rec = implStore.get(at.root, key)
|
|
1169
|
+
if (!rec) return json(res, 404, { error: 'no-url' })
|
|
1170
|
+
// 眼前看的不是记录里的地址了(模型刚换了地址、画面还没跟上):不比,等画面换过去
|
|
1171
|
+
if (typeof body.url === 'string' && body.url && body.url !== rec.url) return json(res, 200, { error: 'stale' })
|
|
1172
|
+
// 画幅能设 100–8000(自定义画幅):照眼前的比,不走模型那边 200 起的尺寸规矩
|
|
1173
|
+
const m = typeof body.size === 'string' ? /^(\d{2,5})x(\d{2,5})$/.exec(body.size) : null
|
|
1174
|
+
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') : ''
|
|
1175
|
+
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 })
|
|
1176
|
+
if (!out.r) return json(res, 200, { error: 'failed', text: out.text })
|
|
1177
|
+
// 比的这会儿换了设计、或这屏改填了别的地址:这次的分数不算数,别写回去
|
|
1178
|
+
const now = implStore.get(at.root, key)
|
|
1179
|
+
if (out.root !== at.root || implRoot(tracker.snapshot(session)) !== at.root || !now || now.url !== rec.url) return json(res, 200, { error: 'stale' })
|
|
1180
|
+
const next = keepImpl(session, out, 'user', !!size)
|
|
1181
|
+
json(res, 200, { rec: next, text: out.r.text, ok: !!out.r.jpg })
|
|
1182
|
+
}
|
|
1183
|
+
|
|
1184
|
+
/** GET ?session=&screen= → 最近一次比对的拼图(设计稿 | 做出来的 | 差异) */
|
|
1185
|
+
function implShotRoute(req, res) {
|
|
1186
|
+
if (!admit(req, res)) return
|
|
1187
|
+
const q = new URL(req.url, 'http://x').searchParams
|
|
1188
|
+
const key = q.get('screen') || ''
|
|
1189
|
+
const at = implScreen(q.get('session') || '', key)
|
|
1190
|
+
const jpg = at ? implStore.shot(at.root, key) : null
|
|
1191
|
+
if (!jpg) return json(res, 404, { error: 'none' })
|
|
1192
|
+
res.writeHead(200, { 'Content-Type': 'image/jpeg', 'Content-Length': jpg.length, 'Cache-Control': 'no-store' })
|
|
1193
|
+
res.end(jpg)
|
|
1194
|
+
}
|
|
1195
|
+
|
|
1029
1196
|
/** 外框库:GET 列出来(首页「套外框」下面挑外框用) */
|
|
1030
1197
|
function framesRoute(req, res) {
|
|
1031
1198
|
if (!admit(req, res)) return
|
|
@@ -1418,16 +1585,22 @@ export function apply(ctx) {
|
|
|
1418
1585
|
}
|
|
1419
1586
|
|
|
1420
1587
|
/** 存进导出目录:文件名只留字母数字中文和 -,同名不覆盖(加 -2、-3 …) */
|
|
1421
|
-
function
|
|
1588
|
+
function writeExport(session, name, ext, buf) {
|
|
1422
1589
|
const base = String(name || 'design').replace(new RegExp(`\\.${ext}$`, 'i'), '').replace(/[^\w\u4e00-\u9fff-]+/g, '-').replace(/^-+|-+$/g, '').slice(0, 60) || 'design'
|
|
1423
1590
|
const dir = exportDir(session)
|
|
1424
|
-
|
|
1425
|
-
|
|
1426
|
-
|
|
1427
|
-
|
|
1428
|
-
fs.writeFileSync(file, buf)
|
|
1591
|
+
fs.mkdirSync(dir, { recursive: true })
|
|
1592
|
+
// 独占创建(wx):查名和写入之间别人刚好建了同名文件,也不会盖掉它,换下一个名字
|
|
1593
|
+
for (let i = 1; i < 1000; i++) {
|
|
1594
|
+
const file = path.join(dir, i === 1 ? `${base}.${ext}` : `${base}-${i}.${ext}`)
|
|
1595
|
+
try { fs.writeFileSync(file, buf, { flag: 'wx' }) } catch (e) { if (e.code === 'EEXIST') continue; throw e }
|
|
1429
1596
|
exported.add(file)
|
|
1430
|
-
|
|
1597
|
+
return file
|
|
1598
|
+
}
|
|
1599
|
+
throw new Error('too many files with this name')
|
|
1600
|
+
}
|
|
1601
|
+
function saveExport(res, session, name, ext, buf) {
|
|
1602
|
+
try {
|
|
1603
|
+
json(res, 200, { path: writeExport(session, name, ext, buf) })
|
|
1431
1604
|
} catch (e) {
|
|
1432
1605
|
json(res, 500, { error: e.message })
|
|
1433
1606
|
}
|
|
@@ -1486,6 +1659,70 @@ export function apply(ctx) {
|
|
|
1486
1659
|
saveExport(res, String(body.session || ''), body.name, format, buf)
|
|
1487
1660
|
}
|
|
1488
1661
|
|
|
1662
|
+
/**
|
|
1663
|
+
* 整套 HTML 演示(#26):本地文件的设计,所有屏打进一个 .html(见 htmlpack.js),存进 exports/。
|
|
1664
|
+
* 屏、版本、画幅、深浅色、外壳、调节滑块还没存的值都按右侧栏眼前的来;不截图,几秒出来
|
|
1665
|
+
*/
|
|
1666
|
+
async function htmlPackRoute(req, res) {
|
|
1667
|
+
if (!admit(req, res)) return
|
|
1668
|
+
if (req.method !== 'POST') return json(res, 405, { error: 'method not allowed' })
|
|
1669
|
+
let body
|
|
1670
|
+
try { body = await readBody(req, 1024 * 1024) } catch (e) { return json(res, 400, { error: e.message }) }
|
|
1671
|
+
const session = String(body.session || '')
|
|
1672
|
+
const st = tracker.stateOf(session)
|
|
1673
|
+
const t = st && st.target
|
|
1674
|
+
if (!t || t.kind !== 'file') return json(res, 400, { error: 'not-file' })
|
|
1675
|
+
if (targetMoved(res, session, body.target)) return
|
|
1676
|
+
const v = Math.max(0, Math.floor(Number(body.v) || 0))
|
|
1677
|
+
const okKey = (k) => typeof k === 'string' && /^[^\\]+\.html?$/i.test(k) && !k.split('/').some((p) => !p || p === '.' || p === '..')
|
|
1678
|
+
const seen = new Set()
|
|
1679
|
+
const screens = []
|
|
1680
|
+
for (const s of Array.isArray(body.screens) ? body.screens.slice(0, PACK_SCREENS) : []) {
|
|
1681
|
+
if (!s || !okKey(s.key) || seen.has(s.key)) continue
|
|
1682
|
+
seen.add(s.key)
|
|
1683
|
+
screens.push({ key: s.key, label: String(s.label || s.key).slice(0, 80), top: s.top === true })
|
|
1684
|
+
}
|
|
1685
|
+
if (!screens.length) return json(res, 400, { error: 'noScreens' })
|
|
1686
|
+
const kind = typeof body.kind === 'string' ? body.kind : 'web'
|
|
1687
|
+
const dim = (x, d) => (Number.isFinite(+x) && +x >= 100 && +x <= 8000 ? Math.round(+x) : d)
|
|
1688
|
+
const w = dim(body.w, 1280), h = dim(body.h, 800)
|
|
1689
|
+
const deps = new Set()
|
|
1690
|
+
const read = (rel) => {
|
|
1691
|
+
deps.add(rel)
|
|
1692
|
+
const f = bundleFile(t.root, v, rel)
|
|
1693
|
+
if (!f) return null
|
|
1694
|
+
const s = fs.statSync(f)
|
|
1695
|
+
return s.isFile() && s.size <= BUNDLE_MAX ? fs.readFileSync(f) : null
|
|
1696
|
+
}
|
|
1697
|
+
const strictOf = (page) => library.strictHtml(page) || market.strictHtml(page) || (v > 0 && library.strictVersions(t.root))
|
|
1698
|
+
const tw = body.tweak && typeof body.tweak === 'object' ? body.tweak : null
|
|
1699
|
+
const plain = (o) => Object.fromEntries(Object.entries(o && typeof o === 'object' ? o : {}).filter(([k, x]) => typeof k === 'string' && k.length < 80 && (typeof x === 'string' || typeof x === 'number' || x === null || typeof x === 'boolean')).slice(0, 200))
|
|
1700
|
+
let pack
|
|
1701
|
+
try {
|
|
1702
|
+
pack = buildPack({
|
|
1703
|
+
screens, read, strictOf, kind, w, h,
|
|
1704
|
+
os: body.os === 'win' ? 'win' : '',
|
|
1705
|
+
dark: body.dark === true,
|
|
1706
|
+
scheme: body.scheme === 'dark' ? 'dark' : 'light',
|
|
1707
|
+
tweak: tw ? { vars: plain(tw.vars), attrs: plain(tw.attrs) } : null,
|
|
1708
|
+
title: String(body.title || '').slice(0, 120),
|
|
1709
|
+
lang: body.lang === 'en' ? 'en' : 'zh',
|
|
1710
|
+
fit: body.fit === true,
|
|
1711
|
+
})
|
|
1712
|
+
} catch (e) {
|
|
1713
|
+
return json(res, e.code ? 400 : 500, { error: e.code || e.message })
|
|
1714
|
+
}
|
|
1715
|
+
// 打包时会话换了设计:别把 A 的演示存进 B 的 exports/
|
|
1716
|
+
if (targetMoved(res, session, body.target)) return
|
|
1717
|
+
const ai = new Set([...deps].filter((x) => isPhotoAsset(x)).map((x) => path.basename(x)))
|
|
1718
|
+
try {
|
|
1719
|
+
const file = writeExport(session, body.name, 'html', Buffer.from(pack.html, 'utf8'))
|
|
1720
|
+
json(res, 200, { path: file, count: pack.count, missing: pack.missing, ai: ai.size, bytes: pack.html.length })
|
|
1721
|
+
} catch (e) {
|
|
1722
|
+
json(res, 500, { error: e.message })
|
|
1723
|
+
}
|
|
1724
|
+
}
|
|
1725
|
+
|
|
1489
1726
|
/** 图片格式看文件头,不信文件名 */
|
|
1490
1727
|
function imageExt(buf) {
|
|
1491
1728
|
if (buf.length < 12) return ''
|
|
@@ -1563,6 +1800,10 @@ export function apply(ctx) {
|
|
|
1563
1800
|
async function sessionCwd(id) {
|
|
1564
1801
|
const known = tracker.cwdOf(id)
|
|
1565
1802
|
if (known && path.resolve(known) !== path.resolve(os.homedir())) return known
|
|
1803
|
+
return (await dshCwd(id)) || known
|
|
1804
|
+
}
|
|
1805
|
+
/** DSH 里这个会话的目录(会话头里记的);问不到回空 */
|
|
1806
|
+
async function dshCwd(id) {
|
|
1566
1807
|
const ok = (c) => typeof c === 'string' && path.isAbsolute(c) ? c : ''
|
|
1567
1808
|
try {
|
|
1568
1809
|
const ss = ctx && typeof ctx.get === 'function' ? ctx.get('sessions') : null
|
|
@@ -1578,7 +1819,7 @@ export function apply(ctx) {
|
|
|
1578
1819
|
if (c) return c
|
|
1579
1820
|
}
|
|
1580
1821
|
} catch (e) { log(e) }
|
|
1581
|
-
return
|
|
1822
|
+
return ''
|
|
1582
1823
|
}
|
|
1583
1824
|
const lib = createLibraryRoutes({ admit, readBody, json, tracker, library, inspiration, studio, open, storeLoad: fileStore.load, version: VERSION, exported, log, sessionCwd, vocab: () => market.vocab() })
|
|
1584
1825
|
// 画布上睡着的屏的缩略图(issue #17)
|
|
@@ -1594,7 +1835,7 @@ export function apply(ctx) {
|
|
|
1594
1835
|
})
|
|
1595
1836
|
const mk = createMarketRoutes({ admit, readBody, json, tracker, library, market, open, systemsDir: SYSTEMS_DIR, log, sessionCwd })
|
|
1596
1837
|
|
|
1597
|
-
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 }
|
|
1838
|
+
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, htmlpack: htmlPackRoute, reveal: revealRoute, phone: phoneRoute, check: checkRoute, locate: locateRoute, tweak: tweakRoute, systems: systemsRoute, frames: framesRoute, systhumb: systhumbRoute, project: projectRoute, impl: implRoute, implcmp: implCmpRoute, implshot: implShotRoute }
|
|
1598
1839
|
for (const [key, handler] of Object.entries(routes)) {
|
|
1599
1840
|
ctx.effect(() => ctx.webServer.register({
|
|
1600
1841
|
kind: 'exact',
|