Bento, the office suite that fits in a file
TypeScript mit-licenseofficeoffice-365office-suiteoffice-toolsoffice365open-sourcepowerpointself-hostedsingle-page-appslidesslideshareslideshowslideshow-galleryslideshow-makerslideshows
Layout apply with roles; picker from insert-gaps
nyblnetaae74ed95dee
diff --git a/CLAUDE.md b/CLAUDE.mdindex 4e2c88b5..d3a0a3b5 100644--- a/CLAUDE.md+++ b/CLAUDE.md@@ -61,7 +61,11 @@ One HTML file = the document + viewer + editor. See `README.md` for the vision.their chrome morphs across transitions (and re-apply-by-lineage stayspossible). Text `placeholder` prompts render dimmed in the editor and arehidden in present/print (`hidePlaceholders` RenderOpt). Picker on the- New-slide button; "Save slide as layout" in the slide panel.+ New-slide button AND the insert-gaps; "Save slide as layout" + "Apply+ layout" in the slide panel. Apply (model.applyLayout) matches donors by id,+ then by `role` (title/subtitle/body/kicker — Role select in the element+ panel); layout supplies frame+typography, content rides along; layout-owned+ leftovers are dropped UNLESS they are text someone wrote; user extras stay.- **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 f1daa25e..64cf38b3 100644--- a/docs/architecture.md+++ b/docs/architecture.md@@ -177,7 +177,7 @@ BentoDoc├─ hover? ← { type:'focus-group', dim } | { type:'reveal', default }└─ elements: SlideElement[] ← array order = paint order (z)├─ common: id · x y w h · rotation · opacity- │ fx? · link? · group? · groupId? · showOnHover?+ │ fx? · link? · group? · groupId? · showOnHover? · role?├─ 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?diff --git a/slides/src/editor/editor.ts b/slides/src/editor/editor.tsindex 445fd2ec..d5afad2b 100644--- a/slides/src/editor/editor.ts+++ b/slides/src/editor/editor.ts@@ -3,8 +3,8 @@import type { Store } from '../store'import {- builtinLayouts, defaultChart, defaultImage, defaultShape, defaultText, emptySlide,- instantiateLayout, uid,+ applyLayout, builtinLayouts, defaultChart, defaultImage, defaultShape, defaultText,+ instantiateLayout, layoutElementIds, uid,type ShapeKind, type Slide, type SlideElement,} from '../model'import { CHART_PRESETS } from '../charts'@@ -50,6 +50,9 @@ export class Editor {window.addEventListener('beforeunload', (ev) => {if (store.dirty) ev.preventDefault()})+ document.addEventListener('bento:apply-layout', ((ev: CustomEvent) => {+ this.openLayoutPicker(ev.detail.anchor as HTMLElement, { kind: 'apply' })+ }) as EventListener)this.rebuildSidebar()}@@ -260,11 +263,20 @@ export class Editor {// --- layouts ---------------------------------------------------------------- /** Popover over the New-slide button: built-in layouts + this document's. */- private openLayoutPicker(anchor: HTMLElement) {+ /** Layout popover. Serves three flows: the New-slide button, the+ * insert-gaps (both insert at a position), and Apply-to-current-slide. */+ private openLayoutPicker(+ anchor: HTMLElement,+ action: { kind: 'insert'; at: number } | { kind: 'apply' } = { kind: 'insert', at: this.store.currentIndex + 1 },+ ) {document.querySelector('.ed-layoutpick')?.remove()const pick = div('ed-layoutpick')const doc = this.store.doc+ if (action.kind === 'apply') {+ const t = div('ed-layoutpick-title')+ t.textContent = 'Apply layout to this slide'+ pick.appendChild(t)+ }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) {@@ -280,7 +292,8 @@ export class Editor {item.appendChild(name)item.addEventListener('click', () => {pick.remove()- this.addSlideFromLayout(ly)+ if (action.kind === 'insert') this.insertSlideFromLayout(ly, action.at)+ else this.applyLayoutToCurrent(ly)})if (custom) {const del = document.createElement('button')@@ -302,8 +315,15 @@ export class Editor {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`+ if (anchor.classList.contains('ed-add-slide')) {+ // bottom-of-sidebar button: open upward from it+ pick.style.left = `${Math.max(8, r.left)}px`+ pick.style.bottom = `${window.innerHeight - r.top + 8}px`+ } else {+ // insert-gap or panel button: open beside the anchor, clamped on-screen+ pick.style.left = `${Math.max(8, Math.min(r.right + 10, window.innerWidth - 440))}px`+ pick.style.top = `${Math.max(8, Math.min(r.top - 40, window.innerHeight - 460))}px`+ }document.body.appendChild(pick)const close = (ev: PointerEvent) => {if (!pick.contains(ev.target as Node)) {@@ -314,12 +334,24 @@ export class Editor {setTimeout(() => document.addEventListener('pointerdown', close, true))}- private addSlideFromLayout(layout: Slide) {+ private insertSlideFromLayout(layout: Slide, at: number) {const slide = instantiateLayout(layout)this.store.commit(() => {- this.store.doc.slides.splice(this.store.currentIndex + 1, 0, slide)+ this.store.doc.slides.splice(at, 0, slide)}, 'slides')- this.store.goTo(this.store.currentIndex + 1)+ this.store.goTo(at)+ }++ /** Re-arrange the current slide onto a layout: content matched by id, then+ * by role; the layout brings frame + typography; extras are kept on top. */+ private applyLayoutToCurrent(layout: Slide) {+ const known = layoutElementIds(this.store.doc)+ this.store.commit(() => {+ const s = this.store.slide+ s.elements = applyLayout(s, layout, known)+ s.background = layout.background+ })+ this.store.select([])}/** Slim hover strip between thumbnails — click inserts a blank slide there. */@@ -331,13 +363,7 @@ export class Editor {plus.textContent = '+'plus.tabIndex = -1gap.appendChild(plus)- gap.addEventListener('click', () => {- const bg = this.store.slide.background- this.store.commit(() => {- this.store.doc.slides.splice(at, 0, emptySlide({ background: bg }))- }, 'slides')- this.store.goTo(at)- })+ gap.addEventListener('click', () => this.openLayoutPicker(gap, { kind: 'insert', at }))return gap}diff --git a/slides/src/editor/panels.ts b/slides/src/editor/panels.tsindex 55bb910f..9d30ee7e 100644--- a/slides/src/editor/panels.ts+++ b/slides/src/editor/panels.ts@@ -150,6 +150,14 @@ export class PropsPanel {}}+ const applyLy = document.createElement('button')+ applyLy.className = 'ed-btn ed-btn-block'+ applyLy.textContent = '⧉ Apply layout…'+ applyLy.title = 'Re-arrange this slide onto a layout — content moves by matching id, then role; extra elements are kept'+ applyLy.addEventListener('click', () =>+ document.dispatchEvent(new CustomEvent('bento:apply-layout', { detail: { anchor: applyLy } })))+ this.host.appendChild(applyLy)+const saveLy = document.createElement('button')saveLy.className = 'ed-btn ed-btn-block'saveLy.textContent = '+ Save slide as layout…'@@ -198,6 +206,14 @@ export class PropsPanel {)this.host.appendChild(geo)+ this.row('Role', this.select(+ ['none', 'title', 'subtitle', 'body', 'kicker'],+ el.role ?? 'none',+ (v) => this.mutate(el.id, (e) => {+ if (v === 'none') delete e.role+ else e.role = v+ }, true)))+if (el.type === 'text') this.buildTextProps(el)if (el.type === 'shape') this.buildShapeProps(el)if (el.type === 'image') this.buildImageProps(el)diff --git a/slides/src/model.ts b/slides/src/model.tsindex b185b38d..705168aa 100644--- a/slides/src/model.ts+++ b/slides/src/model.ts@@ -54,6 +54,13 @@ export interface ElementBase {* The slide's hover.default set is shown when nothing is hovered.*/showOnHover?: string+ /**+ * Layout role — what this element IS on the slide ('title', 'subtitle',+ * 'body', 'kicker'). Applying a different layout moves content between+ * same-role elements, PowerPoint-placeholder style. Free-form string;+ * those four are the conventions the built-in layouts use.+ */+ role?: string}export interface TextElement extends ElementBase {@@ -327,38 +334,38 @@ export function builtinLayouts(): Slide[] {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' }),+ { fontSize: 76, fontWeight: 700, valign: 'middle', role: 'title' }),ph('lt-sub', 'Click to add subtitle', { x: 160, y: 556, w: 1100, h: 60 },- { fontSize: 28, color: '#45566B', valign: 'middle' }),+ { fontSize: 28, color: '#45566B', valign: 'middle', role: 'subtitle' }),],},{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' }),+ { fontSize: 44, fontWeight: 700, valign: 'middle', role: 'title' }),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 }),+ { fontSize: 26, color: '#586A80', valign: 'top', lineHeight: 1.5, role: 'body' }),],},{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' }),+ { fontSize: 44, fontWeight: 700, valign: 'middle', role: 'title' }),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 }),+ { fontSize: 24, valign: 'top', lineHeight: 1.5, role: 'body' }),ph('l2c-right', 'Right column', { x: 820, y: 208, w: 660, h: 600 },- { fontSize: 24, valign: 'top', lineHeight: 1.5 }),+ { fontSize: 24, valign: 'top', lineHeight: 1.5, role: 'body' }),],},{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' }),+ { fontSize: 64, fontWeight: 700, color: '#FFFFFF', valign: 'middle', role: 'title' }),ph('lsec-kicker', 'PART 1', { x: 160, y: 350, w: 800, h: 40 },- { fontSize: 18, fontWeight: 600, color: '#F7A600', letterSpacing: 3, valign: 'middle' }),+ { fontSize: 18, fontWeight: 600, color: '#F7A600', letterSpacing: 3, valign: 'middle', role: 'kicker' }),],},{ id: 'layout-blank', name: 'Blank', background: '#FFFFFF', transition: 'fade', notes: '', elements: [] },@@ -371,6 +378,65 @@ export function instantiateLayout(layout: Slide): Slide {return { ...copy, id: uid('slide'), name: undefined, stateOf: undefined, notes: '' }}+const textHasContent = (e: SlideElement) =>+ e.type !== 'text' || !!e.html.replace(/<br\s*\/?>/gi, '').replace(/\u200B/g, '').trim()++/**+ * Apply a layout to an existing slide's elements. The matching ladder:+ * 1. by id — re-applying the slide's own layout resets frames/typography+ * while keeping content+ * 2. by role — cross-layout: the slide's 'title' moves into the new+ * layout's 'title' frame (same element type required;+ * donors consumed in document order)+ * Content (text html, link) rides along; the layout provides frame and+ * typography. Leftover slide elements that belong to some KNOWN layout+ * (old chrome, unfilled placeholders) are dropped; everything else is user+ * content and survives on top of the new layout's elements.+ */+export function applyLayout(+ slide: Slide,+ layout: Slide,+ knownLayoutElementIds: Set<string>,+): SlideElement[] {+ const donors = slide.elements+ const consumed = new Set<SlideElement>()+ const findDonor = (lel: SlideElement): SlideElement | undefined => {+ const byId = donors.find((e) => !consumed.has(e) && e.id === lel.id)+ if (byId) return byId+ if (!lel.role) return undefined+ return donors.find(+ (e) => !consumed.has(e) && e.role === lel.role && e.type === lel.type && textHasContent(e),+ )+ }+ const out: SlideElement[] = layout.elements.map((lel) => {+ const copy = JSON.parse(JSON.stringify(lel)) as SlideElement+ const d = findDonor(lel)+ if (d) {+ consumed.add(d)+ if (copy.type === 'text' && d.type === 'text' && textHasContent(d)) copy.html = d.html+ if (d.link) copy.link = d.link+ }+ return copy+ })+ for (const e of donors) {+ if (consumed.has(e)) continue+ // layout-owned leftovers: drop chrome and EMPTY placeholders, but text+ // someone actually wrote is never silently lost — it rides along as-is+ if (knownLayoutElementIds.has(e.id) && !(e.type === 'text' && textHasContent(e))) continue+ out.push(e) // survives, painted above the layout+ }+ return out+}++/** Every element id owned by any known layout (built-ins + the document's). */+export function layoutElementIds(doc: BentoDoc): Set<string> {+ const ids = new Set<string>()+ for (const ly of [...builtinLayouts(), ...(doc.layouts ?? [])]) {+ for (const e of ly.elements) ids.add(e.id)+ }+ return ids+}+export function newDoc(): BentoDoc {return {format: FORMAT,diff --git a/slides/src/styles.css b/slides/src/styles.cssindex 8b382468..5f6ebf28 100644--- a/slides/src/styles.css+++ b/slides/src/styles.css@@ -511,6 +511,11 @@ html, body {max-height: 70vh;overflow-y: auto;}+.ed-layoutpick-title {+ font-size: 13px;+ font-weight: 700;+ margin: 2px 2px 10px;+}.ed-layoutpick-h {font-size: 10.5px;font-weight: 700;