From 5b33aa6649aafe959431ba0fbe0dbb78a3d15794 Mon Sep 17 00:00:00 2001 From: Pablo Yus <10373244+PabloYG@users.noreply.github.com> Date: Thu, 29 Sep 2022 04:03:28 +0100 Subject: [PATCH] Enter in table cell adds a row after the current one (#4186) Co-authored-by: Tom Moor --- .gitignore | 2 +- shared/editor/nodes/Table.ts | 22 ++++++++++++++++++---- shared/editor/queries/getRowIndex.ts | 10 ++++++++++ 3 files changed, 29 insertions(+), 5 deletions(-) diff --git a/.gitignore b/.gitignore index 5e12a63a2..bd0a9fcda 100644 --- a/.gitignore +++ b/.gitignore @@ -11,4 +11,4 @@ fakes3/* .idea *.pem *.key -*.cert +*.cert \ No newline at end of file diff --git a/shared/editor/nodes/Table.ts b/shared/editor/nodes/Table.ts index 9a4e2c664..b6635f3e5 100644 --- a/shared/editor/nodes/Table.ts +++ b/shared/editor/nodes/Table.ts @@ -12,15 +12,18 @@ import { toggleHeaderCell, toggleHeaderColumn, toggleHeaderRow, + CellSelection, } from "prosemirror-tables"; import { addRowAt, createTable, getCellsInColumn, moveRow, + setTextSelection, } from "prosemirror-utils"; import { Decoration, DecorationSet } from "prosemirror-view"; import { MarkdownSerializerState } from "../lib/markdown/serializer"; +import { getRowIndexFromText } from "../queries/getRowIndex"; import tablesRule from "../rules/tables"; import { Dispatch } from "../types"; import Node from "./Node"; @@ -123,12 +126,23 @@ export default class Table extends Node { if (!isInTable(state)) { return false; } + const index = getRowIndexFromText( + (state.selection as unknown) as CellSelection + ); - // TODO: Adding row at the end for now, can we find the current cell - // row index and add the row below that? - const cells = getCellsInColumn(0)(state.selection) || []; + if (index === 0) { + const cells = getCellsInColumn(0)(state.selection); + if (!cells) { + return false; + } - dispatch(addRowAt(cells.length, true)(state.tr)); + const tr = addRowAt(index + 2, true)(state.tr); + dispatch( + setTextSelection(cells[1].pos)(moveRow(index + 2, index + 1)(tr)) + ); + } else { + dispatch(addRowAt(index + 1, true)(state.tr)); + } return true; }, }; diff --git a/shared/editor/queries/getRowIndex.ts b/shared/editor/queries/getRowIndex.ts index 39cbb8f3f..e11aa2001 100644 --- a/shared/editor/queries/getRowIndex.ts +++ b/shared/editor/queries/getRowIndex.ts @@ -9,3 +9,13 @@ export default function getRowIndex(selection: CellSelection) { const path = (selection.$from as any).path; return path[path.length - 8]; } + +export function getRowIndexFromText(selection: CellSelection) { + const isRowSelection = selection.isRowSelection && selection.isRowSelection(); + const path = (selection.$from as any).path; + if (isRowSelection) { + return path[path.length - 8]; + } else { + return path[path.length - 11]; + } +}