@schukai/monster 4.1.1 → 4.2.0
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.
@@ -13,18 +13,18 @@
|
|
13
13
|
*/
|
14
14
|
|
15
15
|
import {
|
16
|
-
|
17
|
-
|
18
|
-
|
16
|
+
assembleMethodSymbol,
|
17
|
+
CustomElement,
|
18
|
+
registerCustomElement,
|
19
19
|
} from "../../dom/customelement.mjs";
|
20
20
|
import "../notify/notify.mjs";
|
21
|
-
import {
|
22
|
-
import {
|
23
|
-
import {
|
24
|
-
import {
|
25
|
-
import {
|
21
|
+
import {ViewerStyleSheet} from "./stylesheet/viewer.mjs";
|
22
|
+
import {instanceSymbol} from "../../constants.mjs";
|
23
|
+
import {isString} from "../../types/is.mjs";
|
24
|
+
import {getGlobal} from "../../types/global.mjs";
|
25
|
+
import {MediaType, parseMediaType} from "../../types/mediatype.mjs";
|
26
26
|
|
27
|
-
export {
|
27
|
+
export {Viewer};
|
28
28
|
|
29
29
|
/**
|
30
30
|
* @private
|
@@ -45,278 +45,286 @@ const viewerElementSymbol = Symbol("viewerElement");
|
|
45
45
|
* @summary A simple viewer component for PDF, HTML and images.
|
46
46
|
*/
|
47
47
|
class Viewer extends CustomElement {
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
|
278
|
-
|
279
|
-
|
280
|
-
|
281
|
-
|
282
|
-
|
283
|
-
|
284
|
-
|
285
|
-
|
286
|
-
|
287
|
-
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
|
296
|
-
|
297
|
-
|
298
|
-
|
299
|
-
|
300
|
-
|
301
|
-
|
302
|
-
|
303
|
-
|
304
|
-
|
305
|
-
|
306
|
-
|
307
|
-
|
308
|
-
|
309
|
-
|
310
|
-
|
311
|
-
|
312
|
-
|
313
|
-
|
314
|
-
|
315
|
-
|
316
|
-
|
317
|
-
|
318
|
-
|
319
|
-
|
48
|
+
/**
|
49
|
+
* This method is called by the `instanceof` operator.
|
50
|
+
* @return {symbol}
|
51
|
+
*/
|
52
|
+
static get [instanceSymbol]() {
|
53
|
+
return Symbol.for("@schukai/monster/components/content/viewer@@instance");
|
54
|
+
}
|
55
|
+
|
56
|
+
/**
|
57
|
+
* To set the options via the HTML tag, the attribute `data-monster-options` must be used.
|
58
|
+
* @see {@link https://monsterjs.org/en/doc/#configurate-a-monster-control}
|
59
|
+
*
|
60
|
+
* The individual configuration values can be found in the table.
|
61
|
+
*
|
62
|
+
* @property {Object} templates Template definitions
|
63
|
+
* @property {string} templates.main Main template
|
64
|
+
* @property {string} content Content to be displayed in the viewer
|
65
|
+
* @property {Object} classes Css classes
|
66
|
+
* @property {string} classes.viewer Css class for the viewer
|
67
|
+
*/
|
68
|
+
get defaults() {
|
69
|
+
return Object.assign({}, super.defaults, {
|
70
|
+
templates: {
|
71
|
+
main: getTemplate(),
|
72
|
+
},
|
73
|
+
content: "<slot></slot>",
|
74
|
+
classes: {
|
75
|
+
viewer: "",
|
76
|
+
},
|
77
|
+
});
|
78
|
+
}
|
79
|
+
|
80
|
+
/**
|
81
|
+
* Sets the content of an element based on the provided content and media type.
|
82
|
+
*
|
83
|
+
* @param {string} content - The content to be set.
|
84
|
+
* @param {string} [mediaType="text/plain"] - The media type of the content. Defaults to "text/plain" if not specified.
|
85
|
+
* @return {void} This method does not return a value.
|
86
|
+
* @throws {Error} Throws an error if shadowRoot is not defined.
|
87
|
+
*/
|
88
|
+
setContent(content, mediaType = "text/plain") {
|
89
|
+
if (!this.shadowRoot) {
|
90
|
+
throw new Error("no shadow-root is defined");
|
91
|
+
}
|
92
|
+
|
93
|
+
let type;
|
94
|
+
try {
|
95
|
+
const m = new parseMediaType(mediaType);
|
96
|
+
switch (m.type) {
|
97
|
+
case "image":
|
98
|
+
return this.setImage(content);
|
99
|
+
}
|
100
|
+
|
101
|
+
mediaType = m.toString();
|
102
|
+
} catch (error) {
|
103
|
+
type = null;
|
104
|
+
}
|
105
|
+
|
106
|
+
if (mediaType === undefined || mediaType === null || mediaType === "") {
|
107
|
+
mediaType = "text/plain";
|
108
|
+
}
|
109
|
+
|
110
|
+
switch (mediaType) {
|
111
|
+
case "text/html":
|
112
|
+
this.setHTML(content);
|
113
|
+
break;
|
114
|
+
case "text/plain":
|
115
|
+
this.setPlainText(content);
|
116
|
+
break;
|
117
|
+
case "application/pdf":
|
118
|
+
this.setPDF(content);
|
119
|
+
break;
|
120
|
+
case "image/png":
|
121
|
+
case "image/jpeg":
|
122
|
+
case "image/gif":
|
123
|
+
this.setImage(content);
|
124
|
+
break;
|
125
|
+
default:
|
126
|
+
this.setOption("content", content);
|
127
|
+
}
|
128
|
+
}
|
129
|
+
|
130
|
+
/**
|
131
|
+
* Configures and embeds a PDF document into the application with customizable display settings.
|
132
|
+
*
|
133
|
+
* @param {Blob|URL|string} data The PDF data to be embedded. Can be provided as a Blob, URL or base64 string.
|
134
|
+
* @param {boolean} [navigation=true] Determines whether the navigation pane is displayed in the PDF viewer.
|
135
|
+
* @param {boolean} [toolbar=true] Controls the visibility of the toolbar in the PDF viewer.
|
136
|
+
* @param {boolean} [scrollbar=false] Configures the display of the scrollbar in the PDF viewer.
|
137
|
+
* @return {void} This method returns nothing but sets the embedded PDF as the content.
|
138
|
+
*/
|
139
|
+
setPDF(data, navigation = true, toolbar = true, scrollbar = false) {
|
140
|
+
const hashes =
|
141
|
+
"#toolbar=" +
|
142
|
+
(toolbar ? "1" : "0") +
|
143
|
+
"&navpanes=" +
|
144
|
+
(navigation ? "1" : "0") +
|
145
|
+
"&scrollbar=" +
|
146
|
+
(scrollbar ? "1" : "0");
|
147
|
+
|
148
|
+
let pdfURL = "";
|
149
|
+
if (isBlob(data)) {
|
150
|
+
pdfURL = URL.createObjectURL(data);
|
151
|
+
pdfURL += hashes;
|
152
|
+
} else if (isURL(data)) {
|
153
|
+
// check if the url already contains the hashes
|
154
|
+
if (data?.hash?.indexOf("#") === -1) {
|
155
|
+
pdfURL = data.toString() + hashes;
|
156
|
+
} else {
|
157
|
+
pdfURL = data.toString();
|
158
|
+
}
|
159
|
+
} else if (isString(data)) {
|
160
|
+
//URL.createObjectURL(data);
|
161
|
+
const blobObj = new Blob([atob(data)], {type: "application/pdf"});
|
162
|
+
const url = window.URL.createObjectURL(blobObj);
|
163
|
+
|
164
|
+
pdfURL = data;
|
165
|
+
} else {
|
166
|
+
throw new Error("Blob or URL expected");
|
167
|
+
}
|
168
|
+
|
169
|
+
const html =
|
170
|
+
'<object part="pdf" data="' +
|
171
|
+
pdfURL +
|
172
|
+
'" width="100%" height="100%" type="application/pdf"></object>';
|
173
|
+
|
174
|
+
this.setOption("content", html);
|
175
|
+
}
|
176
|
+
|
177
|
+
/**
|
178
|
+
* Sets an image for the target by accepting a blob, URL, or string representation of the image.
|
179
|
+
*
|
180
|
+
* @param {(Blob|string)} data - The image data, which can be a Blob, a valid URL, or a string representation of the image.
|
181
|
+
* @return {void} Does not return a value.
|
182
|
+
*/
|
183
|
+
setImage(data) {
|
184
|
+
if (isBlob(data)) {
|
185
|
+
data = URL.createObjectURL(data);
|
186
|
+
} else if (isURL(data)) {
|
187
|
+
// nothing to do
|
188
|
+
} else if (isString(data)) {
|
189
|
+
// nothing to do
|
190
|
+
} else {
|
191
|
+
throw new Error("Blob or URL expected");
|
192
|
+
}
|
193
|
+
|
194
|
+
this.setOption("content", '<img src="' + data + '" alt="image" />');
|
195
|
+
}
|
196
|
+
|
197
|
+
/**
|
198
|
+
*
|
199
|
+
* if the data is a string, it is interpreted as HTML.
|
200
|
+
* if the data is an url, the HTML is loaded from the url and set as content.
|
201
|
+
* if the data is an HTMLElement, the outerHTML is used as content.
|
202
|
+
*
|
203
|
+
* @param {HTMLElement|URL|string|Blob} data
|
204
|
+
*/
|
205
|
+
setHTML(data) {
|
206
|
+
if (data instanceof Blob) {
|
207
|
+
blobToText(data)
|
208
|
+
.then((html) => {
|
209
|
+
this.setOption("content", html);
|
210
|
+
})
|
211
|
+
.catch((error) => {
|
212
|
+
throw new Error(error);
|
213
|
+
});
|
214
|
+
|
215
|
+
return;
|
216
|
+
} else if (data instanceof HTMLElement) {
|
217
|
+
data = data.outerHTML;
|
218
|
+
} else if (isString(data)) {
|
219
|
+
// nothing to do
|
220
|
+
} else if (isURL(data)) {
|
221
|
+
// fetch element
|
222
|
+
getGlobal()
|
223
|
+
.fetch(data)
|
224
|
+
.then((response) => {
|
225
|
+
return response.text();
|
226
|
+
})
|
227
|
+
.then((html) => {
|
228
|
+
this.setOption("content", html);
|
229
|
+
})
|
230
|
+
.catch((error) => {
|
231
|
+
throw new Error(error);
|
232
|
+
});
|
233
|
+
} else {
|
234
|
+
throw new Error("HTMLElement or string expected");
|
235
|
+
}
|
236
|
+
|
237
|
+
this.setOption("content", data);
|
238
|
+
}
|
239
|
+
|
240
|
+
/**
|
241
|
+
* Sets the plain text content by processing the input data, which can be of various types, including Blob,
|
242
|
+
* HTMLElement, string, or a valid URL. The method extracts and sets the raw text content into a predefined option.
|
243
|
+
*
|
244
|
+
* @param {Blob|HTMLElement|string} data - The input data to be processed. It can be a Blob object, an HTMLElement,
|
245
|
+
* a plain string, or a string formatted as a valid URL. The method determines
|
246
|
+
* the data type and processes it accordingly.
|
247
|
+
* @return {void} - This method does not return any value. It processes the content and updates the relevant option
|
248
|
+
* property.
|
249
|
+
*/
|
250
|
+
setPlainText(data) {
|
251
|
+
|
252
|
+
const mkPreSpan = (text) => {
|
253
|
+
const pre = document.createElement("pre");
|
254
|
+
pre.innerText = text;
|
255
|
+
pre.setAttribute("part", "text");
|
256
|
+
return pre.outerHTML;
|
257
|
+
}
|
258
|
+
|
259
|
+
if (data instanceof Blob) {
|
260
|
+
blobToText(data)
|
261
|
+
.then((text) => {
|
262
|
+
const div = document.createElement("div");
|
263
|
+
div.innerHTML = test;
|
264
|
+
text = div.innerText;
|
265
|
+
|
266
|
+
this.setOption("content", mkPreSpan(text));
|
267
|
+
})
|
268
|
+
.catch((error) => {
|
269
|
+
throw new Error(error);
|
270
|
+
});
|
271
|
+
|
272
|
+
return;
|
273
|
+
} else if (data instanceof HTMLElement) {
|
274
|
+
data = data.outerText;
|
275
|
+
} else if (isString(data)) {
|
276
|
+
const div = document.createElement("div");
|
277
|
+
div.innerHTML = data;
|
278
|
+
data = div.innerText;
|
279
|
+
} else if (isURL(data)) {
|
280
|
+
|
281
|
+
getGlobal()
|
282
|
+
.fetch(data)
|
283
|
+
.then((response) => {
|
284
|
+
return response.text();
|
285
|
+
})
|
286
|
+
.then((text) => {
|
287
|
+
const div = document.createElement("div");
|
288
|
+
div.innerHTML = text;
|
289
|
+
text = div.innerText;
|
290
|
+
|
291
|
+
this.setOption("content", mkPreSpan(text));
|
292
|
+
})
|
293
|
+
.catch((error) => {
|
294
|
+
throw new Error(error);
|
295
|
+
});
|
296
|
+
} else {
|
297
|
+
throw new Error("HTMLElement or string expected");
|
298
|
+
}
|
299
|
+
|
300
|
+
this.setOption("content", mkPreSpan(data));
|
301
|
+
}
|
302
|
+
|
303
|
+
/**
|
304
|
+
*
|
305
|
+
* @return {Viewer}
|
306
|
+
*/
|
307
|
+
[assembleMethodSymbol]() {
|
308
|
+
super[assembleMethodSymbol]();
|
309
|
+
|
310
|
+
initControlReferences.call(this);
|
311
|
+
initEventHandler.call(this);
|
312
|
+
}
|
313
|
+
|
314
|
+
/**
|
315
|
+
*
|
316
|
+
* @return {string}
|
317
|
+
*/
|
318
|
+
static getTag() {
|
319
|
+
return "monster-viewer";
|
320
|
+
}
|
321
|
+
|
322
|
+
/**
|
323
|
+
* @return {CSSStyleSheet[]}
|
324
|
+
*/
|
325
|
+
static getCSSStyleSheet() {
|
326
|
+
return [ViewerStyleSheet];
|
327
|
+
}
|
320
328
|
}
|
321
329
|
|
322
330
|
/**
|
@@ -325,12 +333,12 @@ class Viewer extends CustomElement {
|
|
325
333
|
* @return {boolean}
|
326
334
|
*/
|
327
335
|
function isURL(variable) {
|
328
|
-
|
329
|
-
|
330
|
-
|
331
|
-
|
332
|
-
|
333
|
-
|
336
|
+
try {
|
337
|
+
new URL(variable);
|
338
|
+
return true;
|
339
|
+
} catch (error) {
|
340
|
+
return false;
|
341
|
+
}
|
334
342
|
}
|
335
343
|
|
336
344
|
/**
|
@@ -339,7 +347,7 @@ function isURL(variable) {
|
|
339
347
|
* @return {boolean}
|
340
348
|
*/
|
341
349
|
function isBlob(variable) {
|
342
|
-
|
350
|
+
return variable instanceof Blob;
|
343
351
|
}
|
344
352
|
|
345
353
|
/**
|
@@ -348,12 +356,12 @@ function isBlob(variable) {
|
|
348
356
|
* @return {Promise<unknown>}
|
349
357
|
*/
|
350
358
|
function blobToText(blob) {
|
351
|
-
|
352
|
-
|
353
|
-
|
354
|
-
|
355
|
-
|
356
|
-
|
359
|
+
return new Promise((resolve, reject) => {
|
360
|
+
const reader = new FileReader();
|
361
|
+
reader.onloadend = () => resolve(reader.result);
|
362
|
+
reader.onerror = reject;
|
363
|
+
reader.readAsText(blob);
|
364
|
+
});
|
357
365
|
}
|
358
366
|
|
359
367
|
/**
|
@@ -362,18 +370,18 @@ function blobToText(blob) {
|
|
362
370
|
* @throws {Error} no shadow-root is defined
|
363
371
|
*/
|
364
372
|
function initControlReferences() {
|
365
|
-
|
366
|
-
|
367
|
-
|
373
|
+
if (!this.shadowRoot) {
|
374
|
+
throw new Error("no shadow-root is defined");
|
375
|
+
}
|
368
376
|
|
369
|
-
|
377
|
+
this[viewerElementSymbol] = this.shadowRoot.getElementById("viewer");
|
370
378
|
}
|
371
379
|
|
372
380
|
/**
|
373
381
|
* @private
|
374
382
|
*/
|
375
383
|
function initEventHandler() {
|
376
|
-
|
384
|
+
return this;
|
377
385
|
}
|
378
386
|
|
379
387
|
/**
|
@@ -381,8 +389,8 @@ function initEventHandler() {
|
|
381
389
|
* @return {string}
|
382
390
|
*/
|
383
391
|
function getTemplate() {
|
384
|
-
|
385
|
-
|
392
|
+
// language=HTML
|
393
|
+
return `
|
386
394
|
<div id="viewer" data-monster-role="viewer" part="viewer"
|
387
395
|
data-monster-replace="path:content"
|
388
396
|
data-monster-attributes="class path:classes.viewer">
|