import type {
BaseSelection,
- ElementFormatType,
LexicalCommand,
LexicalNode,
TextFormatType,
);
export const DROP_COMMAND: LexicalCommand<DragEvent> =
createCommand('DROP_COMMAND');
-export const FORMAT_ELEMENT_COMMAND: LexicalCommand<ElementFormatType> =
- createCommand('FORMAT_ELEMENT_COMMAND');
export const DRAGSTART_COMMAND: LexicalCommand<DragEvent> =
createCommand('DRAGSTART_COMMAND');
export const DRAGOVER_COMMAND: LexicalCommand<DragEvent> =
*
*/
-import type {ElementFormatType} from './nodes/LexicalElementNode';
import type {
TextDetailType,
TextFormatType,
unmergeable: IS_UNMERGEABLE,
};
-export const ELEMENT_TYPE_TO_FORMAT: Record<
- Exclude<ElementFormatType, ''>,
- number
-> = {
- center: IS_ALIGN_CENTER,
- end: IS_ALIGN_END,
- justify: IS_ALIGN_JUSTIFY,
- left: IS_ALIGN_LEFT,
- right: IS_ALIGN_RIGHT,
- start: IS_ALIGN_START,
-};
-
-export const ELEMENT_FORMAT_TO_TYPE: Record<number, ElementFormatType> = {
- [IS_ALIGN_CENTER]: 'center',
- [IS_ALIGN_END]: 'end',
- [IS_ALIGN_JUSTIFY]: 'justify',
- [IS_ALIGN_LEFT]: 'left',
- [IS_ALIGN_RIGHT]: 'right',
- [IS_ALIGN_START]: 'start',
-};
-
export const TEXT_MODE_TO_TYPE: Record<TextModeType, 0 | 1 | 2> = {
normal: IS_NORMAL,
segmented: IS_SEGMENTED,
* Output for a DOM conversion.
* Node can be set to 'ignore' to ignore the conversion and handling of the DOMNode
* including all its children.
+ *
+ * You can specify a function to run for each converted child (forChild) or on all
+ * the child nodes after the conversion is complete (after).
+ * The key difference here is that forChild runs for every deeply nested child node
+ * of the current node, whereas after will run only once after the
+ * transformation of the node and all its children is complete.
*/
export type DOMConversionOutput = {
after?: (childLexicalNodes: Array<LexicalNode>) => Array<LexicalNode>;
} from './LexicalNode';
export type {
BaseSelection,
- ElementPointType as ElementPoint,
NodeSelection,
Point,
PointType,
RangeSelection,
- TextPointType as TextPoint,
} from './LexicalSelection';
export type {
- ElementFormatType,
SerializedElementNode,
} from './nodes/LexicalElementNode';
export type {SerializedRootNode} from './nodes/LexicalRootNode';
DRAGSTART_COMMAND,
DROP_COMMAND,
FOCUS_COMMAND,
- FORMAT_ELEMENT_COMMAND,
FORMAT_TEXT_COMMAND,
INDENT_CONTENT_COMMAND,
INSERT_LINE_BREAK_COMMAND,
SerializedLexicalNode
>;
-export type ElementFormatType =
- | 'left'
- | 'start'
- | 'center'
- | 'right'
- | 'end'
- | 'justify'
- | '';
-
// eslint-disable-next-line @typescript-eslint/no-unsafe-declaration-merging
export interface ElementNode {
getTopLevelElement(): ElementNode | null;
function convertTextFormatElement(domNode: HTMLElement): DOMConversionOutput {
const format = nodeNameToTextFormat[domNode.nodeName.toLowerCase()];
+
+ if (format === 'code' && domNode.closest('pre')) {
+ return {node: null};
+ }
+
if (format === undefined) {
return {node: null};
}
return;
}
- throw new Error(
- 'Internal Lexical error: invariant() is meant to be replaced at compile ' +
- 'time. There is no runtime version. Error: ' +
- message,
- );
+ for (const arg of args) {
+ message = (message || '').replace('%s', arg);
+ }
+
+ throw new Error(message);
}
DOMChildConversion,
DOMConversion,
DOMConversionFn,
- ElementFormatType,
LexicalEditor,
LexicalNode,
} from 'lexical';
}
}
}
+
$unwrapArtificalNodes(allArtificialNodes);
return lexicalNodes;
nodes: Array<LexicalNode>,
createWrapperFn: () => ElementNode,
): Array<LexicalNode> {
- const textAlign = (domNode as HTMLElement).style
- .textAlign as ElementFormatType;
const out: Array<LexicalNode> = [];
let continuousInlines: Array<LexicalNode> = [];
// wrap contiguous inline child nodes in para
node.setId(element.id);
}
- return { node };
+ return {
+ node,
+ after(childNodes): LexicalNode[] {
+ // Remove any child nodes that may get parsed since we're manually
+ // controlling the code contents.
+ return [];
+ },
+ };
},
priority: 3,
};
import type {
CommandPayloadType,
- ElementFormatType,
LexicalCommand,
LexicalEditor,
PasteCommandType,
DRAGSTART_COMMAND,
DROP_COMMAND,
ElementNode,
- FORMAT_ELEMENT_COMMAND,
FORMAT_TEXT_COMMAND,
INSERT_LINE_BREAK_COMMAND,
INSERT_PARAGRAPH_COMMAND,
},
COMMAND_PRIORITY_EDITOR,
),
- editor.registerCommand<ElementFormatType>(
- FORMAT_ELEMENT_COMMAND,
- (format) => {
- const selection = $getSelection();
- if (!$isRangeSelection(selection) && !$isNodeSelection(selection)) {
- return false;
- }
- const nodes = selection.getNodes();
- for (const node of nodes) {
- const element = $findMatchingParent(
- node,
- (parentNode): parentNode is ElementNode =>
- $isElementNode(parentNode) && !parentNode.isInline(),
- );
- }
- return true;
- },
- COMMAND_PRIORITY_EDITOR,
- ),
editor.registerCommand<boolean>(
INSERT_LINE_BREAK_COMMAND,
(selectStart) => {