@teipublisher/pb-components 1.42.1 → 1.42.2

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@teipublisher/pb-components",
3
- "version": "1.42.1",
3
+ "version": "1.42.2",
4
4
  "description": "Collection of webcomponents underlying TEI Publisher",
5
5
  "repository": "https://github.com/eeditiones/tei-publisher-components.git",
6
6
  "main": "index.html",
package/pb-elements.json CHANGED
@@ -8393,6 +8393,7 @@
8393
8393
  {
8394
8394
  "name": "pb-print-preview",
8395
8395
  "path": "./src/pb-print-preview.js",
8396
+ "description": "Generates a HTML print preview of a document using the [pagedjs](https://pagedjs.org/) polyfill\nto support CSS Paged Media.",
8396
8397
  "attributes": [
8397
8398
  {
8398
8399
  "name": "src",
@@ -8401,8 +8402,21 @@
8401
8402
  },
8402
8403
  {
8403
8404
  "name": "styles",
8405
+ "description": "Additional CSS stylesheets to be loaded. Relative URLs will be resolved\nrelative to the API endpoint.",
8404
8406
  "type": "string"
8405
8407
  },
8408
+ {
8409
+ "name": "url",
8410
+ "description": "The generated API URL from which the HTML content will be loaded.\nReadonly.",
8411
+ "type": "string",
8412
+ "default": "\"about:blank\""
8413
+ },
8414
+ {
8415
+ "name": "raw",
8416
+ "description": "Set to disable the pagejs polyfill, i.e. show the HTML as delivered from the server.",
8417
+ "type": "boolean",
8418
+ "default": "false"
8419
+ },
8406
8420
  {
8407
8421
  "name": "subscribe",
8408
8422
  "description": "The name of the channel to subscribe to. Only events on a channel corresponding\nto this property are listened to.",
@@ -8445,8 +8459,23 @@
8445
8459
  {
8446
8460
  "name": "styles",
8447
8461
  "attribute": "styles",
8462
+ "description": "Additional CSS stylesheets to be loaded. Relative URLs will be resolved\nrelative to the API endpoint.",
8448
8463
  "type": "string"
8449
8464
  },
8465
+ {
8466
+ "name": "url",
8467
+ "attribute": "url",
8468
+ "description": "The generated API URL from which the HTML content will be loaded.\nReadonly.",
8469
+ "type": "string",
8470
+ "default": "\"about:blank\""
8471
+ },
8472
+ {
8473
+ "name": "raw",
8474
+ "attribute": "raw",
8475
+ "description": "Set to disable the pagejs polyfill, i.e. show the HTML as delivered from the server.",
8476
+ "type": "boolean",
8477
+ "default": "false"
8478
+ },
8450
8479
  {
8451
8480
  "name": "subscribe",
8452
8481
  "attribute": "subscribe",
@@ -59,6 +59,7 @@ import './pb-table-grid.js';
59
59
  import './pb-split-list.js';
60
60
  import './pb-timeline.js';
61
61
  import './pb-combo-box.js';
62
+ import './pb-print-preview.js';
62
63
 
63
64
  import '@polymer/iron-icons/editor-icons';
64
65
  import '@polymer/iron-icons/social-icons';
@@ -1,13 +1,11 @@
1
- import { LitElement, html } from 'lit-element';
2
- import { Previewer } from "pagedjs/dist/paged.esm";
1
+ import { LitElement, html, css } from 'lit-element';
3
2
  import { pbMixin } from './pb-mixin.js';
4
3
  import { resolveURL } from './utils.js';
5
4
 
6
5
  /**
6
+ * Generates a HTML print preview of a document using the [pagedjs](https://pagedjs.org/) polyfill
7
+ * to support CSS Paged Media.
7
8
  *
8
- *
9
- * @customElement pb-print-preview
10
- * @polymer
11
9
  * @demo demo/pb-print-preview.html
12
10
  * @appliesMixin pbMixin
13
11
  */
@@ -21,42 +19,64 @@ export class PbPrintPreview extends pbMixin(LitElement) {
21
19
  src: {
22
20
  type: String
23
21
  },
22
+ /**
23
+ * Additional CSS stylesheets to be loaded. Relative URLs will be resolved
24
+ * relative to the API endpoint.
25
+ */
24
26
  styles: {
25
27
  type: String
26
28
  },
29
+ /**
30
+ * The generated API URL from which the HTML content will be loaded.
31
+ * Readonly.
32
+ */
33
+ url: {
34
+ type: String,
35
+ reflect: true,
36
+ readOnly: true
37
+ },
38
+ /**
39
+ * Set to disable the pagejs polyfill, i.e. show the HTML as delivered from the server.
40
+ */
41
+ raw: {
42
+ type: Boolean
43
+ },
27
44
  ...super.properties
28
45
  };
29
46
  }
30
47
 
31
48
  constructor() {
32
49
  super();
33
- }
34
-
35
- connectedCallback() {
36
- super.connectedCallback();
37
50
 
38
- if (!document.head.querySelector('#pb-print-preview-interface')) {
39
- const style = document.createElement('link');
40
- style.id = 'pb-print-preview-interface';
41
- style.rel = 'Stylesheet';
42
- style.type = 'text/css';
43
- style.href = resolveURL('../css/pagedjs/interface.css');
44
- document.head.appendChild(style);
45
- }
51
+ this.url = 'about:blank';
52
+ this.raw = false;
46
53
  }
47
54
 
48
55
  firstUpdated() {
49
56
  super.firstUpdated();
50
57
 
51
- this._container = this.querySelector('.content');
58
+ this._iframe = this.shadowRoot.querySelector('iframe');
59
+ this._iframe.addEventListener('load', this._preview.bind(this));
52
60
 
53
61
  PbPrintPreview.waitOnce('pb-page-ready', () => {
54
62
  this.refresh();
55
63
  });
56
64
  }
57
65
 
66
+ /**
67
+ * Open the browser's print dialog.
68
+ */
69
+ print() {
70
+ this._iframe.contentWindow.print();
71
+ }
72
+
73
+ /**
74
+ * Refresh the displayed content.
75
+ */
58
76
  refresh() {
59
77
  this.emitTo('pb-start-update');
78
+ this._iframe.className = 'hidden';
79
+ this._iframe.src = 'about:blank';
60
80
 
61
81
  const doc = this.getDocument();
62
82
  const params = new URLSearchParams();
@@ -66,29 +86,48 @@ export class PbPrintPreview extends pbMixin(LitElement) {
66
86
  }
67
87
  params.set('base', `${this.getEndpoint()}/${doc.getCollection()}`);
68
88
 
69
- const url = `${this.getEndpoint()}/api/document/${encodeURIComponent(doc.path)}/print?${params.toString()}`;
70
- fetch(url, {
71
- method: 'GET',
72
- mode: 'cors',
73
- credentials: 'same-origin'
74
- })
75
- .then((response) => response.text())
76
- .then((data) => {
77
- const previewer = new Previewer();
78
- // previewer.registerHandlers(previewHandler(this._container));
79
- previewer.preview(data, this._getCustomStyles(), this._container);
80
- this.emitTo('pb-end-update');
81
- });
89
+ this.url = `${this.getEndpoint()}/api/document/${encodeURIComponent(doc.path)}/print?${params.toString()}`;
90
+ this._iframe.src = this.url;
82
91
  }
83
92
 
84
93
  render() {
85
94
  return html`
86
- <div class="content"></div>
95
+ <iframe title="Preview" src="about:blank"></iframe>
87
96
  `;
88
97
  }
89
98
 
90
- createRenderRoot() {
91
- return this;
99
+ _preview() {
100
+ const idoc = this._iframe.contentDocument;
101
+ if (!idoc.body.firstElementChild) {
102
+ return;
103
+ }
104
+
105
+ this.emitTo('pb-end-update');
106
+ idoc.body.className = 'content';
107
+
108
+ const customStyles = this._getCustomStyles();
109
+ const firstLink = idoc.head.querySelector('link');
110
+ if (firstLink) {
111
+ customStyles.reverse();
112
+ }
113
+ customStyles.forEach((style) => {
114
+ const link = idoc.createElement('link');
115
+ link.rel = 'Stylesheet';
116
+ link.type = 'text/css';
117
+ link.href = style;
118
+ if (firstLink) {
119
+ firstLink.before(link);
120
+ } else {
121
+ idoc.head.appendChild(link);
122
+ }
123
+ });
124
+
125
+ if (!this.raw) {
126
+ const script = idoc.createElement('script');
127
+ script.src = resolveURL('../lib/paged.polyfill.js');
128
+ idoc.head.appendChild(script);
129
+ }
130
+ this._iframe.className = '';
92
131
  }
93
132
 
94
133
  _getCustomStyles() {
@@ -100,12 +139,31 @@ export class PbPrintPreview extends pbMixin(LitElement) {
100
139
  this.toAbsoluteURL(href, this.getEndpoint())
101
140
  );
102
141
  }
103
- if (doc.odd) {
104
- customStyles.push(`${this.getEndpoint()}/transform/${doc.odd}.css`);
105
- }
106
- return customStyles;
107
142
  }
143
+ customStyles.push(resolveURL('../css/pagedjs/interface.css'));
108
144
  return customStyles;
109
145
  }
146
+
147
+ static get styles() {
148
+ return css`
149
+ :host {
150
+ display: block;
151
+ }
152
+
153
+ iframe {
154
+ border: 0;
155
+ width: 100%;
156
+ height: 100%;
157
+ opacity: 1;
158
+ transition: opacity 0.5s ease-out 0.5s;
159
+ }
160
+
161
+ .hidden {
162
+ opacity: 0;
163
+ transition: opacity 0.5s ease-out;
164
+ }
165
+
166
+ `;
167
+ }
110
168
  }
111
169
  customElements.define('pb-print-preview', PbPrintPreview);