$getSelection, $isElementNode,
$isTextNode, $setSelection,
BaseSelection, ElementFormatType, ElementNode,
- LexicalEditor, LexicalNode, TextFormatType
+ LexicalNode, TextFormatType
} from "lexical";
import {LexicalElementNodeCreator, LexicalNodeMatcher} from "./nodes";
import {$findMatchingParent, $getNearestBlockElementAncestorOrThrow} from "@lexical/utils";
return el;
}
-export function selectionContainsNodeType(selection: BaseSelection|null, matcher: LexicalNodeMatcher): boolean {
- return getNodeFromSelection(selection, matcher) !== null;
+export function $selectionContainsNodeType(selection: BaseSelection|null, matcher: LexicalNodeMatcher): boolean {
+ return $getNodeFromSelection(selection, matcher) !== null;
}
-export function getNodeFromSelection(selection: BaseSelection|null, matcher: LexicalNodeMatcher): LexicalNode|null {
+export function $getNodeFromSelection(selection: BaseSelection|null, matcher: LexicalNodeMatcher): LexicalNode|null {
if (!selection) {
return null;
}
return null;
}
-export function selectionContainsTextFormat(selection: BaseSelection|null, format: TextFormatType): boolean {
+export function $selectionContainsTextFormat(selection: BaseSelection|null, format: TextFormatType): boolean {
if (!selection) {
return false;
}
return false;
}
-export function toggleSelectionBlockNodeType(editor: LexicalEditor, matcher: LexicalNodeMatcher, creator: LexicalElementNodeCreator) {
- editor.update(() => {
- const selection = $getSelection();
- const blockElement = selection ? $getNearestBlockElementAncestorOrThrow(selection.getNodes()[0]) : null;
- if (selection && matcher(blockElement)) {
- $setBlocksType(selection, $createParagraphNode);
- } else {
- $setBlocksType(selection, creator);
- }
- });
+export function $toggleSelectionBlockNodeType(matcher: LexicalNodeMatcher, creator: LexicalElementNodeCreator) {
+ const selection = $getSelection();
+ const blockElement = selection ? $getNearestBlockElementAncestorOrThrow(selection.getNodes()[0]) : null;
+ if (selection && matcher(blockElement)) {
+ $setBlocksType(selection, $createParagraphNode);
+ } else {
+ $setBlocksType(selection, creator);
+ }
}
-export function insertNewBlockNodeAtSelection(node: LexicalNode, insertAfter: boolean = true) {
+export function $insertNewBlockNodeAtSelection(node: LexicalNode, insertAfter: boolean = true) {
const selection = $getSelection();
const blockElement = selection ? $getNearestBlockElementAncestorOrThrow(selection.getNodes()[0]) : null;
}
}
-export function selectSingleNode(node: LexicalNode) {
+export function $selectSingleNode(node: LexicalNode) {
const nodeSelection = $createNodeSelection();
nodeSelection.add(node.getKey());
$setSelection(nodeSelection);
}
-export function selectionContainsNode(selection: BaseSelection|null, node: LexicalNode): boolean {
+export function $selectionContainsNode(selection: BaseSelection|null, node: LexicalNode): boolean {
if (!selection) {
return false;
}
return false;
}
-export function selectionContainsElementFormat(selection: BaseSelection|null, format: ElementFormatType): boolean {
- const nodes = getBlockElementNodesInSelection(selection);
+export function $selectionContainsElementFormat(selection: BaseSelection|null, format: ElementFormatType): boolean {
+ const nodes = $getBlockElementNodesInSelection(selection);
for (const node of nodes) {
if (node.getFormatType() === format) {
return true;
return false;
}
-export function getBlockElementNodesInSelection(selection: BaseSelection|null): ElementNode[] {
+export function $getBlockElementNodesInSelection(selection: BaseSelection|null): ElementNode[] {
if (!selection) {
return [];
}
UNDO_COMMAND
} from "lexical";
import {
- getBlockElementNodesInSelection,
- getNodeFromSelection, insertNewBlockNodeAtSelection, selectionContainsElementFormat,
- selectionContainsNodeType,
- selectionContainsTextFormat,
- toggleSelectionBlockNodeType
+ $getBlockElementNodesInSelection,
+ $getNodeFromSelection, $insertNewBlockNodeAtSelection, $selectionContainsElementFormat,
+ $selectionContainsNodeType,
+ $selectionContainsTextFormat,
+ $toggleSelectionBlockNodeType
} from "../../helpers";
import {$createCalloutNode, $isCalloutNodeOfCategory, CalloutCategory} from "../../nodes/callout";
import {
return {
label: `${name} Callout`,
action(context: EditorUiContext) {
- toggleSelectionBlockNodeType(
- context.editor,
- (node) => $isCalloutNodeOfCategory(node, category),
- () => $createCalloutNode(category),
- )
+ context.editor.update(() => {
+ $toggleSelectionBlockNodeType(
+ (node) => $isCalloutNodeOfCategory(node, category),
+ () => $createCalloutNode(category),
+ )
+ });
},
isActive(selection: BaseSelection|null): boolean {
- return selectionContainsNodeType(selection, (node) => $isCalloutNodeOfCategory(node, category));
+ return $selectionContainsNodeType(selection, (node) => $isCalloutNodeOfCategory(node, category));
}
};
}
return {
label: name,
action(context: EditorUiContext) {
- toggleSelectionBlockNodeType(
- context.editor,
+ context.editor.update(() => {
+ $toggleSelectionBlockNodeType(
(node) => isHeaderNodeOfTag(node, tag),
() => $createHeadingNode(tag),
- )
+ )
+ });
},
isActive(selection: BaseSelection|null): boolean {
- return selectionContainsNodeType(selection, (node) => isHeaderNodeOfTag(node, tag));
+ return $selectionContainsNodeType(selection, (node) => isHeaderNodeOfTag(node, tag));
}
};
}
export const blockquote: EditorButtonDefinition = {
label: 'Blockquote',
action(context: EditorUiContext) {
- toggleSelectionBlockNodeType(context.editor, $isQuoteNode, $createQuoteNode);
+ context.editor.update(() => {
+ $toggleSelectionBlockNodeType($isQuoteNode, $createQuoteNode);
+ });
},
isActive(selection: BaseSelection|null): boolean {
- return selectionContainsNodeType(selection, $isQuoteNode);
+ return $selectionContainsNodeType(selection, $isQuoteNode);
}
};
export const paragraph: EditorButtonDefinition = {
label: 'Paragraph',
action(context: EditorUiContext) {
- toggleSelectionBlockNodeType(context.editor, $isParagraphNode, $createParagraphNode);
+ context.editor.update(() => {
+ $toggleSelectionBlockNodeType($isParagraphNode, $createParagraphNode);
+ });
},
isActive(selection: BaseSelection|null): boolean {
- return selectionContainsNodeType(selection, $isParagraphNode);
+ return $selectionContainsNodeType(selection, $isParagraphNode);
}
}
context.editor.dispatchCommand(FORMAT_TEXT_COMMAND, format);
},
isActive(selection: BaseSelection|null): boolean {
- return selectionContainsTextFormat(selection, format);
+ return $selectionContainsTextFormat(selection, format);
}
};
}
function setAlignmentForSection(alignment: ElementFormatType): void {
const selection = $getSelection();
- const elements = getBlockElementNodesInSelection(selection);
+ const elements = $getBlockElementNodesInSelection(selection);
for (const node of elements) {
node.setFormat(alignment);
}
context.editor.update(() => setAlignmentForSection('left'));
},
isActive(selection: BaseSelection|null) {
- return selectionContainsElementFormat(selection, 'left');
+ return $selectionContainsElementFormat(selection, 'left');
}
};
context.editor.update(() => setAlignmentForSection('center'));
},
isActive(selection: BaseSelection|null) {
- return selectionContainsElementFormat(selection, 'center');
+ return $selectionContainsElementFormat(selection, 'center');
}
};
context.editor.update(() => setAlignmentForSection('right'));
},
isActive(selection: BaseSelection|null) {
- return selectionContainsElementFormat(selection, 'right');
+ return $selectionContainsElementFormat(selection, 'right');
}
};
context.editor.update(() => setAlignmentForSection('justify'));
},
isActive(selection: BaseSelection|null) {
- return selectionContainsElementFormat(selection, 'justify');
+ return $selectionContainsElementFormat(selection, 'justify');
}
};
});
},
isActive(selection: BaseSelection|null): boolean {
- return selectionContainsNodeType(selection, (node: LexicalNode | null | undefined): boolean => {
+ return $selectionContainsNodeType(selection, (node: LexicalNode | null | undefined): boolean => {
return $isListNode(node) && (node as ListNode).getListType() === type;
});
}
const linkModal = context.manager.createModal('link');
context.editor.getEditorState().read(() => {
const selection = $getSelection();
- const selectedLink = getNodeFromSelection(selection, $isLinkNode) as LinkNode|null;
+ const selectedLink = $getNodeFromSelection(selection, $isLinkNode) as LinkNode|null;
let formDefaults = {};
if (selectedLink) {
});
},
isActive(selection: BaseSelection|null): boolean {
- return selectionContainsNodeType(selection, $isLinkNode);
+ return $selectionContainsNodeType(selection, $isLinkNode);
}
};
action(context: EditorUiContext) {
context.editor.update(() => {
const selection = context.lastSelection;
- const selectedLink = getNodeFromSelection(selection, $isLinkNode) as LinkNode|null;
+ const selectedLink = $getNodeFromSelection(selection, $isLinkNode) as LinkNode|null;
const selectionPoints = selection?.getStartEndPoints();
if (selectedLink) {
action(context: EditorUiContext) {
const imageModal = context.manager.createModal('image');
const selection = context.lastSelection;
- const selectedImage = getNodeFromSelection(selection, $isImageNode) as ImageNode|null;
+ const selectedImage = $getNodeFromSelection(selection, $isImageNode) as ImageNode|null;
context.editor.getEditorState().read(() => {
let formDefaults = {};
});
},
isActive(selection: BaseSelection|null): boolean {
- return selectionContainsNodeType(selection, $isImageNode);
+ return $selectionContainsNodeType(selection, $isImageNode);
}
};
icon: horizontalRuleIcon,
action(context: EditorUiContext) {
context.editor.update(() => {
- insertNewBlockNodeAtSelection($createHorizontalRuleNode(), false);
+ $insertNewBlockNodeAtSelection($createHorizontalRuleNode(), false);
});
},
isActive(selection: BaseSelection|null): boolean {
- return selectionContainsNodeType(selection, $isHorizontalRuleNode);
+ return $selectionContainsNodeType(selection, $isHorizontalRuleNode);
}
};
action(context: EditorUiContext) {
context.editor.getEditorState().read(() => {
const selection = $getSelection();
- const codeBlock = getNodeFromSelection(context.lastSelection, $isCodeBlockNode) as (CodeBlockNode|null);
+ const codeBlock = $getNodeFromSelection(context.lastSelection, $isCodeBlockNode) as (CodeBlockNode|null);
if (codeBlock === null) {
context.editor.update(() => {
const codeBlock = $createCodeBlockNode();
codeBlock.setCode(selection?.getTextContent() || '');
- insertNewBlockNodeAtSelection(codeBlock, true);
+ $insertNewBlockNodeAtSelection(codeBlock, true);
$openCodeEditorForNode(context.editor, codeBlock);
codeBlock.selectStart();
});
});
},
isActive(selection: BaseSelection|null): boolean {
- return selectionContainsNodeType(selection, $isCodeBlockNode);
+ return $selectionContainsNodeType(selection, $isCodeBlockNode);
}
};
});
},
isActive(selection: BaseSelection|null): boolean {
- return selectionContainsNodeType(selection, $isDetailsNode);
+ return $selectionContainsNodeType(selection, $isDetailsNode);
}
}