|
1 | 1 | import {applyRenderPlugin} from '../modules/file-render-plugin.ts'; |
2 | 2 | import {registerGlobalInitFunc} from '../modules/observer.ts'; |
3 | | -import {createElementFromAttrs} from '../utils/dom.ts'; |
| 3 | +import {createElementFromHTML} from '../utils/dom.ts'; |
| 4 | +import {register3DViewerPlugin} from '../render/plugins/3d-viewer.ts'; |
| 5 | +import {registerPdfViewerPlugin} from '../render/plugins/pdf-viewer.ts'; |
| 6 | +import {htmlEscape} from 'escape-goat'; |
4 | 7 |
|
5 | | -/** |
6 | | - * init file view renderer |
7 | | - * |
8 | | - * detect renderable files and apply appropriate plugins |
9 | | - */ |
10 | | -export function initFileView(): void { |
11 | | - // register file view renderer init function |
12 | | - registerGlobalInitFunc('initFileView', async (container: HTMLElement) => { |
13 | | - // get file info |
14 | | - const treePath = container.getAttribute('data-tree-path'); |
15 | | - const fileLink = container.getAttribute('data-raw-file-link'); |
| 8 | +export function initFileViewRender(): void { |
| 9 | + let pluginRegistered = false; |
16 | 10 |
|
17 | | - // mark loading state |
18 | | - container.classList.add('is-loading'); |
| 11 | + registerGlobalInitFunc('initFileViewRender', async (container: HTMLElement) => { |
| 12 | + if (!pluginRegistered) { |
| 13 | + pluginRegistered = true; |
| 14 | + register3DViewerPlugin(); |
| 15 | + registerPdfViewerPlugin(); |
| 16 | + } |
| 17 | + |
| 18 | + const rawFileLink = container.getAttribute('data-raw-file-link'); |
| 19 | + const elViewRawPrompt = container.querySelector('.file-view-raw-prompt'); |
| 20 | + if (!rawFileLink || !elViewRawPrompt) throw new Error('unexpected file view container'); |
19 | 21 |
|
| 22 | + let rendered = false, errorMsg = ''; |
20 | 23 | try { |
21 | | - // check if filename and url exist |
22 | | - if (!treePath || !fileLink) { |
23 | | - console.error(`missing file name(${treePath}) or file url(${fileLink}) for rendering`); |
24 | | - throw new Error('missing necessary file info'); |
25 | | - } |
| 24 | + rendered = await applyRenderPlugin(container); |
| 25 | + } catch (e) { |
| 26 | + errorMsg = `${e}`; |
| 27 | + } |
26 | 28 |
|
27 | | - // try to apply render plugin |
28 | | - const success = await applyRenderPlugin(container); |
| 29 | + if (rendered) { |
| 30 | + elViewRawPrompt.remove(); |
| 31 | + return; |
| 32 | + } |
29 | 33 |
|
30 | | - // if no suitable plugin is found, show default view |
31 | | - if (!success) { |
32 | | - // show default view raw file link |
33 | | - const fallbackText = container.getAttribute('data-fallback-text'); |
34 | | - const wrapper = createElementFromAttrs( |
35 | | - 'div', |
36 | | - {class: 'view-raw-fallback'}, |
37 | | - createElementFromAttrs('a', { |
38 | | - class: 'ui basic button', |
39 | | - target: '_blank', |
40 | | - href: fileLink, |
41 | | - }, fallbackText || ''), |
42 | | - ); |
43 | | - container.replaceChildren(wrapper); |
44 | | - } |
45 | | - } catch (error) { |
46 | | - console.error('file view init error:', error); |
| 34 | + // remove all children from the container, and only show the raw file link |
| 35 | + container.replaceChildren(elViewRawPrompt); |
47 | 36 |
|
48 | | - // show error message |
49 | | - const fallbackText = container.getAttribute('data-fallback-text'); |
50 | | - const errorHeader = container.getAttribute('data-error-header'); |
51 | | - const errorDiv = createElementFromAttrs( |
52 | | - 'div', |
53 | | - {class: 'ui error message'}, |
54 | | - createElementFromAttrs('div', {class: 'header'}, errorHeader || ''), |
55 | | - createElementFromAttrs('pre', null, JSON.stringify({treePath, fileLink}, null, 2)), |
56 | | - createElementFromAttrs('a', { |
57 | | - class: 'ui basic button', |
58 | | - href: fileLink || '#', |
59 | | - target: '_blank', |
60 | | - }, fallbackText || ''), |
61 | | - ); |
62 | | - container.replaceChildren(errorDiv); |
63 | | - } finally { |
64 | | - // remove loading state |
65 | | - container.classList.remove('is-loading'); |
| 37 | + if (errorMsg) { |
| 38 | + const elErrorMessage = createElementFromHTML(htmlEscape`<div class="ui error message">${errorMsg}</div>`); |
| 39 | + container.insertBefore(elErrorMessage, elViewRawPrompt); |
66 | 40 | } |
67 | 41 | }); |
68 | 42 | } |
0 commit comments