UNPKG

@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) • 5.77 kB
/*! 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. */ import{a as b}from"./chunk-V2ND4NUF.js";import"./chunk-TZWR7E27.js";var k=new WeakMap,S=m=>m&&!m.disposed&&["pcb","schematic"].includes(m.kind)&&typeof m.source=="string"&&m.source.length,E=class extends EventTarget{constructor(n,{parent:t,sourceViewer:h=t,view:s,selection:v}={}){if(super(),!S(t)||!S(h))throw new TypeError("Insets require active, loaded KiCad viewers.");let l=typeof n=="string"?t.host.ownerDocument.querySelector(n):n;if(!l?.ownerDocument||!l.append)throw new TypeError("Choose an inset container.");let p=s||h.kind;if(!["pcb","schematic","3d"].includes(p)||h.kind==="schematic"&&p!=="schematic"||h.kind==="pcb"&&p==="schematic")throw new TypeError("Inset view must match its loaded drawing.");let u=k.get(t)||new Set;if(u.size>=2)throw new Error("Close an inset before opening another (maximum two per parent).");this.parent=t,this.sourceViewer=h,this.disposed=!1,this.stops=[],this.slots=u,u.add(this),k.set(t,u);let x=l.ownerDocument,a=(e,r)=>{let i=x.createElement(e);return r!==void 0&&(i.textContent=r),i};this.root=a("section"),this.root.className="viewer-context-inset",this.root.setAttribute("aria-label","Contextual design preview"),this.root.style.cssText="display:flex;flex-direction:column;min-width:0;height:100%;min-height:0;overflow:hidden;border:1px solid var(--viewer-border,#40566a);border-radius:8px;background:var(--viewer-bar,#182330);color:var(--viewer-ink,#e1eaf3)";let f=a("header");f.style.cssText="display:flex;align-items:center;gap:4px;padding:2px 5px;height:34px;min-height:34px;flex:0 0 auto;font:12px/1 system-ui";let d=a("strong",h.path.split("/").at(-1)||"Design preview");d.style.cssText="flex:1;min-width:0;overflow:hidden;text-overflow:ellipsis;white-space:nowrap";let w=(e,r,i)=>{let c=a("button"),y=a("span");return c.type="button",c.title=e,c.setAttribute("aria-label",e),y.setAttribute("aria-hidden","true"),y.innerHTML='<svg viewBox="0 0 24 24" width="16" height="16" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><path d="'+r+'"/></svg>',c.append(y),c.style.cssText="appearance:none;display:inline-flex;align-items:center;justify-content:center;box-sizing:border-box;flex:0 0 30px;width:30px;height:30px;margin:0;padding:0;background:transparent;color:inherit;border:1px solid var(--viewer-border,#40566a);border-radius:5px;font:600 12px/1 system-ui;cursor:pointer",c.onclick=i,c};this.closeButton=w("Close","M5 5l14 14M19 5 5 19",()=>this.dispose()),this.fitButton=w("Fit selection","M8 3H3v5M16 3h5v5M21 16v5h-5M8 21H3v-5M8 8h8v8H8z",()=>this.focus()),f.append(d,this.fitButton,this.closeButton),this.mount=a("div"),this.mount.style.cssText="flex:1;min-height:0;position:relative",this.root.append(f,this.mount),l.append(this.root);let o=[...new Set([t,h])].map(e=>({viewer:e,source:e.source,path:e.path,instancePath:e.textInstancePath})),g=(e,r,i)=>{e.addEventListener(r,i),this.stops.push(()=>e.removeEventListener(r,i))};for(let e of o)g(e.viewer,"dispose",()=>this.dispose()),g(e.viewer,"ready",()=>{(e.viewer.source!==e.source||e.viewer.path!==e.path||e.viewer.textInstancePath!==e.instancePath)&&this.dispose()});this.ready=this.initialize(p,v).catch(e=>{throw this.dispose(),e})}async initialize(n,t){let{KiCadViewer:h}=await import("./kicad-viewer-KLF4O6BS.js");if(this.disposed)return!1;let s=this.sourceViewer;this.viewer=new h(this.mount,{theme:this.parent.theme,inspectOnSelect:!1,highlightStyle:s.getHighlightStyle().mode,highlightColor:s.getHighlightStyle().color,...s.modelOptions}),this.viewer.root.setAttribute("data-inset","true");let v=o=>{this.disposed||this.viewer.setTheme(o.detail.theme)},l=o=>{this.disposed||this.viewer.setHighlightStyle(o.detail.mode,{color:o.detail.color})};this.parent.addEventListener("themechange",v),this.parent.addEventListener("highlightstylechange",l);let p=this.parent;this.stops.push(()=>p.removeEventListener("themechange",v),()=>p.removeEventListener("highlightstylechange",l));let u=s.source,x=s.path,a=s.textInstancePath;if(await this.viewer.loadSource(u,{path:x,view:n,textVariables:s.textVariables,project:s.project,instancePath:a}),this.disposed)return!1;if(s.disposed||s.source!==u||s.path!==x||s.textInstancePath!==a)return this.dispose(),!1;let f=[...new Set([this.parent,s,this.viewer])],d=!1;for(let o of f){let g=e=>{if(d||this.disposed||!e.detail||e.detail.path!==void 0&&e.detail.path!==o.path||e.detail.view!==void 0&&e.detail.view!==o.view)return;d=!0;let r=f.filter(i=>!i.disposed).map(i=>[i,i.inspectOnSelect]);for(let[i]of r)i.inspectOnSelect=!1;try{for(let i of f)i!==o&&!i.disposed&&b(o,i,o.getSelection()||e.detail.item);o!==this.viewer&&this.focus()}finally{for(let[i,c]of r)i.inspectOnSelect=c;d=!1}};o.addEventListener("select",g),this.stops.push(()=>o.removeEventListener("select",g))}let w=t===void 0?this.parent.getSelection():t;d=!0;try{b(this.parent,this.viewer,w),w&&this.focus()}finally{d=!1}return!0}focus(){if(this.disposed||!this.viewer||!this.viewer.source)return!1;let n=this.viewer,t=n.getSelection();return t?t.kind==="net"?n.selectNet(t.id||t.netName,{locate:!0}):t.kind==="part"?n.selectPart(t.uuid||t.id,{locate:!0}):n.selectObject(t.uuid||t.id,{locate:!0,pinNumber:t.pinNumber}):(n.fit(),!0)}dispose(){if(!this.disposed){this.disposed=!0;for(let n of this.stops)n();this.stops.length=0,this.viewer?.dispose(),this.closeButton.onclick=this.fitButton.onclick=null,this.root.remove(),this.slots.delete(this),this.parent=this.sourceViewer=null,this.dispatchEvent(new CustomEvent("dispose"))}}};export{E as ViewerInset};