Skip to content

armgit / bento

Public Imported from GitHub
0

Bento, the office suite that fits in a file

Gradient fills, alpha-capable color editing, alt-click deep select

nyblnet64bf78f3755d
diff --git a/CLAUDE.md b/CLAUDE.md
index fa1c1758..a035a6a7 100644
--- a/CLAUDE.md
+++ b/CLAUDE.md
@@ -43,6 +43,11 @@ One HTML file = the document + viewer + editor. See `README.md` for the vision.
element ids for it and for cross-state morphs); slide deletion cascades states
and clears inbound links after confirm; `[`/`]` collapse the side panels.
- `src/editor/` — vanilla-TS editor. Moveable + Selecto handle manipulation.
+ Alt/Option-click digs through overlapping elements (capture-phase, beats
+ Moveable's control box). Fill/stroke colors carry alpha (color input + % pair,
+ rgba round-trip); shapes support `fillGradient` (linear, CSS-convention angle,
+ multi-stop) — rendered as per-instance svg `<defs>` gradients (unique ids:
+ url(#…) refs are document-global across canvas/thumbs/present).
## Hard-won details — do not regress
diff --git a/slides/src/editor/canvas.ts b/slides/src/editor/canvas.ts
index e3fb2146..c6bb94ab 100644
--- a/slides/src/editor/canvas.ts
+++ b/slides/src/editor/canvas.ts
@@ -85,6 +85,20 @@ export class SlideCanvas {
this.startPathEdit(ev.detail.id)
}) as EventListener)
+ // Alt/Option-click digs through overlapping elements: first click grabs
+ // the topmost, each further alt-click steps one element deeper (wrapping).
+ // Capture phase so it wins over Selecto AND Moveable's control-box area,
+ // which otherwise swallows clicks over the current selection.
+ document.addEventListener('mousedown', (ev) => {
+ if (!ev.altKey || ev.button !== 0 || this.pathEditor.active) return
+ const r = this.scaleHost.getBoundingClientRect()
+ if (ev.clientX < r.left || ev.clientX > r.right || ev.clientY < r.top || ev.clientY > r.bottom) return
+ if (ev.target instanceof Element && ev.target.closest('.ed-sidebar, .ed-props, .ed-topbar')) return
+ ev.preventDefault()
+ ev.stopPropagation()
+ this.deepSelect((ev.clientX - r.left) / this.scale, (ev.clientY - r.top) / this.scale)
+ }, true)
+
this.stage.addEventListener('dblclick', (ev) => {
const el = (ev.target as HTMLElement).closest<HTMLElement>('.bento-el-text')
if (el) this.startTextEdit(el)
@@ -152,6 +166,26 @@ export class SlideCanvas {
this.wrap.appendChild(bar)
}
+ /** Alt-click: select the element under (px, py), digging one step deeper
+ * below the current selection on each repeat. Coordinates in slide px. */
+ private deepSelect(px: number, py: number) {
+ const stack: string[] = []
+ for (const el of this.store.slide.elements) {
+ if (px < el.x || px > el.x + el.w || py < el.y || py > el.y + el.h) continue
+ const node = this.scaleHost.querySelector<HTMLElement>(`[data-el-id="${CSS.escape(el.id)}"]`)
+ if (!node || node.style.display === 'none') continue // hidden hover set
+ stack.push(el.id)
+ }
+ if (!stack.length) return
+ const topFirst = stack.reverse() // model order is z-order; topmost last
+ let pick = topFirst[0]
+ if (this.store.selection.length === 1) {
+ const i = topFirst.indexOf(this.store.selection[0])
+ if (i >= 0) pick = topFirst[(i + 1) % topFirst.length]
+ }
+ this.store.select([pick])
+ }
+
// --- motion-path editing ----------------------------------------------------
get isPathEditing() {
diff --git a/slides/src/editor/panels.ts b/slides/src/editor/panels.ts
index 94493f46..d64430ce 100644
--- a/slides/src/editor/panels.ts
+++ b/slides/src/editor/panels.ts
@@ -485,9 +485,77 @@ export class PropsPanel {
private buildShapeProps(el: ShapeElement) {
this.section('Fill & stroke')
- this.row('Fill', this.color(el.fill, (v, fin) =>
- this.mutate(el.id, (e) => { (e as ShapeElement).fill = v }, fin)))
- this.row('Stroke', this.color(el.stroke === 'transparent' ? '#1E2A3A' : el.stroke, (v, fin) =>
+ const grad = el.fillGradient
+ this.row('Fill style', this.select(['solid', 'gradient'], grad ? 'gradient' : 'solid', (v) =>
+ this.mutate(el.id, (e) => {
+ const s = e as ShapeElement
+ if (v === 'gradient') {
+ const base = parseColor(s.fill)
+ s.fillGradient ??= {
+ angle: 180,
+ stops: [
+ { at: 0, color: combineColor(base.hex, Math.max(base.a, 0.85)) },
+ { at: 1, color: combineColor(base.hex, 0) },
+ ],
+ }
+ } else {
+ delete s.fillGradient
+ }
+ }, true)))
+
+ if (!grad) {
+ this.row('Fill', this.colorAlpha(el.fill, (v, fin) =>
+ this.mutate(el.id, (e) => { (e as ShapeElement).fill = v }, fin)))
+ } else {
+ this.row('Grad. angle', this.number(grad.angle, 1, (v, fin) =>
+ this.mutate(el.id, (e) => {
+ const g = (e as ShapeElement).fillGradient
+ if (g) g.angle = v
+ }, fin)))
+ grad.stops.forEach((stop, i) => {
+ const wrap = document.createElement('div')
+ wrap.className = 'ed-gradstop'
+ const at = this.number(Math.round(stop.at * 100), 1, (v, fin) =>
+ this.mutate(el.id, (e) => {
+ const g = (e as ShapeElement).fillGradient
+ if (g?.stops[i]) g.stops[i].at = Math.min(Math.max(v / 100, 0), 1)
+ }, fin))
+ at.title = 'Position %'
+ const color = this.colorAlpha(stop.color, (v, fin) =>
+ this.mutate(el.id, (e) => {
+ const g = (e as ShapeElement).fillGradient
+ if (g?.stops[i]) g.stops[i].color = v
+ }, fin))
+ wrap.append(at, color)
+ if (grad.stops.length > 2) {
+ const del = document.createElement('button')
+ del.className = 'ed-btn ed-btn-icon'
+ del.textContent = '✕'
+ del.title = 'Remove stop'
+ del.addEventListener('click', () =>
+ this.mutate(el.id, (e) => {
+ const g = (e as ShapeElement).fillGradient
+ if (g && g.stops.length > 2) g.stops.splice(i, 1)
+ }, true))
+ wrap.appendChild(del)
+ }
+ this.row(`Stop ${i + 1}`, wrap)
+ })
+ const add = document.createElement('button')
+ add.className = 'ed-btn ed-btn-block'
+ add.textContent = '+ Add stop'
+ add.addEventListener('click', () =>
+ this.mutate(el.id, (e) => {
+ const g = (e as ShapeElement).fillGradient
+ if (!g) return
+ const mid = g.stops[Math.floor(g.stops.length / 2)]
+ g.stops.push({ at: 0.5, color: mid?.color ?? '#808080' })
+ g.stops.sort((a, b) => a.at - b.at)
+ }, true))
+ this.host.appendChild(add)
+ }
+
+ this.row('Stroke', this.colorAlpha(el.stroke === 'transparent' ? 'rgba(30, 42, 58, 0)' : el.stroke, (v, fin) =>
this.mutate(el.id, (e) => { (e as ShapeElement).stroke = v }, fin)))
this.row('Stroke width', this.number(el.strokeWidth, 0.5, (v, fin) =>
this.mutate(el.id, (e) => { (e as ShapeElement).strokeWidth = Math.max(v, 0) }, fin)))
@@ -604,12 +672,40 @@ export class PropsPanel {
private color(value: string, onEdit: (v: string, final: boolean) => void): HTMLElement {
const input = document.createElement('input')
input.type = 'color'
- input.value = /^#[0-9a-fA-F]{6}$/.test(value) ? value : '#1E2A3A'
+ input.value = /^#[0-9a-fA-F]{6}$/.test(value) ? value : parseColor(value).hex
input.addEventListener('input', () => onEdit(input.value, false))
input.addEventListener('change', () => onEdit(input.value, true))
return input
}
+ /** Color swatch + opacity %. Native color inputs have no alpha channel, so
+ * the pair round-trips rgba()/#rrggbbaa strings losslessly. */
+ private colorAlpha(value: string, onEdit: (v: string, final: boolean) => void): HTMLElement {
+ const wrap = document.createElement('div')
+ wrap.className = 'ed-coloralpha'
+ const parsed = parseColor(value)
+ const col = document.createElement('input')
+ col.type = 'color'
+ col.value = parsed.hex
+ const alpha = document.createElement('input')
+ alpha.type = 'number'
+ alpha.min = '0'
+ alpha.max = '100'
+ alpha.step = '1'
+ alpha.value = String(Math.round(parsed.a * 100))
+ alpha.title = 'Opacity %'
+ const emit = (final: boolean) => {
+ const raw = parseFloat(alpha.value)
+ const a = Number.isFinite(raw) ? Math.min(Math.max(raw / 100, 0), 1) : 1
+ onEdit(combineColor(col.value, a), final)
+ }
+ col.addEventListener('input', () => emit(false))
+ col.addEventListener('change', () => emit(true))
+ alpha.addEventListener('change', () => emit(true))
+ wrap.append(col, alpha)
+ return wrap
+ }
+
private select(options: string[], value: string, onChange: (v: string) => void): HTMLElement {
const sel = document.createElement('select')
for (const opt of options) {
@@ -632,3 +728,32 @@ export class PropsPanel {
return btn
}
}
+
+/** Any CSS color → {hex, a}. Handles #rgb/#rrggbb/#rrggbbaa/rgb()/rgba()/transparent. */
+export function parseColor(v: string): { hex: string; a: number } {
+ if (!v || v === 'transparent' || v === 'none') return { hex: '#000000', a: 0 }
+ const m = v.match(/rgba?\(([^)]+)\)/)
+ if (m) {
+ const parts = m[1].split(/[\s,/]+/).map((s) => parseFloat(s))
+ const [r, g, b] = parts
+ const a = parts.length > 3 && Number.isFinite(parts[3]) ? parts[3] : 1
+ if ([r, g, b].every((n) => Number.isFinite(n))) {
+ const hex = '#' + [r, g, b].map((n) => Math.round(Math.min(Math.max(n, 0), 255)).toString(16).padStart(2, '0')).join('')
+ return { hex, a: Math.min(Math.max(a, 0), 1) }
+ }
+ }
+ let hex = v.trim()
+ if (/^#[0-9a-fA-F]{3}$/.test(hex)) hex = '#' + [...hex.slice(1)].map((c) => c + c).join('')
+ if (/^#[0-9a-fA-F]{8}$/.test(hex)) return { hex: hex.slice(0, 7), a: parseInt(hex.slice(7), 16) / 255 }
+ if (/^#[0-9a-fA-F]{6}$/.test(hex)) return { hex, a: 1 }
+ return { hex: '#1E2A3A', a: 1 }
+}
+
+/** {hex, a} → the shortest CSS color that keeps the alpha. */
+export function combineColor(hex: string, a: number): string {
+ if (a >= 1) return hex
+ const r = parseInt(hex.slice(1, 3), 16)
+ const g = parseInt(hex.slice(3, 5), 16)
+ const b = parseInt(hex.slice(5, 7), 16)
+ return `rgba(${r}, ${g}, ${b}, ${Math.round(a * 1000) / 1000})`
+}
diff --git a/slides/src/model.ts b/slides/src/model.ts
index 27025cdc..6cc471fa 100644
--- a/slides/src/model.ts
+++ b/slides/src/model.ts
@@ -67,10 +67,20 @@ export interface TextElement extends ElementBase {
export type ShapeKind = 'rect' | 'ellipse' | 'triangle' | 'arrow' | 'line' | 'path'
+/** Linear gradient fill. Colors are any CSS color, including rgba(). */
+export interface GradientFill {
+ /** degrees, CSS convention: 0 = bottom→top, 90 = left→right */
+ angle: number
+ /** ordered stops; `at` is 0..1 along the gradient line */
+ stops: Array<{ at: number; color: string }>
+}
+
export interface ShapeElement extends ElementBase {
type: 'shape'
shape: ShapeKind
fill: string
+ /** when set, wins over `fill` (which is kept as the solid fallback) */
+ fillGradient?: GradientFill
stroke: string
strokeWidth: number
/** corner radius, rect only */
diff --git a/slides/src/render.ts b/slides/src/render.ts
index 4b6e76c6..fefad3a9 100644
--- a/slides/src/render.ts
+++ b/slides/src/render.ts
@@ -68,6 +68,36 @@ export function applyElementFrame(node: HTMLElement, el: SlideElement) {
node.style.opacity = String(el.opacity)
}
+// Gradient ids must be unique per rendered instance: the same element renders
+// on the canvas, in sidebar thumbnails and in the present overlay, and svg
+// url(#…) references resolve document-wide.
+let gradSeq = 0
+
+/** Materialize a GradientFill as a <defs> gradient; returns its url() ref. */
+function gradientRef(svg: SVGSVGElement, g: NonNullable<ShapeElement['fillGradient']>): string {
+ const id = `bento-grad-${gradSeq++}`
+ const defs = document.createElementNS(SVG_NS, 'defs')
+ const lin = document.createElementNS(SVG_NS, 'linearGradient')
+ lin.setAttribute('id', id)
+ // CSS angle convention: 0deg points up, 90deg points right
+ const rad = ((g.angle ?? 180) * Math.PI) / 180
+ const dx = Math.sin(rad) / 2
+ const dy = -Math.cos(rad) / 2
+ lin.setAttribute('x1', String(0.5 - dx))
+ lin.setAttribute('y1', String(0.5 - dy))
+ lin.setAttribute('x2', String(0.5 + dx))
+ lin.setAttribute('y2', String(0.5 + dy))
+ for (const s of g.stops) {
+ const stop = document.createElementNS(SVG_NS, 'stop')
+ stop.setAttribute('offset', String(Math.min(Math.max(s.at, 0), 1)))
+ stop.setAttribute('stop-color', s.color)
+ lin.appendChild(stop)
+ }
+ defs.appendChild(lin)
+ svg.appendChild(defs)
+ return `url(#${id})`
+}
+
export function shapeSvg(el: ShapeElement): SVGSVGElement {
const svg = document.createElementNS(SVG_NS, 'svg')
const { w, h } = el
@@ -134,7 +164,7 @@ export function shapeSvg(el: ShapeElement): SVGSVGElement {
return svg
}
}
- node.setAttribute('fill', el.fill)
+ node.setAttribute('fill', el.fillGradient?.stops.length ? gradientRef(svg, el.fillGradient) : el.fill)
if (el.stroke && el.stroke !== 'transparent' && sw > 0) {
node.setAttribute('stroke', el.stroke)
node.setAttribute('stroke-width', String(sw))
diff --git a/slides/src/styles.css b/slides/src/styles.css
index d9bd7196..9e36ba28 100644
--- a/slides/src/styles.css
+++ b/slides/src/styles.css
@@ -464,6 +464,13 @@ html, body {
border-radius: 999px;
}
+/* color + alpha pair, and gradient stop rows */
+.ed-coloralpha { display: flex; align-items: center; gap: 6px; }
+.ed-coloralpha input[type='number'] { width: 52px; }
+.ed-gradstop { display: flex; align-items: center; gap: 6px; }
+.ed-gradstop > input[type='number'] { width: 44px; }
+.ed-gradstop .ed-btn-icon { padding: 2px 6px; }
+
/* resizable side panels — thin grab strips between panel and canvas */
.ed-resizer {
flex: none;