Files
docmost/packages/editor-ext/src/lib/table/utils/convert-array-of-rows-to-table-node.ts
Mirone 7d1e5bce0d feat: table row/column drag and drop (#1467)
* chore: add dev container

* feat: add drag handle when hovering cell

* feat: add column drag and drop

* feat: add support for row drag and drop

* refactor: extract preview controllers

* fix: hover issue

* refactor: add handle controller

* chore: f

* chore: remove log

* chore: remove dev files

* feat: hide other drop indicators when table dnd working

* feat: add auto scroll and bug fix

* chore: f

* fix: firefox
2025-08-31 18:53:27 +01:00

45 lines
1.1 KiB
TypeScript

import type { Node } from '@tiptap/pm/model'
import { TableMap } from '@tiptap/pm/tables'
/**
* Convert an array of rows to a table node.
*
* @internal
*/
export function convertArrayOfRowsToTableNode(
tableNode: Node,
arrayOfNodes: (Node | null)[][],
): Node {
const rowsPM = []
const map = TableMap.get(tableNode)
for (let rowIndex = 0; rowIndex < map.height; rowIndex++) {
const row = tableNode.child(rowIndex)
const rowCells = []
for (let colIndex = 0; colIndex < map.width; colIndex++) {
if (!arrayOfNodes[rowIndex][colIndex]) continue
const cellPos = map.map[rowIndex * map.width + colIndex]
const cell = arrayOfNodes[rowIndex][colIndex]!
const oldCell = tableNode.nodeAt(cellPos)!
const newCell = oldCell.type.createChecked(
Object.assign({}, cell.attrs),
cell.content,
cell.marks,
)
rowCells.push(newCell)
}
rowsPM.push(row.type.createChecked(row.attrs, rowCells, row.marks))
}
const newTable = tableNode.type.createChecked(
tableNode.attrs,
rowsPM,
tableNode.marks,
)
return newTable
}