Files
edentext/src/lib/export/template.ts
T
Steffen Becker 49e485038e feat: the document saves as a template, and the File menu remembers
A .ott/.dotx is the built document with one label changed — ODF's
package media type, Word's content type for the document part — so the
picker offers both and the chosen extension decides which exporter runs.

Opening or saving also records the file in a recent list. The name is in
localStorage so the menu draws at once; the handle that reopens it is in
IndexedDB beside it. A file that has moved, or whose permission the
browser drops on reload, is reported and dropped from the list.
2026-08-14 08:23:09 +02:00

37 lines
1.8 KiB
TypeScript

import { unzipSync, zipSync, strFromU8, strToU8 } from 'fflate';
// A template is the document it was built from with one label changed: ODF swaps the
// package's media type (in `mimetype` and in the manifest), OOXML the content type of
// the document part. Both word processors then open it as an untitled copy instead of
// the file itself — which is all a .ott/.dotx is.
const ODT_MIME = 'application/vnd.oasis.opendocument.text';
const OTT_MIME = 'application/vnd.oasis.opendocument.text-template';
const DOCX_MAIN = 'application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml';
const DOTX_MAIN = 'application/vnd.openxmlformats-officedocument.wordprocessingml.template.main+xml';
export function odtToOtt(bytes: Uint8Array): Uint8Array {
const files = unzipSync(bytes);
files['mimetype'] = strToU8(OTT_MIME);
const manifest = files['META-INF/manifest.xml'];
if (manifest) {
files['META-INF/manifest.xml'] = strToU8(strFromU8(manifest).replace(
new RegExp(`manifest:media-type="${ODT_MIME}"`, 'g'), `manifest:media-type="${OTT_MIME}"`));
}
// The mimetype entry stays first and uncompressed, as in every ODF package.
const out: Record<string, [Uint8Array, { level: 0 | 6 }]> = { mimetype: [files['mimetype'], { level: 0 }] };
for (const [path, data] of Object.entries(files)) {
if (path !== 'mimetype') out[path] = [data, { level: 6 }];
}
return zipSync(out);
}
export function docxToDotx(bytes: Uint8Array): Uint8Array {
const files = unzipSync(bytes);
const ct = files['[Content_Types].xml'];
if (ct) files['[Content_Types].xml'] = strToU8(strFromU8(ct).replace(DOCX_MAIN, DOTX_MAIN));
const out: Record<string, [Uint8Array, { level: 6 }]> = {};
for (const [path, data] of Object.entries(files)) out[path] = [data, { level: 6 }];
return zipSync(out);
}