Sign in

Search issue titles and bodies, change requests and comments. Private repositories you cannot read are not included.

vole
Site
Clone

Read only, no credentials:

https://rodent.software/vole.git

Write over SSH with a registered key:

git clone ssh://git@rodent.software/vole.git

Self-hosted forge that lives in your Git repo.

Render images and 3D models in the browser views

3b2bcc30534f24b4628b6365e69dcb9065115065 · Keenan Nemetz · 13d ago · parent 87bab8e1

Render images and 3D models in the browser views

A blob whose name is an image is served as that image, so the <img> a README
already emitted works and the source view can show it. An SVG keeps its source
view and previews on request. The instance introduction resolves assets/, so a
landing-page picture is no longer a broken link.

A changed image or model shows both sides in the commit, compare, and change
request diffs, where git only says "binary". The 3D viewer is a vendored
three.js bundle, loaded only when a page has a model, with a download fallback
for a reader without JavaScript or WebGL.

closes: 01M7XQ9AKKKFNEX0F679JRRYQD

server/web/static/app.js +50

@@ -878,3 +878,53 @@ const fitTextarea = (area) => {
878 878 document.body.addEventListener('htmx:oobAfterSwap', read);
879 879 })();
880 880
881
882 // 3D previews. three.js and its loaders are a large module, so the viewer is
883 // imported only when the page contains a model; the server renders a download
884 // link inside every [data-model] host, which is what stays if this never loads.
885 (() => {
886 const SELECTOR = '[data-model]';
887 let viewer = null;
888 const live = new Set();
889
890 const load = () => {
891 if (!viewer) viewer = import('/-/static/models/model-viewer.js');
892 return viewer;
893 };
894
895 const hostsIn = (scope) => {
896 const hosts = [];
897 if (scope instanceof Element && scope.matches(SELECTOR)) hosts.push(scope);
898 if (scope && typeof scope.querySelectorAll === 'function') {
899 scope.querySelectorAll(SELECTOR).forEach((host) => hosts.push(host));
900 }
901 return hosts;
902 };
903
904 const upgrade = (scope) => {
905 const hosts = hostsIn(scope);
906 if (!hosts.length) return;
907 load().then((module) => hosts.forEach((host) => {
908 module.upgrade(host);
909 if (host.isConnected) live.add(host);
910 }), () => {
911 // A failed import leaves the server's fallback in place; a later swap may
912 // try again rather than keeping the rejected promise forever.
913 viewer = null;
914 });
915 };
916
917 upgrade(document);
918 // The code browser swaps the open file into the pane, and a diff swaps the
919 // whole review pane: a new host is a new viewer. A host that is no longer in
920 // the document lost the swap, so its WebGL context and buffers are released
921 // now instead of waiting for the garbage collector.
922 document.body.addEventListener('htmx:afterSwap', (event) => {
923 for (const host of live) {
924 if (host.isConnected) continue;
925 host.dispatchEvent(new CustomEvent('vole:dispose'));
926 live.delete(host);
927 }
928 upgrade(event.target);
929 });
930 })();