diff --git a/README.md b/README.md index 86a26f3..926f2a3 100644 --- a/README.md +++ b/README.md @@ -321,6 +321,7 @@ give one. There are two ways in, both shown with copy-ready snippets in | ------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | **A · `genoffice mcp`** (recommended) | A stdio server the assistant starts itself; GenOffice does not need to be open. One tool per command (`info`, `convert`, `create_docx`, `create_xlsx`, `create_pptx`, `create_pdf`, `docs_read` / `docs_apply` / `docs_check`, `sheet_*`, `slides_*`, `render`, `guide`, `search`, `image`, `media`, `open`) plus the staged deck flow `deck_start` → `deck_page` → `deck_build` → `deck_replace`. Ops, specs and Markdown are passed inline, so a client without a file system still works. | | **B · Local HTTP server** | Runs inside the GenOffice app on `http://127.0.0.1:3093/mcp` (Streamable HTTP, with legacy SSE). Its tools drive a visible Word editor tab: `create_session`, `insert_content`, `replace_blocks`, `apply_ops`, `read_document`, `save_session`, and you watch the document take shape. Off by default; switch it on in the same settings pane. | +| **C · `genoffice mcp --http`** | The stdio tool set as a Streamable HTTP server for clients on other machines: a container, a sandbox, a shared box on your network. Files travel with the calls: `PUT /files/` uploads one and returns a URL, every `file` parameter takes an http(s) URL, and a tool that writes a file hands it back as a download URL plus, when small, the bytes as an MCP resource. `--host 0.0.0.0` opens it to the network, `--token` protects it. | ```bash # Claude Code @@ -332,6 +333,24 @@ claude mcp add --transport stdio genoffice -- genoffice mcp { "mcpServers": { "genoffice": { "command": "genoffice", "args": ["mcp"] } } } ``` +```bash +# On the machine that has GenOffice (private network; add --token for a shared box) +genoffice mcp --http 3093 --host 0.0.0.0 --token "$GENOFFICE_MCP_TOKEN" + +# From the client: upload, then use the URL wherever a tool takes a file +curl -T report.docx -H "Authorization: Bearer $GENOFFICE_MCP_TOKEN" http://server:3093/files/ +# → { "url": "http://server:3093/files//report.docx", ... } +# docs_read({ "file": "http://server:3093/files//report.docx" }) +# docs_apply(...) → output_url, downloadable with curl -o +``` + +Over HTTP every session gets a private scratch folder, relative paths and deck +folders resolve inside it, `open` is not offered, and with +`GENOFFICE_ALLOWED_ROOTS` unset the tools cannot leave the server's own file +store. `render`, `convert` to PDF and `create_pdf` still start a hidden +GenOffice process, so a headless host needs the app installed and a virtual +display (`xvfb-run`). + `genoffice` here is the CLI shipped inside the app (on macOS `/Applications/GenOffice.app/Contents/Resources/cli/genoffice`; the settings pane prints the exact path for your install). The server carries its own diff --git a/apps/docs/electron.vite.config.ts b/apps/docs/electron.vite.config.ts index a7d0713..f9b170f 100644 --- a/apps/docs/electron.vite.config.ts +++ b/apps/docs/electron.vite.config.ts @@ -6,6 +6,14 @@ import { defineConfig, externalizeDepsPlugin } from 'electron-vite' // node_modules is a symlink into the main checkout, so bare specifiers would // silently bundle the other checkout's (possibly stale) code. const localAlias = { + '@genoffice/docx-engine/lazy-media': resolve( + __dirname, + '../../packages/docx-engine/src/lazy-media.ts', + ), + '@genoffice/docx-engine/zip-splice': resolve( + __dirname, + '../../packages/docx-engine/src/zip-splice.ts', + ), '@genoffice/docx-engine': resolve(__dirname, '../../packages/docx-engine/src/index.ts'), } @@ -17,7 +25,9 @@ export default defineConfig({ // (same setup as apps/slides). main: { plugins: [ - externalizeDepsPlugin({ exclude: ['@genoffice/electron-utils', '@genoffice/font-metrics'] }), + externalizeDepsPlugin({ + exclude: ['@genoffice/docx-engine', '@genoffice/electron-utils', '@genoffice/font-metrics'], + }), ], resolve: { alias: localAlias }, }, diff --git a/apps/docs/src/main/docs-main.ts b/apps/docs/src/main/docs-main.ts index bd1f0f2..827054c 100644 --- a/apps/docs/src/main/docs-main.ts +++ b/apps/docs/src/main/docs-main.ts @@ -34,6 +34,7 @@ import { nativeImage, net, shell, + webContents, } from 'electron' import { appMenuLabels, @@ -51,6 +52,7 @@ import { showOpenDialogWithMemory, showSaveDialogWithMemory, aboutMenuItem, + checkUpdatesMenuItem, toggleDevToolsItem, windowMenuTemplate, type HeadlessExportFormat, @@ -123,6 +125,15 @@ import type { import { ATTACHMENT_IMAGE_EXTS } from '../shared/ipc' import { findDocxPath } from '../shared/open-file' import { atomicWriteFile, looksLikeZip } from './atomic-write' +import { + adoptLazyMediaHashes, + forgetLazyMediaOwner, + materializeLazyDocx, + openLazyDocx, + pointLazyMediaAt, + readLazyMedia, + registerLazyMediaProtocol, +} from './lazy-media' import { commitDocPasswordSave, currentDocPasswordIntentRevision, @@ -2385,14 +2396,14 @@ export function removeStarredFiles(filePaths: string[]): void { // ---- original archive (pass-through base: original file archived by content hash) ---- -async function archiveOriginal(filePath: string, bytes: Buffer): Promise { - const hash = sha256Hex(bytes) +async function archiveOriginal(filePath: string, hash: string, size: number): Promise { + // a copy larger than the whole cap would only evict every other original + if (size > ORIGINALS_MAX_BYTES) return const dir = userDataPath('originals') await mkdir(dir, { recursive: true }) const target = join(dir, `${hash}.docx`) if (!existsSync(target)) await copyFile(filePath, target) void pruneOriginals(dir) - return hash } const ORIGINALS_MAX_BYTES = 500 * 1024 * 1024 @@ -2484,6 +2495,7 @@ function dropDocWriter(wcId: number): void { imageExportTemps.delete(wcId) imageExportDirs.delete(wcId) docDiskStates.delete(wcId) + forgetLazyMediaOwner(wcId) // Destroyed renderers count as torn down too: window-close paths never run // teardownDocsRenderer, but an in-flight save handler resuming after the // destruction must still fail its re-check (wcIds are never reused, so the @@ -2497,11 +2509,11 @@ const docDiskStates = new Map>() const sha256Hex = (bytes: Buffer) => createHash('sha256').update(bytes).digest('hex') -async function rememberDiskState(wcId: number, filePath: string, bytes: Buffer): Promise { +async function rememberDiskState(wcId: number, filePath: string, hash: string): Promise { try { const s = await stat(filePath) const states = docDiskStates.get(wcId) ?? new Map() - states.set(filePath, { mtimeMs: s.mtimeMs, size: s.size, hash: sha256Hex(bytes) }) + states.set(filePath, { mtimeMs: s.mtimeMs, size: s.size, hash }) docDiskStates.set(wcId, states) } catch { /* unstatable target: skip tracking; the next save simply won't flag a conflict */ @@ -2530,6 +2542,7 @@ async function diskChangedExternally(wcId: number, filePath: string): Promise { + const wc = webContents.fromId(wcId) + const parent = docsShellWindow ?? (wc && BrowserWindow.fromWebContents(wc)) ?? mainWindow + const options = { type: 'error' as const, message: tm('dlgOpenDoc'), detail } + if (parent && !parent.isDestroyed()) await dialog.showMessageBox(parent, options) + else await dialog.showMessageBox(options) +} + async function loadDocx( filePath: string, wcId: number, @@ -2618,7 +2642,14 @@ async function loadDocx( ): Promise { if (typeof filePath !== 'string' || !/\.docx$/i.test(filePath)) return null if (!existsSync(filePath)) return null - const original = await readFile(filePath) + const size = (await stat(filePath)).size + const lazy = await openLazyDocx(filePath, wcId) + if ((lazy?.bytes.length ?? size) > MAX_OPEN_BYTES) { + const mb = MAX_OPEN_BYTES / 1024 / 1024 + await showOpenError(wcId, `${basename(filePath)}: ${tm('errTooLarge', { mb })}`) + return null + } + const original = lazy?.bytes ?? (await readFile(filePath)) // Password-protected docx (ECMA-376 CFB container): without a password, hand // back a marker — the renderer prompts and retries via docs:open-decrypt. // No side effects (recents/write grant) until the password checks out. @@ -2634,7 +2665,8 @@ async function loadDocx( } // the archive keeps the on-disk original as-is (encrypted ones included: they // reopen with the user's password), so a bad save never loses the source file - const hash = await archiveOriginal(filePath, original) + const hash = lazy?.hash ?? sha256Hex(original) + await archiveOriginal(filePath, hash, size) const recovery = await maybeRecoverDocBytes(filePath, plainBytes) let bytes = recovery.bytes let recovered = recovery.recovered @@ -2648,12 +2680,13 @@ async function loadDocx( recovered = false } } + if (recovered) await adoptLazyMediaHashes(bytes, filePath, wcId) pushRecent(filePath) allowDocWrite(wcId, filePath) if (fileOpenedHook) fileOpenedHook(wcId, filePath) markDiskEncrypted(wcId, filePath, encrypted) // record the on-disk file, not the recovery copy: what matters is what save would overwrite - await rememberDiskState(wcId, filePath, original) + await rememberDiskState(wcId, filePath, hash) return { path: filePath, name: basename(filePath), @@ -3240,9 +3273,26 @@ export function registerProjectIpc(): void { const ALT_CHUNK_VIEWPORT = { width: 794, height: 1123, deviceScaleFactor: 2 } const ALT_CHUNK_HTML_MAX_CHARS = 64 * 1024 * 1024 +/** an encrypted save leaves no plain file to serve lazy pictures from: the + * renderer takes the materialized document back and leaves lazy mode */ +const reissuedDoc = ( + encrypted: boolean, + hashes: Set, + plain: Buffer, +): { data?: ArrayBuffer } => + encrypted && hashes.size > 0 + ? { + data: plain.buffer.slice( + plain.byteOffset, + plain.byteOffset + plain.byteLength, + ) as ArrayBuffer, + } + : {} + /** document/attachment/window IPC (everything except the AI proxy above) */ export function registerDocsIpc(): void { registerZoteroIpc() + void app.whenReady().then(registerLazyMediaProtocol) // Node fetch (undici) direct connections get reset under VPN/tun setups; retry over Chromium's stack setRescueFetch((url, init) => net.fetch(url, init)) setAiUserAgent(`GenOffice/${app.getVersion()}`) @@ -3440,19 +3490,24 @@ export function registerDocsIpc(): void { // Snapshot desired state: the disk password remains unchanged until the // atomic write succeeds, and a newer ribbon intent survives this save. const passwordState = snapshotDocPassword(event.sender.id, filePath) - const bytes = passwordState.password - ? encryptDocx(Buffer.from(data), passwordState.password) - : Buffer.from(data) + const { bytes: plain, hashes } = await materializeLazyDocx(Buffer.from(data)) + const bytes = passwordState.password ? encryptDocx(plain, passwordState.password) : plain await atomicWriteFile(filePath, bytes) // Teardown may have cleared all in-memory secrets while the atomic // write was pending. Never resurrect state for an orphaned renderer. if (tornDownWcIds.has(event.sender.id)) { return { ok: false, error: 'save target is not an opened document' } } - await rememberDiskState(event.sender.id, filePath, bytes) + await rememberDiskState(event.sender.id, filePath, sha256Hex(bytes)) if (tornDownWcIds.has(event.sender.id)) { return { ok: false, error: 'save target is not an opened document' } } + pointLazyMediaAt( + hashes, + filePath, + event.sender.id, + passwordState.password ? plain : undefined, + ) // Commit immediately after the final await: intents received during // post-write bookkeeping are included, with no later async race. const passwordIntentPending = commitDocPasswordSave( @@ -3462,7 +3517,11 @@ export function registerDocsIpc(): void { ) clearRecoveryCopy(filePath) pushRecent(filePath) - return { ok: true, passwordIntentPending } + return { + ok: true, + passwordIntentPending, + ...reissuedDoc(!!passwordState.password, hashes, plain), + } } catch (err) { return { ok: false, error: String(err) } } @@ -3561,13 +3620,18 @@ export function registerDocsIpc(): void { event.sender.id, typeof sourcePath === 'string' && sourcePath ? sourcePath : null, ) - const bytes = passwordState.password - ? encryptDocx(Buffer.from(data), passwordState.password) - : Buffer.from(data) + const { bytes: plain, hashes } = await materializeLazyDocx(Buffer.from(data)) + const bytes = passwordState.password ? encryptDocx(plain, passwordState.password) : plain await atomicWriteFile(result.filePath, bytes) if (tornDownWcIds.has(event.sender.id)) return { ok: false } allowDocWrite(event.sender.id, result.filePath) - await rememberDiskState(event.sender.id, result.filePath, bytes) + await rememberDiskState(event.sender.id, result.filePath, sha256Hex(bytes)) + pointLazyMediaAt( + hashes, + result.filePath, + event.sender.id, + passwordState.password ? plain : undefined, + ) if (tornDownWcIds.has(event.sender.id)) return { ok: false } const passwordIntentPending = commitDocPasswordSave( event.sender.id, @@ -3576,7 +3640,12 @@ export function registerDocsIpc(): void { ) pushRecent(result.filePath) notifyFileSaved(event.sender, result.filePath) - return { ok: true, path: result.filePath, passwordIntentPending } + return { + ok: true, + path: result.filePath, + passwordIntentPending, + ...reissuedDoc(!!passwordState.password, hashes, plain), + } } catch (err) { return { ok: false, error: String(err) } } @@ -3590,9 +3659,8 @@ export function registerDocsIpc(): void { if (tornDownWcIds.has(event.sender.id)) return { ok: false } const filePath = uniquePathIn(defaultSaveDir(), defaultName) const passwordState = snapshotDocPassword(event.sender.id, null) - const bytes = passwordState.password - ? encryptDocx(Buffer.from(data), passwordState.password) - : Buffer.from(data) + const { bytes: plain, hashes } = await materializeLazyDocx(Buffer.from(data)) + const bytes = passwordState.password ? encryptDocx(plain, passwordState.password) : plain await atomicWriteFile(filePath, bytes) // teardown may have happened while the write was in flight — the path is // freshly created, so rolling it back is safe (mirrors docs:write-recovery) @@ -3601,7 +3669,13 @@ export function registerDocsIpc(): void { return { ok: false } } allowDocWrite(event.sender.id, filePath) - await rememberDiskState(event.sender.id, filePath, bytes) + await rememberDiskState(event.sender.id, filePath, sha256Hex(bytes)) + pointLazyMediaAt( + hashes, + filePath, + event.sender.id, + passwordState.password ? plain : undefined, + ) if (tornDownWcIds.has(event.sender.id)) { await unlink(filePath).catch(() => {}) return { ok: false } @@ -3609,7 +3683,12 @@ export function registerDocsIpc(): void { const passwordIntentPending = commitDocPasswordSave(event.sender.id, passwordState, filePath) pushRecent(filePath) notifyFileSaved(event.sender, filePath) - return { ok: true, path: filePath, passwordIntentPending } + return { + ok: true, + path: filePath, + passwordIntentPending, + ...reissuedDoc(!!passwordState.password, hashes, plain), + } } catch (err) { return { ok: false, error: String(err) } } @@ -3649,9 +3728,8 @@ export function registerDocsIpc(): void { } await mkdir(dirname(filePath), { recursive: true }) const passwordState = snapshotDocPassword(event.sender.id, null) - const bytes = passwordState.password - ? encryptDocx(Buffer.from(data), passwordState.password) - : Buffer.from(data) + const { bytes: plain, hashes } = await materializeLazyDocx(Buffer.from(data)) + const bytes = passwordState.password ? encryptDocx(plain, passwordState.password) : plain await atomicWriteFile(filePath, bytes) // teardown may have happened while the write was in flight — only a file // this handler created is safe to roll back; an overwritten one stays @@ -3660,7 +3738,13 @@ export function registerDocsIpc(): void { return { ok: false } } if (tornDownWcIds.has(event.sender.id)) return rollback() - await rememberDiskState(event.sender.id, filePath, bytes) + await rememberDiskState(event.sender.id, filePath, sha256Hex(bytes)) + pointLazyMediaAt( + hashes, + filePath, + event.sender.id, + passwordState.password ? plain : undefined, + ) if (tornDownWcIds.has(event.sender.id)) return rollback() const passwordIntentPending = commitDocPasswordSave( event.sender.id, @@ -3669,7 +3753,12 @@ export function registerDocsIpc(): void { ) pushRecent(filePath) notifyFileSaved(event.sender, filePath) - return { ok: true, path: filePath, passwordIntentPending } + return { + ok: true, + path: filePath, + passwordIntentPending, + ...reissuedDoc(!!passwordState.password, hashes, plain), + } } catch (err) { return { ok: false, error: String(err) } } @@ -3777,12 +3866,22 @@ export function registerDocsIpc(): void { // (the protected wrapper round-tripped as a "protected content" shell). ipcMain.handle( 'docs:copy-image-to-clipboard', - (_event, dataUrl: unknown, meta: unknown): boolean => { - if (typeof dataUrl !== 'string' || !dataUrl.startsWith('data:image/')) return false - const base64 = dataUrl.slice(dataUrl.indexOf(',') + 1) + async (_event, dataUrl: unknown, meta: unknown): Promise => { + if (typeof dataUrl !== 'string') return false + let bytes: Buffer + let htmlSrc = dataUrl + if (dataUrl.startsWith('data:image/')) { + bytes = Buffer.from(dataUrl.slice(dataUrl.indexOf(',') + 1), 'base64') + } else { + const media = await readLazyMedia(dataUrl) + if (!media) return false + bytes = media.body + // another document cannot resolve this document's lazy URL; inline the bytes + htmlSrc = `data:${media.mime};base64,${bytes.toString('base64')}` + } // createFromBuffer, not createFromDataURL — the latter returns an empty // image for valid PNGs in this Electron - const image = nativeImage.createFromBuffer(Buffer.from(base64, 'base64')) + const image = nativeImage.createFromBuffer(bytes) if (image.isEmpty()) return false // the html flavor carries the DISPLAY size + layout meta so an in-app // paste keeps size/align/wrap instead of falling back to bitmap pixels @@ -3804,7 +3903,7 @@ export function registerDocsIpc(): void { } clipboard.write({ image, - html: ``, + html: ``, }) return true }, @@ -4416,6 +4515,7 @@ export function buildDocsMenu(): void { { type: 'separator' }, { label: tm('menuDocsHelp'), enabled: false }, { type: 'separator' }, + checkUpdatesMenuItem(appMenuLabels(getUiLang())), aboutMenuItem(appMenuLabels(getUiLang())), ], }, diff --git a/apps/docs/src/main/lazy-media.ts b/apps/docs/src/main/lazy-media.ts new file mode 100644 index 0000000..bd980f5 --- /dev/null +++ b/apps/docs/src/main/lazy-media.ts @@ -0,0 +1,240 @@ +import { createHash } from 'node:crypto' +import { createReadStream } from 'node:fs' +import { open, stat } from 'node:fs/promises' +import { inflateRawSync } from 'node:zlib' +import { protocol } from 'electron' +import { DOCX_MEDIA_SCHEME_PRIVILEGE } from '@genoffice/electron-utils' +import { + LAZY_MEDIA_SCHEME, + isLazyMediaPart, + parseLazyMediaUrl, +} from '@genoffice/docx-engine/lazy-media' +import { + bufferSource, + lazyMediaHashesIn, + materializeDocx, + openZipFile, + readZipEntries, + slimDocx, + type ZipEntry, + type ZipFile, +} from '@genoffice/docx-engine/zip-splice' + +/** documents carrying at least this much browser-decodable media open lazily */ +const LAZY_MEDIA_MIN_BYTES = 16 * 1024 * 1024 + +interface Source { + path: string + owners: Set + table?: { size: number; mtimeMs: number; entries: Map } + /** the plain document once the file on disk is encrypted and cannot be served */ + bytes?: Buffer + /** entry table parsed from exactly `bytes` (identity-checked, never the file's) */ + memTable?: { bytes: Buffer; entries: Map } +} + +/** original-file sha256 → where its media lives */ +const sources = new Map() + +const MIME: Record = { + png: 'image/png', + jpg: 'image/jpeg', + jpeg: 'image/jpeg', + gif: 'image/gif', + bmp: 'image/bmp', + webp: 'image/webp', + svg: 'image/svg+xml', +} + +function sha256File(path: string): Promise { + return new Promise((resolve, reject) => { + const hash = createHash('sha256') + createReadStream(path) + .on('error', reject) + .on('data', (chunk) => hash.update(chunk)) + .on('end', () => resolve(hash.digest('hex'))) + }) +} + +function registerSource(hash: string, path: string, wcId: number): void { + const source = sources.get(hash) + if (source) { + if (source.path !== path) source.table = undefined + source.path = path + source.owners.add(wcId) + } else { + sources.set(hash, { path, owners: new Set([wcId]) }) + } +} + +/** + * The document with its pictures replaced by placeholders, or null when the + * file should be read whole (small media, encrypted container, odd zip). + */ +export async function openLazyDocx( + filePath: string, + wcId: number, +): Promise<{ bytes: Buffer; hash: string } | null> { + let zip: ZipFile + try { + zip = await openZipFile(filePath) + } catch { + return null + } + try { + let media = 0 + for (const e of zip.entries.values()) if (isLazyMediaPart(e.name)) media += e.usize + if (media < LAZY_MEDIA_MIN_BYTES) return null + const hash = await sha256File(filePath) + const slim = await slimDocx(zip, hash, LAZY_MEDIA_MIN_BYTES) + if (!slim) return null + registerSource(hash, filePath, wcId) + return { bytes: slim.bytes, hash } + } catch { + return null + } finally { + await zip.close() + } +} + +/** a recovery copy names the hash its placeholders were cut under; the + * on-disk file still holds those parts by name */ +export async function adoptLazyMediaHashes( + bytes: Buffer, + filePath: string, + wcId: number, +): Promise { + try { + for (const hash of await lazyMediaHashesIn(bytes)) registerSource(hash, filePath, wcId) + } catch { + /* not a plain zip: nothing lazy in it */ + } +} + +export function forgetLazyMediaOwner(wcId: number): void { + for (const [hash, source] of sources) { + source.owners.delete(wcId) + if (source.owners.size === 0) sources.delete(hash) + } +} + +async function entriesOf(source: Source): Promise | null> { + const st = await stat(source.path).catch(() => null) + if (!st) return null + if (source.table && source.table.size === st.size && source.table.mtimeMs === st.mtimeMs) { + return source.table.entries + } + const zip = await openZipFile(source.path) + try { + // an encrypted save may have switched the source to memory meanwhile + if (!source.bytes) source.table = { size: st.size, mtimeMs: st.mtimeMs, entries: zip.entries } + return zip.entries + } finally { + await zip.close() + } +} + +async function readRange(path: string, offset: number, length: number): Promise { + const fh = await open(path, 'r') + try { + const buf = Buffer.alloc(length) + let done = 0 + while (done < length) { + const { bytesRead } = await fh.read(buf, done, length - done, offset + done) + if (bytesRead === 0) throw new Error('lazy media: source file truncated') + done += bytesRead + } + return buf + } finally { + await fh.close() + } +} + +async function sourceZip(hash: string): Promise { + const source = sources.get(hash) + if (source?.bytes) { + const bytes = source.bytes + const src = bufferSource(bytes) + let mem = source.memTable + if (!mem || mem.bytes !== bytes) { + const entries = new Map() + for (const e of await readZipEntries(src)) entries.set(e.name, e) + source.memTable = mem = { bytes, entries } + } + return { ...src, entries: mem.entries, close: async () => {} } + } + const entries = source ? await entriesOf(source) : null + if (!source || !entries) return null + return { + size: (await stat(source.path)).size, + entries, + read: (offset, length) => { + // the file's offsets mean nothing once the source moved to memory + if (source.bytes) throw new Error('lazy media source replaced during read') + return readRange(source.path, offset, length) + }, + close: async () => {}, + } +} + +/** saved bytes with lazy placeholders swapped back for the original media, + * plus the hashes those placeholders named */ +export async function materializeLazyDocx( + bytes: Buffer, +): Promise<{ bytes: Buffer; hashes: Set }> { + const hashes = await lazyMediaHashesIn(bytes).catch(() => new Set()) + if (hashes.size === 0) return { bytes, hashes } + const opened = new Map>() + const full = await materializeDocx(bytes, (hash) => { + let zip = opened.get(hash) + if (!zip) opened.set(hash, (zip = sourceZip(hash))) + return zip + }) + return { bytes: full, hashes } +} + +/** after a save landed a full copy at `filePath`, serve those hashes from it; + * an encrypted save passes the plain bytes instead, since the file cannot be read by range */ +export function pointLazyMediaAt( + hashes: Set, + filePath: string, + wcId: number, + plain?: Buffer, +): void { + for (const hash of hashes) { + registerSource(hash, filePath, wcId) + const source = sources.get(hash) + if (source) { + source.bytes = plain + source.table = undefined + source.memTable = undefined + } + } +} + +/** the picture behind a lazy URL, or null when it is not served (any more) */ +export async function readLazyMedia(url: string): Promise<{ body: Buffer; mime: string } | null> { + const ref = parseLazyMediaUrl(url) + if (!ref || !isLazyMediaPart(ref.partPath)) return null + const zip = await sourceZip(ref.hash) + const entry = zip?.entries.get(ref.partPath) + if (!zip || !entry || (entry.method !== 0 && entry.method !== 8)) return null + const raw = await zip.read(entry.dataOffset, entry.csize) + const ext = ref.partPath.split('.').pop()?.toLowerCase() ?? '' + return { + body: entry.method === 0 ? raw : inflateRawSync(raw), + mime: MIME[ext] ?? 'application/octet-stream', + } +} + +export function registerLazyMediaProtocol(): void { + if (DOCX_MEDIA_SCHEME_PRIVILEGE.scheme !== LAZY_MEDIA_SCHEME) { + throw new Error('lazy media scheme privilege registered under another name') + } + if (protocol.isProtocolHandled(LAZY_MEDIA_SCHEME)) return + protocol.handle(LAZY_MEDIA_SCHEME, async (request) => { + const media = await readLazyMedia(request.url) + if (!media) return new Response(null, { status: 404 }) + return new Response(new Uint8Array(media.body), { headers: { 'Content-Type': media.mime } }) + }) +} diff --git a/apps/docs/src/preload/index.ts b/apps/docs/src/preload/index.ts index c3427a3..f349da0 100644 --- a/apps/docs/src/preload/index.ts +++ b/apps/docs/src/preload/index.ts @@ -15,7 +15,6 @@ import type { } from '../shared/ipc' import type { ProjectApi } from '@genoffice/project-store' import { installDropOpenBridge } from '@genoffice/electron-utils/drop-open' -import { installFilesPaneBridge } from '@genoffice/electron-utils/files-pane-bridge' const api: DesktopApi = { getLanguage: () => ipcRenderer.invoke('app:get-language'), @@ -226,5 +225,3 @@ contextBridge.exposeInMainWorld('projectApi', projectApi) // open documents dragged from the OS onto this tab as a new shell tab installDropOpenBridge() -// folder tree over the default save folder (Files pane) -installFilesPaneBridge() diff --git a/apps/docs/src/renderer/App.tsx b/apps/docs/src/renderer/App.tsx index d3afa83..111e81a 100644 --- a/apps/docs/src/renderer/App.tsx +++ b/apps/docs/src/renderer/App.tsx @@ -15,7 +15,7 @@ import type { Editor } from '@tiptap/core' import { handleDocsControl, type ControlRequest } from './control' import { DOMParser as PmDOMParser, type Mark as PmMark, Slice as PmSlice } from '@tiptap/pm/model' import { NodeSelection, type Transaction } from '@tiptap/pm/state' -import { Dropdown, FilesEdgeTab, FilesPane, ImageViewer, useAutoSavePref } from '@genoffice/ui' +import { Dropdown, ImageViewer, useAutoSavePref } from '@genoffice/ui' import { wordRangeAtCaret } from './editor/comments' import { markdownPasteHtml } from './editor/markdown-paste' import { pasteTextSlice, singleCellPasteText } from './editor/paste-text' @@ -168,8 +168,10 @@ import { syncPageBorders, syncPageSheets, clampCellBoxTops, + clampCellImageTops, pageBorderStyleOf, type PageGapSpec, + LayoutBatch, } from './editor/pagination-gaps' import { setColumnLayout } from './editor/column-layout' import { syncLineNumbers } from './editor/line-numbers' @@ -205,7 +207,7 @@ import { useShallowStable, useStableCallbacks } from './use-stable' import { FindPanel } from './components/FindPanel' import { Ribbon } from './components/Ribbon' import { computeFormatState } from './components/ribbon-format-state' -import { IconNavPane, IconRedo, IconSave, IconUndo } from './components/icons' +import { IconRedo, IconSave, IconUndo } from './components/icons' import { ToastHost } from './components/toast' import { AI_REWRITE_ACK_KEY, @@ -242,7 +244,7 @@ import { subscribeSubEditorState, } from './editor/active-editor' import type { CompareEntry } from './editor/compare' -import { collectHeadings, hasHeadings } from './editor/headings' +import { collectHeadings } from './editor/headings' import { applyTocPageDisplays } from './editor/toc-refresh' import { setSelectionAlign } from './editor/direction' @@ -284,6 +286,19 @@ import { type FileActionContext, type PendingPdfExport, } from './file-actions' +import { isPhasedContentPending } from './phased-content' + +/** min gap between whole-document pagination passes while a phased open streams its tail */ +const STREAMING_PASS_GAP_MS = 1500 +/** passes wait at least this many times their own duration while streaming */ +const STREAMING_PASS_DUTY = 4 +const STREAMING_PASS_GAP_MAX_MS = 15_000 +/** after an edit, the pass waits this many times its own last duration (300 ms floor) */ +const EDIT_PASS_DUTY = 3 +const EDIT_PASS_DEBOUNCE_MAX_MS = 2000 +const WORD_COUNT_THROTTLE_MS = 400 +/** consecutive follow-up passes a pass may schedule for itself */ +const MAX_FOLLOW_UP_PASSES = 6 import { runHeadlessDocumentExport } from './headless-export' import { installMcpBridge } from './mcp-bridge' import { @@ -794,7 +809,6 @@ export function App() { const [showRuler, setShowRuler] = useState(false) const [showNav, setShowNav] = useState(() => localStorage.getItem('aidocs.showNav') === '1') const closeNav = useCallback(() => setShowNav(false), []) - const [showFiles, setShowFiles] = useState(() => localStorage.getItem('aidocs.showFiles') === '1') const [viewMode, setViewMode] = useState('print') const [readMode, setReadMode] = useState(false) const [showGrid, setShowGrid] = useState(false) @@ -803,6 +817,7 @@ export function App() { const [splitHtml, setSplitHtml] = useState('') const [showFind, setShowFind] = useState(false) const [imageDragOver, setImageDragOver] = useState(false) + const [findFocusInput, setFindFocusInput] = useState(0) const [findFocusReplace, setFindFocusReplace] = useState(0) const [showShortcuts, setShowShortcuts] = useState(false) const ribbonActionsRef = useRef<{ @@ -1237,10 +1252,6 @@ export function App() { localStorage.setItem('aidocs.showNav', showNav ? '1' : '0') }, [showNav]) - useEffect(() => { - localStorage.setItem('aidocs.showFiles', showFiles ? '1' : '0') - }, [showFiles]) - const spellcheckWasOn = useRef(spellcheck) const respellKickBusy = useRef(false) // spell-diag trace: squiggle losses in the field are intermittent and @@ -2835,24 +2846,31 @@ export function App() { ) // pagination-constraint injection: docxIndex → parse-layer semantics (keepNext/keepLines/widow/table-row flags). // DOM measurement only has geometry; these constraints decide page cut points (no orphan headings / unbreakable lines / repeated table headers). + const blockByDocxIndex = useMemo(() => { + const m = new Map() + for (const b of doc?.parsed.blocks ?? []) + if (b.docxIndex !== null && !m.has(b.docxIndex)) m.set(b.docxIndex, b) + return m + }, [doc]) const blockMetaOf = useCallback( (docxIndex: number): BlockMeta | undefined => { - const b = doc?.parsed.blocks.find((bl) => bl.docxIndex === docxIndex) + const b = blockByDocxIndex.get(docxIndex) if (!b) return undefined if (b.type === 'table') { // notes referenced inside cells reserve their page-bottom area like a // paragraph's: the slicer charges them to the rows holding the marks const fnBands = footnoteBandsOf(b) const fnExtra = fnBands.reduce((s, band) => s + band.heightPx, 0) - const flagged = !!b.originalXml && /tblHeader|cantSplit| doc?.parsed.styles.get(id)?.display?.keepNext === true + const xml = b.originalXml ?? '' + const flagged = + /tblHeader|cantSplit|keepNext| styleKeepNext(m[1])) + const modern = (doc?.parsed.compatibilityMode ?? 0) >= 15 + if (!flagged && fnExtra === 0 && !modern) return undefined return { - ...(flagged - ? { - tableRowFlags: tableRowFlags(b.originalXml!), - ...((doc?.parsed.compatibilityMode ?? 0) >= 15 ? { modernTableHeaders: true } : {}), - } - : {}), + ...(flagged ? { tableRowFlags: tableRowFlags(xml, styleKeepNext) } : {}), + ...(modern ? { modernTableHeaders: true } : {}), ...(fnExtra > 0 ? { footnoteExtraPx: fnExtra, footnoteBands: fnBands } : {}), } } @@ -2876,7 +2894,7 @@ export function App() { ...(fnExtra > 0 ? { footnoteExtraPx: fnExtra, footnoteBands: fnBands } : {}), } }, - [doc, defaultParaStyle, footnoteBandsOf], + [doc, blockByDocxIndex, defaultParaStyle, footnoteBandsOf], ) // per-page footnote collection: the page of the referencing block → that page's footnote entries (number/text/estimated height) @@ -2887,7 +2905,7 @@ export function App() { const noOf = new Map(footnotes.map((f, i) => [f.id, noteNo('footnote', f.id, i)])) for (const b of blocks) { if (b.docxIndex === undefined) continue - const pb = doc.parsed.blocks.find((bl) => bl.docxIndex === b.docxIndex) + const pb = blockByDocxIndex.get(b.docxIndex) if (!pb) continue const ids = blockNoteScanRuns(pb) .filter((r) => r.noteRef?.kind === 'footnote') @@ -2922,7 +2940,7 @@ export function App() { } return out }, - [doc, footnotes, sections, section, noteRenderInfoOf, noteNo], + [doc, blockByDocxIndex, footnotes, sections, section, noteRenderInfoOf, noteNo], ) // endnote-area entries (placed together at the document end, shared by pagination preview and page slicing): height measured with the final section's content width @@ -3112,6 +3130,18 @@ export function App() { let slices: PageSlice[] = [] let timer: number | null = null let suppressSig = '' + // passes a pass schedules for itself (widths / suppression applied late); + // a document whose column widths never settle must not paginate forever + let followUps = 0 + let selfScheduled = false + let retrigger: string[] = [] + // several reasons in one pass debounce into one follow-up: count passes, not reasons + const followUp = (why: string) => { + retrigger.push(why) + if (followUps >= MAX_FOLLOW_UP_PASSES) return + selfScheduled = true + onUpdate() + } let secWidthSig = '' let charSpaceSig = '' const colWidthPass = newWidthPassState() @@ -3148,10 +3178,15 @@ export function App() { prev.current === current && prev.total === total ? prev : { current, total }, ) } + let lastPassAt = 0 + let lastPassMs = 0 const remeasure = () => { + timer = null + lastPassAt = performance.now() try { remeasurePass() } finally { + lastPassMs = performance.now() - lastPassAt // a pass that threw between the add and the pre-setPageGaps remove // must not leave the gap widgets hidden pmEl()?.classList.remove('measuring-natural') @@ -3160,6 +3195,9 @@ export function App() { const remeasurePass = () => { const pm = pmEl() if (!pm) return + followUps = selfScheduled ? followUps + 1 : 0 + selfScheduled = false + retrigger = [] const tStart = performance.now() let tMeasure = 0 let tSlice = 0 @@ -3890,6 +3928,7 @@ export function App() { } tGapsBuild = performance.now() - tGaps0 const tSet0 = performance.now() + const layoutBatch = new LayoutBatch(editor.view) // split declared-height rows: resolve the engine's target heights to tr elements const rowFillEls: Array<{ el: Element; targetPx: number; extraPx?: number }> = [] for (const f of rowFills) { @@ -3909,7 +3948,7 @@ export function App() { ...(f.extraPx ? { extraPx: f.extraPx } : {}), }) } - setRowFills(editor.view, rowFillEls) + setRowFills(editor.view, rowFillEls, layoutBatch) // oversized single-line blocks: resolve the engine's page-bottom clips to their elements const oversizeEls: Array<{ el: HTMLElement; clipPx: number }> = [] for (const c of oversizeClips) { @@ -3926,14 +3965,35 @@ export function App() { flow?: boolean carryPx?: number }> = [] + // blocks by rounded top: a scan per shift was floats × blocks on picture-heavy + // documents; the first match in document order wins, as the scan did + const blockAtTop = (pred: (b: BlockBox) => boolean) => { + const byTop = new Map>() + blocks.forEach((bb, i) => { + if (!pred(bb)) return + const k = Math.round(bb.top) + const list = byTop.get(k) + if (list) list.push({ i, bb }) + else byTop.set(k, [{ i, bb }]) + }) + return (top: number): BlockBox | undefined => { + const k = Math.round(top) + let hit: { i: number; bb: BlockBox } | undefined + for (const kk of [k - 1, k, k + 1]) { + const c = byTop.get(kk)?.find((e) => Math.abs(e.bb.top - top) < 0.5) + if (c && (!hit || c.i < hit.i)) hit = c + } + return hit?.bb + } + } + const pageRelBlockAt = blockAtTop((bb) => bb.pageRelVyPx !== undefined) for (const f of floatVShifts) { - const b = blocks.find( - (bb) => bb.pageRelVyPx !== undefined && Math.abs(bb.top - f.blockTop) < 0.5, - ) + const b = pageRelBlockAt(f.blockTop) if (b?.el) floatVEls.push({ el: b.el, dyPx: f.dyPx }) } + const floatTableAt = blockAtTop((bb) => Boolean(bb.floatTable)) for (const f of floatFlows) { - const b = blocks.find((bb) => bb.floatTable && Math.abs(bb.top - f.blockTop) < 0.5) + const b = floatTableAt(f.blockTop) if (b?.el) floatVEls.push({ el: b.el, dyPx: 0, flow: true }) } // split floating table: its anchor paragraph (the next block) starts @@ -3955,9 +4015,9 @@ export function App() { metrics: { marginTop: 0, marginBottom: 0, marginLeft: 0, marginRight: 0 }, }) } - setFloatVShifts(editor.view, floatVEls) + setFloatVShifts(editor.view, floatVEls, layoutBatch) pm.classList.remove('measuring-natural') - setPageGaps(editor.view, gaps, firstPageFloats) + setPageGaps(editor.view, gaps, firstPageFloats, layoutBatch) // mixed-column canvas: paint the engine's regions via per-block width/translate decorations const colSpecs = viewMode === 'print' && !readMode && colMode === 'mixed' && secList @@ -4013,7 +4073,8 @@ export function App() { } layoutSpecs = [...mergedSpecs.values()] } - setColumnLayout(editor.view, layoutSpecs) + setColumnLayout(editor.view, layoutSpecs, layoutBatch) + layoutBatch.commit() // in-table gap bands live in the spanning cell's coordinate space: // re-anchor them to the paper before the strips are measured/aligned alignTableGapFills(pm, factor) @@ -4032,6 +4093,7 @@ export function App() { // Word keeps anchored objects on the page: cell boxes lifted past the // paper top by a negative anchor offset are pushed back down clampCellBoxTops(pm, pm.getBoundingClientRect().top, factor) + clampCellImageTops(pm, factor) syncCutOverlays((pm.closest('.page-wrap') as HTMLElement) ?? pm, overlayCutAnchors, factor) { // page border (w:pgBorders): per-page overlay boxes (w:display can @@ -4064,7 +4126,7 @@ export function App() { const sig = gaps.reduce((s, g, n) => (g.suppressLeadMt ? `${s},${n}` : s), '') if (sig !== suppressSig) { suppressSig = sig - onUpdate() + followUp('suppress') } // freshly applied wrap widths (section widths, unequal column widths, // vertical-text line lengths) change line breaks: one follow-up remeasure with them in the DOM @@ -4073,9 +4135,9 @@ export function App() { .join(',') if (wSig !== secWidthSig) { secWidthSig = wSig - onUpdate() + followUp('width') } - if (colGranted) onUpdate() + if (colGranted) followUp('columns') // freshly applied per-block letter-spacing changes line breaks the same way const cSig = charSpecs.length === 0 @@ -4083,7 +4145,7 @@ export function App() { : `${charSpecs.length}:${[...new Set(charSpecs.map((s) => s.charSpacePt))].join(',')}` if (cSig !== charSpaceSig) { charSpaceSig = cSig - onUpdate() + followUp('charSpace') } // the last page paints as a full sheet like the ones above it: // extend the canvas to that page's paper bottom, measured from the last gap @@ -4115,6 +4177,8 @@ export function App() { ;(window as unknown as Record).__pageDebug = { slices, colMode, + retrigger, + followUps, colSpecs: colSpecs.map((s) => ({ w: s.widthPx === undefined ? null : Math.round(s.widthPx), dx: Math.round(s.dx), @@ -4170,8 +4234,29 @@ export function App() { locate() } const onUpdate = () => { + // while the tail streams, chunks land every few frames: a plain debounce + // would either never fire (dense chunks) or pay a whole-document pass + // per chunk (sparse ones); keep the pass already due and pace new ones + if (isPhasedContentPending()) { + if (timer) return + // a whole-document pass grows with the streamed content; keep it to a + // fraction of the streaming time so the tail lands sooner + const gap = Math.min( + STREAMING_PASS_GAP_MAX_MS, + Math.max(STREAMING_PASS_GAP_MS, lastPassMs * STREAMING_PASS_DUTY), + ) + const delay = Math.max(300, gap - (performance.now() - lastPassAt)) + timer = window.setTimeout(remeasure, delay) + return + } if (timer) window.clearTimeout(timer) - timer = window.setTimeout(remeasure, 300) + // a whole-document pass after every pause blocks typing on long + // documents: wait longer when the last pass was slow (Word paginates in + // the background too); short documents keep the 300 ms feel + timer = window.setTimeout( + remeasure, + Math.min(EDIT_PASS_DEBOUNCE_MAX_MS, Math.max(300, lastPassMs * EDIT_PASS_DUTY)), + ) } remeasure() // async @font-face loading triggers a full reflow (line-break points change); pagination @@ -4272,6 +4357,28 @@ export function App() { } }, [editor, sections]) + // status-bar word count: the whole text is re-walked per doc, so pace it + // (Word refreshes its count on idle too). A trailing throttle, not a + // debounce: the streamed tail of a phased open would keep resetting one + const [wordCount, setWordCount] = useState(0) + useEffect(() => { + if (!editor) return + let timer = 0 + const refresh = () => { + timer = 0 + setWordCount(wordCountOfDoc(editor.state.doc)) + } + const onUpdate = () => { + if (!timer) timer = window.setTimeout(refresh, WORD_COUNT_THROTTLE_MS) + } + refresh() + editor.on('update', onUpdate) + return () => { + window.clearTimeout(timer) + editor.off('update', onUpdate) + } + }, [editor, docLoading]) + /** Word word-count dialog: pages/lines estimated from the current layout */ const openStats = useCallback(() => { if (!editor) return @@ -4409,7 +4516,11 @@ export function App() { } if ((e.metaKey || e.ctrlKey) && e.key === 'f') { e.preventDefault() - if (doc) setShowFind(true) + if (doc) { + // bump even when the panel is already open: focus returns to the find box + setShowFind(true) + setFindFocusInput((n) => n + 1) + } } // Word's replace: Ctrl+H everywhere (macOS Cmd+H is the system hide role, // which never reaches the renderer, so this branch is Ctrl+H there too) @@ -4748,7 +4859,10 @@ export function App() { setRibbonTabRequest({ tab: 'layout', nonce: Date.now() }) break case 'find': - if (doc) setShowFind(true) + if (doc) { + setShowFind(true) + setFindFocusInput((n) => n + 1) + } break case 'export-pdf': void exportPdf() @@ -5414,7 +5528,6 @@ export function App() { onShowMarks: setShowMarks, onShowRuler: setShowRuler, onShowNav: setShowNav, - onShowFiles: setShowFiles, onShowComments: () => setShowComments(true), onNewComment: startNewComment, onTrackChanges: setTrackChanges, @@ -5500,8 +5613,6 @@ export function App() { if (!editor) return null - const wordCount = wordCountOfDoc(editor.state.doc) - // canvas geometry is anchored to the first section (stable across cursor moves); // sections with a different content width carry per-block width decorations // the shared paper must cover the widest section or its content lays out past @@ -5645,7 +5756,6 @@ export function App() { showMarks={showMarks} showRuler={showRuler} showNav={showNav} - showFiles={showFiles} commentCount={comments.length} openCommentCount={comments.filter((c) => !c.parentId && c.done !== true).length} canComment={!editor.state.selection.empty || wordRangeAtCaret(editor) !== null} @@ -5710,34 +5820,13 @@ export function App() { // and land focus on the replace field setFindFocusReplace(0) }} + focusFindNonce={findFocusInput} focusReplaceNonce={findFocusReplace} /> )} - {doc && showFiles && !readMode && ( - setShowFiles(false)} - /> - )} - {/* the edge tab needs a free left edge: with the navigation pane open the ribbon toggle remains */} - {doc && !showFiles && !showNav && !readMode && ( - setShowFiles(true)} /> - )} {doc && showNav && ( )} - {doc && !showNav && !readMode && hasHeadings(editor.state.doc) && ( - - )} {doc && ( ) const [style, setStyle] = useState(initialStyle) const [color, setColor] = useState(`#${(textAttrs.color as string | null) ?? '000000'}`) + // the input has no "unset" state: an untouched swatch keeps the run's colour as it was + const [colorTouched, setColorTouched] = useState(false) const [underline, setUnderline] = useState(editor.isActive('underline')) const [strike, setStrike] = useState(editor.isActive('strike')) const [vertAlign, setVertAlign] = useState((textAttrs.vertAlign as string | null) ?? '') @@ -574,12 +576,13 @@ export function FontDialog({ editor, onClose }: { editor: Editor; onClose: () => onClose() return } - const hex = color.replace('#', '').toUpperCase() let chain = editor .chain() .focus() .setMark('docTextStyle', { - color: hex === '000000' ? null : hex, + color: colorTouched + ? color.replace('#', '').toUpperCase() + : ((textAttrs.color as string | null) ?? null), sizeHalfPoints: Math.round(size * 2), // picks target only their script's rFonts slot; the other slot survives ...(!font @@ -672,7 +675,10 @@ export function FontDialog({ editor, onClose }: { editor: Editor; onClose: () => type="color" className="font-color-input" value={color} - onChange={(e) => setColor(e.target.value)} + onChange={(e) => { + setColor(e.target.value) + setColorTouched(true) + }} />