@betteroffice/pptx-react 0.1.0 → 0.2.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 +258 -10
- package/dist/index.d.ts +976 -46
- package/dist/index.js +4 -4
- package/package.json +4 -4
package/README.md
CHANGED
|
@@ -39,20 +39,270 @@ for metric-compatible open faces.
|
|
|
39
39
|
|
|
40
40
|
Click text to place the Rust-computed caret, type to edit the yrs story and
|
|
41
41
|
trigger Rust reflow, drag or resize shapes on the canvas, or use the toolbar
|
|
42
|
-
for bold, italic, size, color, slides, and
|
|
42
|
+
for bold, italic, size, color, slides, text boxes, image insertion, and shape ordering.
|
|
43
43
|
|
|
44
44
|
Props: `file`, `fonts`, `collaboration`, `i18n`, `className`, `fileName`,
|
|
45
|
-
`onReady` (exposes the core `PresentationHandle`,
|
|
46
|
-
host-driven edits, `refreshProposals`, and `save`),
|
|
47
|
-
`onSave` (receives the saved bytes;
|
|
45
|
+
`onReady` (exposes the core `PresentationHandle`, the editor's `commands`, a
|
|
46
|
+
`refresh` callback for host-driven edits, `refreshProposals`, and `save`),
|
|
47
|
+
`onChange` (deck snapshots), `onError`, `onSave` (receives the saved bytes;
|
|
48
|
+
without it, saving downloads the file), `readOnly`, `toolbar`, and `showToolbar`.
|
|
49
|
+
|
|
50
|
+
## Compose the toolbar
|
|
51
|
+
|
|
52
|
+
Every built-in control runs through one command store per editor,
|
|
53
|
+
`api.commands`. Hosts arrange the same controls with their own actions:
|
|
54
|
+
|
|
55
|
+
```tsx
|
|
56
|
+
import {
|
|
57
|
+
EditorToolbar,
|
|
58
|
+
PptxEditor,
|
|
59
|
+
ToolbarButton,
|
|
60
|
+
ToolbarCommandButton,
|
|
61
|
+
ToolbarCommandSelect,
|
|
62
|
+
ToolbarGroup,
|
|
63
|
+
} from '@betteroffice/pptx-react';
|
|
64
|
+
|
|
65
|
+
<PptxEditor
|
|
66
|
+
file={file}
|
|
67
|
+
fonts={fonts}
|
|
68
|
+
toolbar={
|
|
69
|
+
<EditorToolbar mode="commands">
|
|
70
|
+
<EditorToolbar.Toolbar>
|
|
71
|
+
<ToolbarGroup label="Text">
|
|
72
|
+
<ToolbarCommandSelect id="fontFamily" />
|
|
73
|
+
<ToolbarCommandButton id="bold" />
|
|
74
|
+
</ToolbarGroup>
|
|
75
|
+
<ToolbarCommandButton id="undo" />
|
|
76
|
+
<ToolbarCommandButton id="slideshow" />
|
|
77
|
+
<ToolbarButton title="Share" onClick={share}>Share</ToolbarButton>
|
|
78
|
+
</EditorToolbar.Toolbar>
|
|
79
|
+
</EditorToolbar>
|
|
80
|
+
}
|
|
81
|
+
/>;
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
- **Toolbar region.** `showToolbar={false}` hides it; otherwise omitting
|
|
85
|
+
`toolbar` keeps the default controls (read-only shows only the Present
|
|
86
|
+
button), `null` removes them, and supplied chrome replaces them, also while
|
|
87
|
+
`readOnly`. Collaborator chips stay beside supplied chrome.
|
|
88
|
+
- **Outside the editor.** Capture `api.commands` from `onReady` into state and
|
|
89
|
+
render the parts under `<PptxCommandProvider commands={commands}>`;
|
|
90
|
+
`commands={null}` reports the editor as unavailable until it is ready. Such
|
|
91
|
+
chrome follows the editor's locale, and shortcuts pressed inside it reach the
|
|
92
|
+
editor.
|
|
93
|
+
- **Command contract.** `getState(id, args?)` returns serializable state
|
|
94
|
+
(`enabled`, `active` with `'mixed'`, `value`, and `options` that each carry
|
|
95
|
+
their own `state`); a disabled state
|
|
96
|
+
always carries `disabledReason: { code, message }`, for example
|
|
97
|
+
`text-selection-required`, `shape-required`, `z-order-boundary`,
|
|
98
|
+
`review-active` or `proposal-stale`. `getDescriptor(id)` lists the label key
|
|
99
|
+
and shortcuts, which labels show per platform (`Ctrl+B`, `⌘B`).
|
|
100
|
+
`usePptxCommand` and `usePptxCommandState` bind custom controls.
|
|
101
|
+
- **Ordering.** `execute(id, args)` waits for input accepted before it, such as
|
|
102
|
+
a picture still decoding, keeps later typing behind it, checks availability
|
|
103
|
+
again and resolves to `{ ok: true, status }` or `{ ok: false, failure }`
|
|
104
|
+
(`input-failed`, `document-replaced`, `target-changed`,
|
|
105
|
+
`gesture-active`, `command-failed`). Keystrokes typed meanwhile land in the
|
|
106
|
+
text they were typed into, and a picture on the slide the picker opened on;
|
|
107
|
+
input queued for a replaced document is dropped. Save runs the host's `onSaveRequest` outside that queue, so the request
|
|
108
|
+
may await `flushPendingInput()`.
|
|
109
|
+
- **Authority.** Commands enforce read-only mode and the other gates for the UI.
|
|
110
|
+
`api.handle` stays direct, unrestricted host access; call `api.refresh()`
|
|
111
|
+
after using it.
|
|
112
|
+
- **Overflow.** At narrow widths trailing groups move into a More menu with
|
|
113
|
+
arrow, Home/End, typeahead and submenu navigation. Built-in controls keep all
|
|
114
|
+
their choices there; host buttons and dropdowns get entries automatically,
|
|
115
|
+
and `ToolbarOverflow` gives other content one. A group holding content
|
|
116
|
+
without an entry stays in the row, which scrolls horizontally when that
|
|
117
|
+
content does not fit.
|
|
118
|
+
- **Compatibility.** The prop-based `EditorToolbar`, `Toolbar`,
|
|
119
|
+
`EditorToolbarContext` and `useEditorToolbar` still work and are deprecated:
|
|
120
|
+
without `mode` they bind to their props, host children follow the default
|
|
121
|
+
controls, and command-mode chrome rejects those props. Inside
|
|
122
|
+
`EditorToolbar mode="commands"`, `useEditorToolbar()` returns a view of the
|
|
123
|
+
commands whose callbacks run them; mixed marks read as `undefined`.
|
|
124
|
+
|
|
125
|
+
## Host editing controls
|
|
126
|
+
|
|
127
|
+
`onSaveRequest` runs for toolbar and Ctrl/Cmd+S saves before serialization.
|
|
128
|
+
Return `true` to continue built-in saving; `false` or `void` handles or cancels
|
|
129
|
+
it. Promises are awaited and concurrent requests are coalesced. `onSave` still
|
|
130
|
+
receives the resulting bytes when built-in saving continues. A request waiting
|
|
131
|
+
on a replaced or closed document is discarded.
|
|
132
|
+
|
|
133
|
+
The API received by `onReady` exposes `flushPendingInput(): Promise<void>` and
|
|
134
|
+
the `commands` store described above.
|
|
135
|
+
Await it before inspecting or mutating the core from a host workflow, then call
|
|
136
|
+
`api.save()` for explicit serialization without re-entering `onSaveRequest`.
|
|
137
|
+
`save()` remains synchronous and rejects while asynchronous input is pending.
|
|
138
|
+
Flush rejects stale document handles, failed input, and unfinished pointer
|
|
139
|
+
gestures. Finish or cancel the gesture before retrying.
|
|
140
|
+
|
|
141
|
+
PPTX keyboard and notes edits are synchronous; flushing also waits for accepted
|
|
142
|
+
image imports. A failed import rejects a flush waiting for it and is reported
|
|
143
|
+
through `onError`; it does not block later saves of the existing presentation.
|
|
144
|
+
|
|
145
|
+
`api.getPositionAtPoint(clientX, clientY)` returns a shape or text hit with
|
|
146
|
+
`slide` (1-based), `slideId`, and `shapeId`; text hits include `storyId` and the
|
|
147
|
+
UTF-16 `position`. It uses the current slide and canvas bounds without changing
|
|
148
|
+
focus or selection. Outside content, unavailable geometry, stale handles, and
|
|
149
|
+
proposal previews return `null`. Use `api.refresh()` after core mutations.
|
|
150
|
+
|
|
151
|
+
`api.readContent()`, `api.findText()`, `api.validateEdits()` and
|
|
152
|
+
`api.applyEdits()` run the core's version-checked edit batches after flushing
|
|
153
|
+
pending input, and `api.version()` returns the flushed session version:
|
|
154
|
+
|
|
155
|
+
```ts
|
|
156
|
+
const read = await api.readContent();
|
|
157
|
+
if (read.ok) {
|
|
158
|
+
const result = await api.applyEdits({
|
|
159
|
+
expectVersion: read.version,
|
|
160
|
+
steps: [{ op: 'setSlideNotes', target: { slideId: read.slides[0].id }, text: 'Opening' }],
|
|
161
|
+
});
|
|
162
|
+
if (!result.ok) console.warn(result.failure.code);
|
|
163
|
+
}
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
An applied batch refreshes the slides, history and proposals once and calls
|
|
167
|
+
`onChange`; refusals and no-op batches refresh nothing. A refusal never rolls
|
|
168
|
+
back input the flush committed, so an edit that lands between the read and the
|
|
169
|
+
batch refuses with `stale-version`. Read-only editors refuse `validateEdits` and
|
|
170
|
+
`applyEdits` with `read-only`, and the promises reject when the presentation is
|
|
171
|
+
replaced while input flushes.
|
|
172
|
+
|
|
173
|
+
## Host plugins
|
|
174
|
+
|
|
175
|
+
Host-owned tools (review aids, checks, templates) install through the `plugins`
|
|
176
|
+
prop. A plugin contributes a docked panel, an overlay and commands, and works
|
|
177
|
+
through restricted clients rather than the editor API or the presentation
|
|
178
|
+
handle:
|
|
179
|
+
|
|
180
|
+
```tsx
|
|
181
|
+
import { PptxEditor, definePptxPlugin } from '@betteroffice/pptx-react';
|
|
182
|
+
|
|
183
|
+
type State = { version: string | null; slides: number };
|
|
184
|
+
|
|
185
|
+
const review = definePptxPlugin<State>({
|
|
186
|
+
id: 'acme.review',
|
|
187
|
+
createState: () => ({ version: null, slides: 0 }),
|
|
188
|
+
async onEvent(context, event) {
|
|
189
|
+
if (event.type !== 'load' && event.type !== 'document-change') return;
|
|
190
|
+
const read = await context.read.readContent();
|
|
191
|
+
if (read.ok) context.setState({ version: read.version, slides: read.slides.length }, read.version);
|
|
192
|
+
},
|
|
193
|
+
panel: {
|
|
194
|
+
title: 'Review',
|
|
195
|
+
placement: 'right',
|
|
196
|
+
render: ({ context }) => <p>{context.state.slides} slides</p>,
|
|
197
|
+
},
|
|
198
|
+
overlay: ({ context, geometry }) => {
|
|
199
|
+
const target = context.snapshot.selection?.target;
|
|
200
|
+
const box = target && target.kind !== 'slide' ? geometry.getShapeRect(target.shapeId) : null;
|
|
201
|
+
return box ? (
|
|
202
|
+
<div style={{ position: 'absolute', left: box.x, top: box.y, width: box.width, height: box.height, outline: '2px dashed #2563eb' }} />
|
|
203
|
+
) : null;
|
|
204
|
+
},
|
|
205
|
+
});
|
|
206
|
+
|
|
207
|
+
<PptxEditor
|
|
208
|
+
file={bytes}
|
|
209
|
+
fonts={fonts}
|
|
210
|
+
plugins={[review]}
|
|
211
|
+
pluginGrants={{ 'acme.review': { document: 'write', editBatches: true } }}
|
|
212
|
+
onPluginError={(error) => report(error)}
|
|
213
|
+
/>;
|
|
214
|
+
```
|
|
215
|
+
|
|
216
|
+
- **Lifecycle.** Once a presentation is open, each plugin gets fresh state,
|
|
217
|
+
`initialize`, then one `load` event (`loaded`, `replaced`, or `attached` for a
|
|
218
|
+
plugin added to an open presentation), and its contributions appear. A
|
|
219
|
+
presentation change the hook did not make itself aborts it and delivers
|
|
220
|
+
`load` again; after ten such runs the plugin is stopped and reported. It then
|
|
221
|
+
receives `document-change` (the committed version only, for typing, commands,
|
|
222
|
+
batches, its own included, undo, redo and remote updates, never for refusals
|
|
223
|
+
or no-ops), `selection-change`, `mode-change` (with `readOnly`),
|
|
224
|
+
`layout-change` and `grants-change`. Events describe current state: several
|
|
225
|
+
changes may arrive as one, and a newer one aborts the hook still handling the
|
|
226
|
+
previous (`context.signal`), except a change that hook's own edit batch made.
|
|
227
|
+
Replacing the presentation, removing the plugin, changing its `revision`,
|
|
228
|
+
unmounting, or a failure ends the activation: its signals abort, its clients
|
|
229
|
+
refuse, and every `onCleanup` disposer runs once with the reason. Plugins are
|
|
230
|
+
matched by `id` and `revision`, so new array or callback identities and
|
|
231
|
+
reordering keep their state. `definePptxPlugin` copies the panel, commands
|
|
232
|
+
and toolbar, so changing them takes a new definition. `context.run(action)`
|
|
233
|
+
gives event handlers a fresh context and isolates their failures. `onReady` is
|
|
234
|
+
unaffected by plugins.
|
|
235
|
+
- **Stale results.** `setState` returns false once the context is superseded or
|
|
236
|
+
ended, or when the presentation is no longer at `atVersion` (by default the
|
|
237
|
+
version the context was created at).
|
|
238
|
+
- **Reads and edits.** `context.read` offers `version`, `readContent`,
|
|
239
|
+
`findText` and `validateEdits`, and `context.edits.applyEdits` the editor's
|
|
240
|
+
version-checked batches. Each call waits for pending input in order with it,
|
|
241
|
+
but plugin handlers run outside that queue, so a command can await its own
|
|
242
|
+
batch.
|
|
243
|
+
- **Grants.** Without a grant a plugin reads, validates and navigates only, and
|
|
244
|
+
`context.edits` is null. Built-in commands need their id in `commands`, and
|
|
245
|
+
mutating ones also `document: 'write'`; edit batches need `document: 'write'`
|
|
246
|
+
and `editBatches`, and `history: 'none'` also `untrackedHistory`. The grant
|
|
247
|
+
and `readOnly` are checked again right before each change, so a revoked grant,
|
|
248
|
+
`readOnly` or a replaced presentation refuses even through a client obtained
|
|
249
|
+
earlier. Mutating built-in commands refuse plugins with `unsupported-policy`;
|
|
250
|
+
plugins change presentations through edit batches.
|
|
251
|
+
- **Contributed commands** register as `plugin:<pluginId>/<id>` on
|
|
252
|
+
`api.commands`. They always run with their own plugin's clients, even when the
|
|
253
|
+
toolbar, a shortcut or the host invokes them. `execute` returns
|
|
254
|
+
`{ ok: true, status }` or a failure with the plugin's own code, or a refused
|
|
255
|
+
edit batch as-is; callers receive it unchanged. `mutatesDocument` disables a
|
|
256
|
+
command while read-only but grants nothing. Plugin shortcuts must use Mod or
|
|
257
|
+
Alt, or a function key; built-in and clipboard shortcuts win, and a clashing
|
|
258
|
+
plugin shortcut is not bound and is reported. `toolbar` lists local ids in
|
|
259
|
+
order: the default toolbar shows them, and replacement chrome places
|
|
260
|
+
`<PptxPluginToolbar />` where it wants them. `ToolbarCommandButton`,
|
|
261
|
+
`ToolbarCommand`, `usePptxCommand` and `usePptxCommandState` also take a
|
|
262
|
+
contributed id.
|
|
263
|
+
- **Selection and navigation.** `snapshot.selection` names the current slide
|
|
264
|
+
(`slideId` and the one-based `slide`) and a slide, shape or text target; text
|
|
265
|
+
keeps its `anchor` and `focus` direction in UTF-16 story offsets.
|
|
266
|
+
`navigation.goToSlide`, `selectShape` and `selectText` resolve session ids
|
|
267
|
+
after pending input against `expectVersion`, keep keyboard focus unless
|
|
268
|
+
`focus: true`, and refuse with `stale-version`, `missing-target`,
|
|
269
|
+
`layout-unavailable` or `unsupported` (during proposal review) rather than
|
|
270
|
+
retarget. Ids are session-scoped.
|
|
271
|
+
- **Geometry.** `context.geometry` describes the painted current version, and is
|
|
272
|
+
`null` during proposal review.
|
|
273
|
+
`layout.width` and `height` are the unzoomed slide in display-list pixels and
|
|
274
|
+
`layout.zoom` the resolved scale, fit included. `geometry.toOverlayRect` takes
|
|
275
|
+
a `slide-emu` (9,525 EMU per pixel) or `slide-px` rectangle and returns pixels
|
|
276
|
+
of the unscaled overlay layer, which sits on the slide canvas below selection
|
|
277
|
+
handles and proposal controls and ignores the pointer unless an element sets
|
|
278
|
+
`pointer-events: auto`. `getShapeRect(shapeId)` returns the rendered bounds of
|
|
279
|
+
a shape and its group descendants, and `getPositionAtPoint` adds `version` and
|
|
280
|
+
`layoutId` to a hit. Every method returns null once its layout is gone.
|
|
281
|
+
- **Panels** dock left, right or bottom of the slide, beside the thumbnail rail
|
|
282
|
+
and outside the slide's keyboard handling, with tabs when several share a
|
|
283
|
+
side. `preferredSize` is clamped to 40% of the workspace. In a narrow editor
|
|
284
|
+
side docks show their tabs, and a tab opens its panel as a drawer that Escape
|
|
285
|
+
closes. Collapsing a panel keeps the plugin running.
|
|
286
|
+
- **Failures.** Every contribution renders behind its own error boundary with a
|
|
287
|
+
command context restricted to its plugin. Keys pressed in plugin chrome are
|
|
288
|
+
the user's: built-in shortcuts act on the editor there, and a plugin's text
|
|
289
|
+
fields keep their own editing keys. A throwing hook, renderer,
|
|
290
|
+
command-state function, command or `context.run` action stops only that
|
|
291
|
+
plugin, runs its cleanups and reports `{ pluginId, generation, phase, error }`
|
|
292
|
+
to `onPluginError`. Plugins run in the page's realm: grants govern the
|
|
293
|
+
supported API, not a sandbox.
|
|
48
294
|
|
|
49
295
|
## What works today
|
|
50
296
|
|
|
51
297
|
- Slide rendering with Rust layout and text shaping, painted onto canvas
|
|
52
298
|
- Canvas interactions: shape selection, drag, and resize
|
|
53
299
|
- Text editing with caret and selection computed by the engine
|
|
54
|
-
- Slide management (add, delete), text boxes, undo/redo
|
|
300
|
+
- Slide management (add, delete), text boxes, image insertion, shape ordering, undo/redo
|
|
55
301
|
- Saving the deck back to `.pptx` — toolbar button, Ctrl/Cmd+S, or `api.save()`
|
|
302
|
+
- A command store behind every control, with composable toolbar parts and an
|
|
303
|
+
accessible overflow menu
|
|
304
|
+
- Host-owned plugins with panels, overlays, lifecycle events, and explicitly
|
|
305
|
+
granted commands and edit batches
|
|
56
306
|
- Localized UI via the `i18n` prop
|
|
57
307
|
([`@betteroffice/pptx-i18n`](https://www.npmjs.com/package/@betteroffice/pptx-i18n))
|
|
58
308
|
- Real-time collaboration with people or agents; the deck is a CRDT
|
|
@@ -98,9 +348,8 @@ step. Rejecting it leaves the deck untouched. If a target changed, preview its
|
|
|
98
348
|
current state before choosing **Apply updated proposal**. A further target
|
|
99
349
|
change detected at that click refreshes the preview for another review.
|
|
100
350
|
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
using `api.refresh()`, which also refreshes the proposal list.
|
|
351
|
+
Accepted proposals are saved and synchronized. `api.refresh()` also refreshes the
|
|
352
|
+
proposal list.
|
|
104
353
|
|
|
105
354
|
## Collaboration
|
|
106
355
|
|
|
@@ -130,8 +379,7 @@ import { CollaborationProvider } from '@betteroffice/pptx';
|
|
|
130
379
|
|
|
131
380
|
## Framework notes
|
|
132
381
|
|
|
133
|
-
|
|
134
|
-
`next/dynamic` and `ssr: false`.
|
|
382
|
+
Under Next.js, load the editor with `next/dynamic` and `ssr: false`.
|
|
135
383
|
|
|
136
384
|
[JavaScript guide](https://docs.betteroffice.dev/docs/javascript) ·
|
|
137
385
|
[Changelog](https://github.com/openooxml/betteroffice/blob/main/packages/pptx-react/CHANGELOG.md) · Apache-2.0.
|