@treasure-ai/treasure-deck 2026.11.0 → 2026.11.2
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/dist/build.d.ts +3 -0
- package/dist/build.js +4 -2
- package/dist/build.js.map +1 -1
- package/dist/cli.js +77 -1
- package/dist/cli.js.map +1 -1
- package/dist/deckstyle.d.ts +137 -0
- package/dist/deckstyle.js +722 -0
- package/dist/deckstyle.js.map +1 -0
- package/dist/dev.d.ts +6 -0
- package/dist/dev.js +27 -1
- package/dist/dev.js.map +1 -1
- package/dist/parse.d.ts +6 -0
- package/dist/parse.js +15 -0
- package/dist/parse.js.map +1 -1
- package/dist/render.d.ts +10 -1
- package/dist/render.js +16 -3
- package/dist/render.js.map +1 -1
- package/dist/slides.d.ts +168 -0
- package/dist/slides.js +615 -0
- package/dist/slides.js.map +1 -0
- package/dist/slidesexport.d.ts +32 -0
- package/dist/slidesexport.js +44 -0
- package/dist/slidesexport.js.map +1 -0
- package/dist/slideslayout.d.ts +68 -0
- package/dist/slideslayout.js +588 -0
- package/dist/slideslayout.js.map +1 -0
- package/dist/themecheck.d.ts +1 -1
- package/docs/commands.md +22 -0
- package/docs/editor.md +21 -3
- package/docs/syntax.md +4 -1
- package/docs/themes.md +1 -0
- package/package.json +1 -1
- package/runtime/base.css +12 -8
- package/runtime/edit-core.cjs +58 -0
- package/runtime/embed.js +363 -54
- package/skills/treasure-deck/SKILL.md +3 -3
- package/templates/specimen/specimen.md +2 -0
- package/themes/editorial/theme.css +1 -1
- package/themes/gold/theme.css +2 -2
- package/themes/treasure-ai/theme.css +2 -2
- package/themes/treasure-ai-2026/theme.css +4 -4
- package/themes/wvlet/theme.css +2 -2
|
@@ -0,0 +1,588 @@
|
|
|
1
|
+
import { LIGATURE_FONTS, SYSTEM_FONTS } from './deckstyle.js';
|
|
2
|
+
import { createHash } from 'node:crypto';
|
|
3
|
+
import { DeckError } from './parse.js';
|
|
4
|
+
import { slideNotes, slidesFont } from './slides.js';
|
|
5
|
+
/**
|
|
6
|
+
* `deck slides` from the page as Chrome draws it: what the theme draws behind the content (background, ornaments,
|
|
7
|
+
* footer) is a layout's background, shared by the slides with the same picture, its title placeholder placed and
|
|
8
|
+
* styled as theirs. On each slide: its title in the placeholder, the pictures its content draws (panels, rules,
|
|
9
|
+
* images, diagrams), and, laid out as in Google Slides, one text box per column, card, header, or code block, its
|
|
10
|
+
* blocks as paragraphs (lists as Slides lists), spaced and indented as on the page; tables as native tables. Sizes,
|
|
11
|
+
* weights, and colors are the page's.
|
|
12
|
+
*/
|
|
13
|
+
/** EMU per CSS px: the 1280 px wide slide is Google's 10 in (9,144,000 EMU) wide page. */
|
|
14
|
+
const EMU_PX = 9144000 / 1280;
|
|
15
|
+
/** Points per CSS px, on the same scale (720 pt across). */
|
|
16
|
+
const PT_PX = 720 / 1280;
|
|
17
|
+
/** A text box's inner margins in Google Slides (0.1 in each side, 0.05 in top and bottom), in px. */
|
|
18
|
+
const INSET_X = 12.8;
|
|
19
|
+
const INSET_Y = 6.4;
|
|
20
|
+
/** How far below a text box's top inset Slides draws its first line's glyphs, in px (measured, Inter 12–60 px). */
|
|
21
|
+
const GLYPH_GAP = 5;
|
|
22
|
+
/** How much of what a line under 100% spacing lacks Slides takes off the line's height (measured on table rows). */
|
|
23
|
+
const SQUEEZE = 0.75;
|
|
24
|
+
/**
|
|
25
|
+
* How far short of what such a line lacks Slides pulls its glyphs up, in px: they go up by all of it but this
|
|
26
|
+
* (measured: 2.6 → 1, 4.6 → 3, 7.9 → 6, 13.2 → 10, 40.4 → 38.3 px, Inter 12–170 px).
|
|
27
|
+
*/
|
|
28
|
+
const PULL_SHORT = 1.6;
|
|
29
|
+
/**
|
|
30
|
+
* Fonts differ in width from the browser's (Inter stands in for the system font, and Slides has no letter-spacing):
|
|
31
|
+
* room so a line that fits there doesn't wrap here. A single line gets more, as nothing below it moves. Text is set
|
|
32
|
+
* to the page's width (its scale, chips' padding), so wrapping text gets little: more lets Slides fit words on a line
|
|
33
|
+
* that the page wraps (measured with scripts/slides-regress.ts: 4% → 1.5% made 59 of 182 slides closer, 4 farther).
|
|
34
|
+
*/
|
|
35
|
+
const SLACK = 0.015;
|
|
36
|
+
const SLACK_LINE = 0.3;
|
|
37
|
+
const PAGE_W = 1280;
|
|
38
|
+
/** A Slides table cell's padding, top and bottom together, in px. */
|
|
39
|
+
const CELL_PAD_Y = 24;
|
|
40
|
+
/** The object ID that stands for a picture until it's uploaded and has a URL (`exportSlides` swaps them). */
|
|
41
|
+
export const imageRef = (index) => `deck-image:${index}`;
|
|
42
|
+
const emu = (px) => ({ magnitude: Math.round(px * EMU_PX), unit: 'EMU' });
|
|
43
|
+
const pt = (px) => ({ magnitude: Math.round(px * PT_PX * 10) / 10, unit: 'PT' });
|
|
44
|
+
const SYSTEM = new RegExp(`^(${SYSTEM_FONTS})`, 'i');
|
|
45
|
+
const LIGATURE = new RegExp(`^(${LIGATURE_FONTS})`, 'i');
|
|
46
|
+
/**
|
|
47
|
+
* A family Google Slides has for a CSS font-family list: system UI fonts become Inter, the closest Google Font; where
|
|
48
|
+
* the page turns ligatures off, a ligature code font becomes Roboto Mono, so code shows as typed.
|
|
49
|
+
*/
|
|
50
|
+
/** Japanese and Chinese characters (kana, CJK ideographs, half-width katakana), and Korean. */
|
|
51
|
+
const CJK = /[\u3040-\u30ff\u3400-\u9fff\uf900-\ufaff\uff66-\uff9f]/;
|
|
52
|
+
const HANGUL = /[\uac00-\ud7af\u1100-\u11ff]/;
|
|
53
|
+
/** CJK families Google Slides has. */
|
|
54
|
+
const CJK_IN_SLIDES = /^(Noto (Sans|Serif) (JP|KR|SC|TC)|M PLUS|BIZ UD|Zen |Sawarabi|Kosugi|Kiwi Maru|Shippori)/i;
|
|
55
|
+
/**
|
|
56
|
+
* The CJK font for text with CJK characters: the list's first that Slides has, else Noto Serif JP for a Mincho
|
|
57
|
+
* (serif) list and Noto Sans JP otherwise, as Slides would draw them in a font of its own choosing (denser than the
|
|
58
|
+
* page's Hiragino or Noto). Korean text gets Noto Sans KR.
|
|
59
|
+
*/
|
|
60
|
+
function cjkFont(list, text) {
|
|
61
|
+
if (HANGUL.test(text))
|
|
62
|
+
return 'Noto Sans KR';
|
|
63
|
+
if (!CJK.test(text))
|
|
64
|
+
return undefined;
|
|
65
|
+
const families = list.split(',').map((f) => f.trim().replace(/^['"]|['"]$/g, ''));
|
|
66
|
+
return families.find((f) => CJK_IN_SLIDES.test(f)) ?? (/Mincho|Serif|Ming|serif$/i.test(list) ? 'Noto Serif JP' : 'Noto Sans JP');
|
|
67
|
+
}
|
|
68
|
+
export function runFont(list, ligatures = true, text = '') {
|
|
69
|
+
const cjk = cjkFont(list, text);
|
|
70
|
+
if (cjk)
|
|
71
|
+
return cjk;
|
|
72
|
+
const first = list.split(',')[0].trim().replace(/^['"]|['"]$/g, '');
|
|
73
|
+
if (SYSTEM.test(first))
|
|
74
|
+
return 'Inter';
|
|
75
|
+
if (!ligatures && LIGATURE.test(first))
|
|
76
|
+
return 'Roboto Mono';
|
|
77
|
+
return slidesFont(list, /mono/i.test(list) ? 'Roboto Mono' : 'Arial');
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* A line's height in Slides at 100% spacing, as a multiple of the font size: the same for every font (measured with
|
|
81
|
+
* scripts/slides-fonts.ts: Inter, Roboto, Roboto Mono, Arial, JetBrains Mono, Playfair Display, Manrope, Nunito,
|
|
82
|
+
* Source Serif 4, Source Sans 3, IBM Plex Mono, Fira Code all 1.214–1.232).
|
|
83
|
+
*/
|
|
84
|
+
const SINGLE_LINE = 1.22;
|
|
85
|
+
/** Slides puts lines a whole number of these apart (in pt), rounding down: 3/4 pt, a CSS px at 96 dpi. */
|
|
86
|
+
const LINE_STEP = 0.75;
|
|
87
|
+
/**
|
|
88
|
+
* Line spacing (percent of single, to a tenth) that gives the page's line height. Slides rounds each line's height
|
|
89
|
+
* down to a step, so aim at the middle of the step nearest the page's: lines then drift by under half a step, not a
|
|
90
|
+
* whole one. With `atMost` (a table cell's line, which must not grow its row), the step at or under it.
|
|
91
|
+
*/
|
|
92
|
+
/** A paragraph's biggest text: Slides sets every line's height by it (spacing × its size), not by the first run's. */
|
|
93
|
+
const biggest = (runs) => runs.reduce((a, r) => (r.text.trim() && r.size > a.size ? r : a), runs[0]);
|
|
94
|
+
function lineSpacing(lineHeight, run, atMost = false) {
|
|
95
|
+
const natural = SINGLE_LINE;
|
|
96
|
+
const size = pt(run.size).magnitude;
|
|
97
|
+
const steps = Math.max(1, (atMost ? Math.floor : Math.round)((lineHeight * PT_PX) / LINE_STEP));
|
|
98
|
+
// A line tighter than the font: Slides takes only SQUEEZE of what it lacks, so ask for the rest beyond it.
|
|
99
|
+
const glyphs = size * natural;
|
|
100
|
+
const aim = (steps + 0.5) * LINE_STEP;
|
|
101
|
+
const asked = aim < glyphs ? glyphs - (glyphs - aim) / SQUEEZE : aim;
|
|
102
|
+
return Math.round(Math.min(300, Math.max(50, (asked / glyphs) * 100)) * 10) / 10;
|
|
103
|
+
}
|
|
104
|
+
const sameLook = (a, b) => a.size === b.size && a.weight === b.weight && a.italic === b.italic && a.family === b.family && a.underline === b.underline &&
|
|
105
|
+
a.strike === b.strike && a.link === b.link && a.ligatures === b.ligatures && JSON.stringify(a.highlight) === JSON.stringify(b.highlight) &&
|
|
106
|
+
JSON.stringify(a.color) === JSON.stringify(b.color);
|
|
107
|
+
function mergeRuns(runs) {
|
|
108
|
+
const out = [];
|
|
109
|
+
for (const r of runs) {
|
|
110
|
+
const last = out[out.length - 1];
|
|
111
|
+
if (last && sameLook(last, r))
|
|
112
|
+
last.text += r.text;
|
|
113
|
+
else
|
|
114
|
+
out.push({ ...r });
|
|
115
|
+
}
|
|
116
|
+
return out;
|
|
117
|
+
}
|
|
118
|
+
/** updateTextStyle for each run, from `at` in the text of `where` (a shape, or a table cell). */
|
|
119
|
+
function runStyles(where, runs, at = 0, scale = 1, whole) {
|
|
120
|
+
const out = [];
|
|
121
|
+
const start = at;
|
|
122
|
+
for (const r of runs) {
|
|
123
|
+
const end = at + r.text.length;
|
|
124
|
+
const style = {
|
|
125
|
+
weightedFontFamily: { fontFamily: runFont(r.family, r.ligatures, r.text), weight: Math.min(900, Math.max(100, Math.round(r.weight / 100) * 100)) },
|
|
126
|
+
fontSize: pt(r.size * scale),
|
|
127
|
+
foregroundColor: { opaqueColor: { rgbColor: r.color } },
|
|
128
|
+
italic: r.italic,
|
|
129
|
+
underline: r.underline && !r.link,
|
|
130
|
+
strikethrough: r.strike,
|
|
131
|
+
};
|
|
132
|
+
if (r.link)
|
|
133
|
+
style.link = { url: r.link };
|
|
134
|
+
if (r.highlight)
|
|
135
|
+
style.backgroundColor = { opaqueColor: { rgbColor: r.highlight } };
|
|
136
|
+
const request = (from, to) => ({ updateTextStyle: { ...where, textRange: { type: 'FIXED_RANGE', startIndex: from, endIndex: to }, style, fields: Object.keys(style).join(',') } });
|
|
137
|
+
// A run that is the whole paragraph (`whole`: its end) goes in two parts, so it doesn't restyle the bullet.
|
|
138
|
+
if (whole !== undefined && at === start && end === whole && end - at > 1)
|
|
139
|
+
out.push(request(at, end - 1), request(end - 1, end));
|
|
140
|
+
else if (end > at)
|
|
141
|
+
out.push(request(at, end));
|
|
142
|
+
at = end;
|
|
143
|
+
}
|
|
144
|
+
return out;
|
|
145
|
+
}
|
|
146
|
+
/** The style that gives a bullet the theme's marker: its color, and a size that draws the glyph as big. */
|
|
147
|
+
function markerStyle(objectId, start, end, marker, first, level = 0) {
|
|
148
|
+
// A bullet glyph (◆, ●) is about 0.72 of its font size tall; never smaller than 0.6 of the item's text, as a theme's
|
|
149
|
+
// fine marker would be a speck as a glyph. A nested one (○, about half its font size) is set at the text's size.
|
|
150
|
+
// Never bigger than the text (as set, scaled): Slides makes a line as tall as its biggest glyph, the bullet's too.
|
|
151
|
+
const size = Math.min(first.size, marker.size ?? Math.max((marker.height ?? 0) / 0.72, first.size * (level > 0 ? 1 : 0.6)));
|
|
152
|
+
const style = {
|
|
153
|
+
foregroundColor: { opaqueColor: { rgbColor: marker.color } },
|
|
154
|
+
fontSize: pt(size),
|
|
155
|
+
weightedFontFamily: { fontFamily: runFont(marker.family ?? first.family, false), weight: Math.min(900, Math.max(100, Math.round((marker.weight ?? 400) / 100) * 100)) },
|
|
156
|
+
};
|
|
157
|
+
return { updateTextStyle: { objectId, textRange: { type: 'FIXED_RANGE', startIndex: start, endIndex: end }, style, fields: Object.keys(style).join(',') } };
|
|
158
|
+
}
|
|
159
|
+
const bulletPreset = (list) => list.ordered ? 'NUMBERED_DIGIT_ALPHA_ROMAN' : list.diamond ? 'BULLET_DIAMOND_CIRCLE_SQUARE' : 'BULLET_DISC_CIRCLE_SQUARE';
|
|
160
|
+
/**
|
|
161
|
+
* One text box for a frame: its paragraphs, each list a Slides list (nested by level), each paragraph spaced from the
|
|
162
|
+
* last and indented as on the page, every run in its look.
|
|
163
|
+
*/
|
|
164
|
+
export function frameRequests(objectId, pageObjectId, frame, placeholder) {
|
|
165
|
+
const paragraphs = frame.paragraphs
|
|
166
|
+
.map((p) => ({ ...p, runs: mergeRuns(p.runs) }))
|
|
167
|
+
.filter((p) => p.runs.some((r) => r.text.trim()));
|
|
168
|
+
if (!paragraphs.length)
|
|
169
|
+
return [];
|
|
170
|
+
// The box starts at the leftmost of its text and its list markers, which hang left of their items' text.
|
|
171
|
+
const left = Math.min(frame.x, ...paragraphs.map((p) => p.list?.marker?.x ?? p.x));
|
|
172
|
+
frame = { ...frame, x: left, w: frame.w + (frame.x - left) };
|
|
173
|
+
const first = paragraphs[0];
|
|
174
|
+
const last = paragraphs[paragraphs.length - 1];
|
|
175
|
+
// Lines that don't wrap (a title block, a label) get more room: nothing below them moves.
|
|
176
|
+
const oneLine = paragraphs.every((p) => p.lines === 1);
|
|
177
|
+
const slack = frame.w * (oneLine ? SLACK_LINE : SLACK);
|
|
178
|
+
// Grown on the side the text runs toward, within the page.
|
|
179
|
+
const room = { left: Math.max(0, frame.x - INSET_X), right: Math.max(0, PAGE_W - frame.x - frame.w - INSET_X) };
|
|
180
|
+
const grow = {
|
|
181
|
+
START: [0, Math.min(slack, room.right)],
|
|
182
|
+
END: [Math.min(slack, room.left), 0],
|
|
183
|
+
CENTER: [Math.min(slack / 2, room.left), Math.min(slack / 2, room.right)],
|
|
184
|
+
JUSTIFIED: [0, 0],
|
|
185
|
+
}[first.align];
|
|
186
|
+
const x = frame.x - INSET_X - grow[0];
|
|
187
|
+
const w = frame.w + 2 * INSET_X + grow[0] + grow[1];
|
|
188
|
+
// Where Slides draws the first line's glyphs (the font it draws in, at the size it's set), measured: GLYPH_GAP below
|
|
189
|
+
// the box's inset, whatever the line spacing above 100% (the extra goes below the line); spacing under 100% pulls
|
|
190
|
+
// them up by what the line lacks, less PULL_SHORT. The box goes where that puts them on the page's glyphs.
|
|
191
|
+
const run0 = first.runs[0];
|
|
192
|
+
const glyphs = SINGLE_LINE * biggest(first.runs).size * (first.scale ?? 1);
|
|
193
|
+
const pulled = Math.max(0, glyphs - first.lineHeight - PULL_SHORT);
|
|
194
|
+
const y = first.glyphTop + (first.lineContent - glyphs) / 2 - INSET_Y - GLYPH_GAP + pulled;
|
|
195
|
+
const bottom = last.top + last.lines * last.lineHeight;
|
|
196
|
+
const h = bottom - y + INSET_Y;
|
|
197
|
+
const out = placeholder === undefined
|
|
198
|
+
? [
|
|
199
|
+
{
|
|
200
|
+
createShape: {
|
|
201
|
+
objectId,
|
|
202
|
+
shapeType: 'TEXT_BOX',
|
|
203
|
+
elementProperties: {
|
|
204
|
+
pageObjectId,
|
|
205
|
+
size: { width: emu(w), height: emu(h) },
|
|
206
|
+
transform: { scaleX: 1, scaleY: 1, translateX: Math.round(x * EMU_PX), translateY: Math.round(y * EMU_PX), unit: 'EMU' },
|
|
207
|
+
},
|
|
208
|
+
},
|
|
209
|
+
},
|
|
210
|
+
]
|
|
211
|
+
: [
|
|
212
|
+
// A placeholder (the layout's title) keeps its size and is scaled to the box; its scale is known once its size
|
|
213
|
+
// is (`resolveLayouts`), so the transform says the box it must cover.
|
|
214
|
+
{ updatePageElementTransform: { objectId, applyMode: 'ABSOLUTE', transform: { deckBox: { x: Math.round(x * EMU_PX), y: Math.round(y * EMU_PX), w: Math.round(w * EMU_PX), h: Math.round(h * EMU_PX) }, deckLayout: placeholder } } },
|
|
215
|
+
{ updateShapeProperties: { objectId, shapeProperties: { contentAlignment: 'TOP', autofit: { autofitType: 'NONE' } }, fields: 'contentAlignment,autofit.autofitType' } },
|
|
216
|
+
];
|
|
217
|
+
// The text with list items indented by tabs (their level); making the bullets takes the tabs out.
|
|
218
|
+
const texts = paragraphs.map((p) => p.runs.map((r) => r.text).join(''));
|
|
219
|
+
const withTabs = paragraphs.map((p, i) => (p.list ? '\t'.repeat(p.list.level) : '') + texts[i]).join('\n');
|
|
220
|
+
out.push({ insertText: { objectId, insertionIndex: 0, text: withTabs } });
|
|
221
|
+
// The whole text in the first run's font and size first, so no line keeps the box's default (18 pt) size, which
|
|
222
|
+
// Slides would make the line's height.
|
|
223
|
+
const base = { weightedFontFamily: { fontFamily: runFont(run0.family, run0.ligatures), weight: 400 }, fontSize: pt(run0.size * (first.scale ?? 1)) };
|
|
224
|
+
out.push({ updateTextStyle: { objectId, textRange: { type: 'ALL' }, style: base, fields: 'weightedFontFamily,fontSize' } });
|
|
225
|
+
// Each list (consecutive items under one top-level kind) gets its bullets, last list first.
|
|
226
|
+
const lists = [];
|
|
227
|
+
let at = 0;
|
|
228
|
+
paragraphs.forEach((p, i) => {
|
|
229
|
+
const len = (p.list ? p.list.level : 0) + texts[i].length;
|
|
230
|
+
const open = lists[lists.length - 1];
|
|
231
|
+
const prev = paragraphs[i - 1];
|
|
232
|
+
if (p.list && open && prev?.list && open.end === at - 1 && (p.list.level > 0 || bulletPreset(p.list) === open.preset))
|
|
233
|
+
open.end = at + len;
|
|
234
|
+
else if (p.list)
|
|
235
|
+
lists.push({ start: at, end: at + len, preset: bulletPreset(p.list) });
|
|
236
|
+
at += len + 1;
|
|
237
|
+
});
|
|
238
|
+
for (const l of [...lists].reverse()) {
|
|
239
|
+
out.push({ createParagraphBullets: { objectId, textRange: { type: 'FIXED_RANGE', startIndex: l.start, endIndex: l.end }, bulletPreset: l.preset } });
|
|
240
|
+
}
|
|
241
|
+
// Then, in the text without tabs, paragraph by paragraph: the bullet's look, the runs, the spacing and indents.
|
|
242
|
+
// Slides draws no space above or below a list item, but a line's extra height goes below its glyphs: a one-line
|
|
243
|
+
// item takes the gap after it into its line, and the next paragraph has none above.
|
|
244
|
+
const gapAfter = (i) => {
|
|
245
|
+
const p = paragraphs[i], next = paragraphs[i + 1];
|
|
246
|
+
return next ? Math.max(0, next.top - (p.top + p.lines * p.lineHeight)) : 0;
|
|
247
|
+
};
|
|
248
|
+
const absorbs = (i) => !!paragraphs[i]?.list && paragraphs[i].lines === 1 && gapAfter(i) > 0;
|
|
249
|
+
at = 0;
|
|
250
|
+
paragraphs.forEach((p, i) => {
|
|
251
|
+
const end = at + texts[i].length;
|
|
252
|
+
// Slides gives a bullet the style of a request that covers its item's whole text, and leaves it alone otherwise:
|
|
253
|
+
// the theme's marker first, then the runs, none of which covers the whole item.
|
|
254
|
+
const marker = p.list?.marker;
|
|
255
|
+
if (marker)
|
|
256
|
+
out.push(markerStyle(objectId, at, end, marker, { ...p.runs[0], size: p.runs[0].size * (p.scale ?? 1) }, p.list.level));
|
|
257
|
+
out.push(...runStyles({ objectId }, p.runs, at, p.scale, marker ? end : undefined));
|
|
258
|
+
const above = i > 0 && !absorbs(i - 1) ? gapAfter(i - 1) : 0;
|
|
259
|
+
const line = absorbs(i) ? p.lineHeight + gapAfter(i) : p.lineHeight;
|
|
260
|
+
const indent = Math.max(0, p.x - frame.x);
|
|
261
|
+
// A list item's marker hangs left of its text, where the theme draws it. Slides draws a bullet so it *ends* at
|
|
262
|
+
// the first-line indent, so that's where the theme's marker ends, at least half the text size before it.
|
|
263
|
+
const gap = p.runs[0].size / 2;
|
|
264
|
+
const markerEnd = marker?.x !== undefined ? marker.x + (marker.width ?? 0) - frame.x : indent - gap;
|
|
265
|
+
const hang = Math.min(markerEnd, indent - gap);
|
|
266
|
+
const style = {
|
|
267
|
+
alignment: p.align,
|
|
268
|
+
lineSpacing: lineSpacing(line, { ...biggest(p.runs), size: biggest(p.runs).size * (p.scale ?? 1) }),
|
|
269
|
+
spaceAbove: pt(above),
|
|
270
|
+
spaceBelow: pt(0),
|
|
271
|
+
indentStart: pt(indent),
|
|
272
|
+
indentFirstLine: pt(p.list ? Math.min(indent, Math.max(0, hang)) : indent),
|
|
273
|
+
};
|
|
274
|
+
out.push({ updateParagraphStyle: { objectId, textRange: { type: 'FIXED_RANGE', startIndex: at, endIndex: at + texts[i].length }, style, fields: Object.keys(style).join(',') } });
|
|
275
|
+
at += texts[i].length + 1;
|
|
276
|
+
});
|
|
277
|
+
return out;
|
|
278
|
+
}
|
|
279
|
+
/** A native table where the page has one: column widths, row heights, cell fills, the rule between rows, and text. */
|
|
280
|
+
export function tableRequests(objectId, pageObjectId, table) {
|
|
281
|
+
const columns = table.widths.length;
|
|
282
|
+
const out = [
|
|
283
|
+
{
|
|
284
|
+
createTable: {
|
|
285
|
+
objectId,
|
|
286
|
+
rows: table.rows.length,
|
|
287
|
+
columns,
|
|
288
|
+
elementProperties: {
|
|
289
|
+
pageObjectId,
|
|
290
|
+
size: { width: emu(table.w), height: emu(table.h) },
|
|
291
|
+
transform: { scaleX: 1, scaleY: 1, translateX: Math.round(table.x * EMU_PX), translateY: Math.round(table.y * EMU_PX), unit: 'EMU' },
|
|
292
|
+
},
|
|
293
|
+
},
|
|
294
|
+
},
|
|
295
|
+
];
|
|
296
|
+
table.widths.forEach((width, c) => {
|
|
297
|
+
out.push({ updateTableColumnProperties: { objectId, columnIndices: [c], tableColumnProperties: { columnWidth: emu(width) }, fields: 'columnWidth' } });
|
|
298
|
+
});
|
|
299
|
+
table.rows.forEach((row, r) => {
|
|
300
|
+
out.push({ updateTableRowProperties: { objectId, rowIndices: [r], tableRowProperties: { minRowHeight: emu(row.height) }, fields: 'minRowHeight' } });
|
|
301
|
+
});
|
|
302
|
+
// No borders but the rule between rows, as the page draws them.
|
|
303
|
+
const none = { tableBorderFill: { solidFill: { color: { rgbColor: { red: 0, green: 0, blue: 0 } }, alpha: 0 } }, weight: { magnitude: 1, unit: 'PT' } };
|
|
304
|
+
out.push({ updateTableBorderProperties: { objectId, borderPosition: 'ALL', tableBorderProperties: none, fields: 'tableBorderFill,weight' } });
|
|
305
|
+
if (table.rule) {
|
|
306
|
+
const rule = { tableBorderFill: { solidFill: { color: { rgbColor: table.rule.color }, alpha: 1 } }, weight: pt(Math.max(1, table.rule.width)) };
|
|
307
|
+
out.push({ updateTableBorderProperties: { objectId, borderPosition: 'INNER_HORIZONTAL', tableBorderProperties: rule, fields: 'tableBorderFill,weight' } });
|
|
308
|
+
}
|
|
309
|
+
table.rows.forEach((row, rowIndex) => row.cells.forEach((cell, columnIndex) => {
|
|
310
|
+
if (columnIndex >= columns)
|
|
311
|
+
return;
|
|
312
|
+
const location = { rowIndex, columnIndex };
|
|
313
|
+
const fill = cell.fill ? { solidFill: { color: { rgbColor: cell.fill } } } : { propertyState: 'NOT_RENDERED' };
|
|
314
|
+
out.push({
|
|
315
|
+
updateTableCellProperties: {
|
|
316
|
+
objectId,
|
|
317
|
+
tableRange: { location, rowSpan: 1, columnSpan: 1 },
|
|
318
|
+
tableCellProperties: { tableCellBackgroundFill: fill, contentAlignment: cell.valign },
|
|
319
|
+
fields: 'tableCellBackgroundFill,contentAlignment',
|
|
320
|
+
},
|
|
321
|
+
});
|
|
322
|
+
const runs = mergeRuns(cell.runs);
|
|
323
|
+
const text = runs.map((r) => r.text).join('');
|
|
324
|
+
if (!text.trim())
|
|
325
|
+
return;
|
|
326
|
+
const where = { objectId, cellLocation: location };
|
|
327
|
+
out.push({ insertText: { ...where, insertionIndex: 0, text } });
|
|
328
|
+
out.push(...runStyles(where, runs, 0, cell.scale));
|
|
329
|
+
// Slides pads a cell (more than a page's table, and the API can't change it), so the text's lines are set tight
|
|
330
|
+
// enough that the row stays the page's height.
|
|
331
|
+
const lines = text.split('\n').length;
|
|
332
|
+
const fit = (row.height - CELL_PAD_Y) / lines;
|
|
333
|
+
const style = { alignment: cell.align, spaceAbove: pt(0), spaceBelow: pt(0), lineSpacing: lineSpacing(Math.min(fit, cell.lineHeight ?? fit), { ...biggest(runs), size: biggest(runs).size * (cell.scale ?? 1) }, true) };
|
|
334
|
+
out.push({ updateParagraphStyle: { ...where, textRange: { type: 'ALL' }, style, fields: Object.keys(style).join(',') } });
|
|
335
|
+
}));
|
|
336
|
+
return out;
|
|
337
|
+
}
|
|
338
|
+
/** The layouts `deck slides` makes of a presentation's own (the API can't add layouts), by name: the slide kinds' first. */
|
|
339
|
+
const PREFERRED = { title: 'TITLE', section: 'SECTION_HEADER', content: 'TITLE_AND_BODY', columns: 'TITLE_AND_TWO_COLUMNS' };
|
|
340
|
+
const POOL = ['TITLE_ONLY', 'TITLE_AND_BODY', 'TITLE_AND_TWO_COLUMNS', 'ONE_COLUMN_TEXT', 'MAIN_POINT', 'BIG_NUMBER', 'SECTION_TITLE_AND_DESCRIPTION', 'TITLE', 'SECTION_HEADER'];
|
|
341
|
+
const layoutRef = (k) => `deck-layout:${k}`;
|
|
342
|
+
const titleRef = (k) => `deck-title:${k}`;
|
|
343
|
+
/** Layout k's body placeholder n, by object ID; a slide's mapping to one that the layout can't have is dropped. */
|
|
344
|
+
const bodyRef = (k, n) => `deck-body:${k}:${n}`;
|
|
345
|
+
/**
|
|
346
|
+
* Plan a deck from its capture. Slides whose theme picture is the same (the background, ornaments, footer rules)
|
|
347
|
+
* share a layout: the picture is its background, the first slide's title places and styles its title placeholder,
|
|
348
|
+
* and footer text alike on all of them goes on it. Each slide then has its title in the placeholder and, on top of
|
|
349
|
+
* the layout, the pictures its own content draws (panels, images, diagrams), its tables, and its text boxes.
|
|
350
|
+
* With `layouts: false` (adding to someone's presentation, whose layouts stay as they are) every slide is blank with
|
|
351
|
+
* the picture as its own background. Pictures are in `images`, referred to by `imageRef(i)` until uploaded.
|
|
352
|
+
*/
|
|
353
|
+
export function planRendered(built, captured, prefix, options = {}) {
|
|
354
|
+
const requests = [];
|
|
355
|
+
const notes = [];
|
|
356
|
+
const images = [];
|
|
357
|
+
const indexOf = new Map();
|
|
358
|
+
const image = (b) => {
|
|
359
|
+
const key = createHash('sha1').update(b).digest('hex');
|
|
360
|
+
if (!indexOf.has(key))
|
|
361
|
+
indexOf.set(key, images.push(b) - 1);
|
|
362
|
+
return indexOf.get(key);
|
|
363
|
+
};
|
|
364
|
+
const slides = built.deck.slides.map((slide, i) => ({ slide, page: captured.slides[i] })).filter((s) => s.page);
|
|
365
|
+
// Slides by their theme picture; a picture of two slides or more, or of a title, section, or columns slide, is a layout.
|
|
366
|
+
const layouts = [];
|
|
367
|
+
const layoutOf = new Map();
|
|
368
|
+
if (options.layouts !== false) {
|
|
369
|
+
const groups = new Map();
|
|
370
|
+
slides.forEach((s, i) => {
|
|
371
|
+
const k = image(s.page.background);
|
|
372
|
+
groups.set(k, [...(groups.get(k) ?? []), i]);
|
|
373
|
+
});
|
|
374
|
+
const used = new Set();
|
|
375
|
+
// A picture's slides split by their columns (content, two, three…): each kind of column layout is a layout of its
|
|
376
|
+
// own, so slides people add to it in Slides get those columns.
|
|
377
|
+
const shape = (m) => {
|
|
378
|
+
const { slide, page } = slides[m];
|
|
379
|
+
return slide.layout === 'content' || slide.layout === 'columns' ? `cols:${(page.bodies ?? []).length}` : slide.layout;
|
|
380
|
+
};
|
|
381
|
+
const parts = [];
|
|
382
|
+
for (const [background, members] of groups) {
|
|
383
|
+
if (members.length < 2 && !['title', 'section', 'columns'].includes(slides[members[0]].slide.layout))
|
|
384
|
+
continue;
|
|
385
|
+
const split = new Map();
|
|
386
|
+
for (const m of members)
|
|
387
|
+
split.set(shape(m), [...(split.get(shape(m)) ?? []), m]);
|
|
388
|
+
for (const part of split.values())
|
|
389
|
+
parts.push([background, part]);
|
|
390
|
+
}
|
|
391
|
+
for (const [background, members] of parts) {
|
|
392
|
+
const kind = slides[members[0]].slide.layout;
|
|
393
|
+
const preferred = PREFERRED[kind];
|
|
394
|
+
const name = preferred && !used.has(preferred) ? preferred : POOL.find((n) => !used.has(n));
|
|
395
|
+
if (!name)
|
|
396
|
+
break;
|
|
397
|
+
used.add(name);
|
|
398
|
+
const k = layouts.length;
|
|
399
|
+
// Footer text every slide of it has, alike and in the same place.
|
|
400
|
+
const footers = (m) => slides[m].page.frames.filter((f) => f.role === 'footer').map((f) => JSON.stringify(f));
|
|
401
|
+
const shared = footers(members[0]).filter((f) => members.every((m) => footers(m).includes(f)));
|
|
402
|
+
const title = members.map((m) => slides[m].page.frames.find((f) => f.role === 'title')).find(Boolean);
|
|
403
|
+
// Content and columns slides: body placeholders where the first one's columns are, for slides people add.
|
|
404
|
+
const bodies = kind === 'content' || kind === 'columns' ? slides[members[0]].page.bodies ?? [] : [];
|
|
405
|
+
layouts.push({
|
|
406
|
+
id: `${prefix}_l${k + 1}`, name, background, title, frames: shared.map((f) => JSON.parse(f)), slides: members.length,
|
|
407
|
+
...(bodies.length ? { bodies } : {}),
|
|
408
|
+
});
|
|
409
|
+
for (const m of members)
|
|
410
|
+
layoutOf.set(m, k);
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
slides.forEach(({ slide, page }, i) => {
|
|
414
|
+
const id = `${prefix}_s${slide.number}`;
|
|
415
|
+
const k = layoutOf.get(i);
|
|
416
|
+
const layout = k === undefined ? undefined : layouts[k];
|
|
417
|
+
if (layout) {
|
|
418
|
+
requests.push({
|
|
419
|
+
createSlide: {
|
|
420
|
+
objectId: id,
|
|
421
|
+
slideLayoutReference: { layoutId: layoutRef(k) },
|
|
422
|
+
placeholderIdMappings: [
|
|
423
|
+
{ layoutPlaceholder: { type: titleRef(k), index: 0 }, objectId: `${id}_title` },
|
|
424
|
+
...(layout.bodies ?? []).map((_, n) => ({ layoutPlaceholderObjectId: bodyRef(k, n), objectId: `${id}_body${n + 1}` })),
|
|
425
|
+
],
|
|
426
|
+
},
|
|
427
|
+
});
|
|
428
|
+
// The slide's text is in its own boxes: its copies of the layout's (empty) body placeholders go.
|
|
429
|
+
(layout.bodies ?? []).forEach((_, n) => requests.push({ deleteObject: { objectId: `${id}_body${n + 1}` } }));
|
|
430
|
+
}
|
|
431
|
+
else {
|
|
432
|
+
requests.push({ createSlide: { objectId: id, slideLayoutReference: { predefinedLayout: 'BLANK' } } });
|
|
433
|
+
requests.push({
|
|
434
|
+
updatePageProperties: {
|
|
435
|
+
objectId: id,
|
|
436
|
+
pageProperties: { pageBackgroundFill: { stretchedPictureFill: { contentUrl: imageRef(image(page.background)) } } },
|
|
437
|
+
fields: 'pageBackgroundFill',
|
|
438
|
+
},
|
|
439
|
+
});
|
|
440
|
+
}
|
|
441
|
+
// The content's own pictures under its tables and text.
|
|
442
|
+
page.visuals.forEach((v, n) => {
|
|
443
|
+
requests.push({
|
|
444
|
+
createImage: {
|
|
445
|
+
objectId: `${id}_v${n + 1}`,
|
|
446
|
+
url: imageRef(image(v.image)),
|
|
447
|
+
elementProperties: {
|
|
448
|
+
pageObjectId: id,
|
|
449
|
+
size: { width: emu(v.w), height: emu(v.h) },
|
|
450
|
+
transform: { scaleX: 1, scaleY: 1, translateX: Math.round(v.x * EMU_PX), translateY: Math.round(v.y * EMU_PX), unit: 'EMU' },
|
|
451
|
+
},
|
|
452
|
+
},
|
|
453
|
+
});
|
|
454
|
+
});
|
|
455
|
+
page.tables.forEach((table, n) => requests.push(...tableRequests(`${id}_table${n + 1}`, id, table)));
|
|
456
|
+
const onLayout = new Set(layout?.frames.map((f) => JSON.stringify(f)));
|
|
457
|
+
let titled = false;
|
|
458
|
+
page.frames.forEach((frame, n) => {
|
|
459
|
+
if (layout && frame.role === 'title' && !titled) {
|
|
460
|
+
const title = frameRequests(`${id}_title`, id, frame, k);
|
|
461
|
+
titled = title.length > 0;
|
|
462
|
+
requests.push(...title);
|
|
463
|
+
}
|
|
464
|
+
else if (!(frame.role === 'footer' && onLayout.has(JSON.stringify(frame)))) {
|
|
465
|
+
requests.push(...frameRequests(`${id}_t${n + 1}`, id, frame));
|
|
466
|
+
}
|
|
467
|
+
});
|
|
468
|
+
// A slide without a title doesn't keep the layout's empty title placeholder. One with a title has it in front: it
|
|
469
|
+
// was made with the slide, under the pictures (a title on a panel).
|
|
470
|
+
if (layout && !titled)
|
|
471
|
+
requests.push({ deleteObject: { objectId: `${id}_title` } });
|
|
472
|
+
else if (layout && page.visuals.length)
|
|
473
|
+
requests.push({ updatePageElementsZOrder: { pageElementObjectIds: [`${id}_title`], operation: 'BRING_TO_FRONT' } });
|
|
474
|
+
const rich = slideNotes(built, slide);
|
|
475
|
+
if (rich)
|
|
476
|
+
notes.push({ slideId: id, rich });
|
|
477
|
+
});
|
|
478
|
+
return { title: built.title, requests, notes, warnings: [], style: captured.style, images, layouts: layouts.length ? layouts : undefined };
|
|
479
|
+
}
|
|
480
|
+
const isTitle = (type) => type === 'TITLE' || type === 'CENTERED_TITLE';
|
|
481
|
+
/**
|
|
482
|
+
* Make the plan's layouts of a presentation's own, by name (else any free one with a title): each loses what it had
|
|
483
|
+
* but its title placeholder (and slide number), gets its picture as background and its title placed and styled; the
|
|
484
|
+
* master's background is the most used layout's picture. Returns those requests, and the plan's requests with the
|
|
485
|
+
* layouts' IDs, title types, and placeholder transforms filled in.
|
|
486
|
+
*/
|
|
487
|
+
export function resolveLayouts(plan, pages, masters) {
|
|
488
|
+
const layouts = plan.layouts ?? [];
|
|
489
|
+
if (!layouts.length)
|
|
490
|
+
return { setup: [], requests: plan.requests };
|
|
491
|
+
const taken = new Set();
|
|
492
|
+
const titleOf = (p) => p.pageElements?.find((e) => isTitle(e.shape?.placeholder?.type));
|
|
493
|
+
const bodyOf = (p) => p.pageElements?.find((e) => e.shape?.placeholder?.type === 'BODY');
|
|
494
|
+
const chosen = layouts.map((l) => {
|
|
495
|
+
// By name, else any free one with a title; one with a body placeholder first where the layout wants bodies.
|
|
496
|
+
const free = pages.filter((p) => !taken.has(p.objectId) && titleOf(p));
|
|
497
|
+
const named = (p) => p.layoutProperties?.name === l.name;
|
|
498
|
+
const page = (l.bodies ? free.find((p) => named(p) && bodyOf(p)) ?? free.find((p) => bodyOf(p)) : undefined) ?? free.find(named) ?? free[0];
|
|
499
|
+
if (!page)
|
|
500
|
+
throw new DeckError(`the presentation has no layout left with a title for ${layouts.length} kinds of slide; export to a new presentation`);
|
|
501
|
+
taken.add(page.objectId);
|
|
502
|
+
const title = titleOf(page);
|
|
503
|
+
const body = l.bodies ? bodyOf(page) : undefined;
|
|
504
|
+
// Its body placeholders by object ID: the one it has, and copies of it (an older export may have left it none).
|
|
505
|
+
const bodies = body ? l.bodies.map((_, n) => (n ? `${l.id}_b${n + 1}` : body.objectId)) : [];
|
|
506
|
+
return { page, title, body, bodies, base: { w: title.size?.width?.magnitude ?? 3000000, h: title.size?.height?.magnitude ?? 3000000 } };
|
|
507
|
+
});
|
|
508
|
+
const fill = (value) => {
|
|
509
|
+
if (typeof value === 'string') {
|
|
510
|
+
const b = /^deck-body:(\d+):(\d+)$/.exec(value);
|
|
511
|
+
if (b)
|
|
512
|
+
return chosen[Number(b[1])].bodies[Number(b[2])] ?? null;
|
|
513
|
+
const m = /^deck-(layout|title):(\d+)$/.exec(value);
|
|
514
|
+
if (!m)
|
|
515
|
+
return value;
|
|
516
|
+
const c = chosen[Number(m[2])];
|
|
517
|
+
return m[1] === 'layout' ? c.page.objectId : c.title.shape.placeholder.type;
|
|
518
|
+
}
|
|
519
|
+
if (Array.isArray(value))
|
|
520
|
+
return value.map(fill);
|
|
521
|
+
if (value && typeof value === 'object') {
|
|
522
|
+
const o = value;
|
|
523
|
+
if (o.deckBox) {
|
|
524
|
+
const box = o.deckBox;
|
|
525
|
+
const { base } = chosen[o.deckLayout];
|
|
526
|
+
return { scaleX: box.w / base.w, scaleY: box.h / base.h, translateX: box.x, translateY: box.y, unit: 'EMU' };
|
|
527
|
+
}
|
|
528
|
+
return Object.fromEntries(Object.entries(o).map(([k, v]) => [k, fill(v)]));
|
|
529
|
+
}
|
|
530
|
+
return value;
|
|
531
|
+
};
|
|
532
|
+
const setup = [];
|
|
533
|
+
layouts.forEach((l, k) => {
|
|
534
|
+
const { page, title, body, bodies } = chosen[k];
|
|
535
|
+
for (const e of page.pageElements ?? []) {
|
|
536
|
+
if (e !== title && e !== body && e.shape?.placeholder?.type !== 'SLIDE_NUMBER')
|
|
537
|
+
setup.push({ deleteObject: { objectId: e.objectId } });
|
|
538
|
+
}
|
|
539
|
+
if (body) {
|
|
540
|
+
// A body placeholder on each of the first slide's columns: the one it has, then copies of it.
|
|
541
|
+
const base = { w: body.size?.width?.magnitude ?? 3000000, h: body.size?.height?.magnitude ?? 3000000 };
|
|
542
|
+
bodies.slice(1).forEach((id) => setup.push({ duplicateObject: { objectId: body.objectId, objectIds: { [body.objectId]: id } } }));
|
|
543
|
+
l.bodies.forEach((b, n) => setup.push({
|
|
544
|
+
updatePageElementTransform: {
|
|
545
|
+
objectId: bodies[n], applyMode: 'ABSOLUTE',
|
|
546
|
+
transform: { scaleX: (b.w * EMU_PX) / base.w, scaleY: (b.h * EMU_PX) / base.h, translateX: Math.round(b.x * EMU_PX), translateY: Math.round(b.y * EMU_PX), unit: 'EMU' },
|
|
547
|
+
},
|
|
548
|
+
}));
|
|
549
|
+
}
|
|
550
|
+
setup.push({
|
|
551
|
+
updatePageProperties: { objectId: page.objectId, pageProperties: { pageBackgroundFill: { stretchedPictureFill: { contentUrl: imageRef(l.background) } } }, fields: 'pageBackgroundFill' },
|
|
552
|
+
});
|
|
553
|
+
if (l.title) {
|
|
554
|
+
// Placed as the first slide's title; its text (a layout's can't be edited) styled as that title's first run.
|
|
555
|
+
const placed = frameRequests(title.objectId, page.objectId, l.title, k);
|
|
556
|
+
setup.push(...placed.filter((r) => 'updatePageElementTransform' in r || 'updateShapeProperties' in r));
|
|
557
|
+
const text = (title.shape?.text?.textElements ?? []).map((t) => t.textRun?.content ?? '').join('');
|
|
558
|
+
if (text.length > 1) {
|
|
559
|
+
const all = (r, kind) => {
|
|
560
|
+
const v = r[kind];
|
|
561
|
+
return v && v.textRange.startIndex === 0 ? { [kind]: { ...v, textRange: { type: 'ALL' } } } : undefined;
|
|
562
|
+
};
|
|
563
|
+
for (const kind of ['updateTextStyle', 'updateParagraphStyle']) {
|
|
564
|
+
const first = placed.map((r) => all(r, kind)).find(Boolean);
|
|
565
|
+
if (first)
|
|
566
|
+
setup.push(first);
|
|
567
|
+
}
|
|
568
|
+
}
|
|
569
|
+
}
|
|
570
|
+
l.frames.forEach((f, n) => setup.push(...frameRequests(`${l.id}_t${n + 1}`, page.objectId, f)));
|
|
571
|
+
});
|
|
572
|
+
const main = layouts.reduce((a, b) => (b.slides > a.slides ? b : a));
|
|
573
|
+
for (const m of masters) {
|
|
574
|
+
setup.push({ updatePageProperties: { objectId: m.objectId, pageProperties: { pageBackgroundFill: { stretchedPictureFill: { contentUrl: imageRef(main.background) } } }, fields: 'pageBackgroundFill' } });
|
|
575
|
+
}
|
|
576
|
+
// A slide's mapping to a body placeholder its layout couldn't have goes, and with it the slide's request to delete it.
|
|
577
|
+
const dropped = new Set();
|
|
578
|
+
const requests = fill(plan.requests).map((r) => {
|
|
579
|
+
const create = r.createSlide;
|
|
580
|
+
if (!create?.placeholderIdMappings)
|
|
581
|
+
return r;
|
|
582
|
+
const kept = create.placeholderIdMappings.filter((m) => m.layoutPlaceholderObjectId !== null || (dropped.add(m.objectId), false));
|
|
583
|
+
return { createSlide: { ...create, placeholderIdMappings: kept } };
|
|
584
|
+
});
|
|
585
|
+
const deleted = (r) => dropped.has(r.deleteObject?.objectId ?? '');
|
|
586
|
+
return { setup: fill(setup), requests: requests.filter((r) => !deleted(r)) };
|
|
587
|
+
}
|
|
588
|
+
//# sourceMappingURL=slideslayout.js.map
|