UNPKG

@macrostrat/column-components

Version:

React rendering primitives for stratigraphic columns

188 lines (187 loc) 5.15 kB
"use strict"; Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" }); const React = require("react"); const h = require("@macrostrat/hyper"); const d3Path = require("d3-path"); const layout = require("./context/layout.cjs"); const columnBox = require("./util/column-box.cjs"); const _interopDefault = (e) => e && e.__esModule ? e : { default: e }; const h__default = /* @__PURE__ */ _interopDefault(h); let sequence = 0; function getUniqueIdentifier() { const id = `uuid-${sequence}`; sequence += 1; return id; } class UUIDComponent extends React.Component { UUID; constructor(props) { super(props); this.UUID = getUniqueIdentifier(); } } const UUIDContext = React.createContext(null); function useBasicUUID() { const ref = React.useRef(getUniqueIdentifier()); return ref.current; } const useUUID = function() { const uuid = React.useContext(UUIDContext); if (uuid == null) { return useBasicUUID(); } return uuid; }; function UUIDProvider({ children }) { const ref = React.useRef(getUniqueIdentifier()); return h__default.default(UUIDContext.Provider, { value: ref.current, children }); } function SimpleFrame(props) { const { pixelHeight: height, width } = React.useContext(layout.ColumnLayoutContext); let { id: frameID, className } = props; if (frameID.startsWith("#")) { frameID = frameID.slice(1); } return h__default.default("rect", { id: frameID, x: 0, y: 0, width, height, className }); } function GrainsizeFrame(props) { let { id: frameID, zigZagBottom = false, zigZagTop = false } = props; const { scale, divisions, grainsizeScale: gs } = layout.useColumnLayout(); if (frameID.startsWith("#")) { frameID = frameID.slice(1); } if (divisions.length === 0) { return null; } const [bottomOfSection, topOfSection] = scale.domain(); const topOf = function(d2) { let { top } = d2; if (top > topOfSection) { top = topOfSection; } return scale(top); }; const bottomOf = function(d2) { let { bottom } = d2; if (bottom < bottomOfSection) { bottom = bottomOfSection; } return scale(bottom); }; const filteredDivisions = Array.from(divisions).filter(function(d2) { if (d2.top <= bottomOfSection) { return false; } if (d2.bottom > topOfSection) { return false; } return true; }); let d = d3Path.path(); let currentGrainsize = "m"; let i = 0; for (const div of filteredDivisions) { if (i === 0) { const y = bottomOf(div); d.moveTo(0, y); } if (div.grainsize != null) { currentGrainsize = div.grainsize; } const x1 = gs(currentGrainsize); if (i === 0 && zigZagBottom) { columnBox.drawZigZagAtConstantHeight(d, 0, x1, bottomOf(div)); } else { d.lineTo(x1, bottomOf(div)); } d.lineTo(x1, bottomOf(div)); d.lineTo(x1, topOf(div)); if (i === filteredDivisions.length - 1) { if (zigZagTop) { columnBox.drawZigZagAtConstantHeight(d, x1, 0, topOf(div)); } else { d.lineTo(0, topOf(div)); } } i++; } d.closePath(); return h__default.default("path", { id: frameID, key: frameID, d: d.toString() }); } const ClipPath = function(props) { let { id, children, ...rest } = props; if (id.startsWith("#")) { id = id.slice(1); } return React.createElement("clipPath", { id, key: id, ...rest }, children); }; const UseFrame = function(props) { const { id: frameID, ...rest } = props; return h__default.default("use.frame", { xlinkHref: frameID, fill: "transparent", key: "frame", ...rest }); }; const prefixID = function(uuid, prefixes) { const res = {}; for (let prefix of Array.from(prefixes)) { res[prefix + "ID"] = `#${uuid}-${prefix}`; } return res; }; function ClippingFrame(props) { const { left = 0, shiftY = 0, className, onClick, children, frame = SimpleFrame, clip = true } = props; const uuid = useUUID(); const { frameID, clipID } = prefixID(uuid, ["frame", "clip"]); let transform = null; if (left != null) { transform = `translate(${left} ${shiftY})`; } const frameClassName = "clip-frame column-clip-frame"; let _frame = h__default.default(frame, { id: frameID, className: frameClassName }); let defs = null; let clipPath = null; if (clip) { defs = h__default.default("defs", { key: "defs" }, [ _frame, h__default.default(ClipPath, { id: clipID }, h__default.default(UseFrame, { id: frameID })) ]); clipPath = `url(${clipID})`; _frame = h__default.default(UseFrame, { id: frameID, className: frameClassName }); } return h__default.default("g", { className, transform, onClick }, [ defs, h__default.default("g.inner", { clipPath }, children), // Frame must go last _frame ]); } exports.ClipPath = ClipPath; exports.ClippingFrame = ClippingFrame; exports.GrainsizeFrame = GrainsizeFrame; exports.SimpleFrame = SimpleFrame; exports.UUIDComponent = UUIDComponent; exports.UUIDProvider = UUIDProvider; exports.useUUID = useUUID; //# sourceMappingURL=frame.cjs.map