@schukai/monster 4.24.3 → 4.25.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,21 +13,24 @@
13
13
  */
14
14
 
15
15
  import {
16
- assembleMethodSymbol,
17
- CustomElement,
18
- registerCustomElement,
16
+ assembleMethodSymbol,
17
+ CustomElement,
18
+ registerCustomElement,
19
19
  } from "../../dom/customelement.mjs";
20
20
  import "../notify/notify.mjs";
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
- import { MarkdownToHTML } from "../../text/markdown-parser.mjs";
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
+ import {MarkdownToHTML} from "../../text/markdown-parser.mjs";
27
27
  import "../layout/tabs.mjs";
28
28
  import "./viewer/message.mjs";
29
+ import {getLocaleOfDocument} from "../../dom/locale.mjs";
30
+ import {Button} from "../form/button.mjs";
31
+ import {findTargetElementFromEvent} from "../../dom/events.mjs";
29
32
 
30
- export { Viewer };
33
+ export {Viewer};
31
34
 
32
35
  /**
33
36
  * @private
@@ -48,604 +51,657 @@ const viewerElementSymbol = Symbol("viewerElement");
48
51
  * @summary A simple viewer component for PDF, HTML, and images.
49
52
  */
50
53
  class Viewer extends CustomElement {
51
- /**
52
- * This method is called by the `instanceof` operator.
53
- * @return {symbol}
54
- */
55
- static get [instanceSymbol]() {
56
- return Symbol.for("@schukai/monster/components/content/viewer@@instance");
57
- }
58
-
59
- /**
60
- * To set the options via the HTML tag, the attribute `data-monster-options` must be used.
61
- * @see {@link https://monsterjs.org/en/doc/#configurate-a-monster-control}
62
- *
63
- * The individual configuration values can be found in the table.
64
- *
65
- * @property {Object} templates Template definitions
66
- * @property {string} templates.main Main template
67
- * @property {string} content Content to be displayed in the viewer
68
- * @property {Object} classes Css classes
69
- * @property {string} classes.viewer Css class for the viewer
70
- * @property {Object} renderers Renderers for different media types
71
- * @property {function} renderers.image Function to render image content
72
- * @property {function} renderers.html Function to render HTML content
73
- * @property {function} renderers.pdf Function to render PDF content
74
- * @property {function} renderers.plaintext Function to render plain text content
75
- * @property {function} renderers.markdown Function to render Markdown content
76
- */
77
- get defaults() {
78
- return Object.assign({}, super.defaults, {
79
- templates: {
80
- main: getTemplate(),
81
- },
82
- content: "<slot></slot>",
83
- classes: {
84
- viewer: "",
85
- },
86
- renderers: {
87
- image: this.setImage,
88
- html: this.setHTML,
89
- pdf: this.setPDF,
90
- plaintext: this.setPlainText,
91
- markdown: this.setMarkdown,
92
- audio: this.setAudio,
93
- video: this.setVideo,
94
- message: this.setMessage,
95
- },
96
- });
97
- }
98
-
99
- /**
100
- * Sets the content of an element based on the provided content and media type.
101
- *
102
- * @param {string} content - The content to be set.
103
- * @param {string} [mediaType="text/plain"] - The media type of the content. Defaults to "text/plain" if not specified.
104
- * @return {void} This method does not return a value.
105
- * @throws {Error} Throws an error if shadowRoot is not defined.
106
- */
107
- setContent(content, mediaType = "text/plain") {
108
- if (!this.shadowRoot) {
109
- throw new Error("no shadow-root is defined");
110
- }
111
-
112
- const renderers = this.getOption("renderers");
113
-
114
- const isDataURL = (value) => {
115
- return (
116
- (typeof value === "string" && value.startsWith("data:")) ||
117
- (value instanceof URL && value.protocol === "data:")
118
- );
119
- };
120
-
121
- if (isDataURL(content)) {
122
- try {
123
- const dataUrl = content.toString();
124
- const [header] = dataUrl.split(",");
125
- const [typeSegment] = header.split(";");
126
- mediaType = typeSegment.replace("data:", "") || "text/plain";
127
- } catch (error) {
128
- this.dispatchEvent(
129
- new CustomEvent("viewer-error", {
130
- detail: "Invalid data URL format",
131
- }),
132
- );
133
- return;
134
- }
135
- }
136
-
137
- if (mediaType === undefined || mediaType === null || mediaType === "") {
138
- mediaType = "text/plain";
139
- }
140
-
141
- let mediaTypeObject;
142
-
143
- try {
144
- mediaTypeObject = new parseMediaType(mediaType);
145
- if (!(mediaTypeObject instanceof MediaType)) {
146
- this.dispatchEvent(
147
- new CustomEvent("viewer-error", { detail: "Invalid MediaType" }),
148
- );
149
- return;
150
- }
151
- } catch (error) {
152
- this.dispatchEvent(new CustomEvent("viewer-error", { detail: error }));
153
- return;
154
- }
155
-
156
- const checkRenderer = (renderer, contentType) => {
157
- if (renderers && typeof renderers[renderer] === "function") {
158
- return true;
159
- } else {
160
- this.dispatchEvent(
161
- new CustomEvent("viewer-error", {
162
- detail: `Renderer for ${contentType} not found`,
163
- }),
164
- );
165
- return false;
166
- }
167
- };
168
-
169
- switch (mediaTypeObject.type) {
170
- case "text":
171
- switch (mediaTypeObject.subtype) {
172
- case "html":
173
- if (checkRenderer("html", mediaTypeObject.toString())) {
174
- renderers.html.call(this, content);
175
- }
176
- break;
177
- case "plain":
178
- if (checkRenderer("plaintext", mediaTypeObject.toString())) {
179
- renderers.plaintext.call(this, content);
180
- }
181
- break;
182
- case "markdown":
183
- if (checkRenderer("markdown", mediaTypeObject.toString())) {
184
- this.setMarkdown(content);
185
- }
186
- break;
187
- default:
188
- if (checkRenderer("plaintext", mediaTypeObject.toString())) {
189
- renderers.plaintext.call(this, content);
190
- }
191
- break;
192
- }
193
- break;
194
-
195
- case "application":
196
- switch (mediaTypeObject.subtype) {
197
- case "pdf":
198
- if (checkRenderer("pdf", mediaTypeObject.toString())) {
199
- renderers.pdf.call(this, content);
200
- }
201
- break;
202
-
203
- default:
204
- this.setOption("content", content);
205
- break;
206
- }
207
- break;
208
-
209
- case "message":
210
- switch (mediaTypeObject.subtype) {
211
- case "rfc822":
212
- if (checkRenderer("message", mediaTypeObject.toString())) {
213
- renderers.message.call(this, content);
214
- }
215
- break;
216
-
217
- default:
218
- this.setOption("content", content);
219
- break;
220
- }
221
- break;
222
-
223
- case "audio":
224
- if (checkRenderer(mediaTypeObject.type, mediaTypeObject.toString())) {
225
- renderers[mediaTypeObject.type].call(this, content);
226
- }
227
- break;
228
-
229
- case "video":
230
- if (checkRenderer(mediaTypeObject.type, mediaTypeObject.toString())) {
231
- renderers[mediaTypeObject.type].call(this, content);
232
- }
233
- break;
234
-
235
- case "image":
236
- if (checkRenderer("image", mediaTypeObject.toString())) {
237
- renderers.image.call(this, content);
238
- }
239
- break;
240
-
241
- default:
242
- this.setOption("content", content);
243
- this.dispatchEvent(
244
- new CustomEvent("viewer-error", {
245
- detail: `Unsupported media type: ${mediaTypeObject.toString()}`,
246
- }),
247
- ); // Notify about unsupported media type
248
- return;
249
- }
250
- }
251
-
252
- /**
253
- * Sets the audio content for the viewer. Accepts a Blob, URL, or string and processes it
254
- * to configure audio playback within the viewer. Throws an error if the input type is invalid.
255
- *
256
- * @param {Blob|string} data - The audio content. This can be a Blob, a URL, or a string.
257
- * @return {void} No return value.
258
- */
259
- setAudio(data) {
260
- if (isBlob(data)) {
261
- data = URL.createObjectURL(data);
262
- } else if (isURL(data)) {
263
- // nothing to do
264
- } else if (isString(data)) {
265
- // nothing to do
266
- } else {
267
- this.dispatchEvent(
268
- new CustomEvent("viewer-error", { detail: "Blob or URL expected" }),
269
- );
270
- throw new Error("Blob or URL expected");
271
- }
272
-
273
- this.setOption(
274
- "content",
275
- `
54
+ /**
55
+ * This method is called by the `instanceof` operator.
56
+ * @return {symbol}
57
+ */
58
+ static get [instanceSymbol]() {
59
+ return Symbol.for("@schukai/monster/components/content/viewer@@instance");
60
+ }
61
+
62
+ /**
63
+ * To set the options via the HTML tag, the attribute `data-monster-options` must be used.
64
+ * @see {@link https://monsterjs.org/en/doc/#configurate-a-monster-control}
65
+ *
66
+ * The individual configuration values can be found in the table.
67
+ *
68
+ * @property {Object} templates Template definitions
69
+ * @property {string} templates.main Main template
70
+ * @property {string} content Content to be displayed in the viewer
71
+ * @property {Object} classes Css classes
72
+ * @property {string} classes.viewer Css class for the viewer
73
+ * @property {Object} renderers Renderers for different media types
74
+ * @property {function} renderers.image Function to render image content
75
+ * @property {function} renderers.html Function to render HTML content
76
+ * @property {function} renderers.pdf Function to render PDF content
77
+ * @property {function} renderers.plaintext Function to render plain text content
78
+ * @property {function} renderers.markdown Function to render Markdown content
79
+ */
80
+ get defaults() {
81
+ return Object.assign({}, super.defaults, {
82
+ templates: {
83
+ main: getTemplate(),
84
+ },
85
+ content: "<slot></slot>",
86
+ classes: {
87
+ viewer: "",
88
+ },
89
+ labels: getLabels(),
90
+ renderers: {
91
+ image: this.setImage,
92
+ html: this.setHTML,
93
+ pdf: this.setPDF,
94
+ download: this.setDownload,
95
+ plaintext: this.setPlainText,
96
+ markdown: this.setMarkdown,
97
+ audio: this.setAudio,
98
+ video: this.setVideo,
99
+ message: this.setMessage,
100
+ },
101
+
102
+ });
103
+ }
104
+
105
+ /**
106
+ * Sets the content of an element based on the provided content and media type.
107
+ *
108
+ * @param {string} content - The content to be set.
109
+ * @param {string} [mediaType="text/plain"] - The media type of the content. Defaults to "text/plain" if not specified.
110
+ * @return {void} This method does not return a value.
111
+ * @throws {Error} Throws an error if shadowRoot is not defined.
112
+ */
113
+ setContent(content, mediaType = "text/plain") {
114
+ if (!this.shadowRoot) {
115
+ throw new Error("no shadow-root is defined");
116
+ }
117
+
118
+ const renderers = this.getOption("renderers");
119
+
120
+ const isDataURL = (value) => {
121
+ return (
122
+ (typeof value === "string" && value.startsWith("data:")) ||
123
+ (value instanceof URL && value.protocol === "data:")
124
+ );
125
+ };
126
+
127
+ if (isDataURL(content)) {
128
+ try {
129
+ const dataUrl = content.toString();
130
+ const [header] = dataUrl.split(",");
131
+ const [typeSegment] = header.split(";");
132
+ mediaType = typeSegment.replace("data:", "") || "text/plain";
133
+ } catch (error) {
134
+ this.dispatchEvent(
135
+ new CustomEvent("viewer-error", {
136
+ detail: "Invalid data URL format",
137
+ }),
138
+ );
139
+ return;
140
+ }
141
+ }
142
+
143
+ if (mediaType === undefined || mediaType === null || mediaType === "") {
144
+ mediaType = "text/plain";
145
+ }
146
+
147
+ let mediaTypeObject;
148
+
149
+ try {
150
+ mediaTypeObject = new parseMediaType(mediaType);
151
+ if (!(mediaTypeObject instanceof MediaType)) {
152
+ this.dispatchEvent(
153
+ new CustomEvent("viewer-error", {detail: "Invalid MediaType"}),
154
+ );
155
+ return;
156
+ }
157
+ } catch (error) {
158
+ this.dispatchEvent(new CustomEvent("viewer-error", {detail: error}));
159
+ return;
160
+ }
161
+
162
+ const checkRenderer = (renderer, contentType) => {
163
+ if (renderers && typeof renderers[renderer] === "function") {
164
+ return true;
165
+ } else {
166
+ this.dispatchEvent(
167
+ new CustomEvent("viewer-error", {
168
+ detail: `Renderer for ${contentType} not found`,
169
+ }),
170
+ );
171
+ return false;
172
+ }
173
+ };
174
+
175
+ switch (mediaTypeObject.type) {
176
+ case "text":
177
+ switch (mediaTypeObject.subtype) {
178
+ case "html":
179
+ if (checkRenderer("html", mediaTypeObject.toString())) {
180
+ renderers.html.call(this, content);
181
+ }
182
+ break;
183
+ case "plain":
184
+ if (checkRenderer("plaintext", mediaTypeObject.toString())) {
185
+ renderers.plaintext.call(this, content);
186
+ }
187
+ break;
188
+ case "markdown":
189
+ if (checkRenderer("markdown", mediaTypeObject.toString())) {
190
+ this.setMarkdown(content);
191
+ }
192
+ break;
193
+ default:
194
+ if (checkRenderer("plaintext", mediaTypeObject.toString())) {
195
+ renderers.plaintext.call(this, content);
196
+ }
197
+ break;
198
+ }
199
+ break;
200
+
201
+ case "application":
202
+ switch (mediaTypeObject.subtype) {
203
+ case "pdf":
204
+ if (checkRenderer("pdf", mediaTypeObject.toString())) {
205
+ renderers.pdf.call(this, content);
206
+ }
207
+ break;
208
+
209
+ default:
210
+
211
+ // Handle octet-stream as a generic binary data type
212
+ if (checkRenderer("download", mediaTypeObject.toString())) {
213
+ renderers.download.call(this, content);
214
+ break;
215
+ }
216
+
217
+ this.setOption("content", content + "!!");
218
+ break;
219
+ }
220
+ break;
221
+
222
+ case "message":
223
+ switch (mediaTypeObject.subtype) {
224
+ case "rfc822":
225
+ if (checkRenderer("message", mediaTypeObject.toString())) {
226
+ renderers.message.call(this, content);
227
+ }
228
+ break;
229
+
230
+ default:
231
+ this.setOption("content", content);
232
+ break;
233
+ }
234
+ break;
235
+
236
+ case "audio":
237
+ if (checkRenderer(mediaTypeObject.type, mediaTypeObject.toString())) {
238
+ renderers[mediaTypeObject.type].call(this, content);
239
+ }
240
+ break;
241
+
242
+ case "video":
243
+ if (checkRenderer(mediaTypeObject.type, mediaTypeObject.toString())) {
244
+ renderers[mediaTypeObject.type].call(this, content);
245
+ }
246
+ break;
247
+
248
+ case "image":
249
+ if (checkRenderer("image", mediaTypeObject.toString())) {
250
+ renderers.image.call(this, content);
251
+ }
252
+ break;
253
+
254
+ default:
255
+ this.setOption("content", content);
256
+ this.dispatchEvent(
257
+ new CustomEvent("viewer-error", {
258
+ detail: `Unsupported media type: ${mediaTypeObject.toString()}`,
259
+ }),
260
+ ); // Notify about unsupported media type
261
+ return;
262
+ }
263
+ }
264
+
265
+ /**
266
+ * Sets the audio content for the viewer. Accepts a Blob, URL, or string and processes it
267
+ * to configure audio playback within the viewer. Throws an error if the input type is invalid.
268
+ *
269
+ * @param {Blob|string} data - The audio content. This can be a Blob, a URL, or a string.
270
+ * @return {void} No return value.
271
+ */
272
+ setAudio(data) {
273
+ if (isBlob(data)) {
274
+ data = URL.createObjectURL(data);
275
+ } else if (isURL(data)) {
276
+ // nothing to do
277
+ } else if (isString(data)) {
278
+ // nothing to do
279
+ } else {
280
+ this.dispatchEvent(
281
+ new CustomEvent("viewer-error", {detail: "Blob or URL expected"}),
282
+ );
283
+ throw new Error("Blob or URL expected");
284
+ }
285
+
286
+ this.setOption(
287
+ "content",
288
+ `
276
289
  <audio controls part="audio" style="max-width: 100%">
277
290
  <source src="${data}">
278
291
  </audio>`,
279
- );
280
- }
281
-
282
- /**
283
- * Sets the video content for the viewer. The method accepts a Blob, URL, or string,
284
- * verifies its type, and updates the viewer's content accordingly.
285
- *
286
- * @param {Blob|string} data - The video data to set. It can be a Blob, URL, or string.
287
- * @return {void} This method does not return a value. It updates the viewer's state.
288
- * @throws {Error} Throws an error if the provided data is not a Blob or URL.
289
- */
290
- setVideo(data) {
291
- if (isBlob(data)) {
292
- data = URL.createObjectURL(data);
293
- } else if (isURL(data)) {
294
- // nothing to do
295
- } else if (isString(data)) {
296
- // nothing to do
297
- } else {
298
- this.dispatchEvent(
299
- new CustomEvent("viewer-error", { detail: "Blob or URL expected" }),
300
- );
301
- throw new Error("Blob or URL expected");
302
- }
303
-
304
- this.setOption(
305
- "content",
306
- `
292
+ );
293
+ }
294
+
295
+ /**
296
+ * Sets the video content for the viewer. The method accepts a Blob, URL, or string,
297
+ * verifies its type, and updates the viewer's content accordingly.
298
+ *
299
+ * @param {Blob|string} data - The video data to set. It can be a Blob, URL, or string.
300
+ * @return {void} This method does not return a value. It updates the viewer's state.
301
+ * @throws {Error} Throws an error if the provided data is not a Blob or URL.
302
+ */
303
+ setVideo(data) {
304
+ if (isBlob(data)) {
305
+ data = URL.createObjectURL(data);
306
+ } else if (isURL(data)) {
307
+ // nothing to do
308
+ } else if (isString(data)) {
309
+ // nothing to do
310
+ } else {
311
+ this.dispatchEvent(
312
+ new CustomEvent("viewer-error", {detail: "Blob or URL expected"}),
313
+ );
314
+ throw new Error("Blob or URL expected");
315
+ }
316
+
317
+ this.setOption(
318
+ "content",
319
+ `
307
320
  <video controls part="video" style="max-width: 100%">
308
321
  <source src="${data}">
309
322
  </video>`,
310
- );
311
- }
312
-
313
- /**
314
- * Renders Markdown content using built-in or custom Markdown parser.
315
- * Overrideable via `customRenderers['text/markdown']`.
316
- *
317
- * @param {string|Blob} data
318
- */
319
- setMarkdown(data) {
320
- if (isBlob(data)) {
321
- blobToText(data)
322
- .then((markdownText) => {
323
- try {
324
- const html = MarkdownToHTML.convert(markdownText);
325
- this.setHTML(html);
326
- } catch (error) {
327
- this.setPlainText(markdownText); // Fallback to plain text if conversion fails
328
- this.dispatchEvent(
329
- new CustomEvent("viewer-error", { detail: error }),
330
- );
331
- }
332
- })
333
- .catch((error) => {
334
- this.dispatchEvent(
335
- new CustomEvent("viewer-error", { detail: error }),
336
- );
337
- throw new Error(error);
338
- });
339
- return;
340
- } else if (isURL(data)) {
341
- getGlobal()
342
- .fetch(data)
343
- .then((response) => {
344
- return response.text();
345
- })
346
- .then((markdownText) => {
347
- try {
348
- const html = MarkdownToHTML.convert(markdownText);
349
- this.setHTML(html);
350
- } catch (error) {
351
- this.setPlainText(markdownText); // Fallback to plain text if conversion fails
352
- this.dispatchEvent(
353
- new CustomEvent("viewer-error", { detail: error }),
354
- );
355
- }
356
- })
357
- .catch((error) => {
358
- this.dispatchEvent(
359
- new CustomEvent("viewer-error", { detail: error }),
360
- );
361
- throw new Error(error);
362
- });
363
- return;
364
- } else if (isString(data)) {
365
- try {
366
- const html = MarkdownToHTML.convert(data);
367
- this.setHTML(html);
368
- } catch (error) {
369
- this.setPlainText(data); // Fallback to plain text if conversion fails
370
- this.dispatchEvent(new CustomEvent("viewer-error", { detail: error }));
371
- }
372
- return;
373
- }
374
-
375
- this.dispatchEvent(
376
- new CustomEvent("viewer-error", { detail: "Blob or string expected" }),
377
- );
378
- throw new Error("Blob or string expected");
379
- }
380
-
381
- /**
382
- * Configures and embeds a PDF document into the application with customizable display settings.
383
- *
384
- * @param {Blob|URL|string} data The PDF data to be embedded. Can be provided as a Blob, URL, or base64 string.
385
- * @param {boolean} [navigation=true] Determines whether the navigation pane is displayed in the PDF viewer.
386
- * @param {boolean} [toolbar=true] Controls the visibility of the toolbar in the PDF viewer.
387
- * @param {boolean} [scrollbar=false] Configures the display of the scrollbar in the PDF viewer.
388
- * @return {void} This method returns nothing but sets the embedded PDF as the content.
389
- */
390
- setPDF(data, navigation = true, toolbar = true, scrollbar = false) {
391
- const hashes =
392
- "#toolbar=" +
393
- (toolbar ? "1" : "0") +
394
- "&navpanes=" +
395
- (navigation ? "1" : "0") +
396
- "&scrollbar=" +
397
- (scrollbar ? "1" : "0");
398
-
399
- let pdfURL = "";
400
- if (isBlob(data)) {
401
- pdfURL = URL.createObjectURL(data);
402
- pdfURL += hashes;
403
- } else if (isURL(data)) {
404
- // check if the url already contains the hashes
405
- if (data?.hash?.indexOf("#") === -1) {
406
- pdfURL = data.toString() + hashes;
407
- } else {
408
- pdfURL = data.toString();
409
- }
410
- } else if (isString(data)) {
411
- //URL.createObjectURL(data);
412
- const blobObj = new Blob([atob(data)], { type: "application/pdf" });
413
- const url = window.URL.createObjectURL(blobObj);
414
-
415
- pdfURL = data;
416
- } else {
417
- this.dispatchEvent(
418
- new CustomEvent("viewer-error", { detail: "Blob or URL expected" }),
419
- );
420
- throw new Error("Blob or URL expected");
421
- }
422
-
423
- const html =
424
- '<object part="pdf" data="' +
425
- pdfURL +
426
- '" width="100%" height="100%" type="application/pdf"></object>';
427
-
428
- this.setOption("content", html);
429
- }
430
-
431
- /**
432
- * Sets the content for displaying an email message.
433
- * The data is expected to be an object with a structure containing
434
- * 'to', 'from', 'subject', 'parts', and 'headers'.
435
- * The parts are processed to display plain text and HTML in separate tabs,
436
- * and attachments are listed.
437
- *
438
- * @param {object} emailData - The structured email data.
439
- */
440
- setMessage(emailData) {
441
- if (!emailData || typeof emailData !== "object") {
442
- this.dispatchEvent(
443
- new CustomEvent("viewer-error", {
444
- detail: "Invalid email data provided",
445
- }),
446
- );
447
- return;
448
- }
449
-
450
- this.setOption(
451
- "content",
452
- '<monster-message-content part="message"></monster-message-content>',
453
- );
454
-
455
- setTimeout(() => {
456
- const messageContent = this.shadowRoot.querySelector(
457
- "monster-message-content",
458
- );
459
- if (!messageContent) {
460
- this.dispatchEvent(
461
- new CustomEvent("viewer-error", {
462
- detail: "Message content element not found",
463
- }),
464
- );
465
- return;
466
- }
467
-
468
- messageContent.setMessage(emailData);
469
- }, 100);
470
- }
471
-
472
- /**
473
- * Sets an image for the target by accepting a blob, URL, or string representation of the image.
474
- *
475
- * @param {(Blob|string)} data - The image data, which can be a Blob, a valid URL, or a string representation of the image.
476
- * @return {void} Does not return a value.
477
- */
478
- setImage(data) {
479
- if (isBlob(data)) {
480
- data = URL.createObjectURL(data);
481
- } else if (isURL(data)) {
482
- // nothing to do
483
- } else if (isString(data)) {
484
- // nothing to do
485
- } else {
486
- this.dispatchEvent(
487
- new CustomEvent("viewer-error", { detail: "Blob or URL expected" }),
488
- );
489
- throw new Error("Blob or URL expected");
490
- }
491
-
492
- this.setOption(
493
- "content",
494
- `<img style="max-width: 100%" src="${data}" alt="image" part="image" onerror="this.dispatchEvent(new CustomEvent('viewer-error', {detail: 'Image loading error'}));">`,
495
- );
496
- }
497
-
498
- /**
499
- *
500
- * if the data is a string, it is interpreted as HTML.
501
- * if the data is a URL, the HTML is loaded from the url and set as content.
502
- * if the data is an HTMLElement, the outerHTML is used as content.
503
- *
504
- * @param {HTMLElement|URL|string|Blob} data
505
- */
506
- setHTML(data) {
507
- if (data instanceof Blob) {
508
- blobToText(data)
509
- .then((html) => {
510
- this.setOption("content", html);
511
- })
512
- .catch((error) => {
513
- this.dispatchEvent(
514
- new CustomEvent("viewer-error", { detail: error }),
515
- );
516
- throw new Error(error);
517
- });
518
-
519
- return;
520
- } else if (data instanceof HTMLElement) {
521
- data = data.outerHTML;
522
- } else if (isString(data)) {
523
- // nothing to do
524
- } else if (isURL(data)) {
525
- // fetch element
526
- getGlobal()
527
- .fetch(data)
528
- .then((response) => {
529
- return response.text();
530
- })
531
- .then((html) => {
532
- this.setOption("content", html);
533
- })
534
- .catch((error) => {
535
- this.dispatchEvent(
536
- new CustomEvent("viewer-error", { detail: error }),
537
- );
538
- throw new Error(error);
539
- });
540
- } else {
541
- this.dispatchEvent(
542
- new CustomEvent("viewer-error", {
543
- detail: "HTMLElement or string expected",
544
- }),
545
- );
546
- throw new Error("HTMLElement or string expected");
547
- }
548
-
549
- this.setOption("content", data);
550
- }
551
-
552
- /**
553
- * Sets the plain text content by processing the input data, which can be of various types, including Blob,
554
- * HTMLElement, string, or a valid URL. The method extracts and sets the raw text content into a predefined option.
555
- *
556
- * @param {Blob|HTMLElement|string} data - The input data to be processed. It can be a Blob object, an HTMLElement,
557
- * a plain string, or a string formatted as a valid URL. The method determines
558
- * the data type and processes it accordingly.
559
- * @return {void} - This method does not return any value. It processes the content and updates the relevant option
560
- * property.
561
- */
562
- setPlainText(data) {
563
- const mkPreSpan = (text) => {
564
- const pre = document.createElement("pre");
565
- pre.innerText = text;
566
- pre.setAttribute("part", "text");
567
- return pre.outerHTML;
568
- };
569
-
570
- if (data instanceof Blob) {
571
- blobToText(data)
572
- .then((text) => {
573
- const div = document.createElement("div");
574
- div.innerHTML = text;
575
- text = div.innerText;
576
-
577
- this.setOption("content", mkPreSpan(text));
578
- })
579
- .catch((error) => {
580
- this.dispatchEvent(
581
- new CustomEvent("viewer-error", { detail: error }),
582
- );
583
- throw new Error(error);
584
- });
585
-
586
- return;
587
- } else if (data instanceof HTMLElement) {
588
- data = data.outerText;
589
- } else if (isString(data)) {
590
- const div = document.createElement("div");
591
- div.innerHTML = data;
592
- data = div.innerText;
593
- } else if (isURL(data)) {
594
- getGlobal()
595
- .fetch(data)
596
- .then((response) => {
597
- return response.text();
598
- })
599
- .then((text) => {
600
- const div = document.createElement("div");
601
- div.innerHTML = text;
602
- text = div.innerText;
603
-
604
- this.setOption("content", mkPreSpan(text));
605
- })
606
- .catch((error) => {
607
- this.dispatchEvent(
608
- new CustomEvent("viewer-error", { detail: error }),
609
- );
610
- throw new Error(error);
611
- });
612
- } else {
613
- this.dispatchEvent(
614
- new CustomEvent("viewer-error", {
615
- detail: "HTMLElement or string expected",
616
- }),
617
- );
618
- throw new Error("HTMLElement or string expected");
619
- }
620
-
621
- this.setOption("content", mkPreSpan(data));
622
- }
623
-
624
- /**
625
- *
626
- * @return {Viewer}
627
- */
628
- [assembleMethodSymbol]() {
629
- super[assembleMethodSymbol]();
630
-
631
- initControlReferences.call(this);
632
- initEventHandler.call(this);
633
- }
634
-
635
- /**
636
- *
637
- * @return {string}
638
- */
639
- static getTag() {
640
- return "monster-viewer";
641
- }
642
-
643
- /**
644
- * @return {CSSStyleSheet[]}
645
- */
646
- static getCSSStyleSheet() {
647
- return [ViewerStyleSheet];
648
- }
323
+ );
324
+ }
325
+
326
+ /**
327
+ * Renders Markdown content using built-in or custom Markdown parser.
328
+ * Overrideable via `customRenderers['text/markdown']`.
329
+ *
330
+ * @param {string|Blob} data
331
+ */
332
+ setMarkdown(data) {
333
+ if (isBlob(data)) {
334
+ blobToText(data)
335
+ .then((markdownText) => {
336
+ try {
337
+ const html = MarkdownToHTML.convert(markdownText);
338
+ this.setHTML(html);
339
+ } catch (error) {
340
+ this.setPlainText(markdownText); // Fallback to plain text if conversion fails
341
+ this.dispatchEvent(
342
+ new CustomEvent("viewer-error", {detail: error}),
343
+ );
344
+ }
345
+ })
346
+ .catch((error) => {
347
+ this.dispatchEvent(
348
+ new CustomEvent("viewer-error", {detail: error}),
349
+ );
350
+ throw new Error(error);
351
+ });
352
+ return;
353
+ } else if (isURL(data)) {
354
+ getGlobal()
355
+ .fetch(data)
356
+ .then((response) => {
357
+ return response.text();
358
+ })
359
+ .then((markdownText) => {
360
+ try {
361
+ const html = MarkdownToHTML.convert(markdownText);
362
+ this.setHTML(html);
363
+ } catch (error) {
364
+ this.setPlainText(markdownText); // Fallback to plain text if conversion fails
365
+ this.dispatchEvent(
366
+ new CustomEvent("viewer-error", {detail: error}),
367
+ );
368
+ }
369
+ })
370
+ .catch((error) => {
371
+ this.dispatchEvent(
372
+ new CustomEvent("viewer-error", {detail: error}),
373
+ );
374
+ throw new Error(error);
375
+ });
376
+ return;
377
+ } else if (isString(data)) {
378
+ try {
379
+ const html = MarkdownToHTML.convert(data);
380
+ this.setHTML(html);
381
+ } catch (error) {
382
+ this.setPlainText(data); // Fallback to plain text if conversion fails
383
+ this.dispatchEvent(new CustomEvent("viewer-error", {detail: error}));
384
+ }
385
+ return;
386
+ }
387
+
388
+ this.dispatchEvent(
389
+ new CustomEvent("viewer-error", {detail: "Blob or string expected"}),
390
+ );
391
+ throw new Error("Blob or string expected");
392
+ }
393
+
394
+ /**
395
+ * Configures and embeds a PDF document into the application with customizable display settings.
396
+ *
397
+ * @param {Blob|URL|string} data The PDF data to be embedded. Can be provided as a Blob, URL, or base64 string.
398
+ * @param {boolean} [navigation=true] Determines whether the navigation pane is displayed in the PDF viewer.
399
+ * @param {boolean} [toolbar=true] Controls the visibility of the toolbar in the PDF viewer.
400
+ * @param {boolean} [scrollbar=false] Configures the display of the scrollbar in the PDF viewer.
401
+ * @return {void} This method returns nothing but sets the embedded PDF as the content.
402
+ */
403
+ setPDF(data, navigation = true, toolbar = true, scrollbar = false) {
404
+ const hashes =
405
+ "#toolbar=" +
406
+ (toolbar ? "1" : "0") +
407
+ "&navpanes=" +
408
+ (navigation ? "1" : "0") +
409
+ "&scrollbar=" +
410
+ (scrollbar ? "1" : "0");
411
+
412
+ let pdfURL = "";
413
+ if (isBlob(data)) {
414
+ pdfURL = URL.createObjectURL(data);
415
+ pdfURL += hashes;
416
+ } else if (isURL(data)) {
417
+ // check if the url already contains the hashes
418
+ if (data?.hash?.indexOf("#") === -1) {
419
+ pdfURL = data.toString() + hashes;
420
+ } else {
421
+ pdfURL = data.toString();
422
+ }
423
+ } else if (isString(data)) {
424
+ //URL.createObjectURL(data);
425
+ const blobObj = new Blob([atob(data)], {type: "application/pdf"});
426
+ const url = window.URL.createObjectURL(blobObj);
427
+
428
+ pdfURL = data;
429
+ } else {
430
+ this.dispatchEvent(
431
+ new CustomEvent("viewer-error", {detail: "Blob or URL expected"}),
432
+ );
433
+ throw new Error("Blob or URL expected");
434
+ }
435
+
436
+ const html =
437
+ '<object part="pdf" data="' +
438
+ pdfURL +
439
+ '" width="100%" height="100%" type="application/pdf"></object>';
440
+
441
+ this.setOption("content", html);
442
+ }
443
+
444
+ /**
445
+ * Sets the download functionality for the viewer.
446
+ * @param data
447
+ * @param filename
448
+ */
449
+ setDownload(data, filename = "download") {
450
+
451
+ const rawData = data;
452
+
453
+ if (isBlob(data)) {
454
+ data = URL.createObjectURL(data);
455
+ } else if (isURL(data)) {
456
+ // nothing to do
457
+ } else if (isString(data)) {
458
+ // nothing to do
459
+ } else {
460
+ this.dispatchEvent(
461
+ new CustomEvent("viewer-error", {detail: "Blob or URL expected"}),
462
+ );
463
+ throw new Error("Blob or URL expected");
464
+ }
465
+
466
+ const button = `<monster-button data-monster-role="download">` + this.getOption('labels.download') + `</monster-button>`;
467
+ this.setOption("content", button);
468
+
469
+ this.addEventListener("click", (event) => {
470
+
471
+ const element = findTargetElementFromEvent(event, "data-monster-role", "download");
472
+ if (element instanceof Button) {
473
+ const anchor = document.createElement("a");
474
+ anchor.href = URL.createObjectURL(new Blob([rawData]))
475
+ anchor.download = filename;
476
+ anchor.style.display = "none";
477
+ document.body.appendChild(anchor);
478
+ anchor.click();
479
+ document.body.removeChild(anchor);
480
+ }
481
+
482
+
483
+ })
484
+
485
+ }
486
+
487
+ /**
488
+ * Sets the content for displaying an email message.
489
+ * The data is expected to be an object with a structure containing
490
+ * 'to', 'from', 'subject', 'parts', and 'headers'.
491
+ * The parts are processed to display plain text and HTML in separate tabs,
492
+ * and attachments are listed.
493
+ *
494
+ * @param {object} emailData - The structured email data.
495
+ */
496
+ setMessage(emailData) {
497
+ if (!emailData || typeof emailData !== "object") {
498
+ this.dispatchEvent(
499
+ new CustomEvent("viewer-error", {
500
+ detail: "Invalid email data provided",
501
+ }),
502
+ );
503
+ return;
504
+ }
505
+
506
+ this.setOption(
507
+ "content",
508
+ '<monster-message-content part="message"></monster-message-content>',
509
+ );
510
+
511
+ setTimeout(() => {
512
+ const messageContent = this.shadowRoot.querySelector(
513
+ "monster-message-content",
514
+ );
515
+ if (!messageContent) {
516
+ this.dispatchEvent(
517
+ new CustomEvent("viewer-error", {
518
+ detail: "Message content element not found",
519
+ }),
520
+ );
521
+ return;
522
+ }
523
+
524
+ messageContent.setMessage(emailData);
525
+ }, 100);
526
+ }
527
+
528
+ /**
529
+ * Sets an image for the target by accepting a blob, URL, or string representation of the image.
530
+ *
531
+ * @param {(Blob|string)} data - The image data, which can be a Blob, a valid URL, or a string representation of the image.
532
+ * @return {void} Does not return a value.
533
+ */
534
+ setImage(data) {
535
+ if (isBlob(data)) {
536
+ data = URL.createObjectURL(data);
537
+ } else if (isURL(data)) {
538
+ // nothing to do
539
+ } else if (isString(data)) {
540
+ // nothing to do
541
+ } else {
542
+ this.dispatchEvent(
543
+ new CustomEvent("viewer-error", {detail: "Blob or URL expected"}),
544
+ );
545
+ throw new Error("Blob or URL expected");
546
+ }
547
+
548
+ this.setOption(
549
+ "content",
550
+ `<img style="max-width: 100%" src="${data}" alt="image" part="image" onerror="this.dispatchEvent(new CustomEvent('viewer-error', {detail: 'Image loading error'}));">`,
551
+ );
552
+ }
553
+
554
+ /**
555
+ *
556
+ * if the data is a string, it is interpreted as HTML.
557
+ * if the data is a URL, the HTML is loaded from the url and set as content.
558
+ * if the data is an HTMLElement, the outerHTML is used as content.
559
+ *
560
+ * @param {HTMLElement|URL|string|Blob} data
561
+ */
562
+ setHTML(data) {
563
+ if (data instanceof Blob) {
564
+ blobToText(data)
565
+ .then((html) => {
566
+ this.setOption("content", html);
567
+ })
568
+ .catch((error) => {
569
+ this.dispatchEvent(
570
+ new CustomEvent("viewer-error", {detail: error}),
571
+ );
572
+ throw new Error(error);
573
+ });
574
+
575
+ return;
576
+ } else if (data instanceof HTMLElement) {
577
+ data = data.outerHTML;
578
+ } else if (isString(data)) {
579
+ // nothing to do
580
+ } else if (isURL(data)) {
581
+ // fetch element
582
+ getGlobal()
583
+ .fetch(data)
584
+ .then((response) => {
585
+ return response.text();
586
+ })
587
+ .then((html) => {
588
+ this.setOption("content", html);
589
+ })
590
+ .catch((error) => {
591
+ this.dispatchEvent(
592
+ new CustomEvent("viewer-error", {detail: error}),
593
+ );
594
+ throw new Error(error);
595
+ });
596
+ } else {
597
+ this.dispatchEvent(
598
+ new CustomEvent("viewer-error", {
599
+ detail: "HTMLElement or string expected",
600
+ }),
601
+ );
602
+ throw new Error("HTMLElement or string expected");
603
+ }
604
+
605
+ this.setOption("content", data);
606
+ }
607
+
608
+ /**
609
+ * Sets the plain text content by processing the input data, which can be of various types, including Blob,
610
+ * HTMLElement, string, or a valid URL. The method extracts and sets the raw text content into a predefined option.
611
+ *
612
+ * @param {Blob|HTMLElement|string} data - The input data to be processed. It can be a Blob object, an HTMLElement,
613
+ * a plain string, or a string formatted as a valid URL. The method determines
614
+ * the data type and processes it accordingly.
615
+ * @return {void} - This method does not return any value. It processes the content and updates the relevant option
616
+ * property.
617
+ */
618
+ setPlainText(data) {
619
+ const mkPreSpan = (text) => {
620
+ const pre = document.createElement("pre");
621
+ pre.innerText = text;
622
+ pre.setAttribute("part", "text");
623
+ return pre.outerHTML;
624
+ };
625
+
626
+ if (data instanceof Blob) {
627
+ blobToText(data)
628
+ .then((text) => {
629
+ const div = document.createElement("div");
630
+ div.innerHTML = text;
631
+ text = div.innerText;
632
+
633
+ this.setOption("content", mkPreSpan(text));
634
+ })
635
+ .catch((error) => {
636
+ this.dispatchEvent(
637
+ new CustomEvent("viewer-error", {detail: error}),
638
+ );
639
+ throw new Error(error);
640
+ });
641
+
642
+ return;
643
+ } else if (data instanceof HTMLElement) {
644
+ data = data.outerText;
645
+ } else if (isString(data)) {
646
+ const div = document.createElement("div");
647
+ div.innerHTML = data;
648
+ data = div.innerText;
649
+ } else if (isURL(data)) {
650
+ getGlobal()
651
+ .fetch(data)
652
+ .then((response) => {
653
+ return response.text();
654
+ })
655
+ .then((text) => {
656
+ const div = document.createElement("div");
657
+ div.innerHTML = text;
658
+ text = div.innerText;
659
+
660
+ this.setOption("content", mkPreSpan(text));
661
+ })
662
+ .catch((error) => {
663
+ this.dispatchEvent(
664
+ new CustomEvent("viewer-error", {detail: error}),
665
+ );
666
+ throw new Error(error);
667
+ });
668
+ } else {
669
+ this.dispatchEvent(
670
+ new CustomEvent("viewer-error", {
671
+ detail: "HTMLElement or string expected",
672
+ }),
673
+ );
674
+ throw new Error("HTMLElement or string expected");
675
+ }
676
+
677
+ this.setOption("content", mkPreSpan(data));
678
+ }
679
+
680
+ /**
681
+ *
682
+ * @return {Viewer}
683
+ */
684
+ [assembleMethodSymbol]() {
685
+ super[assembleMethodSymbol]();
686
+
687
+ initControlReferences.call(this);
688
+ initEventHandler.call(this);
689
+ }
690
+
691
+ /**
692
+ *
693
+ * @return {string}
694
+ */
695
+ static getTag() {
696
+ return "monster-viewer";
697
+ }
698
+
699
+ /**
700
+ * @return {CSSStyleSheet[]}
701
+ */
702
+ static getCSSStyleSheet() {
703
+ return [ViewerStyleSheet];
704
+ }
649
705
  }
650
706
 
651
707
  /**
@@ -654,12 +710,12 @@ class Viewer extends CustomElement {
654
710
  * @return {boolean}
655
711
  */
656
712
  function isURL(variable) {
657
- try {
658
- new URL(variable);
659
- return true;
660
- } catch (error) {
661
- return false;
662
- }
713
+ try {
714
+ new URL(variable);
715
+ return true;
716
+ } catch (error) {
717
+ return false;
718
+ }
663
719
  }
664
720
 
665
721
  /**
@@ -668,7 +724,7 @@ function isURL(variable) {
668
724
  * @return {boolean}
669
725
  */
670
726
  function isBlob(variable) {
671
- return variable instanceof Blob;
727
+ return variable instanceof Blob;
672
728
  }
673
729
 
674
730
  /**
@@ -677,12 +733,12 @@ function isBlob(variable) {
677
733
  * @return {Promise<unknown>}
678
734
  */
679
735
  function blobToText(blob) {
680
- return new Promise((resolve, reject) => {
681
- const reader = new FileReader();
682
- reader.onloadend = () => resolve(reader.result);
683
- reader.onerror = reject;
684
- reader.readAsText(blob);
685
- });
736
+ return new Promise((resolve, reject) => {
737
+ const reader = new FileReader();
738
+ reader.onloadend = () => resolve(reader.result);
739
+ reader.onerror = reject;
740
+ reader.readAsText(blob);
741
+ });
686
742
  }
687
743
 
688
744
  /**
@@ -691,18 +747,122 @@ function blobToText(blob) {
691
747
  * @throws {Error} no shadow-root is defined
692
748
  */
693
749
  function initControlReferences() {
694
- if (!this.shadowRoot) {
695
- throw new Error("no shadow-root is defined");
696
- }
750
+ if (!this.shadowRoot) {
751
+ throw new Error("no shadow-root is defined");
752
+ }
697
753
 
698
- this[viewerElementSymbol] = this.shadowRoot.getElementById("viewer");
754
+ this[viewerElementSymbol] = this.shadowRoot.getElementById("viewer");
699
755
  }
700
756
 
701
757
  /**
702
758
  * @private
703
759
  */
704
760
  function initEventHandler() {
705
- return this;
761
+ return this;
762
+ }
763
+
764
+ function getLabels() {
765
+ switch (getLocaleOfDocument().language) {
766
+ case "de": // German
767
+ return {
768
+ download: "Herunterladen",
769
+ };
770
+
771
+ case "es": // Spanish
772
+ return {
773
+ download: "Descargar",
774
+ };
775
+
776
+ case "zh": // Mandarin
777
+ return {
778
+ download: "下载",
779
+ };
780
+
781
+ case "hi": // Hindi
782
+ return {
783
+ download: "下载",
784
+ };
785
+
786
+ case "bn": // Bengali
787
+ return {
788
+ download: "ডাউনলোড",
789
+ };
790
+
791
+ case "pt": // Portuguese
792
+ return {
793
+ download: "Baixar",
794
+ };
795
+
796
+ case "ru": // Russian
797
+ return {
798
+ download: "Скачать",
799
+ };
800
+
801
+ case "ja": // Japanese
802
+ return {
803
+ download: "ダウンロード",
804
+ };
805
+
806
+ case "pa": // Western Punjabi
807
+ return {
808
+ download: "ਡਾਊਨਲੋਡ",
809
+ };
810
+
811
+ case "mr": // Marathi
812
+ return {
813
+ download: "डाउनलोड",
814
+ };
815
+
816
+ case "fr": // French
817
+ return {
818
+ download: "Télécharger",
819
+ };
820
+
821
+ case "it": // Italian
822
+ return {
823
+ download: "Scarica",
824
+ };
825
+
826
+ case "nl": // Dutch
827
+ return {
828
+ download: "Downloaden",
829
+ };
830
+
831
+ case "sv": // Swedish
832
+ return {
833
+ download: "Ladda ner",
834
+ };
835
+
836
+ case "pl": // Polish
837
+ return {
838
+ download: "Ściągnij",
839
+ };
840
+
841
+ case "da": // Danish
842
+ return {
843
+ download: "Lad ned",
844
+ };
845
+
846
+ case "fi": // Finnish
847
+ return {
848
+ download: "Lataa",
849
+ };
850
+
851
+ case "no": // Norwegian
852
+ return {
853
+ download: "Laste ned",
854
+ };
855
+
856
+ case "cs": // Czech
857
+ return {
858
+ download: "Stáhnout",
859
+ };
860
+
861
+ default:
862
+ return {
863
+ download: "Download",
864
+ };
865
+ }
706
866
  }
707
867
 
708
868
  /**
@@ -710,8 +870,8 @@ function initEventHandler() {
710
870
  * @return {string}
711
871
  */
712
872
  function getTemplate() {
713
- // language=HTML
714
- return `
873
+ // language=HTML
874
+ return `
715
875
  <div id="viewer" data-monster-role="viewer" part="viewer"
716
876
  data-monster-replace="path:content"
717
877
  data-monster-attributes="class path:classes.viewer">