Bento, the office suite that fits in a file
TypeScript mit-licenseofficeoffice-365office-suiteoffice-toolsoffice365open-sourcepowerpointself-hostedsingle-page-appslidesslideshareslideshowslideshow-galleryslideshow-makerslideshows
Slide layouts: templates with id lineage, placeholders, picker
nyblnetfc208ac2b058
diff --git a/CLAUDE.md b/CLAUDE.mdindex 98409a62..4e2c88b5 100644--- a/CLAUDE.md+++ b/CLAUDE.md@@ -56,6 +56,12 @@ One HTML file = the document + viewer + editor. See `README.md` for the vision.reached by element `link`s, morphing when ids are shared. Authoring: select anelement → "+ New state linked from this element" in the Presenting panel. Stateslive adjacent to their parent and render nested in the sidebar.+- **Layouts**: `doc.layouts` (Slide-shaped templates) + built-ins in model.ts.+ Instantiating KEEPS element ids — slides from the same layout share ids, so+ their chrome morphs across transitions (and re-apply-by-lineage stays+ possible). Text `placeholder` prompts render dimmed in the editor and are+ hidden in present/print (`hidePlaceholders` RenderOpt). Picker on the+ New-slide button; "Save slide as layout" in the slide panel.- **Hover content is in-slide, not states**: `showOnHover` sets + slide`hover:'reveal'` (with a default set) swap content on pointer-over. Editor previewsone set at a time. Rule of thumb: click → state slide; hover → reveal set.diff --git a/docs/architecture.md b/docs/architecture.mdindex 139b3795..f1daa25e 100644--- a/docs/architecture.md+++ b/docs/architecture.md@@ -168,6 +168,7 @@ BentoDoc├─ present?: { slideNumber?, controls?, progress? }├─ assets?: { key → data URI } ← images, fonts; referenced as "asset:key"├─ fonts?: [{ family, assetKey }] ← @font-face injected at boot+├─ layouts?: Slide[] ← templates; instantiation KEEPS element ids (lineage → morph continuity)└─ slides: Slide[] ← linear order; states sit right after their parent├─ id ← stable; morph matches elements ACROSS slides by element id├─ background · transition ← none | fade | slide | zoom | morph@@ -177,7 +178,7 @@ BentoDoc└─ elements: SlideElement[] ← array order = paint order (z)├─ common: id · x y w h · rotation · opacity│ fx? · link? · group? · groupId? · showOnHover?- ├─ text: html (sanitized inline: b/i/u/s/code/br/span…) · fontSize · fontFamily+ ├─ text: html (sanitized inline: b/i/u/s/code/br/span…) · placeholder? · fontSize · fontFamily│ fontWeight · color · align · valign · lineHeight · letterSpacing?├─ shape: shape (rect|ellipse|triangle|arrow|line|path) · fill · fillGradient?│ stroke · strokeWidth · strokeStyle? (solid|dashed|dotted) · radiusdiff --git a/slides/src/editor/editor.ts b/slides/src/editor/editor.tsindex 51ca20c2..445fd2ec 100644--- a/slides/src/editor/editor.ts+++ b/slides/src/editor/editor.ts@@ -3,8 +3,9 @@import type { Store } from '../store'import {- defaultChart, defaultImage, defaultShape, defaultText, emptySlide, uid,- type ShapeKind, type SlideElement,+ builtinLayouts, defaultChart, defaultImage, defaultShape, defaultText, emptySlide,+ instantiateLayout, uid,+ type ShapeKind, type Slide, type SlideElement,} from '../model'import { CHART_PRESETS } from '../charts'import { renderSlide, renderThumbnail } from '../render'@@ -249,13 +250,78 @@ export class Editor {this.sidebar.appendChild(item)})this.sidebar.appendChild(this.insertGap(slides.length))- const add = btn(ICONS.plus, 'New slide', () => this.addSlide())+ const add = btn(ICONS.plus, 'New slide', () => this.openLayoutPicker(add))add.classList.add('ed-add-slide')+ add.title = 'New slide from a layout'this.sidebar.appendChild(add)this.sidebar.scrollTop = scrollthis.highlightSidebar()}+ // --- layouts ---------------------------------------------------------------++ /** Popover over the New-slide button: built-in layouts + this document's. */+ private openLayoutPicker(anchor: HTMLElement) {+ document.querySelector('.ed-layoutpick')?.remove()+ const pick = div('ed-layoutpick')+ const doc = this.store.doc+ const sections: Array<[string, Slide[], boolean]> = [['Built-in', builtinLayouts(), false]]+ if (doc.layouts?.length) sections.push(['This document', doc.layouts, true])+ for (const [label, layouts, custom] of sections) {+ const h = div('ed-layoutpick-h')+ h.textContent = label+ pick.appendChild(h)+ const grid = div('ed-layoutpick-grid')+ for (const ly of layouts) {+ const item = div('ed-layoutpick-item')+ item.appendChild(renderThumbnail(ly, doc, 104))+ const name = div('ed-layoutpick-name')+ name.textContent = ly.name ?? 'Untitled'+ item.appendChild(name)+ item.addEventListener('click', () => {+ pick.remove()+ this.addSlideFromLayout(ly)+ })+ if (custom) {+ const del = document.createElement('button')+ del.className = 'ed-layoutpick-del'+ del.textContent = '✕'+ del.title = 'Delete this layout'+ del.addEventListener('click', (ev) => {+ ev.stopPropagation()+ this.store.commit(() => {+ doc.layouts = doc.layouts!.filter((l) => l.id !== ly.id)+ if (!doc.layouts.length) delete doc.layouts+ })+ pick.remove()+ })+ item.appendChild(del)+ }+ grid.appendChild(item)+ }+ pick.appendChild(grid)+ }+ const r = anchor.getBoundingClientRect()+ pick.style.left = `${Math.max(8, r.left)}px`+ pick.style.bottom = `${window.innerHeight - r.top + 8}px`+ document.body.appendChild(pick)+ const close = (ev: PointerEvent) => {+ if (!pick.contains(ev.target as Node)) {+ pick.remove()+ document.removeEventListener('pointerdown', close, true)+ }+ }+ setTimeout(() => document.addEventListener('pointerdown', close, true))+ }++ private addSlideFromLayout(layout: Slide) {+ const slide = instantiateLayout(layout)+ this.store.commit(() => {+ this.store.doc.slides.splice(this.store.currentIndex + 1, 0, slide)+ }, 'slides')+ this.store.goTo(this.store.currentIndex + 1)+ }+/** Slim hover strip between thumbnails — click inserts a blank slide there. */private insertGap(at: number): HTMLElement {const gap = div('ed-insertgap')@@ -322,14 +388,6 @@ export class Editor {// --- slide ops ------------------------------------------------------------------- private addSlide() {- const bg = this.store.slide.background- this.store.commit(() => {- this.store.doc.slides.splice(this.store.currentIndex + 1, 0, emptySlide({ background: bg }))- }, 'slides')- this.store.goTo(this.store.currentIndex + 1)- }-private duplicateSlide(i: number) {// Duplicated slides keep element ids → set transition to morph and you// get PowerPoint-Morph behaviour for free.@@ -384,7 +442,7 @@ export class Editor {for (const slide of this.store.doc.slides) {if (slide.stateOf) continueconst page = div('bp-page')- const surface = renderSlide(slide, this.store.doc, { svgAsImage: true })+ const surface = renderSlide(slide, this.store.doc, { svgAsImage: true, hidePlaceholders: true })// normalise to the print page size regardless of doc sizeconst s = 1600 / this.store.doc.size.widthsurface.style.transformOrigin = '0 0'diff --git a/slides/src/editor/panels.ts b/slides/src/editor/panels.tsindex b7b38555..55bb910f 100644--- a/slides/src/editor/panels.ts+++ b/slides/src/editor/panels.ts@@ -3,7 +3,7 @@// into a single undo checkpoint.import type { Store } from '../store'-import { uid, type ChartElement, type LineEnding, type ShapeElement, type SlideElement, type TextElement, type TransitionKind } from '../model'+import { uid, type ChartElement, type LineEnding, type ShapeElement, type Slide, type SlideElement, type TextElement, type TransitionKind } from '../model'import { CHART_PRESETS } from '../charts'import { FONT_CHOICES, firstFamily, injectFonts } from '../fonts'import { ICONS } from '../icons'@@ -150,6 +150,21 @@ export class PropsPanel {}}+ const saveLy = document.createElement('button')+ saveLy.className = 'ed-btn ed-btn-block'+ saveLy.textContent = '+ Save slide as layout…'+ saveLy.title = "Add this slide to the document's layout picker (New slide button)"+ saveLy.addEventListener('click', () => {+ const name = window.prompt('Layout name', this.store.slide.name ?? 'My layout')+ if (!name) return+ this.edit(() => {+ const doc = this.store.doc+ const copy: Slide = JSON.parse(JSON.stringify(this.store.slide))+ doc.layouts = [...(doc.layouts ?? []), { ...copy, id: uid('layout'), name, stateOf: undefined, notes: '' }]+ }, true)+ })+ this.host.appendChild(saveLy)+this.section('Speaker notes')const notes = document.createElement('textarea')notes.className = 'ed-notes'diff --git a/slides/src/model.ts b/slides/src/model.tsindex 57f04f58..b185b38d 100644--- a/slides/src/model.ts+++ b/slides/src/model.ts@@ -69,6 +69,12 @@ export interface TextElement extends ElementBase {lineHeight: number/** px; optional tracking for letter-spaced caps labels */letterSpacing?: number+ /**+ * Layout placeholder prompt ("Click to add title"). While the element's+ * html is empty: the editor shows this dimmed; present and print hide the+ * element entirely. Cleared content brings the prompt back.+ */+ placeholder?: string}export type ShapeKind = 'rect' | 'ellipse' | 'triangle' | 'arrow' | 'line' | 'path'@@ -193,6 +199,14 @@ export interface BentoDoc {* data living in assets (data: URI). Elements then use `family` normally.*/fonts?: Array<{ family: string; asset: string; weight?: string; style?: string }>+ /**+ * Slide layouts: slide-shaped templates that live outside slides[].+ * Instantiating one deep-copies its elements KEEPING their ids — slides+ * born from the same layout share ids, so their common chrome morphs+ * across transitions and stays traceable for a future re-apply merge.+ * When absent, the editor offers its built-in starter layouts.+ */+ layouts?: Slide[]slides: Slide[]modified: string}@@ -273,6 +287,90 @@ export function emptySlide(partial: Partial<Slide> = {}): Slide {}}+// ---------------------------------------------------------------------------+// Layouts. A layout is a Slide that lives in doc.layouts (or the built-in+// set below). Element ids are deterministic per layout and are KEPT when a+// layout is instantiated: slides born from the same layout share ids, so+// their common chrome morphs across transitions.++const ph = (+ id: string,+ placeholder: string,+ frame: { x: number; y: number; w: number; h: number },+ type: Partial<TextElement> = {},+): TextElement => ({+ id,+ type: 'text',+ ...frame,+ rotation: 0, opacity: 1,+ html: '',+ placeholder,+ fontSize: 32,+ fontFamily: FONT_STACK,+ fontWeight: 400,+ color: '#1E2A3A',+ align: 'left',+ valign: 'top',+ lineHeight: 1.25,+ ...type,+})++const bar = (id: string, frame: { x: number; y: number; w: number; h: number }): ShapeElement => ({+ id, type: 'shape', shape: 'rect', ...frame,+ rotation: 0, opacity: 1, fill: '#F7A600', stroke: 'transparent', strokeWidth: 0, radius: 2,+})++/** The layouts every document offers out of the box (not persisted until edited). */+export function builtinLayouts(): Slide[] {+ return [+ {+ id: 'layout-title', name: 'Title', background: '#FFFFFF', transition: 'fade', notes: '', elements: [+ bar('lt-bar', { x: 160, y: 380, w: 72, h: 8 }),+ ph('lt-title', 'Click to add title', { x: 160, y: 404, w: 1280, h: 140 },+ { fontSize: 76, fontWeight: 700, valign: 'middle' }),+ ph('lt-sub', 'Click to add subtitle', { x: 160, y: 556, w: 1100, h: 60 },+ { fontSize: 28, color: '#45566B', valign: 'middle' }),+ ],+ },+ {+ id: 'layout-title-content', name: 'Title + content', background: '#FFFFFF', transition: 'fade', notes: '', elements: [+ ph('ltc-title', 'Click to add title', { x: 120, y: 72, w: 1360, h: 84 },+ { fontSize: 44, fontWeight: 700, valign: 'middle' }),+ bar('ltc-rule', { x: 120, y: 168, w: 1360, h: 3 }),+ ph('ltc-body', 'Click to add content', { x: 120, y: 208, w: 1360, h: 600 },+ { fontSize: 26, color: '#586A80', valign: 'top', lineHeight: 1.5 }),+ ],+ },+ {+ id: 'layout-two-col', name: 'Two columns', background: '#FFFFFF', transition: 'fade', notes: '', elements: [+ ph('l2c-title', 'Click to add title', { x: 120, y: 72, w: 1360, h: 84 },+ { fontSize: 44, fontWeight: 700, valign: 'middle' }),+ bar('l2c-rule', { x: 120, y: 168, w: 1360, h: 3 }),+ ph('l2c-left', 'Left column', { x: 120, y: 208, w: 660, h: 600 },+ { fontSize: 24, valign: 'top', lineHeight: 1.5 }),+ ph('l2c-right', 'Right column', { x: 820, y: 208, w: 660, h: 600 },+ { fontSize: 24, valign: 'top', lineHeight: 1.5 }),+ ],+ },+ {+ id: 'layout-section', name: 'Section divider', background: '#1E2A3A', transition: 'fade', notes: '', elements: [+ bar('lsec-bar', { x: 160, y: 396, w: 72, h: 8 }),+ ph('lsec-title', 'Section title', { x: 160, y: 420, w: 1280, h: 120 },+ { fontSize: 64, fontWeight: 700, color: '#FFFFFF', valign: 'middle' }),+ ph('lsec-kicker', 'PART 1', { x: 160, y: 350, w: 800, h: 40 },+ { fontSize: 18, fontWeight: 600, color: '#F7A600', letterSpacing: 3, valign: 'middle' }),+ ],+ },+ { id: 'layout-blank', name: 'Blank', background: '#FFFFFF', transition: 'fade', notes: '', elements: [] },+ ]+}++/** A fresh slide from a layout — new slide id, element ids KEPT (lineage). */+export function instantiateLayout(layout: Slide): Slide {+ const copy: Slide = JSON.parse(JSON.stringify(layout))+ return { ...copy, id: uid('slide'), name: undefined, stateOf: undefined, notes: '' }+}+export function newDoc(): BentoDoc {return {format: FORMAT,diff --git a/slides/src/present.ts b/slides/src/present.tsindex a4ee5a7b..e4cbf7fd 100644--- a/slides/src/present.ts+++ b/slides/src/present.ts@@ -41,7 +41,7 @@ export function startPresentation(// Morph slides swap instantly; the Flip animation supplies the motion.section.dataset.transition = slide.transition === 'morph' ? 'none' : slide.transitionif (slide.stateOf) section.dataset.bentoState = '1' // dimmed in overview- const surface = renderSlide(slide, doc)+ const surface = renderSlide(slide, doc, { hidePlaceholders: true })// reveal slides start with only the default hover set visibleif (slide.hover?.type === 'reveal') applyRevealSet(surface, slide.hover.default ?? null, slide.hover.default)section.appendChild(surface)diff --git a/slides/src/render.ts b/slides/src/render.tsindex 9560fd65..c4cc4c6b 100644--- a/slides/src/render.ts+++ b/slides/src/render.ts@@ -9,6 +9,8 @@ const SVG_NS = 'http://www.w3.org/2000/svg'export interface RenderOpts {/** render svg elements as <img> (cheap DOM) — used by thumbnails */svgAsImage?: boolean+ /** hide empty placeholder text entirely — present mode and print */+ hidePlaceholders?: boolean}/** Resolve "asset:<key>" references against the document's asset table. */@@ -305,6 +307,16 @@ export function renderElement(el: SlideElement, doc: BentoDoc, opts: RenderOptsif (el.letterSpacing) inner.style.letterSpacing = `${el.letterSpacing}px`inner.style.width = '100%'inner.innerHTML = sanitizeHtml(el.html)+ // layout placeholder: prompt while empty (editor), gone while presenting+ const isEmpty = !inner.textContent?.trim() && !el.html.includes('<img')+ if (el.placeholder && isEmpty) {+ if (opts.hidePlaceholders) {+ node.style.display = 'none'+ } else {+ inner.textContent = el.placeholder+ inner.style.opacity = '0.38'+ }+ }node.appendChild(inner)break}diff --git a/slides/src/styles.css b/slides/src/styles.cssindex 9f2b1481..8b382468 100644--- a/slides/src/styles.css+++ b/slides/src/styles.css@@ -498,6 +498,70 @@ html, body {}.ed-arrange-btn:disabled { opacity: 0.35; cursor: default; }+/* layout picker — popover over the New-slide button */+.ed-layoutpick {+ position: fixed;+ z-index: 200;+ background: #fff;+ border: 1px solid var(--line);+ border-radius: 12px;+ box-shadow: 0 12px 32px rgb(15 23 36 / 0.18);+ padding: 12px 14px;+ max-width: 420px;+ max-height: 70vh;+ overflow-y: auto;+}+.ed-layoutpick-h {+ font-size: 10.5px;+ font-weight: 700;+ text-transform: uppercase;+ letter-spacing: 0.07em;+ color: var(--muted);+ margin: 4px 2px 8px;+}+.ed-layoutpick-h + .ed-layoutpick-h, .ed-layoutpick-grid + .ed-layoutpick-h { margin-top: 14px; }+.ed-layoutpick-grid {+ display: grid;+ grid-template-columns: repeat(3, auto);+ gap: 10px;+}+.ed-layoutpick-item {+ position: relative;+ cursor: pointer;+ border: 2px solid var(--line);+ border-radius: 8px;+ padding: 3px;+ text-align: center;+ transition: border-color 0.12s ease;+}+.ed-layoutpick-item:hover { border-color: var(--accent); }+.ed-layoutpick-name {+ font-size: 11px;+ color: var(--muted);+ margin-top: 3px;+ overflow: hidden;+ text-overflow: ellipsis;+ white-space: nowrap;+ max-width: 110px;+}+.ed-layoutpick-del {+ position: absolute;+ top: -7px;+ right: -7px;+ width: 18px;+ height: 18px;+ border: none;+ border-radius: 50%;+ background: #c9302c;+ color: #fff;+ font-size: 10px;+ line-height: 1;+ padding: 0;+ cursor: pointer;+ display: none;+}+.ed-layoutpick-item:hover .ed-layoutpick-del { display: block; }+/* slide-insertion gaps between sidebar thumbnails */.ed-insertgap {flex: none;