@file-viewer/svelte 2.0.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/LICENSE +160 -0
- package/README.en.md +175 -0
- package/README.md +175 -0
- package/dist/controller.d.ts +121 -0
- package/dist/controller.js +417 -0
- package/dist/index.d.ts +38 -0
- package/dist/index.js +78 -0
- package/package.json +78 -0
- package/src/FileViewer.svelte +163 -0
- package/src/FileViewer.svelte.d.ts +51 -0
- package/src/controller.ts +593 -0
|
@@ -0,0 +1,417 @@
|
|
|
1
|
+
import { createViewer } from '@file-viewer/core';
|
|
2
|
+
import { DEFAULT_FILE_VIEWER_SOURCE_FILENAME, getExtension, normalizeFilename, readFileViewerBuffer, resolveFileViewerSourceFilename, wrapFileViewerFileRef, } from '@file-viewer/core';
|
|
3
|
+
const isBrowser = () => typeof window !== 'undefined' && typeof document !== 'undefined';
|
|
4
|
+
const hasSource = (options = {}) => {
|
|
5
|
+
return !!(options.url || options.file || options.buffer);
|
|
6
|
+
};
|
|
7
|
+
const toViewerSourceInput = (options = {}) => ({
|
|
8
|
+
url: options.url,
|
|
9
|
+
file: options.file,
|
|
10
|
+
buffer: options.buffer,
|
|
11
|
+
filename: options.filename || options.name,
|
|
12
|
+
name: options.name,
|
|
13
|
+
type: options.type,
|
|
14
|
+
size: options.size,
|
|
15
|
+
});
|
|
16
|
+
const canUseFetch = () => typeof fetch === 'function';
|
|
17
|
+
const defaultFetchFile = async ({ url, signal }) => {
|
|
18
|
+
if (!canUseFetch()) {
|
|
19
|
+
throw new Error('fetch is not available in the current environment.');
|
|
20
|
+
}
|
|
21
|
+
const response = await fetch(url, { signal });
|
|
22
|
+
if (!response.ok) {
|
|
23
|
+
throw new Error(`Failed to fetch file: ${response.status} ${response.statusText}`);
|
|
24
|
+
}
|
|
25
|
+
return response.blob();
|
|
26
|
+
};
|
|
27
|
+
const resolveViewerSourceFilename = (source) => {
|
|
28
|
+
return normalizeFilename(source.filename || source.name || resolveFileViewerSourceFilename({
|
|
29
|
+
file: source.file,
|
|
30
|
+
url: source.url,
|
|
31
|
+
fallback: DEFAULT_FILE_VIEWER_SOURCE_FILENAME,
|
|
32
|
+
}), source.type ? `preview.${source.type}` : DEFAULT_FILE_VIEWER_SOURCE_FILENAME);
|
|
33
|
+
};
|
|
34
|
+
const resolveViewerLoadSource = async (source, options = {}) => {
|
|
35
|
+
var _a, _b, _c;
|
|
36
|
+
const filename = resolveViewerSourceFilename(source);
|
|
37
|
+
const type = source.type || getExtension(filename);
|
|
38
|
+
if (source.buffer) {
|
|
39
|
+
return {
|
|
40
|
+
buffer: source.buffer,
|
|
41
|
+
filename,
|
|
42
|
+
type,
|
|
43
|
+
size: (_a = source.size) !== null && _a !== void 0 ? _a : source.buffer.byteLength,
|
|
44
|
+
url: source.url,
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
if (source.file) {
|
|
48
|
+
const file = wrapFileViewerFileRef(source.file, filename);
|
|
49
|
+
return {
|
|
50
|
+
file,
|
|
51
|
+
buffer: await readFileViewerBuffer(file),
|
|
52
|
+
filename: file.name || filename,
|
|
53
|
+
type: type || getExtension(file.name),
|
|
54
|
+
size: (_b = source.size) !== null && _b !== void 0 ? _b : file.size,
|
|
55
|
+
url: source.url,
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
if (source.url) {
|
|
59
|
+
const fileRef = await (options.fetchFile || defaultFetchFile)({
|
|
60
|
+
url: source.url,
|
|
61
|
+
signal: options.signal,
|
|
62
|
+
source,
|
|
63
|
+
});
|
|
64
|
+
if (!fileRef) {
|
|
65
|
+
throw new Error('Downloaded file is empty.');
|
|
66
|
+
}
|
|
67
|
+
const file = wrapFileViewerFileRef(fileRef, filename);
|
|
68
|
+
return {
|
|
69
|
+
file,
|
|
70
|
+
buffer: await readFileViewerBuffer(file),
|
|
71
|
+
filename: file.name || filename,
|
|
72
|
+
type: type || getExtension(file.name),
|
|
73
|
+
size: (_c = source.size) !== null && _c !== void 0 ? _c : file.size,
|
|
74
|
+
url: source.url,
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
return {
|
|
78
|
+
filename,
|
|
79
|
+
type,
|
|
80
|
+
};
|
|
81
|
+
};
|
|
82
|
+
export const createViewerControllerHandle = (getController, dispose) => ({
|
|
83
|
+
load(options) {
|
|
84
|
+
var _a, _b;
|
|
85
|
+
return (_b = (_a = getController()) === null || _a === void 0 ? void 0 : _a.load(options)) !== null && _b !== void 0 ? _b : Promise.resolve();
|
|
86
|
+
},
|
|
87
|
+
update(options) {
|
|
88
|
+
var _a, _b;
|
|
89
|
+
return (_b = (_a = getController()) === null || _a === void 0 ? void 0 : _a.update(options)) !== null && _b !== void 0 ? _b : Promise.resolve();
|
|
90
|
+
},
|
|
91
|
+
reload() {
|
|
92
|
+
var _a, _b;
|
|
93
|
+
return (_b = (_a = getController()) === null || _a === void 0 ? void 0 : _a.reload()) !== null && _b !== void 0 ? _b : Promise.resolve();
|
|
94
|
+
},
|
|
95
|
+
destroy() {
|
|
96
|
+
dispose();
|
|
97
|
+
},
|
|
98
|
+
getController,
|
|
99
|
+
getApi() {
|
|
100
|
+
var _a, _b;
|
|
101
|
+
return (_b = (_a = getController()) === null || _a === void 0 ? void 0 : _a.getApi()) !== null && _b !== void 0 ? _b : null;
|
|
102
|
+
},
|
|
103
|
+
downloadOriginalFile() {
|
|
104
|
+
var _a, _b;
|
|
105
|
+
return (_b = (_a = getController()) === null || _a === void 0 ? void 0 : _a.downloadOriginalFile()) !== null && _b !== void 0 ? _b : Promise.resolve();
|
|
106
|
+
},
|
|
107
|
+
printRenderedHtml() {
|
|
108
|
+
var _a, _b;
|
|
109
|
+
return (_b = (_a = getController()) === null || _a === void 0 ? void 0 : _a.printRenderedHtml()) !== null && _b !== void 0 ? _b : Promise.resolve();
|
|
110
|
+
},
|
|
111
|
+
exportRenderedHtml() {
|
|
112
|
+
var _a, _b;
|
|
113
|
+
return (_b = (_a = getController()) === null || _a === void 0 ? void 0 : _a.exportRenderedHtml()) !== null && _b !== void 0 ? _b : Promise.resolve();
|
|
114
|
+
},
|
|
115
|
+
zoomIn() {
|
|
116
|
+
var _a, _b;
|
|
117
|
+
return (_b = (_a = getController()) === null || _a === void 0 ? void 0 : _a.zoomIn()) !== null && _b !== void 0 ? _b : Promise.resolve(null);
|
|
118
|
+
},
|
|
119
|
+
zoomOut() {
|
|
120
|
+
var _a, _b;
|
|
121
|
+
return (_b = (_a = getController()) === null || _a === void 0 ? void 0 : _a.zoomOut()) !== null && _b !== void 0 ? _b : Promise.resolve(null);
|
|
122
|
+
},
|
|
123
|
+
resetZoom() {
|
|
124
|
+
var _a, _b;
|
|
125
|
+
return (_b = (_a = getController()) === null || _a === void 0 ? void 0 : _a.resetZoom()) !== null && _b !== void 0 ? _b : Promise.resolve(null);
|
|
126
|
+
},
|
|
127
|
+
searchDocument(query) {
|
|
128
|
+
var _a, _b;
|
|
129
|
+
return (_b = (_a = getController()) === null || _a === void 0 ? void 0 : _a.searchDocument(query)) !== null && _b !== void 0 ? _b : Promise.resolve(null);
|
|
130
|
+
},
|
|
131
|
+
clearDocumentSearch() {
|
|
132
|
+
var _a, _b;
|
|
133
|
+
return (_b = (_a = getController()) === null || _a === void 0 ? void 0 : _a.clearDocumentSearch()) !== null && _b !== void 0 ? _b : Promise.resolve(null);
|
|
134
|
+
},
|
|
135
|
+
nextSearchResult() {
|
|
136
|
+
var _a, _b;
|
|
137
|
+
return (_b = (_a = getController()) === null || _a === void 0 ? void 0 : _a.nextSearchResult()) !== null && _b !== void 0 ? _b : Promise.resolve(null);
|
|
138
|
+
},
|
|
139
|
+
previousSearchResult() {
|
|
140
|
+
var _a, _b;
|
|
141
|
+
return (_b = (_a = getController()) === null || _a === void 0 ? void 0 : _a.previousSearchResult()) !== null && _b !== void 0 ? _b : Promise.resolve(null);
|
|
142
|
+
},
|
|
143
|
+
collectDocumentAnchors() {
|
|
144
|
+
var _a, _b;
|
|
145
|
+
return (_b = (_a = getController()) === null || _a === void 0 ? void 0 : _a.collectDocumentAnchors()) !== null && _b !== void 0 ? _b : Promise.resolve([]);
|
|
146
|
+
},
|
|
147
|
+
scrollToAnchor(anchor) {
|
|
148
|
+
var _a, _b;
|
|
149
|
+
return (_b = (_a = getController()) === null || _a === void 0 ? void 0 : _a.scrollToAnchor(anchor)) !== null && _b !== void 0 ? _b : Promise.resolve(false);
|
|
150
|
+
},
|
|
151
|
+
scrollToLine(line) {
|
|
152
|
+
var _a, _b;
|
|
153
|
+
return (_b = (_a = getController()) === null || _a === void 0 ? void 0 : _a.scrollToLine(line)) !== null && _b !== void 0 ? _b : Promise.resolve(false);
|
|
154
|
+
},
|
|
155
|
+
getDocumentTextChunks() {
|
|
156
|
+
var _a, _b;
|
|
157
|
+
return (_b = (_a = getController()) === null || _a === void 0 ? void 0 : _a.getDocumentTextChunks()) !== null && _b !== void 0 ? _b : [];
|
|
158
|
+
},
|
|
159
|
+
getOperationAvailability() {
|
|
160
|
+
var _a, _b;
|
|
161
|
+
return (_b = (_a = getController()) === null || _a === void 0 ? void 0 : _a.getOperationAvailability()) !== null && _b !== void 0 ? _b : null;
|
|
162
|
+
},
|
|
163
|
+
getZoomState() {
|
|
164
|
+
var _a, _b;
|
|
165
|
+
return (_b = (_a = getController()) === null || _a === void 0 ? void 0 : _a.getZoomState()) !== null && _b !== void 0 ? _b : null;
|
|
166
|
+
},
|
|
167
|
+
getSearchState() {
|
|
168
|
+
var _a, _b;
|
|
169
|
+
return (_b = (_a = getController()) === null || _a === void 0 ? void 0 : _a.getSearchState()) !== null && _b !== void 0 ? _b : null;
|
|
170
|
+
},
|
|
171
|
+
getState() {
|
|
172
|
+
var _a, _b;
|
|
173
|
+
return (_b = (_a = getController()) === null || _a === void 0 ? void 0 : _a.getState()) !== null && _b !== void 0 ? _b : null;
|
|
174
|
+
},
|
|
175
|
+
subscribe(listener) {
|
|
176
|
+
var _a, _b;
|
|
177
|
+
return (_b = (_a = getController()) === null || _a === void 0 ? void 0 : _a.subscribe(listener)) !== null && _b !== void 0 ? _b : (() => { });
|
|
178
|
+
},
|
|
179
|
+
});
|
|
180
|
+
const callApi = async (api, action, fallback) => {
|
|
181
|
+
if (!api) {
|
|
182
|
+
return fallback;
|
|
183
|
+
}
|
|
184
|
+
return action(api);
|
|
185
|
+
};
|
|
186
|
+
const isAbortError = (error) => {
|
|
187
|
+
return Boolean(error && typeof error === 'object' && error.name === 'AbortError');
|
|
188
|
+
};
|
|
189
|
+
export const mountViewer = (container, initialOptions = {}, coreOptions = {}) => {
|
|
190
|
+
if (!isBrowser()) {
|
|
191
|
+
throw new Error('Flyfish File Viewer can only be mounted in a browser DOM environment.');
|
|
192
|
+
}
|
|
193
|
+
let disposed = false;
|
|
194
|
+
let currentOptions = initialOptions;
|
|
195
|
+
let currentSource = hasSource(currentOptions)
|
|
196
|
+
? toViewerSourceInput(currentOptions)
|
|
197
|
+
: null;
|
|
198
|
+
let abortController = null;
|
|
199
|
+
const listeners = new Set();
|
|
200
|
+
const state = {
|
|
201
|
+
loading: false,
|
|
202
|
+
ready: false,
|
|
203
|
+
error: null,
|
|
204
|
+
lastEvent: null,
|
|
205
|
+
lifecycle: null,
|
|
206
|
+
availability: null,
|
|
207
|
+
search: null,
|
|
208
|
+
zoom: null,
|
|
209
|
+
location: null,
|
|
210
|
+
};
|
|
211
|
+
const snapshotState = () => ({
|
|
212
|
+
...state,
|
|
213
|
+
search: state.search
|
|
214
|
+
? { ...state.search, matches: [...state.search.matches] }
|
|
215
|
+
: null,
|
|
216
|
+
});
|
|
217
|
+
const notifyState = (event) => {
|
|
218
|
+
var _a;
|
|
219
|
+
const snapshot = snapshotState();
|
|
220
|
+
(_a = currentOptions.onStateChange) === null || _a === void 0 ? void 0 : _a.call(currentOptions, snapshot, event);
|
|
221
|
+
listeners.forEach(listener => listener(snapshot, event));
|
|
222
|
+
};
|
|
223
|
+
const applyViewerEvent = (event) => {
|
|
224
|
+
var _a;
|
|
225
|
+
state.lastEvent = event;
|
|
226
|
+
if (event.type === 'load-start') {
|
|
227
|
+
state.loading = true;
|
|
228
|
+
state.ready = false;
|
|
229
|
+
state.error = null;
|
|
230
|
+
state.lifecycle = event.payload;
|
|
231
|
+
}
|
|
232
|
+
else if (event.type === 'load-complete') {
|
|
233
|
+
state.loading = false;
|
|
234
|
+
state.ready = true;
|
|
235
|
+
state.lifecycle = event.payload;
|
|
236
|
+
}
|
|
237
|
+
else if (event.type === 'unload-start') {
|
|
238
|
+
state.loading = true;
|
|
239
|
+
state.ready = false;
|
|
240
|
+
state.lifecycle = event.payload;
|
|
241
|
+
}
|
|
242
|
+
else if (event.type === 'unload-complete') {
|
|
243
|
+
state.loading = false;
|
|
244
|
+
state.ready = false;
|
|
245
|
+
state.lifecycle = event.payload;
|
|
246
|
+
}
|
|
247
|
+
else if (event.type === 'operation-availability-change') {
|
|
248
|
+
state.availability = event.payload;
|
|
249
|
+
}
|
|
250
|
+
else if (event.type === 'search-change') {
|
|
251
|
+
state.search = event.payload;
|
|
252
|
+
}
|
|
253
|
+
else if (event.type === 'location-change') {
|
|
254
|
+
state.location = event.payload;
|
|
255
|
+
}
|
|
256
|
+
else if (event.type === 'zoom-change') {
|
|
257
|
+
state.zoom = event.payload;
|
|
258
|
+
}
|
|
259
|
+
(_a = currentOptions.onEvent) === null || _a === void 0 ? void 0 : _a.call(currentOptions, event);
|
|
260
|
+
notifyState(event);
|
|
261
|
+
};
|
|
262
|
+
const instance = createViewer(container, {
|
|
263
|
+
registry: coreOptions.registry,
|
|
264
|
+
options: currentOptions.options,
|
|
265
|
+
onEvent: applyViewerEvent,
|
|
266
|
+
});
|
|
267
|
+
const cancel = () => {
|
|
268
|
+
abortController === null || abortController === void 0 ? void 0 : abortController.abort();
|
|
269
|
+
abortController = null;
|
|
270
|
+
};
|
|
271
|
+
const loadSource = async (nextSource) => {
|
|
272
|
+
var _a;
|
|
273
|
+
cancel();
|
|
274
|
+
currentSource = nextSource;
|
|
275
|
+
abortController = typeof AbortController !== 'undefined' ? new AbortController() : null;
|
|
276
|
+
const controller = abortController;
|
|
277
|
+
try {
|
|
278
|
+
state.loading = true;
|
|
279
|
+
state.error = null;
|
|
280
|
+
notifyState();
|
|
281
|
+
const resolvedSource = await resolveViewerLoadSource(nextSource, {
|
|
282
|
+
fetchFile: coreOptions.fetchFile,
|
|
283
|
+
signal: controller === null || controller === void 0 ? void 0 : controller.signal,
|
|
284
|
+
});
|
|
285
|
+
return await instance.load(resolvedSource);
|
|
286
|
+
}
|
|
287
|
+
catch (error) {
|
|
288
|
+
if (isAbortError(error) && (controller === null || controller === void 0 ? void 0 : controller.signal.aborted)) {
|
|
289
|
+
return null;
|
|
290
|
+
}
|
|
291
|
+
state.loading = false;
|
|
292
|
+
state.ready = false;
|
|
293
|
+
state.error = error;
|
|
294
|
+
notifyState();
|
|
295
|
+
(_a = coreOptions.onError) === null || _a === void 0 ? void 0 : _a.call(coreOptions, error, nextSource);
|
|
296
|
+
throw error;
|
|
297
|
+
}
|
|
298
|
+
finally {
|
|
299
|
+
if (abortController === controller) {
|
|
300
|
+
abortController = null;
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
};
|
|
304
|
+
if (currentSource) {
|
|
305
|
+
void loadSource(currentSource);
|
|
306
|
+
}
|
|
307
|
+
const controller = {
|
|
308
|
+
container,
|
|
309
|
+
async load(nextOptions) {
|
|
310
|
+
if (disposed)
|
|
311
|
+
return;
|
|
312
|
+
currentOptions = nextOptions;
|
|
313
|
+
instance.updateOptions(currentOptions.options || {});
|
|
314
|
+
if (hasSource(currentOptions)) {
|
|
315
|
+
await loadSource(toViewerSourceInput(currentOptions));
|
|
316
|
+
}
|
|
317
|
+
},
|
|
318
|
+
async update(nextOptions = {}) {
|
|
319
|
+
var _a;
|
|
320
|
+
if (disposed)
|
|
321
|
+
return;
|
|
322
|
+
currentOptions = {
|
|
323
|
+
...currentOptions,
|
|
324
|
+
...nextOptions,
|
|
325
|
+
options: (_a = nextOptions.options) !== null && _a !== void 0 ? _a : currentOptions.options,
|
|
326
|
+
};
|
|
327
|
+
instance.updateOptions(currentOptions.options || {});
|
|
328
|
+
if (hasSource(currentOptions)) {
|
|
329
|
+
await loadSource(toViewerSourceInput(currentOptions));
|
|
330
|
+
}
|
|
331
|
+
else {
|
|
332
|
+
currentSource = null;
|
|
333
|
+
await instance.load({ filename: DEFAULT_FILE_VIEWER_SOURCE_FILENAME });
|
|
334
|
+
}
|
|
335
|
+
},
|
|
336
|
+
async reload() {
|
|
337
|
+
if (disposed)
|
|
338
|
+
return;
|
|
339
|
+
if (currentSource) {
|
|
340
|
+
await loadSource(currentSource);
|
|
341
|
+
}
|
|
342
|
+
},
|
|
343
|
+
destroy() {
|
|
344
|
+
if (disposed)
|
|
345
|
+
return;
|
|
346
|
+
disposed = true;
|
|
347
|
+
cancel();
|
|
348
|
+
void instance.destroy('component-unmount');
|
|
349
|
+
container.innerHTML = '';
|
|
350
|
+
},
|
|
351
|
+
getApi() {
|
|
352
|
+
return instance;
|
|
353
|
+
},
|
|
354
|
+
downloadOriginalFile() {
|
|
355
|
+
return callApi(instance, api => api.download(), undefined);
|
|
356
|
+
},
|
|
357
|
+
printRenderedHtml() {
|
|
358
|
+
return callApi(instance, api => api.print(), undefined);
|
|
359
|
+
},
|
|
360
|
+
exportRenderedHtml() {
|
|
361
|
+
return callApi(instance, api => api.exportHtml({ download: true }).then(() => undefined), undefined);
|
|
362
|
+
},
|
|
363
|
+
zoomIn() {
|
|
364
|
+
return callApi(instance, api => api.zoomIn(), null);
|
|
365
|
+
},
|
|
366
|
+
zoomOut() {
|
|
367
|
+
return callApi(instance, api => api.zoomOut(), null);
|
|
368
|
+
},
|
|
369
|
+
resetZoom() {
|
|
370
|
+
return callApi(instance, api => api.resetZoom(), null);
|
|
371
|
+
},
|
|
372
|
+
searchDocument(query) {
|
|
373
|
+
return callApi(instance, api => api.search(query), null);
|
|
374
|
+
},
|
|
375
|
+
clearDocumentSearch() {
|
|
376
|
+
return callApi(instance, api => api.clearSearch(), null);
|
|
377
|
+
},
|
|
378
|
+
nextSearchResult() {
|
|
379
|
+
return callApi(instance, api => api.nextSearchResult(), null);
|
|
380
|
+
},
|
|
381
|
+
previousSearchResult() {
|
|
382
|
+
return callApi(instance, api => api.previousSearchResult(), null);
|
|
383
|
+
},
|
|
384
|
+
collectDocumentAnchors() {
|
|
385
|
+
return callApi(instance, api => api.collectDocumentAnchors(), []);
|
|
386
|
+
},
|
|
387
|
+
scrollToAnchor(anchor) {
|
|
388
|
+
return callApi(instance, api => api.scrollToDocumentAnchor(anchor), false);
|
|
389
|
+
},
|
|
390
|
+
scrollToLine(line) {
|
|
391
|
+
return callApi(instance, api => api.scrollToLine(line), false);
|
|
392
|
+
},
|
|
393
|
+
getDocumentTextChunks() {
|
|
394
|
+
return instance.getDocumentTextChunks();
|
|
395
|
+
},
|
|
396
|
+
getOperationAvailability() {
|
|
397
|
+
return instance.getCapabilities();
|
|
398
|
+
},
|
|
399
|
+
getZoomState() {
|
|
400
|
+
return instance.getZoomState();
|
|
401
|
+
},
|
|
402
|
+
getSearchState() {
|
|
403
|
+
return instance.getSearchState();
|
|
404
|
+
},
|
|
405
|
+
getState() {
|
|
406
|
+
return snapshotState();
|
|
407
|
+
},
|
|
408
|
+
subscribe(listener) {
|
|
409
|
+
listeners.add(listener);
|
|
410
|
+
listener(snapshotState());
|
|
411
|
+
return () => {
|
|
412
|
+
listeners.delete(listener);
|
|
413
|
+
};
|
|
414
|
+
},
|
|
415
|
+
};
|
|
416
|
+
return controller;
|
|
417
|
+
};
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { type ViewerController, type ViewerControllerHandle, type ViewerMountOptions, type ViewerCoreOptions } from './controller.js';
|
|
2
|
+
export type { FileRef, ViewerAiOptions, ViewerArchiveOptions, ViewerCadOptions, ViewerController, ViewerControllerAccessor, ViewerControllerHandle, ViewerDocxOptions, ViewerEvent, ViewerEventHandler, ViewerEventType, ViewerFetchFile, ViewerFetchInput, ViewerMountOptions, ViewerOptions, ViewerPdfOptions, ViewerSpreadsheetOptions, ViewerSearchOptions, ViewerSourceInput, ViewerThemeMode, ViewerToolbarOptions, ViewerToolbarPosition, ViewerTypstOptions, ViewerWatermarkOptions, ViewerLifecycleContext, ViewerOperationContext, ViewerState, ViewerStateListener } from './controller.js';
|
|
3
|
+
export interface FileViewerSvelteActionOptions extends ViewerMountOptions {
|
|
4
|
+
/**
|
|
5
|
+
* Clear the container before mounting a fresh native viewer. Enabled by default.
|
|
6
|
+
*/
|
|
7
|
+
replace?: boolean;
|
|
8
|
+
}
|
|
9
|
+
export interface FileViewerSvelteActionReturn {
|
|
10
|
+
update(options?: FileViewerSvelteActionOptions): void;
|
|
11
|
+
destroy(): void;
|
|
12
|
+
getController: ViewerControllerHandle['getController'];
|
|
13
|
+
getApi: ViewerControllerHandle['getApi'];
|
|
14
|
+
load: ViewerControllerHandle['load'];
|
|
15
|
+
reload: ViewerControllerHandle['reload'];
|
|
16
|
+
downloadOriginalFile: ViewerControllerHandle['downloadOriginalFile'];
|
|
17
|
+
printRenderedHtml: ViewerControllerHandle['printRenderedHtml'];
|
|
18
|
+
exportRenderedHtml: ViewerControllerHandle['exportRenderedHtml'];
|
|
19
|
+
zoomIn: ViewerControllerHandle['zoomIn'];
|
|
20
|
+
zoomOut: ViewerControllerHandle['zoomOut'];
|
|
21
|
+
resetZoom: ViewerControllerHandle['resetZoom'];
|
|
22
|
+
searchDocument: ViewerControllerHandle['searchDocument'];
|
|
23
|
+
clearDocumentSearch: ViewerControllerHandle['clearDocumentSearch'];
|
|
24
|
+
nextSearchResult: ViewerControllerHandle['nextSearchResult'];
|
|
25
|
+
previousSearchResult: ViewerControllerHandle['previousSearchResult'];
|
|
26
|
+
collectDocumentAnchors: ViewerControllerHandle['collectDocumentAnchors'];
|
|
27
|
+
scrollToAnchor: ViewerControllerHandle['scrollToAnchor'];
|
|
28
|
+
scrollToLine: ViewerControllerHandle['scrollToLine'];
|
|
29
|
+
getDocumentTextChunks: ViewerControllerHandle['getDocumentTextChunks'];
|
|
30
|
+
getOperationAvailability: ViewerControllerHandle['getOperationAvailability'];
|
|
31
|
+
getZoomState: ViewerControllerHandle['getZoomState'];
|
|
32
|
+
getSearchState: ViewerControllerHandle['getSearchState'];
|
|
33
|
+
getState: ViewerControllerHandle['getState'];
|
|
34
|
+
subscribe: ViewerControllerHandle['subscribe'];
|
|
35
|
+
}
|
|
36
|
+
export declare const mountViewer: (container: HTMLElement, options?: ViewerMountOptions, coreOptions?: ViewerCoreOptions) => ViewerController;
|
|
37
|
+
export declare const fileViewer: (node: HTMLElement, initialOptions?: FileViewerSvelteActionOptions) => FileViewerSvelteActionReturn;
|
|
38
|
+
export default fileViewer;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { createViewerControllerHandle, mountViewer as mountCoreViewer } from './controller.js';
|
|
2
|
+
import { fileViewerCoreRendererRegistry } from '@file-viewer/core';
|
|
3
|
+
const canUseDom = () => typeof window !== 'undefined' && typeof document !== 'undefined';
|
|
4
|
+
const clearContainer = (node) => {
|
|
5
|
+
node.innerHTML = '';
|
|
6
|
+
};
|
|
7
|
+
export const mountViewer = (container, options = {}, coreOptions = {}) => mountCoreViewer(container, options, {
|
|
8
|
+
registry: fileViewerCoreRendererRegistry,
|
|
9
|
+
...coreOptions
|
|
10
|
+
});
|
|
11
|
+
export const fileViewer = (node, initialOptions = {}) => {
|
|
12
|
+
let options = initialOptions;
|
|
13
|
+
let controller = null;
|
|
14
|
+
const handle = createViewerControllerHandle(() => controller, () => {
|
|
15
|
+
controller === null || controller === void 0 ? void 0 : controller.destroy();
|
|
16
|
+
controller = null;
|
|
17
|
+
if (options.replace !== false) {
|
|
18
|
+
clearContainer(node);
|
|
19
|
+
}
|
|
20
|
+
});
|
|
21
|
+
const mount = () => {
|
|
22
|
+
if (!canUseDom()) {
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
if (controller) {
|
|
26
|
+
if (options.replace === false) {
|
|
27
|
+
controller.update(options);
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
controller.destroy();
|
|
31
|
+
controller = null;
|
|
32
|
+
}
|
|
33
|
+
if (options.replace !== false) {
|
|
34
|
+
clearContainer(node);
|
|
35
|
+
}
|
|
36
|
+
const { replace: _replace, ...viewerOptions } = options;
|
|
37
|
+
controller = mountViewer(node, viewerOptions);
|
|
38
|
+
};
|
|
39
|
+
mount();
|
|
40
|
+
return {
|
|
41
|
+
update(nextOptions = {}) {
|
|
42
|
+
options = { ...options, ...nextOptions };
|
|
43
|
+
if (controller) {
|
|
44
|
+
const { replace: _replace, ...viewerOptions } = options;
|
|
45
|
+
controller.update(viewerOptions);
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
mount();
|
|
49
|
+
},
|
|
50
|
+
destroy() {
|
|
51
|
+
handle.destroy();
|
|
52
|
+
},
|
|
53
|
+
getController: handle.getController,
|
|
54
|
+
getApi: handle.getApi,
|
|
55
|
+
load: handle.load,
|
|
56
|
+
reload: handle.reload,
|
|
57
|
+
downloadOriginalFile: handle.downloadOriginalFile,
|
|
58
|
+
printRenderedHtml: handle.printRenderedHtml,
|
|
59
|
+
exportRenderedHtml: handle.exportRenderedHtml,
|
|
60
|
+
zoomIn: handle.zoomIn,
|
|
61
|
+
zoomOut: handle.zoomOut,
|
|
62
|
+
resetZoom: handle.resetZoom,
|
|
63
|
+
searchDocument: handle.searchDocument,
|
|
64
|
+
clearDocumentSearch: handle.clearDocumentSearch,
|
|
65
|
+
nextSearchResult: handle.nextSearchResult,
|
|
66
|
+
previousSearchResult: handle.previousSearchResult,
|
|
67
|
+
collectDocumentAnchors: handle.collectDocumentAnchors,
|
|
68
|
+
scrollToAnchor: handle.scrollToAnchor,
|
|
69
|
+
scrollToLine: handle.scrollToLine,
|
|
70
|
+
getDocumentTextChunks: handle.getDocumentTextChunks,
|
|
71
|
+
getOperationAvailability: handle.getOperationAvailability,
|
|
72
|
+
getZoomState: handle.getZoomState,
|
|
73
|
+
getSearchState: handle.getSearchState,
|
|
74
|
+
getState: handle.getState,
|
|
75
|
+
subscribe: handle.subscribe
|
|
76
|
+
};
|
|
77
|
+
};
|
|
78
|
+
export default fileViewer;
|
package/package.json
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@file-viewer/svelte",
|
|
3
|
+
"version": "2.0.0",
|
|
4
|
+
"private": false,
|
|
5
|
+
"type": "module",
|
|
6
|
+
"description": "Standard Svelte component package for Flyfish File Viewer",
|
|
7
|
+
"keywords": [
|
|
8
|
+
"file-viewer",
|
|
9
|
+
"document-preview",
|
|
10
|
+
"svelte",
|
|
11
|
+
"sveltekit",
|
|
12
|
+
"native-viewer",
|
|
13
|
+
"office-preview"
|
|
14
|
+
],
|
|
15
|
+
"svelte": "./src/FileViewer.svelte",
|
|
16
|
+
"main": "./src/FileViewer.svelte",
|
|
17
|
+
"module": "./src/FileViewer.svelte",
|
|
18
|
+
"publishConfig": {
|
|
19
|
+
"access": "public",
|
|
20
|
+
"registry": "https://registry.npmjs.org/"
|
|
21
|
+
},
|
|
22
|
+
"author": {
|
|
23
|
+
"name": "Wangyu",
|
|
24
|
+
"email": "wybaby168@gmail.com"
|
|
25
|
+
},
|
|
26
|
+
"repository": {
|
|
27
|
+
"type": "git",
|
|
28
|
+
"url": "git+https://github.com/flyfish-dev/file-viewer-svelte.git",
|
|
29
|
+
"directory": "packages/components/svelte"
|
|
30
|
+
},
|
|
31
|
+
"homepage": "https://doc.flyfish-viewer.app/",
|
|
32
|
+
"bugs": {
|
|
33
|
+
"url": "https://github.com/flyfish-dev/file-viewer-svelte/issues"
|
|
34
|
+
},
|
|
35
|
+
"types": "./src/FileViewer.svelte.d.ts",
|
|
36
|
+
"exports": {
|
|
37
|
+
".": {
|
|
38
|
+
"types": "./src/FileViewer.svelte.d.ts",
|
|
39
|
+
"svelte": "./src/FileViewer.svelte",
|
|
40
|
+
"import": "./src/FileViewer.svelte",
|
|
41
|
+
"default": "./src/FileViewer.svelte"
|
|
42
|
+
},
|
|
43
|
+
"./action": {
|
|
44
|
+
"types": "./dist/index.d.ts",
|
|
45
|
+
"import": "./dist/index.js",
|
|
46
|
+
"default": "./dist/index.js"
|
|
47
|
+
},
|
|
48
|
+
"./types": {
|
|
49
|
+
"types": "./dist/index.d.ts",
|
|
50
|
+
"import": "./dist/index.js",
|
|
51
|
+
"default": "./dist/index.js"
|
|
52
|
+
},
|
|
53
|
+
"./package.json": "./package.json"
|
|
54
|
+
},
|
|
55
|
+
"files": [
|
|
56
|
+
"dist",
|
|
57
|
+
"src/FileViewer.svelte",
|
|
58
|
+
"src/FileViewer.svelte.d.ts",
|
|
59
|
+
"src/controller.ts",
|
|
60
|
+
"README.md",
|
|
61
|
+
"README.en.md"
|
|
62
|
+
],
|
|
63
|
+
"peerDependencies": {
|
|
64
|
+
"svelte": ">=3.59 <6"
|
|
65
|
+
},
|
|
66
|
+
"dependencies": {
|
|
67
|
+
"@file-viewer/core": "^2.0.0"
|
|
68
|
+
},
|
|
69
|
+
"devDependencies": {
|
|
70
|
+
"svelte": "^5.46.0",
|
|
71
|
+
"typescript": "^6.0.3"
|
|
72
|
+
},
|
|
73
|
+
"license": "Apache-2.0",
|
|
74
|
+
"scripts": {
|
|
75
|
+
"build": "tsc -b tsconfig.json",
|
|
76
|
+
"type-check": "tsc -b tsconfig.json"
|
|
77
|
+
}
|
|
78
|
+
}
|