Files
gitea/web_src/js/render/plugins/frontend-viewer-3d.ts
silverwind d5831b9385 Frontend iframe renderer framework: 3D models, OpenAPI (#37233)
Introduces a frontend external-render framework that runs renderer
plugins inside an `iframe` (loaded via `srcdoc` to keep the CSP
`sandbox` directive working without origin-related console noise), and
migrates the 3D viewer and OpenAPI/Swagger renderers onto it. PDF and
asciicast paths are refactored to share the same `data-render-name`
mechanism.

Adds e2e coverage for 3D, PDF, asciicast and OpenAPI render paths, plus
a regression for the `RefTypeNameSubURL` double-escape on non-ASCII
branch names.

Signed-off-by: silverwind <me@silverwind.io>
Co-authored-by: Claude (Opus 4.6) <noreply@anthropic.com>
Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
2026-04-17 22:30:17 +00:00

37 lines
1.3 KiB
TypeScript

import type {FrontendRenderFunc} from '../plugin.ts';
import {basename} from '../../utils.ts';
import * as OV from 'online-3d-viewer';
import {colord} from 'colord';
/* a simple text STL file example:
solid SimpleTriangle
facet normal 0 0 1
outer loop
vertex 0 0 0
vertex 1 0 0
vertex 0 1 0
endloop
endfacet
endsolid SimpleTriangle
*/
export const frontendRender: FrontendRenderFunc = async (opts): Promise<boolean> => {
try {
opts.container.style.height = `${window.innerHeight}px`;
const bgColor = colord(getComputedStyle(document.body).backgroundColor).toRgb();
const primaryColor = colord(getComputedStyle(document.documentElement).getPropertyValue('--color-primary').trim()).toRgb();
const viewer = new OV.EmbeddedViewer(opts.container, {
backgroundColor: new OV.RGBAColor(bgColor.r, bgColor.g, bgColor.b, 255),
defaultColor: new OV.RGBColor(primaryColor.r, primaryColor.g, primaryColor.b),
edgeSettings: new OV.EdgeSettings(false, new OV.RGBColor(0, 0, 0), 1),
});
const blob = new Blob([opts.contentBytes()]);
const file = new File([blob], basename(opts.treePath));
viewer.LoadModelFromFileList([file]);
return true;
} catch (error) {
console.error(error);
return false;
}
};