1717import esbuildWasmUrl from 'esbuild-wasm/esbuild.wasm?url' ;
1818import ManifoldWorker from 'manifold-3d/lib/worker.bundled.js?worker' ;
1919import manifoldWasmUrl from 'manifold-3d/manifold.wasm?url' ;
20- import { AutoTypings , LocalStorageCache } from 'monaco-editor-auto-typings' ;
20+ import { AutoTypings , JsDelivrSourceResolver , LocalStorageCache } from 'monaco-editor-auto-typings' ;
2121import * as monaco from 'monaco-editor/esm/vs/editor/editor.main' ;
2222// '?worker' is vite convention to load a module as a web worker.
2323import editorWorker from 'monaco-editor/esm/vs/editor/editor.worker?worker' ;
@@ -28,8 +28,13 @@ const CODE_START = '<code>';
2828const exampleFunctions = self . examples ;
2929
3030if ( navigator . serviceWorker ) {
31- navigator . serviceWorker . register (
32- '/service-worker.js' , { scope : './index.html' } ) ;
31+ // Resolve against the current page URL so production asset paths don't
32+ // redirect registration into /assets.
33+ const serviceWorkerUrl = new URL ( './service-worker.js' , window . location . href ) ;
34+ navigator . serviceWorker . register ( serviceWorkerUrl , { scope : './' } )
35+ . catch ( error => {
36+ console . error ( 'Service worker registration failed:' , error ) ;
37+ } ) ;
3338}
3439
3540let editor = undefined ;
@@ -307,6 +312,7 @@ async function createEditor() {
307312 editor = monaco . editor . create ( document . getElementById ( 'editor' ) , {
308313 language : 'typescript' ,
309314 automaticLayout : true ,
315+ minimap : { enabled : false } ,
310316 } ) ;
311317
312318 monaco . languages . typescript . typescriptDefaults . setCompilerOptions ( {
@@ -316,15 +322,20 @@ async function createEditor() {
316322 } ) ;
317323
318324 // Make sure `manifold-3d/manifoldCAD` types are available for import.
325+ const manifoldCADTypesUrl =
326+ new URL ( './manifoldCAD.d.ts' , window . location . href ) ;
327+ const manifoldCADGlobalsTypesUrl =
328+ new URL ( './manifoldCADGlobals.d.ts' , window . location . href ) ;
329+
319330 monaco . languages . typescript . typescriptDefaults . addExtraLib (
320- await ( await fetch ( '/manifoldCAD.d.ts' ) ) . text ( ) ,
331+ await ( await fetch ( manifoldCADTypesUrl ) ) . text ( ) ,
321332 'inmemory://model/node_modules/manifold-3d/manifoldCAD.d.ts' ) ;
322333
323334 // Types in the global namespace for top-level scripts.
324335 // This could be improved in the future. API-Extractor intentionally doesn't
325336 // global variables, so another tool may be a better fit.
326337 monaco . languages . typescript . typescriptDefaults . addExtraLib (
327- ( await ( await fetch ( '/manifoldCADGlobals.d.ts' ) ) . text ( ) )
338+ ( await ( await fetch ( manifoldCADGlobalsTypesUrl ) ) . text ( ) )
328339 . replace ( / ^ e x p o r t / gm, '' ) ) ;
329340
330341 // Load up all scripts so that monaco can check types of multi-file models.
@@ -333,16 +344,39 @@ async function createEditor() {
333344 }
334345
335346 // Initialize auto typing on monaco editor.
347+ const typeIndicator = document . getElementById ( 'type-indicator' ) ;
336348 self . window . typecache = new LocalStorageCache ( ) ;
349+
350+ // We inject manifold-3d typings locally above, so avoid extra CDN probes for
351+ // that package path. This prevents noisy 404s in production logs.
352+ const jsDelivrResolver = new JsDelivrSourceResolver ( ) ;
353+ const sourceResolver = {
354+ resolvePackageJson : async ( packageName , version , subPath ) => {
355+ if ( packageName === 'manifold-3d' ) return '' ;
356+ return jsDelivrResolver . resolvePackageJson ( packageName , version , subPath ) ;
357+ } ,
358+ resolveSourceFile : async ( packageName , version , path ) => {
359+ if ( packageName === 'manifold-3d' ) return '' ;
360+ return jsDelivrResolver . resolveSourceFile ( packageName , version , path ) ;
361+ }
362+ } ;
363+
337364 const autoTypings = await AutoTypings . create ( editor , {
365+ sourceResolver,
338366 sourceCache : self . window . typecache ,
339- onError : e => { console . error ( e ) } ,
340- onUpdate : ( update , text ) => { console . debug ( text ) } ,
367+ onError : e => {
368+ console . error ( e ) ;
369+ if ( typeIndicator ) typeIndicator . textContent = '' ;
370+ } ,
371+ onUpdate : ( _ , text ) => {
372+ if ( typeIndicator ) typeIndicator . textContent = 'Fetching types...' ;
373+ console . debug ( text ) ;
374+ } ,
341375 onUpdateVersions : ( versions ) => {
342- console . debug ( versions )
376+ if ( typeIndicator ) typeIndicator . textContent = '' ;
377+ console . debug ( versions ) ;
343378 }
344379 } ) ;
345-
346380 for ( const [ name ] of exampleFunctions ) {
347381 const button = createDropdownItem ( name ) ;
348382 fileDropdown . appendChild ( button . parentElement ) ;
@@ -609,7 +643,7 @@ runButton.onclick = function() {
609643 }
610644} ;
611645
612- function clickSave ( saveButton , filename , outputName ) {
646+ function clickSave ( saveButton , extension , outputName ) {
613647 const container = saveButton . parentElement ;
614648 return ( ) => {
615649 const oldSave = saveContainer . firstElementChild ;
@@ -619,14 +653,17 @@ function clickSave(saveButton, filename, outputName) {
619653 container . appendChild ( saveArrow . parentElement ) ;
620654 }
621655 const link = document . createElement ( 'a' ) ;
622- link . download = filename ;
656+
657+ link . download =
658+ `${ currentFileElement . textContent . trim ( ) || 'manifold' } .${ extension } ` ;
659+
623660 link . href = output [ outputName ] ;
624661 link . click ( ) ;
625662 } ;
626663}
627664
628665const glbButton = document . querySelector ( '#glb' ) ;
629- glbButton . onclick = clickSave ( glbButton , 'manifold. glb' , 'glbURL' ) ;
666+ glbButton . onclick = clickSave ( glbButton , 'glb' , 'glbURL' ) ;
630667
631668const threemfButton = document . querySelector ( '#threemf' ) ;
632- threemfButton . onclick = clickSave ( threemfButton , 'manifold. 3mf' , 'threeMFURL' ) ;
669+ threemfButton . onclick = clickSave ( threemfButton , '3mf' , 'threeMFURL' ) ;
0 commit comments