@macrostrat/column-components
Version:
React rendering primitives for stratigraphic columns
87 lines (86 loc) • 2.63 kB
JavaScript
;
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const React = require("react");
const hyper = require("../hyper.cjs");
const layout = require("./layout.cjs");
const editor = require("./editor.cjs");
const heightRange = require("./height-range.cjs");
const defs = require("./defs.cjs");
const modelEditor = require("../context/model-editor.cjs");
const getHeights = function(position, tolerance = 0.1) {
let { startHeight, dragHeight, ...rest } = position;
if (dragHeight == null) {
dragHeight = startHeight;
}
let rng = [startHeight, dragHeight];
rng.sort();
let [height, top_height] = rng;
if (top_height - height < tolerance) {
top_height = null;
}
return { height, top_height, ...rest };
};
const HeightRange = function(props) {
let { position, tolerance } = props;
if (!position) {
return null;
}
if (tolerance == null) {
tolerance = 0.1;
}
const val = getHeights(position, tolerance);
return hyper(heightRange.HeightRangeAnnotation, val);
};
const NewNotePositioner = function(props) {
const { paddingLeft, scale } = React.useContext(layout.NoteLayoutContext);
const { onCreateNote } = React.useContext(editor.NoteEditorContext);
if (onCreateNote == null) {
return null;
}
const { tolerance } = props;
const [notePosition, setPosition] = React.useState(null);
const { model } = React.useContext(modelEditor.ModelEditorContext);
if (model != null) {
return null;
}
const eventHeight = (evt) => scale.invert(evt.nativeEvent.offsetY);
return hyper("g.new-note", [
hyper(defs, { fill: "dodgerblue", size: 4, prefix: "new_" }),
hyper(layout.NoteRect, {
width: paddingLeft,
fill: "transparent",
padding: 0,
style: { cursor: "drag" },
onMouseDown(evt) {
if (notePosition != null) {
return;
}
return setPosition({
startHeight: eventHeight(evt),
offsetX: evt.nativeEvent.offsetX
});
},
onMouseMove(evt) {
if (notePosition == null) {
return;
}
return setPosition({
...notePosition,
dragHeight: eventHeight(evt)
});
},
onMouseUp(evt) {
const dragHeight = eventHeight(evt);
const finalPos = getHeights({ ...notePosition, dragHeight });
setPosition(null);
return onCreateNote(finalPos);
}
}),
hyper(HeightRange, { position: notePosition })
]);
};
NewNotePositioner.defaultProps = {
tolerance: 0.1
};
exports.NewNotePositioner = NewNotePositioner;
//# sourceMappingURL=new.cjs.map