@expofp/floorplan
Version:
Interactive floor plan library for expos and events
3 lines (2 loc) • 3.59 kB
JavaScript
import{Rect as u}from"@expofp/geometry";import{update as L}from"@expofp/renderer";import{reaction as m}from"mobx";import{FONT_WEIGHT_PRIMARY_BOOTH as g,FONT_WEIGHT_PRIMARY_SPECIAL as y}from"../../../../constants";import{createTextDef as F}from"../../../../engine/defs";import{loadCustomFontsUrls as D}from"../../../../engine/fonts";import{getQuarterTurns as x,getRollValue as R,rotateRectByQuarters as S,rotateTextDefByQuarters as T,shouldFlip as v}from"../../../../engine/update-def";import{heatmapStore as B,layersStore as I,uiState as b}from"../../../../store";import{SpecialBooth as w}from"../../../../store/BoothStore";import z from"../../../../tools/settings";import{getFont as O}from"./canvases";import{BoothLabelDrawer as C,getFontUrlByWeight as M,getMinZoomFactorForDot as P}from"./config-booth-labels";import{LABEL_MARGIN_FRACTION as f}from"./label-inset.js";import _ from"./TextFitter";const l=new Map;function A(i){let t=l.get(i);if(!t){const s=[18,16,14,12,10,7].map(o=>o*i),e=14*i;t=new _(O,s,e),l.set(i,t),setTimeout(()=>l.delete(i),5e3)}return t}export default function N(i){if(!(!(i instanceof w)||i.noLabels))return new V(i)}class V extends C{constructor(t){super(t),this.steps=[],this.text="";const s=this.booth.rect,e=Number(getComputedStyle(document.body).getPropertyValue(y))||Number(getComputedStyle(document.body).getPropertyValue(g))||500,o=M(e,D()),r=s.expand({x:-(s.size.width*f+this.padding),y:-(s.size.height*f+this.padding)});this.setText(),this.canvasRect=new u(r.center,r.size,t.rotate,t.elevation),this.setFactors(this.text),this.labelIsVertical=Math.abs(Math.abs(t.rotate)-Math.PI/2)<.01,this.fontSizes=[this.steps,[]],this.minZoomDotVisible=P(this.steps.map(a=>a.scaleFactor)),this.shapeLabel=F([{text:this.text,color:t.labelColor||z.boothLabelColor,fontUrl:o,fontSize:0}],new u(r.center,r.size,t.rotate,t.elevation),[this.padding,this.padding],{horizontal:"center",text:"center",vertical:"center"}),this.shapeLabel.onScale=({pixelSize:a})=>this.layOutLabel(a),this.getShapeDot(),m(()=>I.layersLoaded&&this.booth.skipDim===!0,a=>{this.shapeLabel.dim=a?0:void 0,this.shapeDot.dim=a?0:void 0,L(this.shapeLabel,this.shapeDot)}),m(()=>b.mapSettings,({roll:a})=>{R(a??0)&&(this.shapeLabel.onRoll=({roll:c,pixelSize:p})=>{const h=this.shapeLabel.bounds.rotation;if(this.labelIsVertical)return v(c,h)?(this.shapeLabel.bounds.rotate(-2*h,this.shapeLabel.bounds),[this.shapeLabel,this.shapeDot]):void 0;const n=x(c,h);if(n===0)return;const{size:d}=this.canvasRect;return this.shapeLabel=T(this.shapeLabel,n),d.x!==d.y&&!this.shapeLabel.hidden&&(this.canvasRect=S(this.canvasRect,n),this.setFactors(this.text),this.layOutLabel(p),this.layOutDot(p)),[this.shapeLabel,this.shapeDot]})},{fireImmediately:!0})}getShape(){return[this.shapeLabel,this.shapeDot]}layOutLabel(t){const s=this.steps.find(e=>e.scaleFactor<1/t);return s?(this.shapeLabel.lines[0].fontSize=s.value*t,this.shapeLabel.lines[0].text=s.lines.join(`
`),this.shapeLabel.padding=[this.paddingScaled[0]*t+this.padding,this.paddingScaled[1]*t+this.padding],[this.shapeLabel]):(this.shapeLabel.lines[0].fontSize=0,[this.shapeLabel])}layOutDot(t){const s=this.steps.find(r=>r.scaleFactor<1/t),e=this.shapeDot.bounds,o=this.shapeDot.source;return e.scale({x:o.width*t/e.size.x,y:o.height*t/e.size.y},e.center,e),this.shapeDot.hidden=!!s,[this.shapeDot]}setFactors(t){this.steps=A(devicePixelRatio).getStepsForRect(t,this.canvasRect.size.x,this.canvasRect.size.y)}setText(){const t=this.booth.title||this.booth.name,e=`Clicks: ${B.getTotalClicksByBooth(this.booth)}`;this.text=b.heatmap?`${t} - ${e}`:t}}