dsh-design-studio 0.13.1 → 0.15.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/core.js CHANGED
@@ -31,7 +31,8 @@ const SHELL_TOOL = /^(bash|shell|sh|zsh|powershell|pwsh|exec|run_command|termina
31
31
  // 放到后台跑的命令(DSH:bash 带 run_in_background,或前台超时转后台)回一个 job id,输出要用 job_output 去读
32
32
  const JOB_READ_TOOL = /^(job_output|bash_output|read_job|job_read)$/i
33
33
  const BG_JOB = /\bbackground job\s+`?([\w.:-]{3,})/i
34
- const MAX_SCREENS = 24
34
+ const MAX_SCREENS = 60 // 右侧最多显示几屏,再多的收起来并提示(画布同时只挂 24 屏,见 client.js 的 LIVE_MAX)
35
+ const MAX_ROUTES = 300 // 开发服务器的路由先都记着(只为算出收起了几屏);再多就是写飞了
35
36
 
36
37
  // 设计类型:决定右侧用什么画幅显示(网页框、手机、小程序、平板、桌面软件窗口、海报、幻灯片、邮件)
37
38
  export const KINDS = ['web', 'app', 'miniprogram', 'tablet', 'desktop', 'poster', 'slides', 'email']
@@ -64,8 +65,20 @@ export function parseSize(input) {
64
65
  return `${w}x${h}`
65
66
  }
66
67
 
67
- /** 目录里的几屏:顶层的 HTML 文件,按创建时间(= 模型生成的顺序)排,最多 24 个 */
68
- export function listScreens(root) {
68
+ /** 右侧显示的几屏:超过上限时留最新生成的,最早的先收起来。keep:正打开的那屏,不在里面也留着(挤掉最早的一屏) */
69
+ export function listScreens(root, keep = '') {
70
+ const kept = screensByBirth(root).slice(-MAX_SCREENS)
71
+ if (keep && !kept.includes(keep)) { if (kept.length >= MAX_SCREENS) kept.shift(); kept.push(keep) }
72
+ return inManifestOrder(root, kept)
73
+ }
74
+
75
+ /** 目录里所有的屏。按文件名找、改全套的时候用这个,别漏掉收起来的 */
76
+ export function allScreens(root) {
77
+ return inManifestOrder(root, screensByBirth(root))
78
+ }
79
+
80
+ /** 顶层的 HTML 文件,按创建时间(= 模型生成的顺序)排 */
81
+ function screensByBirth(root) {
69
82
  let dirents
70
83
  try { dirents = fs.readdirSync(root, { withFileTypes: true }) } catch { return [] }
71
84
  const out = []
@@ -80,14 +93,16 @@ export function listScreens(root) {
80
93
  out.push({ name: d.name, born })
81
94
  }
82
95
  out.sort((a, b) => a.born - b.born || a.name.localeCompare(b.name))
83
- // 有设计清单(design.json,从设计库来的模板 / 分享过的设计):按清单里的顺序排,清单外的(模型后加的)按创建时间排在后面
96
+ return out.map((x) => x.name)
97
+ }
98
+
99
+ /** 有设计清单(design.json,从设计库来的模板 / 分享过的设计):按清单里的顺序排,清单外的(模型后加的)按创建时间排在后面 */
100
+ function inManifestOrder(root, names) {
84
101
  const order = manifestOrder(root)
85
- if (order.length) {
86
- const rank = new Map(order.map((f, i) => [f.toLowerCase(), i]))
87
- const listed = out.filter((x) => rank.has(x.name.toLowerCase())).sort((a, b) => rank.get(a.name.toLowerCase()) - rank.get(b.name.toLowerCase()))
88
- return [...listed, ...out.filter((x) => !rank.has(x.name.toLowerCase()))].slice(0, MAX_SCREENS).map((x) => x.name)
89
- }
90
- return out.slice(0, MAX_SCREENS).map((x) => x.name)
102
+ if (!order.length) return names
103
+ const rank = new Map(order.map((f, i) => [f.toLowerCase(), i]))
104
+ const listed = names.filter((x) => rank.has(x.toLowerCase())).sort((a, b) => rank.get(a.toLowerCase()) - rank.get(b.toLowerCase()))
105
+ return [...listed, ...names.filter((x) => !rank.has(x.toLowerCase()))]
91
106
  }
92
107
 
93
108
  /** design.json 里 screens 的文件名(读不了、写错了都当没有) */
@@ -113,7 +128,7 @@ function normRoutes(list) {
113
128
  if (m) s = m[3] || '/'
114
129
  if (!s.startsWith('/')) s = '/' + s
115
130
  if (!out.includes(s)) out.push(s)
116
- if (out.length >= MAX_SCREENS) break
131
+ if (out.length >= MAX_ROUTES) break
117
132
  }
118
133
  return out.length ? out : null
119
134
  }
@@ -161,7 +176,7 @@ export function parseTarget(input, cwd = os.homedir()) {
161
176
  if (st.isDirectory()) {
162
177
  // 有 index.html 开它;没有就开最先做的那屏(设计目录一屏一个文件,画布上会把同目录的几屏都排出来)
163
178
  const index = path.join(p, 'index.html')
164
- const first = fs.existsSync(index) ? 'index.html' : listScreens(p)[0]
179
+ const first = fs.existsSync(index) ? 'index.html' : allScreens(p)[0]
165
180
  if (!first) {
166
181
  try { fs.readdirSync(p) } catch (e) { throw new Error(`读不了这个文件夹(${e.code === 'EACCES' || e.code === 'EPERM' ? '没有权限' : e.code || e.message}):${p}`) }
167
182
  throw new Error(`这个文件夹里没有 HTML 文件:${p}`)
@@ -638,19 +653,24 @@ export function createTracker({ open, drafts = null, skipPorts = () => new Set()
638
653
 
639
654
  function refreshScreens(st) {
640
655
  const t = st.target
656
+ st.hidden = 0
641
657
  if (!t) { st.screens = []; return }
642
658
  if (t.kind === 'file') {
643
- const found = listScreens(t.root)
659
+ const all = allScreens(t.root)
644
660
  const me = path.basename(t.file)
645
- if (!found.includes(me)) found.push(me)
661
+ const found = listScreens(t.root, me)
646
662
  // 已经排好的几屏位置不动:编辑器改文件常是写临时文件再换名,创建时间会变,按它重排画布会乱跳
647
663
  const list = st.screens.filter((k) => found.includes(k))
648
664
  for (const k of found) if (!list.includes(k)) placeScreen(list, k)
649
- st.screens = list.slice(0, MAX_SCREENS)
665
+ st.screens = list
666
+ st.hidden = Math.max(0, all.length + (all.includes(me) ? 0 : 1) - list.length)
650
667
  } else {
651
668
  const list = st.routes ? [...st.routes] : []
652
669
  if (!list.includes(t.path)) list.unshift(t.path)
653
- st.screens = list
670
+ const kept = list.slice(0, MAX_SCREENS)
671
+ if (!kept.includes(t.path)) kept[MAX_SCREENS - 1] = t.path // 正看的这个路由始终在
672
+ st.screens = kept
673
+ st.hidden = list.length - kept.length
654
674
  }
655
675
  }
656
676
 
@@ -705,7 +725,7 @@ export function createTracker({ open, drafts = null, skipPorts = () => new Set()
705
725
  const t = st.target
706
726
  const key = String(focus)
707
727
  if (t.kind === 'file') {
708
- if (!listScreens(t.root).includes(key) && key !== path.basename(t.file)) throw new Error(`没有这一屏:${key}`)
728
+ if (!allScreens(t.root).includes(key) && key !== path.basename(t.file)) throw new Error(`没有这一屏:${key}`)
709
729
  await setTarget(id, { kind: 'file', file: path.join(t.root, key), root: t.root }, { source: st.source, popup: false })
710
730
  } else {
711
731
  if (!key.startsWith('/')) throw new Error(`路由要以 / 开头:${key}`)
@@ -1165,6 +1185,8 @@ export function createTracker({ open, drafts = null, skipPorts = () => new Set()
1165
1185
  origin,
1166
1186
  vrev: st.vrev,
1167
1187
  focus: file ? path.basename(target.file) : target.path,
1188
+ hidden: st.hidden || 0, // 超过上限收起来的屏数
1189
+ maxScreens: MAX_SCREENS,
1168
1190
  screens: st.screens.map((key) => ({
1169
1191
  key,
1170
1192
  url: file ? `${origin}/${encodeURIComponent(key)}` : origin + key,
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 = pages.map(() => add(null)) // 页先占号:页上的链接要指到后面的页
71
- pages.forEach((p, i) => {
72
- const info = jpegInfo(p.jpg)
73
- if (!info) throw new Error('不是 JPEG')
74
- const cs = info.comps === 1 ? '/DeviceGray' : info.comps === 4 ? '/DeviceCMYK' : '/DeviceRGB'
75
- const W = +(p.w * 0.75).toFixed(2)
76
- const H = +(p.h * 0.75).toFixed(2)
77
- 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'])
78
- const draw = `q ${W} 0 0 ${H} 0 0 cm /Im0 Do Q`
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 pt = (v) => +(v * 0.75).toFixed(2)
81
- const annots = pageLinks(p, pages.length, i).map((l) =>
82
- add([`<< /Type /Annot /Subtype /Link /Rect [${pt(l.x)} ${pt(p.h - l.y - l.h)} ${pt(l.x + l.w)} ${pt(p.h - l.y)}] /Border [0 0 0] /Dest [${kids[l.to]} 0 R /Fit] >>`]))
83
- objs[kids[i] - 1] = [`<< /Type /Page /Parent ${tree} 0 R /MediaBox [0 0 ${W} ${H}] /Resources << /XObject << /Im0 ${img} 0 R >> >> /Contents ${content} 0 R` +
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 (pages.length > 1 && marks.length) {
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 { cx, cy } = slideSize(pages[0].w, pages[0].h)
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 = pages.map((p, i) => {
244
- if (!jpegInfo(p.jpg)) throw new Error('不是 JPEG')
245
- const n = i + 1
246
- const k = Math.min(cx / p.w, cy / p.h)
247
- const w = Math.round(p.w * k)
248
- const ht = Math.round(p.h * k)
249
- const x = Math.round((cx - w) / 2)
250
- const y = Math.round((cy - ht) / 2)
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
- const hot = pageLinks(p, pages.length, i).map((l, j) => {
255
- let r = targets.indexOf(l.to)
256
- if (r < 0) { targets.push(l.to); r = targets.length - 1 }
257
- return `<p:sp><p:nvSpPr><p:cNvPr id="${j + 3}" name="Link ${j + 1}"><a:hlinkClick r:id="rId${r + 3}" action="ppaction://hlinksldjump"/></p:cNvPr><p:cNvSpPr/><p:nvPr/></p:nvSpPr>` +
258
- `<p:spPr><a:xfrm><a:off x="${x + Math.round(l.x * k)}" y="${y + Math.round(l.y * k)}"/><a:ext cx="${Math.max(1, Math.round(l.w * k))}" cy="${Math.max(1, Math.round(l.h * k))}"/></a:xfrm>` +
259
- '<a:prstGeom prst="rect"><a:avLst/></a:prstGeom><a:noFill/><a:ln><a:noFill/></a:ln></p:spPr></p:sp>'
260
- }).join('')
261
- files.push({ name: `ppt/media/image${n}.jpeg`, data: p.jpg })
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="${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}<p:pic><p:nvPicPr><p:cNvPr id="2" name="${name}" descr="${name}"/><p:cNvPicPr><a:picLocks noChangeAspect="1"/></p:cNvPicPr><p:nvPr/></p:nvPicPr>` +
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'], ['rId2', `${REL}/image`, `../media/image${n}.jpeg`],
274
- ...targets.map((to, r) => [`rId${r + 3}`, `${REL}/slide`, `slide${to + 1}.xml`])]),
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
  })
@@ -10,7 +10,7 @@
10
10
 
11
11
  import fs from 'node:fs'
12
12
  import path from 'node:path'
13
- import { checkPack, normalizeHtml, titleOf, metaKind, message, LIMITS, FORMAT, pctBytes, imageInfo } from './policy.js'
13
+ import { checkPack, normalizeHtml, titleOf, metaKind, message, LIMITS, capsOf, FORMAT, pctBytes, imageInfo } from './policy.js'
14
14
  import { writeZip } from './zip.js'
15
15
 
16
16
  const PACK_EXT = /\.(html|css|svg|png|jpe?g|webp|gif|avif|json|md)$/i
@@ -237,7 +237,7 @@ export function packDesign(root, { screens, version = '', license = '', author =
237
237
  const { files: picked, skipped, tooBig } = collectDesign(root)
238
238
  const notes = []
239
239
  const errors = []
240
- if (tooBig) errors.push({ code: 'count', n: '2000+', max: LIMITS.files, sev: 'error' })
240
+ if (tooBig) errors.push({ code: 'count', n: '2000+', max: capsOf(mode).files, sev: 'error' })
241
241
  const screenSet = new Set(screens.map((s) => s.toLowerCase()))
242
242
  let existing = null
243
243
  const files = []
@@ -16,8 +16,11 @@ import { scanCss, cssTokens, URL_FUNCS } from './css.js'
16
16
 
17
17
  export const FORMAT = 1
18
18
  export const LIMITS = {
19
- files: 60, // 一套最多几个文件
20
- screens: 24, // 最多几屏(和工作台画布一样)
19
+ files: 60, // 市场里一套最多几个文件(分享、审核)
20
+ screens: 24, // 市场里一套最多几屏(分享、审核)
21
+ localFiles: 200, // 自己打 zip、导入 zip 的上限(屏数和工作台一样)
22
+ localScreens: 60,
23
+ localTotal: 5 * 1024 * 1024,
21
24
  depth: 3, // 目录最多几层
22
25
  html: 200 * 1024,
23
26
  css: 200 * 1024,
@@ -29,6 +32,10 @@ export const LIMITS = {
29
32
  total: 2 * 1024 * 1024,
30
33
  name: 64, // 文件名长度
31
34
  }
35
+ /** 这个模式下的数量上限:进市场的(share / ci)严,只在自己手上的(zip / install)宽 */
36
+ export const capsOf = (mode) => (mode === 'share' || mode === 'ci'
37
+ ? { files: LIMITS.files, screens: LIMITS.screens, total: LIMITS.total }
38
+ : { files: LIMITS.localFiles, screens: LIMITS.localScreens, total: LIMITS.localTotal })
32
39
  export const TYPES = ['template', 'system', 'style']
33
40
  export const LICENSES = ['CC0-1.0', 'CC-BY-4.0']
34
41
  export const KINDS = ['web', 'app', 'miniprogram', 'tablet', 'desktop', 'poster', 'slides', 'email']
@@ -1247,7 +1254,7 @@ export function checkManifest(m, { mode = 'install', vocab = null } = {}) {
1247
1254
  if (m.size !== undefined && !/^\d{3,4}x\d{3,4}$/.test(String(m.size))) bad('size', '写成 1080x1920 这样')
1248
1255
  if (m.os !== undefined && !['mac', 'windows'].includes(m.os)) bad('os', '只能是 mac / windows')
1249
1256
  if (!Array.isArray(m.screens) || !m.screens.length) bad('screens', '至少一屏')
1250
- else if (m.screens.length > LIMITS.screens) bad('screens', `最多 ${LIMITS.screens} 屏`)
1257
+ else if (m.screens.length > capsOf(mode).screens) bad('screens', `最多 ${capsOf(mode).screens} 屏`)
1251
1258
  else {
1252
1259
  const seen = new Set()
1253
1260
  m.screens.forEach((s, i) => {
@@ -1269,7 +1276,7 @@ export function checkManifest(m, { mode = 'install', vocab = null } = {}) {
1269
1276
  if (m.lang !== undefined && !['zh', 'en', 'both'].includes(m.lang)) bad('lang', '只能是 zh / en / both')
1270
1277
  if (m.dark !== undefined && typeof m.dark !== 'boolean') bad('dark', '要是 true / false')
1271
1278
  if (m.assets !== undefined) {
1272
- if (!Array.isArray(m.assets) || m.assets.length > LIMITS.files) bad('assets', '要是列表')
1279
+ if (!Array.isArray(m.assets) || m.assets.length > capsOf(mode).files) bad('assets', '要是列表')
1273
1280
  else m.assets.forEach((a, i) => {
1274
1281
  if (!isObj(a) || typeof a.path !== 'string' || !cleanPath(a.path)) return bad(`assets[${i}]`, '要有 path(包里的图片)')
1275
1282
  if (!['self', 'cc0', 'pd'].includes(a.source)) bad(`assets[${i}].source`, 'self(自己做的)/ cc0 / pd(公有领域)')
@@ -1372,8 +1379,9 @@ export function checkPack(files, { mode = 'install', vocab = null } = {}) {
1372
1379
  bytes += size
1373
1380
  list.push({ path: p, ext, kind, data: f.data, size })
1374
1381
  }
1375
- if (list.length > LIMITS.files) err({ code: 'count', n: list.length, max: LIMITS.files })
1376
- if (bytes > LIMITS.total) err({ code: 'total', n: kb(bytes), max: kb(LIMITS.total) })
1382
+ const caps = capsOf(mode)
1383
+ if (list.length > caps.files) err({ code: 'count', n: list.length, max: caps.files })
1384
+ if (bytes > caps.total) err({ code: 'total', n: kb(bytes), max: kb(caps.total) })
1377
1385
 
1378
1386
  const texts = new Map()
1379
1387
  for (const f of list) {
@@ -16,7 +16,7 @@ import fs from 'node:fs'
16
16
  import os from 'node:os'
17
17
  import path from 'node:path'
18
18
  import { execFile } from 'node:child_process'
19
- import { listScreens } from '../core.js'
19
+ import { listScreens, allScreens } from '../core.js'
20
20
  import { parseMeta, metaOfFile, KIND_SIZE, prepare } from '../look.js'
21
21
  import { packDesign, zipOf, splitTitle } from './pack.js'
22
22
  import { PERSONAL_CODES } from './policy.js'
@@ -215,7 +215,7 @@ export function createLibraryRoutes({ admit, readBody, json, tracker, library, i
215
215
  const snap = body.session ? tracker.snapshot(String(body.session)) : null
216
216
  const open = snap && snap.target && snap.target.kind === 'file' && path.resolve(snap.target.root) === dir
217
217
  if (!open && !byDir(dir)) return json(res, 404, { error: 'not a design here' })
218
- const screens = listScreens(dir)
218
+ const screens = allScreens(dir) // 打包按整套算:屏数超了要明确报,不能悄悄少几屏
219
219
  if (!screens.length) return json(res, 400, { error: 'noScreens' })
220
220
  const local = localManifest(dir)
221
221
  // 分享向导先要一份预填:名字、介绍、授权、分享过没有
package/lib/handoff.js CHANGED
@@ -14,7 +14,7 @@ import os from 'node:os'
14
14
  import path from 'node:path'
15
15
  import { pathToFileURL } from 'node:url'
16
16
  import { prepare } from './look.js'
17
- import { listScreens } from './core.js'
17
+ import { allScreens } from './core.js'
18
18
  import { readTweaks, shownValue } from './tweaks.js'
19
19
  import { tokensOf } from './system.js'
20
20
 
@@ -827,5 +827,5 @@ export async function compareScreens(browser, { design, impl, size, at = 'top',
827
827
 
828
828
  /** 设计目录里的几屏(交接、对比时按文件名找) */
829
829
  export function screensOf(root) {
830
- return listScreens(root)
830
+ return allScreens(root)
831
831
  }
package/lib/index.js CHANGED
@@ -169,8 +169,9 @@ export const previewTool = {
169
169
  const st = await tracker.setTarget(id, target, opts)
170
170
  const what = target.kind === 'file' ? target.file : `localhost:${target.port}${target.path}`
171
171
  const many = target.kind === 'file' ? '同目录再写别的 HTML 文件,会作为新的一屏并排出现。' : ''
172
+ const over = st.hidden ? `右侧最多显示 ${st.screens.length} 屏,还有 ${st.hidden} 屏没显示。` : ''
172
173
  return {
173
- text: `已在右侧「设计工作台」打开 ${what}。${target.kind === 'file' ? '这个目录里的文件一改就自动刷新。' + many : '开发服务器的热更新照常生效。'}`,
174
+ text: `已在右侧「设计工作台」打开 ${what}。${target.kind === 'file' ? '这个目录里的文件一改就自动刷新。' + many : '开发服务器的热更新照常生效。'}${over}`,
174
175
  url: st.url,
175
176
  }
176
177
  },
@@ -1078,6 +1079,14 @@ export function apply(ctx) {
1078
1079
  }
1079
1080
  }
1080
1081
 
1082
+ /** 导出途中换了设计:还按开始时的那套比一下,对不上就不存(不然 A 的文件会落到 B 的 exports/ 里) */
1083
+ const targetKeyOf = (t) => (!t ? '' : t.kind === 'file' ? `file|${t.root}` : `proxy|${t.port}`)
1084
+ function targetMoved(res, session, expect) {
1085
+ if (typeof expect !== 'string' || !expect || expect === targetKeyOf(tracker.stateOf(session).target)) return false
1086
+ json(res, 409, { error: 'designChanged' })
1087
+ return true
1088
+ }
1089
+
1081
1090
  async function exportRoute(req, res) {
1082
1091
  if (!admit(req, res)) return
1083
1092
  if (req.method !== 'POST') return json(res, 405, { error: 'method not allowed' })
@@ -1085,6 +1094,7 @@ export function apply(ctx) {
1085
1094
  try { body = await readBody(req, MAX_EXPORT) } catch (e) { return json(res, 400, { error: e.message }) }
1086
1095
  const buf = Buffer.from(String(body.png || ''), 'base64')
1087
1096
  if (buf.length < 8 || buf.readUInt32BE(0) !== 0x89504e47) return json(res, 400, { error: '不是 PNG' })
1097
+ if (targetMoved(res, String(body.session || ''), body.target)) return
1088
1098
  saveExport(res, String(body.session || ''), body.name, 'png', buf)
1089
1099
  }
1090
1100
 
@@ -1112,10 +1122,13 @@ export function apply(ctx) {
1112
1122
  const title = String(body.title || '').slice(0, 120)
1113
1123
  let buf
1114
1124
  try {
1115
- buf = format === 'pdf' ? makePdf(pages, { title }) : makePptx(pages, { title, bg: String(body.bg || '') })
1125
+ const bg = String(body.bg || '')
1126
+ const opts = { layout: String(body.layout || '').slice(0, 20), cols: body.cols === 2 ? 2 : 1 }
1127
+ buf = format === 'pdf' ? makePdf(pages, { title, bg, ...opts }) : makePptx(pages, { title, bg, ...opts })
1116
1128
  } catch (e) {
1117
1129
  return json(res, 400, { error: e.message })
1118
1130
  }
1131
+ if (targetMoved(res, String(body.session || ''), body.target)) return
1119
1132
  saveExport(res, String(body.session || ''), body.name, format, buf)
1120
1133
  }
1121
1134