dsh-design-studio 0.13.0 → 0.14.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/client.js +313 -31
- package/lib/doc.js +118 -46
- package/lib/index.js +29 -9
- package/lib/look.js +32 -2
- package/lib/picker.js +275 -3
- package/package.json +1 -1
package/lib/doc.js
CHANGED
|
@@ -47,6 +47,45 @@ export function pageLinks(p, count, self) {
|
|
|
47
47
|
return out
|
|
48
48
|
}
|
|
49
49
|
|
|
50
|
+
// ── 版面和拼版 ───────────────────────────────────────────────────────────
|
|
51
|
+
|
|
52
|
+
/** 版面:空或 'auto' = 跟随画幅(返回 null);'16:9'、'4:3'、'a3'(A3 横向)、'宽x高'(CSS 像素)。别的写法当没写 */
|
|
53
|
+
export function layoutSize(layout) {
|
|
54
|
+
const s = String(layout || '').trim().toLowerCase()
|
|
55
|
+
if (s === '16:9') return [1280, 720]
|
|
56
|
+
if (s === '4:3') return [1280, 960]
|
|
57
|
+
if (s === 'a3') return [1587, 1123]
|
|
58
|
+
const m = /^(\d{3,4})\s*[x×*]\s*(\d{3,4})$/.exec(s)
|
|
59
|
+
if (!m) return null
|
|
60
|
+
const [w, h] = [+m[1], +m[2]]
|
|
61
|
+
return w >= 200 && h >= 200 && w <= 8000 && h <= 8000 ? [w, h] : null
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* 每页纸上放哪几屏:[{ W, H, items: [{ i, x, y, w, h, k }] }],i 是第几屏,x / y 是放在纸上的位置,k 是缩放(都是 CSS 像素)。
|
|
66
|
+
* 什么都不选:每屏一张纸、大小就是这一屏。选了版面:放大或缩小到整屏装得下,居中留边。每页 2 屏:左右各一格,单出来的一屏放左边。
|
|
67
|
+
*/
|
|
68
|
+
export function sheetsOf(pages, { layout = '', cols = 1, gap = 24 } = {}) {
|
|
69
|
+
const box = layoutSize(layout)
|
|
70
|
+
const per = cols === 2 ? 2 : 1
|
|
71
|
+
const m = per === 2 ? gap : 0 // 一页一屏不留边
|
|
72
|
+
const maxW = Math.max(...pages.map((p) => p.w))
|
|
73
|
+
const maxH = Math.max(...pages.map((p) => p.h))
|
|
74
|
+
const sheets = []
|
|
75
|
+
for (let i = 0; i < pages.length; i += per) {
|
|
76
|
+
const W = box ? box[0] : per === 2 ? maxW * 2 + gap * 3 : pages[i].w
|
|
77
|
+
const H = box ? box[1] : per === 2 ? maxH + gap * 2 : pages[i].h
|
|
78
|
+
const cw = (W - m * (per + 1)) / per
|
|
79
|
+
const ch = H - m * 2
|
|
80
|
+
const items = pages.slice(i, i + per).map((p, j) => {
|
|
81
|
+
const k = box ? Math.min(cw / p.w, ch / p.h) : 1
|
|
82
|
+
return { i: i + j, x: m + j * (cw + m) + (cw - p.w * k) / 2, y: m + (ch - p.h * k) / 2, w: p.w, h: p.h, k }
|
|
83
|
+
})
|
|
84
|
+
sheets.push({ W, H, items })
|
|
85
|
+
}
|
|
86
|
+
return sheets
|
|
87
|
+
}
|
|
88
|
+
|
|
50
89
|
// ── PDF ──────────────────────────────────────────────────────────────────
|
|
51
90
|
|
|
52
91
|
/** PDF 文本串:纯 ASCII 用 ( ),带中文用 UTF-16BE 十六进制 */
|
|
@@ -61,36 +100,52 @@ function pdfText(s) {
|
|
|
61
100
|
/**
|
|
62
101
|
* pages: [{ jpg: Buffer, w, h, title?, links? }],w / h 是这一页的 CSS 像素(1px 按 0.75pt 算,和浏览器打印一样大)
|
|
63
102
|
* 每页一张图铺满;有名字的页进书签,links 是点了跳到别的页的地方(见 pageLinks)。
|
|
103
|
+
* layout / cols 见 sheetsOf;选了它们时纸上空出来的地方铺 bg('RRGGBB')。不选就和原来一样
|
|
64
104
|
*/
|
|
65
|
-
export function makePdf(pages, { title = '' } = {}) {
|
|
105
|
+
export function makePdf(pages, { title = '', layout = '', cols = 1, bg = '' } = {}) {
|
|
106
|
+
const sheets = sheetsOf(pages, { layout, cols })
|
|
107
|
+
const per = cols === 2 ? 2 : 1
|
|
108
|
+
const paper = (per === 2 || layoutSize(layout)) && /^[0-9a-f]{6}$/i.test(bg) ? [0, 2, 4].map((i) => (parseInt(bg.slice(i, i + 2), 16) / 255).toFixed(3)).join(' ') : ''
|
|
66
109
|
const objs = [] // 下标 + 1 = 对象号
|
|
67
110
|
const add = (parts) => { objs.push(parts); return objs.length }
|
|
68
111
|
const catalog = add(null)
|
|
69
112
|
const tree = add(null)
|
|
70
|
-
const kids =
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
const
|
|
75
|
-
const
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
113
|
+
const kids = sheets.map(() => add(null)) // 页先占号:页上的链接要指到后面的页
|
|
114
|
+
const pt = (v) => +(v * 0.75).toFixed(2)
|
|
115
|
+
sheets.forEach((s, si) => {
|
|
116
|
+
const W = pt(s.W)
|
|
117
|
+
const H = pt(s.H)
|
|
118
|
+
const placed = s.items.map((it, j) => {
|
|
119
|
+
const p = pages[it.i]
|
|
120
|
+
const info = jpegInfo(p.jpg)
|
|
121
|
+
if (!info) throw new Error('不是 JPEG')
|
|
122
|
+
const cs = info.comps === 1 ? '/DeviceGray' : info.comps === 4 ? '/DeviceCMYK' : '/DeviceRGB'
|
|
123
|
+
const img = add([`<< /Type /XObject /Subtype /Image /Width ${info.width} /Height ${info.height} /ColorSpace ${cs} /BitsPerComponent 8 /Filter /DCTDecode${info.comps === 4 ? ' /Decode [1 0 1 0 1 0 1 0]' : ''} /Length ${p.jpg.length} >>\nstream\n`, p.jpg, '\nendstream'])
|
|
124
|
+
return { img, draw: `q ${pt(it.w * it.k)} 0 0 ${pt(it.h * it.k)} ${pt(it.x)} ${pt(s.H - it.y - it.h * it.k)} cm /Im${j} Do Q` }
|
|
125
|
+
})
|
|
126
|
+
const draw = (paper ? `${paper} rg 0 0 ${W} ${H} re f ` : '') + placed.map((x) => x.draw).join(' ')
|
|
79
127
|
const content = add([`<< /Length ${draw.length} >>\nstream\n${draw}\nendstream`])
|
|
80
|
-
const
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
128
|
+
const annots = s.items.flatMap((it) => pageLinks(pages[it.i], pages.length, it.i)
|
|
129
|
+
.map((l) => ({ ...l, to: Math.floor(l.to / per) }))
|
|
130
|
+
.filter((l) => l.to !== si) // 拼到同一页上的两屏之间不用跳
|
|
131
|
+
.map((l) => {
|
|
132
|
+
const x = it.x + l.x * it.k
|
|
133
|
+
const y = it.y + l.y * it.k
|
|
134
|
+
const w = l.w * it.k
|
|
135
|
+
const ht = l.h * it.k
|
|
136
|
+
return add([`<< /Type /Annot /Subtype /Link /Rect [${pt(x)} ${pt(s.H - y - ht)} ${pt(x + w)} ${pt(s.H - y)}] /Border [0 0 0] /Dest [${kids[l.to]} 0 R /Fit] >>`])
|
|
137
|
+
}))
|
|
138
|
+
objs[kids[si] - 1] = [`<< /Type /Page /Parent ${tree} 0 R /MediaBox [0 0 ${W} ${H}] /Resources << /XObject << ${placed.map((x, j) => `/Im${j} ${x.img} 0 R`).join(' ')} >> >> /Contents ${content} 0 R` +
|
|
84
139
|
`${annots.length ? ` /Annots [${annots.map((a) => `${a} 0 R`).join(' ')}]` : ''} >>`]
|
|
85
140
|
})
|
|
86
141
|
// 书签:每屏一条,名字是屏名(只有一页就不要了)
|
|
87
142
|
const marks = pages.map((p, i) => ({ i, title: String(p.title || '').trim().slice(0, 120) })).filter((x) => x.title)
|
|
88
143
|
let outlines = 0
|
|
89
|
-
if (
|
|
144
|
+
if (sheets.length > 1 && marks.length) {
|
|
90
145
|
outlines = add(null)
|
|
91
146
|
const items = marks.map(() => add(null))
|
|
92
147
|
items.forEach((o, k) => {
|
|
93
|
-
objs[o - 1] = [`<< /Title ${pdfText(marks[k].title)} /Parent ${outlines} 0 R${k ? ` /Prev ${items[k - 1]} 0 R` : ''}${k < items.length - 1 ? ` /Next ${items[k + 1]} 0 R` : ''} /Dest [${kids[marks[k].i]} 0 R /Fit] >>`]
|
|
148
|
+
objs[o - 1] = [`<< /Title ${pdfText(marks[k].title)} /Parent ${outlines} 0 R${k ? ` /Prev ${items[k - 1]} 0 R` : ''}${k < items.length - 1 ? ` /Next ${items[k + 1]} 0 R` : ''} /Dest [${kids[Math.floor(marks[k].i / per)]} 0 R /Fit] >>`]
|
|
94
149
|
})
|
|
95
150
|
objs[outlines - 1] = [`<< /Type /Outlines /First ${items[0]} 0 R /Last ${items[items.length - 1]} 0 R /Count ${items.length} >>`]
|
|
96
151
|
}
|
|
@@ -221,8 +276,9 @@ function theme() {
|
|
|
221
276
|
'</a:fmtScheme></a:themeElements><a:objectDefaults/><a:extraClrSchemeLst/></a:theme>'
|
|
222
277
|
}
|
|
223
278
|
|
|
224
|
-
/** 幻灯片尺寸:按第一屏的比例,长边 13.333 英寸(和 PowerPoint 默认 16:9
|
|
225
|
-
export function slideSize(w, h) {
|
|
279
|
+
/** 幻灯片尺寸:按第一屏的比例,长边 13.333 英寸(和 PowerPoint 默认 16:9 一样宽);A3 用它的实际大小(420×297 毫米) */
|
|
280
|
+
export function slideSize(w, h, layout = '') {
|
|
281
|
+
if (String(layout).trim().toLowerCase() === 'a3') return { cx: 15120000, cy: 10692000 }
|
|
226
282
|
const LONG = 12192000
|
|
227
283
|
let cx = w >= h ? LONG : Math.round(LONG * w / h)
|
|
228
284
|
let cy = w >= h ? Math.round(LONG * h / w) : LONG
|
|
@@ -233,45 +289,61 @@ export function slideSize(w, h) {
|
|
|
233
289
|
|
|
234
290
|
/**
|
|
235
291
|
* pages: [{ jpg: Buffer, w, h, title, links? }];每张幻灯片一张图,按比例放进去居中(比例不一样的留边,边上铺 bg 色)。
|
|
236
|
-
* links 上盖透明形状,放映时点了跳到那一张。bg: 'RRGGBB'
|
|
292
|
+
* links 上盖透明形状,放映时点了跳到那一张。bg: 'RRGGBB'。layout / cols 见 sheetsOf,不选就和原来一样
|
|
237
293
|
*/
|
|
238
|
-
export function makePptx(pages, { title = '', bg = 'FFFFFF', date = new Date() } = {}) {
|
|
294
|
+
export function makePptx(pages, { title = '', bg = 'FFFFFF', date = new Date(), layout = '', cols = 1 } = {}) {
|
|
239
295
|
if (!pages.length) throw new Error('没有页面')
|
|
240
|
-
const
|
|
296
|
+
const sheets = sheetsOf(pages, { layout, cols })
|
|
297
|
+
const per = cols === 2 ? 2 : 1
|
|
298
|
+
const { cx, cy } = slideSize(sheets[0].W, sheets[0].H, layout)
|
|
241
299
|
const fill = /^[0-9a-f]{6}$/i.test(bg) ? bg.toUpperCase() : 'FFFFFF'
|
|
242
300
|
const files = []
|
|
243
|
-
const slides =
|
|
244
|
-
|
|
245
|
-
const
|
|
246
|
-
const
|
|
247
|
-
const
|
|
248
|
-
const
|
|
249
|
-
const
|
|
250
|
-
|
|
251
|
-
const name = xmlEsc(p.title || `Screen ${n}`)
|
|
252
|
-
// 跳转:每个目标页一条关系(rId3 起),每个按钮一个透明形状
|
|
301
|
+
const slides = sheets.map((s, si) => {
|
|
302
|
+
const n = si + 1
|
|
303
|
+
const k = Math.min(cx / s.W, cy / s.H)
|
|
304
|
+
const left = (cx - Math.round(s.W * k)) / 2
|
|
305
|
+
const top = (cy - Math.round(s.H * k)) / 2
|
|
306
|
+
const count = s.items.length
|
|
307
|
+
const titleOf = (it) => pages[it.i].title || `Screen ${it.i + 1}`
|
|
308
|
+
// 跳转:每个目标页一条关系(排在图片后面),每个按钮一个透明形状
|
|
253
309
|
const targets = []
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
310
|
+
let hotN = 0
|
|
311
|
+
const placed = s.items.map((it, j) => {
|
|
312
|
+
const p = pages[it.i]
|
|
313
|
+
if (!jpegInfo(p.jpg)) throw new Error('不是 JPEG')
|
|
314
|
+
const x = Math.round(left + it.x * k)
|
|
315
|
+
const y = Math.round(top + it.y * k)
|
|
316
|
+
const w = Math.round(it.w * it.k * k)
|
|
317
|
+
const ht = Math.round(it.h * it.k * k)
|
|
318
|
+
const name = xmlEsc(titleOf(it))
|
|
319
|
+
const hot = pageLinks(p, pages.length, it.i).map((l) => ({ ...l, to: Math.floor(l.to / per) })).filter((l) => l.to !== si).map((l) => {
|
|
320
|
+
let r = targets.indexOf(l.to)
|
|
321
|
+
if (r < 0) { targets.push(l.to); r = targets.length - 1 }
|
|
322
|
+
const c = hotN++
|
|
323
|
+
const m = it.k * k
|
|
324
|
+
return `<p:sp><p:nvSpPr><p:cNvPr id="${c + 2 + count}" name="Link ${c + 1}"><a:hlinkClick r:id="rId${r + 2 + count}" action="ppaction://hlinksldjump"/></p:cNvPr><p:cNvSpPr/><p:nvPr/></p:nvSpPr>` +
|
|
325
|
+
`<p:spPr><a:xfrm><a:off x="${x + Math.round(l.x * m)}" y="${y + Math.round(l.y * m)}"/><a:ext cx="${Math.max(1, Math.round(l.w * m))}" cy="${Math.max(1, Math.round(l.h * m))}"/></a:xfrm>` +
|
|
326
|
+
'<a:prstGeom prst="rect"><a:avLst/></a:prstGeom><a:noFill/><a:ln><a:noFill/></a:ln></p:spPr></p:sp>'
|
|
327
|
+
}).join('')
|
|
328
|
+
files.push({ name: `ppt/media/image${it.i + 1}.jpeg`, data: p.jpg })
|
|
329
|
+
return {
|
|
330
|
+
hot,
|
|
331
|
+
pic: `<p:pic><p:nvPicPr><p:cNvPr id="${j + 2}" name="${name}" descr="${name}"/><p:cNvPicPr><a:picLocks noChangeAspect="1"/></p:cNvPicPr><p:nvPr/></p:nvPicPr>` +
|
|
332
|
+
`<p:blipFill><a:blip r:embed="rId${j + 2}"/><a:stretch><a:fillRect/></a:stretch></p:blipFill>` +
|
|
333
|
+
`<p:spPr><a:xfrm><a:off x="${x}" y="${y}"/><a:ext cx="${w}" cy="${ht}"/></a:xfrm><a:prstGeom prst="rect"><a:avLst/></a:prstGeom></p:spPr></p:pic>`,
|
|
334
|
+
}
|
|
335
|
+
})
|
|
262
336
|
files.push({
|
|
263
337
|
name: `ppt/slides/slide${n}.xml`,
|
|
264
|
-
data: XML + `<p:sld xmlns:a="${NS_A}" xmlns:r="${NS_R}" xmlns:p="${NS_P}"><p:cSld name="${
|
|
338
|
+
data: XML + `<p:sld xmlns:a="${NS_A}" xmlns:r="${NS_R}" xmlns:p="${NS_P}"><p:cSld name="${xmlEsc(s.items.map(titleOf).join(' / '))}">` +
|
|
265
339
|
`<p:bg><p:bgPr><a:solidFill><a:srgbClr val="${fill}"/></a:solidFill><a:effectLst/></p:bgPr></p:bg>` +
|
|
266
|
-
`<p:spTree>${EMPTY_TREE}
|
|
267
|
-
`<p:blipFill><a:blip r:embed="rId2"/><a:stretch><a:fillRect/></a:stretch></p:blipFill>` +
|
|
268
|
-
`<p:spPr><a:xfrm><a:off x="${x}" y="${y}"/><a:ext cx="${w}" cy="${ht}"/></a:xfrm><a:prstGeom prst="rect"><a:avLst/></a:prstGeom></p:spPr></p:pic>` +
|
|
269
|
-
hot + '</p:spTree></p:cSld><p:clrMapOvr><a:masterClrMapping/></p:clrMapOvr></p:sld>',
|
|
340
|
+
`<p:spTree>${EMPTY_TREE}${placed.map((x) => x.pic).join('')}${placed.map((x) => x.hot).join('')}</p:spTree></p:cSld><p:clrMapOvr><a:masterClrMapping/></p:clrMapOvr></p:sld>`,
|
|
270
341
|
})
|
|
271
342
|
files.push({
|
|
272
343
|
name: `ppt/slides/_rels/slide${n}.xml.rels`,
|
|
273
|
-
data: rels([['rId1', `${REL}/slideLayout`, '../slideLayouts/slideLayout1.xml'],
|
|
274
|
-
...
|
|
344
|
+
data: rels([['rId1', `${REL}/slideLayout`, '../slideLayouts/slideLayout1.xml'],
|
|
345
|
+
...s.items.map((it, j) => [`rId${j + 2}`, `${REL}/image`, `../media/image${it.i + 1}.jpeg`]),
|
|
346
|
+
...targets.map((to, r) => [`rId${r + 2 + count}`, `${REL}/slide`, `slide${to + 1}.xml`])]),
|
|
275
347
|
})
|
|
276
348
|
return n
|
|
277
349
|
})
|
package/lib/index.js
CHANGED
|
@@ -60,7 +60,7 @@ import { createPhone } from './phone.js'
|
|
|
60
60
|
import { qrMatrix } from './qr.js'
|
|
61
61
|
import { makePdf, makePptx, jpegInfo } from './doc.js'
|
|
62
62
|
import { createBrowser } from './browser.js'
|
|
63
|
-
import { designOf, metaOfFile, sizeOf, pickScreens, measureAll, shootAll, cropIssues, formatMeasure, issueSnapshot, formatShots, whereOf, MAX_SCREENS, MAX_SIZES, MAX_SHOTS, MAX_CROPS } from './look.js'
|
|
63
|
+
import { designOf, fitSize, metaOfFile, sizeOf, pickScreens, measureAll, shootAll, cropIssues, formatMeasure, issueSnapshot, formatShots, whereOf, MAX_SCREENS, MAX_SIZES, MAX_SHOTS, MAX_CROPS } from './look.js'
|
|
64
64
|
import { SYSTEMS_DIR, extractSystem, listSystems, setDefault, skillSection } from './system.js'
|
|
65
65
|
import { readTweaks, applyTweaks, lintTweaks } from './tweaks.js'
|
|
66
66
|
import { FRAMEWORKS, detectProject, projectFor, defaultFramework, buildHandoff, compareScreens, implUrl, defaultDest, pickedDest, within } from './handoff.js'
|
|
@@ -176,6 +176,9 @@ export const previewTool = {
|
|
|
176
176
|
},
|
|
177
177
|
}
|
|
178
178
|
|
|
179
|
+
/** 无头浏览器用的尺寸:没写尺寸的幻灯片 / 海报按页面实际大小(和右侧栏的画幅一样) */
|
|
180
|
+
const fitted = (b, d, names) => (d.auto && b && b.available() ? fitSize(b, pickScreens(d.snap, names, names && names.length ? 40 : 6).list, d.kind, d.size) : d.size)
|
|
181
|
+
|
|
179
182
|
/** 这个会话在预览的设计:哪几屏、哪类、多大(工具在无头浏览器里量 / 截图用) */
|
|
180
183
|
function designFor(id) {
|
|
181
184
|
const snap = studio.tracker.snapshot(id)
|
|
@@ -236,14 +239,15 @@ export const checkTool = {
|
|
|
236
239
|
const id = toolSession(exec, '体检')
|
|
237
240
|
const a = args || {}
|
|
238
241
|
if (!studio.tracker.snapshot(id).url) return { text: '还没有在预览的页面:先用 preview_page 打开设计文件,再体检。' }
|
|
239
|
-
const
|
|
242
|
+
const design = designFor(id)
|
|
243
|
+
const { snap, kind } = design
|
|
240
244
|
const want = sizesFrom(a.sizes, kind)
|
|
241
245
|
const b = studio.browser
|
|
242
246
|
if (b && b.available()) {
|
|
243
247
|
try {
|
|
244
248
|
const pick = pickScreens(snap, a.screens, MAX_SCREENS)
|
|
245
249
|
if (!pick.list.length) return { text: `没找到要量的屏:${pick.missing.join('、')}。现在有:${(snap.screens || []).map((x) => x.key).join('、') || snap.focus}` }
|
|
246
|
-
const sizes = want.sizes.length ? want.sizes : [
|
|
250
|
+
const sizes = want.sizes.length ? want.sizes : [await fitted(b, design, a.screens)]
|
|
247
251
|
const rows = await measureAll(b, { screens: pick.list, sizes, kind, dark: !!a.dark, locate: (items) => locateRules(studio.tracker.stateOf(id), items) })
|
|
248
252
|
if (rows.some((r) => !r.error)) {
|
|
249
253
|
// 同一个会话上次量的结果:这次比一比修好了几个、新出了几个
|
|
@@ -369,8 +373,9 @@ export const lookTool = {
|
|
|
369
373
|
if (!studio.tracker.snapshot(id).url) return { text: '还没有在预览的页面:先用 preview_page 打开设计文件,再看。' }
|
|
370
374
|
const b = studio.browser
|
|
371
375
|
if (!b || !b.available()) return { text: '这台电脑上没找到 Chrome / Edge(无头浏览器截图要用),看不了。用 check_design 拿测量数字,或者让用户看右侧栏。' }
|
|
372
|
-
const
|
|
373
|
-
const
|
|
376
|
+
const design = designFor(id)
|
|
377
|
+
const { snap, kind } = design
|
|
378
|
+
const want = a.size ? sizeOf(a.size, kind) : await fitted(b, design, a.screens)
|
|
374
379
|
if (!want) return { text: `认不出尺寸「${String(a.size).slice(0, 20)}」:写成 375x667,或只写宽度 375。` }
|
|
375
380
|
const at = ['top', 'bottom', 'full'].includes(a.at) ? a.at : 'top'
|
|
376
381
|
const pick = pickScreens(snap, a.screens, MAX_SHOTS)
|
|
@@ -469,7 +474,9 @@ export const handoffTool = {
|
|
|
469
474
|
if (!snap.url) return { text: '还没有在预览的设计:先用 preview_page 打开设计文件。' }
|
|
470
475
|
const t = studio.tracker.stateOf(id).target
|
|
471
476
|
if (!t || t.kind !== 'file') return { text: '现在预览的是开发服务器上的页面,它已经是代码了:直接在那个项目里改;要比对设计稿就先用 preview_page 打开设计的 HTML 文件。' }
|
|
472
|
-
const
|
|
477
|
+
const design = designFor(id)
|
|
478
|
+
const { kind } = design
|
|
479
|
+
const size = await fitted(studio.browser, design, a.screens)
|
|
473
480
|
const pick = pickScreens(snap, a.screens, 12)
|
|
474
481
|
if (!pick.list.length) return { text: `没找到要转的屏:${pick.missing.join('、')}。现在有:${(snap.screens || []).map((x) => x.key).join('、')}` }
|
|
475
482
|
const cwd = studio.tracker.cwdOf(id)
|
|
@@ -524,8 +531,9 @@ export const compareTool = {
|
|
|
524
531
|
if (!snap.url) return { text: '还没有在预览的设计稿:先用 preview_page 打开设计文件,再比。' }
|
|
525
532
|
const b = studio.browser
|
|
526
533
|
if (!b || !b.available()) return { text: '这台电脑上没找到 Chrome / Edge(比对要用无头浏览器截图),比不了。' }
|
|
527
|
-
const
|
|
528
|
-
const
|
|
534
|
+
const design = designFor(id)
|
|
535
|
+
const { kind } = design
|
|
536
|
+
const want = a.size ? sizeOf(a.size, kind) : await fitted(b, design, typeof a.screen === 'string' && a.screen.trim() ? [a.screen] : null)
|
|
529
537
|
if (!want) return { text: `认不出尺寸「${String(a.size).slice(0, 20)}」:写成 375x667,或只写宽度 375。` }
|
|
530
538
|
// 指定了哪屏就只认那屏,找不到就说(别悄悄拿第一屏比,结果对不上号);没指定才用正在看的 / 第一屏
|
|
531
539
|
const asked = typeof a.screen === 'string' && a.screen.trim()
|
|
@@ -1070,6 +1078,14 @@ export function apply(ctx) {
|
|
|
1070
1078
|
}
|
|
1071
1079
|
}
|
|
1072
1080
|
|
|
1081
|
+
/** 导出途中换了设计:还按开始时的那套比一下,对不上就不存(不然 A 的文件会落到 B 的 exports/ 里) */
|
|
1082
|
+
const targetKeyOf = (t) => (!t ? '' : t.kind === 'file' ? `file|${t.root}` : `proxy|${t.port}`)
|
|
1083
|
+
function targetMoved(res, session, expect) {
|
|
1084
|
+
if (typeof expect !== 'string' || !expect || expect === targetKeyOf(tracker.stateOf(session).target)) return false
|
|
1085
|
+
json(res, 409, { error: 'designChanged' })
|
|
1086
|
+
return true
|
|
1087
|
+
}
|
|
1088
|
+
|
|
1073
1089
|
async function exportRoute(req, res) {
|
|
1074
1090
|
if (!admit(req, res)) return
|
|
1075
1091
|
if (req.method !== 'POST') return json(res, 405, { error: 'method not allowed' })
|
|
@@ -1077,6 +1093,7 @@ export function apply(ctx) {
|
|
|
1077
1093
|
try { body = await readBody(req, MAX_EXPORT) } catch (e) { return json(res, 400, { error: e.message }) }
|
|
1078
1094
|
const buf = Buffer.from(String(body.png || ''), 'base64')
|
|
1079
1095
|
if (buf.length < 8 || buf.readUInt32BE(0) !== 0x89504e47) return json(res, 400, { error: '不是 PNG' })
|
|
1096
|
+
if (targetMoved(res, String(body.session || ''), body.target)) return
|
|
1080
1097
|
saveExport(res, String(body.session || ''), body.name, 'png', buf)
|
|
1081
1098
|
}
|
|
1082
1099
|
|
|
@@ -1104,10 +1121,13 @@ export function apply(ctx) {
|
|
|
1104
1121
|
const title = String(body.title || '').slice(0, 120)
|
|
1105
1122
|
let buf
|
|
1106
1123
|
try {
|
|
1107
|
-
|
|
1124
|
+
const bg = String(body.bg || '')
|
|
1125
|
+
const opts = { layout: String(body.layout || '').slice(0, 20), cols: body.cols === 2 ? 2 : 1 }
|
|
1126
|
+
buf = format === 'pdf' ? makePdf(pages, { title, bg, ...opts }) : makePptx(pages, { title, bg, ...opts })
|
|
1108
1127
|
} catch (e) {
|
|
1109
1128
|
return json(res, 400, { error: e.message })
|
|
1110
1129
|
}
|
|
1130
|
+
if (targetMoved(res, String(body.session || ''), body.target)) return
|
|
1111
1131
|
saveExport(res, String(body.session || ''), body.name, format, buf)
|
|
1112
1132
|
}
|
|
1113
1133
|
|
package/lib/look.js
CHANGED
|
@@ -51,6 +51,28 @@ export function sizeOf(v, kind) {
|
|
|
51
51
|
return [w, dh]
|
|
52
52
|
}
|
|
53
53
|
|
|
54
|
+
// 定死画面的类型:页面比默认画幅大(写死了 1440×810 又没写进 meta)就按页面实际大小。比例不像一页的(几页竖着堆的)不算
|
|
55
|
+
const AUTO_RATIO = { slides: [1.2, 2.4], poster: [0.45, 2.5] }
|
|
56
|
+
|
|
57
|
+
/** 各屏量到的实际大小 → 'WxH'(都没比默认大:'')。和右侧栏 client.js 的 autoSize 一样 */
|
|
58
|
+
export function autoSize(list, kind) {
|
|
59
|
+
const range = AUTO_RATIO[kind]
|
|
60
|
+
if (!range) return ''
|
|
61
|
+
const [dw, dh] = KIND_SIZE[kind]
|
|
62
|
+
let W = 0, H = 0
|
|
63
|
+
for (const n of list) {
|
|
64
|
+
if (!Array.isArray(n)) continue
|
|
65
|
+
const w = Math.ceil(+n[0]), h = Math.ceil(+n[1])
|
|
66
|
+
if (!(w >= 200 && h >= 200 && w <= 4000 && h <= 6000)) continue
|
|
67
|
+
if ((w <= dw && h <= dh) || w / h < range[0] || w / h > range[1]) continue
|
|
68
|
+
W = Math.max(W, w)
|
|
69
|
+
H = Math.max(H, h)
|
|
70
|
+
}
|
|
71
|
+
if (!W) return ''
|
|
72
|
+
// 一组屏共用一个画幅:别的屏量不出比默认大(正好默认大小的也报不出来),画幅不能比默认小,不然它们被裁
|
|
73
|
+
return list.length > 1 ? `${Math.max(W, dw)}x${Math.max(H, dh)}` : `${W}x${H}`
|
|
74
|
+
}
|
|
75
|
+
|
|
54
76
|
/** 设计稿是哪类、多大:preview_page 给的 → 页面里的 <meta name="dsh-design"> → 默认网页 */
|
|
55
77
|
export function designOf(snap, readHead) {
|
|
56
78
|
let meta = { kind: '', size: '' }
|
|
@@ -58,8 +80,9 @@ export function designOf(snap, readHead) {
|
|
|
58
80
|
try { meta = parseMeta(readHead() || '') } catch { /* 读不到就按默认 */ }
|
|
59
81
|
}
|
|
60
82
|
const kind = KIND_LIST.includes(snap.kind) ? snap.kind : meta.kind || 'web'
|
|
61
|
-
const
|
|
62
|
-
|
|
83
|
+
const named = sizeOf(snap.size, kind) || sizeOf(meta.size, kind)
|
|
84
|
+
const size = named || KIND_SIZE[kind]
|
|
85
|
+
return !named && AUTO_RATIO[kind] ? { kind, size, auto: true } : { kind, size }
|
|
63
86
|
}
|
|
64
87
|
|
|
65
88
|
/** 本地 HTML 文件里 dsh-design 那个 meta 的内容(只读开头一段) */
|
|
@@ -109,6 +132,13 @@ export async function prepare(page, dark) {
|
|
|
109
132
|
return { info: await page.eval('window.__dshDesign.info()'), darkOk }
|
|
110
133
|
}
|
|
111
134
|
|
|
135
|
+
/** 没写尺寸的幻灯片 / 海报:开页面量一下,比默认画幅大就按页面实际大小(和右侧栏的画幅一样)。量不到就还是默认 */
|
|
136
|
+
export async function fitSize(browser, screens, kind, size) {
|
|
137
|
+
const [w, h] = size
|
|
138
|
+
const seen = await Promise.all(screens.map((sc) => browser.withPage({ url: sc.url, w, h }, async (page) => (await prepare(page, false)).info.natural).catch(() => null)))
|
|
139
|
+
return sizeOf(autoSize(seen, kind), kind) || size
|
|
140
|
+
}
|
|
141
|
+
|
|
112
142
|
/**
|
|
113
143
|
* 量:每屏 × 每种尺寸开一个页面跑体检。
|
|
114
144
|
* @returns [{ key, label, w, h, issues, metrics, error }]
|