@schukai/monster 4.22.0 → 4.22.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/CHANGELOG.md +16 -0
- package/package.json +1 -1
- package/source/components/content/viewer.mjs +526 -355
package/CHANGELOG.md
CHANGED
@@ -2,6 +2,22 @@
|
|
2
2
|
|
3
3
|
|
4
4
|
|
5
|
+
## [4.22.2] - 2025-06-22
|
6
|
+
|
7
|
+
### Bug Fixes
|
8
|
+
|
9
|
+
- Add audio and video support to the viewer [#309](https://gitlab.schukai.com/oss/libraries/javascript/monster/issues/309)
|
10
|
+
|
11
|
+
|
12
|
+
|
13
|
+
## [4.22.1] - 2025-06-22
|
14
|
+
|
15
|
+
### Bug Fixes
|
16
|
+
|
17
|
+
- Refactor Viewer component and clean up markdown-related content [#309](https://gitlab.schukai.com/oss/libraries/javascript/monster/issues/309)
|
18
|
+
|
19
|
+
|
20
|
+
|
5
21
|
## [4.22.0] - 2025-06-22
|
6
22
|
|
7
23
|
### Add Features
|
package/package.json
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"author":"schukai GmbH","dependencies":{"@floating-ui/dom":"^1.7.1","@popperjs/core":"^2.11.8"},"description":"Monster is a simple library for creating fast, robust and lightweight websites.","homepage":"https://monsterjs.org/","keywords":["framework","web","dom","css","sass","mobile-first","app","front-end","templates","schukai","core","shopcloud","alvine","monster","buildmap","stack","observer","observable","uuid","node","nodelist","css-in-js","logger","log","theme"],"license":"AGPL 3.0","main":"source/monster.mjs","module":"source/monster.mjs","name":"@schukai/monster","repository":{"type":"git","url":"https://gitlab.schukai.com/oss/libraries/javascript/monster.git"},"type":"module","version":"4.22.
|
1
|
+
{"author":"schukai GmbH","dependencies":{"@floating-ui/dom":"^1.7.1","@popperjs/core":"^2.11.8"},"description":"Monster is a simple library for creating fast, robust and lightweight websites.","homepage":"https://monsterjs.org/","keywords":["framework","web","dom","css","sass","mobile-first","app","front-end","templates","schukai","core","shopcloud","alvine","monster","buildmap","stack","observer","observable","uuid","node","nodelist","css-in-js","logger","log","theme"],"license":"AGPL 3.0","main":"source/monster.mjs","module":"source/monster.mjs","name":"@schukai/monster","repository":{"type":"git","url":"https://gitlab.schukai.com/oss/libraries/javascript/monster.git"},"type":"module","version":"4.22.2"}
|
@@ -13,19 +13,19 @@
|
|
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 {
|
26
|
-
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
|
+
import {MarkdownToHTML} from "../../text/markdown-parser.mjs";
|
27
27
|
|
28
|
-
export {
|
28
|
+
export {Viewer};
|
29
29
|
|
30
30
|
/**
|
31
31
|
* @private
|
@@ -46,331 +46,502 @@ const viewerElementSymbol = Symbol("viewerElement");
|
|
46
46
|
* @summary A simple viewer component for PDF, HTML, and images.
|
47
47
|
*/
|
48
48
|
class Viewer extends CustomElement {
|
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
|
-
|
320
|
-
|
321
|
-
|
322
|
-
|
323
|
-
|
324
|
-
|
325
|
-
|
326
|
-
|
327
|
-
|
328
|
-
|
329
|
-
|
330
|
-
|
331
|
-
|
332
|
-
|
333
|
-
|
334
|
-
|
335
|
-
|
336
|
-
|
337
|
-
|
338
|
-
|
339
|
-
|
340
|
-
|
341
|
-
|
342
|
-
|
343
|
-
|
344
|
-
|
345
|
-
|
346
|
-
|
347
|
-
|
348
|
-
|
349
|
-
|
350
|
-
|
351
|
-
|
352
|
-
|
353
|
-
|
354
|
-
|
355
|
-
|
356
|
-
|
357
|
-
|
358
|
-
|
359
|
-
|
360
|
-
|
361
|
-
|
362
|
-
|
363
|
-
|
364
|
-
|
365
|
-
|
366
|
-
|
367
|
-
|
368
|
-
|
369
|
-
|
370
|
-
|
371
|
-
|
372
|
-
|
373
|
-
|
49
|
+
/**
|
50
|
+
* This method is called by the `instanceof` operator.
|
51
|
+
* @return {symbol}
|
52
|
+
*/
|
53
|
+
static get [instanceSymbol]() {
|
54
|
+
return Symbol.for("@schukai/monster/components/content/viewer@@instance");
|
55
|
+
}
|
56
|
+
|
57
|
+
/**
|
58
|
+
* To set the options via the HTML tag, the attribute `data-monster-options` must be used.
|
59
|
+
* @see {@link https://monsterjs.org/en/doc/#configurate-a-monster-control}
|
60
|
+
*
|
61
|
+
* The individual configuration values can be found in the table.
|
62
|
+
*
|
63
|
+
* @property {Object} templates Template definitions
|
64
|
+
* @property {string} templates.main Main template
|
65
|
+
* @property {string} content Content to be displayed in the viewer
|
66
|
+
* @property {Object} classes Css classes
|
67
|
+
* @property {string} classes.viewer Css class for the viewer
|
68
|
+
* @property {Object} renderers Renderers for different media types
|
69
|
+
* @property {function} renderers.image Function to render image content
|
70
|
+
* @property {function} renderers.html Function to render HTML content
|
71
|
+
* @property {function} renderers.pdf Function to render PDF content
|
72
|
+
* @property {function} renderers.plaintext Function to render plain text content
|
73
|
+
* @property {function} renderers.markdown Function to render Markdown content
|
74
|
+
*/
|
75
|
+
get defaults() {
|
76
|
+
return Object.assign({}, super.defaults, {
|
77
|
+
templates: {
|
78
|
+
main: getTemplate(),
|
79
|
+
},
|
80
|
+
content: "<slot></slot>",
|
81
|
+
classes: {
|
82
|
+
viewer: "",
|
83
|
+
},
|
84
|
+
renderers: {
|
85
|
+
image: this.setImage,
|
86
|
+
html: this.setHTML,
|
87
|
+
pdf: this.setPDF,
|
88
|
+
plaintext: this.setPlainText,
|
89
|
+
markdown: this.setMarkdown,
|
90
|
+
audio: this.setAudio,
|
91
|
+
video: this.setVideo,
|
92
|
+
}
|
93
|
+
});
|
94
|
+
}
|
95
|
+
|
96
|
+
/**
|
97
|
+
* Sets the content of an element based on the provided content and media type.
|
98
|
+
*
|
99
|
+
* @param {string} content - The content to be set.
|
100
|
+
* @param {string} [mediaType="text/plain"] - The media type of the content. Defaults to "text/plain" if not specified.
|
101
|
+
* @return {void} This method does not return a value.
|
102
|
+
* @throws {Error} Throws an error if shadowRoot is not defined.
|
103
|
+
*/
|
104
|
+
setContent(content, mediaType = "text/plain") {
|
105
|
+
if (!this.shadowRoot) {
|
106
|
+
throw new Error("no shadow-root is defined");
|
107
|
+
}
|
108
|
+
|
109
|
+
const renderers = this.getOption("renderers");
|
110
|
+
|
111
|
+
const isDataURL = (value) => {
|
112
|
+
return (typeof value === "string" && value.startsWith("data:")) ||
|
113
|
+
(value instanceof URL && value.protocol === "data:");
|
114
|
+
};
|
115
|
+
|
116
|
+
if (isDataURL(content)) {
|
117
|
+
try {
|
118
|
+
const dataUrl = content.toString();
|
119
|
+
const [header] = dataUrl.split(",");
|
120
|
+
const [typeSegment] = header.split(";");
|
121
|
+
mediaType = typeSegment.replace("data:", "") || "text/plain";
|
122
|
+
} catch (error) {
|
123
|
+
this.dispatchEvent(new CustomEvent("viewer-error", {detail: "Invalid data URL format"}));
|
124
|
+
return;
|
125
|
+
}
|
126
|
+
}
|
127
|
+
|
128
|
+
if (mediaType === undefined || mediaType === null || mediaType === "") {
|
129
|
+
mediaType = "text/plain";
|
130
|
+
}
|
131
|
+
|
132
|
+
let mediaTypeObject;
|
133
|
+
|
134
|
+
try {
|
135
|
+
mediaTypeObject = new parseMediaType(mediaType);
|
136
|
+
if (!(mediaTypeObject instanceof MediaType)) {
|
137
|
+
this.dispatchEvent(new CustomEvent("viewer-error", {detail: "Invalid MediaType"}));
|
138
|
+
return;
|
139
|
+
}
|
140
|
+
|
141
|
+
} catch (error) {
|
142
|
+
this.dispatchEvent(new CustomEvent("viewer-error", {detail: error}));
|
143
|
+
return
|
144
|
+
}
|
145
|
+
|
146
|
+
const checkRenderer = (renderer, contentType) => {
|
147
|
+
if (renderers && typeof renderers[renderer] === "function") {
|
148
|
+
return true;
|
149
|
+
} else {
|
150
|
+
this.dispatchEvent(new CustomEvent("viewer-error", {detail: `Renderer for ${contentType} not found`}));
|
151
|
+
return false;
|
152
|
+
}
|
153
|
+
}
|
154
|
+
|
155
|
+
switch (mediaTypeObject.type) {
|
156
|
+
case "text":
|
157
|
+
switch (mediaTypeObject.subtype) {
|
158
|
+
case "html":
|
159
|
+
if (checkRenderer("html", mediaTypeObject.toString())) {
|
160
|
+
renderers.html.call(this, content);
|
161
|
+
}
|
162
|
+
break;
|
163
|
+
case "plain":
|
164
|
+
if (checkRenderer("plaintext", mediaTypeObject.toString())) {
|
165
|
+
renderers.plaintext.call(this, content);
|
166
|
+
}
|
167
|
+
break;
|
168
|
+
case "markdown":
|
169
|
+
if (checkRenderer("markdown", mediaTypeObject.toString())) {
|
170
|
+
this.setMarkdown(content);
|
171
|
+
}
|
172
|
+
break;
|
173
|
+
default:
|
174
|
+
if (checkRenderer("plaintext", mediaTypeObject.toString())) {
|
175
|
+
renderers.plaintext.call(this, content);
|
176
|
+
}
|
177
|
+
break;
|
178
|
+
}
|
179
|
+
break;
|
180
|
+
|
181
|
+
case "application":
|
182
|
+
switch (mediaTypeObject.subtype) {
|
183
|
+
case "pdf":
|
184
|
+
if (checkRenderer("pdf", mediaTypeObject.toString())) {
|
185
|
+
renderers.pdf.call(this, content);
|
186
|
+
}
|
187
|
+
break;
|
188
|
+
default:
|
189
|
+
this.setOption("content", content);
|
190
|
+
break;
|
191
|
+
}
|
192
|
+
break;
|
193
|
+
|
194
|
+
case "audio":
|
195
|
+
if (checkRenderer(mediaTypeObject.type, mediaTypeObject.toString())) {
|
196
|
+
renderers[mediaTypeObject.type].call(this, content);
|
197
|
+
}
|
198
|
+
break;
|
199
|
+
|
200
|
+
case "video":
|
201
|
+
if (checkRenderer(mediaTypeObject.type, mediaTypeObject.toString())) {
|
202
|
+
renderers[mediaTypeObject.type].call(this, content);
|
203
|
+
}
|
204
|
+
break;
|
205
|
+
|
206
|
+
case "image":
|
207
|
+
if (checkRenderer("image", mediaTypeObject.toString())) {
|
208
|
+
renderers.image.call(this, content);
|
209
|
+
}
|
210
|
+
break;
|
211
|
+
|
212
|
+
default:
|
213
|
+
this.setOption("content", content);
|
214
|
+
this.dispatchEvent(new CustomEvent("viewer-error", {detail: `Unsupported media type: ${mediaTypeObject.toString()}`})); // Notify about unsupported media type
|
215
|
+
return;
|
216
|
+
}
|
217
|
+
}
|
218
|
+
|
219
|
+
/**
|
220
|
+
* Sets the audio content for the viewer. Accepts a Blob, URL, or string and processes it
|
221
|
+
* to configure audio playback within the viewer. Throws an error if the input type is invalid.
|
222
|
+
*
|
223
|
+
* @param {Blob|string} data - The audio content. This can be a Blob, a URL, or a string.
|
224
|
+
* @return {void} No return value.
|
225
|
+
*/
|
226
|
+
setAudio(data) {
|
227
|
+
if (isBlob(data)) {
|
228
|
+
data = URL.createObjectURL(data);
|
229
|
+
} else if (isURL(data)) {
|
230
|
+
// nothing to do
|
231
|
+
} else if (isString(data)) {
|
232
|
+
// nothing to do
|
233
|
+
} else {
|
234
|
+
this.dispatchEvent(new CustomEvent("viewer-error", {detail: "Blob or URL expected"}));
|
235
|
+
throw new Error("Blob or URL expected");
|
236
|
+
}
|
237
|
+
|
238
|
+
this.setOption(
|
239
|
+
"content",
|
240
|
+
`
|
241
|
+
<audio controls part="audio">
|
242
|
+
<source src="${data}">
|
243
|
+
</audio>`
|
244
|
+
);
|
245
|
+
}
|
246
|
+
|
247
|
+
/**
|
248
|
+
* Sets the video content for the viewer. The method accepts a Blob, URL, or string,
|
249
|
+
* verifies its type, and updates the viewer's content accordingly.
|
250
|
+
*
|
251
|
+
* @param {Blob|string} data - The video data to set. It can be a Blob, URL, or string.
|
252
|
+
* @return {void} This method does not return a value. It updates the viewer's state.
|
253
|
+
* @throws {Error} Throws an error if the provided data is not a Blob or URL.
|
254
|
+
*/
|
255
|
+
setVideo(data) {
|
256
|
+
if (isBlob(data)) {
|
257
|
+
data = URL.createObjectURL(data);
|
258
|
+
} else if (isURL(data)) {
|
259
|
+
// nothing to do
|
260
|
+
} else if (isString(data)) {
|
261
|
+
// nothing to do
|
262
|
+
} else {
|
263
|
+
this.dispatchEvent(new CustomEvent("viewer-error", {detail: "Blob or URL expected"}));
|
264
|
+
throw new Error("Blob or URL expected");
|
265
|
+
}
|
266
|
+
|
267
|
+
this.setOption(
|
268
|
+
"content",
|
269
|
+
`
|
270
|
+
<video controls part="video">
|
271
|
+
<source src="${data}">
|
272
|
+
</video>`
|
273
|
+
);
|
274
|
+
}
|
275
|
+
|
276
|
+
/**
|
277
|
+
* Renders Markdown content using built-in or custom Markdown parser.
|
278
|
+
* Overrideable via `customRenderers['text/markdown']`.
|
279
|
+
*
|
280
|
+
* @param {string|Blob} data
|
281
|
+
*/
|
282
|
+
setMarkdown(data) {
|
283
|
+
|
284
|
+
if (isBlob(data)) {
|
285
|
+
blobToText(data)
|
286
|
+
.then((markdownText) => {
|
287
|
+
try {
|
288
|
+
const html = MarkdownToHTML.convert(markdownText);
|
289
|
+
this.setHTML(html);
|
290
|
+
} catch (error) {
|
291
|
+
this.setPlainText(markdownText); // Fallback to plain text if conversion fails
|
292
|
+
this.dispatchEvent(new CustomEvent("viewer-error", {detail: error}));
|
293
|
+
}
|
294
|
+
})
|
295
|
+
.catch((error) => {
|
296
|
+
this.dispatchEvent(new CustomEvent("viewer-error", {detail: error}));
|
297
|
+
throw new Error(error);
|
298
|
+
});
|
299
|
+
return;
|
300
|
+
} else if (isURL(data)) {
|
301
|
+
getGlobal()
|
302
|
+
.fetch(data)
|
303
|
+
.then((response) => {
|
304
|
+
return response.text();
|
305
|
+
})
|
306
|
+
.then((markdownText) => {
|
307
|
+
try {
|
308
|
+
const html = MarkdownToHTML.convert(markdownText);
|
309
|
+
this.setHTML(html);
|
310
|
+
} catch (error) {
|
311
|
+
this.setPlainText(markdownText); // Fallback to plain text if conversion fails
|
312
|
+
this.dispatchEvent(new CustomEvent("viewer-error", {detail: error}));
|
313
|
+
}
|
314
|
+
})
|
315
|
+
.catch((error) => {
|
316
|
+
this.dispatchEvent(new CustomEvent("viewer-error", {detail: error}));
|
317
|
+
throw new Error(error);
|
318
|
+
});
|
319
|
+
return;
|
320
|
+
} else if (isString(data)) {
|
321
|
+
try {
|
322
|
+
const html = MarkdownToHTML.convert(data);
|
323
|
+
this.setHTML(html);
|
324
|
+
} catch (error) {
|
325
|
+
this.setPlainText(data); // Fallback to plain text if conversion fails
|
326
|
+
this.dispatchEvent(new CustomEvent("viewer-error", {detail: error}));
|
327
|
+
}
|
328
|
+
return;
|
329
|
+
}
|
330
|
+
|
331
|
+
this.dispatchEvent(new CustomEvent("viewer-error", {detail: "Blob or string expected"}));
|
332
|
+
throw new Error("Blob or string expected");
|
333
|
+
|
334
|
+
}
|
335
|
+
|
336
|
+
/**
|
337
|
+
* Configures and embeds a PDF document into the application with customizable display settings.
|
338
|
+
*
|
339
|
+
* @param {Blob|URL|string} data The PDF data to be embedded. Can be provided as a Blob, URL, or base64 string.
|
340
|
+
* @param {boolean} [navigation=true] Determines whether the navigation pane is displayed in the PDF viewer.
|
341
|
+
* @param {boolean} [toolbar=true] Controls the visibility of the toolbar in the PDF viewer.
|
342
|
+
* @param {boolean} [scrollbar=false] Configures the display of the scrollbar in the PDF viewer.
|
343
|
+
* @return {void} This method returns nothing but sets the embedded PDF as the content.
|
344
|
+
*/
|
345
|
+
setPDF(data, navigation = true, toolbar = true, scrollbar = false) {
|
346
|
+
const hashes =
|
347
|
+
"#toolbar=" +
|
348
|
+
(toolbar ? "1" : "0") +
|
349
|
+
"&navpanes=" +
|
350
|
+
(navigation ? "1" : "0") +
|
351
|
+
"&scrollbar=" +
|
352
|
+
(scrollbar ? "1" : "0");
|
353
|
+
|
354
|
+
let pdfURL = "";
|
355
|
+
if (isBlob(data)) {
|
356
|
+
pdfURL = URL.createObjectURL(data);
|
357
|
+
pdfURL += hashes;
|
358
|
+
} else if (isURL(data)) {
|
359
|
+
// check if the url already contains the hashes
|
360
|
+
if (data?.hash?.indexOf("#") === -1) {
|
361
|
+
pdfURL = data.toString() + hashes;
|
362
|
+
} else {
|
363
|
+
pdfURL = data.toString();
|
364
|
+
}
|
365
|
+
} else if (isString(data)) {
|
366
|
+
//URL.createObjectURL(data);
|
367
|
+
const blobObj = new Blob([atob(data)], {type: "application/pdf"});
|
368
|
+
const url = window.URL.createObjectURL(blobObj);
|
369
|
+
|
370
|
+
pdfURL = data;
|
371
|
+
} else {
|
372
|
+
this.dispatchEvent(new CustomEvent("viewer-error", {detail: "Blob or URL expected"}));
|
373
|
+
throw new Error("Blob or URL expected");
|
374
|
+
}
|
375
|
+
|
376
|
+
const html =
|
377
|
+
'<object part="pdf" data="' +
|
378
|
+
pdfURL +
|
379
|
+
'" width="100%" height="100%" type="application/pdf"></object>';
|
380
|
+
|
381
|
+
this.setOption("content", html);
|
382
|
+
}
|
383
|
+
|
384
|
+
/**
|
385
|
+
* Sets an image for the target by accepting a blob, URL, or string representation of the image.
|
386
|
+
*
|
387
|
+
* @param {(Blob|string)} data - The image data, which can be a Blob, a valid URL, or a string representation of the image.
|
388
|
+
* @return {void} Does not return a value.
|
389
|
+
*/
|
390
|
+
setImage(data) {
|
391
|
+
if (isBlob(data)) {
|
392
|
+
data = URL.createObjectURL(data);
|
393
|
+
} else if (isURL(data)) {
|
394
|
+
// nothing to do
|
395
|
+
} else if (isString(data)) {
|
396
|
+
// nothing to do
|
397
|
+
} else {
|
398
|
+
this.dispatchEvent(new CustomEvent("viewer-error", {detail: "Blob or URL expected"}));
|
399
|
+
throw new Error("Blob or URL expected");
|
400
|
+
}
|
401
|
+
|
402
|
+
const onloaderror = `onerror="this.dispatchEvent(new CustomEvent('viewer-error', {detail: 'Image loading error'}));"`;
|
403
|
+
|
404
|
+
this.setOption(
|
405
|
+
"content",
|
406
|
+
`<img style="max-width: 100%" src="${data}" alt="image" part="image" ${onloaderror}>`
|
407
|
+
);
|
408
|
+
}
|
409
|
+
|
410
|
+
/**
|
411
|
+
*
|
412
|
+
* if the data is a string, it is interpreted as HTML.
|
413
|
+
* if the data is a URL, the HTML is loaded from the url and set as content.
|
414
|
+
* if the data is an HTMLElement, the outerHTML is used as content.
|
415
|
+
*
|
416
|
+
* @param {HTMLElement|URL|string|Blob} data
|
417
|
+
*/
|
418
|
+
setHTML(data) {
|
419
|
+
if (data instanceof Blob) {
|
420
|
+
blobToText(data)
|
421
|
+
.then((html) => {
|
422
|
+
this.setOption("content", html);
|
423
|
+
})
|
424
|
+
.catch((error) => {
|
425
|
+
this.dispatchEvent(new CustomEvent("viewer-error", {detail: error}));
|
426
|
+
throw new Error(error);
|
427
|
+
});
|
428
|
+
|
429
|
+
return;
|
430
|
+
} else if (data instanceof HTMLElement) {
|
431
|
+
data = data.outerHTML;
|
432
|
+
} else if (isString(data)) {
|
433
|
+
// nothing to do
|
434
|
+
} else if (isURL(data)) {
|
435
|
+
// fetch element
|
436
|
+
getGlobal()
|
437
|
+
.fetch(data)
|
438
|
+
.then((response) => {
|
439
|
+
return response.text();
|
440
|
+
})
|
441
|
+
.then((html) => {
|
442
|
+
this.setOption("content", html);
|
443
|
+
})
|
444
|
+
.catch((error) => {
|
445
|
+
this.dispatchEvent(new CustomEvent("viewer-error", {detail: error}));
|
446
|
+
throw new Error(error);
|
447
|
+
});
|
448
|
+
} else {
|
449
|
+
this.dispatchEvent(new CustomEvent("viewer-error", {detail: "HTMLElement or string expected"}));
|
450
|
+
throw new Error("HTMLElement or string expected");
|
451
|
+
}
|
452
|
+
|
453
|
+
this.setOption("content", data);
|
454
|
+
}
|
455
|
+
|
456
|
+
/**
|
457
|
+
* Sets the plain text content by processing the input data, which can be of various types, including Blob,
|
458
|
+
* HTMLElement, string, or a valid URL. The method extracts and sets the raw text content into a predefined option.
|
459
|
+
*
|
460
|
+
* @param {Blob|HTMLElement|string} data - The input data to be processed. It can be a Blob object, an HTMLElement,
|
461
|
+
* a plain string, or a string formatted as a valid URL. The method determines
|
462
|
+
* the data type and processes it accordingly.
|
463
|
+
* @return {void} - This method does not return any value. It processes the content and updates the relevant option
|
464
|
+
* property.
|
465
|
+
*/
|
466
|
+
setPlainText(data) {
|
467
|
+
const mkPreSpan = (text) => {
|
468
|
+
const pre = document.createElement("pre");
|
469
|
+
pre.innerText = text;
|
470
|
+
pre.setAttribute("part", "text");
|
471
|
+
return pre.outerHTML;
|
472
|
+
};
|
473
|
+
|
474
|
+
if (data instanceof Blob) {
|
475
|
+
blobToText(data)
|
476
|
+
.then((text) => {
|
477
|
+
const div = document.createElement("div");
|
478
|
+
div.innerHTML = test;
|
479
|
+
text = div.innerText;
|
480
|
+
|
481
|
+
this.setOption("content", mkPreSpan(text));
|
482
|
+
})
|
483
|
+
.catch((error) => {
|
484
|
+
this.dispatchEvent(new CustomEvent("viewer-error", {detail: error}));
|
485
|
+
throw new Error(error);
|
486
|
+
});
|
487
|
+
|
488
|
+
return;
|
489
|
+
} else if (data instanceof HTMLElement) {
|
490
|
+
data = data.outerText;
|
491
|
+
} else if (isString(data)) {
|
492
|
+
const div = document.createElement("div");
|
493
|
+
div.innerHTML = data;
|
494
|
+
data = div.innerText;
|
495
|
+
} else if (isURL(data)) {
|
496
|
+
getGlobal()
|
497
|
+
.fetch(data)
|
498
|
+
.then((response) => {
|
499
|
+
return response.text();
|
500
|
+
})
|
501
|
+
.then((text) => {
|
502
|
+
const div = document.createElement("div");
|
503
|
+
div.innerHTML = text;
|
504
|
+
text = div.innerText;
|
505
|
+
|
506
|
+
this.setOption("content", mkPreSpan(text));
|
507
|
+
})
|
508
|
+
.catch((error) => {
|
509
|
+
this.dispatchEvent(new CustomEvent("viewer-error", {detail: error}));
|
510
|
+
throw new Error(error);
|
511
|
+
});
|
512
|
+
} else {
|
513
|
+
this.dispatchEvent(new CustomEvent("viewer-error", {detail: "HTMLElement or string expected"}));
|
514
|
+
throw new Error("HTMLElement or string expected");
|
515
|
+
}
|
516
|
+
|
517
|
+
this.setOption("content", mkPreSpan(data));
|
518
|
+
}
|
519
|
+
|
520
|
+
/**
|
521
|
+
*
|
522
|
+
* @return {Viewer}
|
523
|
+
*/
|
524
|
+
[assembleMethodSymbol]() {
|
525
|
+
super[assembleMethodSymbol]();
|
526
|
+
|
527
|
+
initControlReferences.call(this);
|
528
|
+
initEventHandler.call(this);
|
529
|
+
}
|
530
|
+
|
531
|
+
/**
|
532
|
+
*
|
533
|
+
* @return {string}
|
534
|
+
*/
|
535
|
+
static getTag() {
|
536
|
+
return "monster-viewer";
|
537
|
+
}
|
538
|
+
|
539
|
+
/**
|
540
|
+
* @return {CSSStyleSheet[]}
|
541
|
+
*/
|
542
|
+
static getCSSStyleSheet() {
|
543
|
+
return [ViewerStyleSheet];
|
544
|
+
}
|
374
545
|
}
|
375
546
|
|
376
547
|
/**
|
@@ -379,12 +550,12 @@ class Viewer extends CustomElement {
|
|
379
550
|
* @return {boolean}
|
380
551
|
*/
|
381
552
|
function isURL(variable) {
|
382
|
-
|
383
|
-
|
384
|
-
|
385
|
-
|
386
|
-
|
387
|
-
|
553
|
+
try {
|
554
|
+
new URL(variable);
|
555
|
+
return true;
|
556
|
+
} catch (error) {
|
557
|
+
return false;
|
558
|
+
}
|
388
559
|
}
|
389
560
|
|
390
561
|
/**
|
@@ -393,7 +564,7 @@ function isURL(variable) {
|
|
393
564
|
* @return {boolean}
|
394
565
|
*/
|
395
566
|
function isBlob(variable) {
|
396
|
-
|
567
|
+
return variable instanceof Blob;
|
397
568
|
}
|
398
569
|
|
399
570
|
/**
|
@@ -402,12 +573,12 @@ function isBlob(variable) {
|
|
402
573
|
* @return {Promise<unknown>}
|
403
574
|
*/
|
404
575
|
function blobToText(blob) {
|
405
|
-
|
406
|
-
|
407
|
-
|
408
|
-
|
409
|
-
|
410
|
-
|
576
|
+
return new Promise((resolve, reject) => {
|
577
|
+
const reader = new FileReader();
|
578
|
+
reader.onloadend = () => resolve(reader.result);
|
579
|
+
reader.onerror = reject;
|
580
|
+
reader.readAsText(blob);
|
581
|
+
});
|
411
582
|
}
|
412
583
|
|
413
584
|
/**
|
@@ -416,18 +587,18 @@ function blobToText(blob) {
|
|
416
587
|
* @throws {Error} no shadow-root is defined
|
417
588
|
*/
|
418
589
|
function initControlReferences() {
|
419
|
-
|
420
|
-
|
421
|
-
|
590
|
+
if (!this.shadowRoot) {
|
591
|
+
throw new Error("no shadow-root is defined");
|
592
|
+
}
|
422
593
|
|
423
|
-
|
594
|
+
this[viewerElementSymbol] = this.shadowRoot.getElementById("viewer");
|
424
595
|
}
|
425
596
|
|
426
597
|
/**
|
427
598
|
* @private
|
428
599
|
*/
|
429
600
|
function initEventHandler() {
|
430
|
-
|
601
|
+
return this;
|
431
602
|
}
|
432
603
|
|
433
604
|
/**
|
@@ -435,8 +606,8 @@ function initEventHandler() {
|
|
435
606
|
* @return {string}
|
436
607
|
*/
|
437
608
|
function getTemplate() {
|
438
|
-
|
439
|
-
|
609
|
+
// language=HTML
|
610
|
+
return `
|
440
611
|
<div id="viewer" data-monster-role="viewer" part="viewer"
|
441
612
|
data-monster-replace="path:content"
|
442
613
|
data-monster-attributes="class path:classes.viewer">
|