docviewhelper 0.0.1 → 0.1.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.
- package/index.d.mts +54 -0
- package/index.d.ts +54 -2
- package/index.js +282 -0
- package/index.js.map +1 -0
- package/index.mjs +242 -0
- package/index.mjs.map +1 -0
- package/package.json +12 -6
- package/README.md +0 -3
- package/index.esm.js +0 -3249
- package/index.umd.js +0 -3342
- package/lib/helper.d.ts +0 -24
- package/lib/model.d.ts +0 -17
package/index.d.mts
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
type ViewerType = 'google' | 'office' | 'mammoth' | 'pdf' | 'url';
|
|
2
|
+
type ViewerRenderPhase = 'idle' | 'loading' | 'ready' | 'error';
|
|
3
|
+
type ViewerRecoveryMode = 'google-probe' | 'google-final-retry' | 'office-auto-retry';
|
|
4
|
+
interface ViewerRecoveryPlan {
|
|
5
|
+
modes: ViewerRecoveryMode[];
|
|
6
|
+
}
|
|
7
|
+
interface Props {
|
|
8
|
+
loaded?: () => void;
|
|
9
|
+
url: string;
|
|
10
|
+
queryParams?: string;
|
|
11
|
+
viewerUrl?: string;
|
|
12
|
+
googleCheckInterval?: number;
|
|
13
|
+
disableContent?: 'none' | 'all' | 'poput' | 'popout-hide';
|
|
14
|
+
googleCheckContentLoaded?: boolean;
|
|
15
|
+
viewer?: ViewerType;
|
|
16
|
+
}
|
|
17
|
+
interface IFrameReloader {
|
|
18
|
+
subscribe: (iframe: HTMLIFrameElement, interval?: number, maxChecks?: number) => any;
|
|
19
|
+
unsubscribe: () => void;
|
|
20
|
+
}
|
|
21
|
+
declare const defaultProps: Props;
|
|
22
|
+
|
|
23
|
+
declare const fileToArray: (url: string) => Promise<ArrayBuffer>;
|
|
24
|
+
declare const timeout: (ms: number) => Promise<unknown>;
|
|
25
|
+
declare const handleFileUpload: (fileInput: any) => Promise<string>;
|
|
26
|
+
declare const getbaseUrl: () => string;
|
|
27
|
+
declare const getLocation: (href: string) => {
|
|
28
|
+
href: string;
|
|
29
|
+
protocol: string;
|
|
30
|
+
host: string;
|
|
31
|
+
hostname: string;
|
|
32
|
+
port: string;
|
|
33
|
+
pathname: string;
|
|
34
|
+
search: string;
|
|
35
|
+
hash: string;
|
|
36
|
+
} | null;
|
|
37
|
+
declare const getDocxToHtml: (url: string) => Promise<any>;
|
|
38
|
+
declare const googleCheckSubscription: () => IFrameReloader;
|
|
39
|
+
declare const iframeIsLoaded: (iframe: HTMLIFrameElement) => boolean;
|
|
40
|
+
declare const getViewerDetails: (url: string, configuredViewer?: ViewerType, queryParams?: string, viewerUrl?: string) => {
|
|
41
|
+
url: string;
|
|
42
|
+
externalViewer: boolean;
|
|
43
|
+
};
|
|
44
|
+
declare const getViewerRecoveryPlan: ({ viewer, googleCheckContentLoaded, googleFinalRetryDelay, officeAutoRetry, }: {
|
|
45
|
+
viewer: ViewerType;
|
|
46
|
+
googleCheckContentLoaded?: boolean;
|
|
47
|
+
googleFinalRetryDelay?: number;
|
|
48
|
+
officeAutoRetry?: boolean;
|
|
49
|
+
}) => ViewerRecoveryPlan;
|
|
50
|
+
declare const replaceLocalUrl: (url: string, overrideLocalhost: string) => string;
|
|
51
|
+
declare const uploadToCloud: (fileUrl: string, api: string) => Promise<unknown>;
|
|
52
|
+
declare const isLocalFile: (file: string) => boolean;
|
|
53
|
+
|
|
54
|
+
export { type IFrameReloader, type ViewerRecoveryMode, type ViewerRecoveryPlan, type ViewerRenderPhase, type ViewerType, defaultProps, fileToArray, getDocxToHtml, getLocation, getViewerDetails, getViewerRecoveryPlan, getbaseUrl, googleCheckSubscription, handleFileUpload, iframeIsLoaded, isLocalFile, replaceLocalUrl, timeout, uploadToCloud };
|
package/index.d.ts
CHANGED
|
@@ -1,2 +1,54 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
type ViewerType = 'google' | 'office' | 'mammoth' | 'pdf' | 'url';
|
|
2
|
+
type ViewerRenderPhase = 'idle' | 'loading' | 'ready' | 'error';
|
|
3
|
+
type ViewerRecoveryMode = 'google-probe' | 'google-final-retry' | 'office-auto-retry';
|
|
4
|
+
interface ViewerRecoveryPlan {
|
|
5
|
+
modes: ViewerRecoveryMode[];
|
|
6
|
+
}
|
|
7
|
+
interface Props {
|
|
8
|
+
loaded?: () => void;
|
|
9
|
+
url: string;
|
|
10
|
+
queryParams?: string;
|
|
11
|
+
viewerUrl?: string;
|
|
12
|
+
googleCheckInterval?: number;
|
|
13
|
+
disableContent?: 'none' | 'all' | 'poput' | 'popout-hide';
|
|
14
|
+
googleCheckContentLoaded?: boolean;
|
|
15
|
+
viewer?: ViewerType;
|
|
16
|
+
}
|
|
17
|
+
interface IFrameReloader {
|
|
18
|
+
subscribe: (iframe: HTMLIFrameElement, interval?: number, maxChecks?: number) => any;
|
|
19
|
+
unsubscribe: () => void;
|
|
20
|
+
}
|
|
21
|
+
declare const defaultProps: Props;
|
|
22
|
+
|
|
23
|
+
declare const fileToArray: (url: string) => Promise<ArrayBuffer>;
|
|
24
|
+
declare const timeout: (ms: number) => Promise<unknown>;
|
|
25
|
+
declare const handleFileUpload: (fileInput: any) => Promise<string>;
|
|
26
|
+
declare const getbaseUrl: () => string;
|
|
27
|
+
declare const getLocation: (href: string) => {
|
|
28
|
+
href: string;
|
|
29
|
+
protocol: string;
|
|
30
|
+
host: string;
|
|
31
|
+
hostname: string;
|
|
32
|
+
port: string;
|
|
33
|
+
pathname: string;
|
|
34
|
+
search: string;
|
|
35
|
+
hash: string;
|
|
36
|
+
} | null;
|
|
37
|
+
declare const getDocxToHtml: (url: string) => Promise<any>;
|
|
38
|
+
declare const googleCheckSubscription: () => IFrameReloader;
|
|
39
|
+
declare const iframeIsLoaded: (iframe: HTMLIFrameElement) => boolean;
|
|
40
|
+
declare const getViewerDetails: (url: string, configuredViewer?: ViewerType, queryParams?: string, viewerUrl?: string) => {
|
|
41
|
+
url: string;
|
|
42
|
+
externalViewer: boolean;
|
|
43
|
+
};
|
|
44
|
+
declare const getViewerRecoveryPlan: ({ viewer, googleCheckContentLoaded, googleFinalRetryDelay, officeAutoRetry, }: {
|
|
45
|
+
viewer: ViewerType;
|
|
46
|
+
googleCheckContentLoaded?: boolean;
|
|
47
|
+
googleFinalRetryDelay?: number;
|
|
48
|
+
officeAutoRetry?: boolean;
|
|
49
|
+
}) => ViewerRecoveryPlan;
|
|
50
|
+
declare const replaceLocalUrl: (url: string, overrideLocalhost: string) => string;
|
|
51
|
+
declare const uploadToCloud: (fileUrl: string, api: string) => Promise<unknown>;
|
|
52
|
+
declare const isLocalFile: (file: string) => boolean;
|
|
53
|
+
|
|
54
|
+
export { type IFrameReloader, type ViewerRecoveryMode, type ViewerRecoveryPlan, type ViewerRenderPhase, type ViewerType, defaultProps, fileToArray, getDocxToHtml, getLocation, getViewerDetails, getViewerRecoveryPlan, getbaseUrl, googleCheckSubscription, handleFileUpload, iframeIsLoaded, isLocalFile, replaceLocalUrl, timeout, uploadToCloud };
|
package/index.js
ADDED
|
@@ -0,0 +1,282 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
defaultProps: () => defaultProps,
|
|
24
|
+
fileToArray: () => fileToArray,
|
|
25
|
+
getDocxToHtml: () => getDocxToHtml,
|
|
26
|
+
getLocation: () => getLocation,
|
|
27
|
+
getViewerDetails: () => getViewerDetails,
|
|
28
|
+
getViewerRecoveryPlan: () => getViewerRecoveryPlan,
|
|
29
|
+
getbaseUrl: () => getbaseUrl,
|
|
30
|
+
googleCheckSubscription: () => googleCheckSubscription,
|
|
31
|
+
handleFileUpload: () => handleFileUpload,
|
|
32
|
+
iframeIsLoaded: () => iframeIsLoaded,
|
|
33
|
+
isLocalFile: () => isLocalFile,
|
|
34
|
+
replaceLocalUrl: () => replaceLocalUrl,
|
|
35
|
+
timeout: () => timeout,
|
|
36
|
+
uploadToCloud: () => uploadToCloud
|
|
37
|
+
});
|
|
38
|
+
module.exports = __toCommonJS(index_exports);
|
|
39
|
+
|
|
40
|
+
// src/lib/helper.ts
|
|
41
|
+
var fileToArray = (url) => {
|
|
42
|
+
return new Promise((resolve, reject) => {
|
|
43
|
+
try {
|
|
44
|
+
const request = new XMLHttpRequest();
|
|
45
|
+
request.open("GET", url, true);
|
|
46
|
+
request.responseType = "blob";
|
|
47
|
+
request.onload = () => {
|
|
48
|
+
const reader = new FileReader();
|
|
49
|
+
reader.readAsArrayBuffer(request.response);
|
|
50
|
+
reader.onloadend = () => {
|
|
51
|
+
resolve(reader.result);
|
|
52
|
+
};
|
|
53
|
+
};
|
|
54
|
+
request.send();
|
|
55
|
+
} catch {
|
|
56
|
+
reject(`error while retrieving file ${url}.`);
|
|
57
|
+
}
|
|
58
|
+
});
|
|
59
|
+
};
|
|
60
|
+
var timeout = (ms) => {
|
|
61
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
62
|
+
};
|
|
63
|
+
var reloadIFrame = (iframe) => {
|
|
64
|
+
if (iframe) {
|
|
65
|
+
const url = iframe.src;
|
|
66
|
+
iframe.src = "about:blank";
|
|
67
|
+
setTimeout(() => {
|
|
68
|
+
if (iframe) {
|
|
69
|
+
iframe.src = url;
|
|
70
|
+
}
|
|
71
|
+
}, 100);
|
|
72
|
+
}
|
|
73
|
+
};
|
|
74
|
+
var handleFileUpload = (fileInput) => {
|
|
75
|
+
return new Promise((resolve, reject) => {
|
|
76
|
+
if (fileInput.target.files && fileInput.target.files[0]) {
|
|
77
|
+
const reader = new FileReader();
|
|
78
|
+
reader.onload = (e) => {
|
|
79
|
+
resolve(e.target.result);
|
|
80
|
+
};
|
|
81
|
+
reader.readAsDataURL(fileInput.target.files[0]);
|
|
82
|
+
} else {
|
|
83
|
+
reject("no files selected");
|
|
84
|
+
}
|
|
85
|
+
});
|
|
86
|
+
};
|
|
87
|
+
var getbaseUrl = () => {
|
|
88
|
+
const pathArray = window.location.href.split("/");
|
|
89
|
+
const protocol = pathArray[0];
|
|
90
|
+
const host = pathArray[2];
|
|
91
|
+
return protocol + "//" + host;
|
|
92
|
+
};
|
|
93
|
+
var getLocation = (href) => {
|
|
94
|
+
const match = href.match(
|
|
95
|
+
/^(https?\:)\/\/(([^:\/?#]*)(?:\:([0-9]+))?)([\/]{0,1}[^?#]*)(\?[^#]*|)(#.*|)$/
|
|
96
|
+
);
|
|
97
|
+
return match && {
|
|
98
|
+
href,
|
|
99
|
+
protocol: match[1],
|
|
100
|
+
host: match[2],
|
|
101
|
+
hostname: match[3],
|
|
102
|
+
port: match[4],
|
|
103
|
+
pathname: match[5],
|
|
104
|
+
search: match[6],
|
|
105
|
+
hash: match[7]
|
|
106
|
+
};
|
|
107
|
+
};
|
|
108
|
+
var getDocxToHtml = async (url) => {
|
|
109
|
+
if (!mammoth) {
|
|
110
|
+
console.error(
|
|
111
|
+
"Please install mammoth and make sure mammoth.browser.min.js is loaded."
|
|
112
|
+
);
|
|
113
|
+
}
|
|
114
|
+
const arrayBuffer = await fileToArray(url);
|
|
115
|
+
const resultObject = await mammoth.convertToHtml({ arrayBuffer });
|
|
116
|
+
return resultObject.value;
|
|
117
|
+
};
|
|
118
|
+
var googleCheckSubscription = () => {
|
|
119
|
+
let subscription = null;
|
|
120
|
+
let checkCount = 0;
|
|
121
|
+
return {
|
|
122
|
+
subscribe: (iframe, interval = 3e3, maxChecks = 5) => {
|
|
123
|
+
if (!iframeIsLoaded(iframe)) {
|
|
124
|
+
subscription = setInterval(() => {
|
|
125
|
+
checkCount++;
|
|
126
|
+
if (checkCount >= maxChecks) {
|
|
127
|
+
clearInterval(subscription);
|
|
128
|
+
}
|
|
129
|
+
reloadIFrame(iframe);
|
|
130
|
+
}, interval);
|
|
131
|
+
return subscription;
|
|
132
|
+
} else {
|
|
133
|
+
if (subscription) {
|
|
134
|
+
clearInterval(subscription);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
},
|
|
138
|
+
unsubscribe: () => {
|
|
139
|
+
if (subscription) {
|
|
140
|
+
clearInterval(subscription);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
};
|
|
144
|
+
};
|
|
145
|
+
var iframeIsLoaded = (iframe) => {
|
|
146
|
+
let isLoaded = false;
|
|
147
|
+
try {
|
|
148
|
+
if (!internetExplorer()) {
|
|
149
|
+
isLoaded = !iframe?.contentDocument;
|
|
150
|
+
} else {
|
|
151
|
+
isLoaded = !iframe?.contentWindow?.document;
|
|
152
|
+
}
|
|
153
|
+
} catch {
|
|
154
|
+
}
|
|
155
|
+
return isLoaded;
|
|
156
|
+
};
|
|
157
|
+
var internetExplorer = () => /MSIE (\d+\.\d+);/.test(navigator.userAgent) || navigator.userAgent.indexOf("Trident/") > -1;
|
|
158
|
+
var getViewerDetails = (url, configuredViewer = "google", queryParams = "", viewerUrl = "") => {
|
|
159
|
+
switch (configuredViewer) {
|
|
160
|
+
case "google":
|
|
161
|
+
viewerUrl = `https://docs.google.com/gview?url=%URL%&embedded=true`;
|
|
162
|
+
break;
|
|
163
|
+
case "office": {
|
|
164
|
+
viewerUrl = `https://view.officeapps.live.com/op/embed.aspx?src=%URL%`;
|
|
165
|
+
break;
|
|
166
|
+
}
|
|
167
|
+
case "pdf": {
|
|
168
|
+
viewerUrl = "";
|
|
169
|
+
break;
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
const externalViewer = configuredViewer === "google" || configuredViewer === "office" || configuredViewer === "url";
|
|
173
|
+
const u = url?.indexOf("/") ? encodeURIComponent(url) : url;
|
|
174
|
+
let fullUrl = viewerUrl ? viewerUrl.replace("%URL%", u) : url;
|
|
175
|
+
if (queryParams && externalViewer && configuredViewer !== "url") {
|
|
176
|
+
const start = queryParams.startsWith("&") ? "" : "&";
|
|
177
|
+
fullUrl = `${fullUrl}${start}${queryParams}`;
|
|
178
|
+
}
|
|
179
|
+
return {
|
|
180
|
+
url: fullUrl,
|
|
181
|
+
externalViewer
|
|
182
|
+
};
|
|
183
|
+
};
|
|
184
|
+
var getViewerRecoveryPlan = ({
|
|
185
|
+
viewer,
|
|
186
|
+
googleCheckContentLoaded = true,
|
|
187
|
+
googleFinalRetryDelay = 0,
|
|
188
|
+
officeAutoRetry = false
|
|
189
|
+
}) => {
|
|
190
|
+
const modes = [];
|
|
191
|
+
if (viewer === "google" && googleCheckContentLoaded) {
|
|
192
|
+
modes.push("google-probe");
|
|
193
|
+
}
|
|
194
|
+
if (viewer === "google" && googleFinalRetryDelay > 0) {
|
|
195
|
+
modes.push("google-final-retry");
|
|
196
|
+
}
|
|
197
|
+
if (viewer === "office" && officeAutoRetry) {
|
|
198
|
+
modes.push("office-auto-retry");
|
|
199
|
+
}
|
|
200
|
+
return { modes };
|
|
201
|
+
};
|
|
202
|
+
var replaceLocalUrl = (url, overrideLocalhost) => {
|
|
203
|
+
const loc = getLocation(url);
|
|
204
|
+
const locReplace = getLocation(overrideLocalhost);
|
|
205
|
+
if (loc && locReplace) {
|
|
206
|
+
return url.replace(
|
|
207
|
+
loc.port ? `${loc.hostname}:${loc.port}` : loc.hostname,
|
|
208
|
+
locReplace.port ? `${locReplace.hostname}:${locReplace.port}` : locReplace.hostname
|
|
209
|
+
);
|
|
210
|
+
}
|
|
211
|
+
return url;
|
|
212
|
+
};
|
|
213
|
+
var getBlobFromUrl = (url) => {
|
|
214
|
+
return new Promise((resolve, reject) => {
|
|
215
|
+
const request = new XMLHttpRequest();
|
|
216
|
+
request.open("GET", url, true);
|
|
217
|
+
request.responseType = "blob";
|
|
218
|
+
request.onload = () => {
|
|
219
|
+
resolve(request.response);
|
|
220
|
+
};
|
|
221
|
+
request.onerror = reject;
|
|
222
|
+
request.send();
|
|
223
|
+
});
|
|
224
|
+
};
|
|
225
|
+
var uploadToCloud = (fileUrl, api) => new Promise((resolve, reject) => {
|
|
226
|
+
getBlobFromUrl(fileUrl).then((blob) => {
|
|
227
|
+
const loc = getLocation(fileUrl);
|
|
228
|
+
const name = loc?.pathname ? loc?.pathname?.split("/")[loc?.pathname?.split("/").length - 1] : "";
|
|
229
|
+
const formData = new FormData();
|
|
230
|
+
const request = new XMLHttpRequest();
|
|
231
|
+
formData.append("file", blob, name);
|
|
232
|
+
request.onreadystatechange = (e) => {
|
|
233
|
+
if (request.readyState === XMLHttpRequest.DONE) {
|
|
234
|
+
if (request.status === 200) {
|
|
235
|
+
resolve(request.responseText);
|
|
236
|
+
} else {
|
|
237
|
+
reject(request.responseText);
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
};
|
|
241
|
+
request.onerror = reject;
|
|
242
|
+
request.open("post", api, true);
|
|
243
|
+
request.send(formData);
|
|
244
|
+
});
|
|
245
|
+
});
|
|
246
|
+
var isLocalFile = (file) => {
|
|
247
|
+
const loc = getLocation(file);
|
|
248
|
+
const hostname = loc?.hostname || "";
|
|
249
|
+
return ["localhost", "127.0.0.1", "", "::1"].includes(hostname) || hostname.startsWith("192.168.") || hostname.startsWith("10.0.") || hostname.endsWith(".local");
|
|
250
|
+
};
|
|
251
|
+
|
|
252
|
+
// src/lib/model.ts
|
|
253
|
+
var defaultProps = {
|
|
254
|
+
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
|
255
|
+
loaded: () => {
|
|
256
|
+
},
|
|
257
|
+
disableContent: "none",
|
|
258
|
+
googleCheckContentLoaded: true,
|
|
259
|
+
googleCheckInterval: 3e3,
|
|
260
|
+
queryParams: "",
|
|
261
|
+
url: "",
|
|
262
|
+
viewer: "google",
|
|
263
|
+
viewerUrl: ""
|
|
264
|
+
};
|
|
265
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
266
|
+
0 && (module.exports = {
|
|
267
|
+
defaultProps,
|
|
268
|
+
fileToArray,
|
|
269
|
+
getDocxToHtml,
|
|
270
|
+
getLocation,
|
|
271
|
+
getViewerDetails,
|
|
272
|
+
getViewerRecoveryPlan,
|
|
273
|
+
getbaseUrl,
|
|
274
|
+
googleCheckSubscription,
|
|
275
|
+
handleFileUpload,
|
|
276
|
+
iframeIsLoaded,
|
|
277
|
+
isLocalFile,
|
|
278
|
+
replaceLocalUrl,
|
|
279
|
+
timeout,
|
|
280
|
+
uploadToCloud
|
|
281
|
+
});
|
|
282
|
+
//# sourceMappingURL=index.js.map
|
package/index.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../packages/docviewhelper/src/index.ts","../../../packages/docviewhelper/src/lib/helper.ts","../../../packages/docviewhelper/src/lib/model.ts"],"sourcesContent":["export * from './lib/helper';\nexport * from './lib/model';\n","// eslint-disable-next-line no-var\ndeclare var mammoth: any;\nimport { IFrameReloader, ViewerRecoveryPlan, ViewerType } from './model';\n\nexport const fileToArray = (url: string): Promise<ArrayBuffer> => {\n return new Promise<ArrayBuffer>((resolve, reject) => {\n try {\n const request = new XMLHttpRequest();\n request.open('GET', url, true);\n request.responseType = 'blob';\n request.onload = () => {\n const reader = new FileReader();\n reader.readAsArrayBuffer(request.response);\n reader.onloadend = () => {\n resolve(reader.result as ArrayBuffer);\n };\n };\n request.send();\n } catch {\n reject(`error while retrieving file ${url}.`);\n }\n });\n};\n\nexport const timeout = (ms: number) => {\n return new Promise((resolve) => setTimeout(resolve, ms));\n};\n\nconst reloadIFrame = (iframe: HTMLIFrameElement) => {\n if (iframe) {\n const url = iframe.src;\n iframe.src = 'about:blank';\n setTimeout(() => {\n if (iframe) {\n iframe.src = url;\n }\n }, 100);\n }\n};\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport const handleFileUpload = (fileInput: any) => {\n return new Promise<string>((resolve, reject) => {\n if (fileInput.target.files && fileInput.target.files[0]) {\n const reader = new FileReader();\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n reader.onload = (e: any) => {\n resolve(e.target.result);\n };\n reader.readAsDataURL(fileInput.target.files[0]);\n } else {\n reject('no files selected');\n }\n });\n};\n\nexport const getbaseUrl = (): string => {\n const pathArray = window.location.href.split('/');\n const protocol = pathArray[0];\n const host = pathArray[2];\n return protocol + '//' + host;\n};\n\nexport const getLocation = (href: string) => {\n // eslint-disable-next-line no-useless-escape\n const match = href.match(\n /^(https?\\:)\\/\\/(([^:\\/?#]*)(?:\\:([0-9]+))?)([\\/]{0,1}[^?#]*)(\\?[^#]*|)(#.*|)$/,\n );\n return (\n match && {\n href,\n protocol: match[1],\n host: match[2],\n hostname: match[3],\n port: match[4],\n pathname: match[5],\n search: match[6],\n hash: match[7],\n }\n );\n};\n\nexport const getDocxToHtml = async (url: string) => {\n if (!mammoth) {\n console.error(\n 'Please install mammoth and make sure mammoth.browser.min.js is loaded.',\n );\n }\n const arrayBuffer = await fileToArray(url);\n const resultObject = await mammoth.convertToHtml({ arrayBuffer });\n return resultObject.value;\n};\n\nexport const googleCheckSubscription = (): IFrameReloader => {\n let subscription: any = null;\n let checkCount = 0;\n return {\n subscribe: (iframe: HTMLIFrameElement, interval = 3000, maxChecks = 5) => {\n if (!iframeIsLoaded(iframe)) {\n subscription = setInterval(() => {\n checkCount++;\n if (checkCount >= maxChecks) {\n clearInterval(subscription);\n }\n reloadIFrame(iframe);\n }, interval);\n return subscription;\n } else {\n if (subscription) {\n clearInterval(subscription);\n }\n }\n },\n unsubscribe: () => {\n if (subscription) {\n clearInterval(subscription);\n }\n },\n };\n};\n\nexport const iframeIsLoaded = (iframe: HTMLIFrameElement) => {\n // its #document <html><head></head><body></body></html> when google is returning a 204\n // so if contentDocument = null then it's loaded.\n let isLoaded = false;\n try {\n if (!internetExplorer()) {\n isLoaded = !iframe?.contentDocument;\n } else {\n isLoaded = !iframe?.contentWindow?.document;\n }\n } catch {\n // ignore message Blocked a frame with origin \"http://...\" from accessing a cross-origin frame.\n }\n return isLoaded;\n};\n\nconst internetExplorer = () =>\n /MSIE (\\d+\\.\\d+);/.test(navigator.userAgent) ||\n navigator.userAgent.indexOf('Trident/') > -1;\n\nexport const getViewerDetails = (\n url: string,\n configuredViewer: ViewerType = 'google',\n queryParams = '',\n viewerUrl = '',\n) => {\n switch (configuredViewer) {\n case 'google':\n viewerUrl = `https://docs.google.com/gview?url=%URL%&embedded=true`;\n break;\n case 'office': {\n viewerUrl = `https://view.officeapps.live.com/op/embed.aspx?src=%URL%`;\n break;\n }\n case 'pdf': {\n viewerUrl = '';\n break;\n }\n }\n const externalViewer =\n configuredViewer === 'google' ||\n configuredViewer === 'office' ||\n configuredViewer === 'url';\n\n const u = url?.indexOf('/') ? encodeURIComponent(url) : url;\n let fullUrl = viewerUrl ? viewerUrl.replace('%URL%', u) : url;\n if (queryParams && externalViewer && configuredViewer !== 'url') {\n const start = queryParams.startsWith('&') ? '' : '&';\n fullUrl = `${fullUrl}${start}${queryParams}`;\n }\n return {\n url: fullUrl,\n externalViewer,\n };\n};\n\nexport const getViewerRecoveryPlan = ({\n viewer,\n googleCheckContentLoaded = true,\n googleFinalRetryDelay = 0,\n officeAutoRetry = false,\n}: {\n viewer: ViewerType;\n googleCheckContentLoaded?: boolean;\n googleFinalRetryDelay?: number;\n officeAutoRetry?: boolean;\n}): ViewerRecoveryPlan => {\n const modes: ViewerRecoveryPlan['modes'] = [];\n\n if (viewer === 'google' && googleCheckContentLoaded) {\n modes.push('google-probe');\n }\n\n if (viewer === 'google' && googleFinalRetryDelay > 0) {\n modes.push('google-final-retry');\n }\n\n if (viewer === 'office' && officeAutoRetry) {\n modes.push('office-auto-retry');\n }\n\n return { modes };\n};\n\nexport const replaceLocalUrl = (url: string, overrideLocalhost: string) => {\n const loc = getLocation(url);\n const locReplace = getLocation(overrideLocalhost);\n if (loc && locReplace) {\n return url.replace(\n loc.port ? `${loc.hostname}:${loc.port}` : loc.hostname,\n locReplace.port\n ? `${locReplace.hostname}:${locReplace.port}`\n : locReplace.hostname,\n );\n }\n return url;\n};\n\nconst getBlobFromUrl = (url: string) => {\n return new Promise<File>((resolve, reject) => {\n const request = new XMLHttpRequest();\n request.open('GET', url, true);\n request.responseType = 'blob';\n request.onload = () => {\n resolve(request.response as File);\n };\n request.onerror = reject;\n request.send();\n });\n};\n\nexport const uploadToCloud = (fileUrl: string, api: string) =>\n new Promise((resolve, reject) => {\n getBlobFromUrl(fileUrl).then((blob) => {\n const loc = getLocation(fileUrl);\n const name = loc?.pathname\n ? loc?.pathname?.split('/')[loc?.pathname?.split('/').length - 1]\n : '';\n const formData = new FormData();\n const request = new XMLHttpRequest();\n formData.append('file', blob, name);\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n request.onreadystatechange = (e) => {\n if (request.readyState === XMLHttpRequest.DONE) {\n if (request.status === 200) {\n resolve(request.responseText);\n } else {\n reject(request.responseText);\n }\n }\n };\n request.onerror = reject;\n request.open('post', api, true);\n request.send(formData);\n });\n });\n\nexport const isLocalFile = (file: string) => {\n const loc = getLocation(file);\n const hostname = loc?.hostname || '';\n return (\n ['localhost', '127.0.0.1', '', '::1'].includes(hostname) ||\n hostname.startsWith('192.168.') ||\n hostname.startsWith('10.0.') ||\n hostname.endsWith('.local')\n );\n};\n","export type ViewerType = 'google' | 'office' | 'mammoth' | 'pdf' | 'url';\nexport type ViewerRenderPhase = 'idle' | 'loading' | 'ready' | 'error';\nexport type ViewerRecoveryMode =\n | 'google-probe'\n | 'google-final-retry'\n | 'office-auto-retry';\n\nexport interface ViewerRecoveryPlan {\n modes: ViewerRecoveryMode[];\n}\n\ninterface Props {\n loaded?: () => void;\n url: string;\n queryParams?: string;\n viewerUrl?: string;\n googleCheckInterval?: number;\n disableContent?: 'none' | 'all' | 'poput' | 'popout-hide';\n googleCheckContentLoaded?: boolean;\n viewer?: ViewerType;\n}\n\nexport interface IFrameReloader {\n subscribe: (\n iframe: HTMLIFrameElement,\n interval?: number,\n maxChecks?: number\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n ) => any;\n unsubscribe: () => void;\n}\n\nexport const defaultProps: Props = {\n // eslint-disable-next-line @typescript-eslint/no-empty-function\n loaded: () => {},\n disableContent: 'none',\n googleCheckContentLoaded: true,\n googleCheckInterval: 3000,\n queryParams: '',\n url: '',\n viewer: 'google',\n viewerUrl: '',\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACIO,IAAM,cAAc,CAAC,QAAsC;AAChE,SAAO,IAAI,QAAqB,CAAC,SAAS,WAAW;AACnD,QAAI;AACF,YAAM,UAAU,IAAI,eAAe;AACnC,cAAQ,KAAK,OAAO,KAAK,IAAI;AAC7B,cAAQ,eAAe;AACvB,cAAQ,SAAS,MAAM;AACrB,cAAM,SAAS,IAAI,WAAW;AAC9B,eAAO,kBAAkB,QAAQ,QAAQ;AACzC,eAAO,YAAY,MAAM;AACvB,kBAAQ,OAAO,MAAqB;AAAA,QACtC;AAAA,MACF;AACA,cAAQ,KAAK;AAAA,IACf,QAAQ;AACN,aAAO,+BAA+B,GAAG,GAAG;AAAA,IAC9C;AAAA,EACF,CAAC;AACH;AAEO,IAAM,UAAU,CAAC,OAAe;AACrC,SAAO,IAAI,QAAQ,CAAC,YAAY,WAAW,SAAS,EA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/helper.ts
|
|
2
|
+
var fileToArray = (url) => {
|
|
3
|
+
return new Promise((resolve, reject) => {
|
|
4
|
+
try {
|
|
5
|
+
const request = new XMLHttpRequest();
|
|
6
|
+
request.open("GET", url, true);
|
|
7
|
+
request.responseType = "blob";
|
|
8
|
+
request.onload = () => {
|
|
9
|
+
const reader = new FileReader();
|
|
10
|
+
reader.readAsArrayBuffer(request.response);
|
|
11
|
+
reader.onloadend = () => {
|
|
12
|
+
resolve(reader.result);
|
|
13
|
+
};
|
|
14
|
+
};
|
|
15
|
+
request.send();
|
|
16
|
+
} catch {
|
|
17
|
+
reject(`error while retrieving file ${url}.`);
|
|
18
|
+
}
|
|
19
|
+
});
|
|
20
|
+
};
|
|
21
|
+
var timeout = (ms) => {
|
|
22
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
23
|
+
};
|
|
24
|
+
var reloadIFrame = (iframe) => {
|
|
25
|
+
if (iframe) {
|
|
26
|
+
const url = iframe.src;
|
|
27
|
+
iframe.src = "about:blank";
|
|
28
|
+
setTimeout(() => {
|
|
29
|
+
if (iframe) {
|
|
30
|
+
iframe.src = url;
|
|
31
|
+
}
|
|
32
|
+
}, 100);
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
var handleFileUpload = (fileInput) => {
|
|
36
|
+
return new Promise((resolve, reject) => {
|
|
37
|
+
if (fileInput.target.files && fileInput.target.files[0]) {
|
|
38
|
+
const reader = new FileReader();
|
|
39
|
+
reader.onload = (e) => {
|
|
40
|
+
resolve(e.target.result);
|
|
41
|
+
};
|
|
42
|
+
reader.readAsDataURL(fileInput.target.files[0]);
|
|
43
|
+
} else {
|
|
44
|
+
reject("no files selected");
|
|
45
|
+
}
|
|
46
|
+
});
|
|
47
|
+
};
|
|
48
|
+
var getbaseUrl = () => {
|
|
49
|
+
const pathArray = window.location.href.split("/");
|
|
50
|
+
const protocol = pathArray[0];
|
|
51
|
+
const host = pathArray[2];
|
|
52
|
+
return protocol + "//" + host;
|
|
53
|
+
};
|
|
54
|
+
var getLocation = (href) => {
|
|
55
|
+
const match = href.match(
|
|
56
|
+
/^(https?\:)\/\/(([^:\/?#]*)(?:\:([0-9]+))?)([\/]{0,1}[^?#]*)(\?[^#]*|)(#.*|)$/
|
|
57
|
+
);
|
|
58
|
+
return match && {
|
|
59
|
+
href,
|
|
60
|
+
protocol: match[1],
|
|
61
|
+
host: match[2],
|
|
62
|
+
hostname: match[3],
|
|
63
|
+
port: match[4],
|
|
64
|
+
pathname: match[5],
|
|
65
|
+
search: match[6],
|
|
66
|
+
hash: match[7]
|
|
67
|
+
};
|
|
68
|
+
};
|
|
69
|
+
var getDocxToHtml = async (url) => {
|
|
70
|
+
if (!mammoth) {
|
|
71
|
+
console.error(
|
|
72
|
+
"Please install mammoth and make sure mammoth.browser.min.js is loaded."
|
|
73
|
+
);
|
|
74
|
+
}
|
|
75
|
+
const arrayBuffer = await fileToArray(url);
|
|
76
|
+
const resultObject = await mammoth.convertToHtml({ arrayBuffer });
|
|
77
|
+
return resultObject.value;
|
|
78
|
+
};
|
|
79
|
+
var googleCheckSubscription = () => {
|
|
80
|
+
let subscription = null;
|
|
81
|
+
let checkCount = 0;
|
|
82
|
+
return {
|
|
83
|
+
subscribe: (iframe, interval = 3e3, maxChecks = 5) => {
|
|
84
|
+
if (!iframeIsLoaded(iframe)) {
|
|
85
|
+
subscription = setInterval(() => {
|
|
86
|
+
checkCount++;
|
|
87
|
+
if (checkCount >= maxChecks) {
|
|
88
|
+
clearInterval(subscription);
|
|
89
|
+
}
|
|
90
|
+
reloadIFrame(iframe);
|
|
91
|
+
}, interval);
|
|
92
|
+
return subscription;
|
|
93
|
+
} else {
|
|
94
|
+
if (subscription) {
|
|
95
|
+
clearInterval(subscription);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
},
|
|
99
|
+
unsubscribe: () => {
|
|
100
|
+
if (subscription) {
|
|
101
|
+
clearInterval(subscription);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
};
|
|
105
|
+
};
|
|
106
|
+
var iframeIsLoaded = (iframe) => {
|
|
107
|
+
let isLoaded = false;
|
|
108
|
+
try {
|
|
109
|
+
if (!internetExplorer()) {
|
|
110
|
+
isLoaded = !iframe?.contentDocument;
|
|
111
|
+
} else {
|
|
112
|
+
isLoaded = !iframe?.contentWindow?.document;
|
|
113
|
+
}
|
|
114
|
+
} catch {
|
|
115
|
+
}
|
|
116
|
+
return isLoaded;
|
|
117
|
+
};
|
|
118
|
+
var internetExplorer = () => /MSIE (\d+\.\d+);/.test(navigator.userAgent) || navigator.userAgent.indexOf("Trident/") > -1;
|
|
119
|
+
var getViewerDetails = (url, configuredViewer = "google", queryParams = "", viewerUrl = "") => {
|
|
120
|
+
switch (configuredViewer) {
|
|
121
|
+
case "google":
|
|
122
|
+
viewerUrl = `https://docs.google.com/gview?url=%URL%&embedded=true`;
|
|
123
|
+
break;
|
|
124
|
+
case "office": {
|
|
125
|
+
viewerUrl = `https://view.officeapps.live.com/op/embed.aspx?src=%URL%`;
|
|
126
|
+
break;
|
|
127
|
+
}
|
|
128
|
+
case "pdf": {
|
|
129
|
+
viewerUrl = "";
|
|
130
|
+
break;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
const externalViewer = configuredViewer === "google" || configuredViewer === "office" || configuredViewer === "url";
|
|
134
|
+
const u = url?.indexOf("/") ? encodeURIComponent(url) : url;
|
|
135
|
+
let fullUrl = viewerUrl ? viewerUrl.replace("%URL%", u) : url;
|
|
136
|
+
if (queryParams && externalViewer && configuredViewer !== "url") {
|
|
137
|
+
const start = queryParams.startsWith("&") ? "" : "&";
|
|
138
|
+
fullUrl = `${fullUrl}${start}${queryParams}`;
|
|
139
|
+
}
|
|
140
|
+
return {
|
|
141
|
+
url: fullUrl,
|
|
142
|
+
externalViewer
|
|
143
|
+
};
|
|
144
|
+
};
|
|
145
|
+
var getViewerRecoveryPlan = ({
|
|
146
|
+
viewer,
|
|
147
|
+
googleCheckContentLoaded = true,
|
|
148
|
+
googleFinalRetryDelay = 0,
|
|
149
|
+
officeAutoRetry = false
|
|
150
|
+
}) => {
|
|
151
|
+
const modes = [];
|
|
152
|
+
if (viewer === "google" && googleCheckContentLoaded) {
|
|
153
|
+
modes.push("google-probe");
|
|
154
|
+
}
|
|
155
|
+
if (viewer === "google" && googleFinalRetryDelay > 0) {
|
|
156
|
+
modes.push("google-final-retry");
|
|
157
|
+
}
|
|
158
|
+
if (viewer === "office" && officeAutoRetry) {
|
|
159
|
+
modes.push("office-auto-retry");
|
|
160
|
+
}
|
|
161
|
+
return { modes };
|
|
162
|
+
};
|
|
163
|
+
var replaceLocalUrl = (url, overrideLocalhost) => {
|
|
164
|
+
const loc = getLocation(url);
|
|
165
|
+
const locReplace = getLocation(overrideLocalhost);
|
|
166
|
+
if (loc && locReplace) {
|
|
167
|
+
return url.replace(
|
|
168
|
+
loc.port ? `${loc.hostname}:${loc.port}` : loc.hostname,
|
|
169
|
+
locReplace.port ? `${locReplace.hostname}:${locReplace.port}` : locReplace.hostname
|
|
170
|
+
);
|
|
171
|
+
}
|
|
172
|
+
return url;
|
|
173
|
+
};
|
|
174
|
+
var getBlobFromUrl = (url) => {
|
|
175
|
+
return new Promise((resolve, reject) => {
|
|
176
|
+
const request = new XMLHttpRequest();
|
|
177
|
+
request.open("GET", url, true);
|
|
178
|
+
request.responseType = "blob";
|
|
179
|
+
request.onload = () => {
|
|
180
|
+
resolve(request.response);
|
|
181
|
+
};
|
|
182
|
+
request.onerror = reject;
|
|
183
|
+
request.send();
|
|
184
|
+
});
|
|
185
|
+
};
|
|
186
|
+
var uploadToCloud = (fileUrl, api) => new Promise((resolve, reject) => {
|
|
187
|
+
getBlobFromUrl(fileUrl).then((blob) => {
|
|
188
|
+
const loc = getLocation(fileUrl);
|
|
189
|
+
const name = loc?.pathname ? loc?.pathname?.split("/")[loc?.pathname?.split("/").length - 1] : "";
|
|
190
|
+
const formData = new FormData();
|
|
191
|
+
const request = new XMLHttpRequest();
|
|
192
|
+
formData.append("file", blob, name);
|
|
193
|
+
request.onreadystatechange = (e) => {
|
|
194
|
+
if (request.readyState === XMLHttpRequest.DONE) {
|
|
195
|
+
if (request.status === 200) {
|
|
196
|
+
resolve(request.responseText);
|
|
197
|
+
} else {
|
|
198
|
+
reject(request.responseText);
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
};
|
|
202
|
+
request.onerror = reject;
|
|
203
|
+
request.open("post", api, true);
|
|
204
|
+
request.send(formData);
|
|
205
|
+
});
|
|
206
|
+
});
|
|
207
|
+
var isLocalFile = (file) => {
|
|
208
|
+
const loc = getLocation(file);
|
|
209
|
+
const hostname = loc?.hostname || "";
|
|
210
|
+
return ["localhost", "127.0.0.1", "", "::1"].includes(hostname) || hostname.startsWith("192.168.") || hostname.startsWith("10.0.") || hostname.endsWith(".local");
|
|
211
|
+
};
|
|
212
|
+
|
|
213
|
+
// src/lib/model.ts
|
|
214
|
+
var defaultProps = {
|
|
215
|
+
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
|
216
|
+
loaded: () => {
|
|
217
|
+
},
|
|
218
|
+
disableContent: "none",
|
|
219
|
+
googleCheckContentLoaded: true,
|
|
220
|
+
googleCheckInterval: 3e3,
|
|
221
|
+
queryParams: "",
|
|
222
|
+
url: "",
|
|
223
|
+
viewer: "google",
|
|
224
|
+
viewerUrl: ""
|
|
225
|
+
};
|
|
226
|
+
export {
|
|
227
|
+
defaultProps,
|
|
228
|
+
fileToArray,
|
|
229
|
+
getDocxToHtml,
|
|
230
|
+
getLocation,
|
|
231
|
+
getViewerDetails,
|
|
232
|
+
getViewerRecoveryPlan,
|
|
233
|
+
getbaseUrl,
|
|
234
|
+
googleCheckSubscription,
|
|
235
|
+
handleFileUpload,
|
|
236
|
+
iframeIsLoaded,
|
|
237
|
+
isLocalFile,
|
|
238
|
+
replaceLocalUrl,
|
|
239
|
+
timeout,
|
|
240
|
+
uploadToCloud
|
|
241
|
+
};
|
|
242
|
+
//# sourceMappingURL=index.mjs.map
|