UNPKG

@expofp/floorplan

Version:

Interactive floor plan library for expos and events

2 lines (1 loc) • 2.49 kB
import{Box as l}from"@expofp/geometry";import*as c from"d3-selection";import{STRING_META_DELIMITER as h}from"../constants.js";import{eventScopedCache as A,eventScopedValue as M}from"../tools/event-scoped-cache.js";import p from"../tools/logger.js";import{extractMetaFromString as u}from"../utils/extractMetaFromString.js";import{getFpPaths as d,getFpSvg as v,getFpSvgJs as x}from"./fp-payloads.js";const m=A();function w(o,e=""){const t=m().get(e);if(t)return t;const s=new DOMParser().parseFromString(o,"image/svg+xml").documentElement;s.firstChild?.tagName==="parsererror"&&p.error("Parsed svg with error: ",s);const n=new Map;return c.select(s).selectAll("style").each(function(){const i=this.textContent??"",f=/\.([a-z0-9.]+)\s*{[^}]*fill\s*:\s*([^};]+);[^}]*}/gi;let a;for(;(a=f.exec(i))!==null;){const S=a[1],b=a[2];n.set(S,b)}}),c.select(s).selectAll("*[class]").each(function(){const i=n.get(this.className.baseVal);i!==void 0&&(this.style.fill=i)}),F(s),m().set(e,s),s}function E(){const o=w(v()??""),e=x().__viewbox,t=c.select(o).select("rect#VIEWBOX").node(),r=o.viewBox.baseVal,s=new l(r.x,r.y,r.width,r.height);let n,i=!1;e?(n=new l(e.x,e.y,e.width,e.height),i=!0,t?.remove()):t?(n=l.fromSvg(t),i=!0,t.remove()):n=s.scale(1.1),c.select(o).attr("width",s.size.width),c.select(o).attr("height",s.size.height),p.log("svgArea",n,"svgViewBox",s);const f=x().__fpLayersMode?c.select(o).selectAll("[data-floor]").nodes().map(a=>(a.remove(),{name:a.dataset.floor??"",rect:l.fromSvg(a)})).sort():[];return{area:n,areaExplicit:i,floors:f}}const g=M(E);export function getSvgArea(){return g().area}export function isSvgAreaExplicit(){return g().areaExplicit}export function getFloors(){return g().floors}export function getTrianglesFromFpPaths(o,e){const t=gtePathByIndex(o,e),r=t.positions.map(n=>[n[0],Math.abs(n[1])]),s=[];for(const n of t.cells)s.push([r[n[0]],r[n[1]],r[n[2]]]);return s}export function gtePathByIndex(o,e=""){const t=d(e)??d();if(!t)throw new Error(`No meshes for layer "${e}"`);return t[o]}export function getLayerSvg(o=""){g();const e=typeof o=="string"?o:o.rootParent?.name??o.name,t=m().get(e);if(t)return t;const r=v(e);return r?w(r,e):m().get("")}function F(o){c.select(o).selectAll("[id], [data-name]").each(function(){let e={};const t=this.getAttribute("data-name");if(t&&t.includes(h)){const{meta:s}=u(t);e={...e,...s}}const r=this.getAttribute("id");if(r&&r.includes(h)){const{meta:s}=u(r);e={...e,...s}}this.setAttribute("data-meta",JSON.stringify(e))})}