|
| 1 | +/** |
| 2 | + * AnyWidget wrapper for repo-review app |
| 3 | + * Provides the render() interface for MyST's anywidget directive |
| 4 | + */ |
| 5 | + |
| 6 | +export async function render({ model, el }) { |
| 7 | + // Create a root container for the React app |
| 8 | + const root = document.createElement("div"); |
| 9 | + root.id = "root"; |
| 10 | + root.style.width = "100%"; |
| 11 | + root.style.minHeight = "600px"; |
| 12 | + |
| 13 | + // Add Material Design fonts if not already present |
| 14 | + ensureFonts(); |
| 15 | + |
| 16 | + // Append root to the target element |
| 17 | + el.appendChild(root); |
| 18 | + |
| 19 | + // Determine the correct path to the app |
| 20 | + // The widget will be in /build/ and the app is in /assets/js/ |
| 21 | + const baseUrl = new URL(import.meta.url); |
| 22 | + const appUrl = new URL("../../assets/js/repo-review-app.min.js", baseUrl).href; |
| 23 | + |
| 24 | + // Load the app via dynamic import |
| 25 | + try { |
| 26 | + const appModule = await import(appUrl); |
| 27 | + |
| 28 | + // The app should export a mountApp function |
| 29 | + if (appModule.mountApp) { |
| 30 | + appModule.mountApp({ |
| 31 | + header: true, |
| 32 | + deps: [ |
| 33 | + "repo-review~=1.0.0", |
| 34 | + "sp-repo-review==2026.04.04", |
| 35 | + "validate-pyproject[all]~=0.25.0", |
| 36 | + "validate-pyproject-schema-store==2026.03.29", |
| 37 | + ], |
| 38 | + }); |
| 39 | + } else { |
| 40 | + console.warn( |
| 41 | + "repo-review-app.min.js does not export mountApp, attempting alternative load" |
| 42 | + ); |
| 43 | + // Try loading as a script tag instead |
| 44 | + loadAppAsScript(appUrl); |
| 45 | + } |
| 46 | + } catch (error) { |
| 47 | + console.error("Failed to load repo-review app via import:", error); |
| 48 | + // Fallback: load as a regular script |
| 49 | + const appSrc = appUrl.replace(/\.mjs$/, ".js").replace(/\.js$/, ".min.js"); |
| 50 | + loadAppAsScript(appSrc); |
| 51 | + } |
| 52 | + |
| 53 | + function loadAppAsScript(src) { |
| 54 | + const script = document.createElement("script"); |
| 55 | + script.type = "module"; |
| 56 | + script.textContent = `import('./repo-review-app.min.js').then(m => m.mountApp ? m.mountApp({ |
| 57 | + header: true, |
| 58 | + deps: [ |
| 59 | + "repo-review~=1.0.0", |
| 60 | + "sp-repo-review==2026.04.04", |
| 61 | + "validate-pyproject[all]~=0.25.0", |
| 62 | + "validate-pyproject-schema-store==2026.03.29", |
| 63 | + ], |
| 64 | + }) : console.error("mountApp not found")).catch(e => { |
| 65 | + root.innerHTML = "<p style='color: red; padding: 20px;'>Failed to load repo-review app. Please check the browser console for details.</p>"; |
| 66 | + console.error("Failed to load repo-review app:", e); |
| 67 | + });`; |
| 68 | + document.head.appendChild(script); |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +/** |
| 73 | + * Ensure Material Design fonts are loaded |
| 74 | + */ |
| 75 | +function ensureFonts() { |
| 76 | + // Check if fonts are already loaded |
| 77 | + if (document.querySelector('link[href*="fonts.googleapis.com"]')) { |
| 78 | + return; |
| 79 | + } |
| 80 | + |
| 81 | + // Add Roboto font for Material Design |
| 82 | + const robotoLink = document.createElement("link"); |
| 83 | + robotoLink.rel = "stylesheet"; |
| 84 | + robotoLink.href = |
| 85 | + "https://fonts.googleapis.com/css?family=Roboto:300,400,500,700&display=swap"; |
| 86 | + document.head.appendChild(robotoLink); |
| 87 | + |
| 88 | + // Add Material Icons |
| 89 | + const iconsLink = document.createElement("link"); |
| 90 | + iconsLink.rel = "stylesheet"; |
| 91 | + iconsLink.href = "https://fonts.googleapis.com/icon?family=Material+Icons"; |
| 92 | + document.head.appendChild(iconsLink); |
| 93 | +} |
0 commit comments