{"version":3,"file":"blockLayout.cjs","sources":["../../../../../../../src/components/ui/editor/ihub-editor/blocks/blockLayout.ts"],"sourcesContent":["import type { EditorView } from \"@tiptap/pm/view\";\nimport type { Node as PMNode } from \"@tiptap/pm/model\";\nimport { CONTAINER_TYPES, canDropAt } from \"./blockOps\";\n\nexport interface LaidOutBlock {\n  pos: number;\n  node: PMNode;\n  el: HTMLElement;\n  rect: DOMRect;\n}\n\nexport interface Rect {\n  top: number;\n  bottom: number;\n}\n\n/** Pixels inside a container's first/last child that still count as \"inside\". */\nconst INNER_EDGE = 6;\n\n/** Insertion index for pointer Y: before the first block whose midpoint is below it. */\nexport function computeDropIndex(rects: Rect[], y: number): number {\n  for (let i = 0; i < rects.length; i += 1) {\n    if (y < (rects[i].top + rects[i].bottom) / 2) return i;\n  }\n  return rects.length;\n}\n\n/** Y (viewport) of the line between blocks for a drop index. */\nexport function indicatorY(rects: Rect[], index: number): number {\n  if (!rects.length) return 0;\n  if (index <= 0) return rects[0].top - 4;\n  if (index >= rects.length) return rects[rects.length - 1].bottom + 4;\n  return (rects[index - 1].bottom + rects[index].top) / 2;\n}\n\n/** Movable child blocks of `parent` (content starting at `start`) with their DOM. */\nexport function childBlocks(view: EditorView, parent: PMNode, start: number): LaidOutBlock[] {\n  const out: LaidOutBlock[] = [];\n  parent.forEach((node, offset) => {\n    if (node.type.name === \"toggleSummary\") return;\n    const pos = start + offset;\n    const dom = view.nodeDOM(pos);\n    if (dom instanceof HTMLElement) out.push({ pos, node, el: dom, rect: dom.getBoundingClientRect() });\n  });\n  return out;\n}\n\nfunction nearest(blocks: LaidOutBlock[], y: number): LaidOutBlock | null {\n  let best: LaidOutBlock | null = null;\n  let distance = Infinity;\n  blocks.forEach((block) => {\n    const { top, bottom } = block.rect;\n    const d = y < top ? top - y : y > bottom ? y - bottom : 0;\n    if (d < distance) {\n      distance = d;\n      best = block;\n    }\n  });\n  return best;\n}\n\n/** Children of a container when the pointer is over its inner content, else null. */\nfunction innerLevel(view: EditorView, block: LaidOutBlock, y: number): LaidOutBlock[] | null {\n  const { node } = block;\n  if (!CONTAINER_TYPES.has(node.type.name)) return null;\n  if (node.type.name === \"toggleBlock\" && !node.attrs.open) return null;\n  const inner = childBlocks(view, node, block.pos + 1);\n  if (!inner.length) return null;\n  const top = inner[0].rect.top - INNER_EDGE;\n  const bottom = inner[inner.length - 1].rect.bottom + INNER_EDGE;\n  return y >= top && y <= bottom ? inner : null;\n}\n\n/**\n * The list of sibling blocks the pointer is at: the page's top-level blocks,\n * or — when the pointer is over a banner / callout / open toggle's content —\n * that container's children (recursively). `skipPos` stops descent into the\n * block being dragged, so it can't be dropped into itself.\n */\nexport function levelAt(view: EditorView, y: number, skipPos?: number): LaidOutBlock[] {\n  let level = childBlocks(view, view.state.doc, 0);\n  for (;;) {\n    const hit = nearest(level, y);\n    if (!hit || hit.pos === skipPos) return level;\n    const inner = innerLevel(view, hit, y);\n    if (!inner) return level;\n    level = inner;\n  }\n}\n\n/** The block under the pointer at the deepest level (for the hover handle). */\nexport function blockAt(view: EditorView, y: number): LaidOutBlock | null {\n  return nearest(levelAt(view, y), y);\n}\n\nexport interface DropTarget {\n  /** Gap position the block will be inserted at. */\n  pos: number;\n  /** Viewport coordinates of the indicator line. */\n  y: number;\n  left: number;\n  width: number;\n}\n\n/** Where a drag of the block at `fromPos` would land for pointer Y, or null for a no-op / invalid drop. */\nexport function dropTargetAt(view: EditorView, y: number, fromPos: number): DropTarget | null {\n  const source = view.state.doc.nodeAt(fromPos);\n  if (!source) return null;\n  const level = levelAt(view, y, fromPos);\n  if (!level.length) return null;\n  const rects = level.map((b) => b.rect);\n  const index = computeDropIndex(rects, y);\n  const pos = index < level.length ? level[index].pos : level[level.length - 1].pos + level[level.length - 1].node.nodeSize;\n  const end = fromPos + source.nodeSize;\n  if (pos >= fromPos && pos <= end) return null;\n  if (!canDropAt(view.state.doc, source, pos)) return null;\n  const left = Math.min(...rects.map((r) => r.left));\n  const right = Math.max(...rects.map((r) => r.right));\n  return { pos, y: indicatorY(rects, index), left, width: right - left };\n}\n"],"names":["computeDropIndex","rects","y","i","length","top","bottom","indicatorY","index","childBlocks","view","parent","start","out","forEach","node","offset","type","name","pos","dom","nodeDOM","HTMLElement","push","el","rect","getBoundingClientRect","nearest","blocks","best","distance","Infinity","block","_block$rect","d","innerLevel","CONTAINER_TYPES","has","attrs","open","inner","levelAt","skipPos","level","state","doc","hit","fromPos","source","nodeAt","map","b","nodeSize","end","canDropAt","left","Math","min","apply","_toConsumableArray","r","right","max","width"],"mappings":"0KAoBgB,SAAAA,EAAiBC,EAAeC,GAC9C,IAAK,IAAIC,EAAI,EAAGA,EAAIF,EAAMG,OAAQD,GAAK,EACrC,GAAID,GAAKD,EAAME,GAAGE,IAAMJ,EAAME,GAAGG,QAAU,EAAG,OAAOH,EAEvD,OAAOF,EAAMG,MACf,CAGgB,SAAAG,EAAWN,EAAeO,GACxC,OAAKP,EAAMG,OACPI,GAAS,EAAUP,EAAM,GAAGI,IAAM,EAClCG,GAASP,EAAMG,OAAeH,EAAMA,EAAMG,OAAS,GAAGE,OAAS,GAC3DL,EAAMO,EAAQ,GAAGF,OAASL,EAAMO,GAAOH,KAAO,EAH5B,CAI5B,UAGgBI,EAAYC,EAAkBC,EAAgBC,GAC5D,IAAMC,EAAsB,GAO5B,OANAF,EAAOG,QAAQ,SAACC,EAAMC,GACpB,GAAuB,kBAAnBD,EAAKE,KAAKC,KAAd,CACA,IAAMC,EAAMP,EAAQI,EACdI,EAAMV,EAAKW,QAAQF,GACrBC,aAAeE,aAAaT,EAAIU,KAAK,CAAEJ,IAAAA,EAAKJ,KAAAA,EAAMS,GAAIJ,EAAKK,KAAML,EAAIM,yBAHjC,CAI1C,GACOb,CACT,CAEA,SAASc,EAAQC,EAAwB1B,GACvC,IAAI2B,EAA4B,KAC5BC,EAAWC,IASf,OARAH,EAAOd,QAAQ,SAACkB,GACd,IAAAC,EAAwBD,EAAMP,KAAtBpB,EAAG4B,EAAH5B,IAAKC,EAA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