diff --git a/kits/slides/app/package.json b/kits/slides/app/package.json index 4b7bc20..4b88856 100644 --- a/kits/slides/app/package.json +++ b/kits/slides/app/package.json @@ -5,7 +5,8 @@ "type": "module", "scripts": { "dev": "vite", - "build": "vite build" + "build": "vite build", + "test": "node --test src/lib/*.test.mjs" }, "dependencies": { "echarts": "^5.5.1", diff --git a/kits/slides/app/src/lib/deckContent.js b/kits/slides/app/src/lib/deckContent.js new file mode 100644 index 0000000..37b5e9a --- /dev/null +++ b/kits/slides/app/src/lib/deckContent.js @@ -0,0 +1,19 @@ +// What an element carries, read the way the deck view draws it, so an export cannot leave out what the deck +// shows. The deck model (plugin/skills/slide-design/deck_model.py) is the schema and reifyui's slide elements +// are the renderer: a flowchart's Mermaid source is content.mermaid and a code block's text is content.source. +// +// PowerPoint export used to read content.code / content.text for a flowchart and content.runs for a code +// block. No deck has those, so every diagram and every code block was left out of the .pptx, and nothing +// said so. + +/** The Mermaid source of a flowchart element, or '' when it has none. */ +export function flowchartSource(el) { + const src = el?.content?.mermaid; + return typeof src === 'string' ? src.trim() && src : ''; +} + +/** A code element's text as one run (its line breaks kept), or [] when it has none. */ +export function codeRuns(el) { + const src = el?.content?.source; + return typeof src === 'string' && src.trim() ? [{ text: src }] : []; +} diff --git a/kits/slides/app/src/lib/deckContent.test.mjs b/kits/slides/app/src/lib/deckContent.test.mjs new file mode 100644 index 0000000..f0b9857 --- /dev/null +++ b/kits/slides/app/src/lib/deckContent.test.mjs @@ -0,0 +1,30 @@ +import { test } from 'node:test'; +import assert from 'node:assert/strict'; +import { codeRuns, flowchartSource } from './deckContent.js'; + +// The shapes the agent writes, from the deck model: these are what the deck view draws. +const flow = { type: 'flowchart', content: { mermaid: 'flowchart LR\n A["Introduced"] --> B["Committee"]' } }; +const code = { type: 'code', content: { lang: 'python', source: 'def add(a, b):\n return a + b\n' } }; + +test('a flowchart is exported from its Mermaid source', () => { + assert.equal(flowchartSource(flow), flow.content.mermaid); +}); + +test('a code block is exported from its source, line breaks and all', () => { + assert.deepEqual(codeRuns(code), [{ text: code.content.source }]); +}); + +test('fields the deck view does not draw are not exported either', () => { + // the old readers' fields: a deck carrying only these shows nothing on the slide + assert.equal(flowchartSource({ type: 'flowchart', content: { code: 'flowchart LR\n A --> B' } }), ''); + assert.deepEqual(codeRuns({ type: 'code', content: { runs: [{ text: 'x = 1' }] } }), []); +}); + +test('an element with nothing to show gives nothing, so the export can say it left it out', () => { + for (const el of [{}, { content: null }, { content: { mermaid: ' ' } }, { content: { mermaid: 42 } }]) { + assert.equal(flowchartSource(el), ''); + } + for (const el of [{}, { content: {} }, { content: { source: '\n' } }]) { + assert.deepEqual(codeRuns(el), []); + } +}); diff --git a/kits/slides/app/src/lib/exportPptx.js b/kits/slides/app/src/lib/exportPptx.js index 05429aa..a3b1714 100644 --- a/kits/slides/app/src/lib/exportPptx.js +++ b/kits/slides/app/src/lib/exportPptx.js @@ -11,6 +11,7 @@ // pptxgenjs is imported by this module ONLY, so it lands in its own chunk and costs nothing until // someone actually exports. import { ROLE_STYLE } from 'reifyui/slides'; // one source of truth — never re-declare it here +import { codeRuns, flowchartSource } from './deckContent.js'; const PX_IN = 13.333 / 1920; const inch = (v) => +((v || 0) * PX_IN).toFixed(4); @@ -139,7 +140,7 @@ async function addChart(slide, el, notes) { } async function addFlowchart(slide, el, notes) { - const src = (el.content || {}).code || (el.content || {}).text; + const src = flowchartSource(el); if (!src) return false; const mermaid = (await import('mermaid')).default; // htmlLabels:false matters — with it on, mermaid emits , the one shape that @@ -236,7 +237,7 @@ export async function downloadPptx(deck, resolveSrc, onProgress) { } else if (el.type === 'chart') { await addChart(slide, el, notes); } else if (el.type === 'flowchart') { - await addFlowchart(slide, el, notes); + if (!(await addFlowchart(slide, el, notes))) notes.dropped.push(`a diagram on slide ${i + 1} with no Mermaid source`); } else if (el.type === 'embed') { const html = el.content?.html || ''; if (/ ({ ...p, options: { ...p.options, fontFace: 'Consolas' } })), { ...frameOf(el), valign: 'top', margin: 0, inset: 0, isTextBox: true, fit: 'none' }); + } else { + notes.dropped.push(`a code block on slide ${i + 1} with no text`); } } } catch (e) {