{"version":3,"file":"blockOps.cjs","sources":["../../../../../../../src/components/ui/editor/ihub-editor/blocks/blockOps.ts"],"sourcesContent":["import { Fragment, type Node as PMNode } from \"@tiptap/pm/model\";\nimport { NodeSelection, TextSelection, type EditorState, type Transaction } from \"@tiptap/pm/state\";\n\n/** Blocks whose children can be dragged in and out (their content is `block+`). */\nexport const CONTAINER_TYPES = new Set([\"sectionBanner\", \"callout\", \"toggleBlock\"]);\n\n/** First child index that can hold a moved block (a toggle's summary stays first). */\nexport function firstMovableIndex(parent: PMNode): number {\n  return parent.type.name === \"toggleBlock\" ? 1 : 0;\n}\n\nexport interface TopBlock {\n  node: PMNode;\n  /** Position directly before the block. */\n  pos: number;\n  index: number;\n}\n\n/** The top-level block containing `pos`. */\nexport function topBlockAt(doc: PMNode, pos: number): TopBlock | null {\n  if (!doc.childCount) return null;\n  const clamped = Math.max(0, Math.min(pos, doc.content.size));\n  const index = Math.min(doc.resolve(clamped).index(0), doc.childCount - 1);\n  return topBlockByIndex(doc, index);\n}\n\n/** The top-level block at child `index`. */\nexport function topBlockByIndex(doc: PMNode, index: number): TopBlock | null {\n  if (index < 0 || index >= doc.childCount) return null;\n  let offset = 0;\n  for (let i = 0; i < index; i += 1) offset += doc.child(i).nodeSize;\n  return { node: doc.child(index), pos: offset, index };\n}\n\n/**\n * The innermost block around the selection that can be moved: a top-level block\n * or a direct child of a container (never a toggle's summary line).\n */\nexport function movableBlockAt(state: EditorState): number | null {\n  const { selection } = state;\n  if (selection instanceof NodeSelection) return selection.from;\n  const $from = selection.$from;\n  for (let depth = $from.depth; depth >= 1; depth -= 1) {\n    const node = $from.node(depth);\n    const parent = $from.node(depth - 1);\n    if (node.type.name === \"toggleSummary\") continue;\n    if (depth === 1 || CONTAINER_TYPES.has(parent.type.name)) return $from.before(depth);\n  }\n  return null;\n}\n\n/** Put the caret (or a node selection for atoms) inside the block at `pos`. */\nfunction selectBlock(tr: Transaction, pos: number): Transaction {\n  const node = tr.doc.nodeAt(pos);\n  if (!node) return tr;\n  if (node.isTextblock) return tr.setSelection(TextSelection.create(tr.doc, pos + 1));\n  if (NodeSelection.isSelectable(node)) return tr.setSelection(NodeSelection.create(tr.doc, pos));\n  return tr.setSelection(TextSelection.near(tr.doc.resolve(pos + 1)));\n}\n\n/** Can `node` be placed at the gap `pos` (a position between blocks)? */\nexport function canDropAt(doc: PMNode, node: PMNode, pos: number): boolean {\n  if (pos < 0 || pos > doc.content.size) return false;\n  const $pos = doc.resolve(pos);\n  // Must be a gap between blocks, not a position inside text.\n  if ($pos.parent.inlineContent) return false;\n  if ($pos.index() < firstMovableIndex($pos.parent)) return false;\n  return $pos.parent.canReplace($pos.index(), $pos.index(), Fragment.from(node));\n}\n\n/**\n * Move the block at `fromPos` to the gap at `toPos` (any depth: into or out of\n * banners, callouts and toggles). Returns null for no-op or invalid drops,\n * including dropping a container into itself.\n */\nexport function moveBlock(state: EditorState, fromPos: number, toPos: number): Transaction | null {\n  const node = state.doc.nodeAt(fromPos);\n  if (!node) return null;\n  const end = fromPos + node.nodeSize;\n  if (toPos >= fromPos && toPos <= end) return null;\n  if (!canDropAt(state.doc, node, toPos)) return null;\n  const tr = state.tr.delete(fromPos, end);\n  const target = tr.mapping.map(toPos);\n  tr.insert(target, node);\n  return selectBlock(tr, target).scrollIntoView();\n}\n\n/** Swap a block with its previous (-1) or next (+1) sibling inside the same parent. */\nexport function moveBlockSibling(state: EditorState, pos: number, direction: -1 | 1): Transaction | null {\n  const $pos = state.doc.resolve(pos);\n  const parent = $pos.parent;\n  const index = $pos.index();\n  const target = direction === -1 ? index - 1 : index + 2;\n  if (target < firstMovableIndex(parent) || target > parent.childCount) return null;\n  return moveBlock(state, pos, $pos.posAtIndex(target));\n}\n\nexport function duplicateBlock(state: EditorState, pos: number): Transaction | null {\n  const node = state.doc.nodeAt(pos);\n  if (!node) return null;\n  const at = pos + node.nodeSize;\n  const tr = state.tr.insert(at, node.copy(node.content));\n  return selectBlock(tr, at).scrollIntoView();\n}\n\nexport function deleteBlock(state: EditorState, pos: number): Transaction | null {\n  const node = state.doc.nodeAt(pos);\n  if (!node) return null;\n  const tr = state.tr.delete(pos, pos + node.nodeSize);\n  // Never leave the document empty.\n  if (tr.doc.childCount === 0) tr.insert(0, state.schema.nodes.paragraph.create());\n  const $at = tr.doc.resolve(Math.min(tr.mapping.map(pos), tr.doc.content.size));\n  return tr.setSelection(TextSelection.near($at)).scrollIntoView();\n}\n\n/** Insert an empty paragraph after the block and put the caret in it. */\nexport function insertParagraphAfterBlock(state: EditorState, pos: number): Transaction | null {\n  const node = state.doc.nodeAt(pos);\n  if (!node) return null;\n  const at = pos + node.nodeSize;\n  const tr = state.tr.insert(at, state.schema.nodes.paragraph.create());\n  return tr.setSelection(TextSelection.create(tr.doc, at + 1)).scrollIntoView();\n}\n\n// ---- Top-level (index-based) wrappers ----\n\n/**\n * Move the top-level block at `from` so it lands before the block currently at\n * `to` (`to === childCount` means the end). Returns null for a no-op drop.\n */\nexport function moveTopBlockTo(state: EditorState, from: number, to: number): Transaction | null {\n  const block = topBlockByIndex(state.doc, from);\n  if (!block || to < 0 || to > state.doc.childCount) return null;\n  const toPos = to === state.doc.childCount ? state.doc.content.size : topBlockByIndex(state.doc, to)!.pos;\n  return moveBlock(state, block.pos, toPos);\n}\n\n/** Swap the top-level block at `index` with its neighbour above (-1) or below (+1). */\nexport function moveTopBlock(state: EditorState, index: number, direction: -1 | 1): Transaction | null {\n  const block = topBlockByIndex(state.doc, index);\n  return block ? moveBlockSibling(state, block.pos, direction) : null;\n}\n\nexport function duplicateTopBlock(state: EditorState, index: number): Transaction | null {\n  const block = topBlockByIndex(state.doc, index);\n  return block ? duplicateBlock(state, block.pos) : null;\n}\n\nexport function deleteTopBlock(state: EditorState, index: number): Transaction | null {\n  const block = topBlockByIndex(state.doc, index);\n  return block ? deleteBlock(state, block.pos) : null;\n}\n\nexport function insertParagraphAfter(state: EditorState, index: number): Transaction | null {\n  const block = topBlockByIndex(state.doc, index);\n  return block ? insertParagraphAfterBlock(state, block.pos) : null;\n}\n"],"names":["CONTAINER_TYPES","Set","firstMovableIndex","parent","type","name","selectBlock","tr","pos","node","doc","nodeAt","isTextblock","setSelection","TextSelection","create","NodeSelection","isSelectable","near","resolve","canDropAt","content","size","$pos","inlineContent","index","canReplace","Fragment","from","moveBlock","state","fromPos","toPos","end","nodeSize","target","mapping","map","insert","scrollIntoView","childCount","schema","nodes","paragraph","$at","Math","min","at","copy","selection","$from","depth","has","before","direction","posAtIndex"],"mappings":"yTAIaA,EAAkB,IAAIC,IAAI,CAAC,gBAAiB,UAAW,gBAG9D,SAAUC,EAAkBC,GAChC,MAA4B,gBAArBA,EAAOC,KAAKC,KAAyB,EAAI,CAClD,CA2CA,SAASC,EAAYC,EAAiBC,GACpC,IAAMC,EAAOF,EAAGG,IAAIC,OAAOH,GAC3B,OAAKC,EACDA,EAAKG,YAAoBL,EAAGM,aAAaC,EAAAA,cAAcC,OAAOR,EAAGG,IAAKF,EAAM,IAC5EQ,EAAaA,cAACC,aAAaR,GAAcF,EAAGM,aAAaG,EAAaA,cAACD,OAAOR,EAAGG,IAAKF,IACnFD,EAAGM,aAAaC,EAAaA,cAACI,KAAKX,EAAGG,IAAIS,QAAQX,EAAM,KAH7CD,CAIpB,UAGgBa,EAAUV,EAAaD,EAAcD,GACnD,GAAIA,EAAM,GAAKA,EAAME,EAAIW,QAAQC,KAAM,OAAO,EAC9C,IAAMC,EAAOb,EAAIS,QAAQX,GAEzB,OAAIe,EAAKpB,OAAOqB,kBACZD,EAAKE,QAAUvB,EAAkBqB,EAAKpB,UACnCoB,EAAKpB,OAAOuB,WAAWH,EAAKE,QAASF,EAAKE,QAASE,EAAQA,SAACC,KAAKnB,IAC1E,UAOgBoB,EAAUC,EAAoBC,EAAiBC,GAC7D,IAAMvB,EAAOqB,EAAMpB,IAAIC,OAAOoB,GAC9B,IAAKtB,EAAM,OAAO,KAClB,IAAMwB,EAAMF,EAAUtB,EAAKyB,SAC3B,GAAIF,GAASD,GAAW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