Skip to content

armgit / bento

Public Imported from GitHub
0

Bento, the office suite that fits in a file

v0.9.5: new charts inherit the deck palette; table→chart charts every numeric column

nyblnet60fd04e88cb3
diff --git a/CLAUDE.md b/CLAUDE.md
index d46ed715..479ea416 100644
--- a/CLAUDE.md
+++ b/CLAUDE.md
@@ -91,7 +91,13 @@ One HTML file = the document + viewer + editor. See `README.md` for the vision.
(host exposes `__bentoChart`). Unknown option keys are ignored gracefully —
exotic ECharts configs degrade, don't crash. Bar/line series data must be
PLAIN NUMBERS ({value,itemStyle} item objects coerce to 0 — only pie takes
- {name,value}); per-item bar colors unsupported, color by series. Shapes:
+ {name,value}); per-item bar colors unsupported, color by series. New charts
+ (+ Chart, preset switch, table→chart) inherit the deck's palette:
+ `applyChartPalette` (model.ts) bakes `option.color` from `doc.theme.chartPalette`
+ (optional format field; the starter deck declares peach/steel) or, absent that,
+ `deriveChartPalette(accent)` (accent + a cool HSL counterpart, each tinted).
+ tableToChart makes ONE series per numeric column (commas/`%` stripped, blanks→0,
+ first column = x labels). Shapes:
`strokeStyle` solid/dashed/dotted (legacy `strokeDash` still honoured);
line shapes have `lineStart`/`lineEnd` tips (arrow/dot/bar) rendered as
per-instance svg markers (sized in strokeWidth units, endpoints inset);
diff --git a/slides/package.json b/slides/package.json
index 25c9d965..ff5672d0 100644
--- a/slides/package.json
+++ b/slides/package.json
@@ -1,7 +1,7 @@
{
"name": "bento-slides",
"private": true,
- "version": "0.9.4",
+ "version": "0.9.5",
"type": "module",
"scripts": {
"dev": "vite",
diff --git a/slides/src/editor/editor.ts b/slides/src/editor/editor.ts
index 092201cd..c69c1280 100644
--- a/slides/src/editor/editor.ts
+++ b/slides/src/editor/editor.ts
@@ -4,7 +4,7 @@
import type { Store } from '../store'
import {
FORMAT_VERSION,
- applyLayout, builtinLayouts, defaultChart, defaultImage, defaultShape, defaultTable, defaultText,
+ applyChartPalette, applyLayout, builtinLayouts, defaultChart, defaultImage, defaultShape, defaultTable, defaultText,
instantiateLayout, layoutElementIds, newDocId, uid,
type ShapeKind, type Slide, type SlideElement,
} from '../model'
@@ -182,7 +182,7 @@ export class Editor {
this.shapeDropdown(),
btn(ICONS.image, t('Image'), () => this.pickImage()),
btn(ICONS.table, t('Table'), () => this.canvas.insert(defaultTable())),
- btn(ICONS.chart, t('Chart'), () => this.canvas.insert(defaultChart(CHART_PRESETS.bar()))),
+ btn(ICONS.chart, t('Chart'), () => this.canvas.insert(defaultChart(applyChartPalette(CHART_PRESETS.bar(), this.store.doc.theme)))),
)
const commentB = btn(ICONS.comment, t('Comment'), () => this.canvas.toggleCommentMode(),
t('Comment (C) — click an element or a spot on the slide'))
diff --git a/slides/src/editor/panels.ts b/slides/src/editor/panels.ts
index 7a36583e..0b0cf10c 100644
--- a/slides/src/editor/panels.ts
+++ b/slides/src/editor/panels.ts
@@ -3,7 +3,7 @@
// into a single undo checkpoint.
import type { Store } from '../store'
-import { defaultChart, uid, type ChartElement, type LineEnding, type ShapeElement, type Slide, type SlideElement, type TableElement, type TextElement, type TransitionKind } from '../model'
+import { applyChartPalette, defaultChart, uid, type ChartElement, type LineEnding, type ShapeElement, type Slide, type SlideElement, type TableElement, type TextElement, type TransitionKind } from '../model'
import { CHART_PRESETS } from '../charts'
import { FONT_CHOICES, firstFamily, injectFonts } from '../fonts'
import { ICONS } from '../icons'
@@ -778,7 +778,7 @@ export class PropsPanel {
this.mutate(el.id, (e) => {
const c = e as ChartElement
c.preset = v
- c.option = CHART_PRESETS[v]()
+ c.option = applyChartPalette(CHART_PRESETS[v](), this.store.doc.theme)
}, true)))
const hint = document.createElement('p')
@@ -888,23 +888,31 @@ export class PropsPanel {
/** Bridge: build a bar chart from a table's numeric columns and insert it. */
private tableToChart(el: TableElement) {
const bodyRows = el.header ? el.rows.slice(1) : el.rows
- const strip = (html: string) => html.replace(/<[^>]*>/g, '').replace(/&[a-z]+;/gi, '').trim()
+ // strip markup + entities + thousands separators so "1,204" / "+222%" parse
+ const strip = (html: string) => html.replace(/<[^>]*>/g, '').replace(/&[a-z]+;/gi, '').replace(/,/g, '').trim()
+ const num = (html: string) => parseFloat(strip(html))
const labels = bodyRows.map((r) => strip(r.cells[0]?.html ?? ''))
- // find the first body column (after labels) that parses as numbers
- let dataCol = -1
+ const headerRow = el.header ? el.rows[0] : null
+ // every column after the labels that is mostly numeric becomes its own series
+ const cols: number[] = []
for (let c = 1; c < el.columns.length; c++) {
- if (bodyRows.some((r) => !Number.isNaN(parseFloat(strip(r.cells[c]?.html ?? ''))))) { dataCol = c; break }
+ const parsed = bodyRows.map((r) => num(r.cells[c]?.html ?? '')).filter((n) => !Number.isNaN(n))
+ if (parsed.length >= Math.ceil(bodyRows.length / 2)) cols.push(c)
}
- if (dataCol < 0) { this.toast(t('No numeric column found to chart')); return }
- const nums = bodyRows.map((r) => parseFloat(strip(r.cells[dataCol]?.html ?? '')) || 0)
- const headerRow = el.header ? el.rows[0] : null
- const seriesName = headerRow ? strip(headerRow.cells[dataCol]?.html ?? '') : ''
- const option = {
+ if (!cols.length) { this.toast(t('No numeric column found to chart')); return }
+ const series = cols.map((c) => ({
+ type: 'bar',
+ name: headerRow ? strip(headerRow.cells[c]?.html ?? '') : '',
+ data: bodyRows.map((r) => { const n = num(r.cells[c]?.html ?? ''); return Number.isNaN(n) ? 0 : n }),
+ }))
+ const option: Record<string, unknown> = {
xAxis: { type: 'category', data: labels },
yAxis: { type: 'value' },
- series: [{ type: 'bar', name: seriesName, data: nums, itemStyle: { color: '#5E7699' } }],
- tooltip: { trigger: 'item', formatter: '{b}: {c}' },
+ series,
+ tooltip: { trigger: 'axis' },
}
+ if (series.length > 1) option.legend = { bottom: 0 }
+ applyChartPalette(option, this.store.doc.theme)
const chart = defaultChart(option, {
x: el.x, y: Math.min(el.y + el.h + 24, 480), w: Math.max(el.w, 640), h: 300, preset: 'bar',
})
diff --git a/slides/src/model.ts b/slides/src/model.ts
index bd6fb3c7..096063c6 100644
--- a/slides/src/model.ts
+++ b/slides/src/model.ts
@@ -283,6 +283,8 @@ export interface BentoDoc {
color: string
accent: string
fontFamily: string
+ /** ordered series colours for new charts; derived from accent when absent */
+ chartPalette?: string[]
}
/** present-mode chrome; decks with built-in chrome can turn Reveal's off */
present?: {
@@ -366,6 +368,68 @@ export function defaultText(partial: Partial<TextElement> = {}): TextElement {
}
}
+// --- chart palette -----------------------------------------------------------
+// New charts should wear the deck's colours, not a stock palette. A deck can
+// declare theme.chartPalette; otherwise we synthesise a harmonious set from the
+// single accent (accent + a cool structural counterpart, each with a light and
+// deep tint) so any deck gets on-brand charts out of the box.
+
+function hexToHsl(hex: string): [number, number, number] {
+ const m = hex.replace('#', '')
+ const r = parseInt(m.slice(0, 2), 16) / 255
+ const g = parseInt(m.slice(2, 4), 16) / 255
+ const b = parseInt(m.slice(4, 6), 16) / 255
+ const max = Math.max(r, g, b), min = Math.min(r, g, b)
+ let h = 0, s = 0; const l = (max + min) / 2
+ if (max !== min) {
+ const d = max - min
+ s = l > 0.5 ? d / (2 - max - min) : d / (max + min)
+ if (max === r) h = (g - b) / d + (g < b ? 6 : 0)
+ else if (max === g) h = (b - r) / d + 2
+ else h = (r - g) / d + 4
+ h *= 60
+ }
+ return [h, s * 100, l * 100]
+}
+
+function hslToHex(h: number, s: number, l: number): string {
+ h = ((h % 360) + 360) % 360; s = Math.max(0, Math.min(100, s)) / 100; l = Math.max(0, Math.min(100, l)) / 100
+ const c = (1 - Math.abs(2 * l - 1)) * s
+ const x = c * (1 - Math.abs(((h / 60) % 2) - 1))
+ const mm = l - c / 2
+ let r = 0, g = 0, b = 0
+ if (h < 60) { r = c; g = x } else if (h < 120) { r = x; g = c }
+ else if (h < 180) { g = c; b = x } else if (h < 240) { g = x; b = c }
+ else if (h < 300) { r = x; b = c } else { r = c; b = x }
+ const to = (v: number) => Math.round((v + mm) * 255).toString(16).padStart(2, '0')
+ return `#${to(r)}${to(g)}${to(b)}`
+}
+
+export function deriveChartPalette(accent: string): string[] {
+ let h: number, s: number, l: number
+ try { [h, s, l] = hexToHsl(accent) } catch { return ['#5470c6', '#91cc75', '#fac858', '#ee6666'] }
+ const coolH = h + 190
+ return [
+ accent,
+ hslToHex(coolH, Math.max(20, s * 0.5), Math.min(56, Math.max(44, l))), // cool counterpart
+ hslToHex(h, s * 0.92, Math.min(84, l + 14)), // accent light
+ hslToHex(coolH, Math.max(16, s * 0.38), Math.min(74, l + 20)), // cool light
+ hslToHex(h, s, Math.max(28, l - 16)), // accent deep
+ hslToHex(coolH, Math.max(24, s * 0.55), Math.max(26, l - 6)), // cool deep
+ ]
+}
+
+export function chartColorsFor(theme: BentoDoc['theme']): string[] {
+ return theme.chartPalette?.length ? theme.chartPalette.slice() : deriveChartPalette(theme.accent)
+}
+
+/** Give a chart option the deck's palette unless it already sets explicit colours. */
+export function applyChartPalette<T extends Record<string, unknown>>(option: T, theme: BentoDoc['theme']): T {
+ const cur = (option as { color?: unknown }).color
+ if (!Array.isArray(cur) || cur.length === 0) (option as { color?: string[] }).color = chartColorsFor(theme)
+ return option
+}
+
export function defaultChart(option: Record<string, unknown>, partial: Partial<ChartElement> = {}): ChartElement {
return {
id: uid('c'),
diff --git a/slides/src/starterdeck.ts b/slides/src/starterdeck.ts
index 7fc39d7f..097cdc3b 100644
--- a/slides/src/starterdeck.ts
+++ b/slides/src/starterdeck.ts
@@ -287,6 +287,9 @@ export function starterDoc(): BentoDoc {
const doc = newDoc()
doc.title = 'Bento Slides Showcase'
doc.theme.fontFamily = BODY
+ doc.theme.accent = PEACH
+ // new charts (+ Chart, table→chart) inherit the deck's midnight-&-peach family
+ doc.theme.chartPalette = [PEACH, STEEL, PEACH_SOFT, STEEL_SOFT, MIST, PEACH_DEEP]
doc.fonts = [
{ family: 'Fraunces', asset: 'font-fraunces-900', weight: '900' },
{ family: 'Instrument Sans', asset: 'font-instrument', weight: '400 700' },