@macrostrat/column-components
Version:
React rendering primitives for stratigraphic columns
3 lines (2 loc) • 4.85 kB
JavaScript
import"./column-components.c66242f5.js";import"./column-components.8ae02d44.js";import"./column-components.89b08d55.js";import"./column-components.b2a20dc1.js";import{createContext as t,useContext as e}from"react";import{StatefulComponent as n}from"@macrostrat/ui-components";import{compareAgeRanges as o,AgeRangeRelationship as i}from"@macrostrat/stratigraphy-utils";var r=globalThis,s={},a={},h=r.parcelRequirea149;null==h&&((h=function(t){if(t in s)return s[t].exports;if(t in a){var e=a[t];delete a[t];var n={id:t,exports:{}};return s[t]=n,e.call(n.exports,n,n.exports),n.exports}var o=Error("Cannot find module '"+t+"'");throw o.code="MODULE_NOT_FOUND",o}).register=function(t,e){a[t]=e},r.parcelRequirea149=h),h.register;var d=h("4bJ9d"),l=h("7j1lu"),u=h("7NDat"),p=h("aVUB5");let c=t(null),m=function(){let t=[];return function(e){let n=0,o=t.length+1;for(let i=0,r=0<=o;r?i<=o:i>=o;r?i++:i--)if(null==t[i]&&(t[i]=e.height),t[i]<e.height){let o=e.top_height||e.height;t[i]=o,n=i;break}return n}};class g extends n{static #t=this.contextType=p.ColumnContext;static #e=this.defaultProps={paddingLeft:60,estimatedTextHeight:(t,e)=>(t.note,12)};constructor(t){super(t),this.computeContextValue=this.computeContextValue.bind(this),this.savedRendererForWidth=this.savedRendererForWidth.bind(this),this.generatePath=this.generatePath.bind(this),this.createNodeForNote=this.createNodeForNote.bind(this),this.computeForceLayout=this.computeForceLayout.bind(this),this.updateHeight=this.updateHeight.bind(this),this.updateNotes=this.updateNotes.bind(this),this.componentDidMount=this.componentDidMount.bind(this),this.componentDidUpdate=this.componentDidUpdate.bind(this);let{noteComponent:e}=this.props;this.state={notes:[],elementHeights:{},columnIndex:{},nodes:{},generatePath:this.generatePath,createNodeForNote:this.createNodeForNote,noteComponent:e}}render(){let{children:t,width:e}=this.props;return(0,d.default)(c.Provider,{value:this.state},(0,d.default)(p.ColumnLayoutProvider,{width:e},t))}computeContextValue(){let{width:t,paddingLeft:e}=this.props,{scaleClamped:n}=this.context,o=new(0,u.Renderer)({direction:"right",layerGap:e,nodeHeight:5});return this.setState({renderer:o,updateHeight:this.updateHeight,generatePath:this.generatePath,...{paddingLeft:e,scale:n,width:t}})}savedRendererForWidth(t){return null==this._rendererIndex&&(this._rendererIndex={}),null==this._rendererIndex[t]&&(this._rendererIndex[t]=new(0,u.Renderer)({direction:"right",layerGap:t,nodeHeight:5})),this._rendererIndex[t]}generatePath(t,e){let{paddingLeft:n}=this.props,o=this.savedRendererForWidth(n-e);try{return o.generatePath(t)}catch(t){return null}}createNodeForNote(t){let{notes:e,elementHeights:n}=this.state,{scaleClamped:o}=this.context,{id:i}=t,r=n[i]||10,s=o(t.height);if((0,l.hasSpan)(t)){let e=[s-5,o(t.top_height)+5];if(e[0]-e[1]>0)return new(0,u.FlexibleNode)(e,r);s=(e[0]+e[1])/2}return new(0,u.Node)(s,r)}computeForceLayout(t,e){let{notes:n,nodes:o,elementHeights:i}=this.state,{pixelHeight:r}=this.context,{width:s,paddingLeft:a,forceOptions:h}=this.props;if(0===n.length)return;let d=Object.keys(o).length===n.length;null==e&&(e={});let l=i===e.elementHeights||[];if(d&&l)return;let p=new(0,u.Force)({minPos:0,maxPos:r,nodeSpacing:0,...h}),c=n.map(this.createNodeForNote);p.nodes(c).compute();let m=p.nodes()??[],g={};for(let t=0;t<m.length;t++){let e=m[t];g[n[t].id]=e}return this.updateState({nodes:{$set:g}})}updateHeight(t,e){if(null==e)return;let{elementHeights:n}=this.state;return n[t]=e,this.updateState({elementHeights:{$set:n}})}updateNotes(){let{scaleClamped:t}=this.context,e=this.props.notes.filter(function(t){let e=t.domain(),n=[Math.min(...e),Math.max(...e)];return t=>o(n,[t.height,t.top_height??t.height])!==i.Disjoint}(t)).sort((t,e)=>t.height-e.height),n=e.map(m());return this.setState({notes:e,columnIndex:n})}componentDidMount(){return this._previousContext=null,this.updateNotes(),this.computeContextValue()}componentDidUpdate(t,e){this.props.notes!==t.notes&&this.updateNotes();let{noteComponent:n}=this.props;if((n!==t.noteComponent&&this.setState({noteComponent:n}),this.computeForceLayout.call(t,e),this.props.notes!==t.notes)&&this.context!==this._previousContext)return this.computeContextValue(),this._previousContext=this.context}}function f(t){let{padding:n,width:o,...i}=t;null==n&&(n=5);let{pixelHeight:r}=e(p.ColumnContext);return(null==o&&({width:o}=e(c)),isNaN(o))?null:(0,d.default)("rect",{width:o+2*n,height:r,transform:`translate(${-n},${-n})`,...i})}let x=function({...t}){return(0,d.default)(f,{className:"underlay",...t})};function b(){let t=e(c);if(null==t)throw Error("useNoteLayout must be used within a NoteLayoutProvider");return t}export{c as NoteLayoutContext,g as NoteLayoutProvider,f as NoteRect,x as NoteUnderlay,b as useNoteLayout};
//# sourceMappingURL=column-components.a60d0bb9.js.map