Skip to content

Commit

Permalink
fix: make tab leave table on last cell.
Browse files Browse the repository at this point in the history
Signed-off-by: Max <max@nextcloud.com>
  • Loading branch information
max-nextcloud committed Mar 17, 2022
1 parent 342adb1 commit 9b9bd53
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
"prosemirror-model": "^1.16.1",
"prosemirror-schema-list": "^1.1.6",
"prosemirror-state": "^1.3.4",
"prosemirror-tables": "^1.1.1",
"prosemirror-transform": "^1.3.4",
"prosemirror-utils": "^1.0.0-0",
"prosemirror-view": "^1.23.9",
Expand Down
21 changes: 21 additions & 0 deletions src/nodes/Table.js
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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
},
}
},

Expand All @@ -85,4 +99,11 @@ export default Table.extend({
state.closeBlock(node)
},

addKeyboardShortcuts() {
return {
...this.parent(),
Tab: () => this.editor.commands.goToNextCell() || this.editor.commands.leaveTable(),
}
},

})

0 comments on commit 9b9bd53

Please sign in to comment.