@wae/open-word 0.0.0-stage → 0.0.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 +37 -2
- package/dist/cli.d.ts +10 -0
- package/dist/cli.js +608 -0
- package/dist/errors.d.ts +49 -0
- package/dist/errors.js +53 -0
- package/dist/formats.d.ts +46 -0
- package/dist/formats.js +54 -0
- package/dist/gui.d.ts +53 -0
- package/dist/gui.js +90 -0
- package/dist/index.d.ts +16 -0
- package/dist/index.js +12 -0
- package/dist/native.d.ts +38 -0
- package/dist/native.js +55 -0
- package/dist/open-word.d.ts +99 -0
- package/dist/open-word.js +269 -0
- package/dist/service.d.ts +45 -0
- package/dist/service.js +105 -0
- package/dist/smoke.test.d.ts +10 -0
- package/dist/smoke.test.js +209 -0
- package/dist/types.d.ts +521 -0
- package/dist/types.js +11 -0
- package/locales/en-US/cli.json +55 -0
- package/locales/locales.json +7 -0
- package/package.json +57 -4
|
@@ -0,0 +1,269 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `new OpenWord(options?)` authoring session.
|
|
3
|
+
*
|
|
4
|
+
* The constructor is synchronous and opens nothing: no network, no disk scan,
|
|
5
|
+
* no window, no file. Native initialisation and imports happen inside the async
|
|
6
|
+
* methods, so a failure is always awaited and always carries a stable code.
|
|
7
|
+
*/
|
|
8
|
+
import { OpenWordError } from './errors.js';
|
|
9
|
+
import * as service from './service.js';
|
|
10
|
+
/** Authoring session. One instance manages one declared document surface. */
|
|
11
|
+
export class OpenWord {
|
|
12
|
+
/** BCP-47 tag used for diagnostics and default copy. */
|
|
13
|
+
locale;
|
|
14
|
+
closed = false;
|
|
15
|
+
active = null;
|
|
16
|
+
handles = new Set();
|
|
17
|
+
constructor(options = {}) {
|
|
18
|
+
this.locale = options.locale ?? 'en';
|
|
19
|
+
}
|
|
20
|
+
/** Whether {@link dispose} has run. */
|
|
21
|
+
get disposed() {
|
|
22
|
+
return this.closed;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Format, checker, renderer and export disclosure for the installed engine.
|
|
26
|
+
* Read this instead of treating a method name as a capability.
|
|
27
|
+
*/
|
|
28
|
+
async capabilities() {
|
|
29
|
+
this.assertActive();
|
|
30
|
+
return service.capabilities();
|
|
31
|
+
}
|
|
32
|
+
/** Creates a document from a title and an optional template. */
|
|
33
|
+
async create(options = {}) {
|
|
34
|
+
this.assertActive();
|
|
35
|
+
assertOptionalString(options.title, 'title');
|
|
36
|
+
assertOptionalString(options.template, 'template');
|
|
37
|
+
assertOptionalString(options.author, 'author');
|
|
38
|
+
const created = service.create({ title: options.title, template: options.template, author: options.author });
|
|
39
|
+
this.handles.add(created.handle);
|
|
40
|
+
this.active = created.handle;
|
|
41
|
+
return created.document;
|
|
42
|
+
}
|
|
43
|
+
/** Opens a native project or a document at a path. */
|
|
44
|
+
async open(input) {
|
|
45
|
+
this.assertActive();
|
|
46
|
+
const path = input?.path;
|
|
47
|
+
assertPath(path);
|
|
48
|
+
const intent = input?.intent ?? 'edit';
|
|
49
|
+
if (intent !== 'edit') {
|
|
50
|
+
throw new OpenWordError('format/unsupported', `native documents open for editing; \`${intent}\` needs the projection pipeline`, { details: { feature: `open.intent.${intent}` } });
|
|
51
|
+
}
|
|
52
|
+
const opened = service.open(path);
|
|
53
|
+
this.handles.add(opened.handle);
|
|
54
|
+
this.active = opened.handle;
|
|
55
|
+
return opened.document;
|
|
56
|
+
}
|
|
57
|
+
/** Writes the active document to a native project path. */
|
|
58
|
+
async save(target, options = {}) {
|
|
59
|
+
this.assertActive();
|
|
60
|
+
assertPath(target);
|
|
61
|
+
return service.save(this.requireHandle(), target, options.overwrite === true);
|
|
62
|
+
}
|
|
63
|
+
/** Reports block, paragraph, heading, table, word and reading-time counts. */
|
|
64
|
+
async stats() {
|
|
65
|
+
this.assertActive();
|
|
66
|
+
return service.stats(this.requireHandle());
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Reads the nested heading outline of the active document.
|
|
70
|
+
*
|
|
71
|
+
* Every section appears once, nested under the closest preceding heading
|
|
72
|
+
* with a lower level. Each item carries its `section-N` id, heading level,
|
|
73
|
+
* title (`null` when empty), depth-first `blockIndex` and child items.
|
|
74
|
+
*/
|
|
75
|
+
async outline() {
|
|
76
|
+
this.assertActive();
|
|
77
|
+
return service.outline(this.requireHandle());
|
|
78
|
+
}
|
|
79
|
+
/** Exports the active document to one projection target. */
|
|
80
|
+
async export(options) {
|
|
81
|
+
this.assertActive();
|
|
82
|
+
const format = options?.format;
|
|
83
|
+
if (typeof format !== 'string' || format.length === 0) {
|
|
84
|
+
throw new OpenWordError('session/argument-invalid', '`format` must be a non-empty string');
|
|
85
|
+
}
|
|
86
|
+
assertPath(options?.target);
|
|
87
|
+
return service.exportDocument(this.requireHandle(), format, options.target, options.overwrite !== false);
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Applies one atomic edit batch and returns the committed summary with one
|
|
91
|
+
* result per operation.
|
|
92
|
+
*
|
|
93
|
+
* `expectedRevision` rejects a batch written against an older revision with
|
|
94
|
+
* `document/revision-conflict` before anything mutates.
|
|
95
|
+
*/
|
|
96
|
+
async edit(operations, options = {}) {
|
|
97
|
+
this.assertActive();
|
|
98
|
+
assertOperations(operations);
|
|
99
|
+
const expectedRevision = options?.expectedRevision;
|
|
100
|
+
if (expectedRevision !== undefined && (!Number.isInteger(expectedRevision) || expectedRevision < 0)) {
|
|
101
|
+
throw new OpenWordError('session/argument-invalid', '`expectedRevision` must be a non-negative integer');
|
|
102
|
+
}
|
|
103
|
+
return service.edit(this.requireHandle(), { operations, expectedRevision });
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Undoes the last `steps` committed mutations (default 1).
|
|
107
|
+
*
|
|
108
|
+
* History is process-local and capped at 50 mutations; asking for more steps
|
|
109
|
+
* than are retained rejects with `session/argument-invalid` and changes
|
|
110
|
+
* nothing. A successful undo bumps the revision once.
|
|
111
|
+
*/
|
|
112
|
+
async undo(options = {}) {
|
|
113
|
+
this.assertActive();
|
|
114
|
+
const steps = options?.steps;
|
|
115
|
+
assertSteps(steps);
|
|
116
|
+
return service.undo(this.requireHandle(), steps);
|
|
117
|
+
}
|
|
118
|
+
/** Replays undone mutations, most recent first. */
|
|
119
|
+
async redo(options = {}) {
|
|
120
|
+
this.assertActive();
|
|
121
|
+
const steps = options?.steps;
|
|
122
|
+
assertSteps(steps);
|
|
123
|
+
return service.redo(this.requireHandle(), steps);
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Appends another document's blocks to the active document.
|
|
127
|
+
*
|
|
128
|
+
* The donor is opened through the same routing as `open()`, so `.docx`,
|
|
129
|
+
* `.md`, `.html`, `.rtf`, `.txt` and `.oword` are accepted; merging the
|
|
130
|
+
* file the document was opened from is allowed.
|
|
131
|
+
*/
|
|
132
|
+
async merge(path, options = {}) {
|
|
133
|
+
this.assertActive();
|
|
134
|
+
assertPath(path);
|
|
135
|
+
const mode = options?.mode ?? 'append';
|
|
136
|
+
if (mode !== 'append') {
|
|
137
|
+
throw new OpenWordError('object/unsupported', `merge mode \`${mode}\` is not supported`);
|
|
138
|
+
}
|
|
139
|
+
return service.merge(this.requireHandle(), path, mode);
|
|
140
|
+
}
|
|
141
|
+
/** Inspects an input through a transient handle that is always released. */
|
|
142
|
+
async inspect(input, options) {
|
|
143
|
+
this.assertActive();
|
|
144
|
+
const path = input?.path;
|
|
145
|
+
assertPath(path);
|
|
146
|
+
const opened = service.open(path);
|
|
147
|
+
try {
|
|
148
|
+
return service.inspect(opened.handle, options?.checks);
|
|
149
|
+
}
|
|
150
|
+
finally {
|
|
151
|
+
closeQuietly(opened.handle);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* Finds text across section titles, paragraphs, list items and table cells.
|
|
156
|
+
*
|
|
157
|
+
* Offsets are character positions inside the matched block's plain text.
|
|
158
|
+
*/
|
|
159
|
+
async find(query, options = {}) {
|
|
160
|
+
this.assertActive();
|
|
161
|
+
if (typeof query !== 'string' || query.length === 0) {
|
|
162
|
+
throw new OpenWordError('session/argument-invalid', '`query` must be a non-empty string');
|
|
163
|
+
}
|
|
164
|
+
const limit = options?.limit;
|
|
165
|
+
if (limit !== undefined && (!Number.isInteger(limit) || limit < 1)) {
|
|
166
|
+
throw new OpenWordError('session/argument-invalid', '`limit` must be a positive integer');
|
|
167
|
+
}
|
|
168
|
+
return service.find(this.requireHandle(), query, limit);
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* Replaces `find` with `replacement` and reports the changed blocks.
|
|
172
|
+
*
|
|
173
|
+
* Matching is case-sensitive by default; `wholeWord` requires no word
|
|
174
|
+
* character on either side. A pass that finds nothing changes nothing, so
|
|
175
|
+
* it records no history entry. A committing pass advances the revision once
|
|
176
|
+
* and can be undone with `undo()`.
|
|
177
|
+
*/
|
|
178
|
+
async replace(find, replacement, options = {}) {
|
|
179
|
+
this.assertActive();
|
|
180
|
+
if (typeof find !== 'string' || find.length === 0) {
|
|
181
|
+
throw new OpenWordError('session/argument-invalid', '`find` must be a non-empty string');
|
|
182
|
+
}
|
|
183
|
+
if (typeof replacement !== 'string') {
|
|
184
|
+
throw new OpenWordError('session/argument-invalid', '`replacement` must be a string');
|
|
185
|
+
}
|
|
186
|
+
const caseSensitive = options?.caseSensitive;
|
|
187
|
+
const wholeWord = options?.wholeWord;
|
|
188
|
+
for (const [name, value] of [
|
|
189
|
+
['caseSensitive', caseSensitive],
|
|
190
|
+
['wholeWord', wholeWord],
|
|
191
|
+
]) {
|
|
192
|
+
if (value !== undefined && typeof value !== 'boolean') {
|
|
193
|
+
throw new OpenWordError('session/argument-invalid', `\`${name}\` must be a boolean`);
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
const limit = options?.limit;
|
|
197
|
+
if (limit !== undefined && (!Number.isInteger(limit) || limit < 1)) {
|
|
198
|
+
throw new OpenWordError('session/argument-invalid', '`limit` must be a positive integer');
|
|
199
|
+
}
|
|
200
|
+
const expectedRevision = options?.expectedRevision;
|
|
201
|
+
if (expectedRevision !== undefined && (!Number.isInteger(expectedRevision) || expectedRevision < 0)) {
|
|
202
|
+
throw new OpenWordError('session/argument-invalid', '`expectedRevision` must be a non-negative integer');
|
|
203
|
+
}
|
|
204
|
+
return service.replace(this.requireHandle(), {
|
|
205
|
+
find,
|
|
206
|
+
replace: replacement,
|
|
207
|
+
caseSensitive,
|
|
208
|
+
wholeWord,
|
|
209
|
+
limit,
|
|
210
|
+
expectedRevision,
|
|
211
|
+
});
|
|
212
|
+
}
|
|
213
|
+
/** Releases every tracked handle. Idempotent. */
|
|
214
|
+
async dispose() {
|
|
215
|
+
for (const handle of this.handles) {
|
|
216
|
+
closeQuietly(handle);
|
|
217
|
+
}
|
|
218
|
+
this.handles.clear();
|
|
219
|
+
this.active = null;
|
|
220
|
+
this.closed = true;
|
|
221
|
+
}
|
|
222
|
+
/** @internal Throws `document/not-found` when no document is active. */
|
|
223
|
+
requireHandle() {
|
|
224
|
+
if (this.active === null) {
|
|
225
|
+
throw new OpenWordError('document/not-found', 'no document is open in this session; call `create` or `open` first');
|
|
226
|
+
}
|
|
227
|
+
return this.active;
|
|
228
|
+
}
|
|
229
|
+
/** @internal Throws `session/disposed` once the session is gone. */
|
|
230
|
+
assertActive() {
|
|
231
|
+
if (this.closed) {
|
|
232
|
+
throw new OpenWordError('session/disposed', 'the OpenWord session has been disposed; create a new instance');
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
function assertPath(value) {
|
|
237
|
+
if (typeof value !== 'string' || value.length === 0) {
|
|
238
|
+
throw new OpenWordError('session/argument-invalid', '`path` must be a non-empty string');
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
function assertOptionalString(value, field) {
|
|
242
|
+
if (value !== undefined && typeof value !== 'string') {
|
|
243
|
+
throw new OpenWordError('session/argument-invalid', `\`${field}\` must be a string when present`);
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
function assertSteps(value) {
|
|
247
|
+
if (value !== undefined && (!Number.isInteger(value) || value < 1)) {
|
|
248
|
+
throw new OpenWordError('session/argument-invalid', '`steps` must be a positive integer');
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
function assertOperations(value) {
|
|
252
|
+
if (!Array.isArray(value) || value.length === 0) {
|
|
253
|
+
throw new OpenWordError('session/argument-invalid', '`operations` must be a non-empty array');
|
|
254
|
+
}
|
|
255
|
+
for (const operation of value) {
|
|
256
|
+
if (typeof operation !== 'object' || operation === null || typeof operation.type !== 'string') {
|
|
257
|
+
throw new OpenWordError('session/argument-invalid', 'each edit operation needs a `type` string');
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
function closeQuietly(handle) {
|
|
262
|
+
try {
|
|
263
|
+
service.close(handle);
|
|
264
|
+
}
|
|
265
|
+
catch {
|
|
266
|
+
// A handle that never reached the engine is already released; disposal
|
|
267
|
+
// must not mask the original failure.
|
|
268
|
+
}
|
|
269
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Orthogonal native operations.
|
|
3
|
+
*
|
|
4
|
+
* One function performs one operation and returns data; argument parsing, help
|
|
5
|
+
* text and exit codes belong to `cli.ts` and to the authoring surface. Nothing
|
|
6
|
+
* here knows about a command line.
|
|
7
|
+
*/
|
|
8
|
+
import type { Document, DocumentCapabilities, DocumentCreateInput, DocumentOutline, DocumentSummary, EditReport, EditRequest, ExportFormat, ExportReport, FindHit, ImportReport, InspectCheck, InspectReport, MergeResult, RedoResult, ReplaceReport, ReplaceRequest, SaveReport, StatsReport, UndoResult } from './types.js';
|
|
9
|
+
/** A released-on-close document handle plus its committed summary and import losses. */
|
|
10
|
+
export interface DocumentHandle extends ImportReport {
|
|
11
|
+
readonly handle: number;
|
|
12
|
+
readonly document: Document;
|
|
13
|
+
}
|
|
14
|
+
/** Reads the capability matrix of the installed addon. */
|
|
15
|
+
export declare function capabilities(): DocumentCapabilities;
|
|
16
|
+
/** Creates a document and returns its handle and committed summary. */
|
|
17
|
+
export declare function create(request: DocumentCreateInput): DocumentHandle;
|
|
18
|
+
/** Opens a document at a path and returns its handle and committed summary. */
|
|
19
|
+
export declare function open(path: string): DocumentHandle;
|
|
20
|
+
/** Writes one document identified by a handle to a native project path. */
|
|
21
|
+
export declare function save(handle: number, target: string, overwrite: boolean): SaveReport;
|
|
22
|
+
/** Runs the declared rule checks against one document handle. */
|
|
23
|
+
export declare function inspect(handle: number, checks?: readonly InspectCheck[]): InspectReport;
|
|
24
|
+
/** Reads the committed summary of one document handle. */
|
|
25
|
+
export declare function summary(handle: number): DocumentSummary;
|
|
26
|
+
/** Reads the nested heading outline of one document handle. */
|
|
27
|
+
export declare function outline(handle: number): DocumentOutline;
|
|
28
|
+
/** Reads block, word and reading-time statistics of one document handle. */
|
|
29
|
+
export declare function stats(handle: number): StatsReport;
|
|
30
|
+
/** Searches one document for text and returns the matches. */
|
|
31
|
+
export declare function find(handle: number, query: string, limit?: number): readonly FindHit[];
|
|
32
|
+
/** Replaces text in one document and reports the affected blocks. */
|
|
33
|
+
export declare function replace(handle: number, request: ReplaceRequest): ReplaceReport;
|
|
34
|
+
/** Applies one edit batch and returns the new committed summary. */
|
|
35
|
+
export declare function edit(handle: number, request: EditRequest): EditReport;
|
|
36
|
+
/** Undoes committed edits on a document handle. */
|
|
37
|
+
export declare function undo(handle: number, steps?: number): UndoResult;
|
|
38
|
+
/** Replays undone edits on a document handle. */
|
|
39
|
+
export declare function redo(handle: number, steps?: number): RedoResult;
|
|
40
|
+
/** Appends another document's blocks to a document handle. */
|
|
41
|
+
export declare function merge(handle: number, path: string, mode?: string): MergeResult;
|
|
42
|
+
/** Writes one export target from a document handle. */
|
|
43
|
+
export declare function exportDocument(handle: number, format: ExportFormat, target: string, overwrite?: boolean): ExportReport;
|
|
44
|
+
/** Releases one document handle. */
|
|
45
|
+
export declare function close(handle: number): void;
|
package/dist/service.js
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Orthogonal native operations.
|
|
3
|
+
*
|
|
4
|
+
* One function performs one operation and returns data; argument parsing, help
|
|
5
|
+
* text and exit codes belong to `cli.ts` and to the authoring surface. Nothing
|
|
6
|
+
* here knows about a command line.
|
|
7
|
+
*/
|
|
8
|
+
import { NotImplementedError, OpenWordError, errorCodes } from './errors.js';
|
|
9
|
+
import { nativeBinding } from './native.js';
|
|
10
|
+
const knownCodes = new Set(errorCodes);
|
|
11
|
+
function binding(feature) {
|
|
12
|
+
const native = nativeBinding();
|
|
13
|
+
if (!native) {
|
|
14
|
+
throw new NotImplementedError(feature);
|
|
15
|
+
}
|
|
16
|
+
return native;
|
|
17
|
+
}
|
|
18
|
+
function decode(work) {
|
|
19
|
+
try {
|
|
20
|
+
return JSON.parse(work());
|
|
21
|
+
}
|
|
22
|
+
catch (error) {
|
|
23
|
+
throw translate(error);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
function translate(error) {
|
|
27
|
+
if (error instanceof OpenWordError) {
|
|
28
|
+
return error;
|
|
29
|
+
}
|
|
30
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
31
|
+
const separator = message.indexOf(': ');
|
|
32
|
+
const code = separator > 0 ? message.slice(0, separator) : '';
|
|
33
|
+
if (knownCodes.has(code)) {
|
|
34
|
+
return new OpenWordError(code, message.slice(separator + 2), { details: { feature: code } });
|
|
35
|
+
}
|
|
36
|
+
return new OpenWordError('format/invalid', message, { cause: error });
|
|
37
|
+
}
|
|
38
|
+
/** Reads the capability matrix of the installed addon. */
|
|
39
|
+
export function capabilities() {
|
|
40
|
+
return decode(() => binding('capabilities').capabilities());
|
|
41
|
+
}
|
|
42
|
+
/** Creates a document and returns its handle and committed summary. */
|
|
43
|
+
export function create(request) {
|
|
44
|
+
return decode(() => binding('create').document_create(JSON.stringify({ title: request.title, template: request.template, author: request.author })));
|
|
45
|
+
}
|
|
46
|
+
/** Opens a document at a path and returns its handle and committed summary. */
|
|
47
|
+
export function open(path) {
|
|
48
|
+
return decode(() => binding('open').document_open(path));
|
|
49
|
+
}
|
|
50
|
+
/** Writes one document identified by a handle to a native project path. */
|
|
51
|
+
export function save(handle, target, overwrite) {
|
|
52
|
+
return decode(() => binding('save').document_save(handle, target, overwrite));
|
|
53
|
+
}
|
|
54
|
+
/** Runs the declared rule checks against one document handle. */
|
|
55
|
+
export function inspect(handle, checks) {
|
|
56
|
+
return decode(() => checks === undefined
|
|
57
|
+
? binding('inspect').document_inspect(handle)
|
|
58
|
+
: binding('inspect').document_inspect(handle, JSON.stringify(checks)));
|
|
59
|
+
}
|
|
60
|
+
/** Reads the committed summary of one document handle. */
|
|
61
|
+
export function summary(handle) {
|
|
62
|
+
return decode(() => binding('summary').document_summary(handle));
|
|
63
|
+
}
|
|
64
|
+
/** Reads the nested heading outline of one document handle. */
|
|
65
|
+
export function outline(handle) {
|
|
66
|
+
return decode(() => binding('outline').document_outline(handle));
|
|
67
|
+
}
|
|
68
|
+
/** Reads block, word and reading-time statistics of one document handle. */
|
|
69
|
+
export function stats(handle) {
|
|
70
|
+
return decode(() => binding('stats').word_stats(handle));
|
|
71
|
+
}
|
|
72
|
+
/** Searches one document for text and returns the matches. */
|
|
73
|
+
export function find(handle, query, limit) {
|
|
74
|
+
return decode(() => limit === undefined
|
|
75
|
+
? binding('find').document_find(handle, query)
|
|
76
|
+
: binding('find').document_find(handle, query, limit));
|
|
77
|
+
}
|
|
78
|
+
/** Replaces text in one document and reports the affected blocks. */
|
|
79
|
+
export function replace(handle, request) {
|
|
80
|
+
return decode(() => binding('replace').document_replace(handle, JSON.stringify(request)));
|
|
81
|
+
}
|
|
82
|
+
/** Applies one edit batch and returns the new committed summary. */
|
|
83
|
+
export function edit(handle, request) {
|
|
84
|
+
return decode(() => binding('edit').document_edit(handle, JSON.stringify(request)));
|
|
85
|
+
}
|
|
86
|
+
/** Undoes committed edits on a document handle. */
|
|
87
|
+
export function undo(handle, steps) {
|
|
88
|
+
return decode(() => steps === undefined ? binding('undo').document_undo(handle) : binding('undo').document_undo(handle, steps));
|
|
89
|
+
}
|
|
90
|
+
/** Replays undone edits on a document handle. */
|
|
91
|
+
export function redo(handle, steps) {
|
|
92
|
+
return decode(() => steps === undefined ? binding('redo').document_redo(handle) : binding('redo').document_redo(handle, steps));
|
|
93
|
+
}
|
|
94
|
+
/** Appends another document's blocks to a document handle. */
|
|
95
|
+
export function merge(handle, path, mode = 'append') {
|
|
96
|
+
return decode(() => binding('merge').document_merge(handle, path, mode));
|
|
97
|
+
}
|
|
98
|
+
/** Writes one export target from a document handle. */
|
|
99
|
+
export function exportDocument(handle, format, target, overwrite = true) {
|
|
100
|
+
return decode(() => binding('export').document_export(handle, format, target, overwrite));
|
|
101
|
+
}
|
|
102
|
+
/** Releases one document handle. */
|
|
103
|
+
export function close(handle) {
|
|
104
|
+
binding('document.close').document_close(handle);
|
|
105
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Smoke tests for the authoring shell.
|
|
3
|
+
*
|
|
4
|
+
* They assert shell semantics only: the stable error code without a built
|
|
5
|
+
* binding, a session that closes idempotently, synchronous argument validation
|
|
6
|
+
* for create/open/edit/save/export/find, and a format registry that classifies
|
|
7
|
+
* the whole family. Document behaviour is covered by the engine's own round-trip
|
|
8
|
+
* tests, not here.
|
|
9
|
+
*/
|
|
10
|
+
export {};
|
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Smoke tests for the authoring shell.
|
|
3
|
+
*
|
|
4
|
+
* They assert shell semantics only: the stable error code without a built
|
|
5
|
+
* binding, a session that closes idempotently, synchronous argument validation
|
|
6
|
+
* for create/open/edit/save/export/find, and a format registry that classifies
|
|
7
|
+
* the whole family. Document behaviour is covered by the engine's own round-trip
|
|
8
|
+
* tests, not here.
|
|
9
|
+
*/
|
|
10
|
+
import assert from 'node:assert/strict';
|
|
11
|
+
import test from 'node:test';
|
|
12
|
+
import { OpenWord, OpenWordError, documentFormats, formatFromPath, formatInfo } from './index.js';
|
|
13
|
+
import { nativeBinding } from './native.js';
|
|
14
|
+
const installed = (() => {
|
|
15
|
+
try {
|
|
16
|
+
return nativeBinding() !== undefined;
|
|
17
|
+
}
|
|
18
|
+
catch {
|
|
19
|
+
return false;
|
|
20
|
+
}
|
|
21
|
+
})();
|
|
22
|
+
const withAddon = installed ? 'a built addon is installed' : false;
|
|
23
|
+
const withoutAddon = installed ? false : 'a built addon is not installed';
|
|
24
|
+
test('session construction is synchronous and side-effect free', async () => {
|
|
25
|
+
const word = new OpenWord();
|
|
26
|
+
assert.equal(word.locale, 'en');
|
|
27
|
+
assert.equal(word.disposed, false);
|
|
28
|
+
await word.dispose();
|
|
29
|
+
assert.equal(word.disposed, true);
|
|
30
|
+
});
|
|
31
|
+
test('without an installed addon engine calls keep capability/not-implemented', { skip: withAddon }, async () => {
|
|
32
|
+
const word = new OpenWord({ locale: 'zh-CN' });
|
|
33
|
+
assert.equal(word.locale, 'zh-CN');
|
|
34
|
+
const calls = [
|
|
35
|
+
() => word.capabilities(),
|
|
36
|
+
() => word.create({ title: 'Research review' }),
|
|
37
|
+
() => word.open({ path: 'report.docx' }),
|
|
38
|
+
() => word.inspect({ path: 'report.docx' }),
|
|
39
|
+
];
|
|
40
|
+
for (const call of calls) {
|
|
41
|
+
await assert.rejects(call, (error) => {
|
|
42
|
+
assert.ok(error instanceof OpenWordError);
|
|
43
|
+
assert.equal(error.code, 'capability/not-implemented');
|
|
44
|
+
assert.ok(error.details.feature);
|
|
45
|
+
return true;
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
await word.dispose();
|
|
49
|
+
});
|
|
50
|
+
test('dispose is idempotent and closes the session', async () => {
|
|
51
|
+
const word = new OpenWord();
|
|
52
|
+
await word.dispose();
|
|
53
|
+
await word.dispose();
|
|
54
|
+
await assert.rejects(word.capabilities(), (error) => error instanceof OpenWordError && error.code === 'session/disposed');
|
|
55
|
+
});
|
|
56
|
+
test('dispose after a rejected create and open stays clean and idempotent', { skip: withAddon }, async () => {
|
|
57
|
+
const word = new OpenWord();
|
|
58
|
+
await assert.rejects(word.create({ title: 'Rejected' }), (error) => error instanceof OpenWordError);
|
|
59
|
+
await assert.rejects(word.open({ path: 'report.docx' }), (error) => error instanceof OpenWordError);
|
|
60
|
+
await word.dispose();
|
|
61
|
+
await word.dispose();
|
|
62
|
+
await assert.rejects(word.capabilities(), (error) => error instanceof OpenWordError && error.code === 'session/disposed');
|
|
63
|
+
});
|
|
64
|
+
test('open rejects a missing path before reaching the engine', async () => {
|
|
65
|
+
const word = new OpenWord();
|
|
66
|
+
const inputs = [{}, { path: '' }, { path: 42 }];
|
|
67
|
+
for (const input of inputs) {
|
|
68
|
+
await assert.rejects(word.open(input), (error) => error instanceof OpenWordError && error.code === 'session/argument-invalid');
|
|
69
|
+
}
|
|
70
|
+
await word.dispose();
|
|
71
|
+
});
|
|
72
|
+
test('open rejects a non-editing intent before reaching the engine', async () => {
|
|
73
|
+
const word = new OpenWord();
|
|
74
|
+
await assert.rejects(word.open({ path: 'template.dotx', intent: 'template' }), (error) => error instanceof OpenWordError && error.code === 'format/unsupported');
|
|
75
|
+
await word.dispose();
|
|
76
|
+
});
|
|
77
|
+
test('edit, save and export validate arguments before the engine is reached', async () => {
|
|
78
|
+
const word = new OpenWord();
|
|
79
|
+
const invalid = [
|
|
80
|
+
() => word.save(''),
|
|
81
|
+
() => word.export({ format: '', target: 'report.docx' }),
|
|
82
|
+
() => word.export({ format: 'docx', target: '' }),
|
|
83
|
+
() => word.edit([]),
|
|
84
|
+
() => word.edit([{ type: 42 }]),
|
|
85
|
+
() => word.edit([{ type: 'paragraph.append', text: 'x' }], { expectedRevision: -1 }),
|
|
86
|
+
];
|
|
87
|
+
for (const call of invalid) {
|
|
88
|
+
await assert.rejects(call, (error) => error instanceof OpenWordError && error.code === 'session/argument-invalid');
|
|
89
|
+
}
|
|
90
|
+
await word.dispose();
|
|
91
|
+
});
|
|
92
|
+
test('edit, save and export need an open document', async () => {
|
|
93
|
+
const word = new OpenWord();
|
|
94
|
+
const calls = [
|
|
95
|
+
() => word.save('report.oword'),
|
|
96
|
+
() => word.export({ format: 'docx', target: 'report.docx' }),
|
|
97
|
+
() => word.edit([{ type: 'paragraph.append', text: 'x' }]),
|
|
98
|
+
];
|
|
99
|
+
for (const call of calls) {
|
|
100
|
+
await assert.rejects(call, (error) => error instanceof OpenWordError && error.code === 'document/not-found');
|
|
101
|
+
}
|
|
102
|
+
await word.dispose();
|
|
103
|
+
});
|
|
104
|
+
test('find validates the query and limit before the engine is reached', async () => {
|
|
105
|
+
const word = new OpenWord();
|
|
106
|
+
const invalid = [
|
|
107
|
+
() => word.find(''),
|
|
108
|
+
() => word.find(42),
|
|
109
|
+
() => word.find('alpha', { limit: 0 }),
|
|
110
|
+
() => word.find('alpha', { limit: -1 }),
|
|
111
|
+
() => word.find('alpha', { limit: 1.5 }),
|
|
112
|
+
];
|
|
113
|
+
for (const call of invalid) {
|
|
114
|
+
await assert.rejects(call, (error) => error instanceof OpenWordError && error.code === 'session/argument-invalid');
|
|
115
|
+
}
|
|
116
|
+
await word.dispose();
|
|
117
|
+
});
|
|
118
|
+
test('find needs an open document', async () => {
|
|
119
|
+
const word = new OpenWord();
|
|
120
|
+
await assert.rejects(word.find('alpha'), (error) => error instanceof OpenWordError && error.code === 'document/not-found');
|
|
121
|
+
await word.dispose();
|
|
122
|
+
});
|
|
123
|
+
test('outline nests headings by level in an open document', { skip: withoutAddon }, async () => {
|
|
124
|
+
const word = new OpenWord();
|
|
125
|
+
await word.create({ title: 'Outline' });
|
|
126
|
+
await word.edit([
|
|
127
|
+
{ type: 'heading.append', level: 1, text: 'Overview' },
|
|
128
|
+
{ type: 'heading.append', level: 2, text: 'Details' },
|
|
129
|
+
{ type: 'heading.append', level: 1, text: 'Appendix' },
|
|
130
|
+
]);
|
|
131
|
+
const report = await word.outline();
|
|
132
|
+
assert.equal(report.revision, 3);
|
|
133
|
+
assert.equal(report.items.length, 2);
|
|
134
|
+
assert.equal(report.items[0].title, 'Overview');
|
|
135
|
+
assert.equal(report.items[0].level, 1);
|
|
136
|
+
assert.ok(report.items[0].id.startsWith('section-'));
|
|
137
|
+
assert.equal(report.items[0].children.length, 1);
|
|
138
|
+
assert.equal(report.items[0].children[0].title, 'Details');
|
|
139
|
+
assert.equal(report.items[0].children[0].level, 2);
|
|
140
|
+
assert.deepEqual(report.items[0].children[0].children, []);
|
|
141
|
+
assert.equal(report.items[1].title, 'Appendix');
|
|
142
|
+
assert.deepEqual(report.items[1].children, []);
|
|
143
|
+
await word.dispose();
|
|
144
|
+
});
|
|
145
|
+
test('replace validates the request before the engine is reached', async () => {
|
|
146
|
+
const word = new OpenWord();
|
|
147
|
+
const invalid = [
|
|
148
|
+
() => word.replace('', 'x'),
|
|
149
|
+
() => word.replace(42, 'x'),
|
|
150
|
+
() => word.replace('alpha', 42),
|
|
151
|
+
() => word.replace('alpha', 'x', { caseSensitive: 'yes' }),
|
|
152
|
+
() => word.replace('alpha', 'x', { wholeWord: 1 }),
|
|
153
|
+
() => word.replace('alpha', 'x', { limit: 0 }),
|
|
154
|
+
() => word.replace('alpha', 'x', { expectedRevision: -1 }),
|
|
155
|
+
];
|
|
156
|
+
for (const call of invalid) {
|
|
157
|
+
await assert.rejects(call, (error) => error instanceof OpenWordError && error.code === 'session/argument-invalid');
|
|
158
|
+
}
|
|
159
|
+
await word.dispose();
|
|
160
|
+
});
|
|
161
|
+
test('replace needs an open document', async () => {
|
|
162
|
+
const word = new OpenWord();
|
|
163
|
+
await assert.rejects(word.replace('alpha', 'x'), (error) => error instanceof OpenWordError && error.code === 'document/not-found');
|
|
164
|
+
await word.dispose();
|
|
165
|
+
});
|
|
166
|
+
test('undo, redo and merge validate arguments before the engine is reached', async () => {
|
|
167
|
+
const word = new OpenWord();
|
|
168
|
+
const invalid = [
|
|
169
|
+
() => word.undo({ steps: 0 }),
|
|
170
|
+
() => word.undo({ steps: -1 }),
|
|
171
|
+
() => word.redo({ steps: 1.5 }),
|
|
172
|
+
() => word.merge(''),
|
|
173
|
+
() => word.merge(42),
|
|
174
|
+
];
|
|
175
|
+
for (const call of invalid) {
|
|
176
|
+
await assert.rejects(call, (error) => error instanceof OpenWordError && error.code === 'session/argument-invalid');
|
|
177
|
+
}
|
|
178
|
+
await word.dispose();
|
|
179
|
+
});
|
|
180
|
+
test('undo, redo and merge need an open document', async () => {
|
|
181
|
+
const word = new OpenWord();
|
|
182
|
+
const calls = [
|
|
183
|
+
() => word.undo(),
|
|
184
|
+
() => word.redo({ steps: 2 }),
|
|
185
|
+
() => word.merge('other.md'),
|
|
186
|
+
];
|
|
187
|
+
for (const call of calls) {
|
|
188
|
+
await assert.rejects(call, (error) => error instanceof OpenWordError && error.code === 'document/not-found');
|
|
189
|
+
}
|
|
190
|
+
await word.dispose();
|
|
191
|
+
});
|
|
192
|
+
test('format registry classifies the document format family', () => {
|
|
193
|
+
const expected = ['docx', 'docm', 'dotx', 'dotm', 'doc', 'rtf', 'odt', 'txt', 'md', 'html', 'pdf'];
|
|
194
|
+
assert.deepEqual([...documentFormats].sort(), [...expected].sort());
|
|
195
|
+
assert.equal(formatInfo('dotx').defaultIntent, 'template');
|
|
196
|
+
assert.equal(formatInfo('dotm').defaultIntent, 'template');
|
|
197
|
+
assert.equal(formatInfo('dotm').macroEnabled, true);
|
|
198
|
+
assert.equal(formatInfo('docm').macroEnabled, true);
|
|
199
|
+
assert.equal(formatInfo('doc').opensAs, 'none');
|
|
200
|
+
assert.equal(formatInfo('doc').defaultIntent, null);
|
|
201
|
+
assert.equal(formatInfo('rtf').defaultIntent, 'read');
|
|
202
|
+
assert.equal(formatInfo('odt').family, 'interchange');
|
|
203
|
+
assert.equal(formatInfo('pdf').opensAs, 'none');
|
|
204
|
+
assert.equal(formatInfo('pdf').defaultIntent, null);
|
|
205
|
+
assert.equal(formatFromPath('Report.DOCX'), 'docx');
|
|
206
|
+
assert.equal(formatFromPath('report.oword'), undefined);
|
|
207
|
+
assert.equal(formatFromPath('notes.key'), undefined);
|
|
208
|
+
assert.equal(formatFromPath('no-extension'), undefined);
|
|
209
|
+
});
|