@file-viewer/web 2.1.9 → 2.1.10

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.
@@ -1,5 +1,5 @@
1
1
  import { createViewer } from '@file-viewer/core';
2
- import { DEFAULT_FILE_VIEWER_SOURCE_FILENAME, getExtension, normalizeFilename, readFileViewerBuffer, resolveFileViewerSourceFilename, wrapFileViewerFileRef, } from '@file-viewer/core';
2
+ import { DEFAULT_FILE_VIEWER_SOURCE_FILENAME, createFileViewerTranslator, getExtension, hasVisibleFileViewerToolbarActions, isFileViewerZoomButtonDisabled, normalizeFileViewerToolbar, normalizeFilename, readFileViewerBuffer, resolveFileViewerSourceFilename, resolveFileViewerToolbarPosition, resolveVisibleFileViewerToolbar, wrapFileViewerFileRef, } from '@file-viewer/core';
3
3
  const isBrowser = () => typeof window !== 'undefined' && typeof document !== 'undefined';
4
4
  const hasSource = (options = {}) => {
5
5
  return !!(options.url || options.file || options.buffer);
@@ -186,10 +186,68 @@ const callApi = async (api, action, fallback) => {
186
186
  const isAbortError = (error) => {
187
187
  return Boolean(error && typeof error === 'object' && error.name === 'AbortError');
188
188
  };
189
+ const DEFAULT_TOOLBAR_AVAILABILITY = {
190
+ download: false,
191
+ print: false,
192
+ exportHtml: false,
193
+ zoom: false,
194
+ zoomIn: false,
195
+ zoomOut: false,
196
+ zoomReset: false,
197
+ };
198
+ const DEFAULT_TOOLBAR_ZOOM_STATE = {
199
+ scale: 1,
200
+ label: '100%',
201
+ canZoomIn: false,
202
+ canZoomOut: false,
203
+ canReset: false,
204
+ };
205
+ const WEB_VIEWER_STYLE = `
206
+ .file-viewer-web-shell{position:relative;width:100%;height:100%;min-height:0;display:flex;flex-direction:column;background:transparent}
207
+ .file-viewer-web-content{position:relative;flex:1 1 auto;min-height:0;min-width:0}
208
+ .file-viewer-web-toolbar{flex:0 0 auto;min-height:45px;display:inline-flex;align-items:center;justify-content:flex-end;gap:6px;padding:6px 10px;border-bottom:1px solid rgba(20,35,53,.06);background:rgba(255,255,255,.92);box-sizing:border-box;z-index:20}
209
+ .file-viewer-web-toolbar[hidden]{display:none!important}
210
+ .file-viewer-web-toolbar[data-toolbar-position="bottom-right"]{position:absolute;right:calc(16px + env(safe-area-inset-right,0px));bottom:calc(16px + env(safe-area-inset-bottom,0px));min-height:42px;padding:6px;border:1px solid rgba(20,35,53,.1);border-radius:999px;background:rgba(255,255,255,.94);box-shadow:0 18px 44px rgba(15,23,42,.16);backdrop-filter:blur(16px)}
211
+ .file-viewer-web-toolbar-group{display:inline-flex;align-items:center;gap:2px;padding:2px;border:1px solid rgba(20,35,53,.08);border-radius:999px;background:rgba(20,35,53,.035)}
212
+ .file-viewer-web-toolbar button{min-width:42px;height:30px;padding:0 10px;border:0;border-radius:8px;background:transparent;color:#40546a;font:inherit;font-size:12px;font-weight:800;line-height:1;white-space:nowrap;cursor:pointer}
213
+ .file-viewer-web-toolbar button:hover:not(:disabled){background:rgba(33,163,102,.1);color:#16774c}
214
+ .file-viewer-web-toolbar button:disabled{color:#aab5c0;cursor:not-allowed}
215
+ .file-viewer-web-toolbar .file-viewer-web-icon-button{width:30px;min-width:30px;padding:0;display:inline-flex;align-items:center;justify-content:center}
216
+ .file-viewer-web-toolbar .file-viewer-web-zoom-meter{min-width:48px;padding:0 8px;color:#23465e}
217
+ .file-viewer-web-toolbar[data-toolbar-position="bottom-right"] button{min-width:48px;height:32px;border-radius:999px}
218
+ .file-viewer-web-toolbar[data-toolbar-position="bottom-right"] .file-viewer-web-icon-button{width:32px;min-width:32px}
219
+ .file-viewer-web-toolbar[data-toolbar-position="bottom-right"] .file-viewer-web-zoom-meter{min-width:54px}
220
+ .file-viewer-web-shell[data-viewer-theme='dark'] .file-viewer-web-toolbar{border-color:rgba(148,163,184,.18);background:rgba(15,23,42,.9)}
221
+ .file-viewer-web-shell[data-viewer-theme='dark'] .file-viewer-web-toolbar button{color:#d7dee8}
222
+ @media (prefers-color-scheme:dark){.file-viewer-web-shell[data-viewer-theme='system'] .file-viewer-web-toolbar{border-color:rgba(148,163,184,.18);background:rgba(15,23,42,.9)}.file-viewer-web-shell[data-viewer-theme='system'] .file-viewer-web-toolbar button{color:#d7dee8}}
223
+ `;
224
+ const createButton = (documentRef, label, className, onClick) => {
225
+ const button = documentRef.createElement('button');
226
+ button.type = 'button';
227
+ button.className = className;
228
+ button.textContent = label;
229
+ button.title = label;
230
+ button.setAttribute('aria-label', label);
231
+ button.addEventListener('click', () => {
232
+ void onClick();
233
+ });
234
+ return button;
235
+ };
189
236
  export const mountViewer = (container, initialOptions = {}, coreOptions = {}) => {
190
237
  if (!isBrowser()) {
191
238
  throw new Error('Flyfish File Viewer can only be mounted in a browser DOM environment.');
192
239
  }
240
+ const documentRef = container.ownerDocument;
241
+ const styleEl = documentRef.createElement('style');
242
+ styleEl.textContent = WEB_VIEWER_STYLE;
243
+ const shell = documentRef.createElement('div');
244
+ shell.className = 'file-viewer-web-shell';
245
+ const toolbarEl = documentRef.createElement('div');
246
+ toolbarEl.className = 'file-viewer-web-toolbar';
247
+ const contentEl = documentRef.createElement('div');
248
+ contentEl.className = 'file-viewer-web-content';
249
+ shell.append(toolbarEl, contentEl);
250
+ container.replaceChildren(styleEl, shell);
193
251
  let disposed = false;
194
252
  let currentOptions = initialOptions;
195
253
  let currentSource = hasSource(currentOptions)
@@ -214,9 +272,119 @@ export const mountViewer = (container, initialOptions = {}, coreOptions = {}) =>
214
272
  ? { ...state.search, matches: [...state.search.matches] }
215
273
  : null,
216
274
  });
275
+ const getCurrentExtension = () => {
276
+ var _a;
277
+ if ((_a = state.lifecycle) === null || _a === void 0 ? void 0 : _a.type) {
278
+ return state.lifecycle.type;
279
+ }
280
+ return currentSource ? getExtension(resolveViewerSourceFilename(currentSource)) : '';
281
+ };
282
+ let instance;
283
+ const getToolbarZoomState = () => state.zoom || instance.getZoomState() || DEFAULT_TOOLBAR_ZOOM_STATE;
284
+ const getToolbarAvailability = () => state.availability || instance.getCapabilities() || DEFAULT_TOOLBAR_AVAILABILITY;
285
+ const syncShellTheme = () => {
286
+ var _a;
287
+ shell.dataset.viewerTheme = ((_a = currentOptions.options) === null || _a === void 0 ? void 0 : _a.theme) || 'light';
288
+ };
289
+ let controller = null;
290
+ const renderToolbar = () => {
291
+ if (disposed) {
292
+ return;
293
+ }
294
+ syncShellTheme();
295
+ const options = currentOptions.options || {};
296
+ const toolbar = normalizeFileViewerToolbar(options);
297
+ const availability = getToolbarAvailability();
298
+ const visibleToolbar = resolveVisibleFileViewerToolbar(toolbar, availability);
299
+ const showToolbar = hasVisibleFileViewerToolbarActions(visibleToolbar);
300
+ const toolbarPosition = resolveFileViewerToolbarPosition(options, getCurrentExtension());
301
+ const toolbarDisabled = state.loading || !!state.error;
302
+ const zoomState = getToolbarZoomState();
303
+ const t = createFileViewerTranslator(options);
304
+ toolbarEl.hidden = !showToolbar;
305
+ toolbarEl.dataset.toolbarPosition = toolbarPosition;
306
+ toolbarEl.replaceChildren();
307
+ if (!showToolbar) {
308
+ return;
309
+ }
310
+ if (visibleToolbar.zoom) {
311
+ const group = documentRef.createElement('div');
312
+ group.className = 'file-viewer-web-toolbar-group';
313
+ group.setAttribute('aria-label', t('toolbar.zoomGroup'));
314
+ if (availability.zoomOut) {
315
+ const button = createButton(documentRef, '-', 'file-viewer-web-icon-button', () => controller === null || controller === void 0 ? void 0 : controller.zoomOut());
316
+ button.title = t('toolbar.zoomOut');
317
+ button.setAttribute('aria-label', t('toolbar.zoomOut'));
318
+ button.disabled = isFileViewerZoomButtonDisabled({
319
+ action: 'canZoomOut',
320
+ availability,
321
+ toolbarDisabled,
322
+ zoomState,
323
+ });
324
+ group.appendChild(button);
325
+ }
326
+ if (availability.zoomReset) {
327
+ const meter = createButton(documentRef, zoomState.label, 'file-viewer-web-zoom-meter', () => controller === null || controller === void 0 ? void 0 : controller.resetZoom());
328
+ meter.title = t('toolbar.zoomReset');
329
+ meter.disabled = isFileViewerZoomButtonDisabled({
330
+ action: 'canReset',
331
+ availability,
332
+ toolbarDisabled,
333
+ zoomState,
334
+ });
335
+ group.appendChild(meter);
336
+ }
337
+ if (availability.zoomIn) {
338
+ const button = createButton(documentRef, '+', 'file-viewer-web-icon-button', () => controller === null || controller === void 0 ? void 0 : controller.zoomIn());
339
+ button.title = t('toolbar.zoomIn');
340
+ button.setAttribute('aria-label', t('toolbar.zoomIn'));
341
+ button.disabled = isFileViewerZoomButtonDisabled({
342
+ action: 'canZoomIn',
343
+ availability,
344
+ toolbarDisabled,
345
+ zoomState,
346
+ });
347
+ group.appendChild(button);
348
+ }
349
+ if (availability.zoomReset) {
350
+ const button = createButton(documentRef, '1:1', 'file-viewer-web-icon-button', () => controller === null || controller === void 0 ? void 0 : controller.resetZoom());
351
+ button.title = t('toolbar.zoomReset');
352
+ button.setAttribute('aria-label', t('toolbar.zoomReset'));
353
+ button.disabled = isFileViewerZoomButtonDisabled({
354
+ action: 'canReset',
355
+ availability,
356
+ toolbarDisabled,
357
+ zoomState,
358
+ });
359
+ group.appendChild(button);
360
+ }
361
+ if (group.childElementCount) {
362
+ toolbarEl.appendChild(group);
363
+ }
364
+ }
365
+ if (visibleToolbar.download) {
366
+ const button = createButton(documentRef, t('toolbar.download'), '', () => controller === null || controller === void 0 ? void 0 : controller.downloadOriginalFile());
367
+ button.title = t('toolbar.downloadTitle');
368
+ button.disabled = toolbarDisabled;
369
+ toolbarEl.appendChild(button);
370
+ }
371
+ if (visibleToolbar.print) {
372
+ const button = createButton(documentRef, t('toolbar.print'), '', () => controller === null || controller === void 0 ? void 0 : controller.printRenderedHtml());
373
+ button.title = t('toolbar.printTitle');
374
+ button.disabled = toolbarDisabled;
375
+ toolbarEl.appendChild(button);
376
+ }
377
+ if (visibleToolbar.exportHtml) {
378
+ const button = createButton(documentRef, t('toolbar.exportHtml'), '', () => controller === null || controller === void 0 ? void 0 : controller.exportRenderedHtml());
379
+ button.title = t('toolbar.exportHtmlTitle');
380
+ button.disabled = toolbarDisabled;
381
+ toolbarEl.appendChild(button);
382
+ }
383
+ };
217
384
  const notifyState = (event) => {
218
385
  var _a;
219
386
  const snapshot = snapshotState();
387
+ renderToolbar();
220
388
  (_a = currentOptions.onStateChange) === null || _a === void 0 ? void 0 : _a.call(currentOptions, snapshot, event);
221
389
  listeners.forEach(listener => listener(snapshot, event));
222
390
  };
@@ -259,11 +427,12 @@ export const mountViewer = (container, initialOptions = {}, coreOptions = {}) =>
259
427
  (_a = currentOptions.onEvent) === null || _a === void 0 ? void 0 : _a.call(currentOptions, event);
260
428
  notifyState(event);
261
429
  };
262
- const instance = createViewer(container, {
430
+ instance = createViewer(contentEl, {
263
431
  registry: coreOptions.registry,
264
432
  options: currentOptions.options,
265
433
  onEvent: applyViewerEvent,
266
434
  });
435
+ renderToolbar();
267
436
  const cancel = () => {
268
437
  abortController === null || abortController === void 0 ? void 0 : abortController.abort();
269
438
  abortController = null;
@@ -304,13 +473,14 @@ export const mountViewer = (container, initialOptions = {}, coreOptions = {}) =>
304
473
  if (currentSource) {
305
474
  void loadSource(currentSource);
306
475
  }
307
- const controller = {
476
+ controller = {
308
477
  container,
309
478
  async load(nextOptions) {
310
479
  if (disposed)
311
480
  return;
312
481
  currentOptions = nextOptions;
313
482
  instance.updateOptions(currentOptions.options || {});
483
+ renderToolbar();
314
484
  if (hasSource(currentOptions)) {
315
485
  await loadSource(toViewerSourceInput(currentOptions));
316
486
  }
@@ -325,6 +495,7 @@ export const mountViewer = (container, initialOptions = {}, coreOptions = {}) =>
325
495
  options: (_a = nextOptions.options) !== null && _a !== void 0 ? _a : currentOptions.options,
326
496
  };
327
497
  instance.updateOptions(currentOptions.options || {});
498
+ renderToolbar();
328
499
  if (hasSource(currentOptions)) {
329
500
  await loadSource(toViewerSourceInput(currentOptions));
330
501
  }