Skip to content

armgit / bento

Public Imported from GitHub
0

Bento, the office suite that fits in a file

Shared assets, inline svg elements with scoped CSS, fx/link editor UI

nyblnet72abce8ff1f6
diff --git a/slides/src/editor/panels.ts b/slides/src/editor/panels.ts
index 6ce6156b..baabb59f 100644
--- a/slides/src/editor/panels.ts
+++ b/slides/src/editor/panels.ts
@@ -73,7 +73,7 @@ export class PropsPanel {
}
private buildElementPanel(el: SlideElement) {
- this.section({ text: 'Text', shape: 'Shape', image: 'Image' }[el.type])
+ this.section({ text: 'Text', shape: 'Shape', image: 'Image', svg: 'Diagram' }[el.type])
this.opsRow([el])
// geometry
@@ -94,12 +94,55 @@ export class PropsPanel {
if (el.type === 'shape') this.buildShapeProps(el)
if (el.type === 'image') this.buildImageProps(el)
+ this.buildPresentingProps(el)
+
const morph = document.createElement('p')
morph.className = 'ed-hint'
morph.innerHTML = `Morph id: <code>${el.id}</code>`
this.host.appendChild(morph)
}
+ /** fx + link — how the element behaves while presenting. */
+ private buildPresentingProps(el: SlideElement) {
+ this.section('Presenting')
+ const setFx = (patch: Partial<NonNullable<SlideElement['fx']>>) =>
+ this.mutate(el.id, (e) => {
+ const fx = { ...(e.fx ?? {}), ...patch }
+ if (!fx.enter && !fx.countUp && !fx.ambient) delete e.fx
+ else e.fx = fx
+ }, true)
+
+ this.row('Enter', this.select(
+ ['none', 'fade', 'fade-up'], el.fx?.enter ?? 'none',
+ (v) => setFx({ enter: v === 'none' ? undefined : (v as 'fade' | 'fade-up') })))
+ this.row('Count up', this.select(
+ ['off', 'on'], el.fx?.countUp ? 'on' : 'off',
+ (v) => setFx({ countUp: v === 'on' ? true : undefined })))
+ this.row('Ambient', this.select(
+ ['none', 'kenburns'], el.fx?.ambient ?? 'none',
+ (v) => setFx({ ambient: v === 'none' ? undefined : 'kenburns' })))
+
+ // link → slide picker
+ const sel = document.createElement('select')
+ const none = document.createElement('option')
+ none.value = ''
+ none.textContent = 'none'
+ sel.appendChild(none)
+ this.store.doc.slides.forEach((s, i) => {
+ const o = document.createElement('option')
+ o.value = s.id
+ o.textContent = `slide ${i + 1}`
+ if (el.link === s.id) o.selected = true
+ sel.appendChild(o)
+ })
+ sel.addEventListener('change', () =>
+ this.mutate(el.id, (e) => {
+ if (sel.value) e.link = sel.value
+ else delete e.link
+ }, true))
+ this.row('Link to', sel)
+ }
+
private buildTextProps(el: TextElement) {
this.section('Typography')
this.row('Size', this.number(el.fontSize, 1, (v, fin) =>
diff --git a/slides/src/model.ts b/slides/src/model.ts
index 725f2ca7..1591919e 100644
--- a/slides/src/model.ts
+++ b/slides/src/model.ts
@@ -62,13 +62,26 @@ export interface ShapeElement extends ElementBase {
export interface ImageElement extends ElementBase {
type: 'image'
- /** data: URI — Bento files embed all assets */
+ /** data: URI, or "asset:<key>" referencing doc.assets */
src: string
fit: 'contain' | 'cover' | 'fill'
radius: number
}
-export type SlideElement = TextElement | ShapeElement | ImageElement
+export interface SvgElement extends ElementBase {
+ type: 'svg'
+ /** key into doc.assets holding raw SVG markup (preferred: dedupes) */
+ asset?: string
+ /** raw inline SVG markup, used when asset is unset */
+ markup?: string
+ /**
+ * CSS injected inside the svg — hover states, focus dims, and animations
+ * live here and stay self-contained (svg <style> scopes to its svg).
+ */
+ css?: string
+}
+
+export type SlideElement = TextElement | ShapeElement | ImageElement | SvgElement
export interface Slide {
id: string
@@ -96,6 +109,8 @@ export interface BentoDoc {
controls?: boolean
progress?: boolean
}
+ /** shared assets (raw SVG markup or data URIs), referenced by key */
+ assets?: Record<string, string>
slides: Slide[]
modified: string
}
diff --git a/slides/src/present.ts b/slides/src/present.ts
index eb2e7bf3..55758c18 100644
--- a/slides/src/present.ts
+++ b/slides/src/present.ts
@@ -26,6 +26,7 @@ export function startPresentation(
): PresentSession {
const overlay = document.createElement('div')
overlay.className = 'bento-present-overlay'
+ overlay.style.setProperty('--bento-accent', doc.theme.accent)
const revealEl = document.createElement('div')
revealEl.className = 'reveal'
@@ -113,6 +114,7 @@ export function startPresentation(
if (morphing) runMorph(doc, from!, to, fromIdx, toIdx)
else runEnterFx(doc.slides[toIdx], to)
runAmbientFx(doc.slides[toIdx], to)
+ restartSvgAnimations(to)
}) as any)
// Clicking an element with a link jumps to its target slide.
@@ -138,6 +140,7 @@ export function startPresentation(
if (first) {
runEnterFx(doc.slides[startIndex], first)
runAmbientFx(doc.slides[startIndex], first)
+ restartSvgAnimations(first)
}
})
@@ -208,6 +211,16 @@ function runCountUp(node: HTMLElement) {
})
}
+/** Re-parse inline svg elements so their CSS animations replay on entry. */
+function restartSvgAnimations(section: HTMLElement) {
+ for (const host of section.querySelectorAll<HTMLElement>('.bento-el-svg')) {
+ if (host.querySelector('animate, [style*="animation"], style')) {
+ // eslint-disable-next-line no-self-assign
+ host.innerHTML = host.innerHTML
+ }
+ }
+}
+
/** Continuous ambient motion (ken-burns style slow zoom on photos). */
function runAmbientFx(slide: Slide, section: HTMLElement) {
for (const [el, node] of fxNodes(slide, section)) {
diff --git a/slides/src/render.ts b/slides/src/render.ts
index 0bdfde9f..261043c8 100644
--- a/slides/src/render.ts
+++ b/slides/src/render.ts
@@ -1,10 +1,64 @@
// Shared model → DOM renderer. One code path draws slides everywhere:
// editor canvas, sidebar thumbnails, and Reveal.js sections.
-import type { BentoDoc, ShapeElement, Slide, SlideElement } from './model'
+import type { BentoDoc, ShapeElement, Slide, SlideElement, SvgElement } from './model'
const SVG_NS = 'http://www.w3.org/2000/svg'
+export interface RenderOpts {
+ /** render svg elements as <img> (cheap DOM) — used by thumbnails */
+ svgAsImage?: boolean
+}
+
+/** Resolve "asset:<key>" references against the document's asset table. */
+export function resolveAsset(doc: BentoDoc, ref: string): string {
+ return ref.startsWith('asset:') ? (doc.assets?.[ref.slice(6)] ?? '') : ref
+}
+
+function svgMarkup(el: SvgElement, doc: BentoDoc): string {
+ return (el.asset ? doc.assets?.[el.asset] : el.markup) ?? ''
+}
+
+/**
+ * Scope injected svg CSS to one element instance. svg <style> applies
+ * document-wide, so unscoped rules from one diagram would leak into every
+ * other svg on the page (including other slides' copies of the same asset).
+ * @keyframes blocks stay top-level; everything else gets the scope prefix.
+ */
+export function scopeCss(css: string, scope: string): string {
+ let out = ''
+ let i = 0
+ while (i < css.length) {
+ const rest = css.slice(i)
+ const at = rest.match(/^\s*@(keyframes|-webkit-keyframes)/)
+ if (at) {
+ // copy the whole block verbatim, tracking brace depth
+ let depth = 0
+ let j = i
+ let seen = false
+ while (j < css.length) {
+ if (css[j] === '{') { depth++; seen = true }
+ if (css[j] === '}') { depth--; if (seen && depth === 0) { j++; break } }
+ j++
+ }
+ out += css.slice(i, j) + '\n'
+ i = j
+ continue
+ }
+ const open = css.indexOf('{', i)
+ if (open === -1) break
+ const close = css.indexOf('}', open)
+ if (close === -1) break
+ const selectors = css.slice(i, open).trim()
+ if (selectors) {
+ out += selectors.split(',').map((s) => `${scope} ${s.trim()}`).join(', ')
+ out += ' ' + css.slice(open, close + 1) + '\n'
+ }
+ i = close + 1
+ }
+ return out
+}
+
export function applyElementFrame(node: HTMLElement, el: SlideElement) {
node.style.left = `${el.x}px`
node.style.top = `${el.y}px`
@@ -117,7 +171,7 @@ const VALIGN: Record<string, string> = { top: 'flex-start', middle: 'center', bo
* Render one element. The wrapper carries data-el-id (edit-time selection)
* and data-flip-id (GSAP Flip morph matching across slides).
*/
-export function renderElement(el: SlideElement, doc: BentoDoc): HTMLElement {
+export function renderElement(el: SlideElement, doc: BentoDoc, opts: RenderOpts = {}): HTMLElement {
const node = document.createElement('div')
node.className = `bento-el bento-el-${el.type}`
node.dataset.elId = el.id
@@ -149,25 +203,50 @@ export function renderElement(el: SlideElement, doc: BentoDoc): HTMLElement {
break
case 'image': {
const img = document.createElement('img')
- img.src = el.src
+ img.src = resolveAsset(doc, el.src)
img.draggable = false
img.style.cssText = `width:100%;height:100%;object-fit:${el.fit};border-radius:${el.radius}px;display:block`
node.appendChild(img)
break
}
+ case 'svg': {
+ const markup = svgMarkup(el, doc)
+ if (opts.svgAsImage) {
+ // thumbnails: one <img> instead of thousands of svg nodes
+ const img = document.createElement('img')
+ img.src = 'data:image/svg+xml;charset=utf-8,' + encodeURIComponent(markup)
+ img.draggable = false
+ img.style.cssText = 'width:100%;height:100%;object-fit:contain;display:block'
+ node.appendChild(img)
+ break
+ }
+ node.innerHTML = markup
+ const svg = node.querySelector('svg')
+ if (svg) {
+ svg.style.width = '100%'
+ svg.style.height = '100%'
+ svg.style.display = 'block'
+ if (el.css) {
+ const style = document.createElementNS(SVG_NS, 'style')
+ style.textContent = scopeCss(el.css, `[data-el-id="${CSS.escape(el.id)}"]`)
+ svg.prepend(style)
+ }
+ }
+ break
+ }
}
return node
}
/** Render a full slide surface (background + elements) at model coordinates. */
-export function renderSlide(slide: Slide, doc: BentoDoc): HTMLElement {
+export function renderSlide(slide: Slide, doc: BentoDoc, opts: RenderOpts = {}): HTMLElement {
const surface = document.createElement('div')
surface.className = 'bento-slide'
surface.dataset.slideId = slide.id
surface.style.width = `${doc.size.width}px`
surface.style.height = `${doc.size.height}px`
surface.style.background = slide.background
- for (const el of slide.elements) surface.appendChild(renderElement(el, doc))
+ for (const el of slide.elements) surface.appendChild(renderElement(el, doc, opts))
return surface
}
@@ -178,7 +257,7 @@ export function renderThumbnail(slide: Slide, doc: BentoDoc, width: number): HTM
box.className = 'bento-thumb-surface'
box.style.width = `${width}px`
box.style.height = `${doc.size.height * scale}px`
- const inner = renderSlide(slide, doc)
+ const inner = renderSlide(slide, doc, { svgAsImage: true })
inner.style.transformOrigin = '0 0'
inner.style.transform = `scale(${scale})`
box.appendChild(inner)
diff --git a/slides/src/styles.css b/slides/src/styles.css
index e29a55e9..b18b2445 100644
--- a/slides/src/styles.css
+++ b/slides/src/styles.css
@@ -346,6 +346,11 @@ html, body {
}
.bento-present-overlay .reveal .slides section { padding: 0; }
.bento-present-overlay .bento-el[data-link] { cursor: pointer; }
+.bento-present-overlay .bento-el[data-link]:hover {
+ outline: 2.5px solid var(--bento-accent, #f7a600);
+ outline-offset: 3px;
+ border-radius: 999px;
+}
/* moveable accents */
.moveable-control-box {