docxml
Version:
TypeScript (component) library for building and parsing a DOCX file
79 lines (78 loc) • 3.4 kB
JavaScript
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 _BinaryFile_reader;
import * as dntShim from "../../_dnt.shims.js";
/**
* A utility class that represents a binary file inside the DOCX archive -- currently used for
* images.
*/
export class BinaryFile {
constructor(location, reader, mime) {
Object.defineProperty(this, "location", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "mime", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
_BinaryFile_reader.set(this, void 0);
this.location = location;
this.mime = mime;
__classPrivateFieldSet(this, _BinaryFile_reader, reader, "f");
}
/**
* Get all XmlFile instances related to this one, including self. This helps the system
* serialize itself back to DOCX fullly. Probably not useful for consumers of the library.
*
* By default only returns the instance itself but no other related instances.
*/
getRelated() {
return [this];
}
get contentType() {
return Promise.resolve(this.mime);
}
/**
* Let a file tell the system when it is effectively empty, so it can be omitted from the archive.
*/
isEmpty() {
return false;
}
static fromArchive(archive, contentTypes, location) {
const mime = contentTypes.getType(location);
if (mime === undefined) {
throw new Error('Error creating BinaryFile from Archive. No matching content type found in ContentTypesXml');
}
return new BinaryFile(location, () => archive.readBinary(location), mime);
}
static fromDisk(diskLocation, location, mime) {
return new BinaryFile(location, () => dntShim.Deno.readFile(diskLocation), mime);
}
static fromData(data, location, mime) {
return new BinaryFile(location, () => Promise.resolve(data), mime);
}
toUint8Array() {
return __classPrivateFieldGet(this, _BinaryFile_reader, "f").call(this);
}
/**
* Add all related files to the given archive.
*/
addToArchive(archive) {
archive.addBinaryFile(this.location, this.toUint8Array());
}
}
_BinaryFile_reader = new WeakMap();