Skip to content

Commit

Permalink
Add $insertBlockNode (facebook#2633)
Browse files Browse the repository at this point in the history
  • Loading branch information
zurfyx authored Jul 12, 2022
1 parent de71663 commit 7bbe260
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 63 deletions.
30 changes: 3 additions & 27 deletions packages/lexical-playground/src/plugins/TwitterPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,8 @@
*/

import {useLexicalComposerContext} from '@lexical/react/LexicalComposerContext';
import {
$createParagraphNode,
$getRoot,
$getSelection,
$isGridSelection,
$isNodeSelection,
$isRangeSelection,
COMMAND_PRIORITY_EDITOR,
createCommand,
LexicalCommand,
} from 'lexical';
import {$insertBlockNode} from '@lexical/utils';
import {COMMAND_PRIORITY_EDITOR, createCommand, LexicalCommand} from 'lexical';
import {useEffect} from 'react';

import {$createTweetNode, TweetNode} from '../nodes/TweetNode';
Expand All @@ -35,23 +26,8 @@ export default function TwitterPlugin(): JSX.Element | null {
return editor.registerCommand<string>(
INSERT_TWEET_COMMAND,
(payload) => {
const selection = $getSelection();
const tweetNode = $createTweetNode(payload);
if ($isRangeSelection(selection)) {
const focusNode = selection.focus.getNode();
focusNode.getTopLevelElementOrThrow().insertAfter(tweetNode);
} else if ($isNodeSelection(selection) || $isGridSelection(selection)) {
const nodes = selection.getNodes();
nodes[nodes.length - 1]
.getTopLevelElementOrThrow()
.insertAfter(tweetNode);
} else {
const root = $getRoot();
root.append(tweetNode);
}
const paragraphNode = $createParagraphNode();
tweetNode.insertAfter(paragraphNode);
paragraphNode.select();
$insertBlockNode(tweetNode);

return true;
},
Expand Down
30 changes: 3 additions & 27 deletions packages/lexical-playground/src/plugins/YouTubePlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,8 @@
*/

import {useLexicalComposerContext} from '@lexical/react/LexicalComposerContext';
import {
$createParagraphNode,
$getRoot,
$getSelection,
$isGridSelection,
$isNodeSelection,
$isRangeSelection,
COMMAND_PRIORITY_EDITOR,
createCommand,
LexicalCommand,
} from 'lexical';
import {$insertBlockNode} from '@lexical/utils';
import {COMMAND_PRIORITY_EDITOR, createCommand, LexicalCommand} from 'lexical';
import {useEffect} from 'react';

import {$createYouTubeNode, YouTubeNode} from '../nodes/YouTubeNode';
Expand All @@ -35,23 +26,8 @@ export default function YouTubePlugin(): JSX.Element | null {
return editor.registerCommand<string>(
INSERT_YOUTUBE_COMMAND,
(payload) => {
const selection = $getSelection();
const youTubeNode = $createYouTubeNode(payload);
if ($isRangeSelection(selection)) {
const focusNode = selection.focus.getNode();
focusNode.getTopLevelElementOrThrow().insertAfter(youTubeNode);
} else if ($isNodeSelection(selection) || $isGridSelection(selection)) {
const nodes = selection.getNodes();
nodes[nodes.length - 1]
.getTopLevelElementOrThrow()
.insertAfter(youTubeNode);
} else {
const root = $getRoot();
root.append(youTubeNode);
}
const paragraphNode = $createParagraphNode();
youTubeNode.insertAfter(paragraphNode);
paragraphNode.select();
$insertBlockNode(youTubeNode);

return true;
},
Expand Down
2 changes: 2 additions & 0 deletions packages/lexical-utils/flow/LexicalUtils.js.flow
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,5 @@ declare export function $restoreEditorState(
editor: LexicalEditor,
editorState: EditorState,
): void;

declare export function $insertBlockNode<T: LexicalNode>(node: T): T;
38 changes: 29 additions & 9 deletions packages/lexical-utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,23 @@
*
*/

import type {
EditorState,
ElementNode,
Klass,
LexicalEditor,
LexicalNode,
NodeKey,
} from 'lexical';

import {
$createParagraphNode,
$getRoot,
$getSelection,
$isElementNode,
$isGridSelection,
$isNodeSelection,
$isRangeSelection,
$isTextNode,
$setSelection,
createEditor,
EditorState,
ElementNode,
Klass,
LexicalEditor,
LexicalNode,
NodeKey,
} from 'lexical';
import invariant from 'shared/invariant';

Expand Down Expand Up @@ -406,3 +408,21 @@ export function $restoreEditorState(
const selection = editorState._selection;
$setSelection(selection === null ? null : selection.clone());
}

export function $insertBlockNode<T extends LexicalNode>(node: T): T {
const selection = $getSelection();
if ($isRangeSelection(selection)) {
const focusNode = selection.focus.getNode();
focusNode.getTopLevelElementOrThrow().insertAfter(node);
} else if ($isNodeSelection(selection) || $isGridSelection(selection)) {
const nodes = selection.getNodes();
nodes[nodes.length - 1].getTopLevelElementOrThrow().insertAfter(node);
} else {
const root = $getRoot();
root.append(node);
}
const paragraphNode = $createParagraphNode();
node.insertAfter(paragraphNode);
paragraphNode.select();
return node.getLatest();
}

0 comments on commit 7bbe260

Please sign in to comment.