Skip to content

A team of long-lived bots for DeepSeek Harness, with main bots, bot-to-bot messages and group chats.

Move the chart menu to the view card's header

Fei2411b29c24a6a450
diff --git a/ds-bot/client.js b/ds-bot/client.js
index b75e44b1..9ead3a7f 100644
--- a/ds-bot/client.js
+++ b/ds-bot/client.js
@@ -644,6 +644,7 @@ var ZH = {
"Dismiss view": "关闭视图",
Sources: "来源",
Chart: "图表",
+ "Chart {n}": "图表 {n}",
Series: "系列",
"Chart options": "图表选项",
"View as table": "以表格查看",
@@ -5124,41 +5125,6 @@ function saveImage(svg, name) {
image.onerror = () => URL.revokeObjectURL(url);
image.src = url;
}
-function ChartMenu({ asTable, onTable, onCopy, onSave }) {
- const [open, setOpen] = (0, import_react6.useState)(false);
- const root = (0, import_react6.useRef)(null);
- (0, import_react6.useEffect)(() => {
- if (!open) return void 0;
- const away = (event) => {
- if (!root.current?.contains(event.target)) setOpen(false);
- };
- const key = (event) => {
- if (event.key === "Escape") setOpen(false);
- };
- document.addEventListener("pointerdown", away, true);
- document.addEventListener("keydown", key);
- return () => {
- document.removeEventListener("pointerdown", away, true);
- document.removeEventListener("keydown", key);
- };
- }, [open]);
- const pick2 = (run2) => () => {
- setOpen(false);
- run2();
- };
- return (0, import_react6.createElement)(
- "div",
- { ref: root, className: "bt-chart-tools", "data-open": open ? "" : void 0 },
- (0, import_react6.createElement)("button", { type: "button", className: "bt-chart-more", "aria-label": t("Chart options"), "aria-haspopup": "menu", "aria-expanded": open, onClick: () => setOpen((value) => !value) }, (0, import_react6.createElement)(DotsIcon)),
- open ? (0, import_react6.createElement)(
- "div",
- { className: "bt-menu bt-chart-menu", role: "menu" },
- (0, import_react6.createElement)("button", { type: "button", role: "menuitem", onClick: pick2(onTable) }, (0, import_react6.createElement)(TableIcon), asTable ? t("View as chart") : t("View as table")),
- (0, import_react6.createElement)("button", { type: "button", role: "menuitem", onClick: pick2(onCopy) }, (0, import_react6.createElement)(CopyIcon), t("Copy data")),
- (0, import_react6.createElement)("button", { type: "button", role: "menuitem", onClick: pick2(onSave) }, (0, import_react6.createElement)(DownloadIcon), t("Download image"))
- ) : null
- );
-}
var TableIcon = () => (0, import_react6.createElement)(
"svg",
{ width: 16, height: 16, viewBox: "0 0 20 20", fill: "none", stroke: "currentColor", strokeWidth: 1.6, strokeLinecap: "round", strokeLinejoin: "round", "aria-hidden": true },
@@ -5184,7 +5150,7 @@ function ChartTable({ data }) {
)
);
}
-function WidgetChart({ node, lookup }) {
+function WidgetChart({ node, lookup, registerChart }) {
const data = chartData(node, lookup);
const { kind, categories, series, unit } = data;
const box = (0, import_react6.useRef)(null);
@@ -5195,6 +5161,7 @@ function WidgetChart({ node, lookup }) {
const [asTable, setAsTable] = (0, import_react6.useState)(false);
const [copied, setCopied] = (0, import_react6.useState)(false);
const labelId = (0, import_react6.useId)();
+ const menuKey = (0, import_react6.useId)();
const shown = series.map((_, index) => !hidden.includes(index));
const toggle = (index) => {
if (hidden.includes(index)) setHidden(hidden.filter((item) => item !== index));
@@ -5224,6 +5191,17 @@ function WidgetChart({ node, lookup }) {
setTimeout(() => setCopied(false), 1200);
});
};
+ const action = (0, import_react6.useRef)(null);
+ action.current = { copy: copy2, save: () => {
+ if (svg.current) saveImage(svg.current, "chart.png");
+ } };
+ (0, import_react6.useEffect)(() => registerChart?.(menuKey, {
+ label: typeof node.caption === "string" && node.caption !== "" ? node.caption : null,
+ asTable,
+ toggleTable: () => setAsTable((value) => !value),
+ copy: () => action.current.copy(),
+ save: () => action.current.save()
+ }), [registerChart, menuKey, label, asTable]);
const legend = isDonut ? (0, import_react6.createElement)("div", { className: "bt-chart-legend bt-chart-legend-list" }, categories.map((name, at) => (0, import_react6.createElement)(
"div",
{
@@ -5279,11 +5257,8 @@ function WidgetChart({ node, lookup }) {
const body = asTable ? (0, import_react6.createElement)(ChartTable, { data }) : isDonut ? (0, import_react6.createElement)("div", { className: legendBeside ? "bt-chart-donut bt-chart-donut-beside" : "bt-chart-donut", onPointerLeave: () => setActive(null) }, plot, legend) : (0, import_react6.createElement)(import_react6.Fragment, null, legend, plot);
return (0, import_react6.createElement)(
"figure",
- { ref: box, className: "bt-chart", "data-kind": kind, "data-table": asTable ? "" : void 0 },
+ { ref: box, className: "bt-chart", "data-kind": kind },
(0, import_react6.createElement)("span", { id: labelId, hidden: true }, label),
- (0, import_react6.createElement)(ChartMenu, { asTable, onTable: () => setAsTable((value) => !value), onCopy: copy2, onSave: () => {
- if (svg.current) saveImage(svg.current, "chart.png");
- } }),
body,
// The SVG stays mounted under the table, so the image can still be saved from it.
asTable ? (0, import_react6.createElement)("div", { hidden: true }, plot) : null,
@@ -5326,20 +5301,10 @@ var CHART_CSS = `
.bt-chart-donut{display:flex;flex-direction:column;align-items:center;gap:12px}
.bt-chart-donut-beside{flex-direction:row;align-items:center;gap:24px}
.bt-chart-donut-beside .bt-chart-legend-list{flex:1 1 auto;max-width:280px}
-.bt-chart-tools{position:absolute;top:-4px;right:0;z-index:5;opacity:0;transition:opacity .12s ease}
-.bt-chart:hover .bt-chart-tools,.bt-chart-tools:focus-within,.bt-chart-tools[data-open]{opacity:1}
-.bt-chart[data-table] .bt-chart-tools{position:relative;top:0;align-self:flex-end;order:-1;margin-bottom:-4px;opacity:1}
-@media (hover:none){.bt-chart-tools{opacity:1}}
-.bt-chart-more{width:28px;height:28px;padding:0;border:0;border-radius:50%;background:none;color:var(--bt-ink-2);display:flex;align-items:center;justify-content:center;cursor:pointer;transition:background-color .12s ease,color .12s ease}
-.bt-chart-more:hover,.bt-chart-tools[data-open] .bt-chart-more{background:var(--bt-hover);color:var(--bt-ink)}
-.bt-chart-more:focus-visible{outline:2px solid var(--bt-accent);outline-offset:1px}
-.bt-chart-more svg{width:16px;height:16px}
-.bt-menu.bt-chart-menu{position:absolute;top:32px;right:0;--bt-origin:top right;min-width:180px}
-.bt-menu.bt-chart-menu button{gap:8px}
.bt-chart-copied{position:absolute;top:2px;right:36px;font-size:12px;line-height:16px;color:var(--bt-ink-2);animation:bt-veil-in .16s ease-out}
.bt-widget-table .bt-num{text-align:end;font-variant-numeric:tabular-nums}
.bt-widget-table tbody th{padding:6px 10px;font-weight:400;text-align:start;color:var(--bt-ink);border-bottom:0}
-@media (prefers-reduced-motion:reduce){.bt-chart-mark,.bt-chart-dot,.bt-chart-tools{transition:none}.bt-chart-tip{animation:none}}
+@media (prefers-reduced-motion:reduce){.bt-chart-mark,.bt-chart-dot{transition:none}.bt-chart-tip{animation:none}}
`;
// src/client/widget-html.js
@@ -5804,7 +5769,7 @@ function WidgetNode({ node, ctx }) {
case "table":
return (0, import_react8.createElement)(WidgetTable, { node, lookup });
case "chart":
- return (0, import_react8.createElement)(WidgetChart, { node, lookup });
+ return (0, import_react8.createElement)(WidgetChart, { node, lookup, registerChart: ctx.registerChart });
case "progress":
return (0, import_react8.createElement)(WidgetProgress, { node, lookup });
case "callout":
@@ -5833,7 +5798,7 @@ function WidgetNode({ node, ctx }) {
return null;
}
}
-function WidgetView({ view, onSubmit, settled = false, answer = null, busy = false }) {
+function WidgetView({ view, onSubmit, settled = false, answer = null, busy = false, registerChart }) {
const inputs = viewInputs(view);
const [values, setValues] = (0, import_react8.useState)(() => Object.fromEntries(inputs.map((node) => [node.id, inputDefault(node)])));
const sent = answer?.values?.inputs;
@@ -5848,18 +5813,85 @@ function WidgetView({ view, onSubmit, settled = false, answer = null, busy = fal
lookup: viewScope(view, current),
values: current,
locked: settled || busy,
- setInput: (id, value) => setValues((before) => ({ ...before, [id]: value }))
+ setInput: (id, value) => setValues((before) => ({ ...before, [id]: value })),
+ registerChart
};
return (0, import_react8.createElement)(WidgetNode, { node: view, ctx });
}
+function useChartHandles() {
+ const [, bump] = (0, import_react8.useState)(0);
+ const charts = (0, import_react8.useRef)(/* @__PURE__ */ new Map());
+ const register = (0, import_react8.useRef)(null);
+ if (register.current === null) {
+ register.current = (key, handle) => {
+ charts.current.set(key, handle);
+ bump((value) => value + 1);
+ return () => {
+ charts.current.delete(key);
+ bump((value) => value + 1);
+ };
+ };
+ }
+ return { charts: [...charts.current.values()], registerChart: register.current };
+}
+function ViewMenuList({ charts, pick: pick2 }) {
+ return (0, import_react8.createElement)(
+ "div",
+ { className: "bt-menu bt-view-menu", role: "menu" },
+ charts.map((chart, index) => (0, import_react8.createElement)(
+ import_react8.Fragment,
+ { key: index },
+ charts.length > 1 ? (0, import_react8.createElement)(
+ import_react8.Fragment,
+ null,
+ index > 0 ? (0, import_react8.createElement)("div", { className: "bt-menu-sep", role: "separator" }) : null,
+ (0, import_react8.createElement)("div", { className: "bt-view-menu-label" }, chart.label ?? t("Chart {n}", { n: index + 1 }))
+ ) : null,
+ (0, import_react8.createElement)("button", { type: "button", role: "menuitem", onClick: pick2(chart.toggleTable) }, (0, import_react8.createElement)(TableIcon), chart.asTable ? t("View as chart") : t("View as table")),
+ (0, import_react8.createElement)("button", { type: "button", role: "menuitem", onClick: pick2(chart.copy) }, (0, import_react8.createElement)(CopyIcon), t("Copy data")),
+ (0, import_react8.createElement)("button", { type: "button", role: "menuitem", onClick: pick2(chart.save) }, (0, import_react8.createElement)(DownloadIcon), t("Download image"))
+ ))
+ );
+}
+function ViewMenu({ charts }) {
+ const [open, setOpen] = (0, import_react8.useState)(false);
+ const root = (0, import_react8.useRef)(null);
+ (0, import_react8.useEffect)(() => {
+ if (!open) return void 0;
+ const away = (event) => {
+ if (!root.current?.contains(event.target)) setOpen(false);
+ };
+ const key = (event) => {
+ if (event.key === "Escape") setOpen(false);
+ };
+ document.addEventListener("pointerdown", away, true);
+ document.addEventListener("keydown", key);
+ return () => {
+ document.removeEventListener("pointerdown", away, true);
+ document.removeEventListener("keydown", key);
+ };
+ }, [open]);
+ const pick2 = (run2) => () => {
+ setOpen(false);
+ run2();
+ };
+ return (0, import_react8.createElement)(
+ "div",
+ { ref: root, className: "bt-view-tools", "data-open": open ? "" : void 0 },
+ (0, import_react8.createElement)("button", { type: "button", className: "bt-view-more", "aria-label": t("Chart options"), "aria-haspopup": "menu", "aria-expanded": open, onClick: () => setOpen((value) => !value) }, (0, import_react8.createElement)(DotsIcon)),
+ open ? (0, import_react8.createElement)(ViewMenuList, { charts, pick: pick2 }) : null
+ );
+}
function WidgetCard({ title, view, onSubmit, onDismiss, busy = false, raised = false, settled = false, answer = null }) {
const titleId = (0, import_react8.useId)();
+ const { charts, registerChart } = useChartHandles();
const framed = raised || hasControls(view);
const className = ["bt-view", framed ? "bt-q" : "bt-view-bare", settled ? "bt-q-settled" : "", raised ? "bt-q-raised" : ""].filter(Boolean).join(" ");
- const head = title || onDismiss ? (0, import_react8.createElement)(
+ const head = title || onDismiss || countType(view, "chart") > 0 ? (0, import_react8.createElement)(
"div",
{ className: "bt-view-head" },
title ? (0, import_react8.createElement)("p", { className: "bt-view-title", id: titleId }, title) : (0, import_react8.createElement)("span", { className: "bt-view-title" }),
+ countType(view, "chart") > 0 ? (0, import_react8.createElement)(ViewMenu, { charts }) : null,
onDismiss ? (0, import_react8.createElement)("button", { type: "button", className: "bt-q-x", "aria-label": t("Dismiss view"), title: t("Dismiss"), onClick: onDismiss, disabled: busy }, (0, import_react8.createElement)(CloseIcon)) : null
) : null;
return (0, import_react8.createElement)(
@@ -5872,7 +5904,7 @@ function WidgetCard({ title, view, onSubmit, onDismiss, busy = false, raised = f
"aria-busy": busy || void 0
},
head,
- (0, import_react8.createElement)("div", { className: "bt-view-body" }, (0, import_react8.createElement)(WidgetView, { view, onSubmit, settled, answer, busy }))
+ (0, import_react8.createElement)("div", { className: "bt-view-body" }, (0, import_react8.createElement)(WidgetView, { view, onSubmit, settled, answer, busy, registerChart }))
);
}
function PendingView({ sessionId, view, actions, inline }) {
@@ -5955,6 +5987,16 @@ var WIDGET_CSS = `
.bt-view-head{display:flex;align-items:flex-start;gap:8px;min-width:0}
.bt-view-title{flex:1 1 0;min-width:0;margin:0;font-size:13px;line-height:18px;font-weight:500;color:var(--bt-ink);overflow-wrap:anywhere}
.bt-q-settled .bt-view-title{color:var(--bt-ink-2)}
+.bt-view-tools{position:relative;flex:none;align-self:flex-start;opacity:0;transition:opacity .12s ease}
+.bt-view:hover .bt-view-tools,.bt-view-tools:focus-within,.bt-view-tools[data-open]{opacity:1}
+@media (hover:none){.bt-view-tools{opacity:1}}
+.bt-view-more{width:28px;height:28px;padding:0;border:0;border-radius:50%;background:none;color:var(--bt-ink-2);display:flex;align-items:center;justify-content:center;cursor:pointer;transition:background-color .12s ease,color .12s ease}
+.bt-view-more:hover,.bt-view-tools[data-open] .bt-view-more{background:var(--bt-hover);color:var(--bt-ink)}
+.bt-view-more:focus-visible{outline:2px solid var(--bt-accent);outline-offset:1px}
+.bt-view-more svg{width:16px;height:16px}
+.bt-menu.bt-view-menu{position:absolute;top:32px;right:0;--bt-origin:top right;min-width:180px}
+.bt-menu.bt-view-menu button{gap:8px}
+.bt-view-menu-label{padding:2px 8px;font-size:11px;line-height:14px;color:var(--bt-ink-3);white-space:nowrap}
.bt-view-body{min-width:0}
.bt-widget-stack{display:flex;flex-direction:column;gap:12px;min-width:0}
.bt-view-row{display:flex;flex-wrap:wrap;gap:12px 24px;min-width:0}
@@ -6032,7 +6074,7 @@ var WIDGET_CSS = `
.bt-q-inline .bt-view{margin:0;width:100%;max-width:min(100%,max(calc(var(--bt-msg-w,.8) * 100%),560px));order:0}
body[data-bt-layout=flat] .bt-q-inline .bt-view{max-width:calc(var(--bt-flat-w,1) * 100%)}
.bt-q-inline:has(>.bt-view-bare){margin:8px 0 12px}
-@media (prefers-reduced-motion:reduce){.bt-widget-progress-fill,.bt-view-radio-dot{transition:none}.bt-view-bare{animation:none}}
+@media (prefers-reduced-motion:reduce){.bt-widget-progress-fill,.bt-view-radio-dot,.bt-view-tools,.bt-view-more{transition:none}.bt-view-bare{animation:none}}
${CHART_CSS}
${HTML_CSS}
`;
diff --git a/ds-bot/src/client/i18n.js b/ds-bot/src/client/i18n.js
index e04ef55a..1eba67a5 100644
--- a/ds-bot/src/client/i18n.js
+++ b/ds-bot/src/client/i18n.js
@@ -591,6 +591,7 @@ const ZH = {
'Dismiss view': '关闭视图',
Sources: '来源',
Chart: '图表',
+ 'Chart {n}': '图表 {n}',
Series: '系列',
'Chart options': '图表选项',
'View as table': '以表格查看',
diff --git a/ds-bot/src/client/widget-cell.js b/ds-bot/src/client/widget-cell.js
index 51fcaef3..c543ead7 100644
--- a/ds-bot/src/client/widget-cell.js
+++ b/ds-bot/src/client/widget-cell.js
@@ -1,12 +1,12 @@
-import { createElement as h, Fragment, useId, useRef, useState } from 'react'
+import { createElement as h, Fragment, useEffect, useId, useRef, useState } from 'react'
import { MarkdownText } from './dsh-primitives.js'
-import { CheckIcon, CloseIcon, InfoIcon } from './icons.js'
+import { CheckIcon, CloseIcon, CopyIcon, DotsIcon, DownloadIcon, InfoIcon } from './icons.js'
import { t } from './i18n.js'
import { AnsweredCell, keyLetter, useOptionKeys } from './questions.js'
import { useDockHold } from './card-hold.js'
import { heldCardKey } from './sources.js'
import { Segmented } from './motion.js'
-import { CHART_CSS, WidgetChart } from './widget-chart.js'
+import { CHART_CSS, TableIcon, WidgetChart } from './widget-chart.js'
import { HTML_CSS, HtmlFrame } from './widget-html.js'
import { asText, checkShownView, choiceValue, inputDefault, resolve, viewInputs, viewScope } from '../shared/views.js'
@@ -299,7 +299,7 @@ function WidgetNode({ node, ctx }) {
}
case 'metric': return h(WidgetMetric, { node, lookup })
case 'table': return h(WidgetTable, { node, lookup })
- case 'chart': return h(WidgetChart, { node, lookup })
+ case 'chart': return h(WidgetChart, { node, lookup, registerChart: ctx.registerChart })
case 'progress': return h(WidgetProgress, { node, lookup })
case 'callout': return h(WidgetCallout, { node, lookup })
case 'sources': return h(WidgetSources, { node })
@@ -320,7 +320,7 @@ function WidgetNode({ node, ctx }) {
// them as they were sent. Every answer carries the inputs' values. Letter keys
// answer a lone options block from the keyboard; with more than one in a card the
// same letter would hit both.
-export function WidgetView({ view, onSubmit, settled = false, answer = null, busy = false }) {
+export function WidgetView({ view, onSubmit, settled = false, answer = null, busy = false, registerChart }) {
const inputs = viewInputs(view)
const [values, setValues] = useState(() => Object.fromEntries(inputs.map(node => [node.id, inputDefault(node)])))
const sent = answer?.values?.inputs
@@ -329,28 +329,77 @@ export function WidgetView({ view, onSubmit, settled = false, answer = null, bus
const ctx = {
onSubmit: submit, settled, answer, busy, keys: !settled && countType(view, 'options') === 1,
lookup: viewScope(view, current), values: current, locked: settled || busy,
- setInput: (id, value) => setValues(before => ({ ...before, [id]: value })),
+ setInput: (id, value) => setValues(before => ({ ...before, [id]: value })), registerChart,
}
return h(WidgetNode, { node: view, ctx })
}
+// A chart keeps its actions (table view, copy, save) in the card's ⋯ menu: each
+// chart registers a handle in mount order and bumps the card so a table switch
+// shows in the open menu's wording.
+function useChartHandles() {
+ const [, bump] = useState(0)
+ const charts = useRef(new Map())
+ const register = useRef(null)
+ if (register.current === null) {
+ register.current = (key, handle) => {
+ charts.current.set(key, handle)
+ bump(value => value + 1)
+ return () => { charts.current.delete(key); bump(value => value + 1) }
+ }
+ }
+ return { charts: [...charts.current.values()], registerChart: register.current }
+}
+
+// The dropdown's items: one flat list for a lone chart, one labelled group per
+// chart otherwise.
+export function ViewMenuList({ charts, pick }) {
+ return h('div', { className: 'bt-menu bt-view-menu', role: 'menu' },
+ charts.map((chart, index) => h(Fragment, { key: index },
+ charts.length > 1 ? h(Fragment, null,
+ index > 0 ? h('div', { className: 'bt-menu-sep', role: 'separator' }) : null,
+ h('div', { className: 'bt-view-menu-label' }, chart.label ?? t('Chart {n}', { n: index + 1 }))) : null,
+ h('button', { type: 'button', role: 'menuitem', onClick: pick(chart.toggleTable) }, h(TableIcon), chart.asTable ? t('View as chart') : t('View as table')),
+ h('button', { type: 'button', role: 'menuitem', onClick: pick(chart.copy) }, h(CopyIcon), t('Copy data')),
+ h('button', { type: 'button', role: 'menuitem', onClick: pick(chart.save) }, h(DownloadIcon), t('Download image')))))
+}
+
+function ViewMenu({ charts }) {
+ const [open, setOpen] = useState(false)
+ const root = useRef(null)
+ useEffect(() => {
+ if (!open) return undefined
+ const away = (event) => { if (!root.current?.contains(event.target)) setOpen(false) }
+ const key = (event) => { if (event.key === 'Escape') setOpen(false) }
+ document.addEventListener('pointerdown', away, true)
+ document.addEventListener('keydown', key)
+ return () => { document.removeEventListener('pointerdown', away, true); document.removeEventListener('keydown', key) }
+ }, [open])
+ const pick = run => () => { setOpen(false); run() }
+ return h('div', { ref: root, className: 'bt-view-tools', 'data-open': open ? '' : undefined },
+ h('button', { type: 'button', className: 'bt-view-more', 'aria-label': t('Chart options'), 'aria-haspopup': 'menu', 'aria-expanded': open, onClick: () => setOpen(value => !value) }, h(DotsIcon)),
+ open ? h(ViewMenuList, { charts, pick }) : null)
+}
+
// What every view wears. A view with controls is a light card; one that only shows
// sits in the reply without a frame. The title is an optional small heading, and a
// view that still waits for its answer has a dismiss button.
function WidgetCard({ title, view, onSubmit, onDismiss, busy = false, raised = false, settled = false, answer = null }) {
const titleId = useId()
+ const { charts, registerChart } = useChartHandles()
const framed = raised || hasControls(view)
const className = ['bt-view', framed ? 'bt-q' : 'bt-view-bare', settled ? 'bt-q-settled' : '', raised ? 'bt-q-raised' : ''].filter(Boolean).join(' ')
- const head = title || onDismiss
+ const head = title || onDismiss || countType(view, 'chart') > 0
? h('div', { className: 'bt-view-head' },
title ? h('p', { className: 'bt-view-title', id: titleId }, title) : h('span', { className: 'bt-view-title' }),
+ countType(view, 'chart') > 0 ? h(ViewMenu, { charts }) : null,
onDismiss ? h('button', { type: 'button', className: 'bt-q-x', 'aria-label': t('Dismiss view'), title: t('Dismiss'), onClick: onDismiss, disabled: busy }, h(CloseIcon)) : null)
: null
return h('div', {
className, role: 'group', 'aria-labelledby': title ? titleId : undefined, 'aria-label': title ? undefined : t('View'), 'aria-busy': busy || undefined,
},
head,
- h('div', { className: 'bt-view-body' }, h(WidgetView, { view, onSubmit, settled, answer, busy })))
+ h('div', { className: 'bt-view-body' }, h(WidgetView, { view, onSubmit, settled, answer, busy, registerChart })))
}
// A view waiting for its answer, inline at its call or raised in the dock. Like a
@@ -445,6 +494,16 @@ export const WIDGET_CSS = `
.bt-view-head{display:flex;align-items:flex-start;gap:8px;min-width:0}
.bt-view-title{flex:1 1 0;min-width:0;margin:0;font-size:13px;line-height:18px;font-weight:500;color:var(--bt-ink);overflow-wrap:anywhere}
.bt-q-settled .bt-view-title{color:var(--bt-ink-2)}
+.bt-view-tools{position:relative;flex:none;align-self:flex-start;opacity:0;transition:opacity .12s ease}
+.bt-view:hover .bt-view-tools,.bt-view-tools:focus-within,.bt-view-tools[data-open]{opacity:1}
+@media (hover:none){.bt-view-tools{opacity:1}}
+.bt-view-more{width:28px;height:28px;padding:0;border:0;border-radius:50%;background:none;color:var(--bt-ink-2);display:flex;align-items:center;justify-content:center;cursor:pointer;transition:background-color .12s ease,color .12s ease}
+.bt-view-more:hover,.bt-view-tools[data-open] .bt-view-more{background:var(--bt-hover);color:var(--bt-ink)}
+.bt-view-more:focus-visible{outline:2px solid var(--bt-accent);outline-offset:1px}
+.bt-view-more svg{width:16px;height:16px}
+.bt-menu.bt-view-menu{position:absolute;top:32px;right:0;--bt-origin:top right;min-width:180px}
+.bt-menu.bt-view-menu button{gap:8px}
+.bt-view-menu-label{padding:2px 8px;font-size:11px;line-height:14px;color:var(--bt-ink-3);white-space:nowrap}
.bt-view-body{min-width:0}
.bt-widget-stack{display:flex;flex-direction:column;gap:12px;min-width:0}
.bt-view-row{display:flex;flex-wrap:wrap;gap:12px 24px;min-width:0}
@@ -522,7 +581,7 @@ export const WIDGET_CSS = `
.bt-q-inline .bt-view{margin:0;width:100%;max-width:min(100%,max(calc(var(--bt-msg-w,.8) * 100%),560px));order:0}
body[data-bt-layout=flat] .bt-q-inline .bt-view{max-width:calc(var(--bt-flat-w,1) * 100%)}
.bt-q-inline:has(>.bt-view-bare){margin:8px 0 12px}
-@media (prefers-reduced-motion:reduce){.bt-widget-progress-fill,.bt-view-radio-dot{transition:none}.bt-view-bare{animation:none}}
+@media (prefers-reduced-motion:reduce){.bt-widget-progress-fill,.bt-view-radio-dot,.bt-view-tools,.bt-view-more{transition:none}.bt-view-bare{animation:none}}
${CHART_CSS}
${HTML_CSS}
`
diff --git a/ds-bot/src/client/widget-chart.js b/ds-bot/src/client/widget-chart.js
index 30a80856..5dfe5ec0 100644
--- a/ds-bot/src/client/widget-chart.js
+++ b/ds-bot/src/client/widget-chart.js
@@ -1,5 +1,4 @@
import { createElement as h, Fragment, useEffect, useId, useLayoutEffect, useRef, useState } from 'react'
-import { CopyIcon, DotsIcon, DownloadIcon } from './icons.js'
import { t } from './i18n.js'
import { asText, resolve } from '../shared/views.js'
@@ -270,27 +269,7 @@ function saveImage(svg, name) {
image.src = url
}
-function ChartMenu({ asTable, onTable, onCopy, onSave }) {
- const [open, setOpen] = useState(false)
- const root = useRef(null)
- useEffect(() => {
- if (!open) return undefined
- const away = (event) => { if (!root.current?.contains(event.target)) setOpen(false) }
- const key = (event) => { if (event.key === 'Escape') setOpen(false) }
- document.addEventListener('pointerdown', away, true)
- document.addEventListener('keydown', key)
- return () => { document.removeEventListener('pointerdown', away, true); document.removeEventListener('keydown', key) }
- }, [open])
- const pick = run => () => { setOpen(false); run() }
- return h('div', { ref: root, className: 'bt-chart-tools', 'data-open': open ? '' : undefined },
- h('button', { type: 'button', className: 'bt-chart-more', 'aria-label': t('Chart options'), 'aria-haspopup': 'menu', 'aria-expanded': open, onClick: () => setOpen(value => !value) }, h(DotsIcon)),
- open ? h('div', { className: 'bt-menu bt-chart-menu', role: 'menu' },
- h('button', { type: 'button', role: 'menuitem', onClick: pick(onTable) }, h(TableIcon), asTable ? t('View as chart') : t('View as table')),
- h('button', { type: 'button', role: 'menuitem', onClick: pick(onCopy) }, h(CopyIcon), t('Copy data')),
- h('button', { type: 'button', role: 'menuitem', onClick: pick(onSave) }, h(DownloadIcon), t('Download image'))) : null)
-}
-
-const TableIcon = () => h('svg', { width: 16, height: 16, viewBox: '0 0 20 20', fill: 'none', stroke: 'currentColor', strokeWidth: 1.6, strokeLinecap: 'round', strokeLinejoin: 'round', 'aria-hidden': true },
+export const TableIcon = () => h('svg', { width: 16, height: 16, viewBox: '0 0 20 20', fill: 'none', stroke: 'currentColor', strokeWidth: 1.6, strokeLinecap: 'round', strokeLinejoin: 'round', 'aria-hidden': true },
h('path', { d: 'M4.5 4h11A1.5 1.5 0 0 1 17 5.5v9a1.5 1.5 0 0 1-1.5 1.5h-11A1.5 1.5 0 0 1 3 14.5v-9A1.5 1.5 0 0 1 4.5 4z' }), h('path', { d: 'M3 8.5h14M8 8.5V16' }))
function ChartTable({ data }) {
@@ -304,7 +283,7 @@ function ChartTable({ data }) {
series.map((item, index) => h('td', { key: index, className: 'bt-num' }, withUnit(item.values[at], series.length === 1 && !item.name ? '' : unit))))))))
}
-export function WidgetChart({ node, lookup }) {
+export function WidgetChart({ node, lookup, registerChart }) {
const data = chartData(node, lookup)
const { kind, categories, series, unit } = data
const box = useRef(null)
@@ -315,6 +294,7 @@ export function WidgetChart({ node, lookup }) {
const [asTable, setAsTable] = useState(false)
const [copied, setCopied] = useState(false)
const labelId = useId()
+ const menuKey = useId()
const shown = series.map((_, index) => !hidden.includes(index))
const toggle = (index) => {
if (hidden.includes(index)) setHidden(hidden.filter(item => item !== index))
@@ -340,6 +320,17 @@ export function WidgetChart({ node, lookup }) {
const copy = () => {
void navigator.clipboard?.writeText(chartCsv(data)).then(() => { setCopied(true); setTimeout(() => setCopied(false), 1200) })
}
+ const action = useRef(null)
+ action.current = { copy, save: () => { if (svg.current) saveImage(svg.current, 'chart.png') } }
+ // The card's ⋯ menu shows the chart's actions; a new handle on every table
+ // switch keeps its first item's wording current.
+ useEffect(() => registerChart?.(menuKey, {
+ label: typeof node.caption === 'string' && node.caption !== '' ? node.caption : null,
+ asTable,
+ toggleTable: () => setAsTable(value => !value),
+ copy: () => action.current.copy(),
+ save: () => action.current.save(),
+ }), [registerChart, menuKey, label, asTable])
const legend = isDonut
? h('div', { className: 'bt-chart-legend bt-chart-legend-list' }, categories.map((name, at) => h('div', {
key: at, className: 'bt-chart-key', 'data-dim': active !== null && active !== at ? '' : undefined,
@@ -369,9 +360,8 @@ export function WidgetChart({ node, lookup }) {
const body = asTable ? h(ChartTable, { data })
: isDonut ? h('div', { className: legendBeside ? 'bt-chart-donut bt-chart-donut-beside' : 'bt-chart-donut', onPointerLeave: () => setActive(null) }, plot, legend)
: h(Fragment, null, legend, plot)
- return h('figure', { ref: box, className: 'bt-chart', 'data-kind': kind, 'data-table': asTable ? '' : undefined },
+ return h('figure', { ref: box, className: 'bt-chart', 'data-kind': kind },
h('span', { id: labelId, hidden: true }, label),
- h(ChartMenu, { asTable, onTable: () => setAsTable(value => !value), onCopy: copy, onSave: () => { if (svg.current) saveImage(svg.current, 'chart.png') } }),
body,
// The SVG stays mounted under the table, so the image can still be saved from it.
asTable ? h('div', { hidden: true }, plot) : null,
@@ -414,18 +404,8 @@ export const CHART_CSS = `
.bt-chart-donut{display:flex;flex-direction:column;align-items:center;gap:12px}
.bt-chart-donut-beside{flex-direction:row;align-items:center;gap:24px}
.bt-chart-donut-beside .bt-chart-legend-list{flex:1 1 auto;max-width:280px}
-.bt-chart-tools{position:absolute;top:-4px;right:0;z-index:5;opacity:0;transition:opacity .12s ease}
-.bt-chart:hover .bt-chart-tools,.bt-chart-tools:focus-within,.bt-chart-tools[data-open]{opacity:1}
-.bt-chart[data-table] .bt-chart-tools{position:relative;top:0;align-self:flex-end;order:-1;margin-bottom:-4px;opacity:1}
-@media (hover:none){.bt-chart-tools{opacity:1}}
-.bt-chart-more{width:28px;height:28px;padding:0;border:0;border-radius:50%;background:none;color:var(--bt-ink-2);display:flex;align-items:center;justify-content:center;cursor:pointer;transition:background-color .12s ease,color .12s ease}
-.bt-chart-more:hover,.bt-chart-tools[data-open] .bt-chart-more{background:var(--bt-hover);color:var(--bt-ink)}
-.bt-chart-more:focus-visible{outline:2px solid var(--bt-accent);outline-offset:1px}
-.bt-chart-more svg{width:16px;height:16px}
-.bt-menu.bt-chart-menu{position:absolute;top:32px;right:0;--bt-origin:top right;min-width:180px}
-.bt-menu.bt-chart-menu button{gap:8px}
.bt-chart-copied{position:absolute;top:2px;right:36px;font-size:12px;line-height:16px;color:var(--bt-ink-2);animation:bt-veil-in .16s ease-out}
.bt-widget-table .bt-num{text-align:end;font-variant-numeric:tabular-nums}
.bt-widget-table tbody th{padding:6px 10px;font-weight:400;text-align:start;color:var(--bt-ink);border-bottom:0}
-@media (prefers-reduced-motion:reduce){.bt-chart-mark,.bt-chart-dot,.bt-chart-tools{transition:none}.bt-chart-tip{animation:none}}
+@media (prefers-reduced-motion:reduce){.bt-chart-mark,.bt-chart-dot{transition:none}.bt-chart-tip{animation:none}}
`
diff --git a/ds-bot/test/tool-cells.test.js b/ds-bot/test/tool-cells.test.js
index b31aa742..76009316 100644
--- a/ds-bot/test/tool-cells.test.js
+++ b/ds-bot/test/tool-cells.test.js
@@ -13,6 +13,7 @@ const { outputFiles } = await build({
"export { QuestionDock } from './src/client/questions.js'",
"export { SecretDock } from './src/client/secret-card.js'",
"export { countCards } from './src/client/sources.js'",
+ "export { ViewMenuList } from './src/client/widget-cell.js'",
].join('\n'),
resolveDir: fileURLToPath(new URL('..', import.meta.url)),
},
@@ -45,7 +46,7 @@ runInNewContext(outputFiles[0].text, {
throw new Error(`Unexpected platform module ${name}`)
},
})
-const { ToolCell, TriggerCell, callTree, stackReplies, QuestionDock, SecretDock, countCards } = module.exports
+const { ToolCell, TriggerCell, callTree, stackReplies, QuestionDock, SecretDock, countCards, ViewMenuList } = module.exports
// Runs the effects collected since the last render; returns the cleanups.
const mount = () => {
@@ -661,12 +662,35 @@ test('a view works its formulas out, draws a chart, and keeps html in a sandboxe
const chart = cell({ type: 'chart', kind: 'bar', categories: ['A', 'B'], series: [{ values: [3, 5] }], unit: 'ms' })
assert.ok(classed(chart, 'bt-view-bare') !== undefined, 'a view that only shows has no frame')
assert.ok(find(chart, item => item.type === 'svg') !== undefined)
- assert.equal(find(chart, item => item.type === 'figure').props['data-kind'], 'bar')
- assert.ok(classed(chart, 'bt-chart-more') !== undefined, 'the chart has its menu button')
+ const figure = find(chart, item => item.type === 'figure')
+ assert.equal(figure.props['data-kind'], 'bar')
+ // The ⋯ menu sits in the card's head, not on the chart's axes (#476).
+ const head = classed(chart, 'bt-view-head')
+ assert.ok(find(head, item => typeof item.props?.className === 'string' && item.props.className.includes('bt-view-more')) !== undefined, 'the card head has the menu button')
+ assert.equal(find(figure, item => typeof item.props?.className === 'string' && (item.props.className.includes('bt-chart-more') || item.props.className.includes('bt-chart-tools'))), undefined, 'the chart itself has no menu')
+ // A view without a chart has no ⋯ button.
+ const metric = cell({ type: 'metric', label: 'Done', value: '3' })
+ assert.equal(find(metric, item => typeof item.props?.className === 'string' && item.props.className.includes('bt-view-more')), undefined)
for (const kind of ['hbar', 'line', 'area', 'donut']) {
assert.ok(find(cell({ type: 'chart', kind, categories: ['A', 'B'], series: [{ values: [3, 5] }] }), item => item.type === 'svg') !== undefined, kind)
}
+ // The ⋯ menu groups per chart when the view holds more than one (#476).
+ const handle = (label, asTable = false) => ({ label, asTable, toggleTable: () => {}, copy: () => {}, save: () => {} })
+ const menuItems = node => findAll(node, item => item.props?.role === 'menuitem').map(text)
+ const single = expand(ViewMenuList({ charts: [handle('Revenue')], pick: run => run }))
+ assert.equal(findAll(single, item => item.props?.className === 'bt-view-menu-label').length, 0, 'one chart needs no group label')
+ assert.deepEqual(menuItems(single), ['View as table', 'Copy data', 'Download image'])
+ const pair = expand(ViewMenuList({ charts: [handle('Revenue'), handle(null, true), handle('Costs')], pick: run => run }))
+ const labels = findAll(pair, item => item.props?.className === 'bt-view-menu-label').map(text)
+ assert.deepEqual(labels, ['Revenue', 'Chart 2', 'Costs'], 'each group is named after its chart')
+ assert.equal(findAll(pair, item => item.props?.className === 'bt-menu-sep').length, 2, 'groups are separated')
+ assert.deepEqual(menuItems(pair), [
+ 'View as table', 'Copy data', 'Download image',
+ 'View as chart', 'Copy data', 'Download image',
+ 'View as table', 'Copy data', 'Download image',
+ ], 'each chart keeps its three actions and its table state')
+
const frame = find(cell({ type: 'html', html: '<b>hi</b>', summary: 'A greeting' }), item => item.type === 'iframe')
assert.equal(frame.props.sandbox, 'allow-scripts')
assert.match(frame.props.srcDoc, /Content-Security-Policy" content="default-src 'none'/)