Lexical: Do not display two toolbars at once

Signed-off-by: marcin mikołajczak <git@mkljczk.pl>
This commit is contained in:
marcin mikołajczak 2023-08-01 00:42:06 +02:00
parent f259f52b40
commit 43216e7d2a
5 changed files with 21 additions and 33 deletions

View File

@ -0,0 +1,11 @@
import { $createImageNode } from '../nodes/image-node';
import type { LexicalNode } from 'lexical';
import type { ImportHandler } from 'lexical-remark';
const importImage: ImportHandler<any> /* TODO */ = (node: LexicalNode, parser) => {
const lexicalNode = $createImageNode({ altText: node.alt ?? '', src: node.url });
parser.append(lexicalNode);
};
export { importImage };

View File

@ -148,15 +148,8 @@ const ImageComponent = ({
(event: KeyboardEvent) => { (event: KeyboardEvent) => {
const latestSelection = $getSelection(); const latestSelection = $getSelection();
const buttonElem = buttonRef.current; const buttonElem = buttonRef.current;
if ( if (isSelected && $isNodeSelection(latestSelection) && latestSelection.getNodes().length === 1) {
isSelected && if (buttonElem !== null && buttonElem !== document.activeElement) {
$isNodeSelection(latestSelection) &&
latestSelection.getNodes().length === 1
) {
if (
buttonElem !== null &&
buttonElem !== document.activeElement
) {
event.preventDefault(); event.preventDefault();
buttonElem.focus(); buttonElem.focus();
return true; return true;

View File

@ -120,18 +120,12 @@ const getScrollParent = (
if (style.position === 'fixed') { if (style.position === 'fixed') {
return document.body; return document.body;
} }
for ( for (let parent: HTMLElement | null = element; (parent = parent.parentElement);) {
let parent: HTMLElement | null = element;
(parent = parent.parentElement);
) {
style = getComputedStyle(parent); style = getComputedStyle(parent);
if (excludeStaticParent && style.position === 'static') { if (excludeStaticParent && style.position === 'static') {
continue; continue;
} }
if ( if (overflowRegex.test(style.overflow + style.overflowY + style.overflowX)) {
overflowRegex.test(style.overflow + style.overflowY + style.overflowX)
) {
return parent; return parent;
} }
} }
@ -418,17 +412,16 @@ const AutosuggestPlugin = ({
editor.getEditorState().read(() => { editor.getEditorState().read(() => {
const range = document.createRange(); const range = document.createRange();
const match = getQueryTextForSearch(editor); const match = getQueryTextForSearch(editor);
const nativeSelection = window.getSelection();
if (!match) { if (!match || nativeSelection?.anchorOffset !== nativeSelection?.focusOffset) {
closeTypeahead(); closeTypeahead();
return; return;
} }
dispatch(fetchComposeSuggestions(composeId, match.matchingString.trim())); dispatch(fetchComposeSuggestions(composeId, match.matchingString.trim()));
if ( if (!isSelectionOnEntityBoundary(editor, match.leadOffset)) {
!isSelectionOnEntityBoundary(editor, match.leadOffset)
) {
const isRangePositioned = tryToPositionRange(match.leadOffset, range); const isRangePositioned = tryToPositionRange(match.leadOffset, range);
if (isRangePositioned !== null) { if (isRangePositioned !== null) {
startTransition(() => startTransition(() =>

View File

@ -119,10 +119,7 @@ const BlockTypeDropdown = ({ editor, anchorElem, blockType, icon }: {
const formatParagraph = () => { const formatParagraph = () => {
editor.update(() => { editor.update(() => {
const selection = $getSelection(); const selection = $getSelection();
if ( if ($isRangeSelection(selection) || DEPRECATED_$isGridSelection(selection)) {
$isRangeSelection(selection) ||
DEPRECATED_$isGridSelection(selection)
) {
$setBlocksType(selection, () => $createParagraphNode()); $setBlocksType(selection, () => $createParagraphNode());
} }
}); });
@ -509,10 +506,7 @@ const useFloatingTextFormatToolbar = (
setIsLink(false); setIsLink(false);
} }
if ( if (!$isCodeHighlightNode(selection.anchor.getNode()) && selection.getTextContent() !== '') {
!$isCodeHighlightNode(selection.anchor.getNode()) &&
selection.getTextContent() !== ''
) {
setIsText($isTextNode(node)); setIsText($isTextNode(node));
} else { } else {
setIsText(false); setIsText(false);

View File

@ -20,10 +20,7 @@ const FocusPlugin: React.FC<IFocusPlugin> = ({ autoFocus }) => {
() => { () => {
const activeElement = document.activeElement; const activeElement = document.activeElement;
const rootElement = editor.getRootElement(); const rootElement = editor.getRootElement();
if ( if (rootElement !== null && (activeElement === null || !rootElement.contains(activeElement))) {
rootElement !== null &&
(activeElement === null || !rootElement.contains(activeElement))
) {
rootElement.focus({ preventScroll: true }); rootElement.focus({ preventScroll: true });
} }
}, { defaultSelection: 'rootEnd' }, }, { defaultSelection: 'rootEnd' },