easy-template-x 4.0.0 → 4.1.1

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/README.md CHANGED
@@ -303,9 +303,10 @@ Input data:
303
303
  _type: "image",
304
304
  source: fs.readFileSync("hero.png"),
305
305
  format: MimeType.Png,
306
- altText: "Kung Fu Hero", // Optional
307
306
  width: 200,
308
- height: 200
307
+ height: 200,
308
+ altText: "Kung Fu Hero", // Optional
309
+ transparencyPercent: 80 // Optional
309
310
  }
310
311
  }
311
312
  ```
@@ -23,7 +23,6 @@ function _interopNamespaceDefault(e) {
23
23
  }
24
24
 
25
25
  var JSON5__namespace = /*#__PURE__*/_interopNamespaceDefault(JSON5);
26
- var JSZip__namespace = /*#__PURE__*/_interopNamespaceDefault(JSZip);
27
26
 
28
27
  function _defineProperty(e, r, t) {
29
28
  return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, {
@@ -48,6 +47,12 @@ function _toPropertyKey(t) {
48
47
  return "symbol" == typeof i ? i : i + "";
49
48
  }
50
49
 
50
+ class ArgumentError extends Error {
51
+ constructor(message) {
52
+ super(message);
53
+ }
54
+ }
55
+
51
56
  class MalformedFileError extends Error {
52
57
  constructor(expectedFileType) {
53
58
  super(`Malformed file detected. Make sure the file is a valid ${expectedFileType} file.`);
@@ -64,7 +69,7 @@ class MaxXmlDepthError extends Error {
64
69
  }
65
70
  }
66
71
 
67
- class MissingArgumentError extends Error {
72
+ class MissingArgumentError extends ArgumentError {
68
73
  constructor(argName) {
69
74
  super(`Argument '${argName}' is missing.`);
70
75
  _defineProperty(this, "argName", void 0);
@@ -1421,19 +1426,19 @@ class ImagePlugin extends TemplatePlugin {
1421
1426
  return;
1422
1427
  }
1423
1428
 
1424
- // add the image file into the archive
1429
+ // Add the image file into the archive
1425
1430
  const mediaFilePath = await context.docx.mediaFiles.add(content.source, content.format);
1426
1431
  const relType = MimeTypeHelper.getOfficeRelType(content.format);
1427
1432
  const relId = await context.currentPart.rels.add(mediaFilePath, relType);
1428
1433
  await context.docx.contentTypes.ensureContentType(content.format);
1429
1434
 
1430
- // create the xml markup
1435
+ // Create the xml markup
1431
1436
  const imageId = nextImageId++;
1432
- const imageXml = this.createMarkup(imageId, relId, content.altText, content.width, content.height);
1437
+ const imageXml = this.createMarkup(imageId, relId, content);
1433
1438
  XmlNode.insertAfter(imageXml, wordTextNode);
1434
1439
  XmlNode.remove(wordTextNode);
1435
1440
  }
1436
- createMarkup(imageId, relId, altText, width, height) {
1441
+ createMarkup(imageId, relId, content) {
1437
1442
  // http://officeopenxml.com/drwPicInline.php
1438
1443
 
1439
1444
  //
@@ -1450,15 +1455,15 @@ class ImagePlugin extends TemplatePlugin {
1450
1455
  const markupText = `
1451
1456
  <w:drawing>
1452
1457
  <wp:inline distT="0" distB="0" distL="0" distR="0">
1453
- <wp:extent cx="${this.pixelsToEmu(width)}" cy="${this.pixelsToEmu(height)}"/>
1458
+ <wp:extent cx="${this.pixelsToEmu(content.width)}" cy="${this.pixelsToEmu(content.height)}"/>
1454
1459
  <wp:effectExtent l="0" t="0" r="0" b="0"/>
1455
- ${this.docProperties(imageId, name, altText)}
1460
+ ${this.docProperties(imageId, name, content)}
1456
1461
  <wp:cNvGraphicFramePr>
1457
1462
  <a:graphicFrameLocks xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main" noChangeAspect="1"/>
1458
1463
  </wp:cNvGraphicFramePr>
1459
1464
  <a:graphic xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main">
1460
1465
  <a:graphicData uri="http://schemas.openxmlformats.org/drawingml/2006/picture">
1461
- ${this.pictureMarkup(imageId, relId, name, width, height)}
1466
+ ${this.pictureMarkup(imageId, relId, name, content)}
1462
1467
  </a:graphicData>
1463
1468
  </a:graphic>
1464
1469
  </wp:inline>
@@ -1469,9 +1474,9 @@ class ImagePlugin extends TemplatePlugin {
1469
1474
 
1470
1475
  return markupXml;
1471
1476
  }
1472
- docProperties(imageId, name, altText) {
1473
- if (altText) {
1474
- return `<wp:docPr id="${imageId}" name="${name}" descr="${altText}"/>`;
1477
+ docProperties(imageId, name, content) {
1478
+ if (content.altText) {
1479
+ return `<wp:docPr id="${imageId}" name="${name}" descr="${content.altText}"/>`;
1475
1480
  }
1476
1481
  return `
1477
1482
  <wp:docPr id="${imageId}" name="${name}">
@@ -1483,10 +1488,10 @@ class ImagePlugin extends TemplatePlugin {
1483
1488
  </wp:docPr>
1484
1489
  `;
1485
1490
  }
1486
- pictureMarkup(imageId, relId, name, width, height) {
1491
+ pictureMarkup(imageId, relId, name, content) {
1487
1492
  // http://officeopenxml.com/drwPic.php
1488
1493
 
1489
- // legend:
1494
+ // Legend:
1490
1495
  // nvPicPr - non-visual picture properties - id, name, etc.
1491
1496
  // blipFill - binary large image (or) picture fill - image size, image fill, etc.
1492
1497
  // spPr - shape properties - frame size, frame fill, etc.
@@ -1501,6 +1506,7 @@ class ImagePlugin extends TemplatePlugin {
1501
1506
  </pic:nvPicPr>
1502
1507
  <pic:blipFill>
1503
1508
  <a:blip r:embed="${relId}">
1509
+ ${this.transparencyMarkup(content.transparencyPercent)}
1504
1510
  <a:extLst>
1505
1511
  <a:ext uri="{28A0092B-C50C-407E-A947-70E740481C1C}">
1506
1512
  <a14:useLocalDpi xmlns:a14="http://schemas.microsoft.com/office/drawing/2010/main" val="0"/>
@@ -1515,7 +1521,7 @@ class ImagePlugin extends TemplatePlugin {
1515
1521
  <pic:spPr bwMode="auto">
1516
1522
  <a:xfrm>
1517
1523
  <a:off x="0" y="0"/>
1518
- <a:ext cx="${this.pixelsToEmu(width)}" cy="${this.pixelsToEmu(height)}"/>
1524
+ <a:ext cx="${this.pixelsToEmu(content.width)}" cy="${this.pixelsToEmu(content.height)}"/>
1519
1525
  </a:xfrm>
1520
1526
  <a:prstGeom prst="rect">
1521
1527
  <a:avLst/>
@@ -1528,6 +1534,16 @@ class ImagePlugin extends TemplatePlugin {
1528
1534
  </pic:pic>
1529
1535
  `;
1530
1536
  }
1537
+ transparencyMarkup(transparencyPercent) {
1538
+ if (transparencyPercent === null || transparencyPercent === undefined) {
1539
+ return '';
1540
+ }
1541
+ if (transparencyPercent < 0 || transparencyPercent > 100) {
1542
+ throw new ArgumentError(`Transparency percent must be between 0 and 100, but was ${transparencyPercent}.`);
1543
+ }
1544
+ const alpha = Math.round((100 - transparencyPercent) * 1000);
1545
+ return `<a:alphaModFix amt="${alpha}" />`;
1546
+ }
1531
1547
  pixelsToEmu(pixels) {
1532
1548
  // https://stackoverflow.com/questions/20194403/openxml-distance-size-units
1533
1549
  // https://docs.microsoft.com/en-us/windows/win32/vml/msdn-online-vml-units#other-units-of-measurement
@@ -3001,7 +3017,7 @@ class ZipObject {
3001
3017
 
3002
3018
  class Zip {
3003
3019
  static async load(file) {
3004
- const zip = await JSZip__namespace.loadAsync(file);
3020
+ const zip = await JSZip.loadAsync(file);
3005
3021
  return new Zip(zip);
3006
3022
  }
3007
3023
  constructor(zip) {
@@ -3089,7 +3105,7 @@ class TemplateHandler {
3089
3105
  /**
3090
3106
  * Version number of the `easy-template-x` library.
3091
3107
  */
3092
- _defineProperty(this, "version", "3.2.1" );
3108
+ _defineProperty(this, "version", "4.1.0" );
3093
3109
  _defineProperty(this, "xmlParser", new XmlParser());
3094
3110
  _defineProperty(this, "docxParser", void 0);
3095
3111
  _defineProperty(this, "compiler", void 0);
@@ -3237,6 +3253,7 @@ class TemplateHandler {
3237
3253
  }
3238
3254
  }
3239
3255
 
3256
+ exports.ArgumentError = ArgumentError;
3240
3257
  exports.Base64 = Base64;
3241
3258
  exports.Binary = Binary;
3242
3259
  exports.COMMENT_NODE_NAME = COMMENT_NODE_NAME;
@@ -1,7 +1,7 @@
1
1
  import { DOMParser } from '@xmldom/xmldom';
2
2
  import getProp from 'lodash.get';
3
3
  import * as JSON5 from 'json5';
4
- import * as JSZip from 'jszip';
4
+ import JSZip from 'jszip';
5
5
 
6
6
  function _defineProperty(e, r, t) {
7
7
  return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, {
@@ -26,6 +26,12 @@ function _toPropertyKey(t) {
26
26
  return "symbol" == typeof i ? i : i + "";
27
27
  }
28
28
 
29
+ class ArgumentError extends Error {
30
+ constructor(message) {
31
+ super(message);
32
+ }
33
+ }
34
+
29
35
  class MalformedFileError extends Error {
30
36
  constructor(expectedFileType) {
31
37
  super(`Malformed file detected. Make sure the file is a valid ${expectedFileType} file.`);
@@ -42,7 +48,7 @@ class MaxXmlDepthError extends Error {
42
48
  }
43
49
  }
44
50
 
45
- class MissingArgumentError extends Error {
51
+ class MissingArgumentError extends ArgumentError {
46
52
  constructor(argName) {
47
53
  super(`Argument '${argName}' is missing.`);
48
54
  _defineProperty(this, "argName", void 0);
@@ -1399,19 +1405,19 @@ class ImagePlugin extends TemplatePlugin {
1399
1405
  return;
1400
1406
  }
1401
1407
 
1402
- // add the image file into the archive
1408
+ // Add the image file into the archive
1403
1409
  const mediaFilePath = await context.docx.mediaFiles.add(content.source, content.format);
1404
1410
  const relType = MimeTypeHelper.getOfficeRelType(content.format);
1405
1411
  const relId = await context.currentPart.rels.add(mediaFilePath, relType);
1406
1412
  await context.docx.contentTypes.ensureContentType(content.format);
1407
1413
 
1408
- // create the xml markup
1414
+ // Create the xml markup
1409
1415
  const imageId = nextImageId++;
1410
- const imageXml = this.createMarkup(imageId, relId, content.altText, content.width, content.height);
1416
+ const imageXml = this.createMarkup(imageId, relId, content);
1411
1417
  XmlNode.insertAfter(imageXml, wordTextNode);
1412
1418
  XmlNode.remove(wordTextNode);
1413
1419
  }
1414
- createMarkup(imageId, relId, altText, width, height) {
1420
+ createMarkup(imageId, relId, content) {
1415
1421
  // http://officeopenxml.com/drwPicInline.php
1416
1422
 
1417
1423
  //
@@ -1428,15 +1434,15 @@ class ImagePlugin extends TemplatePlugin {
1428
1434
  const markupText = `
1429
1435
  <w:drawing>
1430
1436
  <wp:inline distT="0" distB="0" distL="0" distR="0">
1431
- <wp:extent cx="${this.pixelsToEmu(width)}" cy="${this.pixelsToEmu(height)}"/>
1437
+ <wp:extent cx="${this.pixelsToEmu(content.width)}" cy="${this.pixelsToEmu(content.height)}"/>
1432
1438
  <wp:effectExtent l="0" t="0" r="0" b="0"/>
1433
- ${this.docProperties(imageId, name, altText)}
1439
+ ${this.docProperties(imageId, name, content)}
1434
1440
  <wp:cNvGraphicFramePr>
1435
1441
  <a:graphicFrameLocks xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main" noChangeAspect="1"/>
1436
1442
  </wp:cNvGraphicFramePr>
1437
1443
  <a:graphic xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main">
1438
1444
  <a:graphicData uri="http://schemas.openxmlformats.org/drawingml/2006/picture">
1439
- ${this.pictureMarkup(imageId, relId, name, width, height)}
1445
+ ${this.pictureMarkup(imageId, relId, name, content)}
1440
1446
  </a:graphicData>
1441
1447
  </a:graphic>
1442
1448
  </wp:inline>
@@ -1447,9 +1453,9 @@ class ImagePlugin extends TemplatePlugin {
1447
1453
 
1448
1454
  return markupXml;
1449
1455
  }
1450
- docProperties(imageId, name, altText) {
1451
- if (altText) {
1452
- return `<wp:docPr id="${imageId}" name="${name}" descr="${altText}"/>`;
1456
+ docProperties(imageId, name, content) {
1457
+ if (content.altText) {
1458
+ return `<wp:docPr id="${imageId}" name="${name}" descr="${content.altText}"/>`;
1453
1459
  }
1454
1460
  return `
1455
1461
  <wp:docPr id="${imageId}" name="${name}">
@@ -1461,10 +1467,10 @@ class ImagePlugin extends TemplatePlugin {
1461
1467
  </wp:docPr>
1462
1468
  `;
1463
1469
  }
1464
- pictureMarkup(imageId, relId, name, width, height) {
1470
+ pictureMarkup(imageId, relId, name, content) {
1465
1471
  // http://officeopenxml.com/drwPic.php
1466
1472
 
1467
- // legend:
1473
+ // Legend:
1468
1474
  // nvPicPr - non-visual picture properties - id, name, etc.
1469
1475
  // blipFill - binary large image (or) picture fill - image size, image fill, etc.
1470
1476
  // spPr - shape properties - frame size, frame fill, etc.
@@ -1479,6 +1485,7 @@ class ImagePlugin extends TemplatePlugin {
1479
1485
  </pic:nvPicPr>
1480
1486
  <pic:blipFill>
1481
1487
  <a:blip r:embed="${relId}">
1488
+ ${this.transparencyMarkup(content.transparencyPercent)}
1482
1489
  <a:extLst>
1483
1490
  <a:ext uri="{28A0092B-C50C-407E-A947-70E740481C1C}">
1484
1491
  <a14:useLocalDpi xmlns:a14="http://schemas.microsoft.com/office/drawing/2010/main" val="0"/>
@@ -1493,7 +1500,7 @@ class ImagePlugin extends TemplatePlugin {
1493
1500
  <pic:spPr bwMode="auto">
1494
1501
  <a:xfrm>
1495
1502
  <a:off x="0" y="0"/>
1496
- <a:ext cx="${this.pixelsToEmu(width)}" cy="${this.pixelsToEmu(height)}"/>
1503
+ <a:ext cx="${this.pixelsToEmu(content.width)}" cy="${this.pixelsToEmu(content.height)}"/>
1497
1504
  </a:xfrm>
1498
1505
  <a:prstGeom prst="rect">
1499
1506
  <a:avLst/>
@@ -1506,6 +1513,16 @@ class ImagePlugin extends TemplatePlugin {
1506
1513
  </pic:pic>
1507
1514
  `;
1508
1515
  }
1516
+ transparencyMarkup(transparencyPercent) {
1517
+ if (transparencyPercent === null || transparencyPercent === undefined) {
1518
+ return '';
1519
+ }
1520
+ if (transparencyPercent < 0 || transparencyPercent > 100) {
1521
+ throw new ArgumentError(`Transparency percent must be between 0 and 100, but was ${transparencyPercent}.`);
1522
+ }
1523
+ const alpha = Math.round((100 - transparencyPercent) * 1000);
1524
+ return `<a:alphaModFix amt="${alpha}" />`;
1525
+ }
1509
1526
  pixelsToEmu(pixels) {
1510
1527
  // https://stackoverflow.com/questions/20194403/openxml-distance-size-units
1511
1528
  // https://docs.microsoft.com/en-us/windows/win32/vml/msdn-online-vml-units#other-units-of-measurement
@@ -3067,7 +3084,7 @@ class TemplateHandler {
3067
3084
  /**
3068
3085
  * Version number of the `easy-template-x` library.
3069
3086
  */
3070
- _defineProperty(this, "version", "3.2.1" );
3087
+ _defineProperty(this, "version", "4.1.0" );
3071
3088
  _defineProperty(this, "xmlParser", new XmlParser());
3072
3089
  _defineProperty(this, "docxParser", void 0);
3073
3090
  _defineProperty(this, "compiler", void 0);
@@ -3215,4 +3232,4 @@ class TemplateHandler {
3215
3232
  }
3216
3233
  }
3217
3234
 
3218
- export { Base64, Binary, COMMENT_NODE_NAME, ContentPartType, DelimiterSearcher, Delimiters, Docx, DocxParser, ImagePlugin, LOOP_CONTENT_TYPE, LinkPlugin, LoopPlugin, MalformedFileError, MaxXmlDepthError, MimeType, MimeTypeHelper, MissingArgumentError, MissingCloseDelimiterError, MissingStartDelimiterError, Path, PluginContent, RawXmlPlugin, Regex, ScopeData, TEXT_CONTENT_TYPE, TEXT_NODE_NAME, TagDisposition, TagOptionsParseError, TagParser, TemplateCompiler, TemplateExtension, TemplateHandler, TemplateHandlerOptions, TemplatePlugin, TextPlugin, UnclosedTagError, UnidentifiedFileTypeError, UnknownContentTypeError, UnopenedTagError, UnsupportedFileTypeError, XmlDepthTracker, XmlNode, XmlNodeType, XmlParser, XmlPart, Zip, ZipObject, createDefaultPlugins, first, inheritsFrom, isNumber, isPromiseLike, last, normalizeDoubleQuotes, pushMany, sha1, stringValue, toDictionary };
3235
+ export { ArgumentError, Base64, Binary, COMMENT_NODE_NAME, ContentPartType, DelimiterSearcher, Delimiters, Docx, DocxParser, ImagePlugin, LOOP_CONTENT_TYPE, LinkPlugin, LoopPlugin, MalformedFileError, MaxXmlDepthError, MimeType, MimeTypeHelper, MissingArgumentError, MissingCloseDelimiterError, MissingStartDelimiterError, Path, PluginContent, RawXmlPlugin, Regex, ScopeData, TEXT_CONTENT_TYPE, TEXT_NODE_NAME, TagDisposition, TagOptionsParseError, TagParser, TemplateCompiler, TemplateExtension, TemplateHandler, TemplateHandlerOptions, TemplatePlugin, TextPlugin, UnclosedTagError, UnidentifiedFileTypeError, UnknownContentTypeError, UnopenedTagError, UnsupportedFileTypeError, XmlDepthTracker, XmlNode, XmlNodeType, XmlParser, XmlPart, Zip, ZipObject, createDefaultPlugins, first, inheritsFrom, isNumber, isPromiseLike, last, normalizeDoubleQuotes, pushMany, sha1, stringValue, toDictionary };
@@ -0,0 +1,3 @@
1
+ export declare class ArgumentError extends Error {
2
+ constructor(message: string);
3
+ }
@@ -1,3 +1,4 @@
1
+ export * from './argumentError';
1
2
  export * from './malformedFileError';
2
3
  export * from './maxXmlDepthError';
3
4
  export * from './missingArgumentError';
@@ -1,4 +1,5 @@
1
- export declare class MissingArgumentError extends Error {
1
+ import { ArgumentError } from './argumentError';
2
+ export declare class MissingArgumentError extends ArgumentError {
2
3
  readonly argName: string;
3
4
  constructor(argName: string);
4
5
  }
@@ -5,8 +5,9 @@ export type ImageFormat = MimeType.Jpeg | MimeType.Png | MimeType.Gif | MimeType
5
5
  export interface ImageContent extends PluginContent {
6
6
  _type: 'image';
7
7
  source: Binary;
8
- altText?: string;
9
8
  format: ImageFormat;
10
9
  width: number;
11
10
  height: number;
11
+ altText?: string;
12
+ transparencyPercent?: number;
12
13
  }
@@ -6,5 +6,6 @@ export declare class ImagePlugin extends TemplatePlugin {
6
6
  private createMarkup;
7
7
  private docProperties;
8
8
  private pictureMarkup;
9
+ private transparencyMarkup;
9
10
  private pixelsToEmu;
10
11
  }
@@ -1,4 +1,4 @@
1
- import * as JSZip from 'jszip';
1
+ import JSZip from 'jszip';
2
2
  import { Constructor } from '../types';
3
3
  import { Binary } from '../utils';
4
4
  export declare class JsZipHelper {
@@ -1,4 +1,4 @@
1
- import * as JSZip from 'jszip';
1
+ import JSZip from 'jszip';
2
2
  import { Constructor } from '../types';
3
3
  import { Binary } from '../utils';
4
4
  export declare class ZipObject {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "easy-template-x",
3
- "version": "4.0.0",
3
+ "version": "4.1.1",
4
4
  "description": "Generate docx documents from templates, in Node or in the browser.",
5
5
  "keywords": [
6
6
  "docx",
@@ -21,15 +21,19 @@
21
21
  "url": "https://github.com/alonrbar/easy-template-x/issues"
22
22
  },
23
23
  "type": "module",
24
- "main": "dist/cjs/easy-template-x.js",
25
- "module": "dist/es/easy-template-x.js",
24
+ "main": "dist/cjs/easy-template-x.cjs",
25
+ "module": "dist/es/easy-template-x.mjs",
26
+ "exports": {
27
+ "require": "./dist/cjs/easy-template-x.cjs",
28
+ "import": "./dist/es/easy-template-x.mjs"
29
+ },
26
30
  "typings": "dist/types/index.d.ts",
27
31
  "files": [
28
32
  "dist",
29
33
  "src"
30
34
  ],
31
35
  "scripts": {
32
- "clean": "rimraf .tmp dist test-reports",
36
+ "clean": "rimraf .tmp dist test-reports && yarn clean-dist-verify",
33
37
  "typecheck": "tsc --noEmit",
34
38
  "lint": "eslint \"./{src,test}/**/!(*.d).ts\"",
35
39
  "test": "jest --verbose",
@@ -37,7 +41,13 @@
37
41
  "build-src": "rollup -c",
38
42
  "build-types": "tsc -p tsconfig.types.json --emitDeclarationOnly",
39
43
  "build": "yarn build-types && yarn build-src",
40
- "release": "yarn clean && yarn quality && yarn build"
44
+ "release": "yarn clean && yarn quality && yarn build && yarn dist-verify",
45
+ "dist-verify": "yarn dist-verify-cjs && yarn dist-verify-es",
46
+ "dist-verify-cjs": "cd dist-verify/cjs && npm install && node main.js",
47
+ "dist-verify-es": "cd dist-verify/es && npm install && node main.js",
48
+ "clean-dist-verify": "yarn clean-dist-verify-cjs && yarn clean-dist-verify-es",
49
+ "clean-dist-verify-cjs": "rimraf dist-verify/cjs/node_modules dist-verify/cjs/package-lock.json",
50
+ "clean-dist-verify-es": "rimraf dist-verify/es/node_modules dist-verify/es/package-lock.json"
41
51
  },
42
52
  "packageManager": "yarn@4.3.1",
43
53
  "dependencies": {
@@ -0,0 +1,6 @@
1
+ export class ArgumentError extends Error {
2
+
3
+ constructor(message: string) {
4
+ super(message);
5
+ }
6
+ }
@@ -1,3 +1,4 @@
1
+ export * from './argumentError';
1
2
  export * from './malformedFileError';
2
3
  export * from './maxXmlDepthError';
3
4
  export * from './missingArgumentError';
@@ -1,4 +1,6 @@
1
- export class MissingArgumentError extends Error {
1
+ import { ArgumentError } from './argumentError';
2
+
3
+ export class MissingArgumentError extends ArgumentError {
2
4
 
3
5
  public readonly argName: string;
4
6
 
@@ -7,11 +7,15 @@ export type ImageFormat = MimeType.Jpeg | MimeType.Png | MimeType.Gif | MimeType
7
7
  export interface ImageContent extends PluginContent {
8
8
  _type: 'image';
9
9
  source: Binary;
10
+ format: ImageFormat;
11
+ width: number;
12
+ height: number;
10
13
  /**
11
14
  * Optional. If this is not set the image will be marked as "decorative".
12
15
  */
13
16
  altText?: string;
14
- format: ImageFormat;
15
- width: number;
16
- height: number;
17
+ /**
18
+ * Optional. A value between 0 and 100. If this is not set the image will be fully opaque.
19
+ */
20
+ transparencyPercent?: number;
17
21
  }
@@ -1,4 +1,5 @@
1
1
  import { ScopeData, Tag, TemplateContext } from '../../compilation';
2
+ import { ArgumentError } from '../../errors';
2
3
  import { MimeTypeHelper } from '../../mimeType';
3
4
  import { XmlGeneralNode, XmlNode } from '../../xml';
4
5
  import { TemplatePlugin } from '../templatePlugin';
@@ -30,21 +31,21 @@ export class ImagePlugin extends TemplatePlugin {
30
31
  return;
31
32
  }
32
33
 
33
- // add the image file into the archive
34
+ // Add the image file into the archive
34
35
  const mediaFilePath = await context.docx.mediaFiles.add(content.source, content.format);
35
36
  const relType = MimeTypeHelper.getOfficeRelType(content.format);
36
37
  const relId = await context.currentPart.rels.add(mediaFilePath, relType);
37
38
  await context.docx.contentTypes.ensureContentType(content.format);
38
39
 
39
- // create the xml markup
40
+ // Create the xml markup
40
41
  const imageId = nextImageId++;
41
- const imageXml = this.createMarkup(imageId, relId, content.altText, content.width, content.height);
42
+ const imageXml = this.createMarkup(imageId, relId, content);
42
43
 
43
44
  XmlNode.insertAfter(imageXml, wordTextNode);
44
45
  XmlNode.remove(wordTextNode);
45
46
  }
46
47
 
47
- private createMarkup(imageId: number, relId: string, altText: string, width: number, height: number): XmlNode {
48
+ private createMarkup(imageId: number, relId: string, content: ImageContent): XmlNode {
48
49
 
49
50
  // http://officeopenxml.com/drwPicInline.php
50
51
 
@@ -62,15 +63,15 @@ export class ImagePlugin extends TemplatePlugin {
62
63
  const markupText = `
63
64
  <w:drawing>
64
65
  <wp:inline distT="0" distB="0" distL="0" distR="0">
65
- <wp:extent cx="${this.pixelsToEmu(width)}" cy="${this.pixelsToEmu(height)}"/>
66
+ <wp:extent cx="${this.pixelsToEmu(content.width)}" cy="${this.pixelsToEmu(content.height)}"/>
66
67
  <wp:effectExtent l="0" t="0" r="0" b="0"/>
67
- ${this.docProperties(imageId, name, altText)}
68
+ ${this.docProperties(imageId, name, content)}
68
69
  <wp:cNvGraphicFramePr>
69
70
  <a:graphicFrameLocks xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main" noChangeAspect="1"/>
70
71
  </wp:cNvGraphicFramePr>
71
72
  <a:graphic xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main">
72
73
  <a:graphicData uri="http://schemas.openxmlformats.org/drawingml/2006/picture">
73
- ${this.pictureMarkup(imageId, relId, name, width, height)}
74
+ ${this.pictureMarkup(imageId, relId, name, content)}
74
75
  </a:graphicData>
75
76
  </a:graphic>
76
77
  </wp:inline>
@@ -83,9 +84,9 @@ export class ImagePlugin extends TemplatePlugin {
83
84
  return markupXml;
84
85
  }
85
86
 
86
- private docProperties(imageId: number, name: string, altText: string): string {
87
- if (altText) {
88
- return `<wp:docPr id="${imageId}" name="${name}" descr="${altText}"/>`;
87
+ private docProperties(imageId: number, name: string, content: ImageContent): string {
88
+ if (content.altText) {
89
+ return `<wp:docPr id="${imageId}" name="${name}" descr="${content.altText}"/>`;
89
90
  }
90
91
 
91
92
  return `
@@ -99,11 +100,11 @@ export class ImagePlugin extends TemplatePlugin {
99
100
  `;
100
101
  }
101
102
 
102
- private pictureMarkup(imageId: number, relId: string, name: string, width: number, height: number) {
103
+ private pictureMarkup(imageId: number, relId: string, name: string, content: ImageContent) {
103
104
 
104
105
  // http://officeopenxml.com/drwPic.php
105
106
 
106
- // legend:
107
+ // Legend:
107
108
  // nvPicPr - non-visual picture properties - id, name, etc.
108
109
  // blipFill - binary large image (or) picture fill - image size, image fill, etc.
109
110
  // spPr - shape properties - frame size, frame fill, etc.
@@ -118,6 +119,7 @@ export class ImagePlugin extends TemplatePlugin {
118
119
  </pic:nvPicPr>
119
120
  <pic:blipFill>
120
121
  <a:blip r:embed="${relId}">
122
+ ${this.transparencyMarkup(content.transparencyPercent)}
121
123
  <a:extLst>
122
124
  <a:ext uri="{28A0092B-C50C-407E-A947-70E740481C1C}">
123
125
  <a14:useLocalDpi xmlns:a14="http://schemas.microsoft.com/office/drawing/2010/main" val="0"/>
@@ -132,7 +134,7 @@ export class ImagePlugin extends TemplatePlugin {
132
134
  <pic:spPr bwMode="auto">
133
135
  <a:xfrm>
134
136
  <a:off x="0" y="0"/>
135
- <a:ext cx="${this.pixelsToEmu(width)}" cy="${this.pixelsToEmu(height)}"/>
137
+ <a:ext cx="${this.pixelsToEmu(content.width)}" cy="${this.pixelsToEmu(content.height)}"/>
136
138
  </a:xfrm>
137
139
  <a:prstGeom prst="rect">
138
140
  <a:avLst/>
@@ -146,6 +148,18 @@ export class ImagePlugin extends TemplatePlugin {
146
148
  `;
147
149
  }
148
150
 
151
+ private transparencyMarkup(transparencyPercent: number) {
152
+ if (transparencyPercent === null || transparencyPercent === undefined) {
153
+ return '';
154
+ }
155
+ if (transparencyPercent < 0 || transparencyPercent > 100) {
156
+ throw new ArgumentError(`Transparency percent must be between 0 and 100, but was ${transparencyPercent}.`);
157
+ }
158
+
159
+ const alpha = Math.round((100 - transparencyPercent) * 1000);
160
+ return `<a:alphaModFix amt="${alpha}" />`;
161
+ }
162
+
149
163
  private pixelsToEmu(pixels: number): number {
150
164
 
151
165
  // https://stackoverflow.com/questions/20194403/openxml-distance-size-units
@@ -1,4 +1,4 @@
1
- import * as JSZip from 'jszip';
1
+ import JSZip from 'jszip';
2
2
  import { MissingArgumentError } from '../errors';
3
3
  import { Constructor } from '../types';
4
4
  import { Binary } from '../utils';
package/src/zip/zip.ts CHANGED
@@ -1,4 +1,4 @@
1
- import * as JSZip from 'jszip';
1
+ import JSZip from 'jszip';
2
2
  import { Constructor } from '../types';
3
3
  import { Binary } from '../utils';
4
4
  import { JsZipHelper } from './jsZipHelper';
@@ -1,4 +1,4 @@
1
- import * as JSZip from 'jszip';
1
+ import JSZip from 'jszip';
2
2
  import { Constructor } from '../types';
3
3
  import { Binary } from '../utils';
4
4
  import { JsZipHelper } from './jsZipHelper';