@@ -3,13 +3,17 @@ import { StatefulComponent } from "@macrostrat/ui-components";
33import h from "@macrostrat/hyper" ;
44
55import { hasSpan } from "./utils" ;
6- import { FlexibleNode , Node , Renderer , Force } from "./label-primitives" ;
6+ import { FlexibleNode , Force , Node , Renderer } from "./label-primitives" ;
77import {
8- ColumnLayoutProvider ,
98 ColumnContext ,
109 ColumnCtx ,
1110 ColumnDivision ,
11+ ColumnLayoutProvider ,
1212} from "../context" ;
13+ import {
14+ AgeRangeRelationship ,
15+ compareAgeRanges ,
16+ } from "@macrostrat/stratigraphy-utils" ;
1317
1418const NoteLayoutContext = createContext ( null ) ;
1519
@@ -42,19 +46,16 @@ const buildColumnIndex = function () {
4246 } ;
4347} ;
4448
45- const withinDomain = ( scale ) =>
46- function ( d ) {
47- const [ start , end ] = scale . domain ( ) ;
48- // end height greater than beginning
49- const end_height = d . top_height || d . height ;
50- if ( start < end ) {
51- // Normal scale (e.g. height)
52- return end_height >= start && d . height <= end ;
53- } else {
54- // Inverted scale (e.g. time)
55- return end_height <= start && d . height >= end ;
56- }
49+ function withinDomain ( scale ) {
50+ const scaleDomain = scale . domain ( ) ;
51+ return ( d ) => {
52+ const noteRange : [ number , number ] = [ d . height , d . top_height ?? d . height ] ;
53+
54+ const rel = compareAgeRanges ( scaleDomain , noteRange ) ;
55+
56+ return rel !== AgeRangeRelationship . Disjoint ;
5757 } ;
58+ }
5859
5960interface NoteLayoutProviderProps {
6061 notes : any [ ] ;
@@ -70,6 +71,7 @@ interface NoteLayoutState {
7071 elementHeights ?: object ;
7172 columnIndex ?: object ;
7273 nodes ?: object ;
74+ updateHeight ?: Function ;
7375 generatePath : Function ;
7476 createNodeForNote ?: Function ;
7577 noteComponent ?: any ;
@@ -81,7 +83,7 @@ export interface NoteLayoutCtx {
8183 paddingLeft : number ;
8284 scale : Function ;
8385 width : number ;
84- registerHeight : Function ;
86+ updateHeight : Function ;
8587 generatePath : Function ;
8688 columnIndex ?: any ;
8789 nodes ?: any ;
@@ -110,7 +112,7 @@ class NoteLayoutProvider extends StatefulComponent<
110112 this . generatePath = this . generatePath . bind ( this ) ;
111113 this . createNodeForNote = this . createNodeForNote . bind ( this ) ;
112114 this . computeForceLayout = this . computeForceLayout . bind ( this ) ;
113- this . registerHeight = this . registerHeight . bind ( this ) ;
115+ this . updateHeight = this . updateHeight . bind ( this ) ;
114116 this . updateNotes = this . updateNotes . bind ( this ) ;
115117 this . componentDidMount = this . componentDidMount . bind ( this ) ;
116118 this . componentDidUpdate = this . componentDidUpdate . bind ( this ) ;
@@ -148,8 +150,6 @@ class NoteLayoutProvider extends StatefulComponent<
148150 paddingLeft,
149151 scale,
150152 width,
151- registerHeight : this . registerHeight ,
152- generatePath : this . generatePath ,
153153 } ;
154154
155155 // Compute force layout
@@ -161,6 +161,8 @@ class NoteLayoutProvider extends StatefulComponent<
161161
162162 return this . setState ( {
163163 renderer,
164+ updateHeight : this . updateHeight ,
165+ generatePath : this . generatePath ,
164166 ...forwardedValues ,
165167 } ) ;
166168 }
@@ -195,18 +197,19 @@ class NoteLayoutProvider extends StatefulComponent<
195197 const { id : noteID } = note ;
196198 const pixelHeight = elementHeights [ noteID ] || 10 ;
197199 const padding = 5 ;
198- const lowerHeight = scale ( note . height ) ;
200+ let noteHeight = scale ( note . height ) ;
199201 if ( hasSpan ( note ) ) {
200202 const upperHeight = scale ( note . top_height ) ;
201203 const harr : [ number , number ] = [
202- lowerHeight - padding ,
204+ noteHeight - padding ,
203205 upperHeight + padding ,
204206 ] ;
205207 if ( harr [ 0 ] - harr [ 1 ] > 0 ) {
206208 return new FlexibleNode ( harr , pixelHeight ) ;
207209 }
210+ noteHeight = ( harr [ 0 ] + harr [ 1 ] ) / 2 ;
208211 }
209- return new Node ( lowerHeight , pixelHeight ) ;
212+ return new Node ( noteHeight , pixelHeight ) ;
210213 }
211214
212215 computeForceLayout ( prevProps , prevState ) {
@@ -250,7 +253,7 @@ class NoteLayoutProvider extends StatefulComponent<
250253 return this . updateState ( { nodes : { $set : nodesObj } } ) ;
251254 }
252255
253- registerHeight ( id , height ) {
256+ updateHeight ( id , height ) {
254257 if ( height == null ) {
255258 return ;
256259 }
@@ -332,4 +335,12 @@ const NoteUnderlay = function ({ fill, ...rest }) {
332335 } ) ;
333336} ;
334337
338+ export function useNoteLayout ( ) {
339+ const ctx = useContext ( NoteLayoutContext ) ;
340+ if ( ctx == null ) {
341+ throw new Error ( "useNoteLayout must be used within a NoteLayoutProvider" ) ;
342+ }
343+ return ctx ;
344+ }
345+
335346export { NoteLayoutContext , NoteLayoutProvider , NoteRect , NoteUnderlay } ;
0 commit comments