UNPKG

@macrostrat/column-components

Version:

React rendering primitives for stratigraphic columns

116 lines (115 loc) 3.16 kB
"use strict"; 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 connector = require("./connector.cjs"); const column = require("../context/column.cjs"); function NotesList(props) { let { onClickNote, ...rest } = props; const { notes, nodes: nodeIndex, updateHeight, noteComponent, scale } = layout.useNoteLayout(); const { pixelHeight: columnHeight } = column.useColumn(); const notesInfo = React.useMemo(() => { let notes1 = notes.map((note) => { const node = nodeIndex[note.id]; const pixelHeight = node?.width ?? 10; const pixelOffset = node?.currentPos ?? scale(note.top_height); return { note, node, pixelOffset, pixelHeight, spacing: { above: pixelOffset - pixelHeight, below: columnHeight - pixelOffset } }; }); for (let i = 0; i < notes1.length; i++) { const { spacing, pixelOffset } = notes1[i]; if (i > 0) { const prevNote = notes1[i - 1]; spacing.above = pixelOffset - prevNote.pixelOffset - prevNote.pixelHeight; prevNote.spacing.below = spacing.above; } } return notes1; }, [notes, nodeIndex, scale]); return hyper( "g.notes-list", notesInfo.map(({ note, pixelOffset, pixelHeight, spacing }) => { if (pixelOffset == null || pixelHeight == null) { return null; } return hyper(Note, { key: note.id, note, pixelOffset, pixelHeight, updateHeight, onClick: onClickNote, noteBodyComponent: noteComponent, spacing, ...rest }); }) ); } function Note(props) { const { note, pixelOffset, pixelHeight, updateHeight, deltaConnectorAttachment, noteBodyComponent, spacing, onClick } = props; const ref = React.useRef(null); React.useEffect(() => { if (ref.current) { const newHeight = ref.current.offsetHeight; if (newHeight !== pixelHeight) { updateHeight(note.id, newHeight); } } }, [note, pixelHeight, updateHeight]); const offsetY = pixelOffset; const noteHeight = pixelHeight; const { setEditingNote, editingNote } = React.useContext(editor.NoteEditorContext); const onClick_ = onClick ?? setEditingNote; const _onClickHandler = React.useMemo(() => { if (!onClick_) return void 0; return (evt) => { onClick_(note); }; }, [onClick_]); if (editingNote === note) { return null; } return hyper("g.note", [ hyper(connector.NoteConnector, { note, deltaConnectorAttachment }), hyper( connector.NotePositioner, { offsetY, noteHeight, ref, onClick: _onClickHandler }, hyper(noteBodyComponent, { note, spacing }) ) ]); } exports.NoteConnector = connector.NoteConnector; exports.NotePositioner = connector.NotePositioner; exports.NotesList = NotesList; //# sourceMappingURL=note.cjs.map