{"version":3,"file":"tableOps.cjs","sources":["../../../../../../../src/components/ui/editor/ihub-editor/table/tableOps.ts"],"sourcesContent":["import type { Node as PMNode } from \"@tiptap/pm/model\";\nimport type { EditorState, Transaction } from \"@tiptap/pm/state\";\nimport { CellSelection, TableMap } from \"@tiptap/pm/tables\";\n\nexport type Axis = \"row\" | \"column\";\n\nexport interface TableRef {\n  node: PMNode;\n  /** Position directly before the table node. */\n  pos: number;\n}\n\n/** Find the table node at (or wrapping) `pos`. */\nexport function findTableAt(doc: PMNode, pos: number): TableRef | null {\n  const $pos = doc.resolve(Math.max(0, Math.min(pos, doc.content.size)));\n  for (let depth = $pos.depth; depth > 0; depth -= 1) {\n    const node = $pos.node(depth);\n    if (node.type.spec.tableRole === \"table\") return { node, pos: $pos.before(depth) };\n  }\n  return null;\n}\n\nexport function hasMergedCells(table: PMNode): boolean {\n  let merged = false;\n  table.descendants((node) => {\n    if (merged) return false;\n    if ((node.attrs.colspan ?? 1) > 1 || (node.attrs.rowspan ?? 1) > 1) merged = true;\n    return node.type.spec.tableRole === \"row\";\n  });\n  return merged;\n}\n\nexport function tableSize(table: PMNode): { rows: number; cols: number } {\n  const map = TableMap.get(table);\n  return { rows: map.height, cols: map.width };\n}\n\n/** Absolute position of the cell at (row, col). */\nexport function cellPos(ref: TableRef, row: number, col: number): number {\n  const map = TableMap.get(ref.node);\n  return ref.pos + 1 + map.positionAt(row, col, ref.node);\n}\n\nfunction moveItem<T>(items: T[], from: number, to: number): T[] {\n  const next = [...items];\n  const [item] = next.splice(from, 1);\n  next.splice(to, 0, item);\n  return next;\n}\n\nfunction childrenOf(node: PMNode): PMNode[] {\n  const out: PMNode[] = [];\n  node.forEach((child) => out.push(child));\n  return out;\n}\n\nfunction replaceTable(state: EditorState, ref: TableRef, rows: PMNode[], focus: { row: number; col: number; axis: Axis }): Transaction {\n  const table = ref.node.type.create(ref.node.attrs, rows, ref.node.marks);\n  const tr = state.tr.replaceWith(ref.pos, ref.pos + ref.node.nodeSize, table);\n  const newRef = { node: table, pos: ref.pos };\n  const $cell = tr.doc.resolve(cellPos(newRef, focus.row, focus.col));\n  tr.setSelection(focus.axis === \"row\" ? CellSelection.rowSelection($cell) : CellSelection.colSelection($cell));\n  return tr;\n}\n\n/** Move a row or column to a new index. Returns null when not possible (merged cells / out of range). */\nexport function moveTableLine(state: EditorState, ref: TableRef, axis: Axis, from: number, to: number): Transaction | null {\n  const { rows, cols } = tableSize(ref.node);\n  const limit = axis === \"row\" ? rows : cols;\n  if (from === to || to < 0 || to >= limit || from < 0 || from >= limit) return null;\n  if (hasMergedCells(ref.node)) return null;\n\n  const rowNodes = childrenOf(ref.node);\n  if (axis === \"row\") {\n    return replaceTable(state, ref, moveItem(rowNodes, from, to), { row: to, col: 0, axis });\n  }\n  const moved = rowNodes.map((row) => row.type.create(row.attrs, moveItem(childrenOf(row), from, to), row.marks));\n  return replaceTable(state, ref, moved, { row: 0, col: to, axis });\n}\n\n/** Insert a copy of a row / column right after it. */\nexport function duplicateTableLine(state: EditorState, ref: TableRef, axis: Axis, index: number): Transaction | null {\n  if (hasMergedCells(ref.node)) return null;\n  const rowNodes = childrenOf(ref.node);\n  if (axis === \"row\") {\n    if (!rowNodes[index]) return null;\n    const next = [...rowNodes.slice(0, index + 1), rowNodes[index].copy(rowNodes[index].content), ...rowNodes.slice(index + 1)];\n    return replaceTable(state, ref, next, { row: index + 1, col: 0, axis });\n  }\n  const moved = rowNodes.map((row) => {\n    const cells = childrenOf(row);\n    if (!cells[index]) return row;\n    const next = [...cells.slice(0, index + 1), cells[index].copy(cells[index].content), ...cells.slice(index + 1)];\n    return row.type.create(row.attrs, next, row.marks);\n  });\n  return replaceTable(state, ref, moved, { row: 0, col: index + 1, axis });\n}\n\n/** Set an attribute (e.g. backgroundColor) on every cell in a row or column. */\nexport function setLineCellAttr(state: EditorState, ref: TableRef, axis: Axis, index: number, attr: string, value: unknown): Transaction {\n  const map = TableMap.get(ref.node);\n  const tr = state.tr;\n  const cells = axis === \"row\" ? map.cellsInRect({ left: 0, right: map.width, top: index, bottom: index + 1 }) : map.cellsInRect({ left: index, right: index + 1, top: 0, bottom: map.height });\n  cells.forEach((relative) => {\n    const pos = ref.pos + 1 + relative;\n    const cell = tr.doc.nodeAt(pos);\n    if (cell && cell.type.spec.attrs && attr in cell.attrs) tr.setNodeMarkup(pos, undefined, { ...cell.attrs, [attr]: value });\n  });\n  return tr;\n}\n\n/** Select an entire row or column so prosemirror-tables commands act on it. */\nexport function selectTableLine(state: EditorState, ref: TableRef, axis: Axis, index: number): Transaction {\n  const $cell = state.doc.resolve(axis === \"row\" ? cellPos(ref, index, 0) : cellPos(ref, 0, index));\n  return state.tr.setSelection(axis === \"row\" ? CellSelection.rowSelection($cell) : CellSelection.colSelection($cell));\n}\n"],"names":["hasMergedCells","table","merged","descendants","node","_node$attrs$colspan","_node$attrs$rowspan","attrs","colspan","rowspan","type","spec","tableRole","tableSize","map","TableMap","get","rows","height","cols","width","cellPos","ref","row","col","pos","positionAt","moveItem","items","from","to","next","_toConsumableArray","_next$splice","splice","item","_slicedToArray","childrenOf","out","forEach","child","push","replaceTable","state","focus","create","marks","tr","replaceWith","nodeSize","newRef","$cell","doc","resolve","setSelection","axis","CellSelection","rowSelection","colSelection","index","rowNodes","concat","toConsumableArray","slice","copy","content","moved","cells","$pos","Math","max","min","size","depth","before","_tableSize","limit","attr","value","cellsInRect","left","right","top","bottom","relative","cell","nodeAt","setNodeMarkup","undefined","_objectSpread","objectSpread2","_defineProperty","defineProperty"],"mappings":"wQAsBM,SAAUA,EAAeC,GAC7B,IAAIC,GAAS,EAMb,OALAD,EAAME,YAAY,SAACC,GAAQ,IAAAC,EAAAC,EACzB,OAAIJ,MACmBG,QAAnBA,EAACD,EAAKG,MAAMC,mBAAOH,EAAAA,EAAI,GAAK,YAAKC,EAACF,EAAKG,MAAME,eAAO,IAAAH,EAAAA,EAAI,GAAK,KAAGJ,GAAS,GACzC,QAA7BE,EAAKM,KAAKC,KAAKC,UACxB,GACO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