|
| 1 | +import type {Node, ResolvedPos} from '#pm/model'; |
| 2 | +import {type Command, type EditorState, TextSelection, type Transaction} from '#pm/state'; |
| 3 | +import type {EditorView} from '#pm/view'; |
| 4 | +import {pType} from 'src/extensions/base/specs'; |
| 5 | +import {findParentTableFromPos} from 'src/table-utils'; |
| 6 | +import {getChildByIndex} from 'src/utils/node-children'; |
| 7 | + |
| 8 | +import { |
| 9 | + findParentBodyOnPos, |
| 10 | + findParentCellOnPos, |
| 11 | + findParentHeadOnPos, |
| 12 | + findParentRowOnPos, |
| 13 | +} from './helpers'; |
| 14 | + |
| 15 | +const moveToNextRow = ( |
| 16 | + $pos: ResolvedPos, |
| 17 | + state: EditorState, |
| 18 | + dispatch: EditorView['dispatch'] | undefined, |
| 19 | +) => { |
| 20 | + const tableRes = findParentTableFromPos($pos); |
| 21 | + if (!tableRes) return false; |
| 22 | + |
| 23 | + const tableHeadResult = findParentHeadOnPos($pos); |
| 24 | + const tableBodyResult = findParentBodyOnPos($pos); |
| 25 | + if (!tableHeadResult && !tableBodyResult) return false; |
| 26 | + |
| 27 | + const tableRowResult = findParentRowOnPos($pos); |
| 28 | + const tableCellResult = findParentCellOnPos($pos); |
| 29 | + if (!tableRowResult || !tableCellResult) return false; |
| 30 | + |
| 31 | + const cellIndex = $pos.index(tableCellResult.depth - 1); |
| 32 | + const rowIndex = $pos.index(tableRowResult.depth - 1); |
| 33 | + |
| 34 | + if (!dispatch) return true; |
| 35 | + |
| 36 | + const goToNextRow = (tr: Transaction, nextRow: Node, nextRowPos: number) => { |
| 37 | + const cell = getChildByIndex(nextRow, cellIndex); |
| 38 | + if (cell) { |
| 39 | + const cellPos = nextRowPos + 1 + cell.offset; |
| 40 | + tr.setSelection(TextSelection.create(tr.doc, cellPos + cell.node.nodeSize - 1)); |
| 41 | + } else { |
| 42 | + // fallback if cell not found |
| 43 | + const $rowEnd = tr.doc.resolve(nextRowPos + nextRow.nodeSize - 1); |
| 44 | + tr.setSelection(TextSelection.near($rowEnd, -1)); |
| 45 | + } |
| 46 | + }; |
| 47 | + |
| 48 | + const exitTable = (tr: Transaction) => { |
| 49 | + const afterTablePos = tableRes.pos + tableRes.node.nodeSize; |
| 50 | + tr.insert(afterTablePos, pType(state.schema).create()); |
| 51 | + tr.setSelection(TextSelection.create(tr.doc, afterTablePos + 1)); |
| 52 | + }; |
| 53 | + |
| 54 | + const tr = state.tr; |
| 55 | + |
| 56 | + if (tableHeadResult) { |
| 57 | + // in table head |
| 58 | + const tableBody = getChildByIndex(tableRes.node, tableRes.node.childCount - 1); |
| 59 | + if (tableBody && tableBody.node !== tableHeadResult.node && tableBody.node.firstChild) { |
| 60 | + goToNextRow(tr, tableBody.node.firstChild, tableRes.start + tableBody.offset + 1); |
| 61 | + } else { |
| 62 | + // no table body or it is empty |
| 63 | + exitTable(tr); |
| 64 | + } |
| 65 | + } else { |
| 66 | + // in table body |
| 67 | + const nextRow = getChildByIndex(tableBodyResult!.node, rowIndex + 1); |
| 68 | + if (nextRow) { |
| 69 | + goToNextRow(tr, nextRow.node, tableBodyResult!.start + nextRow.offset); |
| 70 | + } else { |
| 71 | + // pos in last row |
| 72 | + exitTable(tr); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + dispatch(tr.scrollIntoView()); |
| 77 | + |
| 78 | + return true; |
| 79 | +}; |
| 80 | + |
| 81 | +export const moveToNextRowCommand: Command = (state, dispatch) => { |
| 82 | + return ( |
| 83 | + moveToNextRow(state.selection.$head, state, dispatch) || |
| 84 | + moveToNextRow(state.selection.$anchor, state, dispatch) |
| 85 | + ); |
| 86 | +}; |
0 commit comments