Skip to content

Commit f54fecf

Browse files
committed
temp
1 parent 2e7fe6e commit f54fecf

File tree

8 files changed

+48
-97
lines changed

8 files changed

+48
-97
lines changed

routers/web/repo/setting/lfs.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -309,8 +309,6 @@ func LFSFileGet(ctx *context.Context) {
309309
}
310310
ctx.Data["LineNums"] = gotemplate.HTML(output.String())
311311

312-
case st.IsPDF():
313-
ctx.Data["IsPDFFile"] = true
314312
case st.IsVideo():
315313
ctx.Data["IsVideoFile"] = true
316314
case st.IsAudio():

routers/web/repo/view_file.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,8 +237,6 @@ func prepareToRenderFile(ctx *context.Context, entry *git.TreeEntry) {
237237
ctx.Data["LineEscapeStatus"] = statuses
238238
}
239239

240-
case fInfo.st.IsPDF():
241-
ctx.Data["IsPDFFile"] = true
242240
case fInfo.st.IsVideo():
243241
ctx.Data["IsVideoFile"] = true
244242
case fInfo.st.IsAudio():

templates/repo/settings/lfs_file.tmpl

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,8 @@
3030
<audio controls src="{{$.RawFileLink}}">
3131
<strong>{{ctx.Locale.Tr "repo.audio_not_supported_in_browser"}}</strong>
3232
</audio>
33-
{{else if .IsPDFFile}}
34-
<div class="pdf-content is-loading" data-global-init="initPdfViewer" data-src="{{$.RawFileLink}}" data-fallback-button-text="{{ctx.Locale.Tr "diff.view_file"}}"></div>
3533
{{else}}
36-
<a href="{{$.RawFileLink}}" rel="nofollow" class="tw-p-4">{{ctx.Locale.Tr "repo.file_view_raw"}}</a>
34+
{{template "shared/repo/fileviewrender" dict "RawFileLink" $.RawFileLink}}
3735
{{end}}
3836
</div>
3937
{{else if .FileSize}}

templates/repo/view_file.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@
108108
<strong>{{ctx.Locale.Tr "repo.audio_not_supported_in_browser"}}</strong>
109109
</audio>
110110
{{else}}
111-
<div class="file-view-container" data-global-init="initFileView" data-tree-path="{{.TreePath}}" data-raw-file-link="{{$.RawFileLink}}" data-fallback-text="{{ctx.Locale.Tr "repo.file_view_raw"}}" data-error-header="{{ctx.Locale.Tr "repo.file_render_failed"}}"></div>
111+
{{template "shared/repo/fileviewrender" dict "RawFileLink" $.RawFileLink}}
112112
{{end}}
113113
</div>
114114
{{else if .FileSize}}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<div class="file-view-render-container" data-global-init="initFileViewRender"
2+
data-raw-file-link="{{$.RawFileLink}}"
3+
data-fallback-text="{{ctx.Locale.Tr "repo.file_view_raw"}}"
4+
data-error-header="{{ctx.Locale.Tr "repo.file_render_failed"}}">
5+
<a class="file-view-raw-prompt" href="{{$.RawFileLink}}" rel="nofollow" class="tw-p-4">{{ctx.Locale.Tr "repo.file_view_raw"}}</a>
6+
</div>

web_src/js/features/file-view.ts

Lines changed: 30 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,42 @@
11
import {applyRenderPlugin} from '../modules/file-render-plugin.ts';
22
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';
47

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;
1610

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');
1921

22+
let rendered = false, errorMsg = '';
2023
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+
}
2628

27-
// try to apply render plugin
28-
const success = await applyRenderPlugin(container);
29+
if (rendered) {
30+
elViewRawPrompt.remove();
31+
return;
32+
}
2933

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);
4736

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);
6640
}
6741
});
6842
}

web_src/js/index.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ import {initRepoIssueContentHistory} from './features/repo-issue-content.ts';
1919
import {initStopwatch} from './features/stopwatch.ts';
2020
import {initFindFileInRepo} from './features/repo-findfile.ts';
2121
import {initMarkupContent} from './markup/content.ts';
22-
import {initFileView} from './features/file-view.ts';
23-
import {register3DViewerPlugin} from './render/plugins/3d-viewer.ts';
22+
import {initFileViewRender} from './features/file-view.ts';
2423
import {initUserAuthOauth2, initUserCheckAppUrl} from './features/user-auth.ts';
2524
import {initRepoPullRequestAllowMaintainerEdit, initRepoPullRequestReview, initRepoIssueSidebarDependency, initRepoIssueFilterItemLabel} from './features/repo-issue.ts';
2625
import {initRepoEllipsisButton, initCommitStatuses} from './features/repo-commit.ts';
@@ -66,7 +65,6 @@ import {initGlobalButtonClickOnEnter, initGlobalButtons, initGlobalDeleteButton}
6665
import {initGlobalComboMarkdownEditor, initGlobalEnterQuickSubmit, initGlobalFormDirtyLeaveConfirm} from './features/common-form.ts';
6766
import {callInitFunctions} from './modules/init.ts';
6867
import {initRepoViewFileTree} from './features/repo-view-file-tree.ts';
69-
import {registerPdfViewerPlugin} from './render/plugins/pdf-viewer.ts';
7068

7169
initGiteaFomantic();
7270
initSubmitEventPolyfill();
@@ -165,9 +163,7 @@ onDomReady(() => {
165163

166164
initOAuth2SettingsDisableCheckbox,
167165

168-
register3DViewerPlugin,
169-
registerPdfViewerPlugin,
170-
initFileView,
166+
initFileViewRender,
171167
]);
172168

173169
// it must be the last one, then the "querySelectorAll" only needs to be executed once for global init functions.

web_src/js/modules/file-render-plugin.ts

Lines changed: 8 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,11 @@
44
* This module provides a plugin architecture for rendering different file types
55
* in the browser without requiring backend support for identifying file types.
66
*/
7-
8-
/**
9-
* Interface for file render plugins
10-
*/
117
export type FileRenderPlugin = {
128
// unique plugin name
139
name: string;
1410

15-
// test if plugin can handle specified file
11+
// test if plugin can handle a specified file
1612
canHandle: (filename: string, mimeType: string) => boolean;
1713

1814
// render file content
@@ -39,31 +35,16 @@ function findPlugin(filename: string, mimeType: string): FileRenderPlugin | null
3935
/**
4036
* apply render plugin to specified container
4137
*/
42-
export async function applyRenderPlugin(container: HTMLElement): Promise<boolean> {
38+
export async function applyRenderPlugin(container: HTMLElement, rawFileLink: string): Promise<boolean> {
4339
try {
44-
// get file info from container element
45-
const treePath = container.getAttribute('data-tree-path') || '';
46-
const fileLink = container.getAttribute('data-raw-file-link') || '';
47-
48-
if (!treePath || !fileLink) {
49-
console.warn('Missing file name or file URL for renderer');
50-
return false;
51-
}
52-
53-
// get mime type (optional)
5440
const mimeType = container.getAttribute('data-mime-type') || '';
41+
const plugin = findPlugin(rawFileLink, mimeType);
42+
if (!plugin) return false;
5543

56-
// find plugin that can handle this file
57-
const plugin = findPlugin(treePath, mimeType);
58-
if (!plugin) {
59-
return false;
60-
}
61-
62-
// apply plugin to render file
63-
await plugin.render(container, fileLink);
44+
container.classList.add('is-loading');
45+
await plugin.render(container, rawFileLink);
6446
return true;
65-
} catch (error) {
66-
console.error('Error applying render plugin:', error);
67-
return false;
47+
} finally {
48+
container.classList.remove('is-loading');
6849
}
6950
}

0 commit comments

Comments
 (0)