@deepseek-ai/libreoffice-kit 0.0.2-rc4 → 0.0.2-rc6
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/NOTICE +238 -0
- package/README.md +5 -3
- package/README.zh.md +3 -3
- package/assets/font-subset.json +4 -4
- package/lib/browser/assets.json +2 -2
- package/lib/browser/index.js +92 -72
- package/lib/browser/types/connection.d.ts +3 -3
- package/lib/browser/types/font-channel.d.ts +5 -4
- package/lib/browser/types/index.d.ts +2 -2
- package/lib/browser/types/office-engine.d.ts +48 -0
- package/lib/browser/types/office-types.d.ts +107 -0
- package/lib/browser/types/office.d.ts +4 -0
- package/lib/browser/types/protocol.d.ts +34 -37
- package/lib/browser/types/types.d.ts +3 -1
- package/lib/browser/worker.js +338 -507
- package/lib/browser-assets.js +1 -1
- package/lib/cli.js +91 -58
- package/lib/font-source.js +89 -56
- package/lib/font-worker.js +717 -45
- package/lib/index.js +91 -58
- package/lib/types/default-fonts.d.ts +3 -0
- package/lib/types/engine.d.ts +1 -1
- package/lib/types/font-preferences-data.d.ts +16 -0
- package/lib/types/font-preferences.d.ts +29 -0
- package/lib/types/font-source-types.d.ts +2 -0
- package/lib/types/fonts.d.ts +11 -8
- package/lib/types/options.d.ts +1 -1
- package/lib/worker.js +707 -37
- package/package.json +3 -3
- package/sources/browser/packages/browser/package.json +2 -2
- package/sources/browser/packages/browser/src/connection.ts +8 -10
- package/sources/browser/packages/browser/src/font-channel.ts +22 -12
- package/sources/browser/packages/browser/src/index.ts +3 -3
- package/sources/browser/packages/browser/src/office-engine.ts +323 -0
- package/sources/browser/packages/browser/src/office-types.ts +77 -0
- package/sources/browser/packages/browser/src/office.ts +62 -0
- package/sources/browser/packages/browser/src/protocol.ts +19 -18
- package/sources/browser/packages/browser/src/types.ts +3 -1
- package/sources/browser/packages/browser/src/worker.ts +37 -33
- package/sources/browser/scripts/build-browser.mjs +9 -1
- package/sources/font-subset/scripts/platform-matrix.mjs +1 -1
- package/sources/font-subset/scripts/verify-artifacts.mjs +2 -2
- package/lib/browser/types/editor-engine.d.ts +0 -50
- package/lib/browser/types/editor-types.d.ts +0 -141
- package/lib/browser/types/editor.d.ts +0 -9
- package/sources/browser/packages/browser/src/editor-engine.ts +0 -382
- package/sources/browser/packages/browser/src/editor-types.ts +0 -106
- package/sources/browser/packages/browser/src/editor.ts +0 -55
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
/** Browser owner for a retained read-only Office model. */
|
|
2
|
+
import { openBrowserConnection } from './connection.ts'
|
|
3
|
+
import { BrowserRenderError } from './types.ts'
|
|
4
|
+
import type { OfficeDocument, OfficeDocumentEvent, OfficeDocumentOptions, OfficeDocumentState, OfficeDocumentLayoutRequest, OfficeDocumentLayoutResult } from './office-types.ts'
|
|
5
|
+
import type { OfficeOperation } from './protocol.ts'
|
|
6
|
+
|
|
7
|
+
/** Open one read-only Office model; source bytes remain owned by the caller. */
|
|
8
|
+
export async function openOfficeDocument(options: OfficeDocumentOptions, signal?: AbortSignal): Promise<OfficeDocument> {
|
|
9
|
+
const listeners = new Set<(event: OfficeDocumentEvent) => void>()
|
|
10
|
+
let state: OfficeDocumentState | undefined
|
|
11
|
+
const connection = await openBrowserConnection(options, signal, true, (event) => {
|
|
12
|
+
if (event.type === 'state') state = event.state
|
|
13
|
+
for (const listener of listeners) {
|
|
14
|
+
try { listener(event) } catch (error) { console.error('Office document observer failed', error) }
|
|
15
|
+
}
|
|
16
|
+
})
|
|
17
|
+
state = connection.opened.office
|
|
18
|
+
if (!state) { await connection.dispose(); throw new BrowserRenderError('invalid-document', 'LibreOffice returned no reading state.') }
|
|
19
|
+
async function operate(operation: OfficeOperation) {
|
|
20
|
+
const response = await connection.request({ type: 'office-operation', operation })
|
|
21
|
+
if (response.type !== 'office-result') throw new BrowserRenderError('render-failed', 'LibreOffice returned an unexpected reading response.')
|
|
22
|
+
return response
|
|
23
|
+
}
|
|
24
|
+
let layoutRunning = false
|
|
25
|
+
let pendingLayout: { request: OfficeDocumentLayoutRequest; waiters: ReturnType<typeof Promise.withResolvers<OfficeDocumentLayoutResult>>[] } | undefined
|
|
26
|
+
async function applyLayouts(): Promise<void> {
|
|
27
|
+
layoutRunning = true
|
|
28
|
+
while (pendingLayout) {
|
|
29
|
+
const next = pendingLayout
|
|
30
|
+
pendingLayout = undefined
|
|
31
|
+
try {
|
|
32
|
+
const response = await operate({ type: 'layout', request: next.request })
|
|
33
|
+
if (!response.layout) throw new BrowserRenderError('render-failed', 'LibreOffice returned no layout result.')
|
|
34
|
+
for (const waiter of next.waiters) waiter.resolve(response.layout)
|
|
35
|
+
} catch (error) { for (const waiter of next.waiters) waiter.reject(error) }
|
|
36
|
+
}
|
|
37
|
+
layoutRunning = false
|
|
38
|
+
}
|
|
39
|
+
return {
|
|
40
|
+
get state() { return state! },
|
|
41
|
+
subscribe(listener) { listeners.add(listener); return () => { listeners.delete(listener) } },
|
|
42
|
+
async pointer(event) { await operate({ type: 'pointer', event }) },
|
|
43
|
+
async navigate(event) { await operate({ type: 'navigate', event }) },
|
|
44
|
+
async selectAll() { await operate({ type: 'select-all' }) },
|
|
45
|
+
async copy() { return (await operate({ type: 'copy' })).text ?? '' },
|
|
46
|
+
async goToCell(address) { await operate({ type: 'cell', address }) },
|
|
47
|
+
async setPart(part) { await operate({ type: 'part', part }) },
|
|
48
|
+
async setViewport(rectangle, scale) { await operate({ type: 'viewport', rectangle, scale }) },
|
|
49
|
+
setLayout(request) {
|
|
50
|
+
const waiter = Promise.withResolvers<OfficeDocumentLayoutResult>()
|
|
51
|
+
pendingLayout = { request, waiters: [...(pendingLayout?.waiters ?? []), waiter] }
|
|
52
|
+
if (!layoutRunning) void applyLayouts()
|
|
53
|
+
return waiter.promise
|
|
54
|
+
},
|
|
55
|
+
async renderTile(request, tileSignal) {
|
|
56
|
+
const response = await connection.request({ type: 'office-tile', request }, tileSignal)
|
|
57
|
+
if (response.type !== 'tile') throw new BrowserRenderError('render-failed', 'LibreOffice returned an unexpected tile response.')
|
|
58
|
+
return response.tile
|
|
59
|
+
},
|
|
60
|
+
dispose() { listeners.clear(); return connection.dispose() },
|
|
61
|
+
}
|
|
62
|
+
}
|
|
@@ -1,40 +1,41 @@
|
|
|
1
1
|
/** Private messages exchanged by the document owner and its single engine Worker. */
|
|
2
2
|
import type { BrowserDocumentOptions, BrowserFontRequest, BrowserPage, BrowserRenderErrorCode, BrowserTile, BrowserTileRequest } from './types.ts'
|
|
3
|
-
import type {
|
|
3
|
+
import type { OfficeDocumentEvent, OfficeDocumentNavigation, OfficeDocumentOptions, OfficeDocumentState, OfficeDocumentPointer, OfficeDocumentLayoutRequest, OfficeDocumentLayoutResult, OfficeDocumentTileRequest, OfficeDocumentRectangle } from './office-types.ts'
|
|
4
4
|
|
|
5
5
|
export const FONT_CHUNK_BYTES = 1024 * 1024
|
|
6
6
|
export const FONT_HEADER_BYTES = 8
|
|
7
7
|
export const enum FontState { Waiting, Header, Bytes, Done, Error, Cancelled }
|
|
8
|
-
export type WorkerOptions = Omit<BrowserDocumentOptions |
|
|
9
|
-
export type
|
|
10
|
-
| { readonly type: '
|
|
11
|
-
| { readonly type: '
|
|
12
|
-
| { readonly type: '
|
|
8
|
+
export type WorkerOptions = Omit<BrowserDocumentOptions | OfficeDocumentOptions, 'resolveFonts' | 'onMissingFonts'>
|
|
9
|
+
export type OfficeOperation =
|
|
10
|
+
| { readonly type: 'pointer'; readonly event: OfficeDocumentPointer }
|
|
11
|
+
| { readonly type: 'navigate'; readonly event: OfficeDocumentNavigation }
|
|
12
|
+
| { readonly type: 'select-all' }
|
|
13
|
+
| { readonly type: 'cell'; readonly address: string }
|
|
13
14
|
| { readonly type: 'copy' }
|
|
14
15
|
| { readonly type: 'part'; readonly part: number }
|
|
15
|
-
| { readonly type: 'viewport'; readonly rectangle:
|
|
16
|
-
| { readonly type: '
|
|
16
|
+
| { readonly type: 'viewport'; readonly rectangle: OfficeDocumentRectangle; readonly scale: number }
|
|
17
|
+
| { readonly type: 'layout'; readonly request: OfficeDocumentLayoutRequest }
|
|
17
18
|
export type OwnerMessage =
|
|
18
|
-
| { readonly type: 'open'; readonly id: number; readonly options: WorkerOptions; readonly channel: SharedArrayBuffer; readonly
|
|
19
|
+
| { readonly type: 'open'; readonly id: number; readonly options: WorkerOptions; readonly channel: SharedArrayBuffer; readonly office?: boolean }
|
|
19
20
|
| { readonly type: 'tile'; readonly id: number; readonly request: BrowserTileRequest }
|
|
20
|
-
| { readonly type: '
|
|
21
|
-
| { readonly type: '
|
|
22
|
-
| { readonly type: 'edit'; readonly id: number; readonly operation: EditorOperation }
|
|
21
|
+
| { readonly type: 'office-tile'; readonly id: number; readonly request: OfficeDocumentTileRequest }
|
|
22
|
+
| { readonly type: 'office-operation'; readonly id: number; readonly operation: OfficeOperation }
|
|
23
23
|
| { readonly type: 'dispose'; readonly id: number }
|
|
24
24
|
type WithoutId<T> = T extends unknown ? Omit<T, 'id'> : never
|
|
25
25
|
export type OwnerRequest = WithoutId<OwnerMessage>
|
|
26
26
|
export type WorkerMessage =
|
|
27
|
-
| { readonly type: 'opened'; readonly id: number; readonly pages: readonly BrowserPage[]; readonly
|
|
27
|
+
| { readonly type: 'opened'; readonly id: number; readonly pages: readonly BrowserPage[]; readonly office?: OfficeDocumentState }
|
|
28
28
|
| { readonly type: 'tile'; readonly id: number; readonly tile: BrowserTile }
|
|
29
|
-
| { readonly type: '
|
|
30
|
-
| { readonly type: '
|
|
31
|
-
| { readonly type: 'editor-event'; readonly event: BrowserEditorEvent }
|
|
29
|
+
| { readonly type: 'office-result'; readonly id: number; readonly text?: string; readonly layout?: OfficeDocumentLayoutResult }
|
|
30
|
+
| { readonly type: 'office-event'; readonly event: OfficeDocumentEvent }
|
|
32
31
|
| { readonly type: 'disposed'; readonly id: number }
|
|
33
32
|
| { readonly type: 'error'; readonly id: number; readonly code: BrowserRenderErrorCode; readonly message: string; readonly fatal: boolean }
|
|
34
|
-
| { readonly type: 'font'; readonly request: BrowserFontRequest }
|
|
33
|
+
| { readonly type: 'font'; readonly request: BrowserFontRequest; readonly known: readonly FontIdentity[] }
|
|
35
34
|
| { readonly type: 'font-next' }
|
|
36
35
|
| { readonly type: 'missing-fonts'; readonly families: readonly string[] }
|
|
36
|
+
/** Immutable bytes already installed in this document's MEMFS. */
|
|
37
|
+
export interface FontIdentity { readonly id: string; readonly bytes: number; readonly format?: 'ttf' | 'otf' | 'ttc' }
|
|
37
38
|
export interface FontHeader {
|
|
38
|
-
readonly fonts: readonly
|
|
39
|
+
readonly fonts: readonly (FontIdentity & { readonly family: string; readonly alias: string; readonly reference?: true })[]
|
|
39
40
|
readonly missingFamily?: string
|
|
40
41
|
}
|
|
@@ -27,6 +27,8 @@ export interface BrowserFontRequest {
|
|
|
27
27
|
export interface BrowserFontResult {
|
|
28
28
|
readonly fonts: readonly { readonly id: string; readonly data: Uint8Array; readonly family: string; readonly alias: string; readonly format?: 'ttf' | 'otf' | 'ttc' }[]
|
|
29
29
|
readonly missingFamily?: string
|
|
30
|
+
/** Missing scalars reported by the Host, independently of a substituted family. */
|
|
31
|
+
readonly unresolvedCodePoints?: readonly number[]
|
|
30
32
|
}
|
|
31
33
|
|
|
32
34
|
/** All sizes are CSS pixels at 96 pixels per inch. */
|
|
@@ -81,7 +83,7 @@ export interface BrowserDocument {
|
|
|
81
83
|
}
|
|
82
84
|
|
|
83
85
|
/** Stable failures callers may map to their own localized product copy. */
|
|
84
|
-
export type BrowserRenderErrorCode = 'unavailable' | 'invalid-document' | 'timeout' | 'font-limit' | 'font-unavailable' | 'disposed' | 'render-failed' | '
|
|
86
|
+
export type BrowserRenderErrorCode = 'unavailable' | 'invalid-document' | 'timeout' | 'font-limit' | 'font-unavailable' | 'disposed' | 'render-failed' | 'stale-part'
|
|
85
87
|
|
|
86
88
|
/** A rendering failure with a stable machine-readable category. */
|
|
87
89
|
export class BrowserRenderError extends Error {
|
|
@@ -8,8 +8,8 @@ import type { OwnerMessage, WorkerMessage, WorkerOptions } from './protocol.ts'
|
|
|
8
8
|
import type { EnginePage } from './rendering.ts'
|
|
9
9
|
|
|
10
10
|
import type { EmscriptenModule } from './engine-types.ts'
|
|
11
|
-
import type {
|
|
12
|
-
import {
|
|
11
|
+
import type { OfficeDocumentFormat } from './office-types.ts'
|
|
12
|
+
import { OfficeEngine } from './office-engine.ts'
|
|
13
13
|
import { pdfPages, renderRegion } from './engine-rendering.ts'
|
|
14
14
|
|
|
15
15
|
interface EngineGlobal extends DedicatedWorkerGlobalScope {
|
|
@@ -21,7 +21,7 @@ let office = 0
|
|
|
21
21
|
let document = 0
|
|
22
22
|
let pdf = 0
|
|
23
23
|
let pages: EnginePage[] = []
|
|
24
|
-
let
|
|
24
|
+
let reader: OfficeEngine | undefined
|
|
25
25
|
let fatal = false
|
|
26
26
|
let fontFailure: unknown
|
|
27
27
|
let serial = Promise.resolve()
|
|
@@ -37,10 +37,15 @@ function error(code: BrowserRenderErrorCode): BrowserRenderError {
|
|
|
37
37
|
try { return new BrowserRenderError(code, module.UTF8ToString(pointer)) } finally { call('free', [pointer]) }
|
|
38
38
|
}
|
|
39
39
|
|
|
40
|
-
async function open(options: WorkerOptions, channel: SharedArrayBuffer,
|
|
40
|
+
async function open(options: WorkerOptions, channel: SharedArrayBuffer, reading = false): Promise<void> {
|
|
41
41
|
const { assets } = options
|
|
42
42
|
let metadata: ReturnType<typeof inspectDocument>
|
|
43
43
|
try { metadata = options.extension === 'pdf' ? { families: new Map(), codePoints: [] } : inspectDocument(options.data, options.extension, options) } catch (failure) { throw new BrowserRenderError('invalid-document', failure instanceof Error ? failure.message : String(failure)) }
|
|
44
|
+
const initialFont = { family: 'sans-serif', style: '', weight: 5, italic: 0, width: 5, pitch: 0, language: '', codePoints: [] }
|
|
45
|
+
let initialFontPending = options.extension !== 'pdf'
|
|
46
|
+
// Host font discovery runs while the engine downloads and compiles. The first
|
|
47
|
+
// synchronous font read consumes this response through the same bounded channel.
|
|
48
|
+
if (initialFontPending) send({ type: 'font', request: initialFont, known: [] })
|
|
44
49
|
const response = await fetch(assets.dataUrl, { credentials: 'same-origin', signal: lifetime.signal })
|
|
45
50
|
if (!response.ok) throw new BrowserRenderError('unavailable', `LibreOffice data request failed: ${response.status}`)
|
|
46
51
|
const data = await response.arrayBuffer()
|
|
@@ -48,7 +53,7 @@ async function open(options: WorkerOptions, channel: SharedArrayBuffer, editing
|
|
|
48
53
|
let resolveFonts: ReturnType<typeof createFontReader> | undefined
|
|
49
54
|
module = await scope.createLibreOfficeModule({
|
|
50
55
|
noInitialRun: true,
|
|
51
|
-
dshOnCallback(type: number, payload: string) {
|
|
56
|
+
dshOnCallback(type: number, payload: string) { reader?.callback(type, payload) },
|
|
52
57
|
mainScriptUrlOrBlob: assets.loaderUrl,
|
|
53
58
|
locateFile(name: string) {
|
|
54
59
|
const basename = name.slice(name.lastIndexOf('/') + 1)
|
|
@@ -70,10 +75,14 @@ async function open(options: WorkerOptions, channel: SharedArrayBuffer, editing
|
|
|
70
75
|
loaded.FS.writeFile('/dsh/fonts.conf', new TextEncoder().encode(memoryFontConfig(options.fontFallbacks, metadata.families.values())))
|
|
71
76
|
Object.assign(loaded.ENV, { HOME: '/dsh/profile', TMPDIR: '/tmp', FONTCONFIG_FILE: '/dsh/fonts.conf', LOK_HOST_ALLOWLIST: '^$' })
|
|
72
77
|
resolveFonts = createFontReader(channel, options.timeoutMs, options.maxLoadedFontBytes, metadata.families,
|
|
73
|
-
|
|
78
|
+
(request, known) => {
|
|
79
|
+
if (initialFontPending) initialFontPending = false
|
|
80
|
+
else send({ type: 'font', request, known })
|
|
81
|
+
}, () => send({ type: 'font-next' }),
|
|
74
82
|
(path, bytes) => loaded.FS.writeFile(path, bytes), families => send({ type: 'missing-fonts', families }))
|
|
75
|
-
|
|
76
|
-
|
|
83
|
+
if (options.extension === 'pdf') {
|
|
84
|
+
for (const family of new Set(options.fontFallbacks.flat())) resolveFonts({ ...initialFont, family, mode: 'full' })
|
|
85
|
+
} else resolveFonts(initialFont)
|
|
77
86
|
}],
|
|
78
87
|
onAbort() { fatal = true },
|
|
79
88
|
print() {}, printErr() {},
|
|
@@ -93,18 +102,18 @@ async function open(options: WorkerOptions, channel: SharedArrayBuffer, editing
|
|
|
93
102
|
module.FS.writeFile(input, options.data)
|
|
94
103
|
office = module.ccall('dsh_lok_initialize', 'number', ['string', 'string'], [assets.programDirectory, 'file:///dsh/profile'])
|
|
95
104
|
if (!office) throw error('invalid-document')
|
|
96
|
-
document = module.ccall('dsh_lok_document_load', 'number', ['number', 'string', 'string'], [office, `file://${input}`,
|
|
105
|
+
document = module.ccall('dsh_lok_document_load', 'number', ['number', 'string', 'string'], [office, `file://${input}`, 'ReadOnly=true,EnableMacrosExecution=false'])
|
|
97
106
|
if (!document || !call('dsh_lok_document_initialize_rendering', [document])) throw error('invalid-document')
|
|
98
107
|
const type = call('dsh_lok_document_type', [document])
|
|
99
108
|
if (type !== (options.extension === 'docx' || options.extension === 'doc' ? 0 : options.extension === 'xlsx' || options.extension === 'xls' ? 1 : 2)) throw new BrowserRenderError('invalid-document', 'The document type does not match its extension.')
|
|
100
|
-
if (
|
|
101
|
-
|
|
102
|
-
event => send({ type: '
|
|
109
|
+
if (reading) {
|
|
110
|
+
reader = new OfficeEngine(module, document, options.extension as OfficeDocumentFormat,
|
|
111
|
+
event => send({ type: 'office-event', event }), failure => {
|
|
103
112
|
fatal ||= failure instanceof WebAssembly.RuntimeError
|
|
104
|
-
send({ type: '
|
|
105
|
-
}
|
|
106
|
-
await
|
|
107
|
-
pages =
|
|
113
|
+
send({ type: 'office-event', event: { type: 'error', code: 'render-failed', message: failure instanceof Error ? failure.message : String(failure) } })
|
|
114
|
+
})
|
|
115
|
+
await reader.start()
|
|
116
|
+
pages = reader.state.parts.map((part, index) => ({ ...part, x: 0, y: 0, part: type === 0 ? -1 : index }))
|
|
108
117
|
return
|
|
109
118
|
}
|
|
110
119
|
throw new BrowserRenderError('invalid-document', 'Office documents require a retained session.')
|
|
@@ -117,8 +126,8 @@ function render(request: BrowserTileRequest): { width: number; height: number; r
|
|
|
117
126
|
}
|
|
118
127
|
|
|
119
128
|
function dispose(): void {
|
|
120
|
-
|
|
121
|
-
|
|
129
|
+
reader?.stop()
|
|
130
|
+
reader = undefined
|
|
122
131
|
try {
|
|
123
132
|
if (module && !fatal) {
|
|
124
133
|
if (pdf) { const value = pdf; pdf = 0; if (!call('dsh_pdf_destroy', [value])) throw error('render-failed') }
|
|
@@ -137,31 +146,26 @@ function dispose(): void {
|
|
|
137
146
|
|
|
138
147
|
scope.onmessage = (event: MessageEvent<OwnerMessage>) => {
|
|
139
148
|
const message = event.data
|
|
140
|
-
if (message.type === 'dispose') { lifetime.abort();
|
|
149
|
+
if (message.type === 'dispose') { lifetime.abort(); reader?.stop() }
|
|
141
150
|
serial = serial.then(async () => {
|
|
142
151
|
try {
|
|
143
152
|
switch (message.type) {
|
|
144
|
-
case 'open': await open(message.options, message.channel, message.
|
|
153
|
+
case 'open': await open(message.options, message.channel, message.office); send({ type: 'opened', id: message.id, pages: pages.map(({ width, height }) => ({ width, height })), ...(reader ? { office: reader.state } : {}) }); break
|
|
145
154
|
case 'tile': { const tile = render(message.request); send({ type: 'tile', id: message.id, tile }, [tile.rgba.buffer]); break }
|
|
146
|
-
case '
|
|
147
|
-
if (!
|
|
148
|
-
const tile =
|
|
155
|
+
case 'office-tile': {
|
|
156
|
+
if (!reader) throw new BrowserRenderError('disposed', 'Office reader is closed.')
|
|
157
|
+
const tile = reader.render(message.request)
|
|
149
158
|
send({ type: 'tile', id: message.id, tile }, [tile.rgba.buffer])
|
|
150
159
|
break
|
|
151
160
|
}
|
|
152
|
-
case '
|
|
153
|
-
if (!
|
|
154
|
-
const result = await
|
|
155
|
-
send({ type: '
|
|
156
|
-
break
|
|
157
|
-
}
|
|
158
|
-
case 'capture': {
|
|
159
|
-
if (!editor) throw new BrowserRenderError('disposed', 'Office editor is closed.')
|
|
160
|
-
const capture = await editor.capture(message.request)
|
|
161
|
-
send({ type: 'captured', id: message.id, capture }, capture.tiles.map(tile => tile.rgba.buffer))
|
|
161
|
+
case 'office-operation': {
|
|
162
|
+
if (!reader) throw new BrowserRenderError('disposed', 'Office reader is closed.')
|
|
163
|
+
const result = await reader.operation(message.operation)
|
|
164
|
+
send({ type: 'office-result', id: message.id, ...result })
|
|
162
165
|
break
|
|
163
166
|
}
|
|
164
167
|
case 'dispose': dispose(); send({ type: 'disposed', id: message.id }); scope.close(); break
|
|
168
|
+
default: throw new BrowserRenderError('render-failed', 'Unsupported Office Worker request.')
|
|
165
169
|
}
|
|
166
170
|
} catch (failure) {
|
|
167
171
|
fatal ||= failure instanceof WebAssembly.RuntimeError
|
|
@@ -15,6 +15,9 @@ const helpers = ['rendering', 'engine-rendering', 'sheet-geometry'];
|
|
|
15
15
|
/** Compile browser-only JavaScript without copying any engine bytes into the main package. */
|
|
16
16
|
export function buildBrowserSources(buildEntry = true) {
|
|
17
17
|
if (buildEntry) buildKitSources();
|
|
18
|
+
// TypeScript does not remove declarations for deleted source files.
|
|
19
|
+
rmSync(join(source, 'lib/types'), { recursive: true, force: true });
|
|
20
|
+
rmSync(join(source, 'lib/tsconfig.tsbuildinfo'), { force: true });
|
|
18
21
|
pnpm(['exec', 'tsc', '-b', 'packages/browser'], { cwd: root });
|
|
19
22
|
pnpm(['exec', 'tsdown'], { cwd: source });
|
|
20
23
|
const target = join(directory, 'lib/browser');
|
|
@@ -23,6 +26,7 @@ export function buildBrowserSources(buildEntry = true) {
|
|
|
23
26
|
for (const name of ['index.js', 'worker.js']) cpSync(regularFile(source, `lib/${name}`), join(target, name));
|
|
24
27
|
for (const name of readdirSync(join(source, 'lib/types')).filter(name => name.endsWith('.d.ts')))
|
|
25
28
|
cpSync(join(source, 'lib/types', name), join(target, 'types', name));
|
|
29
|
+
rmSync(join(directory, 'sources/browser'), { recursive: true, force: true });
|
|
26
30
|
for (const path of ['packages/browser/src', 'packages/browser/package.json', 'packages/browser/tsconfig.json', 'packages/browser/tsdown.config.ts', 'scripts/build-browser.mjs']) {
|
|
27
31
|
const dest = join(directory, 'sources/browser', path);
|
|
28
32
|
mkdirSync(dirname(dest), { recursive: true }); cpSync(join(root, path), dest, { recursive: true });
|
|
@@ -50,6 +54,10 @@ export function verifyBrowserPackage(target = directory, engineDirectory = join(
|
|
|
50
54
|
'Browser entry must be in the matching main package');
|
|
51
55
|
for (const file of ['lib/browser/index.js', 'lib/browser/worker.js', 'lib/browser/types/index.d.ts', 'lib/browser-assets.js']) regularFile(target, file);
|
|
52
56
|
for (const helper of helpers) regularFile(target, `lib/${helper}.js`);
|
|
57
|
+
const declarations = join(target, 'lib/browser/types');
|
|
58
|
+
const declarationText = readdirSync(declarations).filter(name => name.endsWith('.d.ts')).map(name => readFileSync(join(declarations, name), 'utf8')).join('\n');
|
|
59
|
+
assert(declarationText.includes('openOfficeDocument') && !declarationText.includes('openEditor')
|
|
60
|
+
&& !readdirSync(declarations).some(name => name.startsWith('editor')), 'Browser package retains a removed editing SDK');
|
|
53
61
|
const receipt = readJson(regularFile(target, 'lib/browser/assets.json'));
|
|
54
62
|
const worker = receipt.files?.worker;
|
|
55
63
|
assert(receipt.schemaVersion === 1 && worker?.path === 'lib/browser/worker.js'
|
|
@@ -58,7 +66,7 @@ export function verifyBrowserPackage(target = directory, engineDirectory = join(
|
|
|
58
66
|
const engine = readJson(join(engineDirectory, 'prebuilds.json')).engine;
|
|
59
67
|
const module = new WebAssembly.Module(readFileSync(join(engineDirectory, engine.wasm)));
|
|
60
68
|
const exports = new Set(WebAssembly.Module.exports(module).map(entry => entry.name));
|
|
61
|
-
for (const name of ['dsh_lok_document_paint', 'dsh_lok_document_composition', 'dsh_lok_document_create_view', 'dsh_lok_document_part_info', 'dsh_pdf_open', 'dsh_pdf_paint'])
|
|
69
|
+
for (const name of ['dsh_lok_document_paint', 'dsh_lok_document_configure_view', 'dsh_lok_document_composition', 'dsh_lok_document_create_view', 'dsh_lok_document_part_info', 'dsh_pdf_open', 'dsh_pdf_paint'])
|
|
62
70
|
assert(exports.has(name) || exports.has(`_${name}`), `WASM has no browser export: ${name}`);
|
|
63
71
|
return receipt;
|
|
64
72
|
}
|
|
@@ -53,7 +53,7 @@ export function releaseAssetUrl(version, file) {
|
|
|
53
53
|
/** Return the complete declared matrix, including targets without built assets. */
|
|
54
54
|
export function packageMatrix(repo = root) {
|
|
55
55
|
return readdirSync(join(repo, 'packages'), { withFileTypes: true })
|
|
56
|
-
.filter((entry) => entry.isDirectory() && !['entry', 'browser'].includes(entry.name))
|
|
56
|
+
.filter((entry) => entry.isDirectory() && !['entry', 'browser', 'fonts'].includes(entry.name))
|
|
57
57
|
.sort((a, b) => a.name.localeCompare(b.name))
|
|
58
58
|
.map((entry) => {
|
|
59
59
|
const dir = join(repo, 'packages', entry.name);
|
|
@@ -88,7 +88,7 @@ export function verifyEngineMetadata(manifest, prebuild) {
|
|
|
88
88
|
}
|
|
89
89
|
const exact = (actual, expected) => JSON.stringify(actual) === JSON.stringify(expected);
|
|
90
90
|
if (prebuild.platform === 'wasm') {
|
|
91
|
-
assert(
|
|
91
|
+
assert(manifest.os === undefined && manifest.cpu === undefined && manifest.libc === undefined, 'WASM package must install across operating systems, CPUs and libc variants');
|
|
92
92
|
assert(engine?.kind === 'wasm' && engine.loader === 'assets/soffice.cjs' && engine.wasm === 'assets/soffice.wasm'
|
|
93
93
|
&& engine.data === 'assets/soffice.data' && engine.metadata === 'assets/soffice.data.js.metadata'
|
|
94
94
|
&& engine.programDirectory === '/instdir/program', 'Unsupported WASM engine paths');
|
|
@@ -230,7 +230,7 @@ export function verifyEnginePackage(dir) {
|
|
|
230
230
|
for (const key of ['loader', 'wasm', 'data', 'metadata']) regularFile(dir, engine[key]);
|
|
231
231
|
const module = new WebAssembly.Module(readFileSync(join(dir, engine.wasm)));
|
|
232
232
|
const exports = new Set(WebAssembly.Module.exports(module).filter((entry) => entry.kind === 'function').map((entry) => entry.name.replace(/^_/, '')));
|
|
233
|
-
for (const name of ['dsh_lok_initialize', 'dsh_lok_document_load', 'dsh_lok_document_save_pdf', 'dsh_lok_document_destroy', 'dsh_lok_destroy', 'dsh_lok_error', 'malloc', 'free']) {
|
|
233
|
+
for (const name of ['dsh_lok_initialize', 'dsh_lok_document_load', 'dsh_lok_document_save_pdf', 'dsh_lok_document_export', 'dsh_lok_document_destroy', 'dsh_lok_destroy', 'dsh_lok_error', 'malloc', 'free']) {
|
|
234
234
|
assert(exports.has(name), `WASM engine missing conversion export: ${name}`);
|
|
235
235
|
}
|
|
236
236
|
const dataSize = statSync(join(dir, engine.data)).size;
|
|
@@ -1,50 +0,0 @@
|
|
|
1
|
-
import type { BrowserTile } from './types.ts';
|
|
2
|
-
import type { EmscriptenModule } from './engine-types.ts';
|
|
3
|
-
import type { EditorOperation } from './protocol.ts';
|
|
4
|
-
import type { BrowserEditorCapture, BrowserEditorCaptureRequest, BrowserEditorEvent, BrowserEditorFormat, BrowserEditorSnapshot, BrowserEditorState, BrowserEditorTileRequest } from './editor-types.ts';
|
|
5
|
-
/** One editor, driven exclusively by its owning Worker's messages and timer. */
|
|
6
|
-
export declare class EditorEngine {
|
|
7
|
-
private readonly module;
|
|
8
|
-
private readonly document;
|
|
9
|
-
private readonly format;
|
|
10
|
-
private readonly maxBytes;
|
|
11
|
-
private readonly emit;
|
|
12
|
-
private readonly failure;
|
|
13
|
-
private readonly readOnly;
|
|
14
|
-
state: BrowserEditorState;
|
|
15
|
-
private timer;
|
|
16
|
-
private readonly callbacks;
|
|
17
|
-
private phase;
|
|
18
|
-
private modified;
|
|
19
|
-
private geometryDirty;
|
|
20
|
-
private stopped;
|
|
21
|
-
private renderView;
|
|
22
|
-
private commandResult;
|
|
23
|
-
constructor(module: EmscriptenModule, document: number, format: BrowserEditorFormat, maxBytes: number, emit: (event: BrowserEditorEvent) => void, failure: (error: unknown) => void, readOnly?: boolean);
|
|
24
|
-
private call;
|
|
25
|
-
private text;
|
|
26
|
-
private invoke;
|
|
27
|
-
private requireOpen;
|
|
28
|
-
/** Collect callbacks without reentering LibreOffice from inside its own callback. */
|
|
29
|
-
callback(type: number, payload: string): void;
|
|
30
|
-
/** Register callbacks after document initialization, then begin bounded event slices. */
|
|
31
|
-
start(): Promise<void>;
|
|
32
|
-
private schedule;
|
|
33
|
-
/** Cancel the owned timer before the document or office is destroyed. */
|
|
34
|
-
stop(): void;
|
|
35
|
-
private refreshGeometry;
|
|
36
|
-
private pump;
|
|
37
|
-
private flush;
|
|
38
|
-
/** Apply one ordered command and collect the resulting notifications before acknowledgement. */
|
|
39
|
-
operation(operation: EditorOperation): Promise<{
|
|
40
|
-
text?: string;
|
|
41
|
-
snapshot?: BrowserEditorSnapshot;
|
|
42
|
-
}>;
|
|
43
|
-
private checkText;
|
|
44
|
-
private command;
|
|
45
|
-
/** Painting preserves the active selection and cannot itself mark the document edited. */
|
|
46
|
-
render(request: BrowserEditorTileRequest): BrowserTile;
|
|
47
|
-
/** Capture one bounded batch with no input interleaving; cached tiles are checked at the same generation. */
|
|
48
|
-
capture(request: BrowserEditorCaptureRequest): Promise<BrowserEditorCapture>;
|
|
49
|
-
}
|
|
50
|
-
//# sourceMappingURL=editor-engine.d.ts.map
|
|
@@ -1,141 +0,0 @@
|
|
|
1
|
-
/** Persistent, single-user Office editing in one browser Worker. */
|
|
2
|
-
import type { BrowserDocumentOptions, BrowserPage, BrowserRenderErrorCode, BrowserTile } from './types.ts';
|
|
3
|
-
/** Formats qualified for editing and saving in their original container. */
|
|
4
|
-
export type BrowserEditorFormat = 'doc' | 'docx' | 'xls' | 'xlsx' | 'ppt' | 'pptx';
|
|
5
|
-
/** Editor source, engine resources and Host font service. */
|
|
6
|
-
export type BrowserEditorOptions = Omit<BrowserDocumentOptions, 'extension'> & {
|
|
7
|
-
readonly extension: BrowserEditorFormat;
|
|
8
|
-
readonly readOnly?: boolean;
|
|
9
|
-
};
|
|
10
|
-
/** Document coordinates in CSS pixels at 96 DPI, independent of zoom. */
|
|
11
|
-
export interface BrowserRectangle extends BrowserPage {
|
|
12
|
-
readonly x: number;
|
|
13
|
-
readonly y: number;
|
|
14
|
-
}
|
|
15
|
-
/** A Writer document, Calc worksheet or Impress slide, measured when selected. */
|
|
16
|
-
export interface BrowserEditorPart extends BrowserPage {
|
|
17
|
-
readonly name: string;
|
|
18
|
-
}
|
|
19
|
-
/** Geometry and current editing state; snapshots are immutable to consumers. */
|
|
20
|
-
export interface BrowserEditorState {
|
|
21
|
-
readonly documentType: 'text' | 'spreadsheet' | 'presentation';
|
|
22
|
-
readonly revision: number;
|
|
23
|
-
/** Changes whenever document pixels or geometry are invalidated, independently of saved edits. */
|
|
24
|
-
readonly renderGeneration: number;
|
|
25
|
-
readonly part: number;
|
|
26
|
-
readonly parts: readonly BrowserEditorPart[];
|
|
27
|
-
/** Writer's visible page rectangles within its continuous document. */
|
|
28
|
-
readonly pages: readonly BrowserRectangle[];
|
|
29
|
-
readonly cursor: BrowserRectangle | null;
|
|
30
|
-
readonly cursorVisible: boolean;
|
|
31
|
-
readonly selection: readonly BrowserRectangle[];
|
|
32
|
-
readonly graphicSelection: BrowserRectangle | null;
|
|
33
|
-
readonly cellAddress: string;
|
|
34
|
-
readonly cellFormula: string;
|
|
35
|
-
/** UNO command names to their current values, including undo and formatting state. */
|
|
36
|
-
readonly commands: Readonly<Record<string, string>>;
|
|
37
|
-
}
|
|
38
|
-
/** Input codes follow LibreOfficeKit; pointer coordinates use CSS pixels at 96 DPI. */
|
|
39
|
-
export type BrowserEditorInput = {
|
|
40
|
-
readonly type: 'key';
|
|
41
|
-
readonly action: 'down' | 'up';
|
|
42
|
-
readonly character: number;
|
|
43
|
-
readonly key: number;
|
|
44
|
-
} | {
|
|
45
|
-
readonly type: 'pointer';
|
|
46
|
-
readonly action: 'down' | 'up' | 'move';
|
|
47
|
-
readonly x: number;
|
|
48
|
-
readonly y: number;
|
|
49
|
-
readonly buttons: number;
|
|
50
|
-
readonly modifiers: number;
|
|
51
|
-
readonly clicks: number;
|
|
52
|
-
} | {
|
|
53
|
-
readonly type: 'composition';
|
|
54
|
-
readonly action: 'update' | 'end';
|
|
55
|
-
readonly text: string;
|
|
56
|
-
};
|
|
57
|
-
/** Typed UNO property values, serialized inside the Worker. */
|
|
58
|
-
export type BrowserCommandArguments = Readonly<Record<string, {
|
|
59
|
-
readonly type: 'string' | 'boolean' | 'long' | 'short' | 'float';
|
|
60
|
-
readonly value: string | boolean | number;
|
|
61
|
-
}>>;
|
|
62
|
-
/** One view-relative render request. */
|
|
63
|
-
export interface BrowserEditorTileRequest extends BrowserRectangle {
|
|
64
|
-
readonly part: number;
|
|
65
|
-
readonly scale: number;
|
|
66
|
-
}
|
|
67
|
-
/** One bounded, serialized capture; cached pixels must be current at generation. */
|
|
68
|
-
export interface BrowserEditorCaptureRequest {
|
|
69
|
-
readonly generation?: number;
|
|
70
|
-
readonly maxPixels: number;
|
|
71
|
-
readonly selection?: {
|
|
72
|
-
readonly sheet?: string;
|
|
73
|
-
readonly range?: string;
|
|
74
|
-
readonly scale: number;
|
|
75
|
-
};
|
|
76
|
-
readonly tiles: readonly {
|
|
77
|
-
readonly request: BrowserEditorTileRequest;
|
|
78
|
-
readonly cached?: BrowserTile;
|
|
79
|
-
}[];
|
|
80
|
-
}
|
|
81
|
-
/** Immutable pixels and geometry observed before a later input can run. */
|
|
82
|
-
export interface BrowserEditorCapture {
|
|
83
|
-
readonly state: BrowserEditorState;
|
|
84
|
-
readonly tiles: readonly BrowserTile[];
|
|
85
|
-
readonly regions: readonly BrowserEditorCaptureRegion[];
|
|
86
|
-
}
|
|
87
|
-
/** A Writer page, slide, or bounded worksheet data area ready for raster export. */
|
|
88
|
-
export interface BrowserEditorCaptureRegion {
|
|
89
|
-
readonly part: number;
|
|
90
|
-
readonly rectangle: BrowserRectangle;
|
|
91
|
-
readonly sheet?: string;
|
|
92
|
-
readonly range?: string;
|
|
93
|
-
}
|
|
94
|
-
/** A complete Office export, associated with the edits it includes. */
|
|
95
|
-
export interface BrowserEditorSnapshot {
|
|
96
|
-
readonly data: Uint8Array<ArrayBuffer>;
|
|
97
|
-
readonly revision: number;
|
|
98
|
-
readonly extension: BrowserEditorFormat;
|
|
99
|
-
}
|
|
100
|
-
/** State and tile events are delivered in engine order. A null region invalidates the entire part. */
|
|
101
|
-
export type BrowserEditorEvent = {
|
|
102
|
-
readonly type: 'state';
|
|
103
|
-
readonly state: BrowserEditorState;
|
|
104
|
-
} | {
|
|
105
|
-
readonly type: 'invalidate';
|
|
106
|
-
readonly part: number;
|
|
107
|
-
readonly rectangle: BrowserRectangle | null;
|
|
108
|
-
readonly revision: number;
|
|
109
|
-
} | {
|
|
110
|
-
readonly type: 'error';
|
|
111
|
-
readonly code: BrowserRenderErrorCode;
|
|
112
|
-
readonly message: string;
|
|
113
|
-
};
|
|
114
|
-
/** A retained editable document; hiding a view does not dispose its session. */
|
|
115
|
-
export interface BrowserEditor {
|
|
116
|
-
/** Latest engine snapshot; subscribe for later changes. */
|
|
117
|
-
readonly state: BrowserEditorState;
|
|
118
|
-
/** Observe state and invalidation events; one throwing observer cannot interrupt others. */
|
|
119
|
-
subscribe(listener: (event: BrowserEditorEvent) => void): () => void;
|
|
120
|
-
/** Apply one keyboard, pointer or IME event in order. */
|
|
121
|
-
input(event: BrowserEditorInput): Promise<void>;
|
|
122
|
-
/** Execute a LibreOffice editing command, such as .uno:Bold or .uno:Undo. */
|
|
123
|
-
dispatch(command: string, args?: BrowserCommandArguments): Promise<void>;
|
|
124
|
-
/** Insert plain UTF-8 text at the current selection, with engine-owned undo. */
|
|
125
|
-
paste(text: string): Promise<void>;
|
|
126
|
-
/** Read the current selection as plain text. */
|
|
127
|
-
copy(): Promise<string>;
|
|
128
|
-
/** Activate one worksheet or slide. Writer has a single part. */
|
|
129
|
-
setPart(part: number): Promise<void>;
|
|
130
|
-
/** Inform the engine of visible document coordinates and zoom. */
|
|
131
|
-
setViewport(rectangle: BrowserRectangle, scale: number): Promise<void>;
|
|
132
|
-
/** Render without changing the document; callers own caching and invalidation. */
|
|
133
|
-
renderTile(request: BrowserEditorTileRequest, signal?: AbortSignal): Promise<BrowserTile>;
|
|
134
|
-
/** Capture bounded tiles under one render generation; an empty list is an idle barrier. */
|
|
135
|
-
capture(request: BrowserEditorCaptureRequest, signal?: AbortSignal): Promise<BrowserEditorCapture>;
|
|
136
|
-
/** Export the current revision. Only a successful Host write confirms it saved. */
|
|
137
|
-
save(): Promise<BrowserEditorSnapshot>;
|
|
138
|
-
/** Await engine and Worker disposal; repeated calls share completion. */
|
|
139
|
-
dispose(): Promise<void>;
|
|
140
|
-
}
|
|
141
|
-
//# sourceMappingURL=editor-types.d.ts.map
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import type { BrowserEditor, BrowserEditorOptions } from './editor-types.ts';
|
|
2
|
-
/**
|
|
3
|
-
* Import an Office document once and retain its editable model until disposal.
|
|
4
|
-
* @param options - OOXML bytes, version-matched engine resources and Host fonts.
|
|
5
|
-
* @param signal - Document lifetime; visibility changes must not abort it.
|
|
6
|
-
* @returns An editor whose exports remain unsaved until the caller writes them.
|
|
7
|
-
*/
|
|
8
|
-
export declare function openEditor(options: BrowserEditorOptions, signal?: AbortSignal): Promise<BrowserEditor>;
|
|
9
|
-
//# sourceMappingURL=editor.d.ts.map
|