dsh-ppt 0.34.0 → 0.35.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.
@@ -0,0 +1,170 @@
1
+ /**
2
+ * deck-i18n.mjs —— 100% 版式无损翻译提取与双语化引擎(零新增运行时依赖)。
3
+ *
4
+ * 解决痛点:
5
+ * 把一整套做好的中文 PPT 翻译成英文(或做中英双语版)时,直接让 AI 重写 JSON 极易丢图表、乱坐标、破坏配图与 `[E#]` 溯源。
6
+ * 本模块提供确定性双步接口:
7
+ * 1. `extractTranslationBundle(manifest)` —— 只抽离可翻译文案槽位(标题/副标题/kicker/takeaway/source/bullets/items/rows/notes);
8
+ * 2. `applyTranslationBundle(manifest, translations, { mode: 'replace' | 'bilingual', targetLang })` ——
9
+ * 按槽位精确回填(支持纯替换或中英双语安全拼排),100% 锁死原版式、图表数值、嵌入图片、品牌与证据溯源!
10
+ */
11
+
12
+ function cleanStr(val) {
13
+ return String(val ?? '').replace(/\s+/g, ' ').trim()
14
+ }
15
+
16
+ function trimSafe(str, maxChars) {
17
+ const s = cleanStr(str)
18
+ if (s.length <= maxChars) return s
19
+ return s.slice(0, Math.max(4, maxChars - 1)).trim() + '…'
20
+ }
21
+
22
+ function combineBilingual(orig, trans, sep = ' / ', maxTotal = 160) {
23
+ const a = cleanStr(orig)
24
+ const b = cleanStr(trans)
25
+ if (!b) return a
26
+ if (!a || a.toLowerCase() === b.toLowerCase()) return b
27
+ const joined = `${a}${sep}${b}`
28
+ if (joined.length <= maxTotal) return joined
29
+ const half = Math.max(12, Math.floor((maxTotal - sep.length) / 2))
30
+ return `${trimSafe(a, half)}${sep}${trimSafe(b, half)}`
31
+ }
32
+
33
+ /**
34
+ * 提取工程内所有可翻译文本槽位,返回精简、可直接交给 AI 翻译的 JSON Bundle。
35
+ * @param {object} manifest
36
+ */
37
+ export function extractTranslationBundle(manifest) {
38
+ const slides = Array.isArray(manifest?.slides) ? manifest.slides : []
39
+ return {
40
+ schemaVersion: 1,
41
+ deckTitle: String(manifest?.title || ''),
42
+ sourceLanguage: String(manifest?.language || 'zh'),
43
+ slideCount: slides.length,
44
+ slides: slides.map((s, idx) => {
45
+ const fields = {}
46
+ for (const k of ['kicker', 'title', 'subtitle', 'text', 'takeaway', 'source', 'notes']) {
47
+ if (typeof s[k] === 'string' && s[k].trim()) fields[k] = s[k]
48
+ }
49
+ if (Array.isArray(s.bullets) && s.bullets.length > 0) {
50
+ fields.bullets = [...s.bullets]
51
+ }
52
+ if (Array.isArray(s.items) && s.items.length > 0) {
53
+ fields.items = s.items.map((it) => {
54
+ const out = {}
55
+ for (const k of ['tag', 'title', 'text', 'delta']) {
56
+ if (typeof it?.[k] === 'string' && it[k].trim()) out[k] = it[k]
57
+ }
58
+ if (Array.isArray(it?.bullets) && it.bullets.length > 0) out.bullets = [...it.bullets]
59
+ return out
60
+ })
61
+ }
62
+ if (Array.isArray(s.rows) && s.rows.length > 0) {
63
+ fields.rows = s.rows.map(r => (Array.isArray(r) ? r.map(c => String(c ?? '')) : [String(r ?? '')]))
64
+ }
65
+ return {
66
+ slide: idx + 1,
67
+ id: s.id,
68
+ layout: s.layout || 'bullets',
69
+ fields,
70
+ }
71
+ }),
72
+ }
73
+ }
74
+
75
+ /**
76
+ * 将翻译结果无损回填至工程 manifest,100% 保留版式、图表、图片、品牌、证据与 dataModel。
77
+ * @param {object} manifest
78
+ * @param {object|Array} translations
79
+ * @param {{ mode?: 'replace' | 'bilingual', targetLang?: string }} [options]
80
+ */
81
+ export function applyTranslationBundle(manifest, translations, options = {}) {
82
+ if (!manifest || !Array.isArray(manifest.slides)) {
83
+ throw new Error('dsh-ppt:applyTranslationBundle 需要有效的工程 manifest')
84
+ }
85
+ const mode = String(options.mode || 'replace').toLowerCase() === 'bilingual' ? 'bilingual' : 'replace'
86
+ const next = structuredClone(manifest)
87
+ const slideEntries = Array.isArray(translations)
88
+ ? translations
89
+ : (Array.isArray(translations?.slides) ? translations.slides : [])
90
+
91
+ if (translations && !Array.isArray(translations) && typeof translations.deckTitle === 'string' && translations.deckTitle.trim()) {
92
+ next.title = mode === 'bilingual'
93
+ ? combineBilingual(next.title, translations.deckTitle, ' · ', 110)
94
+ : cleanStr(translations.deckTitle)
95
+ }
96
+
97
+ const resolveField = (orig, translated, maxTotal = 160, sep = ' / ') => {
98
+ if (typeof translated !== 'string' || !translated.trim()) return orig
99
+ return mode === 'bilingual' ? combineBilingual(orig, translated, sep, maxTotal) : translated
100
+ }
101
+
102
+ for (let i = 0; i < slideEntries.length; i += 1) {
103
+ const entry = slideEntries[i]
104
+ if (!entry || typeof entry !== 'object') continue
105
+ let targetIdx = -1
106
+ if (typeof entry.id === 'string') {
107
+ targetIdx = next.slides.findIndex(s => s.id === entry.id)
108
+ }
109
+ if (targetIdx < 0 && Number.isInteger(entry.slide) && entry.slide >= 1 && entry.slide <= next.slides.length) {
110
+ targetIdx = entry.slide - 1
111
+ }
112
+ if (targetIdx < 0 && i < next.slides.length) {
113
+ targetIdx = i
114
+ }
115
+ if (targetIdx < 0) continue
116
+
117
+ const slide = next.slides[targetIdx]
118
+ const f = entry.fields && typeof entry.fields === 'object' ? entry.fields : entry
119
+
120
+ if (typeof f.kicker === 'string') slide.kicker = resolveField(slide.kicker, f.kicker, 64, ' · ')
121
+ if (typeof f.title === 'string') slide.title = resolveField(slide.title, f.title, 76, ' · ')
122
+ if (typeof f.subtitle === 'string') slide.subtitle = resolveField(slide.subtitle, f.subtitle, 130, ' / ')
123
+ if (typeof f.text === 'string') slide.text = resolveField(slide.text, f.text, 180, ' / ')
124
+ if (typeof f.takeaway === 'string') slide.takeaway = resolveField(slide.takeaway, f.takeaway, 150, ' / ')
125
+ if (typeof f.source === 'string') slide.source = resolveField(slide.source, f.source, 110, ' / ')
126
+ if (typeof f.notes === 'string') slide.notes = resolveField(slide.notes, f.notes, 2000, '\n\n')
127
+
128
+ if (Array.isArray(f.bullets) && Array.isArray(slide.bullets)) {
129
+ slide.bullets = slide.bullets.map((orig, bi) => resolveField(orig, f.bullets[bi], 120, ' / '))
130
+ }
131
+
132
+ if (Array.isArray(f.items) && Array.isArray(slide.items)) {
133
+ slide.items = slide.items.map((origItem, ii) => {
134
+ const patchItem = f.items[ii]
135
+ if (!patchItem || typeof patchItem !== 'object') return origItem
136
+ const updated = { ...origItem }
137
+ if (typeof patchItem.tag === 'string') updated.tag = resolveField(origItem.tag, patchItem.tag, 36, ' · ')
138
+ if (typeof patchItem.title === 'string') updated.title = resolveField(origItem.title, patchItem.title, 54, ' / ')
139
+ if (typeof patchItem.text === 'string') updated.text = resolveField(origItem.text, patchItem.text, 110, ' / ')
140
+ if (typeof patchItem.delta === 'string') updated.delta = resolveField(origItem.delta, patchItem.delta, 40, ' / ')
141
+ if (Array.isArray(patchItem.bullets) && Array.isArray(origItem.bullets)) {
142
+ updated.bullets = origItem.bullets.map((b, bi) => resolveField(b, patchItem.bullets[bi], 96, ' / '))
143
+ }
144
+ return updated
145
+ })
146
+ }
147
+
148
+ if (Array.isArray(f.rows) && Array.isArray(slide.rows)) {
149
+ slide.rows = slide.rows.map((origRow, ri) => {
150
+ const patchRow = f.rows[ri]
151
+ if (!Array.isArray(origRow) || !Array.isArray(patchRow)) return origRow
152
+ return origRow.map((cell, ci) => {
153
+ const translated = patchRow[ci]
154
+ if (typeof translated !== 'string' || !translated.trim()) return cell
155
+ // Preserve pure numeric cells untouched in bilingual mode
156
+ if (/^[+-]?[\d,.$¥€£%%]+$/.test(cleanStr(cell)) && cleanStr(cell) === cleanStr(translated)) return cell
157
+ return mode === 'bilingual' ? combineBilingual(cell, translated, ' / ', 42) : translated
158
+ })
159
+ })
160
+ }
161
+ }
162
+
163
+ if (mode === 'bilingual') {
164
+ next.language = 'bilingual'
165
+ } else if (options.targetLang && ['zh', 'en', 'bilingual'].includes(options.targetLang)) {
166
+ next.language = options.targetLang
167
+ }
168
+
169
+ return next
170
+ }
@@ -134,6 +134,16 @@ import {
134
134
  renderInfographicSlideBodyHtml,
135
135
  INFOGRAPHICS_CSS,
136
136
  } from './deck-infographics.mjs'
137
+ import {
138
+ EXEC_CHART_LAYOUTS,
139
+ computeIssueTreeModel,
140
+ computeTreemapModel,
141
+ computeGaugeModel,
142
+ computePricingModel,
143
+ renderExecChartSlideBodyPptx,
144
+ renderExecChartSlideBodyHtml,
145
+ EXEC_CHARTS_CSS,
146
+ } from './deck-charts-exec.mjs'
137
147
 
138
148
  export {
139
149
  CANVAS_IN,
@@ -206,6 +216,12 @@ export {
206
216
  generateVisualBrief,
207
217
  normalizeImageBrief,
208
218
  INFOGRAPHICS_CSS,
219
+ EXEC_CHART_LAYOUTS,
220
+ computeIssueTreeModel,
221
+ computeTreemapModel,
222
+ computeGaugeModel,
223
+ computePricingModel,
224
+ EXEC_CHARTS_CSS,
209
225
  }
210
226
 
211
227
  export const CORE_LAYOUTS = new Set([
@@ -239,6 +255,10 @@ export const CORE_LAYOUTS = new Set([
239
255
  'heatmap',
240
256
  'map',
241
257
  'isometric',
258
+ 'issue-tree',
259
+ 'treemap',
260
+ 'gauge',
261
+ 'pricing',
242
262
  'table',
243
263
  'closing',
244
264
  ])
@@ -260,6 +280,14 @@ export const LAYOUT_ALIASES = Object.freeze({
260
280
  'iso-layers': { layout: 'isometric', variant: 'stack' },
261
281
  '3d-stack': { layout: 'isometric', variant: 'stack' },
262
282
  'iso-pillars': { layout: 'isometric', variant: 'pillars' },
283
+ mece: { layout: 'issue-tree' },
284
+ 'logic-tree': { layout: 'issue-tree' },
285
+ 'rect-map': { layout: 'treemap' },
286
+ 'budget-map': { layout: 'treemap' },
287
+ 'bullet-chart': { layout: 'gauge' },
288
+ 'okr-gauge': { layout: 'gauge' },
289
+ tiers: { layout: 'pricing' },
290
+ packages: { layout: 'pricing' },
263
291
  'kpi-grid': { layout: 'kpi', variant: 'grid' },
264
292
  scorecard: { layout: 'kpi', variant: 'grid' },
265
293
  bento: { layout: 'cards', variant: 'bento' },
@@ -950,6 +978,22 @@ export function renderCoreSlidePptx(slide, index, total, theme, langAttr, manife
950
978
  return shapes.join('')
951
979
  }
952
980
 
981
+ const execChartShapes = renderExecChartSlideBodyPptx(slide, {
982
+ theme,
983
+ langAttr,
984
+ chromeCfg,
985
+ nextId,
986
+ bodyTopY,
987
+ bodyBottomY,
988
+ bodyH,
989
+ cardMaterial: taste.cardMaterial,
990
+ })
991
+ if (execChartShapes !== null) {
992
+ shapes.push(execChartShapes)
993
+ shapes.push(renderSlideChromePptx(slide, index, total, theme, langAttr, manifest, nextId))
994
+ return shapes.join('')
995
+ }
996
+
953
997
  const diagramShapes = renderDiagramSlideBodyPptx(slide, {
954
998
  theme,
955
999
  langAttr,
@@ -2136,6 +2180,7 @@ export function renderCoreSlideHtml(slide, index, total, ui, langId, manifest =
2136
2180
  const showcaseHtml = renderShowcaseSlideBodyHtml(slide, { theme, langId, manifest })
2137
2181
  const proChartHtml = renderProChartSlideBodyHtml(slide, { theme, langId, manifest })
2138
2182
  const infographicHtml = renderInfographicSlideBodyHtml(slide, { theme, langId, manifest })
2183
+ const execChartHtml = renderExecChartSlideBodyHtml(slide, { theme, langId, manifest })
2139
2184
  const diagramHtml = renderDiagramSlideBodyHtml(slide, { theme, langId, chromeStyle })
2140
2185
 
2141
2186
  if (layout === 'cover') {
@@ -2224,6 +2269,8 @@ export function renderCoreSlideHtml(slide, index, total, ui, langId, manifest =
2224
2269
  inner = renderHeader() + proChartHtml
2225
2270
  } else if (infographicHtml !== null) {
2226
2271
  inner = renderHeader() + infographicHtml
2272
+ } else if (execChartHtml !== null) {
2273
+ inner = renderHeader() + execChartHtml
2227
2274
  } else if (diagramHtml !== null) {
2228
2275
  inner = renderHeader(layout === 'harvey' ? ui.tableKicker : '') + diagramHtml
2229
2276
  } else if (layout === 'table' && (slide.tableStyle || slide.visual?.tableStyle) === 'harvey') {
@@ -2469,7 +2516,7 @@ export function renderCoreSlideHtml(slide, index, total, ui, langId, manifest =
2469
2516
  return inner + renderSlideChromeHtml(slide, chromeStyle, folioText, langId, manifest, isFraming)
2470
2517
  }
2471
2518
 
2472
- export const LAYOUT_CSS = `
2519
+ const BASE_LAYOUT_CSS = `
2473
2520
  .slide-inner{display:flex;flex-direction:column;justify-content:center;width:100%;max-width:1560px;margin:0 auto;height:100%;gap:clamp(16px,2vw,28px)}
2474
2521
  .slide--cover .slide-inner,.slide--closing .slide-inner,.slide--section .slide-inner,.slide--statement .slide-inner,.slide--quote .slide-inner{justify-content:center}
2475
2522
  .slide-header{border-bottom:1px solid var(--hairline);padding-bottom:clamp(12px,1.5vw,20px)}
@@ -2582,13 +2629,6 @@ math.deck-math.is-block{display:block;font-size:clamp(20px,2.2vw,32px);margin:4p
2582
2629
  math.deck-math.is-inline{font-size:1.06em;vertical-align:middle}
2583
2630
  .callout-hero.is-omath{padding:22px 30px;display:flex;align-items:center;justify-content:center}
2584
2631
  .formula-caption{text-align:center;color:var(--ink-secondary);font-size:clamp(14px,1.35vw,18px);margin-top:-6px}
2585
- ${AESTHETIC_STUDIO_CSS}
2586
- ${SHOWCASE_CSS}
2587
- ${PRO_CHARTS_CSS}
2588
- ${INFOGRAPHICS_CSS}
2589
- ${DIAGRAMS_CSS}
2590
- ${CHROME_CSS}
2591
- ${MERMAID_CSS}
2592
2632
  @media (max-width:900px){
2593
2633
  .cols-3,.cols-4,.cols-5,.cols-6,.bento-grid.bento-3,.bento-grid.bento-4,.bento-grid.bento-5,.bento-grid.bento-6,.bento-hero-grid,.kpi-spotlight,.section-agenda-shell,.cover-shell.is-architectural,.ed-main-grid,.ed-meta-strip,.tam-board,.arch-board.has-sidecar,.org-children,.flywheel-stage,.mekko-board,.porter-board,.swot-cross{grid-template-columns:1fr}
2594
2634
  .bento-grid.bento-3 > :first-child,.bento-grid.bento-4 > *,.bento-hero-grid > :first-child{grid-row:auto;grid-column:auto}
@@ -2596,3 +2636,27 @@ ${MERMAID_CSS}
2596
2636
  .flywheel-hub,.swot-anchor{position:static;transform:none;margin:0 auto}
2597
2637
  }
2598
2638
  `
2639
+
2640
+ export const LAYOUT_CSS = `${BASE_LAYOUT_CSS}\n${AESTHETIC_STUDIO_CSS}\n${SHOWCASE_CSS}\n${PRO_CHARTS_CSS}\n${INFOGRAPHICS_CSS}\n${EXEC_CHARTS_CSS}\n${DIAGRAMS_CSS}\n${CHROME_CSS}\n${MERMAID_CSS}`
2641
+
2642
+ export function buildLayoutCss(slides = []) {
2643
+ const list = Array.isArray(slides) ? slides : []
2644
+ const used = new Set(list.map((s) => s?.layout || 'bullets'))
2645
+ const hasShowcase = [...SHOWCASE_LAYOUTS].some((l) => used.has(l))
2646
+ const hasPro = [...PRO_CHART_LAYOUTS].some((l) => used.has(l))
2647
+ const hasInfo = [...INFOGRAPHIC_LAYOUTS].some((l) => used.has(l)) || list.some((s) => Boolean(s?.imageBrief))
2648
+ const hasExec = [...EXEC_CHART_LAYOUTS].some((l) => used.has(l))
2649
+ const hasDiag = [...DIAGRAM_LAYOUTS].some((l) => used.has(l)) || list.some((s) => (s?.tableStyle || s?.visual?.tableStyle) === 'harvey')
2650
+ const hasMermaid = used.has('mermaid') || list.some((s) => Boolean(s?.mermaid))
2651
+ return [
2652
+ BASE_LAYOUT_CSS,
2653
+ CHROME_CSS,
2654
+ AESTHETIC_STUDIO_CSS,
2655
+ hasShowcase ? SHOWCASE_CSS : '',
2656
+ hasPro ? PRO_CHARTS_CSS : '',
2657
+ hasInfo ? INFOGRAPHICS_CSS : '',
2658
+ hasExec ? EXEC_CHARTS_CSS : '',
2659
+ hasDiag ? DIAGRAMS_CSS : '',
2660
+ hasMermaid ? MERMAID_CSS : '',
2661
+ ].filter(Boolean).join('\n')
2662
+ }