UNPKG

@macrostrat/column-components

Version:

React rendering primitives for stratigraphic columns

1 lines 6.22 kB
{"version":3,"file":"label-primitives.cjs","sources":["../../src/notes/label-primitives.ts"],"sourcesContent":["import labella from \"labella\";\n\nconst { Node, Renderer, Force } = labella;\n\nexport class FlexibleNode {\n allowedRange: [number, number];\n centerPos: number;\n currentPos: number;\n width: number;\n data: any | null;\n layerIndex: number;\n parent: FlexibleNode | null;\n child: FlexibleNode | null;\n constructor(\n allowedRange: [number, number],\n width: number,\n data: any | null = null,\n ) {\n this.allowedRange = allowedRange;\n this.centerPos = (this.allowedRange[0] + this.allowedRange[1]) / 2;\n this.currentPos = this.centerPos;\n this.width = width;\n this.data = data;\n this.layerIndex = 0;\n }\n\n get idealPos() {\n const [end, start] = this.allowedRange;\n if (this.currentPos < start) {\n return start;\n }\n if (this.currentPos > end) {\n return end;\n }\n return this.currentPos;\n }\n\n // return negative if overlap\n distanceFrom(node) {\n const halfWidth = this.width / 2;\n const nodeHalfWidth = node.width / 2;\n // max(a[0], b[0]) - min(a[1], b[1])\n return (\n Math.max(this.currentPos - halfWidth, node.currentPos - nodeHalfWidth) -\n Math.min(this.currentPos + halfWidth, node.currentPos + nodeHalfWidth)\n );\n }\n\n moveToIdealPosition() {\n this.currentPos = this.idealPos;\n return this;\n }\n\n displacement() {\n return this.idealPos - this.currentPos;\n }\n\n overlapWithNode(node, buffer = 0) {\n return this.distanceFrom(node) - buffer < 0;\n }\n\n overlapWithPoint(pos) {\n const halfWidth = this.width / 2;\n return (\n pos >= this.currentPos - halfWidth && pos <= this.currentPos + halfWidth\n );\n }\n\n positionBefore(node, buffer = 0) {\n return node.currentLeft() - this.width / 2 - buffer;\n }\n\n positionAfter(node, buffer = 0) {\n return node.currentRight() + this.width / 2 + buffer;\n }\n\n currentRight() {\n return this.currentPos + this.width / 2;\n }\n\n currentLeft() {\n return this.currentPos - this.width / 2;\n }\n\n idealRight() {\n return this.idealPos + this.width / 2;\n }\n\n idealLeft() {\n return this.idealPos - this.width / 2;\n }\n\n createStub(width) {\n const stub = new Node(this.idealPos, width, this.data);\n stub.currentPos = this.currentPos;\n stub.child = this;\n this.parent = stub;\n return stub;\n }\n\n removeStub() {\n if (this.parent) {\n this.parent.child = null;\n this.parent = null;\n }\n return this;\n }\n\n isStub() {\n return !!this.child;\n }\n\n getPathToRoot() {\n const path = [];\n let current: FlexibleNode = this;\n while (current) {\n path.push(current);\n current = current.parent;\n }\n return path;\n }\n\n getPathFromRoot() {\n return this.getPathToRoot().reverse();\n }\n\n getPathToRootLength() {\n let length = 0;\n let current: FlexibleNode = this;\n while (current) {\n const targetPos = current.parent\n ? current.parent.currentPos\n : current.idealPos;\n length += Math.abs(current.currentPos - targetPos);\n current = current.parent;\n }\n\n return length;\n }\n\n // Trace back to the node without parent\n getRoot() {\n let previous: FlexibleNode = this;\n let current: FlexibleNode = this;\n while (current) {\n previous = current;\n current = current.parent;\n }\n return previous;\n }\n\n getLayerIndex() {\n return this.layerIndex;\n }\n\n clone() {\n const node = new FlexibleNode(this.allowedRange, this.width, this.data);\n node.currentPos = this.currentPos;\n node.layerIndex = this.layerIndex;\n return node;\n }\n}\n\nexport { Node, Renderer, Force };\n"],"names":["labella"],"mappings":";;;AAEA,MAAM,EAAE,MAAM,UAAU,UAAUA;AAE3B,MAAM,aAAa;AAAA,EACxB;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA,YACE,cACA,OACA,OAAmB,MACnB;AACA,SAAK,eAAe;AACpB,SAAK,aAAa,KAAK,aAAa,CAAC,IAAI,KAAK,aAAa,CAAC,KAAK;AACjE,SAAK,aAAa,KAAK;AACvB,SAAK,QAAQ;AACb,SAAK,OAAO;AACZ,SAAK,aAAa;AAAA,EACpB;AAAA,EAEA,IAAI,WAAW;AACb,UAAM,CAAC,KAAK,KAAK,IAAI,KAAK;AAC1B,QAAI,KAAK,aAAa,OAAO;AAC3B,aAAO;AAAA,IACT;AACA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