{"version":3,"file":"connector.cjs","sources":["../../src/notes/connector.ts"],"sourcesContent":["import { useContext, forwardRef, ReactNode } from \"react\";\nimport h from \"../hyper\";\nimport { NoteLayoutContext } from \"./layout\";\nimport { HeightRangeAnnotation } from \"./height-range\";\nimport { ForeignObject } from \"../util\";\n\ninterface NotePositionerProps {\n  offsetY: number;\n  noteHeight: number;\n  children?: ReactNode;\n  onClick?: (event: React.MouseEvent) => void;\n}\n\nconst NotePositioner = forwardRef(function (\n  props: NotePositionerProps,\n  ref: any,\n) {\n  let { offsetY, noteHeight, onClick, children } = props;\n  const { width, paddingLeft } = useContext(NoteLayoutContext);\n  if (noteHeight == null) {\n    noteHeight = 0;\n  }\n  const outerPad = 5;\n\n  let y = offsetY - noteHeight / 2 - outerPad;\n\n  return h(\n    ForeignObject,\n    {\n      width: width - paddingLeft + 2 * outerPad,\n      x: paddingLeft - outerPad,\n      y,\n      height: noteHeight + 2 * outerPad,\n      overflow: \"visible\",\n      style: { overflowY: \"visible\" },\n    },\n    [\n      h(\n        \"div.note-inner\",\n        {\n          ref,\n          onClick,\n        },\n        children,\n      ),\n    ],\n  );\n});\n\nconst findIndex = function (note) {\n  const { notes } = useContext(NoteLayoutContext);\n  return notes.indexOf(note);\n};\n\nexport interface NodeConnectorOptions {\n  deltaConnectorAttachment?: number; // Delta for connector attachment\n  /** Pixels the connector continues past each end, so it meets what it points\n   * at rather than stopping short of it: whatever marks the note's height in\n   * the column on one side, the note body on the other. One number applies to\n   * both ends; a pair is `[start, end]`. The continuation past the note body\n   * is drawn under it. */\n  connectorOverhang?: number | [number, number];\n  /** Draw a marker at the note's height when the note has no extent\n   * (default `true`). A note whose connector runs into something already\n   * drawn at that height doesn't need one. */\n  showPointMarker?: boolean;\n}\n\ntype NodeConnectorProps = NodeConnectorOptions & {\n  note: any; // Note data type\n  node?: any; // Node data type\n  index?: number; // Index of the note in the layout\n};\n\nconst NoteConnector = function (props: NodeConnectorProps) {\n  let {\n    note,\n    node,\n    deltaConnectorAttachment,\n    index,\n    connectorOverhang = 0,\n    showPointMarker = true,\n  } = props;\n  // Try to avoid scanning for index if we can\n  if (index == null) {\n    index = findIndex(note);\n  }\n  const { nodes, columnIndex, generatePath, paddingLeft } =\n    useContext(NoteLayoutContext);\n  const { height, top_height } = note;\n\n  if (node == null) {\n    node = nodes[note.id];\n  }\n\n  if (node != null && deltaConnectorAttachment != null) {\n    node.currentPos += deltaConnectorAttachment;\n  }\n\n  const offsetX = (columnIndex[index] || 0) * 5;\n\n  return h([\n    h(HeightRangeAnnotation, {\n      offsetX,\n      height,\n      top_height,\n      showPointMarker,\n    }),\n    h(\"g.note-connector\", { transform: `translate(${offsetX})` }, [\n      ...connectorContinuations(node, paddingLeft - offsetX, connectorOverhang),\n      h(\"path.link.col-note-link\", { d: generatePath(node, offsetX) }),\n    ]),\n  ]);\n};\n\n/** Straight segments continuing the connector past each of its ends. The\n * connector runs from the note's position in the column (`idealPos`, where\n * the layout wanted it) to the note body (`currentPos`, where it fits), so\n * the continuations are horizontal at those two heights. */\nfunction connectorContinuations(\n  node: any,\n  layerGap: number,\n  overhang: number | [number, number],\n) {\n  if (node == null) return [];\n  let start = overhang;\n  let end = overhang;\n  if (Array.isArray(overhang)) {\n    [start, end] = overhang;\n  }\n\n  const segments = [];\n  if (start > 0) {\n    segments.push(\n      h(\"line.link.connector-continuation\", {\n        key: \"start\",\n        x1: -start,\n        x2: 0,\n        y1: node.idealPos,\n        y2: node.idealPos,\n      }),\n    );\n  }\n  // `layerGap` comes from the layout context, which is empty on first render\n  if (end > 0 && Number.isFinite(layerGap)) {\n    segments.push(\n      h(\"line.link.connector-continuation\", {\n        key: \"end\",\n        x1: layerGap,\n        x2: layerGap + end,\n        y1: node.currentPos,\n        y2: node.currentPos,\n      }),\n    );\n  }\n  return segments;\n}\n\nexport { NotePositioner, NoteConnector };\n"],"names":["forwardRef","useContext","NoteLayoutContext","h","ForeignObject","index","HeightRangeAnnotation"],"mappings":";;;;;;;AAaA,MAAM,iBAA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