@wayrisignals/kicad-webview
Version:
Interactive KiCad schematic, PCB and 3D viewer for the web, with Gerber, Excellon, linked views and offline HTML exports
10 lines (9 loc) • 5 kB
JavaScript
/*! KiCad WebView 1.2.1 | Copyright (c) 2026 wayri | SPDX-License-Identifier: MPL-2.0 | Source: https://github.com/wayri/kicad-webview/tree/v1.2.1 | Keep LICENSE and THIRD-PARTY-NOTICES.md; bundled dependencies retain their licenses. */
function g(e="dim",t="accent"){if(!["dim","bright","isolate"].includes(e))throw new TypeError("Choose dim, bright or isolate highlight style.");if(!["accent","source"].includes(t))throw new TypeError("Choose accent or source highlight color.");return{mode:e,color:t}}var A=Object.freeze(["realistic","layers","translucent"]);function b(e){if(!A.includes(e))throw new TypeError("Choose realistic, layers or translucent appearance.");return e}var E=64*1024*1024,l=e=>String(e).replaceAll("&","&").replaceAll("<","<").replaceAll(">",">").replaceAll('"',"""),c=e=>JSON.stringify(e).replaceAll("<","\\u003c");function P(e){let t="";for(let n=0;n<e.length;n+=8192)t+=String.fromCharCode(...e.subarray(n,n+8192));return btoa(t)}function N({viewerScript:e,licenseText:t,files:n,title:d="Interactive design",view:s,theme:u="dark",highlightStyle:x="dim",highlightColor:C="source",appearance:p="realistic"}={}){if(typeof e!="string"||!e.trim()||e.length>8*1024*1024)throw new TypeError("Supply the full classic viewer script (up to 8 MiB).");if(typeof t!="string"||!t.trim()||t.length>1024*1024)throw new TypeError("Include the viewer license and dependency notices.");if(!Array.isArray(n)||!n.length||n.length>256)throw new TypeError("Choose 1 to 256 files, or one project ZIP.");if(!["dark","light"].includes(u)||s!==void 0&&!["pcb","schematic","3d","manufacturing"].includes(s))throw new TypeError("Choose a supported theme and view.");let f=g(x,C);b(p);let h=new Set,y=0,a=n.map(o=>{let r=o?.path;if(typeof r!="string"||r.length>1024||!r||r.includes("\\")||r.startsWith("/")||/^[a-z]+:/i.test(r)||r.split("/").some(i=>!i||i==="."||i==="..")||h.has(r))throw new TypeError("Choose unique relative project file paths.");if(h.add(r),!(o.bytes instanceof Uint8Array)||!o.bytes.length)throw new TypeError("Each file needs nonempty Uint8Array bytes.");if(y+=o.bytes.length,y>E)throw new RangeError("HTML export is limited to 64 MiB of input files.");return{path:r,base64:P(o.bytes)}}),m=a.some(o=>/\.zip$/i.test(o.path));if(m&&a.length!==1)throw new TypeError("Export a ZIP alone, or choose individual project files.");let L={theme:u,inspectOnSelect:!1,highlightStyle:f.mode,highlightColor:f.color,appearance:p},w={...s?{view:s}:{}},S=e.replace(/<\/script/gi,"<\\/script");return`<html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><meta http-equiv="Content-Security-Policy" content="default-src 'none'; script-src 'unsafe-inline'; style-src 'unsafe-inline'; img-src data: blob:; font-src data:; worker-src blob:; connect-src 'none'; object-src 'none'; base-uri 'none'; form-action 'none'"><title>${l(d)}</title><style>*{box-sizing:border-box}html,body{margin:0;height:100%;font:14px system-ui;background:#111a24;color:#e1eaf3}body{display:flex;flex-direction:column}header{padding:8px 12px;border-bottom:1px solid #324254;display:flex;align-items:center;gap:8px;flex-wrap:wrap}h1{margin:0;flex:1;font-size:15px}button{font:inherit;min-height:44px;background:#182330;color:#e1eaf3;border:1px solid #52748a;border-radius:6px;padding:5px 10px}#viewer{flex:1;min-height:240px}#status{margin:0;padding:4px 12px;font-size:11px}dialog{max-width:min(700px,95vw);max-height:80dvh;background:#182330;color:#e1eaf3;border:1px solid #52748a;overflow:auto}pre{white-space:pre-wrap;overflow-wrap:anywhere;font:11px/1.5 monospace}</style></head><body><header><h1>${l(d)}</h1><button id="notices">License & source</button></header><div id="viewer" aria-label="Interactive design viewer"></div><p id="status" role="status">Opening bundled design\u2026</p><dialog id="license"><button id="close">Close</button><p>Design files are bundled in this document. No external network access is enabled. Models must be included in the project; unresolved models use labelled proxies.</p><pre>${l(t)}</pre></dialog><script>${S}<\/script><script>
const data=${c(a)};const files=data.map(file=>({path:file.path,bytes:Uint8Array.from(atob(file.base64),c=>c.charCodeAt(0))}));
const viewer=new KiCadWebView.Viewer('#viewer',${c(L)});const status=document.querySelector('#status');
document.querySelector('#notices').onclick=()=>document.querySelector('#license').showModal();document.querySelector('#close').onclick=()=>document.querySelector('#license').close();
viewer.addEventListener('error',event=>status.textContent=event.detail?.message||'Unable to load design.');
${m?"viewer.loadArchive(files[0].bytes,":"viewer.loadFiles(files,"}${c(w)}).then(()=>status.textContent='Read-only \xB7 bundled design \xB7 no external network access').catch(error=>status.textContent=error.message);
window.addEventListener('pagehide',()=>viewer.dispose(),{once:true});
<\/script></body></html>`}export{N as createStandaloneHTML};