UNPKG

@macrostrat/column-components

Version:

React rendering primitives for stratigraphic columns

3 lines (2 loc) 4.79 kB
import"./column-components.8ae02d44.js";import"./column-components.89b08d55.js";import"./column-components.298cc9f3.js";import{createContext as t,useContext as e}from"react";import{StatefulComponent as n}from"@macrostrat/ui-components";import o from"@macrostrat/hyper";import{compareAgeRanges as r,AgeRangeRelationship as i}from"@macrostrat/stratigraphy-utils";var s=globalThis,a={},h={},d=s.parcelRequirea149;null==d&&((d=function(t){if(t in a)return a[t].exports;if(t in h){var e=h[t];delete h[t];var n={id:t,exports:{}};return a[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){h[t]=e},s.parcelRequirea149=d),d.register;var l=d("7j1lu"),u=d("7NDat"),p=d("aVUB5");let c=t(null),m=function(){let t=[];return function(e){let n=0,o=t.length+1;for(let r=0,i=0<=o;i?r<=o:r>=o;i?r++:r--)if(null==t[r]&&(t[r]=e.height),t[r]<e.height){let o=e.top_height||e.height;t[r]=o,n=r;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 o(c.Provider,{value:this.state},o(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:r}=t,i=n[r]||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,i);s=(e[0]+e[1])/2}return new(0,u.Node)(s,i)}computeForceLayout(t,e){let{notes:n,nodes:o,elementHeights:r}=this.state,{pixelHeight:i}=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=r===e.elementHeights||[];if(d&&l)return;let p=new(0,u.Force)({minPos:0,maxPos:i,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();return t=>r(e,[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}}let f=function(t){let{padding:n,width:r,...i}=t;null==n&&(n=5);let{pixelHeight:s}=e(p.ColumnContext);return(null==r&&({width:r}=e(c)),isNaN(r))?null:o("rect",{width:r+2*n,height:s,transform:`translate(${-n},${-n})`,...i})},x=function({fill:t,...e}){return null==t&&(t="transparent"),o(f,{className:"underlay",fill:t,...e})};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.df0ea532.js.map