@wayrisignals/kicad-webview
Version:
Interactive KiCad schematic, PCB and 3D viewer for the web, with Gerber, Excellon, linked views and offline HTML exports
3 lines (2 loc) • 3.33 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. */
var h=n=>!n.disposed&&typeof n.source=="string"&&n.source.length>0,m=(n,i)=>h(n)&&h(i)&&n.kind===i.kind&&n.path===i.path&&n.source===i.source;function b(n,i,e){if(!e)return null;let c=n.getParts(),d=i.getParts(),l=[e.ownerUuid,e.parent?.uuid,e.uuid,e.id].filter(o=>typeof o=="string"&&o),u=c.filter(o=>l.includes(o.uuid));if(u.length!==1)return null;let r=u[0],s;if(n.kind==="pcb"&&i.kind==="schematic"&&r.schematicUuid)s=d.filter(o=>o.uuid===r.schematicUuid);else if(n.kind==="schematic"&&i.kind==="pcb"&&r.uuid)s=d.filter(o=>o.schematicUuid===r.uuid);else if(m(n,i)&&r.uuid)s=d.filter(o=>o.uuid===r.uuid);else return null;return s.length===1?s[0]:null}function N(n,i,e){if(!m(n,i))return null;let c=e.netName||e.net,d=n.getNets(),l=i.getNets(),u=(e.kind==="net"||e.type==="net")&&typeof e.id=="string"&&e.id?d.filter(t=>t.id===e.id):[],r=typeof c=="string"?d.filter(t=>t.name===c):[],s=u.length===1?u[0]:r.length===1?r[0]:null;if(!s)return null;let o=typeof s.id=="string"&&s.id?l.filter(t=>t.id===s.id):[];if(o.length===1)return o[0];let y=l.filter(t=>t.name===s.name);return y.length===1?y[0]:null}function w(n,i,e){if(n===i||!h(n)||!h(i))return null;if(e===null)return i.clearSelection(),"clear";if(!e||typeof e!="object")return null;let c=String(e.kind||e.type||"").toLowerCase();if(m(n,i)&&typeof i.selectObject=="function"&&["pad","pin","segment","track","arc","via","zone"].includes(c)){let r=c==="pin"?e.pinNumber!==void 0?e.ownerUuid:void 0:e.uuid;if(typeof r=="string"&&r&&i.selectObject(r,{locate:i.view==="3d"&&["pad","pin"].includes(c),...e.pinNumber!==void 0?{pinNumber:e.pinNumber}:{}}))return"object"}let l=e.kind==="net"||e.type==="net"?null:b(n,i,e);if(l)return i.selectPart(l.id,{locate:i.view==="3d"})?"part":null;let u=N(n,i,e);return u&&i.selectNet(u.id||u.name,{locate:!1})?"net":null}function E(n,{enabled:i=!0}={}){if(!Array.isArray(n)||n.length<2||n.length>8||new Set(n).size!==n.length)throw new TypeError("Link 2 to 8 distinct viewer instances.");let e=["addEventListener","removeEventListener","getParts","getNets","selectPart","selectNet","clearSelection"];if(n.some(t=>!t||t.disposed||e.some(a=>typeof t[a]!="function")))throw new TypeError("Choose active KiCadViewer instances.");let c=[...n],d=new EventTarget,l=!1,u=!1,r=!!i,s=[],o=(t,a)=>d.dispatchEvent(new CustomEvent(t,{detail:a})),y=(t,a)=>{if(l||u||!r||!h(t))return;let f=a.detail;if(!f||!Object.hasOwn(f,"item")||f.path!==void 0&&f.path!==t.path||f.view!==void 0&&f.view!==t.view)return;let g=f.item===null?null:t.getSelection?.()||f.item;u=!0;try{for(let p of c){if(l||!r)break;if(!(p===t||!h(p)))try{let k=w(t,p,g);k&&!l&&r&&o("link",{source:t,target:p,kind:k})}catch(k){p.disposed||o("linkerror",{source:t,target:p,error:k})}}}finally{u=!1}};for(let t of c){let a=f=>y(t,f);t.addEventListener("select",a),s.push(()=>t.removeEventListener("select",a))}return Object.defineProperty(d,"enabled",{get:()=>r&&!l}),d.setEnabled=t=>{if(l)throw new Error("Linked viewers have been disposed.");r=!!t},d.dispose=()=>{if(!l){l=!0,r=!1;for(let t of s)t();s.length=0,c.length=0}},d}export{w as a,E as b};