2023-01-15 13:49:33 -08:00
|
|
|
/*
|
|
|
|
MIT License
|
|
|
|
|
|
|
|
Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
|
|
|
|
|
|
This source code is licensed under the MIT license found in the
|
|
|
|
LICENSE file in the /app/soapbox/features/compose/editor directory.
|
|
|
|
*/
|
|
|
|
|
2022-12-01 12:32:55 -08:00
|
|
|
import { $isAtNodeEnd } from '@lexical/selection';
|
|
|
|
import { ElementNode, RangeSelection, TextNode } from 'lexical';
|
|
|
|
|
|
|
|
export const getSelectedNode = (
|
|
|
|
selection: RangeSelection,
|
|
|
|
): TextNode | ElementNode => {
|
|
|
|
const anchor = selection.anchor;
|
|
|
|
const focus = selection.focus;
|
|
|
|
const anchorNode = selection.anchor.getNode();
|
|
|
|
const focusNode = selection.focus.getNode();
|
|
|
|
if (anchorNode === focusNode) {
|
|
|
|
return anchorNode;
|
|
|
|
}
|
|
|
|
const isBackward = selection.isBackward();
|
|
|
|
if (isBackward) {
|
|
|
|
return $isAtNodeEnd(focus) ? anchorNode : focusNode;
|
|
|
|
} else {
|
|
|
|
return $isAtNodeEnd(anchor) ? focusNode : anchorNode;
|
|
|
|
}
|
|
|
|
};
|