Last active
February 3, 2020 12:19
-
-
Save Dev1an/48d826a690a224eb881e77afa2544ac8 to your computer and use it in GitHub Desktop.
Prosemirror-Table splitting a cell
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function splitCellVertically(state, dispatch) { | |
| if (!isInTable(state)) { return false } | |
| const rect = selectedRect(state); | |
| const selectionWidth = rect.right - rect.left | |
| const selectionHeight = rect.bottom - rect.top | |
| if (selectionWidth > 1 || selectionHeight > 1) {return false} | |
| if (dispatch) { | |
| const tr = state.tr | |
| const ref = rect | |
| const row = rect.bottom | |
| var map = ref.map; | |
| var tableStart = ref.tableStart; | |
| var table = ref.table; | |
| const $selectedCell = selectionCell(state) | |
| const selectedCellNode = state.doc.nodeAt($selectedCell.pos) | |
| const fragmentAfterCursor = selectedCellNode.cut( | |
| state.selection.head - $selectedCell.pos - 1 | |
| ).content | |
| var rowPos = tableStart; | |
| for (var i = 0; i < row; i++) { rowPos += table.child(i).nodeSize; } | |
| var newCell, refRow = row > 0 ? -1 : 0; | |
| if (rowIsHeader(map, table, row + refRow)) | |
| { refRow = row == 0 || row == map.height ? null : 0; } | |
| for (var col = 0, index = map.width * row; col < map.width; col++, index++) { | |
| // Is current column | |
| if (col == rect.left) { | |
| // create new cell | |
| var type = refRow == null ? tableNodeTypes(table.type.schema).cell | |
| : table.nodeAt(map.map[index + refRow * map.width]).type; | |
| newCell = type.createAndFill(null, fragmentAfterCursor); | |
| } else { | |
| // increase rowSpan | |
| var pos = map.map[index - map.width], attrs = table.nodeAt(pos).attrs; | |
| tr.setNodeMarkup(tableStart + pos, null, setAttr(attrs, "rowspan", attrs.rowspan + 1)); | |
| col += attrs.colspan - 1; | |
| } | |
| } | |
| tr.insert(rowPos, tableNodeTypes(table.type.schema).row.create(null, newCell)); | |
| tr.delete(state.selection.head, $selectedCell.pos + selectedCellNode.nodeSize) | |
| dispatch(tr); | |
| } | |
| return true | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment