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/handoff.js
CHANGED
|
@@ -13,13 +13,14 @@ import { PHOTO_PREFIX, isPhotoAsset } from './photos.js'
|
|
|
13
13
|
import fs from 'node:fs'
|
|
14
14
|
import os from 'node:os'
|
|
15
15
|
import path from 'node:path'
|
|
16
|
-
import { pathToFileURL } from 'node:url'
|
|
16
|
+
import { pathToFileURL, fileURLToPath } from 'node:url'
|
|
17
17
|
import { prepare } from './look.js'
|
|
18
18
|
import { allScreens } from './core.js'
|
|
19
19
|
import { readTweaks, shownValue } from './tweaks.js'
|
|
20
20
|
import { annotationsMd } from './annotations.js'
|
|
21
21
|
import { tokensOf } from './system.js'
|
|
22
22
|
import { FRAMES_DIR } from './frames.js'
|
|
23
|
+
import { FRAME_MAIN, SLOT_BOX } from './slot.js'
|
|
23
24
|
|
|
24
25
|
export const HANDOFF_DIR = 'handoff'
|
|
25
26
|
const MAX_SCREENS = 12
|
|
@@ -743,37 +744,6 @@ const IMPL_DARK = `new Promise((res) => {
|
|
|
743
744
|
setTimeout(() => res(true), 400)
|
|
744
745
|
})`
|
|
745
746
|
|
|
746
|
-
// 套了外框的设计(DSH 插件、外框库):外框是宿主画的,做出来的只有槽位 main 那块 → 只截这块比(#25)
|
|
747
|
-
// 返回整页坐标(截图 clip 用)和视口坐标(对元素块用);槽位太小或看不见给 null,照整屏比
|
|
748
|
-
// 量的是内容那块:槽位自己的边框、内边距是宿主留的白(DSH 设置分区左右 24px),真插件的页面从内容那块开始
|
|
749
|
-
// 外框根和体检一样认:第一个带 data-frame、里面有 data-slot 的元素,而且是登记过的(DSH 自带的四个,或页面里有外框库那段 <template id="dsh-frame">);
|
|
750
|
-
// 普通设计里碰巧写了 data-frame 的包裹层(手机示意框之类)不算
|
|
751
|
-
const FRAME_MAIN = `(() => {
|
|
752
|
-
const f = [...document.querySelectorAll('[data-frame]')].find((x) => x.querySelector('[data-slot]'))
|
|
753
|
-
if (!f) return null
|
|
754
|
-
if (!/^dsh\\/(sidebar|settings|view|panel)$/.test(f.getAttribute('data-frame') || '') && !document.getElementById('dsh-frame')) return null
|
|
755
|
-
return f.querySelector('[data-slot="main"]')
|
|
756
|
-
})()`
|
|
757
|
-
const SLOT_BOX = `(() => {
|
|
758
|
-
const el = ${FRAME_MAIN}
|
|
759
|
-
if (!el) return null
|
|
760
|
-
const r = el.getBoundingClientRect()
|
|
761
|
-
const s = getComputedStyle(el)
|
|
762
|
-
const n = (k) => parseFloat(s[k]) || 0
|
|
763
|
-
const left = r.left + n('borderLeftWidth') + n('paddingLeft'), top = r.top + n('borderTopWidth') + n('paddingTop')
|
|
764
|
-
const right = r.left + n('borderLeftWidth') + el.clientWidth - n('paddingRight'), bottom = r.top + n('borderTopWidth') + el.clientHeight - n('paddingBottom')
|
|
765
|
-
const vx = Math.max(0, Math.round(left)), vy = Math.max(0, Math.round(top))
|
|
766
|
-
const w = Math.min(Math.round(right), innerWidth) - vx, h = Math.min(Math.round(bottom), innerHeight) - vy
|
|
767
|
-
// 外框自己多大:页面开头类型标记里写的(外框库存的「framed 宽x高」),没写就量外框根元素(自带 DSH 外框固定 1280×800)
|
|
768
|
-
const mm = /(\\d{3,4})\\s*[x×]\\s*(\\d{3,4})/.exec((document.querySelector('meta[name="dsh-design"]') || {}).content || '')
|
|
769
|
-
const fr = el.closest('[data-frame]').getBoundingClientRect()
|
|
770
|
-
const fw = mm ? +mm[1] : Math.round(fr.width), fh = mm ? +mm[2] : Math.round(fr.height)
|
|
771
|
-
if (w < 40 || h < 40) return { hidden: true, fw, fh } // 认出了外框,但这个尺寸下槽位露不出来:别退回整屏比(会拿外框去比)
|
|
772
|
-
// 开的尺寸装不下整个槽位(只露出一部分):照样比露出来的,但要说一声
|
|
773
|
-
const part = w < Math.round(right - left) - 1 || h < Math.round(bottom - top) - 1
|
|
774
|
-
return { x: vx + Math.round(scrollX), y: vy + Math.round(scrollY), vx, vy, w, h, part, fw, fh }
|
|
775
|
-
})()`
|
|
776
|
-
|
|
777
747
|
/** 做出来的页面只许开本机的:localhost / 127.0.0.1 / ::1 的 http(s)、本地文件(跳转、脚本改地址也一样查) */
|
|
778
748
|
export function localOnly(u) {
|
|
779
749
|
let x
|
|
@@ -898,26 +868,84 @@ export function blockAt(blocks, bx) {
|
|
|
898
868
|
|
|
899
869
|
const describe = (b) => (b ? `${b.d}${b.t ? `「${b.t}」` : ''}` : '(空白处)')
|
|
900
870
|
|
|
871
|
+
/**
|
|
872
|
+
* 本地 HTML 经预览服务器开时,服务器的根放哪、页面在根下的地址。
|
|
873
|
+
* 默认就是 HTML 所在的文件夹(Vite 打包的 /assets 根路径要这样才对);
|
|
874
|
+
* 页面引用了上级目录(../css/app.css)就往上提几层,最多 3 层,不然那些资源 404、页面没样式。
|
|
875
|
+
* 往上提有边:只在会话目录里提、不出会话目录(会话目录外的 HTML 不提);无论如何不提到家目录(及更上面)——预览服务器能读的范围别一下变大。
|
|
876
|
+
* @returns { root, rel } rel 已按段编码,以 / 开头
|
|
877
|
+
*/
|
|
878
|
+
export function implServe(file, limit = '') {
|
|
879
|
+
let up = 0
|
|
880
|
+
try {
|
|
881
|
+
if (fs.lstatSync(file).isSymbolicLink()) throw new Error('link') // 软链接不读(可能指到目录外面):按所在文件夹
|
|
882
|
+
const fd = fs.openSync(file, 'r')
|
|
883
|
+
let html = ''
|
|
884
|
+
try { const buf = Buffer.alloc(1024 * 1024); html = buf.toString('utf8', 0, fs.readSync(fd, buf, 0, buf.length, 0)) } finally { fs.closeSync(fd) }
|
|
885
|
+
const re = /(?:\b(?:src|href|srcset|poster|data)\s*=\s*["']?|url\(\s*["']?|\bimport\s*(?:[^'"]*from\s*)?["']|\bimport\(\s*["'])((?:\.\.\/)+)/gi
|
|
886
|
+
let m
|
|
887
|
+
while ((m = re.exec(html))) up = Math.max(up, m[1].length / 3)
|
|
888
|
+
} catch { /* 读不了就按所在文件夹 */ }
|
|
889
|
+
let root = path.dirname(file)
|
|
890
|
+
const home = os.homedir()
|
|
891
|
+
const cap = limit && within(file, limit) ? limit : ''
|
|
892
|
+
if (!cap) up = 0
|
|
893
|
+
for (let i = 0; i < Math.min(up, 3); i++) {
|
|
894
|
+
const p = path.dirname(root)
|
|
895
|
+
if (p === root || within(home, p) || (cap && !within(p, cap))) break
|
|
896
|
+
root = p
|
|
897
|
+
}
|
|
898
|
+
const rel = path.relative(root, file).split(path.sep).map(encodeURIComponent).join('/')
|
|
899
|
+
return { root, rel: '/' + rel }
|
|
900
|
+
}
|
|
901
|
+
|
|
901
902
|
/** 做出来的页面地址:本机的 http(s),或本地 HTML 文件 */
|
|
902
903
|
export function implUrl(input, cwd) {
|
|
903
904
|
const s = String(input || '').trim()
|
|
904
|
-
|
|
905
|
+
const bad = () => new Error(`认不出这个地址:${s.slice(0, 80)}(端口是 1–65535 的数)`)
|
|
906
|
+
const web = (x) => { try { return new URL(x) } catch { throw bad() } }
|
|
907
|
+
if (/^\d{2,5}$/.test(s)) {
|
|
908
|
+
if (Number(s) < 1 || Number(s) > 65535) throw bad()
|
|
909
|
+
return `http://localhost:${Number(s)}/`
|
|
910
|
+
}
|
|
905
911
|
if (/^https?:\/\//i.test(s)) {
|
|
906
|
-
const u =
|
|
912
|
+
const u = web(s)
|
|
907
913
|
if (!/^(localhost|127\.0\.0\.1|0\.0\.0\.0|\[::1\])$/i.test(u.hostname)) throw new Error('只比本机的页面(localhost 地址或本地 HTML 文件)')
|
|
908
914
|
return u.href
|
|
909
915
|
}
|
|
910
|
-
if (/^(localhost|127\.0\.0\.1)(:\d+)?(\/|$)/i.test(s)) return 'http://' + s
|
|
911
|
-
|
|
916
|
+
if (/^(localhost|127\.0\.0\.1)(:\d+)?(\/|$)/i.test(s)) return web('http://' + s).href
|
|
917
|
+
// 本地 HTML:路径(相对的按会话目录算,~/ 是家目录)或记下来的 file:// 地址;给文件夹就用里面的 index.html
|
|
918
|
+
// 网络共享(\\server\share、//server/share)不比:只比这台电脑上的
|
|
919
|
+
if (/^(\\\\|\/\/)/.test(s) || /^file:\/\/[^/]/i.test(s)) throw new Error('只比这台电脑上的页面(网络共享里的文件不比)')
|
|
920
|
+
let p
|
|
921
|
+
let tail = '' // file:// 地址带的 ?参数 和 #路由:原样留着
|
|
922
|
+
try {
|
|
923
|
+
if (/^file:\/\//i.test(s)) {
|
|
924
|
+
const u = new URL(s)
|
|
925
|
+
tail = u.search + u.hash
|
|
926
|
+
p = fileURLToPath(u)
|
|
927
|
+
} else {
|
|
928
|
+
p = path.resolve(cwd || '.', s.replace(/^~(?=[\\/]|$)/, os.homedir()))
|
|
929
|
+
// 路径后面带了 #路由 / ?参数(dist/index.html#/login):文件按前半截找
|
|
930
|
+
const m = !fs.existsSync(p) && s.match(/^([^?#]+)([?#].*)$/)
|
|
931
|
+
if (m) { p = path.resolve(cwd || '.', m[1].replace(/^~(?=[\\/]|$)/, os.homedir())); tail = m[2] }
|
|
932
|
+
}
|
|
933
|
+
} catch { throw new Error(`认不出这个地址:${s.slice(0, 80)}`) }
|
|
934
|
+
if (process.platform === 'win32' && /^\\\\/.test(p)) throw new Error('只比这台电脑上的页面(网络共享里的文件不比)')
|
|
912
935
|
if (!fs.existsSync(p)) throw new Error(`找不到 ${p}`)
|
|
913
|
-
|
|
936
|
+
if (fs.statSync(p).isDirectory()) {
|
|
937
|
+
const idx = path.join(p, 'index.html')
|
|
938
|
+
if (!fs.existsSync(idx)) throw new Error(`${p} 是文件夹,里面没有 index.html:给页面的 HTML 文件或开发服务器地址`)
|
|
939
|
+
p = idx
|
|
940
|
+
} else if (!/\.html?$/i.test(p)) throw new Error(`只比网页:给 .html 文件或开发服务器地址(${path.basename(p)} 不是)`)
|
|
941
|
+
return pathToFileURL(p).href + tail
|
|
914
942
|
}
|
|
915
943
|
|
|
916
944
|
/**
|
|
917
945
|
* 比一屏:设计稿(预览服务器上的地址,带设计台脚本)vs 做出来的页面,同样尺寸、同样滚动位置。
|
|
918
946
|
* @returns { pct, boxes: [{ x, y, w, h, design, impl }], jpg, text }
|
|
919
947
|
*/
|
|
920
|
-
export async function compareScreens(browser, { design, impl, size, at = 'top', dark = false, whole = false }) {
|
|
948
|
+
export async function compareScreens(browser, { design, impl, shown = '', size, at = 'top', dark = false, whole = false }) {
|
|
921
949
|
const [w, h] = size
|
|
922
950
|
// 两边一样准备:等画好 → 深色 → 滚到同一个位置(页面和里面能滚的区域都滚)→ 无损截图
|
|
923
951
|
// 设计稿套了外框(whole 没要整屏):只截槽位 main,做出来的页面按槽位的尺寸开
|
|
@@ -943,9 +971,11 @@ export async function compareScreens(browser, { design, impl, size, at = 'top',
|
|
|
943
971
|
return { png, blocks, pageH, blocked: page.blocked ? page.blocked.slice() : [] }
|
|
944
972
|
})
|
|
945
973
|
// 一个接一个截:同时开两个页面时系统字体(中文)偶尔还没画好就截了,差异会虚高
|
|
946
|
-
|
|
974
|
+
// 刚写完文件就比:预览服务器的「改了自动刷新」可能在截图途中把页面刷新一次(页面没了)→ 重截一次
|
|
975
|
+
const again = (fn) => fn().catch((e) => (!blocked.length && /navigated or closed/i.test(e.message) ? fn() : Promise.reject(e)))
|
|
976
|
+
const A = await again(() => shoot(design.url, true, [w, h]))
|
|
947
977
|
if (slot && slot.hidden) return { pct: null, boxes: [], jpg: null, text: `没比:${design.label || design.key} 套了外框,按 ${w}×${h} 开时外框里的 main 槽位露不出来。size 填外框自己的尺寸 ${slot.fw}x${slot.fh} 再比。` }
|
|
948
|
-
const B = await shoot(impl, false, slot ? [slot.w, slot.h] : [w, h]).catch((e) => {
|
|
978
|
+
const B = await again(() => shoot(impl, false, slot ? [slot.w, slot.h] : [w, h])).catch((e) => {
|
|
949
979
|
// 页面自己跳到外网被拦下、截图时页面没了:说清楚是跳走了
|
|
950
980
|
if (blocked.length) throw new Error(`做出来的页面跳到了本机以外的地址,拦下了:${blocked[0].slice(0, 120)}`)
|
|
951
981
|
throw e
|
|
@@ -954,7 +984,7 @@ export async function compareScreens(browser, { design, impl, size, at = 'top',
|
|
|
954
984
|
const r = await browser.withPage({ url: 'about:blank', w: 200, h: 200 }, (page) => page.eval(`(${diffIn})(${JSON.stringify(A.png.toString('base64'))}, ${JSON.stringify(B.png.toString('base64'))}, ${JSON.stringify(labels)})`))
|
|
955
985
|
const boxes = r.boxes.map((bx) => ({ ...bx, design: blockAt(A.blocks, bx), impl: blockAt(B.blocks, bx) }))
|
|
956
986
|
const where = slot ? `只比外框里的 main 槽位 ${slot.w}×${slot.h},做出来的页面按这个尺寸开;外框是宿主画的,不算差异` : `${w}×${h}`
|
|
957
|
-
const lines = [`${design.label || design.key}(${where}${at === 'bottom' ? ',滚到底' : ',顶上一屏'}):设计稿 vs ${impl}`]
|
|
987
|
+
const lines = [`${design.label || design.key}(${where}${at === 'bottom' ? ',滚到底' : ',顶上一屏'}):设计稿 vs ${shown || impl}`]
|
|
958
988
|
if (slot && slot.part) lines.push(`注意:这个尺寸(${w}×${h})装不下整个外框,槽位只露出 ${slot.w}×${slot.h},做出来的页面也只按这么大开;size 填外框自己的尺寸 ${slot.fw}x${slot.fh} 再比。`)
|
|
959
989
|
lines.push(`差异 ${r.pct.toFixed(1)}%(逐像素无损比)${r.pct < 3 ? ':基本一致' : r.pct < 10 ? ':大体对上了,下面几块要改' : ':差得比较多'}`)
|
|
960
990
|
if (B.blocked.length) lines.push(`做出来的页面想跳到本机以外的地址,拦下了:${B.blocked.slice(0, 3).join('、')}`)
|
|
@@ -964,5 +994,6 @@ export async function compareScreens(browser, { design, impl, size, at = 'top',
|
|
|
964
994
|
boxes.forEach((b, i) => lines.push(`${i + 1}. 位置 ${b.x},${b.y}(${b.w}×${b.h}):设计稿是 ${describe(b.design)},做出来的是 ${describe(b.impl)}`))
|
|
965
995
|
lines.push('整块挪了位置先查上面的间距和高度;颜色不对查设计变量;字的宽度不一样查字体、字号、字重。')
|
|
966
996
|
} else lines.push('没有成片的差异。')
|
|
967
|
-
|
|
997
|
+
// cmp:比的那块多大(套了外框是槽位);slot:只比了槽位。右侧栏「和做出来的比」按它把红框标回去
|
|
998
|
+
return { pct: r.pct, boxes, jpg: Buffer.from(r.jpg, 'base64'), text: lines.join('\n'), cmp: slot ? [slot.w, slot.h] : [w, h], slot: !!slot }
|
|
968
999
|
}
|