Bento, the office suite that fits in a file
TypeScript mit-licenseofficeoffice-365office-suiteoffice-toolsoffice365open-sourcepowerpointself-hostedsingle-page-appslidesslideshareslideshowslideshow-galleryslideshow-makerslideshows
Kernel K2: move update, i18n engine and charts into kernel/
nyblnet8bd4daa8979c
diff --git a/kernel/src/app.ts b/kernel/src/app.tsnew file mode 100644index 00000000..251900d3--- /dev/null+++ b/kernel/src/app.ts@@ -0,0 +1,39 @@+// SPDX-License-Identifier: MIT+// Copyright (c) 2026 The Bento/Suite authors+// Per-app identity for the kernel. Every Bento app calls configureApp() once,+// first thing at boot, before any kernel module is used.+//+// Only three values are app-specific across the whole kernel: the id the+// release manifest is signed with, the display name that appears in window+// titles and save dialogs, and where updates are fetched from. Everything+// else in the kernel is genuinely app-agnostic.+//+// Deliberately NOT a general "kernel init" — modules that need no config+// (anim, autosave, charts) take none, so there is no import-order trap and+// no false coupling for apps that skip a module.++export interface AppConfig {+ /** Manifest `app` field this shell will accept — e.g. 'bento-slides'.+ * A manifest signed for another app is rejected even though the signing+ * key is shared platform-wide. */+ appId: string+ /** Human-facing product name: window title suffix, save-picker label. */+ appName: string+ /** Release manifest URL. Dev override: localStorage 'bento-update-url'. */+ manifestUrl: string+}++let config: AppConfig | null = null++/** Called once at boot, before kernel modules are used. */+export function configureApp(cfg: AppConfig): void {+ config = cfg+}++/** The active app config. Throws if the app forgot to configure itself —+ * a loud failure at boot beats silently checking updates against the+ * wrong manifest. */+export function appConfig(): AppConfig {+ if (!config) throw new Error('bento kernel: configureApp() was never called')+ return config+}diff --git a/kernel/src/charts.ts b/kernel/src/charts.tsnew file mode 100644index 00000000..3a1c5194--- /dev/null+++ b/kernel/src/charts.ts@@ -0,0 +1,694 @@+// SPDX-License-Identifier: MIT+// Copyright (c) 2026 The Bento/Suite authors+// charts-lite: Bento's own chart engine (replaces ECharts — see git history).+//+// A `chart` element stores a plain-JSON option in the ECharts option SHAPE —+// the format is unchanged; this module interprets the subset Bento uses:+// bar / line / pie / scatter · category+value axes with nice ticks ·+// legend · axis/item tooltips · inside wheel-zoom + drag-pan · transitions+// between options (same-type data tweens; cross-type staged sweep).+// Unknown option keys are ignored gracefully. Rendering is pure SVG on the+// same substrate as the rest of Bento (anim.ts drives all motion), so the+// whole engine is ~one file and every byte of it is ours (MIT).+//+// Two render paths, same API as always:+// - chartSnapshotSvg(el): static SVG string — editor canvas, thumbs, print.+// - mountChart(el, host, fromOption?): live instance — present mode+// (tooltips, zoom); with fromOption, animates from that option to its own.++import { anim } from './anim.ts'++/** The chart surface the engine actually reads. An app's own chart element+ * type (e.g. slides' ChartElement, which also carries preset/source) satisfies+ * this structurally — the kernel never sees those app-specific fields. */+export interface ChartLike {+ w: number+ h: number+ option: Record<string, unknown>+}++type Opt = Record<string, any>++/** Presets seed new charts and let the panel swap types without data loss. */+export const CHART_PRESETS: Record<string, () => Record<string, unknown>> = {+ bar: () => ({+ tooltip: { trigger: 'axis' },+ legend: { bottom: 0 },+ grid: { left: 56, right: 20, top: 24, bottom: 56 },+ xAxis: { type: 'category', data: ['Q1', 'Q2', 'Q3', 'Q4'] },+ yAxis: { type: 'value' },+ dataZoom: [{ type: 'inside' }],+ series: [+ { type: 'bar', name: 'Series A', data: [12, 20, 15, 28] },+ { type: 'bar', name: 'Series B', data: [8, 14, 19, 21] },+ ],+ }),+ line: () => ({+ tooltip: { trigger: 'axis' },+ legend: { bottom: 0 },+ grid: { left: 56, right: 20, top: 24, bottom: 56 },+ xAxis: { type: 'category', data: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun'] },+ yAxis: { type: 'value' },+ dataZoom: [{ type: 'inside' }],+ series: [{ type: 'line', name: 'Trend', smooth: true, areaStyle: {}, data: [4, 7, 6, 11, 14, 18] }],+ }),+ pie: () => ({+ tooltip: { trigger: 'item' },+ legend: { bottom: 0 },+ series: [{+ type: 'pie', radius: ['38%', '68%'],+ label: { formatter: '{b}: {d}%' },+ data: [+ { name: 'Alpha', value: 42 }, { name: 'Beta', value: 28 },+ { name: 'Gamma', value: 18 }, { name: 'Delta', value: 12 },+ ],+ }],+ }),+ scatter: () => ({+ tooltip: { trigger: 'item' },+ grid: { left: 56, right: 20, top: 24, bottom: 40 },+ xAxis: { type: 'value' },+ yAxis: { type: 'value' },+ dataZoom: [{ type: 'inside' }],+ series: [{+ type: 'scatter', symbolSize: 14,+ data: [[10, 8], [15, 12], [22, 10], [28, 19], [34, 15], [41, 25], [48, 22]],+ }],+ }),+}++const PALETTE = ['#5470c6', '#91cc75', '#fac858', '#ee6666', '#73c0de', '#3ba272', '#fc8452', '#9a60b4']+const SVG_NS = 'http://www.w3.org/2000/svg'++// --- option digestion -------------------------------------------------------++interface YAxisCfg { name?: string; min?: number; max?: number; formatter?: string }++interface Digest {+ w: number; h: number+ font: string+ colors: string[]+ series: Array<Opt>+ categories: string[]+ isPie: boolean+ grid: { x: number; y: number; w: number; h: number }+ legend: null | { color: string }+ axisLabel: string; axisLine: string; splitLine: string+ yAxes: YAxisCfg[] // 1 or 2 value axes; series pick one via yAxisIndex+ tooltipTrigger: 'axis' | 'item' | null+ zoomable: boolean+ labelColor: string+}++function digest(option: Opt, w: number, h: number): Digest {+ const series: Opt[] = Array.isArray(option?.series) ? option.series : option?.series ? [option.series] : []+ const isPie = series.some((s) => s?.type === 'pie')+ const g = option?.grid ?? {}+ const legend = option?.legend ? { color: option.legend?.textStyle?.color ?? '#6B7280' } : null+ const legendH = legend ? 24 : 0+ const yRaw: Opt[] = Array.isArray(option?.yAxis) ? option.yAxis : option?.yAxis ? [option.yAxis] : [{}]+ const yAxes: YAxisCfg[] = yRaw.slice(0, 2).map((a: Opt) => ({+ name: typeof a?.name === 'string' ? a.name : undefined,+ min: typeof a?.min === 'number' ? a.min : undefined,+ max: typeof a?.max === 'number' ? a.max : undefined,+ formatter: typeof a?.axisLabel?.formatter === 'string' ? a.axisLabel.formatter : undefined,+ }))+ if (!yAxes.length) yAxes.push({})+ const twoAxes = !isPie && yAxes.length > 1+ const grid = isPie+ ? { x: 0, y: 0, w, h: h - legendH }+ : {+ x: num(g.left, 48),+ y: num(g.top, 24),+ w: w - num(g.left, 48) - num(g.right, twoAxes ? 56 : 16),+ h: h - num(g.top, 24) - num(g.bottom, 44),+ }+ return {+ w, h,+ font: option?.textStyle?.fontFamily ?? 'sans-serif',+ colors: Array.isArray(option?.color) && option.color.length ? option.color : PALETTE,+ series, isPie,+ categories: (option?.xAxis?.data ?? []).map(String),+ grid,+ legend,+ axisLabel: option?.xAxis?.axisLabel?.color ?? yRaw[0]?.axisLabel?.color ?? '#6B7280',+ axisLine: option?.xAxis?.axisLine?.lineStyle?.color ?? 'rgba(110,120,135,0.45)',+ splitLine: yRaw[0]?.splitLine?.lineStyle?.color ?? 'rgba(110,120,135,0.15)',+ yAxes,+ tooltipTrigger: option?.tooltip ? (option.tooltip.trigger === 'item' ? 'item' : 'axis') : null,+ zoomable: Array.isArray(option?.dataZoom) && option.dataZoom.length > 0,+ labelColor: series.find((s) => s?.type === 'pie')?.label?.color ?? '#6B7280',+ }+}++const num = (v: unknown, d: number): number => (typeof v === 'number' && Number.isFinite(v) ? v : d)++/** Nice tick values for a 0..max (or min..max) range. */+function niceTicks(min: number, max: number, target = 5): number[] {+ if (max === min) max = min + 1+ const span = max - min+ const step0 = Math.pow(10, Math.floor(Math.log10(span / target)))+ const err = span / target / step0+ const step = step0 * (err >= 7.5 ? 10 : err >= 3.5 ? 5 : err >= 1.5 ? 2 : 1)+ const lo = Math.floor(min / step) * step+ const hi = Math.ceil(max / step) * step+ const out: number[] = []+ for (let v = lo; v <= hi + step / 2; v += step) out.push(Math.round(v * 1e6) / 1e6)+ return out+}++const fmt = (v: number): string =>+ Math.abs(v) >= 1000 ? String(Math.round(v * 100) / 100).replace(/\B(?=(\d{3})+(?!\d))/g, ',') : String(Math.round(v * 100) / 100)++/** Nearest "nice" number (1/2/5 × 10^n) to x. */+function niceNum(x: number, round: boolean): number {+ if (!(x > 0)) return 1+ const exp = Math.floor(Math.log10(x))+ const f = x / Math.pow(10, exp)+ const nf = round ? (f < 1.5 ? 1 : f < 3 ? 2 : f < 7 ? 5 : 10) : (f <= 1 ? 1 : f <= 2 ? 2 : f <= 5 ? 5 : 10)+ return nf * Math.pow(10, exp)+}++const linspace = (a: number, b: number, n: number): number[] =>+ Array.from({ length: n }, (_, i) => a + (b - a) * (i / (n - 1)))++/** Exactly `intervals`+1 evenly spaced nice ticks covering lo0..hi0 (lo0 kept as base). */+function fixedTicks(lo0: number, hi0: number, intervals: number): number[] {+ const iv = Math.max(1, intervals)+ let step = niceNum((hi0 - lo0) / iv, false) || 1+ let guard = 0+ while (lo0 + step * iv < hi0 && guard++ < 20) step = niceNum(step * 1.5, false)+ return Array.from({ length: iv + 1 }, (_, i) => Math.round((lo0 + step * i) * 1e6) / 1e6)+}++/** Range + tick labels for one value axis. `forceCount` aligns a 2nd axis to the 1st. */+function axisRange(values: number[], cfg: YAxisCfg, forceCount?: number): { lo: number; hi: number; labels: string[] } {+ let minV = 0, maxV = 0+ for (const v of values) { if (v > maxV) maxV = v; if (v < minV) minV = v }+ const loBase = cfg.min != null ? cfg.min : Math.min(0, minV)+ const hiBase = cfg.max != null ? cfg.max : (maxV || 1)+ const overridden = cfg.min != null || cfg.max != null+ let ticks: number[]+ if (forceCount) ticks = overridden ? linspace(loBase, hiBase, forceCount) : fixedTicks(loBase, hiBase, forceCount - 1)+ else ticks = overridden ? linspace(loBase, hiBase, 6) : niceTicks(loBase, hiBase)+ const apply = (v: number) => (cfg.formatter ? cfg.formatter.replace('{value}', fmt(v)) : fmt(v))+ return { lo: ticks[0], hi: ticks[ticks.length - 1], labels: ticks.map(apply) }+}++// --- svg helpers ------------------------------------------------------------++function elNS<K extends keyof SVGElementTagNameMap>(tag: K, attrs: Record<string, string | number>): SVGElementTagNameMap[K] {+ const n = document.createElementNS(SVG_NS, tag)+ for (const [k, v] of Object.entries(attrs)) n.setAttribute(k, String(v))+ return n+}++function text(x: number, y: number, s: string, fill: string, font: string, size = 12, anchor = 'middle'): SVGTextElement {+ const t = elNS('text', { x, y, fill, 'font-size': size, 'text-anchor': anchor, 'font-family': font })+ t.textContent = s+ return t+}++let gradSeq = 0+function seriesFill(svg: SVGSVGElement, s: Opt, fallback: string): string {+ const c = s?.areaStyle?.color ?? s?.itemStyle?.color+ if (c && typeof c === 'object' && Array.isArray(c.colorStops)) {+ const id = `bc-g${++gradSeq}`+ const lg = elNS('linearGradient', {+ id, x1: num(c.x, 0), y1: num(c.y, 0), x2: num(c.x2, 0), y2: num(c.y2, 1),+ })+ for (const st of c.colorStops) {+ lg.appendChild(elNS('stop', { offset: num(st.offset, 0), 'stop-color': st.color ?? fallback }))+ }+ let defs = svg.querySelector('defs')+ if (!defs) { defs = elNS('defs', {}); svg.prepend(defs) }+ defs.appendChild(lg)+ return `url(#${id})`+ }+ return typeof c === 'string' ? c : fallback+}++// --- core renderer ----------------------------------------------------------++interface View { start: number; end: number } // x-domain window, 0..1++/**+ * Render `option` into a fresh <svg>. `sweep` (0..1) grows series in — used+ * by entrance/cross-type transitions. `view` is the zoom window.+ */+function renderChart(option: Opt, w: number, h: number, sweep = 1, view: View = { start: 0, end: 1 }): SVGSVGElement {+ const d = digest(option, w, h)+ const svg = elNS('svg', { xmlns: SVG_NS, viewBox: `0 0 ${w} ${h}`, width: w, height: h })+ svg.style.cssText = 'width:100%;height:100%;display:block'++ if (d.isPie) renderPie(svg, d, option, sweep)+ else renderCartesian(svg, d, sweep, view)++ if (d.legend && d.series.some((s) => s?.name || s?.type === 'pie')) renderLegend(svg, d)+ return svg+}++function renderLegend(svg: SVGSVGElement, d: Digest) {+ const pie = d.series.find((s) => s?.type === 'pie')+ const items: Array<{ name: string; color: string }> = pie+ ? (pie.data ?? []).map((p: Opt, i: number) => ({ name: String(p?.name ?? i), color: d.colors[i % d.colors.length] }))+ : d.series.map((s, i) => ({ name: String(s?.name ?? `Series ${i + 1}`), color: typeof s?.itemStyle?.color === 'string' ? s.itemStyle.color : typeof s?.lineStyle?.color === 'string' ? s.lineStyle.color : d.colors[i % d.colors.length] }))+ const iw = items.map((it) => 14 + 5 + it.name.length * 6.6 + 16)+ const total = iw.reduce((a, b) => a + b, 0)+ let x = Math.max(8, (d.w - total) / 2)+ const y = d.h - 12+ for (let i = 0; i < items.length; i++) {+ svg.appendChild(elNS('rect', { x, y: y - 9, width: 14, height: 10, rx: 3, fill: items[i].color }))+ svg.appendChild(text(x + 19, y, items[i].name, d.legend!.color, d.font, 12, 'start'))+ x += iw[i]+ }+}++function catWindow(d: Digest, view: View): { cats: string[]; i0: number } {+ const n = Math.max(1, d.categories.length)+ const i0 = Math.min(n - 1, Math.floor(view.start * n))+ const i1 = Math.max(i0 + 1, Math.ceil(view.end * n))+ return { cats: d.categories.slice(i0, i1), i0 }+}++function renderCartesian(svg: SVGSVGElement, d: Digest, sweep: number, view: View) {+ const G = d.grid+ const bars = d.series.filter((s) => s?.type === 'bar')+ const lines = d.series.filter((s) => s?.type === 'line')+ const scatters = d.series.filter((s) => s?.type === 'scatter')++ const { cats, i0 } = catWindow(d, view)+ const nCat = Math.max(1, cats.length)++ // ----- value axes (1 or 2); gridlines align, labels on left & right --------+ const nAxes = d.yAxes.length+ const axisOf = (s: Opt) => Math.min(nAxes - 1, Math.max(0, Math.round(num(s?.yAxisIndex, 0))))+ const perAxis: number[][] = Array.from({ length: nAxes }, () => [])+ for (const s of [...bars, ...lines]) {+ const data: number[] = (s.data ?? []).slice(i0, i0 + nCat).map((v: unknown) => num(v, 0))+ perAxis[axisOf(s)].push(...data)+ }+ let sMinX = Infinity, sMaxX = -Infinity+ for (const s of scatters) {+ for (const p of s.data ?? []) {+ const [px, py] = Array.isArray(p) ? p : [0, 0]+ if (px < sMinX) sMinX = px+ if (px > sMaxX) sMaxX = px+ perAxis[0].push(py)+ }+ }+ const r0 = axisRange(perAxis[0], d.yAxes[0])+ const k = r0.labels.length+ const ranges: Array<ReturnType<typeof axisRange> | null> = [r0, nAxes > 1 ? axisRange(perAxis[1], d.yAxes[1], k) : null]+ const yOf = (v: number, a = 0) => {+ const r = ranges[a] ?? r0+ return G.y + G.h - ((v - r.lo) / (r.hi - r.lo || 1)) * G.h+ }+ // baseline for bars/area = the ZERO line (clamped into the plot), not the+ // axis low — so bars grow from zero and negative values dip below it.+ const baseOf = (a = 0) => Math.max(G.y, Math.min(G.y + G.h, yOf(0, a)))++ // shared gridlines at k evenly spaced rows; labels on the matching side(s)+ for (let j = 0; j < k; j++) {+ const y = G.y + G.h - (j / Math.max(1, k - 1)) * G.h+ svg.appendChild(elNS('line', { x1: G.x, y1: y, x2: G.x + G.w, y2: y, stroke: d.splitLine, 'stroke-width': 1 }))+ svg.appendChild(text(G.x - 8, y + 4, r0.labels[j], d.axisLabel, d.font, 12, 'end'))+ if (ranges[1]) svg.appendChild(text(G.x + G.w + 8, y + 4, ranges[1].labels[j], d.axisLabel, d.font, 12, 'start'))+ }+ if (d.yAxes[0]?.name) svg.appendChild(text(G.x - 8, G.y - 9, d.yAxes[0].name, d.axisLabel, d.font, 11, 'end'))+ if (ranges[1] && d.yAxes[1]?.name) svg.appendChild(text(G.x + G.w + 8, G.y - 9, d.yAxes[1].name, d.axisLabel, d.font, 11, 'start'))++ // x axis line — drawn at the ZERO position (clamped). For all-positive data+ // that's the bottom (as before); when the range crosses zero it sits AT zero,+ // so negative values dip below the axis instead of the axis pinning to the floor.+ const xAxisY = baseOf(0)+ svg.appendChild(elNS('line', { x1: G.x, y1: xAxisY, x2: G.x + G.w, y2: xAxisY, stroke: d.axisLine, 'stroke-width': 1 }))++ if (scatters.length && !d.categories.length) {+ // value x-axis (scatter)+ const xt = niceTicks(sMinX === Infinity ? 0 : Math.min(0, sMinX), sMaxX === -Infinity ? 1 : sMaxX)+ const span = xt[xt.length - 1] - xt[0]+ const vx0 = xt[0] + span * view.start+ const vx1 = xt[0] + span * view.end+ const xOf = (v: number) => G.x + ((v - vx0) / (vx1 - vx0)) * G.w+ for (const tv of xt) {+ if (tv < vx0 || tv > vx1) continue+ svg.appendChild(text(xOf(tv), G.y + G.h + 18, fmt(tv), d.axisLabel, d.font, 12))+ }+ scatters.forEach((s, si) => {+ const color = typeof s?.itemStyle?.color === 'string' ? s.itemStyle.color : d.colors[si % d.colors.length]+ const r = num(s.symbolSize, 10) / 2+ for (const p of s.data ?? []) {+ const [px, py] = Array.isArray(p) ? p : [0, 0]+ const cx = xOf(px)+ if (cx < G.x - r || cx > G.x + G.w + r) continue+ const c = elNS('circle', { cx, cy: yOf(py), r: r * sweep, fill: color, opacity: 0.85 })+ ;(c as any).__tip = { title: '', rows: [{ name: `${fmt(px)}, ${fmt(py)}`, color }] }+ svg.appendChild(c)+ }+ })+ return+ }++ // category x labels (thinned)+ const band = G.w / nCat+ const step = Math.ceil(nCat / Math.max(1, Math.floor(G.w / 56)))+ cats.forEach((c, i) => {+ if (i % step) return+ svg.appendChild(text(G.x + band * (i + 0.5), G.y + G.h + 18, c, d.axisLabel, d.font, 12))+ })++ // bars+ const m = bars.length+ if (m) {+ const groupW = band * 0.62+ const barW = groupW / m+ bars.forEach((s, si) => {+ const ax = axisOf(s)+ const base = baseOf(ax)+ const color = typeof s?.itemStyle?.color === 'string' ? s.itemStyle.color : d.colors[d.series.indexOf(s) % d.colors.length]+ const radius = Array.isArray(s?.itemStyle?.borderRadius) ? num(s.itemStyle.borderRadius[0], 0) : num(s?.itemStyle?.borderRadius, 0)+ const data: number[] = (s.data ?? []).slice(i0, i0 + nCat).map((v: unknown) => num(v, 0))+ data.forEach((v, i) => {+ const x = G.x + band * i + (band - groupW) / 2 + barW * si+ // grow from the zero baseline: positive up, negative down+ const vy = yOf(v, ax)+ const hv = Math.abs(base - vy) * sweep+ const r = elNS('rect', {+ x: x + 1, y: vy <= base ? base - hv : base, width: Math.max(1, barW - 2), height: Math.max(0, hv),+ rx: Math.min(radius, barW / 2), fill: color,+ })+ ;(r as any).__cat = i+ ;(r as any).__tip = { title: cats[i], rows: [{ name: String(s.name ?? ''), value: fmt(v), color }] }+ svg.appendChild(r)+ })+ })+ }++ // lines+ lines.forEach((s) => {+ const si = d.series.indexOf(s)+ const ax = axisOf(s)+ const stroke = typeof s?.lineStyle?.color === 'string' ? s.lineStyle.color : d.colors[si % d.colors.length]+ const width = num(s?.lineStyle?.width, 2)+ const data: number[] = (s.data ?? []).slice(i0, i0 + nCat).map((v: unknown) => num(v, 0))+ const pts = data.map((v, i) => [G.x + band * (i + 0.5), yOf(v, ax)] as [number, number])+ if (pts.length < 2) return+ let path = `M ${pts[0][0]} ${pts[0][1]}`+ if (s.smooth) {+ for (let i = 0; i < pts.length - 1; i++) {+ const p0 = pts[Math.max(0, i - 1)], p1 = pts[i], p2 = pts[i + 1], p3 = pts[Math.min(pts.length - 1, i + 2)]+ const c1x = p1[0] + (p2[0] - p0[0]) / 6, c1y = p1[1] + (p2[1] - p0[1]) / 6+ const c2x = p2[0] - (p3[0] - p1[0]) / 6, c2y = p2[1] - (p3[1] - p1[1]) / 6+ path += ` C ${c1x} ${c1y} ${c2x} ${c2y} ${p2[0]} ${p2[1]}`+ }+ } else {+ for (let i = 1; i < pts.length; i++) path += ` L ${pts[i][0]} ${pts[i][1]}`+ }+ if (s.areaStyle) {+ const fill = seriesFill(svg, s, stroke)+ const area = elNS('path', {+ d: `${path} L ${pts[pts.length - 1][0]} ${baseOf(ax)} L ${pts[0][0]} ${baseOf(ax)} Z`,+ fill, opacity: s.areaStyle.color ? 1 : 0.25,+ })+ svg.appendChild(area)+ }+ const line = elNS('path', { d: path, fill: 'none', stroke, 'stroke-width': width, 'stroke-linecap': 'round' })+ if (sweep < 1) {+ const len = 2000+ line.setAttribute('stroke-dasharray', String(len))+ line.setAttribute('stroke-dashoffset', String(len * (1 - sweep)))+ }+ svg.appendChild(line)+ if (s.symbol !== 'none') {+ pts.forEach(([cx, cy], i) => {+ const c = elNS('circle', { cx, cy, r: 3.5, fill: stroke, opacity: sweep })+ ;(c as any).__cat = i+ ;(c as any).__tip = { title: cats[i], rows: [{ name: String(s.name ?? ''), value: fmt(data[i]), color: stroke }] }+ svg.appendChild(c)+ })+ }+ })++ // invisible per-category hit bands for axis tooltips+ if (d.tooltipTrigger === 'axis') {+ for (let i = 0; i < nCat; i++) {+ const band_ = elNS('rect', { x: G.x + band * i, y: G.y, width: band, height: G.h, fill: 'transparent' })+ ;(band_ as any).__cat = i+ svg.appendChild(band_)+ }+ }+}++function renderPie(svg: SVGSVGElement, d: Digest, _option: Opt, sweep: number) {+ const s = d.series.find((x) => x?.type === 'pie')!+ const data: Array<{ name: string; value: number }> = (s.data ?? []).map((p: Opt, i: number) => ({+ name: String(p?.name ?? i), value: Math.max(0, num(p?.value, 0)),+ }))+ const total = data.reduce((a, b) => a + b.value, 0) || 1+ const cx = d.grid.w / 2+ const cy = d.grid.h / 2+ const R = Math.min(d.grid.w, d.grid.h) / 2+ const [ri, ro] = (Array.isArray(s.radius) ? s.radius : ['0%', s.radius ?? '70%']).map(+ (r: string | number) => (typeof r === 'string' ? (parseFloat(r) / 100) * R : r),+ )+ const border = s?.itemStyle?.borderColor+ const bw = num(s?.itemStyle?.borderWidth, 0)+ const labelFmt: string | null = s?.label === false ? null : (s?.label?.formatter ?? '{b}')++ let a0 = -Math.PI / 2+ const sweepEnd = -Math.PI / 2 + Math.PI * 2 * sweep+ data.forEach((p, i) => {+ const frac = p.value / total+ let a1 = a0 + frac * Math.PI * 2+ const clip0 = Math.min(a0, sweepEnd)+ const clip1 = Math.min(a1, sweepEnd)+ const color = d.colors[i % d.colors.length]+ if (clip1 > clip0 + 0.0001) {+ svg.appendChild(arcPath(cx, cy, ri, ro, clip0, clip1, color, border, bw, {+ title: '', rows: [{ name: p.name, value: `${fmt(p.value)} (${Math.round((frac) * 1000) / 10}%)`, color }],+ }))+ }+ // labels only at full sweep+ if (sweep >= 1 && labelFmt) {+ const mid = (a0 + a1) / 2+ const lx = cx + Math.cos(mid) * (ro + 12)+ const ly = cy + Math.sin(mid) * (ro + 12)+ const right = Math.cos(mid) >= 0+ const label = labelFmt+ .replaceAll('{b}', p.name)+ .replaceAll('{c}', fmt(p.value))+ .replaceAll('{d}', String(Math.round(frac * 1000) / 10))+ svg.appendChild(elNS('line', {+ x1: cx + Math.cos(mid) * ro, y1: cy + Math.sin(mid) * ro, x2: lx, y2: ly,+ stroke: color, 'stroke-width': 1,+ }))+ svg.appendChild(text(lx + (right ? 4 : -4), ly + 4, label, d.labelColor, d.font, 12, right ? 'start' : 'end'))+ }+ a0 = a1+ })+}++function arcPath(+ cx: number, cy: number, ri: number, ro: number, a0: number, a1: number,+ fill: string, border: string | undefined, bw: number, tip: unknown,+): SVGPathElement {+ const large = a1 - a0 > Math.PI ? 1 : 0+ const p = (r: number, a: number) => `${cx + Math.cos(a) * r} ${cy + Math.sin(a) * r}`+ const dstr =+ `M ${p(ro, a0)} A ${ro} ${ro} 0 ${large} 1 ${p(ro, a1)} ` ++ (ri > 0 ? `L ${p(ri, a1)} A ${ri} ${ri} 0 ${large} 0 ${p(ri, a0)} Z` : `L ${cx} ${cy} Z`)+ const path = elNS('path', { d: dstr, fill })+ if (border && bw) { path.setAttribute('stroke', border); path.setAttribute('stroke-width', String(bw)) }+ ;(path as any).__tip = tip+ return path+}++// --- public API -------------------------------------------------------------++const snapshotCache = new Map<string, string>()++/** Static SVG snapshot (string) for canvas/thumbnails/print. */+export function chartSnapshotSvg(el: ChartLike): string {+ const key = `${el.w}x${el.h}:${JSON.stringify(el.option)}`+ const hit = snapshotCache.get(key)+ if (hit) return hit+ let out: string+ try {+ out = new XMLSerializer().serializeToString(renderChart(el.option as Opt, el.w, el.h))+ } catch {+ out = `<svg xmlns="${SVG_NS}"></svg>`+ }+ if (snapshotCache.size > 60) snapshotCache.clear()+ snapshotCache.set(key, out)+ return out+}++/** Numeric-leaf interpolation between two same-shape options. */+function lerpOption(a: any, b: any, t: number): any {+ if (typeof a === 'number' && typeof b === 'number') return a + (b - a) * t+ if (Array.isArray(a) && Array.isArray(b) && a.length === b.length) return b.map((v, i) => lerpOption(a[i], v, t))+ if (a && b && typeof a === 'object' && typeof b === 'object' && !Array.isArray(a) && !Array.isArray(b)) {+ const out: Opt = { ...b }+ for (const k of Object.keys(b)) if (k in a) out[k] = lerpOption(a[k], b[k], t)+ return out+ }+ return t < 1 ? a ?? b : b+}++const sameShape = (a: Opt, b: Opt): boolean => {+ const ta = (a?.series ?? []).map((s: Opt) => s?.type).join()+ const tb = (b?.series ?? []).map((s: Opt) => s?.type).join()+ return ta === tb+}++/**+ * Live chart for present mode. Returns a dispose handle. With `fromOption`,+ * the chart animates from that state to its own: same series types tween+ * their values in place; a type change (bar⇄pie) plays a staged sweep.+ */+export function mountChart(el: ChartLike, host: HTMLElement, fromOption?: Record<string, unknown>): () => void {+ host.innerHTML = ''+ if (getComputedStyle(host).position === 'static') host.style.position = 'relative'+ const w = el.w, h = el.h+ const option = el.option as Opt+ const view: View = { start: 0, end: 1 }+ let disposed = false+ const clock = { t: 0 } // tween target — killed via killTweensOf(clock)++ const draw = (opt: Opt, sweep = 1) => {+ if (disposed) return+ host.querySelector('svg')?.remove()+ const svg = renderChart(opt, w, h, sweep, view)+ host.prepend(svg)+ wireTooltips(svg, opt)+ }++ // tooltip overlay — FIXED to the viewport and parented to <body>, so it is+ // immune to the slide's transform scale and to sibling stacking order+ const tipEl = document.createElement('div')+ tipEl.style.cssText =+ 'position:fixed;pointer-events:none;z-index:10000;display:none;background:rgba(18,26,40,0.92);color:#fff;' ++ 'font-size:12px;line-height:1.5;padding:6px 10px;border-radius:6px;white-space:nowrap;transform:translate(-50%,-110%)'+ document.body.appendChild(tipEl)++ const digestNow = () => digest(option, w, h)++ function wireTooltips(svg: SVGSVGElement, opt: Opt) {+ const d = digest(opt, w, h)+ if (!d.tooltipTrigger) return+ svg.addEventListener('mousemove', (ev) => {+ const target = ev.target as Element & { __tip?: any; __cat?: number }+ if (d.tooltipTrigger === 'axis' && typeof target.__cat === 'number') {+ const { cats, i0 } = catWindow(d, view)+ const i = target.__cat+ const rows = d.series+ .filter((s) => s.type !== 'pie')+ .map((s, si) => ({+ name: String(s.name ?? `#${si + 1}`),+ value: fmt(num((s.data ?? [])[i0 + i], 0)),+ color: typeof s?.itemStyle?.color === 'string' ? s.itemStyle.color : d.colors[si % d.colors.length],+ }))+ showTip(cats[i] ?? '', rows, ev.clientX, ev.clientY)+ } else if (target.__tip) {+ showTip(target.__tip.title, target.__tip.rows, ev.clientX, ev.clientY)+ } else if (d.tooltipTrigger === 'item') {+ tipEl.style.display = 'none'+ }+ })+ svg.addEventListener('mouseleave', () => { tipEl.style.display = 'none' })+ }++ function showTip(title: string, rows: Array<{ name: string; value?: string; color: string }>, x: number, y: number) {+ // in fullscreen, only the fullscreened element's subtree paints — the+ // tip must live inside it (fixed coords stay viewport-correct)+ const tipHost = document.fullscreenElement ?? document.body+ if (tipEl.parentElement !== tipHost) tipHost.appendChild(tipEl)+ tipEl.innerHTML =+ (title ? `<b>${escapeHtml(title)}</b><br>` : '') ++ rows.map((r) =>+ `<span style="display:inline-block;width:8px;height:8px;border-radius:2px;background:${r.color};margin-right:5px"></span>` ++ `${escapeHtml(r.name)}${r.value !== undefined ? `: <b>${escapeHtml(r.value)}</b>` : ''}`,+ ).join('<br>')+ tipEl.style.display = 'block'+ // clamp within the viewport; flip below the cursor near the top edge+ const tw = tipEl.offsetWidth+ const cx = Math.max(tw / 2 + 6, Math.min(window.innerWidth - tw / 2 - 6, x))+ const th = tipEl.offsetHeight+ const above = y - th - 14 >= 4+ tipEl.style.transform = above ? 'translate(-50%,-110%)' : 'translate(-50%,14px)'+ tipEl.style.left = `${cx}px`+ tipEl.style.top = `${y - (above ? 6 : 0)}px`+ }++ // inside dataZoom: wheel to zoom, drag to pan+ if (digestNow().zoomable && !digestNow().isPie) {+ host.addEventListener('wheel', (ev) => {+ ev.preventDefault()+ const span = view.end - view.start+ const rect = host.getBoundingClientRect()+ const fx = Math.min(1, Math.max(0, (ev.clientX - rect.left) / rect.width))+ const factor = ev.deltaY > 0 ? 1.18 : 1 / 1.18+ const newSpan = Math.min(1, Math.max(0.1, span * factor))+ const anchor = view.start + span * fx+ view.start = Math.max(0, anchor - newSpan * fx)+ view.end = Math.min(1, view.start + newSpan)+ view.start = Math.max(0, view.end - newSpan)+ draw(option)+ }, { passive: false })+ let panFrom: { x: number; s: number; e: number } | null = null+ host.addEventListener('mousedown', (ev) => { panFrom = { x: ev.clientX, s: view.start, e: view.end } })+ window.addEventListener('mousemove', (ev) => {+ if (!panFrom || disposed) return+ const rect = host.getBoundingClientRect()+ const dx = (ev.clientX - panFrom.x) / rect.width+ const span = panFrom.e - panFrom.s+ let s = panFrom.s - dx * span+ s = Math.max(0, Math.min(1 - span, s))+ view.start = s+ view.end = s + span+ draw(option)+ })+ window.addEventListener('mouseup', () => { panFrom = null })+ }++ // entrance / transition+ if (fromOption && JSON.stringify(fromOption) !== JSON.stringify(option)) {+ if (sameShape(fromOption as Opt, option)) {+ anim.to(clock, {+ t: 1, duration: 0.65, ease: 'power2.inOut',+ onUpdate: () => draw(lerpOption(fromOption, option, clock.t)),+ } as any)+ } else {+ // staged: hold old frame briefly, then sweep the new type in+ draw(fromOption as Opt)+ anim.to(clock, {+ t: 1, duration: 0.8, delay: 0.05, ease: 'power2.inOut',+ onUpdate: () => {+ const t = clock.t+ if (t < 0.3) {+ const s = host.querySelector('svg')+ if (s) (s as SVGElement).style.opacity = String(1 - t / 0.3)+ } else {+ draw(option, (t - 0.3) / 0.7)+ }+ },+ } as any)+ }+ } else {+ draw(option)+ }++ ;(host as HTMLElement & { __bentoChart?: unknown }).__bentoChart = { option, redraw: () => draw(option), view }++ return () => {+ disposed = true+ anim.killTweensOf(clock)+ tipEl.remove()+ delete (host as HTMLElement & { __bentoChart?: unknown }).__bentoChart+ host.innerHTML = ''+ }+}++const escapeHtml = (s: string): string =>+ s.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>')diff --git a/kernel/src/i18n.ts b/kernel/src/i18n.tsnew file mode 100644index 00000000..96f8fc6d--- /dev/null+++ b/kernel/src/i18n.ts@@ -0,0 +1,87 @@+// SPDX-License-Identifier: MIT+// Copyright (c) 2026 The Bento/Suite authors+// Internationalization, Bento-sized: a ~1KB t() with English-string-as-key+// (gettext philosophy — the source stays readable, fallback is free), catalogs+// compiled into the bundle (self-containment: nothing is fetched), and locale+// resolution that follows the VIEWER (navigator.language, with a per-browser+// override). Language never enters the document format — a deck authored in+// Tokyo opens with French chrome in Paris.+//+// The ENGINE lives here; the CATALOGS are per-app string data and stay in the+// app (slides/src/i18n/*.ts), registered at boot via registerI18n(). Apps+// import their own ./i18n facade, never this module directly — the facade is+// what guarantees catalogs are registered before the first t().+//+// Locale resolution is LAZY on purpose: it depends on the catalog registry+// (navigator.language only wins if we actually have that catalog), so+// resolving at module scope would run against an empty registry and silently+// fall everyone back to English. Registration also clears the memo.++export type Catalog = Record<string, string>+export interface LocaleChoice { code: string; label: string }++let CATALOGS: Record<string, Catalog> = {}+let CHOICES: LocaleChoice[] = [{ code: 'en', label: 'English' }]++/** Register this app's catalogs + picker choices. Call once, at boot. */+export function registerI18n(opts: { catalogs: Record<string, Catalog>; choices: LocaleChoice[] }): void {+ CATALOGS = opts.catalogs+ CHOICES = opts.choices+ current = null // re-resolve: the registry the resolution depends on just changed+}++/** Locales offered in the picker (label in its own language). */+export const localeChoices = (): LocaleChoice[] => CHOICES++/** Accented pseudo-locale (dev builds only): exposes unswept strings and+ * layouts that break under longer text. */+const pseudo = (s: string): string =>+ '⟦' ++ s.replace(/[a-zA-Z]/g, (c) => {+ const map: Record<string, string> = {+ a: 'à', e: 'ē', i: 'ï', o: 'ő', u: 'ū', c: 'ĉ', n: 'ñ', s: 'š', y: 'ý',+ A: 'Å', E: 'Ê', I: 'Ì', O: 'Ø', U: 'Ü', C: 'Ç', N: 'Ñ', S: 'Š', Y: 'Ÿ',+ }+ return map[c] ?? c+ }) ++ '⟧'++function resolve(): string {+ const saved = localStorage.getItem('bento-lang')+ if (saved) return saved+ const nav = navigator.language || 'en'+ if (CATALOGS[nav]) return nav+ const base = nav.split('-')[0]+ if (base === 'zh') return 'zh-Hans'+ return CATALOGS[base] ? base : 'en'+}++let current: string | null = null++function activeLocale(): string {+ if (current === null) current = resolve()+ return current+}++export const locale = (): string => activeLocale()++/** Persist the override and switch. Callers re-render their own UI. */+export function setLocale(code: string): void {+ if (code === 'en') localStorage.removeItem('bento-lang')+ else localStorage.setItem('bento-lang', code)+ current = code+}++/** Translate an English source string, then interpolate {placeholders}. */+export function t(en: string, vars?: Record<string, string | number>): string {+ const cur = activeLocale()+ let out = cur === 'x-pseudo' ? pseudo(en) : (CATALOGS[cur]?.[en] ?? en)+ if (vars) {+ for (const [k, v] of Object.entries(vars)) out = out.replaceAll(`{${k}}`, String(v))+ }+ return out+}++// dev convenience: window.bento.i18n exposes locale switching for testing;+// the pseudo locale is reachable by setLocale('x-pseudo') in any build.+export const i18nApi = { t, locale, setLocale, choices: localeChoices }diff --git a/kernel/src/save.ts b/kernel/src/save.tsindex 86382c58..1d3561f4 100644--- a/kernel/src/save.ts+++ b/kernel/src/save.ts@@ -8,6 +8,7 @@// shell, new document inside. TiddlyWiki pioneered this trick.import type { KernelDoc } from './doc.ts'+import { appConfig } from './app.ts'const DATA_BLOCK_ID = 'bento-doc'// Split so the literal never appears in the bundle (it would terminate the@@ -42,7 +43,7 @@ function serializeBody(shell: Document, body: string, title: string): string {block.textContent = '\n' + body.replace(/</g, '\\u003c') + '\n'const titleEl = clone.querySelector('title')- if (titleEl) titleEl.textContent = title + ' — Bento Slides'+ if (titleEl) titleEl.textContent = title + ' — ' + appConfig().appNameconst html = '<!DOCTYPE html>\n' + clone.documentElement.outerHTML// Belt-and-braces: an unescaped close tag anywhere in generated output would@@ -198,7 +199,7 @@ async function pickHandle(doc: KernelDoc, suffix = ''): Promise<FsFileHandle | ntry {return await (window as any).showSaveFilePicker({suggestedName: suggestedFileName(doc, suffix),- types: [{ description: 'Bento Slides', accept: { 'text/html': ['.html'] } }],+ types: [{ description: appConfig().appName, accept: { 'text/html': ['.html'] } }],})} catch (err: any) {if (err?.name === 'AbortError') return nulldiff --git a/kernel/src/update.ts b/kernel/src/update.tsnew file mode 100644index 00000000..3bd5ed47--- /dev/null+++ b/kernel/src/update.ts@@ -0,0 +1,207 @@+// SPDX-License-Identifier: MIT+// Copyright (c) 2026 The Bento/Suite authors+// Self-update: a shipped Bento file asks the release origin whether a newer+// app shell exists — automatically at launch (per-browser opt-out in the+// About dialog) or on demand — and rebuilds itself as "same document, newer+// app": the data block re-spliced into the fetched shell via the exact+// machinery every save already uses.+//+// Trust model (see docs/architecture.md):+// - The manifest is SIGNED (ECDSA P-256 / SHA-256) with an offline key; the+// matching public key is embedded below in every shipped shell. A+// compromised host or repo cannot forge a release without that key.+// - The manifest pins the new shell's sha256; the download is hashed and+// compared before anything is spliced.+// - Only strictly NEWER versions are ever offered (no downgrade replay).+// - A check is a bare GET: no identifiers, no telemetry, nothing about the+// user or the document. The launch check can be disabled (autoCheckEnabled).+//+// The result is always a NEW downloaded file — the update flow never touches+// the file on disk, so the original is its own rollback.++import type { KernelDoc } from './doc.ts'+import { appConfig } from './app.ts'+import {+ serializeDocInto, serializeAuto, suggestedFileName, downloadFile,+ hasFileHandle, writeUpdatedFile, writeUpdatedFileAs,+} from './save.ts'++declare const __APP_VERSION__: string++/** Version of the running app shell (baked in at build from package.json). */+export const APP_VERSION: string = typeof __APP_VERSION__ !== 'undefined' ? __APP_VERSION__ : '0.0.0'++/** Per-browser preference: check for updates automatically at launch. */+/**+ * Offline mode: a hard, viewer-side switch that blocks EVERY network touch —+ * update checks and online collaboration alike. Same-machine tab sync+ * (BroadcastChannel) is not networking and stays on. The guarantee this+ * buys: with the switch on, nothing about you or a document ever leaves+ * this computer.+ */+export const offlineEnabled = (): boolean => {+ try {+ return localStorage.getItem('bento-offline') === 'on'+ } catch {+ return false+ }+}+export const setOffline = (on: boolean): void => {+ try {+ localStorage.setItem('bento-offline', on ? 'on' : 'off')+ } catch {+ /* storage unavailable */+ }+}++export const autoCheckEnabled = (): boolean => localStorage.getItem('bento-auto-check') !== 'off'+export const setAutoCheck = (on: boolean): void => {+ if (on) localStorage.removeItem('bento-auto-check')+ else localStorage.setItem('bento-auto-check', 'off')+}++/** Where shipped files look for releases (per-app, from configureApp).+ * Dev override: localStorage 'bento-update-url'. */+export const updateManifestUrl = (): string => appConfig().manifestUrl++// Release signing PUBLIC key. The private half lives offline with the+// maintainer (scripts/keygen.mjs → ~/.bento/release-key.json) and signs+// manifests via scripts/sign-release.mjs. Rotating this key orphans every+// previously shipped file — guard the private key instead.+const PUBLIC_KEY_JWK = {+ kty: 'EC',+ crv: 'P-256',+ x: 'GMHSKwWcAoJVq-Dz1ZxWZM6TXATWIKbaQBpjoTystH8',+ y: 'flFNzbdXCmJN8RQYCeG71rBZnnbN-MCEnp1EbCLFrj0',+} as const++export interface ReleaseInfo {+ app: string+ version: string+ /** hex sha256 of the release shell's bytes */+ sha256: string+ /** absolute URL of the release shell */+ url: string+ notes?: string+ at?: string+}++export type UpdateCheck =+ | { status: 'current'; version: string }+ | { status: 'update'; release: ReleaseInfo }+ | { status: 'error'; message: string }++const b64ToBytes = (b64: string) => Uint8Array.from(atob(b64), (c) => c.charCodeAt(0))+const hex = (buf: ArrayBuffer) =>+ [...new Uint8Array(buf)].map((b) => b.toString(16).padStart(2, '0')).join('')++/** Dotted-numeric compare: 0.2.0 > 0.1.9 > 0.1 — positive when a > b. */+export function compareVersions(a: string, b: string): number {+ const pa = a.split('.').map(Number)+ const pb = b.split('.').map(Number)+ for (let i = 0; i < Math.max(pa.length, pb.length); i++) {+ const d = (pa[i] || 0) - (pb[i] || 0)+ if (d) return d+ }+ return 0+}++/**+ * Verify the manifest signature and return its payload. The signature covers+ * the payload's exact string bytes — no JSON canonicalization involved.+ */+async function verifyManifest(raw: string): Promise<ReleaseInfo> {+ let payload: string, sig: string+ try {+ ;({ payload, sig } = JSON.parse(raw))+ } catch {+ throw new Error('the release manifest is not valid JSON')+ }+ if (typeof payload !== 'string' || typeof sig !== 'string')+ throw new Error('the release manifest is malformed')++ const key = await crypto.subtle.importKey(+ 'jwk', PUBLIC_KEY_JWK as JsonWebKey,+ { name: 'ECDSA', namedCurve: 'P-256' }, false, ['verify'],+ )+ const ok = await crypto.subtle.verify(+ { name: 'ECDSA', hash: 'SHA-256' }, key,+ b64ToBytes(sig), new TextEncoder().encode(payload),+ )+ if (!ok) throw new Error('the release signature is INVALID — refusing this update')++ const info = JSON.parse(payload)+ if (+ info?.app !== appConfig().appId ||+ typeof info.version !== 'string' ||+ !/^[0-9a-f]{64}$/i.test(info.sha256 ?? '') ||+ typeof info.url !== 'string'+ )+ throw new Error('the release manifest payload is malformed')+ return info as ReleaseInfo+}++/** Ask the release origin for the latest version. */+export async function checkForUpdates(manifestUrl?: string): Promise<UpdateCheck> {+ const url = manifestUrl ?? localStorage.getItem('bento-update-url') ?? updateManifestUrl()+ try {+ const res = await fetch(url, { cache: 'no-store' })+ if (!res.ok) throw new Error(`release server answered ${res.status}`)+ const release = await verifyManifest(await res.text())+ if (compareVersions(release.version, APP_VERSION) <= 0)+ return { status: 'current', version: APP_VERSION }+ return { status: 'update', release }+ } catch (err: any) {+ return { status: 'error', message: err?.message ?? String(err) }+ }+}++/**+ * Fetch the release shell, verify its hash against the signed manifest, and+ * return the full updated .bento.html: this document inside the new app.+ */+export async function buildUpdatedFile(release: ReleaseInfo, doc: KernelDoc): Promise<string> {+ const res = await fetch(release.url, { cache: 'no-store' })+ if (!res.ok) throw new Error(`downloading the update failed (${res.status})`)+ const bytes = await res.arrayBuffer()+ const digest = hex(await crypto.subtle.digest('SHA-256', bytes))+ if (digest !== release.sha256.toLowerCase())+ throw new Error('the downloaded update failed its integrity check — refusing it')++ const shell = new DOMParser().parseFromString(new TextDecoder().decode(bytes), 'text/html')+ if (!shell.getElementById('bento-doc'))+ throw new Error('the downloaded update is not a Bento shell')+ return serializeDocInto(shell, doc)+}++/** Build the updated file and hand it to the user as a fresh download. */+export async function applyUpdate(release: ReleaseInfo, doc: KernelDoc): Promise<void> {+ downloadFile(await buildUpdatedFile(release, doc), suggestedFileName(doc))+}++/** Can we rewrite the open file directly (a FS Access handle is held)? */+export const canUpdateInPlace = hasFileHandle++/**+ * Update the file on disk, then a reload boots the new app with this document.+ *+ * With a held handle: download a backup of the current version first, then+ * overwrite the file in place silently.+ *+ * Without a handle (e.g. the file was double-clicked open — the browser grants+ * no handle on open): use a save picker AND KEEP the resulting handle, so this+ * update and every later one can rewrite the file in place. The picker can't+ * default to the open file's location without a handle, so the caller must tell+ * the user to overwrite the file they have open; once they do, it is a one-time+ * grant. Returns false if the picker was cancelled.+ */+export async function applyUpdateInPlace(release: ReleaseInfo, doc: KernelDoc): Promise<boolean> {+ const html = await buildUpdatedFile(release, doc)+ if (hasFileHandle()) {+ const base = suggestedFileName(doc).replace(/\.bento\.html$/, '')+ downloadFile(await serializeAuto(doc), `${base}.v${APP_VERSION}-backup.bento.html`)+ await writeUpdatedFile(html)+ return true+ }+ return writeUpdatedFileAs(html, doc, { keepHandle: true })+}diff --git a/slides/src/charts.ts b/slides/src/charts.tsindex ff5de877..cbae9cb9 100644--- a/slides/src/charts.ts+++ b/slides/src/charts.ts@@ -1,686 +1,6 @@// SPDX-License-Identifier: MIT// Copyright (c) 2026 The Bento/Suite authors-// charts-lite: Bento's own chart engine (replaces ECharts — see git history).-//-// A `chart` element stores a plain-JSON option in the ECharts option SHAPE —-// the format is unchanged; this module interprets the subset Bento uses:-// bar / line / pie / scatter · category+value axes with nice ticks ·-// legend · axis/item tooltips · inside wheel-zoom + drag-pan · transitions-// between options (same-type data tweens; cross-type staged sweep).-// Unknown option keys are ignored gracefully. Rendering is pure SVG on the-// same substrate as the rest of Bento (anim.ts drives all motion), so the-// whole engine is ~one file and every byte of it is ours (MIT).-//-// Two render paths, same API as always:-// - chartSnapshotSvg(el): static SVG string — editor canvas, thumbs, print.-// - mountChart(el, host, fromOption?): live instance — present mode-// (tooltips, zoom); with fromOption, animates from that option to its own.--import { anim } from './anim'-import type { ChartElement } from './model'--type Opt = Record<string, any>--/** Presets seed new charts and let the panel swap types without data loss. */-export const CHART_PRESETS: Record<string, () => Record<string, unknown>> = {- bar: () => ({- tooltip: { trigger: 'axis' },- legend: { bottom: 0 },- grid: { left: 56, right: 20, top: 24, bottom: 56 },- xAxis: { type: 'category', data: ['Q1', 'Q2', 'Q3', 'Q4'] },- yAxis: { type: 'value' },- dataZoom: [{ type: 'inside' }],- series: [- { type: 'bar', name: 'Series A', data: [12, 20, 15, 28] },- { type: 'bar', name: 'Series B', data: [8, 14, 19, 21] },- ],- }),- line: () => ({- tooltip: { trigger: 'axis' },- legend: { bottom: 0 },- grid: { left: 56, right: 20, top: 24, bottom: 56 },- xAxis: { type: 'category', data: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun'] },- yAxis: { type: 'value' },- dataZoom: [{ type: 'inside' }],- series: [{ type: 'line', name: 'Trend', smooth: true, areaStyle: {}, data: [4, 7, 6, 11, 14, 18] }],- }),- pie: () => ({- tooltip: { trigger: 'item' },- legend: { bottom: 0 },- series: [{- type: 'pie', radius: ['38%', '68%'],- label: { formatter: '{b}: {d}%' },- data: [- { name: 'Alpha', value: 42 }, { name: 'Beta', value: 28 },- { name: 'Gamma', value: 18 }, { name: 'Delta', value: 12 },- ],- }],- }),- scatter: () => ({- tooltip: { trigger: 'item' },- grid: { left: 56, right: 20, top: 24, bottom: 40 },- xAxis: { type: 'value' },- yAxis: { type: 'value' },- dataZoom: [{ type: 'inside' }],- series: [{- type: 'scatter', symbolSize: 14,- data: [[10, 8], [15, 12], [22, 10], [28, 19], [34, 15], [41, 25], [48, 22]],- }],- }),-}--const PALETTE = ['#5470c6', '#91cc75', '#fac858', '#ee6666', '#73c0de', '#3ba272', '#fc8452', '#9a60b4']-const SVG_NS = 'http://www.w3.org/2000/svg'--// --- option digestion ---------------------------------------------------------interface YAxisCfg { name?: string; min?: number; max?: number; formatter?: string }--interface Digest {- w: number; h: number- font: string- colors: string[]- series: Array<Opt>- categories: string[]- isPie: boolean- grid: { x: number; y: number; w: number; h: number }- legend: null | { color: string }- axisLabel: string; axisLine: string; splitLine: string- yAxes: YAxisCfg[] // 1 or 2 value axes; series pick one via yAxisIndex- tooltipTrigger: 'axis' | 'item' | null- zoomable: boolean- labelColor: string-}--function digest(option: Opt, w: number, h: number): Digest {- const series: Opt[] = Array.isArray(option?.series) ? option.series : option?.series ? [option.series] : []- const isPie = series.some((s) => s?.type === 'pie')- const g = option?.grid ?? {}- const legend = option?.legend ? { color: option.legend?.textStyle?.color ?? '#6B7280' } : null- const legendH = legend ? 24 : 0- const yRaw: Opt[] = Array.isArray(option?.yAxis) ? option.yAxis : option?.yAxis ? [option.yAxis] : [{}]- const yAxes: YAxisCfg[] = yRaw.slice(0, 2).map((a: Opt) => ({- name: typeof a?.name === 'string' ? a.name : undefined,- min: typeof a?.min === 'number' ? a.min : undefined,- max: typeof a?.max === 'number' ? a.max : undefined,- formatter: typeof a?.axisLabel?.formatter === 'string' ? a.axisLabel.formatter : undefined,- }))- if (!yAxes.length) yAxes.push({})- const twoAxes = !isPie && yAxes.length > 1- const grid = isPie- ? { x: 0, y: 0, w, h: h - legendH }- : {- x: num(g.left, 48),- y: num(g.top, 24),- w: w - num(g.left, 48) - num(g.right, twoAxes ? 56 : 16),- h: h - num(g.top, 24) - num(g.bottom, 44),- }- return {- w, h,- font: option?.textStyle?.fontFamily ?? 'sans-serif',- colors: Array.isArray(option?.color) && option.color.length ? option.color : PALETTE,- series, isPie,- categories: (option?.xAxis?.data ?? []).map(String),- grid,- legend,- axisLabel: option?.xAxis?.axisLabel?.color ?? yRaw[0]?.axisLabel?.color ?? '#6B7280',- axisLine: option?.xAxis?.axisLine?.lineStyle?.color ?? 'rgba(110,120,135,0.45)',- splitLine: yRaw[0]?.splitLine?.lineStyle?.color ?? 'rgba(110,120,135,0.15)',- yAxes,- tooltipTrigger: option?.tooltip ? (option.tooltip.trigger === 'item' ? 'item' : 'axis') : null,- zoomable: Array.isArray(option?.dataZoom) && option.dataZoom.length > 0,- labelColor: series.find((s) => s?.type === 'pie')?.label?.color ?? '#6B7280',- }-}--const num = (v: unknown, d: number): number => (typeof v === 'number' && Number.isFinite(v) ? v : d)--/** Nice tick values for a 0..max (or min..max) range. */-function niceTicks(min: number, max: number, target = 5): number[] {- if (max === min) max = min + 1- const span = max - min- const step0 = Math.pow(10, Math.floor(Math.log10(span / target)))- const err = span / target / step0- const step = step0 * (err >= 7.5 ? 10 : err >= 3.5 ? 5 : err >= 1.5 ? 2 : 1)- const lo = Math.floor(min / step) * step- const hi = Math.ceil(max / step) * step- const out: number[] = []- for (let v = lo; v <= hi + step / 2; v += step) out.push(Math.round(v * 1e6) / 1e6)- return out-}--const fmt = (v: number): string =>- Math.abs(v) >= 1000 ? String(Math.round(v * 100) / 100).replace(/\B(?=(\d{3})+(?!\d))/g, ',') : String(Math.round(v * 100) / 100)--/** Nearest "nice" number (1/2/5 × 10^n) to x. */-function niceNum(x: number, round: boolean): number {- if (!(x > 0)) return 1- const exp = Math.floor(Math.log10(x))- const f = x / Math.pow(10, exp)- const nf = round ? (f < 1.5 ? 1 : f < 3 ? 2 : f < 7 ? 5 : 10) : (f <= 1 ? 1 : f <= 2 ? 2 : f <= 5 ? 5 : 10)- return nf * Math.pow(10, exp)-}--const linspace = (a: number, b: number, n: number): number[] =>- Array.from({ length: n }, (_, i) => a + (b - a) * (i / (n - 1)))--/** Exactly `intervals`+1 evenly spaced nice ticks covering lo0..hi0 (lo0 kept as base). */-function fixedTicks(lo0: number, hi0: number, intervals: number): number[] {- const iv = Math.max(1, intervals)- let step = niceNum((hi0 - lo0) / iv, false) || 1- let guard = 0- while (lo0 + step * iv < hi0 && guard++ < 20) step = niceNum(step * 1.5, false)- return Array.from({ length: iv + 1 }, (_, i) => Math.round((lo0 + step * i) * 1e6) / 1e6)-}--/** Range + tick labels for one value axis. `forceCount` aligns a 2nd axis to the 1st. */-function axisRange(values: number[], cfg: YAxisCfg, forceCount?: number): { lo: number; hi: number; labels: string[] } {- let minV = 0, maxV = 0- for (const v of values) { if (v > maxV) maxV = v; if (v < minV) minV = v }- const loBase = cfg.min != null ? cfg.min : Math.min(0, minV)- const hiBase = cfg.max != null ? cfg.max : (maxV || 1)- const overridden = cfg.min != null || cfg.max != null- let ticks: number[]- if (forceCount) ticks = overridden ? linspace(loBase, hiBase, forceCount) : fixedTicks(loBase, hiBase, forceCount - 1)- else ticks = overridden ? linspace(loBase, hiBase, 6) : niceTicks(loBase, hiBase)- const apply = (v: number) => (cfg.formatter ? cfg.formatter.replace('{value}', fmt(v)) : fmt(v))- return { lo: ticks[0], hi: ticks[ticks.length - 1], labels: ticks.map(apply) }-}--// --- svg helpers --------------------------------------------------------------function elNS<K extends keyof SVGElementTagNameMap>(tag: K, attrs: Record<string, string | number>): SVGElementTagNameMap[K] {- const n = document.createElementNS(SVG_NS, tag)- for (const [k, v] of Object.entries(attrs)) n.setAttribute(k, String(v))- return n-}--function text(x: number, y: number, s: string, fill: string, font: string, size = 12, anchor = 'middle'): SVGTextElement {- const t = elNS('text', { x, y, fill, 'font-size': size, 'text-anchor': anchor, 'font-family': font })- t.textContent = s- return t-}--let gradSeq = 0-function seriesFill(svg: SVGSVGElement, s: Opt, fallback: string): string {- const c = s?.areaStyle?.color ?? s?.itemStyle?.color- if (c && typeof c === 'object' && Array.isArray(c.colorStops)) {- const id = `bc-g${++gradSeq}`- const lg = elNS('linearGradient', {- id, x1: num(c.x, 0), y1: num(c.y, 0), x2: num(c.x2, 0), y2: num(c.y2, 1),- })- for (const st of c.colorStops) {- lg.appendChild(elNS('stop', { offset: num(st.offset, 0), 'stop-color': st.color ?? fallback }))- }- let defs = svg.querySelector('defs')- if (!defs) { defs = elNS('defs', {}); svg.prepend(defs) }- defs.appendChild(lg)- return `url(#${id})`- }- return typeof c === 'string' ? c : fallback-}--// --- core renderer ------------------------------------------------------------interface View { start: number; end: number } // x-domain window, 0..1--/**- * Render `option` into a fresh <svg>. `sweep` (0..1) grows series in — used- * by entrance/cross-type transitions. `view` is the zoom window.- */-function renderChart(option: Opt, w: number, h: number, sweep = 1, view: View = { start: 0, end: 1 }): SVGSVGElement {- const d = digest(option, w, h)- const svg = elNS('svg', { xmlns: SVG_NS, viewBox: `0 0 ${w} ${h}`, width: w, height: h })- svg.style.cssText = 'width:100%;height:100%;display:block'-- if (d.isPie) renderPie(svg, d, option, sweep)- else renderCartesian(svg, d, sweep, view)-- if (d.legend && d.series.some((s) => s?.name || s?.type === 'pie')) renderLegend(svg, d)- return svg-}--function renderLegend(svg: SVGSVGElement, d: Digest) {- const pie = d.series.find((s) => s?.type === 'pie')- const items: Array<{ name: string; color: string }> = pie- ? (pie.data ?? []).map((p: Opt, i: number) => ({ name: String(p?.name ?? i), color: d.colors[i % d.colors.length] }))- : d.series.map((s, i) => ({ name: String(s?.name ?? `Series ${i + 1}`), color: typeof s?.itemStyle?.color === 'string' ? s.itemStyle.color : typeof s?.lineStyle?.color === 'string' ? s.lineStyle.color : d.colors[i % d.colors.length] }))- const iw = items.map((it) => 14 + 5 + it.name.length * 6.6 + 16)- const total = iw.reduce((a, b) => a + b, 0)- let x = Math.max(8, (d.w - total) / 2)- const y = d.h - 12- for (let i = 0; i < items.length; i++) {- svg.appendChild(elNS('rect', { x, y: y - 9, width: 14, height: 10, rx: 3, fill: items[i].color }))- svg.appendChild(text(x + 19, y, items[i].name, d.legend!.color, d.font, 12, 'start'))- x += iw[i]- }-}--function catWindow(d: Digest, view: View): { cats: string[]; i0: number } {- const n = Math.max(1, d.categories.length)- const i0 = Math.min(n - 1, Math.floor(view.start * n))- const i1 = Math.max(i0 + 1, Math.ceil(view.end * n))- return { cats: d.categories.slice(i0, i1), i0 }-}--function renderCartesian(svg: SVGSVGElement, d: Digest, sweep: number, view: View) {- const G = d.grid- const bars = d.series.filter((s) => s?.type === 'bar')- const lines = d.series.filter((s) => s?.type === 'line')- const scatters = d.series.filter((s) => s?.type === 'scatter')-- const { cats, i0 } = catWindow(d, view)- const nCat = Math.max(1, cats.length)-- // ----- value axes (1 or 2); gridlines align, labels on left & right --------- const nAxes = d.yAxes.length- const axisOf = (s: Opt) => Math.min(nAxes - 1, Math.max(0, Math.round(num(s?.yAxisIndex, 0))))- const perAxis: number[][] = Array.from({ length: nAxes }, () => [])- for (const s of [...bars, ...lines]) {- const data: number[] = (s.data ?? []).slice(i0, i0 + nCat).map((v: unknown) => num(v, 0))- perAxis[axisOf(s)].push(...data)- }- let sMinX = Infinity, sMaxX = -Infinity- for (const s of scatters) {- for (const p of s.data ?? []) {- const [px, py] = Array.isArray(p) ? p : [0, 0]- if (px < sMinX) sMinX = px- if (px > sMaxX) sMaxX = px- perAxis[0].push(py)- }- }- const r0 = axisRange(perAxis[0], d.yAxes[0])- const k = r0.labels.length- const ranges: Array<ReturnType<typeof axisRange> | null> = [r0, nAxes > 1 ? axisRange(perAxis[1], d.yAxes[1], k) : null]- const yOf = (v: number, a = 0) => {- const r = ranges[a] ?? r0- return G.y + G.h - ((v - r.lo) / (r.hi - r.lo || 1)) * G.h- }- // baseline for bars/area = the ZERO line (clamped into the plot), not the- // axis low — so bars grow from zero and negative values dip below it.- const baseOf = (a = 0) => Math.max(G.y, Math.min(G.y + G.h, yOf(0, a)))-- // shared gridlines at k evenly spaced rows; labels on the matching side(s)- for (let j = 0; j < k; j++) {- const y = G.y + G.h - (j / Math.max(1, k - 1)) * G.h- svg.appendChild(elNS('line', { x1: G.x, y1: y, x2: G.x + G.w, y2: y, stroke: d.splitLine, 'stroke-width': 1 }))- svg.appendChild(text(G.x - 8, y + 4, r0.labels[j], d.axisLabel, d.font, 12, 'end'))- if (ranges[1]) svg.appendChild(text(G.x + G.w + 8, y + 4, ranges[1].labels[j], d.axisLabel, d.font, 12, 'start'))- }- if (d.yAxes[0]?.name) svg.appendChild(text(G.x - 8, G.y - 9, d.yAxes[0].name, d.axisLabel, d.font, 11, 'end'))- if (ranges[1] && d.yAxes[1]?.name) svg.appendChild(text(G.x + G.w + 8, G.y - 9, d.yAxes[1].name, d.axisLabel, d.font, 11, 'start'))-- // x axis line — drawn at the ZERO position (clamped). For all-positive data- // that's the bottom (as before); when the range crosses zero it sits AT zero,- // so negative values dip below the axis instead of the axis pinning to the floor.- const xAxisY = baseOf(0)- svg.appendChild(elNS('line', { x1: G.x, y1: xAxisY, x2: G.x + G.w, y2: xAxisY, stroke: d.axisLine, 'stroke-width': 1 }))-- if (scatters.length && !d.categories.length) {- // value x-axis (scatter)- const xt = niceTicks(sMinX === Infinity ? 0 : Math.min(0, sMinX), sMaxX === -Infinity ? 1 : sMaxX)- const span = xt[xt.length - 1] - xt[0]- const vx0 = xt[0] + span * view.start- const vx1 = xt[0] + span * view.end- const xOf = (v: number) => G.x + ((v - vx0) / (vx1 - vx0)) * G.w- for (const tv of xt) {- if (tv < vx0 || tv > vx1) continue- svg.appendChild(text(xOf(tv), G.y + G.h + 18, fmt(tv), d.axisLabel, d.font, 12))- }- scatters.forEach((s, si) => {- const color = typeof s?.itemStyle?.color === 'string' ? s.itemStyle.color : d.colors[si % d.colors.length]- const r = num(s.symbolSize, 10) / 2- for (const p of s.data ?? []) {- const [px, py] = Array.isArray(p) ? p : [0, 0]- const cx = xOf(px)- if (cx < G.x - r || cx > G.x + G.w + r) continue- const c = elNS('circle', { cx, cy: yOf(py), r: r * sweep, fill: color, opacity: 0.85 })- ;(c as any).__tip = { title: '', rows: [{ name: `${fmt(px)}, ${fmt(py)}`, color }] }- svg.appendChild(c)- }- })- return- }-- // category x labels (thinned)- const band = G.w / nCat- const step = Math.ceil(nCat / Math.max(1, Math.floor(G.w / 56)))- cats.forEach((c, i) => {- if (i % step) return- svg.appendChild(text(G.x + band * (i + 0.5), G.y + G.h + 18, c, d.axisLabel, d.font, 12))- })-- // bars- const m = bars.length- if (m) {- const groupW = band * 0.62- const barW = groupW / m- bars.forEach((s, si) => {- const ax = axisOf(s)- const base = baseOf(ax)- const color = typeof s?.itemStyle?.color === 'string' ? s.itemStyle.color : d.colors[d.series.indexOf(s) % d.colors.length]- const radius = Array.isArray(s?.itemStyle?.borderRadius) ? num(s.itemStyle.borderRadius[0], 0) : num(s?.itemStyle?.borderRadius, 0)- const data: number[] = (s.data ?? []).slice(i0, i0 + nCat).map((v: unknown) => num(v, 0))- data.forEach((v, i) => {- const x = G.x + band * i + (band - groupW) / 2 + barW * si- // grow from the zero baseline: positive up, negative down- const vy = yOf(v, ax)- const hv = Math.abs(base - vy) * sweep- const r = elNS('rect', {- x: x + 1, y: vy <= base ? base - hv : base, width: Math.max(1, barW - 2), height: Math.max(0, hv),- rx: Math.min(radius, barW / 2), fill: color,- })- ;(r as any).__cat = i- ;(r as any).__tip = { title: cats[i], rows: [{ name: String(s.name ?? ''), value: fmt(v), color }] }- svg.appendChild(r)- })- })- }-- // lines- lines.forEach((s) => {- const si = d.series.indexOf(s)- const ax = axisOf(s)- const stroke = typeof s?.lineStyle?.color === 'string' ? s.lineStyle.color : d.colors[si % d.colors.length]- const width = num(s?.lineStyle?.width, 2)- const data: number[] = (s.data ?? []).slice(i0, i0 + nCat).map((v: unknown) => num(v, 0))- const pts = data.map((v, i) => [G.x + band * (i + 0.5), yOf(v, ax)] as [number, number])- if (pts.length < 2) return- let path = `M ${pts[0][0]} ${pts[0][1]}`- if (s.smooth) {- for (let i = 0; i < pts.length - 1; i++) {- const p0 = pts[Math.max(0, i - 1)], p1 = pts[i], p2 = pts[i + 1], p3 = pts[Math.min(pts.length - 1, i + 2)]- const c1x = p1[0] + (p2[0] - p0[0]) / 6, c1y = p1[1] + (p2[1] - p0[1]) / 6- const c2x = p2[0] - (p3[0] - p1[0]) / 6, c2y = p2[1] - (p3[1] - p1[1]) / 6- path += ` C ${c1x} ${c1y} ${c2x} ${c2y} ${p2[0]} ${p2[1]}`- }- } else {- for (let i = 1; i < pts.length; i++) path += ` L ${pts[i][0]} ${pts[i][1]}`- }- if (s.areaStyle) {- const fill = seriesFill(svg, s, stroke)- const area = elNS('path', {- d: `${path} L ${pts[pts.length - 1][0]} ${baseOf(ax)} L ${pts[0][0]} ${baseOf(ax)} Z`,- fill, opacity: s.areaStyle.color ? 1 : 0.25,- })- svg.appendChild(area)- }- const line = elNS('path', { d: path, fill: 'none', stroke, 'stroke-width': width, 'stroke-linecap': 'round' })- if (sweep < 1) {- const len = 2000- line.setAttribute('stroke-dasharray', String(len))- line.setAttribute('stroke-dashoffset', String(len * (1 - sweep)))- }- svg.appendChild(line)- if (s.symbol !== 'none') {- pts.forEach(([cx, cy], i) => {- const c = elNS('circle', { cx, cy, r: 3.5, fill: stroke, opacity: sweep })- ;(c as any).__cat = i- ;(c as any).__tip = { title: cats[i], rows: [{ name: String(s.name ?? ''), value: fmt(data[i]), color: stroke }] }- svg.appendChild(c)- })- }- })-- // invisible per-category hit bands for axis tooltips- if (d.tooltipTrigger === 'axis') {- for (let i = 0; i < nCat; i++) {- const band_ = elNS('rect', { x: G.x + band * i, y: G.y, width: band, height: G.h, fill: 'transparent' })- ;(band_ as any).__cat = i- svg.appendChild(band_)- }- }-}--function renderPie(svg: SVGSVGElement, d: Digest, _option: Opt, sweep: number) {- const s = d.series.find((x) => x?.type === 'pie')!- const data: Array<{ name: string; value: number }> = (s.data ?? []).map((p: Opt, i: number) => ({- name: String(p?.name ?? i), value: Math.max(0, num(p?.value, 0)),- }))- const total = data.reduce((a, b) => a + b.value, 0) || 1- const cx = d.grid.w / 2- const cy = d.grid.h / 2- const R = Math.min(d.grid.w, d.grid.h) / 2- const [ri, ro] = (Array.isArray(s.radius) ? s.radius : ['0%', s.radius ?? '70%']).map(- (r: string | number) => (typeof r === 'string' ? (parseFloat(r) / 100) * R : r),- )- const border = s?.itemStyle?.borderColor- const bw = num(s?.itemStyle?.borderWidth, 0)- const labelFmt: string | null = s?.label === false ? null : (s?.label?.formatter ?? '{b}')-- let a0 = -Math.PI / 2- const sweepEnd = -Math.PI / 2 + Math.PI * 2 * sweep- data.forEach((p, i) => {- const frac = p.value / total- let a1 = a0 + frac * Math.PI * 2- const clip0 = Math.min(a0, sweepEnd)- const clip1 = Math.min(a1, sweepEnd)- const color = d.colors[i % d.colors.length]- if (clip1 > clip0 + 0.0001) {- svg.appendChild(arcPath(cx, cy, ri, ro, clip0, clip1, color, border, bw, {- title: '', rows: [{ name: p.name, value: `${fmt(p.value)} (${Math.round((frac) * 1000) / 10}%)`, color }],- }))- }- // labels only at full sweep- if (sweep >= 1 && labelFmt) {- const mid = (a0 + a1) / 2- const lx = cx + Math.cos(mid) * (ro + 12)- const ly = cy + Math.sin(mid) * (ro + 12)- const right = Math.cos(mid) >= 0- const label = labelFmt- .replaceAll('{b}', p.name)- .replaceAll('{c}', fmt(p.value))- .replaceAll('{d}', String(Math.round(frac * 1000) / 10))- svg.appendChild(elNS('line', {- x1: cx + Math.cos(mid) * ro, y1: cy + Math.sin(mid) * ro, x2: lx, y2: ly,- stroke: color, 'stroke-width': 1,- }))- svg.appendChild(text(lx + (right ? 4 : -4), ly + 4, label, d.labelColor, d.font, 12, right ? 'start' : 'end'))- }- a0 = a1- })-}--function arcPath(- cx: number, cy: number, ri: number, ro: number, a0: number, a1: number,- fill: string, border: string | undefined, bw: number, tip: unknown,-): SVGPathElement {- const large = a1 - a0 > Math.PI ? 1 : 0- const p = (r: number, a: number) => `${cx + Math.cos(a) * r} ${cy + Math.sin(a) * r}`- const dstr =- `M ${p(ro, a0)} A ${ro} ${ro} 0 ${large} 1 ${p(ro, a1)} ` +- (ri > 0 ? `L ${p(ri, a1)} A ${ri} ${ri} 0 ${large} 0 ${p(ri, a0)} Z` : `L ${cx} ${cy} Z`)- const path = elNS('path', { d: dstr, fill })- if (border && bw) { path.setAttribute('stroke', border); path.setAttribute('stroke-width', String(bw)) }- ;(path as any).__tip = tip- return path-}--// --- public API ---------------------------------------------------------------const snapshotCache = new Map<string, string>()--/** Static SVG snapshot (string) for canvas/thumbnails/print. */-export function chartSnapshotSvg(el: ChartElement): string {- const key = `${el.w}x${el.h}:${JSON.stringify(el.option)}`- const hit = snapshotCache.get(key)- if (hit) return hit- let out: string- try {- out = new XMLSerializer().serializeToString(renderChart(el.option as Opt, el.w, el.h))- } catch {- out = `<svg xmlns="${SVG_NS}"></svg>`- }- if (snapshotCache.size > 60) snapshotCache.clear()- snapshotCache.set(key, out)- return out-}--/** Numeric-leaf interpolation between two same-shape options. */-function lerpOption(a: any, b: any, t: number): any {- if (typeof a === 'number' && typeof b === 'number') return a + (b - a) * t- if (Array.isArray(a) && Array.isArray(b) && a.length === b.length) return b.map((v, i) => lerpOption(a[i], v, t))- if (a && b && typeof a === 'object' && typeof b === 'object' && !Array.isArray(a) && !Array.isArray(b)) {- const out: Opt = { ...b }- for (const k of Object.keys(b)) if (k in a) out[k] = lerpOption(a[k], b[k], t)- return out- }- return t < 1 ? a ?? b : b-}--const sameShape = (a: Opt, b: Opt): boolean => {- const ta = (a?.series ?? []).map((s: Opt) => s?.type).join()- const tb = (b?.series ?? []).map((s: Opt) => s?.type).join()- return ta === tb-}--/**- * Live chart for present mode. Returns a dispose handle. With `fromOption`,- * the chart animates from that state to its own: same series types tween- * their values in place; a type change (bar⇄pie) plays a staged sweep.- */-export function mountChart(el: ChartElement, host: HTMLElement, fromOption?: Record<string, unknown>): () => void {- host.innerHTML = ''- if (getComputedStyle(host).position === 'static') host.style.position = 'relative'- const w = el.w, h = el.h- const option = el.option as Opt- const view: View = { start: 0, end: 1 }- let disposed = false- const clock = { t: 0 } // tween target — killed via killTweensOf(clock)-- const draw = (opt: Opt, sweep = 1) => {- if (disposed) return- host.querySelector('svg')?.remove()- const svg = renderChart(opt, w, h, sweep, view)- host.prepend(svg)- wireTooltips(svg, opt)- }-- // tooltip overlay — FIXED to the viewport and parented to <body>, so it is- // immune to the slide's transform scale and to sibling stacking order- const tipEl = document.createElement('div')- tipEl.style.cssText =- 'position:fixed;pointer-events:none;z-index:10000;display:none;background:rgba(18,26,40,0.92);color:#fff;' +- 'font-size:12px;line-height:1.5;padding:6px 10px;border-radius:6px;white-space:nowrap;transform:translate(-50%,-110%)'- document.body.appendChild(tipEl)-- const digestNow = () => digest(option, w, h)-- function wireTooltips(svg: SVGSVGElement, opt: Opt) {- const d = digest(opt, w, h)- if (!d.tooltipTrigger) return- svg.addEventListener('mousemove', (ev) => {- const target = ev.target as Element & { __tip?: any; __cat?: number }- if (d.tooltipTrigger === 'axis' && typeof target.__cat === 'number') {- const { cats, i0 } = catWindow(d, view)- const i = target.__cat- const rows = d.series- .filter((s) => s.type !== 'pie')- .map((s, si) => ({- name: String(s.name ?? `#${si + 1}`),- value: fmt(num((s.data ?? [])[i0 + i], 0)),- color: typeof s?.itemStyle?.color === 'string' ? s.itemStyle.color : d.colors[si % d.colors.length],- }))- showTip(cats[i] ?? '', rows, ev.clientX, ev.clientY)- } else if (target.__tip) {- showTip(target.__tip.title, target.__tip.rows, ev.clientX, ev.clientY)- } else if (d.tooltipTrigger === 'item') {- tipEl.style.display = 'none'- }- })- svg.addEventListener('mouseleave', () => { tipEl.style.display = 'none' })- }-- function showTip(title: string, rows: Array<{ name: string; value?: string; color: string }>, x: number, y: number) {- // in fullscreen, only the fullscreened element's subtree paints — the- // tip must live inside it (fixed coords stay viewport-correct)- const tipHost = document.fullscreenElement ?? document.body- if (tipEl.parentElement !== tipHost) tipHost.appendChild(tipEl)- tipEl.innerHTML =- (title ? `<b>${escapeHtml(title)}</b><br>` : '') +- rows.map((r) =>- `<span style="display:inline-block;width:8px;height:8px;border-radius:2px;background:${r.color};margin-right:5px"></span>` +- `${escapeHtml(r.name)}${r.value !== undefined ? `: <b>${escapeHtml(r.value)}</b>` : ''}`,- ).join('<br>')- tipEl.style.display = 'block'- // clamp within the viewport; flip below the cursor near the top edge- const tw = tipEl.offsetWidth- const cx = Math.max(tw / 2 + 6, Math.min(window.innerWidth - tw / 2 - 6, x))- const th = tipEl.offsetHeight- const above = y - th - 14 >= 4- tipEl.style.transform = above ? 'translate(-50%,-110%)' : 'translate(-50%,14px)'- tipEl.style.left = `${cx}px`- tipEl.style.top = `${y - (above ? 6 : 0)}px`- }-- // inside dataZoom: wheel to zoom, drag to pan- if (digestNow().zoomable && !digestNow().isPie) {- host.addEventListener('wheel', (ev) => {- ev.preventDefault()- const span = view.end - view.start- const rect = host.getBoundingClientRect()- const fx = Math.min(1, Math.max(0, (ev.clientX - rect.left) / rect.width))- const factor = ev.deltaY > 0 ? 1.18 : 1 / 1.18- const newSpan = Math.min(1, Math.max(0.1, span * factor))- const anchor = view.start + span * fx- view.start = Math.max(0, anchor - newSpan * fx)- view.end = Math.min(1, view.start + newSpan)- view.start = Math.max(0, view.end - newSpan)- draw(option)- }, { passive: false })- let panFrom: { x: number; s: number; e: number } | null = null- host.addEventListener('mousedown', (ev) => { panFrom = { x: ev.clientX, s: view.start, e: view.end } })- window.addEventListener('mousemove', (ev) => {- if (!panFrom || disposed) return- const rect = host.getBoundingClientRect()- const dx = (ev.clientX - panFrom.x) / rect.width- const span = panFrom.e - panFrom.s- let s = panFrom.s - dx * span- s = Math.max(0, Math.min(1 - span, s))- view.start = s- view.end = s + span- draw(option)- })- window.addEventListener('mouseup', () => { panFrom = null })- }-- // entrance / transition- if (fromOption && JSON.stringify(fromOption) !== JSON.stringify(option)) {- if (sameShape(fromOption as Opt, option)) {- anim.to(clock, {- t: 1, duration: 0.65, ease: 'power2.inOut',- onUpdate: () => draw(lerpOption(fromOption, option, clock.t)),- } as any)- } else {- // staged: hold old frame briefly, then sweep the new type in- draw(fromOption as Opt)- anim.to(clock, {- t: 1, duration: 0.8, delay: 0.05, ease: 'power2.inOut',- onUpdate: () => {- const t = clock.t- if (t < 0.3) {- const s = host.querySelector('svg')- if (s) (s as SVGElement).style.opacity = String(1 - t / 0.3)- } else {- draw(option, (t - 0.3) / 0.7)- }- },- } as any)- }- } else {- draw(option)- }-- ;(host as HTMLElement & { __bentoChart?: unknown }).__bentoChart = { option, redraw: () => draw(option), view }-- return () => {- disposed = true- anim.killTweensOf(clock)- tipEl.remove()- delete (host as HTMLElement & { __bentoChart?: unknown }).__bentoChart- host.innerHTML = ''- }-}--const escapeHtml = (s: string): string =>- s.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>')+// Facade: the charts-lite engine lives in the shared kernel now+// (kernel/src/charts.ts). It reads only {w, h, option}; slides' ChartElement+// satisfies that structurally, so call sites are unchanged.+export * from '../../kernel/src/charts.ts'diff --git a/slides/src/i18n.ts b/slides/src/i18n.tsindex 5806769a..815edbfa 100644--- a/slides/src/i18n.ts+++ b/slides/src/i18n.ts@@ -1,16 +1,13 @@// SPDX-License-Identifier: MIT// Copyright (c) 2026 The Bento/Suite authors-// Internationalization, Bento-sized: a ~1KB t() with English-string-as-key-// (gettext philosophy — the source stays readable, fallback is free), catalogs-// compiled into the bundle (self-containment: nothing is fetched), and locale-// resolution that follows the VIEWER (navigator.language, with a per-browser-// override from the About dialog). Language never enters the document format —-// a deck authored in Tokyo opens with French chrome in Paris.+// Facade: the i18n ENGINE lives in the shared kernel (kernel/src/i18n.ts);+// the CATALOGS are this app's string data and stay here in ./i18n/*.ts.//-// Catalogs live in src/i18n/<locale>.ts as flat Record<english, translation>.-// Missing keys fall back to English automatically. {name} placeholders are-// interpolated after lookup. Plurals: we sidestep via wording ("Slides: {n}")-// — revisit with Intl.PluralRules if a real plural ever appears.+// This module registers them at import time and re-exports the engine, so+// every consumer keeps importing './i18n' unchanged AND catalog registration+// is guaranteed to precede the first t() call by ES module evaluation order.+// App code must never import the kernel i18n directly — that would bypass+// this registration.import { ja } from './i18n/ja'import { zhHans } from './i18n/zh-Hans'@@ -19,24 +16,13 @@ import { fr } from './i18n/fr'import { de } from './i18n/de'import { zhHant } from './i18n/zh-Hant'import { it } from './i18n/it'+import { registerI18n } from '../../kernel/src/i18n.ts'+import type { LocaleChoice } from '../../kernel/src/i18n.ts'-export type Catalog = Record<string, string>--const CATALOGS: Record<string, Catalog> = {- ja,- 'zh-Hans': zhHans,- zh: zhHans, // zh, zh-CN, zh-SG → simplified- 'zh-Hant': zhHant,- 'zh-TW': zhHant,- 'zh-HK': zhHant,- it,- es,- fr,- de,-}+export type { Catalog } from '../../kernel/src/i18n.ts'/** Locales offered in the About picker (label in its own language). */-export const LOCALE_CHOICES: Array<{ code: string; label: string }> = [+const CHOICES: LocaleChoice[] = [{ code: 'en', label: 'English' },{ code: 'ja', label: '日本語' },{ code: 'zh-Hans', label: '简体中文' },@@ -47,50 +33,23 @@ export const LOCALE_CHOICES: Array<{ code: string; label: string }> = [{ code: 'de', label: 'Deutsch' },]-/** Accented pseudo-locale (dev builds only): exposes unswept strings and- * layouts that break under longer text. */-const pseudo = (s: string): string =>- '⟦' +- s.replace(/[a-zA-Z]/g, (c) => {- const map: Record<string, string> = {- a: 'à', e: 'ē', i: 'ï', o: 'ő', u: 'ū', c: 'ĉ', n: 'ñ', s: 'š', y: 'ý',- A: 'Å', E: 'Ê', I: 'Ì', O: 'Ø', U: 'Ü', C: 'Ç', N: 'Ñ', S: 'Š', Y: 'Ÿ',- }- return map[c] ?? c- }) +- '⟧'--function resolve(): string {- const saved = localStorage.getItem('bento-lang')- if (saved) return saved- const nav = navigator.language || 'en'- if (CATALOGS[nav]) return nav- const base = nav.split('-')[0]- if (base === 'zh') return 'zh-Hans'- return CATALOGS[base] ? base : 'en'-}--let current = resolve()--export const locale = (): string => current--/** Persist the override and switch. Callers re-render their own UI. */-export function setLocale(code: string): void {- if (code === 'en') localStorage.removeItem('bento-lang')- else localStorage.setItem('bento-lang', code)- current = code-}--/** Translate an English source string, then interpolate {placeholders}. */-export function t(en: string, vars?: Record<string, string | number>): string {- let out =- current === 'x-pseudo' ? pseudo(en) : (CATALOGS[current]?.[en] ?? en)- if (vars) {- for (const [k, v] of Object.entries(vars)) out = out.replaceAll(`{${k}}`, String(v))- }- return out-}--// dev convenience: window.bento.i18n exposes locale switching for testing;-// the pseudo locale is reachable by setLocale('x-pseudo') in any build.-export const i18nApi = { t, locale, setLocale, choices: LOCALE_CHOICES }+registerI18n({+ catalogs: {+ ja,+ 'zh-Hans': zhHans,+ zh: zhHans, // zh, zh-CN, zh-SG → simplified+ 'zh-Hant': zhHant,+ 'zh-TW': zhHant,+ 'zh-HK': zhHant,+ it,+ es,+ fr,+ de,+ },+ choices: CHOICES,+})++/** Kept as a const export for call sites that read it directly. */+export const LOCALE_CHOICES = CHOICES++export { t, locale, setLocale, i18nApi, localeChoices } from '../../kernel/src/i18n.ts'diff --git a/slides/src/main.ts b/slides/src/main.tsindex 647c3b1b..ac6b07a5 100644--- a/slides/src/main.ts+++ b/slides/src/main.ts@@ -5,6 +5,7 @@import './styles.css'import { anim } from './anim'+import { configureApp } from '../../kernel/src/app.ts'import {capturePristine, readEmbeddedDoc, serializeFile, serializeAuto, downloadFile,suggestedFileName, parseEnvelope, decryptEnvelope, setEncryptionPassword,@@ -20,6 +21,14 @@ import { startPresentation } from './present'import { SyncSession } from './sync/session'import { onlineTransport, startSharing, stopSharing } from './sync/online'+// Tell the kernel who this app is — must precede any kernel module use+// (window title suffix, save-picker label, update manifest + its `app` check).+configureApp({+ appId: 'bento-slides',+ appName: 'Bento Slides',+ manifestUrl: 'https://bento.page/releases/slides/manifest.json',+})+capturePristine()// --- boot gates: password-encrypted files, read-only player files -----------diff --git a/slides/src/update.ts b/slides/src/update.tsindex 162cf1b0..30e409d9 100644--- a/slides/src/update.ts+++ b/slides/src/update.ts@@ -1,205 +1,6 @@// SPDX-License-Identifier: MIT// Copyright (c) 2026 The Bento/Suite authors-// Self-update: a shipped Bento file asks the release origin whether a newer-// app shell exists — automatically at launch (per-browser opt-out in the-// About dialog) or on demand — and rebuilds itself as "same document, newer-// app": the data block re-spliced into the fetched shell via the exact-// machinery every save already uses.-//-// Trust model (see docs/architecture.md):-// - The manifest is SIGNED (ECDSA P-256 / SHA-256) with an offline key; the-// matching public key is embedded below in every shipped shell. A-// compromised host or repo cannot forge a release without that key.-// - The manifest pins the new shell's sha256; the download is hashed and-// compared before anything is spliced.-// - Only strictly NEWER versions are ever offered (no downgrade replay).-// - A check is a bare GET: no identifiers, no telemetry, nothing about the-// user or the document. The launch check can be disabled (autoCheckEnabled).-//-// The result is always a NEW downloaded file — the update flow never touches-// the file on disk, so the original is its own rollback.--import type { BentoDoc } from './model'-import {- serializeDocInto, serializeAuto, suggestedFileName, downloadFile,- hasFileHandle, writeUpdatedFile, writeUpdatedFileAs,-} from './save'--declare const __APP_VERSION__: string--/** Version of the running app shell (baked in at build from package.json). */-export const APP_VERSION: string = typeof __APP_VERSION__ !== 'undefined' ? __APP_VERSION__ : '0.0.0'--/** Per-browser preference: check for updates automatically at launch. */-/**- * Offline mode: a hard, viewer-side switch that blocks EVERY network touch —- * update checks and online collaboration alike. Same-machine tab sync- * (BroadcastChannel) is not networking and stays on. The guarantee this- * buys: with the switch on, nothing about you or a document ever leaves- * this computer.- */-export const offlineEnabled = (): boolean => {- try {- return localStorage.getItem('bento-offline') === 'on'- } catch {- return false- }-}-export const setOffline = (on: boolean): void => {- try {- localStorage.setItem('bento-offline', on ? 'on' : 'off')- } catch {- /* storage unavailable */- }-}--export const autoCheckEnabled = (): boolean => localStorage.getItem('bento-auto-check') !== 'off'-export const setAutoCheck = (on: boolean): void => {- if (on) localStorage.removeItem('bento-auto-check')- else localStorage.setItem('bento-auto-check', 'off')-}--/** Where shipped files look for releases. Dev override: localStorage 'bento-update-url'. */-export const UPDATE_MANIFEST_URL = 'https://bento.page/releases/slides/manifest.json'--// Release signing PUBLIC key. The private half lives offline with the-// maintainer (scripts/keygen.mjs → ~/.bento/release-key.json) and signs-// manifests via scripts/sign-release.mjs. Rotating this key orphans every-// previously shipped file — guard the private key instead.-const PUBLIC_KEY_JWK = {- kty: 'EC',- crv: 'P-256',- x: 'GMHSKwWcAoJVq-Dz1ZxWZM6TXATWIKbaQBpjoTystH8',- y: 'flFNzbdXCmJN8RQYCeG71rBZnnbN-MCEnp1EbCLFrj0',-} as const--export interface ReleaseInfo {- app: string- version: string- /** hex sha256 of the release shell's bytes */- sha256: string- /** absolute URL of the release shell */- url: string- notes?: string- at?: string-}--export type UpdateCheck =- | { status: 'current'; version: string }- | { status: 'update'; release: ReleaseInfo }- | { status: 'error'; message: string }--const b64ToBytes = (b64: string) => Uint8Array.from(atob(b64), (c) => c.charCodeAt(0))-const hex = (buf: ArrayBuffer) =>- [...new Uint8Array(buf)].map((b) => b.toString(16).padStart(2, '0')).join('')--/** Dotted-numeric compare: 0.2.0 > 0.1.9 > 0.1 — positive when a > b. */-export function compareVersions(a: string, b: string): number {- const pa = a.split('.').map(Number)- const pb = b.split('.').map(Number)- for (let i = 0; i < Math.max(pa.length, pb.length); i++) {- const d = (pa[i] || 0) - (pb[i] || 0)- if (d) return d- }- return 0-}--/**- * Verify the manifest signature and return its payload. The signature covers- * the payload's exact string bytes — no JSON canonicalization involved.- */-async function verifyManifest(raw: string): Promise<ReleaseInfo> {- let payload: string, sig: string- try {- ;({ payload, sig } = JSON.parse(raw))- } catch {- throw new Error('the release manifest is not valid JSON')- }- if (typeof payload !== 'string' || typeof sig !== 'string')- throw new Error('the release manifest is malformed')-- const key = await crypto.subtle.importKey(- 'jwk', PUBLIC_KEY_JWK as JsonWebKey,- { name: 'ECDSA', namedCurve: 'P-256' }, false, ['verify'],- )- const ok = await crypto.subtle.verify(- { name: 'ECDSA', hash: 'SHA-256' }, key,- b64ToBytes(sig), new TextEncoder().encode(payload),- )- if (!ok) throw new Error('the release signature is INVALID — refusing this update')-- const info = JSON.parse(payload)- if (- info?.app !== 'bento-slides' ||- typeof info.version !== 'string' ||- !/^[0-9a-f]{64}$/i.test(info.sha256 ?? '') ||- typeof info.url !== 'string'- )- throw new Error('the release manifest payload is malformed')- return info as ReleaseInfo-}--/** Ask the release origin for the latest version. */-export async function checkForUpdates(manifestUrl?: string): Promise<UpdateCheck> {- const url = manifestUrl ?? localStorage.getItem('bento-update-url') ?? UPDATE_MANIFEST_URL- try {- const res = await fetch(url, { cache: 'no-store' })- if (!res.ok) throw new Error(`release server answered ${res.status}`)- const release = await verifyManifest(await res.text())- if (compareVersions(release.version, APP_VERSION) <= 0)- return { status: 'current', version: APP_VERSION }- return { status: 'update', release }- } catch (err: any) {- return { status: 'error', message: err?.message ?? String(err) }- }-}--/**- * Fetch the release shell, verify its hash against the signed manifest, and- * return the full updated .bento.html: this document inside the new app.- */-export async function buildUpdatedFile(release: ReleaseInfo, doc: BentoDoc): Promise<string> {- const res = await fetch(release.url, { cache: 'no-store' })- if (!res.ok) throw new Error(`downloading the update failed (${res.status})`)- const bytes = await res.arrayBuffer()- const digest = hex(await crypto.subtle.digest('SHA-256', bytes))- if (digest !== release.sha256.toLowerCase())- throw new Error('the downloaded update failed its integrity check — refusing it')-- const shell = new DOMParser().parseFromString(new TextDecoder().decode(bytes), 'text/html')- if (!shell.getElementById('bento-doc'))- throw new Error('the downloaded update is not a Bento shell')- return serializeDocInto(shell, doc)-}--/** Build the updated file and hand it to the user as a fresh download. */-export async function applyUpdate(release: ReleaseInfo, doc: BentoDoc): Promise<void> {- downloadFile(await buildUpdatedFile(release, doc), suggestedFileName(doc))-}--/** Can we rewrite the open file directly (a FS Access handle is held)? */-export const canUpdateInPlace = hasFileHandle--/**- * Update the file on disk, then a reload boots the new app with this document.- *- * With a held handle: download a backup of the current version first, then- * overwrite the file in place silently.- *- * Without a handle (e.g. the file was double-clicked open — the browser grants- * no handle on open): use a save picker AND KEEP the resulting handle, so this- * update and every later one can rewrite the file in place. The picker can't- * default to the open file's location without a handle, so the caller must tell- * the user to overwrite the file they have open; once they do, it is a one-time- * grant. Returns false if the picker was cancelled.- */-export async function applyUpdateInPlace(release: ReleaseInfo, doc: BentoDoc): Promise<boolean> {- const html = await buildUpdatedFile(release, doc)- if (hasFileHandle()) {- const base = suggestedFileName(doc).replace(/\.bento\.html$/, '')- downloadFile(await serializeAuto(doc), `${base}.v${APP_VERSION}-backup.bento.html`)- await writeUpdatedFile(html)- return true- }- return writeUpdatedFileAs(html, doc, { keepHandle: true })-}+// Facade: signed self-update lives in the shared kernel now+// (kernel/src/update.ts). App identity (manifest url, manifest `app` id) is+// supplied by configureApp() in main.ts — see docs/PLATFORM.md §6, §9.+export * from '../../kernel/src/update.ts'