@lobehub/editor
Version:
A powerful and extensible rich text editor built on Meta's Lexical framework, providing a modern editing experience with React integration.
36 lines • 1.34 kB
JavaScript
import { $isAtNodeEnd, $setBlocksType } from '@lexical/selection';
import { $findMatchingParent } from '@lexical/utils';
import { $createParagraphNode, $getSelection, $isRootOrShadowRoot } from 'lexical';
export var $findTopLevelElement = function $findTopLevelElement(node) {
var topLevelElement = node.getKey() === 'root' ? node : $findMatchingParent(node, function (e) {
var parent = e.getParent();
return parent !== null && $isRootOrShadowRoot(parent);
});
if (topLevelElement === null) {
topLevelElement = node.getTopLevelElementOrThrow();
}
return topLevelElement;
};
export var formatParagraph = function formatParagraph(editor) {
editor === null || editor === void 0 || editor.update(function () {
var selection = $getSelection();
$setBlocksType(selection, function () {
return $createParagraphNode();
});
});
};
export var getSelectedNode = function getSelectedNode(selection) {
var anchor = selection.anchor;
var focus = selection.focus;
var anchorNode = selection.anchor.getNode();
var focusNode = selection.focus.getNode();
if (anchorNode === focusNode) {
return anchorNode;
}
var isBackward = selection.isBackward();
if (isBackward) {
return $isAtNodeEnd(focus) ? anchorNode : focusNode;
} else {
return $isAtNodeEnd(anchor) ? anchorNode : focusNode;
}
};