UNPKG

docxml

Version:

TypeScript (component) library for building and parsing a DOCX file

102 lines (101 loc) 4.61 kB
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) { if (kind === "m") throw new TypeError("Private method is not writable"); if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter"); if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it"); return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value; }; var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) { if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter"); if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it"); return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver); }; var _Archive_zip, _Archive_promises; import * as dntShim from "../../_dnt.shims.js"; import { JSZip, readZip } from '../../deps/deno.land/x/jszip@0.11.0/mod.js'; import { parse, serialize } from '../utilities/dom.js'; export class Archive { constructor(zip) { Object.defineProperty(this, "location", { enumerable: true, configurable: true, writable: true, value: void 0 }); _Archive_zip.set(this, void 0); _Archive_promises.set(this, []); __classPrivateFieldSet(this, _Archive_zip, zip || new JSZip(), "f"); } /** * @deprecated For testing purposes only. */ get $$$fileNames() { return __classPrivateFieldGet(this, _Archive_zip, "f").files(); } hasFile(location) { return !!__classPrivateFieldGet(this, _Archive_zip, "f").files()[location]; } readText(location) { if (location.startsWith('/') || location.startsWith('./')) { location = location.substring(location.indexOf('/') + 1); } try { return __classPrivateFieldGet(this, _Archive_zip, "f").file(location).async('string'); } catch (error) { throw new Error(`Could not read "${location}" from archive: ${error.message}. The only files in this archive are: ${Object.keys(__classPrivateFieldGet(this, _Archive_zip, "f").files()).join(', ')}`); } } async asUint8Array() { for await (const { location, promise } of __classPrivateFieldGet(this, _Archive_promises, "f")) { __classPrivateFieldGet(this, _Archive_zip, "f").addFile(location, await promise); } return __classPrivateFieldGet(this, _Archive_zip, "f").generateAsync({ type: 'uint8array' }); } async readXml(location) { return parse(await this.readText(location)); } readBinary(location) { return __classPrivateFieldGet(this, _Archive_zip, "f").file(location).async('uint8array'); } /** * Create a new XML file in the DOCX archive. */ addXmlFile(location, node) { return this.addTextFile(location, `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>${serialize(node)}`); } /** * Create a new JSON file in the DOCX archive. */ // deno-lint-ignore no-explicit-any addJsonFile(location, js) { return this.addTextFile(location, JSON.stringify(js, null, '\t')); } /** * Create a new text file in the DOCX archive. */ addTextFile(location, contents) { __classPrivateFieldGet(this, _Archive_zip, "f").addFile(location, contents); return this; } /** * Create a new text file in the DOCX archive. * * In order to keep this method (and methods that use it, eg. Docx#toArchive) synchronous, * we're only writing a promise to memory for now and leave the asynchronous operations for * output time (see also Archive#toUint8Array). */ addBinaryFile(location, promised) { __classPrivateFieldGet(this, _Archive_promises, "f").push({ location, promise: promised }); return this; } static async fromUInt8Array(data) { return new Archive(await new JSZip().loadAsync(data)); } static async fromFile(location) { return new Archive(await readZip(location)); } async toFile(location) { await dntShim.Deno.writeFile(location, await this.asUint8Array()); } } _Archive_zip = new WeakMap(), _Archive_promises = new WeakMap();