Bento, the office suite that fits in a file
TypeScript mit-licenseofficeoffice-365office-suiteoffice-toolsoffice365open-sourcepowerpointself-hostedsingle-page-appslidesslideshareslideshowslideshow-galleryslideshow-makerslideshows
First-class interactive states: stateOf slides + authoring UX
nyblnet9e72d54b429a
diff --git a/slides/src/editor/editor.ts b/slides/src/editor/editor.tsindex 66902461..307c0d21 100644--- a/slides/src/editor/editor.ts+++ b/slides/src/editor/editor.ts@@ -120,28 +120,54 @@ export class Editor {// --- sidebar -----------------------------------------------------------------+ private makeThumb(slide: import('../model').Slide, i: number, isState: boolean): HTMLElement {+ const item = div('ed-thumb')+ item.dataset.index = String(i)+ item.draggable = !isState+ const num = div('ed-thumb-num')+ if (isState) {+ const parentIdx = this.store.doc.slides.findIndex((s) => s.id === slide.stateOf)+ num.textContent = slide.name ?? `⤷ ${parentIdx + 1}`+ num.title = `Interactive state of slide ${parentIdx + 1} — reached via links while presenting`+ } else {+ num.textContent = String(this.linearNumber(i))+ }+ const surface = renderThumbnail(slide, this.store.doc, 148)+ const tools = div('ed-thumb-tools')+ tools.append(+ btn(ICONS.copy, '', (ev) => { ev.stopPropagation(); this.duplicateSlide(i) }, 'Duplicate slide'),+ btn(ICONS.trash, '', (ev) => { ev.stopPropagation(); this.deleteSlide(i) }, 'Delete slide'),+ )+ item.append(num, surface, tools)+ item.addEventListener('click', () => this.store.goTo(i))+ if (!isState) this.wireThumbDrag(item, i)+ return item+ }++ /** 1-based position among non-state slides (what the audience counts). */+ private linearNumber(i: number): number {+ return this.store.doc.slides.slice(0, i + 1).filter((s) => !s.stateOf).length+ }+private rebuildSidebar() {this.sidebar.innerHTML = ''- this.store.doc.slides.forEach((slide, i) => {- const item = div('ed-thumb')- item.dataset.index = String(i)- item.draggable = true- const num = div('ed-thumb-num')- num.textContent = String(i + 1)- const surface = renderThumbnail(slide, this.store.doc, 148)- const tools = div('ed-thumb-tools')- tools.append(- btn(ICONS.copy, '', (ev) => { ev.stopPropagation(); this.duplicateSlide(i) }, 'Duplicate slide'),- btn(ICONS.trash, '', (ev) => { ev.stopPropagation(); this.deleteSlide(i) }, 'Delete slide'),- )- item.append(num, surface, tools)- item.addEventListener('click', () => this.store.goTo(i))- this.wireThumbDrag(item, i)- this.sidebar.appendChild(item)+ const slides = this.store.doc.slides+ slides.forEach((slide, i) => {+ if (!slide.stateOf) this.sidebar.appendChild(this.makeThumb(slide, i, false))})const add = btn(ICONS.plus, 'New slide', () => this.addSlide())add.classList.add('ed-add-slide')this.sidebar.appendChild(add)++ if (slides.some((s) => s.stateOf)) {+ const divider = div('ed-side-divider')+ divider.textContent = 'Interactive states'+ divider.title = 'Hidden from arrow-key flow; opened by clicking linked elements'+ this.sidebar.appendChild(divider)+ slides.forEach((slide, i) => {+ if (slide.stateOf) this.sidebar.appendChild(this.makeThumb(slide, i, true))+ })+ }this.highlightSidebar()}@@ -170,21 +196,21 @@ export class Editor {}private highlightSidebar() {- this.sidebar.querySelectorAll('.ed-thumb').forEach((n, i) => {- n.classList.toggle('active', i === this.store.currentIndex)+ this.sidebar.querySelectorAll<HTMLElement>('.ed-thumb').forEach((n) => {+ n.classList.toggle('active', Number(n.dataset.index) === this.store.currentIndex)})}private scheduleThumbs() {clearTimeout(this.thumbTimer)this.thumbTimer = window.setTimeout(() => {- const thumbs = this.sidebar.querySelectorAll('.ed-thumb')- this.store.doc.slides.forEach((slide, i) => {- const item = thumbs[i]- if (!item) return this.rebuildSidebar()+ const thumbs = this.sidebar.querySelectorAll<HTMLElement>('.ed-thumb')+ if (thumbs.length !== this.store.doc.slides.length) return this.rebuildSidebar()+ thumbs.forEach((item) => {+ const slide = this.store.doc.slides[Number(item.dataset.index)]+ if (!slide) returnitem.querySelector('.bento-thumb-surface')?.replaceWith(renderThumbnail(slide, this.store.doc, 148))})- if (thumbs.length !== this.store.doc.slides.length) this.rebuildSidebar()}, 150)}diff --git a/slides/src/editor/panels.ts b/slides/src/editor/panels.tsindex baabb59f..7eed1aab 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 type { ShapeElement, SlideElement, TextElement, TransitionKind } from '../model'+import { uid, type ShapeElement, type SlideElement, type TextElement, type TransitionKind } from '../model'import { ICONS } from '../icons'export class PropsPanel {@@ -57,6 +57,52 @@ export class PropsPanel {hint.innerHTML = '<b>Morph</b> animates elements that appear on both this slide and the previous one (copy a slide, then move things around).'this.host.appendChild(hint)+ // interactivity: naming, state-of, hover focus+ this.section('Interactivity')+ const name = document.createElement('input')+ name.type = 'text'+ name.placeholder = 'unnamed'+ name.value = slide.name ?? ''+ name.addEventListener('change', () =>+ this.edit(() => { this.store.slide.name = name.value || undefined }, true))+ this.row('Name', name)++ const stateSel = document.createElement('select')+ const optNone = document.createElement('option')+ optNone.value = ''+ optNone.textContent = 'no — normal slide'+ stateSel.appendChild(optNone)+ this.store.doc.slides.forEach((s, i) => {+ if (s.stateOf || s.id === slide.id) return+ const o = document.createElement('option')+ o.value = s.id+ o.textContent = this.slideLabel(s, i)+ if (slide.stateOf === s.id) o.selected = true+ stateSel.appendChild(o)+ })+ stateSel.addEventListener('change', () =>+ this.edit(() => {+ this.store.slide.stateOf = stateSel.value || undefined+ this.store.emit('slides')+ }, true))+ this.row('State of', stateSel)+ const stateHint = document.createElement('p')+ stateHint.className = 'ed-hint'+ stateHint.innerHTML =+ 'A <b>state</b> is hidden from arrow-key flow — viewers reach it by clicking a linked element. Shared element ids morph between states.'+ this.host.appendChild(stateHint)++ this.row('Hover', this.select(+ ['none', 'focus-group'],+ slide.hover?.type ?? 'none',+ (v) => this.edit(() => {+ this.store.slide.hover = v === 'focus-group' ? { type: 'focus-group', dim: slide.hover?.dim ?? 0.15 } : undefined+ }, true)))+ if (slide.hover) {+ this.row('Hover dim', this.number(slide.hover.dim ?? 0.15, 0.01, (v, fin) =>+ this.edit(() => { if (this.store.slide.hover) this.store.slide.hover.dim = Math.min(Math.max(v, 0), 1) }, fin)))+ }+this.section('Speaker notes')const notes = document.createElement('textarea')notes.className = 'ed-notes'@@ -122,6 +168,18 @@ export class PropsPanel {['none', 'kenburns'], el.fx?.ambient ?? 'none',(v) => setFx({ ambient: v === 'none' ? undefined : 'kenburns' })))+ // group tag (hover focus & interaction targeting)+ const group = document.createElement('input')+ group.type = 'text'+ group.placeholder = 'none'+ group.value = el.group ?? ''+ group.addEventListener('change', () =>+ this.mutate(el.id, (e) => {+ if (group.value) e.group = group.value+ else delete e.group+ }, true))+ this.row('Group', group)+// link → slide pickerconst sel = document.createElement('select')const none = document.createElement('option')@@ -131,7 +189,7 @@ export class PropsPanel {this.store.doc.slides.forEach((s, i) => {const o = document.createElement('option')o.value = s.id- o.textContent = `slide ${i + 1}`+ o.textContent = this.slideLabel(s, i)if (el.link === s.id) o.selected = truesel.appendChild(o)})@@ -141,6 +199,45 @@ export class PropsPanel {else delete e.link}, true))this.row('Link to', sel)++ // one-click interactivity: duplicate this slide as a hidden state+ // (element ids preserved ⇒ it morphs) and link this element to it+ const makeState = document.createElement('button')+ makeState.className = 'ed-btn ed-btn-block'+ makeState.textContent = '+ New state linked from this element'+ makeState.title = 'Duplicates this slide as a hidden interactive state and links the selected element to it'+ makeState.addEventListener('click', () => this.createLinkedState(el))+ this.host.appendChild(makeState)+ }++ /** Duplicate the current slide as a hidden state and link `el` to it. */+ private createLinkedState(el: SlideElement) {+ const src = this.store.slide+ const clone = JSON.parse(JSON.stringify(src)) as typeof src+ clone.id = uid('slide')+ clone.stateOf = src.stateOf ?? src.id // states of a state share one parent+ clone.name = `${src.name ?? 'state'} ${this.store.doc.slides.filter((s) => s.stateOf === clone.stateOf).length + 2}`+ clone.transition = 'morph'+ delete (clone as any).hover // inherit nothing implicit; user can re-enable+ if (src.hover) clone.hover = { ...src.hover }+ const targetIdx = this.store.doc.slides.length+ this.store.commit(() => {+ this.store.doc.slides.push(clone)+ const live = this.store.element(el.id)+ if (live) live.link = clone.id+ }, 'slides')+ this.store.goTo(targetIdx)+ }++ /** Human label for a slide in pickers. */+ private slideLabel(s: { id: string; name?: string; stateOf?: string }, i: number): string {+ const linear = this.store.doc.slides.slice(0, i + 1).filter((x) => !x.stateOf).length+ if (s.stateOf) {+ const p = this.store.doc.slides.findIndex((x) => x.id === s.stateOf)+ const pn = this.store.doc.slides.slice(0, p + 1).filter((x) => !x.stateOf).length+ return `state of ${pn}${s.name ? ` — ${s.name}` : ''}`+ }+ return `slide ${linear}${s.name ? ` — ${s.name}` : ''}`}private buildTextProps(el: TextElement) {diff --git a/slides/src/model.ts b/slides/src/model.tsindex ea409089..e1b39d02 100644--- a/slides/src/model.ts+++ b/slides/src/model.ts@@ -99,6 +99,16 @@ export interface Slide {transition: TransitionKindelements: SlideElement[]notes: string+ /** optional friendly name (link pickers, state badges) */+ name?: string+ /**+ * Interactive state: this slide is a variant of the slide with the given+ * id. It is hidden from linear navigation — reachable only via element+ * links (and morphs smoothly when element ids are shared with its parent).+ * While on a state: ArrowLeft returns to the parent, ArrowRight continues+ * after the parent.+ */+ stateOf?: string/** present-mode hover behaviour: focus the hovered element's group */hover?: { type: 'focus-group'; dim?: number }}diff --git a/slides/src/present.ts b/slides/src/present.tsindex 5534b74e..461fd16f 100644--- a/slides/src/present.ts+++ b/slides/src/present.ts@@ -55,6 +55,30 @@ export function startPresentation(document.body.appendChild(overlay)+ // ——— state-aware linear navigation ———+ // Slides with stateOf are interactive states: linked-to, never walked-to.+ const isState = (i: number) => !!doc.slides[i]?.stateOf+ const anchorOf = (i: number) => {+ const pid = doc.slides[i]?.stateOf+ const p = doc.slides.findIndex((s) => s.id === pid)+ return p >= 0 ? p : i+ }+ const goNext = () => {+ const cur = deck.getIndices().h+ for (let i = (isState(cur) ? anchorOf(cur) : cur) + 1; i < doc.slides.length; i++) {+ if (!isState(i)) return deck.slide(i, 0)+ }+ }+ const goPrev = () => {+ const cur = deck.getIndices().h+ if (isState(cur)) return deck.slide(anchorOf(cur), 0)+ for (let i = cur - 1; i >= 0; i--) {+ if (!isState(i)) return deck.slide(i, 0)+ }+ }+ const visibleIndex = (i: number) => doc.slides.slice(0, i + 1).filter((s) => !s.stateOf).length+ const visibleTotal = doc.slides.filter((s) => !s.stateOf).length+let exited = falseconst deck = new Reveal(revealEl, {embedded: true,@@ -69,7 +93,14 @@ export function startPresentation(backgroundTransition: 'fade',controls: doc.present?.controls ?? true,progress: doc.present?.progress ?? true,- slideNumber: (doc.present?.slideNumber ?? true) ? 'c/t' : false,+ slideNumber: (doc.present?.slideNumber ?? true)+ ? (((slideEl: HTMLElement) => {+ const i = [...slidesEl.children].indexOf(slideEl)+ return [`${visibleIndex(i)} / ${visibleTotal}`]+ }) as any)+ : false,+ // swipes would walk into hidden state slides; arrows are handled below+ touch: !doc.slides.some((s) => s.stateOf),keyboardCondition: null,plugins: [RevealNotes],})@@ -103,15 +134,15 @@ export function startPresentation(exit()return}- const key = ev.key || ({ 37: 'ArrowLeft', 39: 'ArrowRight', 33: 'PageUp', 34: 'PageDown' } as Record<number, string>)[ev.keyCode]- if (key === 'ArrowRight' || key === 'PageDown') {+ const key = ev.key || ({ 32: ' ', 37: 'ArrowLeft', 39: 'ArrowRight', 33: 'PageUp', 34: 'PageDown' } as Record<number, string>)[ev.keyCode]+ if (key === 'ArrowRight' || key === 'PageDown' || key === ' ') {ev.preventDefault()ev.stopPropagation()- deck.next()+ goNext()} else if (key === 'ArrowLeft' || key === 'PageUp') {ev.preventDefault()ev.stopPropagation()- deck.prev()+ goPrev()}}document.addEventListener('keydown', onKeydown, true)diff --git a/slides/src/styles.css b/slides/src/styles.cssindex b18b2445..d6dd8481 100644--- a/slides/src/styles.css+++ b/slides/src/styles.css@@ -166,6 +166,34 @@ html, body {.ed-add-slide { border: 1.5px dashed var(--line); justify-content: center; color: var(--muted); }+.ed-side-divider {+ font-size: 10px;+ font-weight: 700;+ text-transform: uppercase;+ letter-spacing: 0.08em;+ color: var(--muted);+ border-top: 1px solid var(--line);+ padding-top: 10px;+ margin-top: 4px;+}++.ed-btn-block {+ width: 100%;+ justify-content: center;+ border: 1px solid var(--line);+ margin-top: 2px;+}++.ed-row input[type='text'] {+ width: 110px;+ font: inherit;+ font-size: 12.5px;+ padding: 4px 6px;+ border: 1px solid var(--line);+ border-radius: 6px;+ color: var(--ink);+}+/* ————— canvas ————— */.ed-canvas-wrap {