From fca037f64786bee5f0b37860d97c159d18efb027 Mon Sep 17 00:00:00 2001 From: Max Date: Wed, 16 Mar 2022 14:55:39 +0100 Subject: fix: make tab leave table on last cell. Signed-off-by: Max --- src/nodes/Table.js | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) (limited to 'src') diff --git a/src/nodes/Table.js b/src/nodes/Table.js index 5c064939e..26e0b2069 100644 --- a/src/nodes/Table.js +++ b/src/nodes/Table.js @@ -1,6 +1,7 @@ import { Table } from '@tiptap/extension-table' import { Node, mergeAttributes } from '@tiptap/core' import { TextSelection } from 'prosemirror-state' +import { isInTable } from 'prosemirror-tables' /* * Markdown tables do not include captions. @@ -73,6 +74,19 @@ export default Table.extend({ } return true }, + // move to the next node after the table from the last cell + leaveTable: () => ({ tr, dispatch, editor }) => { + if (!isInTable(tr)) return false + const { $head, empty } = tr.selection + if (!empty) return false + // the selection can temporarily be inside the table but outside of cells. + const tableDepth = $head.depth < 3 ? 1 : $head.depth - 2 + const next = tr.doc.resolve($head.after(tableDepth) + 1) + const selection = TextSelection.near(next) + const transaction = tr.setSelection(selection) + if (dispatch) dispatch(transaction.scrollIntoView()) + return true + }, } }, @@ -85,4 +99,11 @@ export default Table.extend({ state.closeBlock(node) }, + addKeyboardShortcuts() { + return { + ...this.parent(), + Tab: () => this.editor.commands.goToNextCell() || this.editor.commands.leaveTable(), + } + }, + }) -- cgit v1.2.3