Fix HR copy and paste bugs (#1323)

* Fix HR copy and paste bugs

* Format

* Move LexicalHorizontalRule to lexical/react
This commit is contained in:
Dominic Gannaway
2022-02-17 20:44:56 +00:00
committed by acywatson
parent 39c7328511
commit f7c1b342b0
10 changed files with 120 additions and 11 deletions

View File

@ -16,7 +16,7 @@ import {
TableNode,
TableRowNode,
} from '@lexical/table';
import {$createParagraphNode, $getSelection} from 'lexical';
import {$createParagraphNode, $getSelection, $isRootNode} from 'lexical';
import {useEffect} from 'react';
import invariant from 'shared/invariant';
@ -41,12 +41,22 @@ export default function TablePlugin(): React$Node {
if (selection === null) {
return true;
}
const focusNode = selection.focus.getNode();
const focus = selection.focus;
const focusNode = focus.getNode();
if (focusNode !== null) {
const topLevelNode = focusNode.getTopLevelElementOrThrow();
const tableNode = $createTableNodeWithDimensions(rows, columns);
topLevelNode.insertAfter(tableNode);
if ($isRootNode(focusNode)) {
const target = focusNode.getChildAtIndex(focus.offset);
if (target !== null) {
target.insertBefore(tableNode);
} else {
focusNode.append(tableNode);
}
} else {
const topLevelNode = focusNode.getTopLevelElementOrThrow();
topLevelNode.insertAfter(tableNode);
}
tableNode.insertAfter($createParagraphNode());
const firstCell = tableNode
.getFirstChildOrThrow<ElementNode>()