@betteroffice/xlsx-react 0.2.0 → 0.3.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/README.md +272 -3
- package/dist/index.d.ts +973 -50
- package/dist/index.js +1 -1
- package/package.json +4 -4
package/README.md
CHANGED
|
@@ -55,14 +55,20 @@ export function App() {
|
|
|
55
55
|
|
|
56
56
|
Without `onSave`, the save button downloads the edited bytes.
|
|
57
57
|
|
|
58
|
-
Props: `file`, `fileName`, `onSave`, `onReady` (a handle for
|
|
59
|
-
edits), `collaboration`, `i18n`,
|
|
58
|
+
Props: `file`, `fileName`, `onSave`, `onChange`, `onReady` (a handle for
|
|
59
|
+
host/agent-driven edits and the editor's `commands`), `collaboration`, `i18n`,
|
|
60
|
+
`readOnly`, `toolbar`, `showToolbar`, and `className`.
|
|
60
61
|
|
|
61
62
|
## What works today
|
|
62
63
|
|
|
63
64
|
- Cell editing with formula recalculation of dependents on every edit
|
|
64
65
|
- Editing toolbar: number formats, fonts, colors, borders, alignment, merges
|
|
66
|
+
- Composable toolbar: built-in controls, the formula bar and host actions in your
|
|
67
|
+
own order, bound to one command store
|
|
65
68
|
- Agent proposals: in-cell tracked-change ghosts plus an accept/reject panel
|
|
69
|
+
- Version-checked edit batches through `onReady`, after pending input commits
|
|
70
|
+
- Host-owned plugins with panels, overlays, lifecycle events, and explicitly
|
|
71
|
+
granted commands and edit batches
|
|
66
72
|
- TSV clipboard copy/paste
|
|
67
73
|
- Accessible grid mirroring the painted canvas for screen readers
|
|
68
74
|
- Localized UI via the `i18n` prop
|
|
@@ -70,9 +76,99 @@ edits), `collaboration`, `i18n`, and `className`.
|
|
|
70
76
|
- Real-time collaboration with people or agents; the workbook is a CRDT
|
|
71
77
|
- Live collaborator selections and presence chips, shown in each peer's color
|
|
72
78
|
|
|
79
|
+
## Compose the toolbar
|
|
80
|
+
|
|
81
|
+
Every built-in control runs through the editor's command store.
|
|
82
|
+
Replace the default toolbar with the parts you need, in your order, next to your
|
|
83
|
+
own actions:
|
|
84
|
+
|
|
85
|
+
```tsx
|
|
86
|
+
import {
|
|
87
|
+
EditorToolbar,
|
|
88
|
+
ToolbarButton,
|
|
89
|
+
ToolbarCommandButton,
|
|
90
|
+
ToolbarCommandSelect,
|
|
91
|
+
ToolbarGroup,
|
|
92
|
+
XlsxEditor,
|
|
93
|
+
} from "@betteroffice/xlsx-react";
|
|
94
|
+
|
|
95
|
+
<XlsxEditor
|
|
96
|
+
file={file}
|
|
97
|
+
toolbar={
|
|
98
|
+
<EditorToolbar mode="commands">
|
|
99
|
+
<EditorToolbar.Toolbar>
|
|
100
|
+
<ToolbarGroup label="Formatting">
|
|
101
|
+
<ToolbarCommandSelect id="numberFormat" />
|
|
102
|
+
<ToolbarCommandButton id="bold" />
|
|
103
|
+
</ToolbarGroup>
|
|
104
|
+
<ToolbarCommandButton id="undo" />
|
|
105
|
+
<ToolbarButton title="Share" onClick={share}>Share</ToolbarButton>
|
|
106
|
+
</EditorToolbar.Toolbar>
|
|
107
|
+
<EditorToolbar.FormulaBar />
|
|
108
|
+
</EditorToolbar>
|
|
109
|
+
}
|
|
110
|
+
/>;
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
- `toolbar` omitted renders the default toolbar (hidden when `readOnly`), `null`
|
|
114
|
+
renders none, and supplied chrome renders in its place, also when read-only.
|
|
115
|
+
`showToolbar={false}` hides the region either way.
|
|
116
|
+
- `<EditorToolbar.Toolbar>` children are the complete arrangement in command mode;
|
|
117
|
+
without children it renders the default controls. Place
|
|
118
|
+
`EditorToolbar.FormulaBar` inside the editor to show its name box and formula input.
|
|
119
|
+
- `ToolbarCommand` renders a command's built-in control, `ToolbarCommandButton` a
|
|
120
|
+
button (bind `args`, such as `{ value: "currency" }` for `numberFormat`), and
|
|
121
|
+
`ToolbarCommandSelect` a selector's picker. `useXlsxCommand(id, args)` and
|
|
122
|
+
`useXlsxCommandState(id, args)` bind custom controls.
|
|
123
|
+
- Outside the editor, capture `api.commands` from `onReady` into state and wrap
|
|
124
|
+
the same parts in `<XlsxCommandProvider commands={commands}>`; pass `null`
|
|
125
|
+
until the editor is ready. Its shortcuts then reach that editor.
|
|
126
|
+
- `commands.getState(id, args?)` returns serializable state (`enabled`,
|
|
127
|
+
`active`, `value`, and `options` whose entries carry their own `state`); a
|
|
128
|
+
disabled command always carries `disabledReason` with a `code` and a
|
|
129
|
+
localized `message`.
|
|
130
|
+
- `commands.execute(id, args)` runs in order with pastes, cell entries and
|
|
131
|
+
chart moves accepted before it: it ends an IME composition, writes the text
|
|
132
|
+
typed so far, then checks availability again. It resolves to
|
|
133
|
+
`{ ok: true, status }` (`executed`, `noop`, `opened`, `requested`) or
|
|
134
|
+
`{ ok: false, failure }`, for example `input-failed`, `document-replaced`,
|
|
135
|
+
`target-changed` when the selection moved while it waited, `gesture-active`
|
|
136
|
+
during a chart drag, `proposal-stale`, or `command-failed` when the change
|
|
137
|
+
could not be applied. A cell entry the workbook refuses
|
|
138
|
+
stays open at its own cell for correction; until it is corrected or discarded
|
|
139
|
+
with Escape, every command fails with `input-failed`. Print waits until the
|
|
140
|
+
canvas has painted the written text, and fails with `render-failed` when it
|
|
141
|
+
cannot.
|
|
142
|
+
- `api.save()` stays synchronous and never returns bytes without accepted
|
|
143
|
+
input: it throws `XlsxSaveRefusedError` with `code` `input-pending` while
|
|
144
|
+
entries or a paste still wait to be written, and `input-failed` while a
|
|
145
|
+
refused entry waits for correction. `await api.commands.execute("save", null)`
|
|
146
|
+
waits for that input and then saves through `onSave`. The API's version, read
|
|
147
|
+
and batch methods reject with `XlsxCommandAdmissionError` instead (see
|
|
148
|
+
[Edit batches](#edit-batches)); both errors are exported to branch on `code`.
|
|
149
|
+
- Every cell write goes through one queue. `api.selectCells`, `api.clearSelection`
|
|
150
|
+
and switching sheets close the open entry and write it at once when nothing
|
|
151
|
+
waits; otherwise it is queued behind the earlier input, so it may not have
|
|
152
|
+
landed when they return. An open entry the workbook refuses stays open and
|
|
153
|
+
they change nothing (`selectCells` returns `false`); one it refused earlier
|
|
154
|
+
reopens at its own cell once they land on its sheet. Commands run right
|
|
155
|
+
after them, even in the same handler, act on the new selection and sheet.
|
|
156
|
+
- The store enforces read-only mode, the selection a command needs and its
|
|
157
|
+
arguments for the editor's UI. `api.handle` stays unrestricted host
|
|
158
|
+
authority, and workbook protection is separate from the store.
|
|
159
|
+
- Narrow toolbars move trailing groups into a keyboard-accessible More menu. Host
|
|
160
|
+
`ToolbarButton`s get an entry there; wrap other content in `ToolbarOverflow`.
|
|
161
|
+
A group holding content without an entry stays in the row, which scrolls
|
|
162
|
+
horizontally when that content does not fit.
|
|
163
|
+
|
|
164
|
+
The prop-based `EditorToolbar`, `Toolbar` and `useEditorToolbar` API keeps
|
|
165
|
+
working unchanged in the default legacy mode and is deprecated for new
|
|
166
|
+
toolbars; in command mode `useEditorToolbar` returns a projection of the
|
|
167
|
+
command state whose callbacks run commands, and passing those props throws.
|
|
168
|
+
|
|
73
169
|
## AI agents
|
|
74
170
|
|
|
75
|
-
`onReady`
|
|
171
|
+
`onReady`'s `handle` is the `WorkbookHandle`. An agent stages edits with
|
|
76
172
|
`propose()` instead of applying them; the editor paints per-cell ghosts and a
|
|
77
173
|
review panel where the human accepts or rejects. The full proposal API lives in
|
|
78
174
|
[`@betteroffice/xlsx`](https://www.npmjs.com/package/@betteroffice/xlsx).
|
|
@@ -93,6 +189,179 @@ import type { XlsxEditorApi } from "@betteroffice/xlsx-react";
|
|
|
93
189
|
/>;
|
|
94
190
|
```
|
|
95
191
|
|
|
192
|
+
## Edit batches
|
|
193
|
+
|
|
194
|
+
The `onReady` API's `version`, `readCells`, `findText`, `validateEdits` and
|
|
195
|
+
`applyEdits` run in the same queue as commands: cell and formula entries, chart
|
|
196
|
+
nudges, pastes and cuts accepted before them land first, and an IME composition
|
|
197
|
+
ends with its text written. When that cannot happen they reject with
|
|
198
|
+
`XlsxCommandAdmissionError`, whose `code` is `input-failed` while a refused entry
|
|
199
|
+
waits for correction, `gesture-active` during a chart drag, and
|
|
200
|
+
`document-replaced` when the workbook is replaced meanwhile. Refusals of the
|
|
201
|
+
batch itself stay data. `applyEdits` keeps the
|
|
202
|
+
caller's `expectVersion`, so input that lands first refuses the batch with
|
|
203
|
+
`stale-version`; read the version through the API to include it. While
|
|
204
|
+
`readOnly`, writes refuse with `read-only`. An applied batch repaints once and
|
|
205
|
+
calls `onChange` once.
|
|
206
|
+
|
|
207
|
+
```tsx
|
|
208
|
+
import { XlsxCommandAdmissionError, XlsxEditor, type XlsxEditorApi } from "@betteroffice/xlsx-react";
|
|
209
|
+
|
|
210
|
+
<XlsxEditor
|
|
211
|
+
file={file}
|
|
212
|
+
onReady={(api: XlsxEditorApi) => {
|
|
213
|
+
void (async () => {
|
|
214
|
+
const version = await api.version();
|
|
215
|
+
const result = await api.applyEdits({
|
|
216
|
+
expectVersion: version,
|
|
217
|
+
steps: [
|
|
218
|
+
{
|
|
219
|
+
op: "setCellInputs",
|
|
220
|
+
target: { sheetId: "sheet:0", range: { kind: "a1", a1: "B3" } },
|
|
221
|
+
inputs: [["120"]],
|
|
222
|
+
},
|
|
223
|
+
],
|
|
224
|
+
});
|
|
225
|
+
if (!result.ok) console.warn(result.failure.code);
|
|
226
|
+
})().catch((error: unknown) => {
|
|
227
|
+
if (error instanceof XlsxCommandAdmissionError) console.warn(error.code);
|
|
228
|
+
else throw error;
|
|
229
|
+
});
|
|
230
|
+
}}
|
|
231
|
+
/>;
|
|
232
|
+
```
|
|
233
|
+
|
|
234
|
+
See [`@betteroffice/xlsx`](https://www.npmjs.com/package/@betteroffice/xlsx) for
|
|
235
|
+
the step vocabulary and its limits.
|
|
236
|
+
|
|
237
|
+
## Host plugins
|
|
238
|
+
|
|
239
|
+
Host-owned tools (review aids, checks, templates) install through the `plugins`
|
|
240
|
+
prop. A plugin contributes a docked panel, an overlay on the grid and commands,
|
|
241
|
+
and works through restricted clients rather than the editor API or the workbook
|
|
242
|
+
handle:
|
|
243
|
+
|
|
244
|
+
```tsx
|
|
245
|
+
import { XlsxEditor, defineXlsxPlugin } from "@betteroffice/xlsx-react";
|
|
246
|
+
|
|
247
|
+
type State = { version: string | null; sheets: number };
|
|
248
|
+
|
|
249
|
+
const review = defineXlsxPlugin<State>({
|
|
250
|
+
id: "acme.review",
|
|
251
|
+
createState: () => ({ version: null, sheets: 0 }),
|
|
252
|
+
async onEvent(context, event) {
|
|
253
|
+
if (event.type !== "load" && event.type !== "document-change") return;
|
|
254
|
+
const read = await context.read.readCells({ ranges: [] });
|
|
255
|
+
if (read.ok) context.setState({ version: read.version, sheets: read.sheets.length }, read.version);
|
|
256
|
+
},
|
|
257
|
+
panel: {
|
|
258
|
+
title: "Review",
|
|
259
|
+
placement: "right",
|
|
260
|
+
render: ({ context }) => <p>{context.state.sheets} sheets</p>,
|
|
261
|
+
},
|
|
262
|
+
overlay: ({ context, geometry }) => {
|
|
263
|
+
const selection = context.snapshot.selection;
|
|
264
|
+
const focus = selection?.cells?.focus;
|
|
265
|
+
const box = selection && focus ? geometry.getCellRect({ sheetId: selection.sheetId, ...focus }) : null;
|
|
266
|
+
return box ? (
|
|
267
|
+
<div style={{ position: "absolute", left: box.x, top: box.y, width: box.width, height: box.height, outline: "2px dashed #2563eb" }} />
|
|
268
|
+
) : null;
|
|
269
|
+
},
|
|
270
|
+
});
|
|
271
|
+
|
|
272
|
+
<XlsxEditor
|
|
273
|
+
file={bytes}
|
|
274
|
+
plugins={[review]}
|
|
275
|
+
pluginGrants={{ "acme.review": { document: "write", editBatches: true } }}
|
|
276
|
+
onPluginError={(error) => report(error)}
|
|
277
|
+
/>;
|
|
278
|
+
```
|
|
279
|
+
|
|
280
|
+
- **Lifecycle.** Once a workbook is open, each plugin gets fresh state,
|
|
281
|
+
`initialize`, then one `load` event (`loaded`, `replaced`, or `attached` for a
|
|
282
|
+
plugin added to an open workbook), and its contributions appear. A workbook
|
|
283
|
+
change the hook did not make itself aborts it and delivers `load` again; after
|
|
284
|
+
ten such runs the plugin is stopped and reported. It then receives
|
|
285
|
+
`document-change` (the committed version after recalculation, for typing,
|
|
286
|
+
pastes, commands, batches, its own included, undo, redo and remote updates,
|
|
287
|
+
never for refusals or no-ops), `selection-change`, `mode-change` (with
|
|
288
|
+
`readOnly`), `layout-change` and `grants-change`. Events describe current
|
|
289
|
+
state: several changes may arrive as one, and a newer one aborts the hook
|
|
290
|
+
still handling the previous (`context.signal`), except a change that hook's
|
|
291
|
+
own edit batch made. Replacing the workbook, removing the plugin, changing its `revision`, unmounting, or a
|
|
292
|
+
failure ends the activation: its signals abort, its clients refuse, and every
|
|
293
|
+
`onCleanup` disposer runs once with the reason. Plugins are matched by `id`
|
|
294
|
+
and `revision`, so new array or callback identities and reordering keep their
|
|
295
|
+
state. `defineXlsxPlugin` copies the panel, commands and toolbar, so changing
|
|
296
|
+
them takes a new definition. `context.run(action)` gives event handlers a
|
|
297
|
+
fresh context and isolates their failures. `onReady` is unaffected by plugins.
|
|
298
|
+
- **Stale results.** `setState` returns false once the context is superseded or
|
|
299
|
+
ended, or when the workbook is no longer at `atVersion` (by default the
|
|
300
|
+
version the context was created at).
|
|
301
|
+
- **Reads and edits.** `context.read` offers `version`, `readCells`, `findText`
|
|
302
|
+
and `validateEdits`, and `context.edits.applyEdits` the editor's
|
|
303
|
+
version-checked batches. Each call waits for pending input in order with it,
|
|
304
|
+
but plugin handlers run outside that queue, so a command can await its own
|
|
305
|
+
batch.
|
|
306
|
+
- **Grants.** Without a grant a plugin reads, validates and navigates only, and
|
|
307
|
+
`context.edits` is null. Built-in commands need their id in `commands`, and
|
|
308
|
+
mutating ones also `document: "write"`; edit batches need `document: "write"`
|
|
309
|
+
and `editBatches`, and `history: "none"` also `untrackedHistory`. The grant
|
|
310
|
+
and `readOnly` are checked again right before each change, so a revoked grant,
|
|
311
|
+
`readOnly` or a replaced workbook refuses even through a client obtained
|
|
312
|
+
earlier. Mutating built-in commands refuse plugins with `unsupported-policy`;
|
|
313
|
+
plugins change workbooks through edit batches. Grants govern the plugin API,
|
|
314
|
+
separately from sheet and workbook protection.
|
|
315
|
+
- **Contributed commands** register as `plugin:<pluginId>/<id>` on
|
|
316
|
+
`api.commands`. They always run with their own plugin's clients, even when the
|
|
317
|
+
toolbar, a shortcut or the host invokes them. `execute` returns
|
|
318
|
+
`{ ok: true, status }` or a failure with the plugin's own code, or a refused
|
|
319
|
+
edit batch as-is; callers receive it unchanged. `mutatesDocument` disables a
|
|
320
|
+
command while read-only but grants nothing. Plugin shortcuts must use Mod or
|
|
321
|
+
Alt, or a function key; built-in shortcuts win and a clashing plugin shortcut
|
|
322
|
+
is reported. `toolbar` lists local ids in
|
|
323
|
+
order: the default toolbar shows them, and replacement chrome places
|
|
324
|
+
`<XlsxPluginToolbar />` where it wants them. `ToolbarCommandButton`,
|
|
325
|
+
`ToolbarCommand`, `useXlsxCommand` and `useXlsxCommandState` also take a
|
|
326
|
+
contributed id.
|
|
327
|
+
- **Selection and navigation.** `snapshot.selection` names the active sheet
|
|
328
|
+
(`sheetId` and the zero-based `sheetIndex`), the selected `cells` with their
|
|
329
|
+
`anchor` and `focus` direction, and the `chartId` of a selected chart.
|
|
330
|
+
`navigation.selectCells({ sheetId, selection }, { expectVersion, focus })`
|
|
331
|
+
activates the sheet, selects the cells and reveals the focus cell.
|
|
332
|
+
`navigation.scrollToCell({ sheetId, row, col }, { expectVersion, align })`
|
|
333
|
+
reveals a cell (`nearest` by default, `start` or `center`) and keeps the
|
|
334
|
+
selection; a cell on another sheet activates it with nothing selected. Both
|
|
335
|
+
resolve sheet ids after pending input against `expectVersion`, keep keyboard
|
|
336
|
+
focus unless `selectCells` gets `focus: true`, and refuse with
|
|
337
|
+
`stale-version` or `missing-target` rather than retarget. Sheet ids and
|
|
338
|
+
versions are session-scoped.
|
|
339
|
+
- **Geometry.** `context.geometry` exists only while the canvas shows a painted
|
|
340
|
+
frame of the current version. `layout` carries the frame's `sheetId`, `zoom`
|
|
341
|
+
and painted `viewport` (unzoomed sheet pixels), and gets a new `id` with every
|
|
342
|
+
scroll, zoom, resize and sheet switch. `getCellRect({ sheetId, row, col })`
|
|
343
|
+
and `getRangeRect({ sheetId, range })` return pixels of the overlay layer,
|
|
344
|
+
zoomed and clipped to the visible grid with frozen panes placed as painted;
|
|
345
|
+
cells scrolled out of view or on another sheet return null. `getCellRect` is
|
|
346
|
+
one grid cell; address a merged area as a range. `getPositionAtPoint` is null;
|
|
347
|
+
check it before calling. Every method returns null once its layout is gone. The overlay layer sits on the grid below the editor's
|
|
348
|
+
selection and cell editor, and ignores the pointer unless an element sets
|
|
349
|
+
`pointer-events: auto`.
|
|
350
|
+
- **Panels** dock left, right or bottom of the grid, below the toolbar and
|
|
351
|
+
above the sheet tabs, with tabs when several share a side. `preferredSize` is
|
|
352
|
+
clamped to 40% of the workspace. In a narrow editor side docks show their
|
|
353
|
+
tabs, and a tab opens its panel as a drawer that Escape closes. Collapsing a
|
|
354
|
+
panel keeps the plugin running. The proposals panel opens inside the grid
|
|
355
|
+
area.
|
|
356
|
+
- **Failures.** Every contribution renders behind its own error boundary with a
|
|
357
|
+
command context restricted to its plugin. Keys pressed in plugin chrome are
|
|
358
|
+
the user's: built-in shortcuts act on the editor there, a plugin's text fields
|
|
359
|
+
keep their own editing keys, and no key or click in a contribution reaches the
|
|
360
|
+
grid. A throwing hook, renderer, command-state function, command or
|
|
361
|
+
`context.run` action stops only that plugin, runs its cleanups and reports
|
|
362
|
+
`{ pluginId, generation, phase, error }` to `onPluginError`. Plugins run in
|
|
363
|
+
the page's realm: grants govern the supported API, not a sandbox.
|
|
364
|
+
|
|
96
365
|
## Collaboration
|
|
97
366
|
|
|
98
367
|
Pass `collaboration` to open a network-ready replica, then attach a transport
|