@open-document/core 0.9.1 → 0.11.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/{build-B0GJXy8I.js → build-BHFGY8eF.js} +1 -1
- package/dist/{check-CuDIWsPT.js → check-UEBmDfT_.js} +3 -3
- package/dist/cli/bin.js +10 -6
- package/dist/{config-CAZ7Z_va.js → config-B_pdNbYg.js} +1166 -591
- package/dist/{context-PWAh9qbf.js → context-BM9_-u9P.js} +1 -1
- package/dist/{dev-CYqppXDI.js → dev-DX3QMIRi.js} +1 -1
- package/dist/{diagram-CCaUUnRe.js → diagram-BK4jTZr4.js} +146 -146
- package/dist/diff-DayvbXfX.js +44 -0
- package/dist/{export-1JtO2OMC.js → export-BPsXj1i6.js} +3 -3
- package/dist/{import-CsrdBOZ8.js → import-71wlAsrd.js} +2 -2
- package/dist/index.d.ts +53 -2
- package/dist/index.js +457 -18
- package/dist/ops/index.d.ts +72 -17
- package/dist/ops/index.js +3 -3
- package/dist/{ops-B2y9BMpk.js → ops-BUo67DaP.js} +642 -124
- package/dist/{preview-DW3PM-JV.js → preview-D3m1-gUL.js} +1 -1
- package/dist/{sdk-BqU6mwD_.d.ts → sdk-CpDEGRIo.d.ts} +2 -0
- package/dist/vite/index.js +1 -1
- package/package.json +2 -2
- package/skills/doc-authoring/SKILL.md +9 -4
- package/skills/doc-authoring/references/pagination.md +1 -1
- package/skills/doc-authoring/references/tables-and-charts.md +18 -1
- package/src/app/components/browser/browser-ui.tsx +105 -0
- package/src/app/components/chart.tsx +239 -0
- package/src/app/components/command-palette.tsx +284 -0
- package/src/app/components/design-panel/design-panel.tsx +22 -20
- package/src/app/components/design-panel/design-provider.tsx +4 -2
- package/src/app/components/diagram.tsx +11 -1
- package/src/app/components/doc-assets.tsx +6 -5
- package/src/app/components/doc-search.tsx +12 -10
- package/src/app/components/doc-sidebar.tsx +24 -18
- package/src/app/components/flow-page.tsx +2 -0
- package/src/app/components/inspector/format-section.tsx +434 -0
- package/src/app/components/inspector/inspector.tsx +310 -89
- package/src/app/components/inspector/use-style-edits.ts +210 -0
- package/src/app/components/language-toggle.tsx +38 -0
- package/src/app/components/palette-items.ts +128 -0
- package/src/app/components/panel/edit-save-card.tsx +4 -2
- package/src/app/components/panel/fields.tsx +100 -9
- package/src/app/components/panel/save-card.tsx +18 -10
- package/src/app/components/sidebar/folder-item.tsx +10 -8
- package/src/app/components/sidebar/icon-picker.tsx +3 -1
- package/src/app/components/sidebar/sidebar.tsx +44 -13
- package/src/app/components/theme-toggle.tsx +5 -3
- package/src/app/components/ui/menu.tsx +5 -0
- package/src/app/lib/agent-bridge.ts +8 -2
- package/src/app/lib/chart.test.ts +108 -0
- package/src/app/lib/chart.ts +343 -0
- package/src/app/lib/doc-preview.tsx +0 -10
- package/src/app/lib/docs.ts +3 -0
- package/src/app/lib/docx/from-dom.ts +11 -11
- package/src/app/lib/docx/ooxml.test.ts +25 -9
- package/src/app/lib/docx/ooxml.ts +58 -37
- package/src/app/lib/export-docx.ts +13 -8
- package/src/app/lib/flow.ts +1 -1
- package/src/app/lib/i18n-ja/browser.ts +88 -0
- package/src/app/lib/i18n-ja/doc.ts +65 -0
- package/src/app/lib/i18n-ja/index.ts +5 -0
- package/src/app/lib/i18n-ja/panels.ts +151 -0
- package/src/app/lib/i18n-ko/browser.ts +87 -0
- package/src/app/lib/i18n-ko/doc.ts +65 -0
- package/src/app/lib/i18n-ko/index.ts +5 -0
- package/src/app/lib/i18n-ko/panels.ts +148 -0
- package/src/app/lib/i18n-zh-cn/browser.ts +84 -0
- package/src/app/lib/i18n-zh-cn/doc.ts +64 -0
- package/src/app/lib/i18n-zh-cn/index.ts +5 -0
- package/src/app/lib/i18n-zh-cn/panels.ts +143 -0
- package/src/app/lib/i18n-zh-tw/browser.ts +84 -0
- package/src/app/lib/i18n-zh-tw/doc.ts +64 -0
- package/src/app/lib/i18n-zh-tw/index.ts +5 -0
- package/src/app/lib/i18n-zh-tw/panels.ts +144 -0
- package/src/app/lib/i18n.test.ts +24 -0
- package/src/app/lib/i18n.tsx +109 -0
- package/src/app/lib/inspector/format.ts +156 -0
- package/src/app/lib/outline.ts +23 -0
- package/src/app/lib/page-range.test.ts +10 -1
- package/src/app/lib/page-range.ts +26 -0
- package/src/app/lib/sdk.ts +2 -0
- package/src/app/main.tsx +4 -1
- package/src/app/routes/assets.tsx +193 -132
- package/src/app/routes/doc.tsx +668 -310
- package/src/app/routes/home-shell.tsx +115 -88
- package/src/app/routes/home.tsx +200 -67
- package/src/app/routes/themes.tsx +42 -35
- package/src/app/styles.css +84 -5
- package/src/app/virtual.d.ts +1 -0
|
@@ -99,6 +99,8 @@ type DocPage = ComponentType;
|
|
|
99
99
|
type FlowSection = {
|
|
100
100
|
readonly __odFlow: true;
|
|
101
101
|
blocks: ReactNode[];
|
|
102
|
+
/** Rendered on every page the section expands into, in the top margin band. */
|
|
103
|
+
header?: ComponentType;
|
|
102
104
|
/** Rendered on every page the section expands into. */
|
|
103
105
|
footer?: ComponentType;
|
|
104
106
|
/** Page padding override in px; defaults to the design's `margin`. */
|
package/dist/vite/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { j as openDocPlugin, t as createViteConfig } from "../config-
|
|
1
|
+
import { j as openDocPlugin, t as createViteConfig } from "../config-B_pdNbYg.js";
|
|
2
2
|
export { createViteConfig, openDocPlugin };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@open-document/core",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.11.0",
|
|
4
4
|
"description": "Runtime and CLI for open-doc — write documents in docs/, we handle the pages, print layout, and export.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|
|
@@ -62,7 +62,7 @@
|
|
|
62
62
|
"dependencies": {
|
|
63
63
|
"@babel/parser": "^8.0.4",
|
|
64
64
|
"@tailwindcss/vite": "^4.3.3",
|
|
65
|
-
"@vitejs/plugin-react": "^6.
|
|
65
|
+
"@vitejs/plugin-react": "^6.1.1",
|
|
66
66
|
"chalk": "^6.0.0",
|
|
67
67
|
"clsx": "^2.1.1",
|
|
68
68
|
"commander": "^15.0.0",
|
|
@@ -138,6 +138,8 @@ The framework builds the document outline by scanning rendered pages for `h1`, `
|
|
|
138
138
|
- **The cover title and the word "Contents" both carry `data-od-outline="skip"`.** A contents list that opens with the cover and lists itself reads as a bug.
|
|
139
139
|
- Override the listed text with `data-od-heading="Short title"` when the visible heading is long or contains markup.
|
|
140
140
|
|
|
141
|
+
Bare elements on the sheet start from a base drawn from the design, not from a blank reset: headings are bold at `--od-size-h1`/`h2`/`h3` in the heading font, paragraphs, lists, quotes and tables have a bottom margin, lists have their markers and indent, links take `--od-accent` with an underline, `code`/`pre` use `--od-font-mono`, and a quote carries a rule on its leading edge. Anything you set inline replaces it, so style the elements that matter to the layout explicitly — margins above all, since they decide where a page breaks.
|
|
142
|
+
|
|
141
143
|
## Footnotes, numbering, and data
|
|
142
144
|
|
|
143
145
|
Four primitives resolve themselves from the rendered pages, the same way the
|
|
@@ -153,6 +155,8 @@ contents list does. Read `references/long-form.md` before using any of them.
|
|
|
153
155
|
- **`<DataTable rows={…}>`** — a print-shaped table from an imported `.csv`.
|
|
154
156
|
- **`<Diagram chart={…} caption>`** — an architecture or flow drawing from an
|
|
155
157
|
imported `.mmd`. Given a caption it numbers as a figure, like `<Figure>`.
|
|
158
|
+
- **`<Chart data x y caption>`** — a bar, line or pie chart from rows, in the
|
|
159
|
+
document's colours; numbers as a figure. See `references/tables-and-charts.md`.
|
|
156
160
|
|
|
157
161
|
`meta.labels` sets what they are called (`圖`, `表`) — the numbering itself is
|
|
158
162
|
structural.
|
|
@@ -226,8 +230,8 @@ const Footer = () => {
|
|
|
226
230
|
```
|
|
227
231
|
|
|
228
232
|
- **Never hardcode** `3 / 12`. Both hooks are 1-based and return `0` outside a page.
|
|
229
|
-
- Define the header/footer once as a local component and
|
|
230
|
-
-
|
|
233
|
+
- Define the header/footer once as a local component. A `flow()` section takes both — `flow(<>…</>, { header: Header, footer: Footer })` — and prints them on every page it expands into; on a fixed page, drop the component in yourself. Cover pages normally omit them.
|
|
234
|
+
- Headers and footers are absolutely positioned inside the margin band (`top: 32` for a header, `bottom: 40` for a footer); they do not consume the text block's vertical budget — but keep at least 24px of clearance between the body copy and either one.
|
|
231
235
|
|
|
232
236
|
## Starter template
|
|
233
237
|
|
|
@@ -360,8 +364,9 @@ A document is not a slide deck. Long-form copy is the point — but it still has
|
|
|
360
364
|
- **Assets panel** (`/assets` in the dev UI): upload, rename, and delete files in the global `assets/` folder or any document's `assets/` folder, with an "unused" badge and a copy-ready import line. Files you reference in source are what it scans, so an import you write by hand shows up there immediately.
|
|
361
365
|
- **Inspect mode** (the "Inspect" button, dev only): click any element on a page to edit its text in place — the change is written straight back into `docs/<id>/index.tsx` — or leave a note for the agent, which is stored as a `@doc-comment` marker and processed by the `apply-comments` skill.
|
|
362
366
|
- **Download menu** — PDF (true page size) and self-contained HTML.
|
|
363
|
-
- **Headless render** — `open-doc export <id> --format pdf|html|png|docx` produces the same output from a script, and `open-doc check <id>` reports layout faults. Both drive the real viewer in a headless browser, so what they produce is what the Download menu produces.
|
|
364
|
-
- **
|
|
367
|
+
- **Headless render** — `open-doc export <id> --format pdf|html|png|docx` produces the same output from a script, and `open-doc check <id>` reports layout faults. Both drive the real viewer in a headless browser, so what they produce is what the Download menu produces. A PDF exported this way also carries the outline as bookmarks and is tagged for screen readers — the same headings the sidebar and `<TableOfContents />` list, so a heading marked `data-od-outline="skip"` stays out and `data-od-heading` sets the bookmark's text. (The Download menu prints through the browser's dialog, which writes no bookmarks.)
|
|
368
|
+
- **What changed** — `open-doc diff <id> [--since <rev>]` (MCP: `diff_document`) renders the document as it is and as it was at a git revision (default `HEAD`), pairs the pages by content, and lists each page as same, changed, added or removed, with the lines of text that moved. It also writes `out/<id>-diff.html`, a self-contained before/after report with the changed regions outlined — hand that to a reviewer. Run it before handing back an edit, so your summary of what changed is what actually changed on the page.
|
|
369
|
+
- **Word (DOCX)** — for review that runs in Word. It carries structure, not page breaks: headings become Word heading styles, `<TableOfContents />` a TOC field, `<Footnote>` real footnotes, a `flow()` header and footer a Word header and footer with page-number fields, and the theme's CJK font the East Asian font. Write headings as real `h1`–`h3` and tables as `<table>`/`<DataTable>` so they arrive as structure; anything drawn with boxes (a chart made of `div`s) arrives as a picture, and absolutely positioned layouts on fixed pages flow as plain paragraphs.
|
|
365
370
|
- **Design panel** (the "Design" button in the document view, dev only): live-tweaks the `design` const — palette, fonts, type scale, margin, leading, radius — previewing on the real pages and writing the values back into `docs/<id>/index.tsx` on save.
|
|
366
371
|
|
|
367
372
|
### Writing for the inspector
|
|
@@ -31,7 +31,7 @@ export default [Cover, Contents, Body] satisfies DocEntry[];
|
|
|
31
31
|
|
|
32
32
|
- **Each direct child of the fragment is one block.** Blocks are atomic: a block never splits across a page, so a table either fits whole or moves to the next page.
|
|
33
33
|
- The framework owns the page shell for flow pages — margin, background, and base typography come from the `design` const. Your blocks keep their own styles. `flow(node, { padding })` overrides the margin for that section.
|
|
34
|
-
- `footer`
|
|
34
|
+
- `header` and `footer` are components rendered on **every** page the section expands into. `useDocPageNumber()` works inside them, so a running header or footer needs no extra wiring. Position each absolutely in its margin band (`top: 32` / `bottom: 40`); neither takes anything from the block budget.
|
|
35
35
|
- Blocks are measured in the real DOM at the real page width after fonts settle, then packed greedily. Change a paragraph and the pagination re-runs on hot reload.
|
|
36
36
|
|
|
37
37
|
### Keep rules
|
|
@@ -95,7 +95,24 @@ Every stat needs a comparison (target, prior period) — a number with nothing t
|
|
|
95
95
|
|
|
96
96
|
## Charts
|
|
97
97
|
|
|
98
|
-
|
|
98
|
+
For a bar, line or pie chart of data, use **`<Chart>`** — it draws from rows (an imported `.csv`, or an array literal), in the document's accent and tints of it, and numbers as a figure when given a caption:
|
|
99
|
+
|
|
100
|
+
```tsx
|
|
101
|
+
import costs from './data/costs.csv';
|
|
102
|
+
|
|
103
|
+
<Chart data={costs} x="month" y={['cost', 'budget']} labels={{ cost: '實際', budget: '預算' }}
|
|
104
|
+
format="integer" caption="每月費用與預算" id="f-cost" />
|
|
105
|
+
<Chart type="line" data={costs} x="month" y="uptime" format="percent" values caption="可用率" />
|
|
106
|
+
<Chart type="pie" data={services} x="service" y="cost" values caption="費用占比" />
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
- `type`: `bar` (default), `line`, `pie`. `y` takes one key or several (one series each); a pie reads the first.
|
|
110
|
+
- `stacked` stacks bar series instead of grouping them; `values` prints each value on its bar, point or slice.
|
|
111
|
+
- `format`: `number`, `integer`, `percent`, or a function — the same vocabulary as `<DataTable>`.
|
|
112
|
+
- `width` / `height` are the drawn size in px (default 560 × 260); it scales down to the column.
|
|
113
|
+
- It lays out synchronously into a fixed box, so it paginates like any block. In Word it arrives as a picture.
|
|
114
|
+
|
|
115
|
+
Anything `<Chart>` doesn't draw — a sparkline in a stat row, an annotated timeline — write as **inline SVG** sized in absolute px:
|
|
99
116
|
|
|
100
117
|
```tsx
|
|
101
118
|
const Bars = () => {
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import type { LucideIcon } from 'lucide-react';
|
|
2
|
+
import { type ReactNode, type RefObject, useLayoutEffect, useRef, useState } from 'react';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* The pieces every page of the document browser shares — Documents, a folder,
|
|
6
|
+
* Themes, Assets — so they line up with each other: one header, one empty
|
|
7
|
+
* state, one card width. The shell supplies the page padding; nothing here
|
|
8
|
+
* adds its own.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
/** The narrowest a card gets; columns share whatever width is left over. */
|
|
12
|
+
export const CARD_WIDTH = 200;
|
|
13
|
+
|
|
14
|
+
export const CARD_GRID =
|
|
15
|
+
'grid gap-x-6 gap-y-8 [grid-template-columns:repeat(auto-fill,minmax(200px,1fr))]';
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* A card's picture is a real page scaled down, and the scale is a number, so the
|
|
19
|
+
* card has to know how wide its column came out. Starts at the narrowest width
|
|
20
|
+
* so the first paint already has the right shape.
|
|
21
|
+
*/
|
|
22
|
+
export function useCardWidth<T extends HTMLElement>(): [RefObject<T>, number] {
|
|
23
|
+
const ref = useRef<T>(null);
|
|
24
|
+
const [width, setWidth] = useState(CARD_WIDTH);
|
|
25
|
+
useLayoutEffect(() => {
|
|
26
|
+
const el = ref.current;
|
|
27
|
+
if (!el) return;
|
|
28
|
+
const measure = () => setWidth(Math.max(1, Math.floor(el.getBoundingClientRect().width)));
|
|
29
|
+
measure();
|
|
30
|
+
const observer = new ResizeObserver(measure);
|
|
31
|
+
observer.observe(el);
|
|
32
|
+
return () => observer.disconnect();
|
|
33
|
+
}, []);
|
|
34
|
+
return [ref, width];
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export function PageHeader({
|
|
38
|
+
title,
|
|
39
|
+
icon: Icon,
|
|
40
|
+
count,
|
|
41
|
+
description,
|
|
42
|
+
actions,
|
|
43
|
+
children,
|
|
44
|
+
}: {
|
|
45
|
+
title: ReactNode;
|
|
46
|
+
icon?: LucideIcon;
|
|
47
|
+
/** How many things the page lists, beside the title. */
|
|
48
|
+
count?: number;
|
|
49
|
+
description?: ReactNode;
|
|
50
|
+
/** Page-level actions, at the right of the title. */
|
|
51
|
+
actions?: ReactNode;
|
|
52
|
+
/** Controls that belong to the header, below the description. */
|
|
53
|
+
children?: ReactNode;
|
|
54
|
+
}) {
|
|
55
|
+
return (
|
|
56
|
+
<header className="mb-8">
|
|
57
|
+
<div className="flex flex-wrap items-center justify-between gap-4">
|
|
58
|
+
<div className="min-w-0">
|
|
59
|
+
<h1 className="flex items-center gap-2.5 font-medium text-lg tracking-tight">
|
|
60
|
+
{Icon && <Icon className="size-4.5 text-muted-foreground" aria-hidden />}
|
|
61
|
+
<span className="truncate">{title}</span>
|
|
62
|
+
{count !== undefined && (
|
|
63
|
+
<span className="font-mono font-normal text-muted-foreground text-xs tabular-nums">
|
|
64
|
+
{String(count).padStart(2, '0')}
|
|
65
|
+
</span>
|
|
66
|
+
)}
|
|
67
|
+
</h1>
|
|
68
|
+
{description && (
|
|
69
|
+
<p className="mt-1 max-w-2xl text-muted-foreground text-sm">{description}</p>
|
|
70
|
+
)}
|
|
71
|
+
</div>
|
|
72
|
+
{actions && <div className="flex flex-none items-center gap-2">{actions}</div>}
|
|
73
|
+
</div>
|
|
74
|
+
{children && <div className="mt-5">{children}</div>}
|
|
75
|
+
</header>
|
|
76
|
+
);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export function EmptyState({
|
|
80
|
+
icon: Icon,
|
|
81
|
+
title,
|
|
82
|
+
children,
|
|
83
|
+
}: {
|
|
84
|
+
icon: LucideIcon;
|
|
85
|
+
title: string;
|
|
86
|
+
children?: ReactNode;
|
|
87
|
+
}) {
|
|
88
|
+
return (
|
|
89
|
+
<div className="rounded-lg border border-border border-dashed px-6 py-14 text-center">
|
|
90
|
+
<Icon className="mx-auto size-6 text-muted-foreground" />
|
|
91
|
+
<p className="mt-3 font-medium text-sm">{title}</p>
|
|
92
|
+
{children && <p className="mt-1 text-muted-foreground text-xs">{children}</p>}
|
|
93
|
+
</div>
|
|
94
|
+
);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/** The two lines under a card's picture: a name that may wrap once, and one quiet line of facts. */
|
|
98
|
+
export function CardText({ title, meta }: { title: ReactNode; meta?: ReactNode }) {
|
|
99
|
+
return (
|
|
100
|
+
<div className="min-w-0">
|
|
101
|
+
<p className="line-clamp-2 font-medium text-sm leading-snug">{title}</p>
|
|
102
|
+
{meta && <p className="mt-1 truncate text-muted-foreground text-xs">{meta}</p>}
|
|
103
|
+
</div>
|
|
104
|
+
);
|
|
105
|
+
}
|
|
@@ -0,0 +1,239 @@
|
|
|
1
|
+
import type { CSSProperties, ReactNode } from 'react';
|
|
2
|
+
import { CHART_FONT_SIZE, type ChartFormat, type ChartType, layoutChart } from '../lib/chart';
|
|
3
|
+
import type { LabelKind } from '../lib/labels';
|
|
4
|
+
import { Figure } from './numbering';
|
|
5
|
+
|
|
6
|
+
export type ChartProps = {
|
|
7
|
+
type?: ChartType;
|
|
8
|
+
/** Rows as parsed from a `.csv`/`.tsv` import, or any array of objects. */
|
|
9
|
+
data: Array<Record<string, unknown>>;
|
|
10
|
+
/** The key holding each row's category — the x axis, or a slice's name. */
|
|
11
|
+
x: string;
|
|
12
|
+
/** The key, or keys, holding the values. One key per series; a pie reads the first. */
|
|
13
|
+
y: string | string[];
|
|
14
|
+
/** Legend names for the `y` keys. */
|
|
15
|
+
labels?: Record<string, string>;
|
|
16
|
+
/** Stack the series instead of grouping them (bar charts). */
|
|
17
|
+
stacked?: boolean;
|
|
18
|
+
/** How axis ticks and value labels print. */
|
|
19
|
+
format?: ChartFormat;
|
|
20
|
+
/** Print each value on its bar, point or slice. */
|
|
21
|
+
values?: boolean;
|
|
22
|
+
/** Series colours, in order. Defaults to the document's accent and tints of it. */
|
|
23
|
+
colors?: string[];
|
|
24
|
+
/** Drawn size in CSS px; the drawing scales down to the column if it is wider. */
|
|
25
|
+
width?: number;
|
|
26
|
+
height?: number;
|
|
27
|
+
/** Caption text. Given one, the chart is numbered like any other figure. */
|
|
28
|
+
caption?: ReactNode;
|
|
29
|
+
captionText?: string;
|
|
30
|
+
kind?: LabelKind;
|
|
31
|
+
id?: string;
|
|
32
|
+
style?: CSSProperties;
|
|
33
|
+
className?: string;
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
const ACCENT = 'var(--od-accent, #2563eb)';
|
|
37
|
+
const TEXT = 'var(--od-text, #16181d)';
|
|
38
|
+
const MUTED = 'var(--od-muted, #6b7280)';
|
|
39
|
+
const RULE = 'var(--od-rule, #e5e7eb)';
|
|
40
|
+
const FONT = 'var(--od-font-body, sans-serif)';
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* The accent first, then tints and shades of it and of the text colour, so a
|
|
44
|
+
* chart takes on the document's palette and changes with it — the same reason
|
|
45
|
+
* the diagram renderer draws in `--od-*` variables rather than its own colours.
|
|
46
|
+
*/
|
|
47
|
+
const SERIES = [
|
|
48
|
+
ACCENT,
|
|
49
|
+
`color-mix(in srgb, ${ACCENT} 45%, #ffffff)`,
|
|
50
|
+
`color-mix(in srgb, ${TEXT} 55%, #ffffff)`,
|
|
51
|
+
`color-mix(in srgb, ${ACCENT} 70%, #000000)`,
|
|
52
|
+
`color-mix(in srgb, ${TEXT} 28%, #ffffff)`,
|
|
53
|
+
`color-mix(in srgb, ${ACCENT} 22%, #ffffff)`,
|
|
54
|
+
];
|
|
55
|
+
|
|
56
|
+
function colorOf(series: number, colors: string[] | undefined): string {
|
|
57
|
+
return colors?.[series] ?? SERIES[series % SERIES.length] ?? ACCENT;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* A bar, line or pie chart drawn from data, in the document's own colours and
|
|
62
|
+
* type. It lays itself out synchronously into a fixed box, so it measures as
|
|
63
|
+
* it prints; the Word export places it as a picture.
|
|
64
|
+
*/
|
|
65
|
+
export function Chart({
|
|
66
|
+
type = 'bar',
|
|
67
|
+
data,
|
|
68
|
+
x,
|
|
69
|
+
y,
|
|
70
|
+
labels,
|
|
71
|
+
stacked,
|
|
72
|
+
format,
|
|
73
|
+
values,
|
|
74
|
+
colors,
|
|
75
|
+
width = 560,
|
|
76
|
+
height = type === 'pie' ? 220 : 260,
|
|
77
|
+
caption,
|
|
78
|
+
captionText,
|
|
79
|
+
kind = 'figure',
|
|
80
|
+
id,
|
|
81
|
+
style,
|
|
82
|
+
className,
|
|
83
|
+
}: ChartProps) {
|
|
84
|
+
const keys = Array.isArray(y) ? y : [y];
|
|
85
|
+
const chart = layoutChart({
|
|
86
|
+
type,
|
|
87
|
+
data,
|
|
88
|
+
x,
|
|
89
|
+
y: keys,
|
|
90
|
+
width,
|
|
91
|
+
height,
|
|
92
|
+
...(labels ? { labels } : {}),
|
|
93
|
+
...(stacked ? { stacked } : {}),
|
|
94
|
+
...(format ? { format } : {}),
|
|
95
|
+
...(values ? { values } : {}),
|
|
96
|
+
});
|
|
97
|
+
const text = { fontFamily: FONT, fontSize: CHART_FONT_SIZE } as const;
|
|
98
|
+
const summary =
|
|
99
|
+
captionText ?? (typeof caption === 'string' ? caption : `${type} chart of ${keys.join(', ')}`);
|
|
100
|
+
|
|
101
|
+
const drawing = (
|
|
102
|
+
<svg
|
|
103
|
+
viewBox={`0 0 ${chart.width} ${chart.height}`}
|
|
104
|
+
width={chart.width}
|
|
105
|
+
height={chart.height}
|
|
106
|
+
role="img"
|
|
107
|
+
aria-label={summary}
|
|
108
|
+
style={{
|
|
109
|
+
display: 'block',
|
|
110
|
+
maxWidth: '100%',
|
|
111
|
+
height: 'auto',
|
|
112
|
+
margin: '0 auto',
|
|
113
|
+
...(caption ? undefined : style),
|
|
114
|
+
}}
|
|
115
|
+
className={caption ? undefined : className}
|
|
116
|
+
>
|
|
117
|
+
{chart.ticks.map((tick) => (
|
|
118
|
+
<g key={tick.value}>
|
|
119
|
+
<line
|
|
120
|
+
x1={chart.plot.x}
|
|
121
|
+
x2={chart.plot.x + chart.plot.width}
|
|
122
|
+
y1={tick.at}
|
|
123
|
+
y2={tick.at}
|
|
124
|
+
style={{ stroke: RULE }}
|
|
125
|
+
strokeWidth={1}
|
|
126
|
+
/>
|
|
127
|
+
<text
|
|
128
|
+
x={chart.plot.x - 6}
|
|
129
|
+
y={tick.at + CHART_FONT_SIZE * 0.35}
|
|
130
|
+
textAnchor="end"
|
|
131
|
+
style={{ ...text, fill: MUTED }}
|
|
132
|
+
>
|
|
133
|
+
{tick.label}
|
|
134
|
+
</text>
|
|
135
|
+
</g>
|
|
136
|
+
))}
|
|
137
|
+
{chart.bars.map((bar) => (
|
|
138
|
+
<rect
|
|
139
|
+
key={`${bar.series}-${bar.x}-${bar.y}`}
|
|
140
|
+
x={bar.x}
|
|
141
|
+
y={bar.y}
|
|
142
|
+
width={bar.width}
|
|
143
|
+
height={bar.height}
|
|
144
|
+
style={{ fill: colorOf(bar.series, colors) }}
|
|
145
|
+
/>
|
|
146
|
+
))}
|
|
147
|
+
{chart.baseline !== null && chart.bars.length > 0 && (
|
|
148
|
+
<line
|
|
149
|
+
x1={chart.plot.x}
|
|
150
|
+
x2={chart.plot.x + chart.plot.width}
|
|
151
|
+
y1={chart.baseline}
|
|
152
|
+
y2={chart.baseline}
|
|
153
|
+
style={{ stroke: MUTED }}
|
|
154
|
+
strokeWidth={1}
|
|
155
|
+
/>
|
|
156
|
+
)}
|
|
157
|
+
{chart.lines.map((line) => (
|
|
158
|
+
<g key={line.series} style={{ stroke: colorOf(line.series, colors) }}>
|
|
159
|
+
<polyline
|
|
160
|
+
points={line.points.map((point) => `${point.x},${point.y}`).join(' ')}
|
|
161
|
+
fill="none"
|
|
162
|
+
strokeWidth={2}
|
|
163
|
+
strokeLinejoin="round"
|
|
164
|
+
/>
|
|
165
|
+
{line.points.map((point) => (
|
|
166
|
+
<circle
|
|
167
|
+
key={point.x}
|
|
168
|
+
cx={point.x}
|
|
169
|
+
cy={point.y}
|
|
170
|
+
r={3}
|
|
171
|
+
strokeWidth={1.5}
|
|
172
|
+
style={{ fill: 'var(--od-bg, #ffffff)' }}
|
|
173
|
+
/>
|
|
174
|
+
))}
|
|
175
|
+
</g>
|
|
176
|
+
))}
|
|
177
|
+
{chart.slices.map((slice) => (
|
|
178
|
+
<path
|
|
179
|
+
key={slice.series}
|
|
180
|
+
d={slice.path}
|
|
181
|
+
style={{ fill: colorOf(slice.series, colors), stroke: 'var(--od-bg, #ffffff)' }}
|
|
182
|
+
strokeWidth={1.5}
|
|
183
|
+
/>
|
|
184
|
+
))}
|
|
185
|
+
{chart.categories.map((category) => (
|
|
186
|
+
<text
|
|
187
|
+
key={category.x}
|
|
188
|
+
x={category.x}
|
|
189
|
+
y={chart.plot.y + chart.plot.height + CHART_FONT_SIZE * 1.5}
|
|
190
|
+
textAnchor="middle"
|
|
191
|
+
style={{ ...text, fill: MUTED }}
|
|
192
|
+
>
|
|
193
|
+
{category.text}
|
|
194
|
+
</text>
|
|
195
|
+
))}
|
|
196
|
+
{chart.values.map((value) => (
|
|
197
|
+
<text
|
|
198
|
+
key={`${value.x}-${value.y}-${value.text}`}
|
|
199
|
+
x={value.x}
|
|
200
|
+
y={value.y}
|
|
201
|
+
textAnchor={value.anchor}
|
|
202
|
+
style={{ ...text, fill: chart.slices.length > 0 ? '#ffffff' : TEXT }}
|
|
203
|
+
>
|
|
204
|
+
{value.text}
|
|
205
|
+
</text>
|
|
206
|
+
))}
|
|
207
|
+
{chart.legend.map((item) => (
|
|
208
|
+
<g key={item.series}>
|
|
209
|
+
<rect
|
|
210
|
+
x={item.x}
|
|
211
|
+
y={item.y - 8}
|
|
212
|
+
width={9}
|
|
213
|
+
height={9}
|
|
214
|
+
rx={2}
|
|
215
|
+
style={{ fill: colorOf(item.series, colors) }}
|
|
216
|
+
/>
|
|
217
|
+
<text x={item.x + 14} y={item.y} style={{ ...text, fill: TEXT }}>
|
|
218
|
+
{item.name}
|
|
219
|
+
</text>
|
|
220
|
+
</g>
|
|
221
|
+
))}
|
|
222
|
+
</svg>
|
|
223
|
+
);
|
|
224
|
+
|
|
225
|
+
if (!caption) return drawing;
|
|
226
|
+
|
|
227
|
+
return (
|
|
228
|
+
<Figure
|
|
229
|
+
{...(id ? { id } : {})}
|
|
230
|
+
caption={caption}
|
|
231
|
+
{...(captionText ? { captionText } : {})}
|
|
232
|
+
kind={kind}
|
|
233
|
+
className={className}
|
|
234
|
+
{...(style ? { style } : {})}
|
|
235
|
+
>
|
|
236
|
+
{drawing}
|
|
237
|
+
</Figure>
|
|
238
|
+
);
|
|
239
|
+
}
|