$textContentRequiresDoubleLinebreakAtEnd,
cloneDecorators,
getElementByKeyOrThrow,
- getTextDirection,
setMutatedNode,
} from './LexicalUtils';
type IntentionallyMarkedAsDirtyElement = boolean;
let subTreeTextContent = '';
-let subTreeDirectionedTextContent = '';
let subTreeTextFormat: number | null = null;
let subTreeTextStyle: string = '';
let editorTextContent = '';
let treatAllNodesAsDirty = false;
let activeEditorStateReadOnly = false;
let activeMutationListeners: MutationListeners;
-let activeTextDirection: 'ltr' | 'rtl' | null = null;
let activeDirtyElements: Map<NodeKey, IntentionallyMarkedAsDirtyElement>;
let activeDirtyLeaves: Set<NodeKey>;
let activePrevNodeMap: NodeMap;
if (childrenSize !== 0) {
const endIndex = childrenSize - 1;
const children = createChildrenArray(node, activeNextNodeMap);
- $createChildrenWithDirection(children, endIndex, node, dom);
+ $createChildren(children, node, 0, endIndex, dom, null);
}
const format = node.__format;
}
// Decorators are always non editable
dom.contentEditable = 'false';
- } else if ($isTextNode(node)) {
- if (!node.isDirectionless()) {
- subTreeDirectionedTextContent += text;
- }
}
subTreeTextContent += text;
editorTextContent += text;
return dom;
}
-function $createChildrenWithDirection(
- children: Array<NodeKey>,
- endIndex: number,
- element: ElementNode,
- dom: HTMLElement,
-): void {
- const previousSubTreeDirectionedTextContent = subTreeDirectionedTextContent;
- subTreeDirectionedTextContent = '';
- $createChildren(children, element, 0, endIndex, dom, null);
- reconcileBlockDirection(element, dom);
- subTreeDirectionedTextContent = previousSubTreeDirectionedTextContent;
-}
-
function $createChildren(
children: Array<NodeKey>,
element: ElementNode,
}
}
-function reconcileBlockDirection(element: ElementNode, dom: HTMLElement): void {
- const previousSubTreeDirectionTextContent: string =
- // @ts-expect-error: internal field
- dom.__lexicalDirTextContent;
- // @ts-expect-error: internal field
- const previousDirection: string = dom.__lexicalDir;
-
- if (
- previousSubTreeDirectionTextContent !== subTreeDirectionedTextContent ||
- previousDirection !== activeTextDirection
- ) {
- const hasEmptyDirectionedTextContent = subTreeDirectionedTextContent === '';
- const direction = hasEmptyDirectionedTextContent
- ? activeTextDirection
- : getTextDirection(subTreeDirectionedTextContent);
-
- if (direction !== previousDirection) {
- const classList = dom.classList;
- const theme = activeEditorConfig.theme;
- let previousDirectionTheme =
- previousDirection !== null ? theme[previousDirection] : undefined;
- let nextDirectionTheme =
- direction !== null ? theme[direction] : undefined;
-
- // Remove the old theme classes if they exist
- if (previousDirectionTheme !== undefined) {
- if (typeof previousDirectionTheme === 'string') {
- const classNamesArr = normalizeClassNames(previousDirectionTheme);
- previousDirectionTheme = theme[previousDirection] = classNamesArr;
- }
-
- // @ts-ignore: intentional
- classList.remove(...previousDirectionTheme);
- }
-
- if (
- direction === null ||
- (hasEmptyDirectionedTextContent && direction === 'ltr')
- ) {
- // Remove direction
- dom.removeAttribute('dir');
- } else {
- // Apply the new theme classes if they exist
- if (nextDirectionTheme !== undefined) {
- if (typeof nextDirectionTheme === 'string') {
- const classNamesArr = normalizeClassNames(nextDirectionTheme);
- // @ts-expect-error: intentional
- nextDirectionTheme = theme[direction] = classNamesArr;
- }
-
- if (nextDirectionTheme !== undefined) {
- classList.add(...nextDirectionTheme);
- }
- }
-
- // Update direction
- dom.dir = direction;
- }
-
- if (!activeEditorStateReadOnly) {
- const writableNode = element.getWritable();
- writableNode.__dir = direction;
- }
- }
-
- activeTextDirection = direction;
- // @ts-expect-error: internal field
- dom.__lexicalDirTextContent = subTreeDirectionedTextContent;
- // @ts-expect-error: internal field
- dom.__lexicalDir = direction;
- }
-}
-
function $reconcileChildrenWithDirection(
prevElement: ElementNode,
nextElement: ElementNode,
dom: HTMLElement,
): void {
- const previousSubTreeDirectionTextContent = subTreeDirectionedTextContent;
- subTreeDirectionedTextContent = '';
subTreeTextFormat = null;
subTreeTextStyle = '';
$reconcileChildren(prevElement, nextElement, dom);
- reconcileBlockDirection(nextElement, dom);
reconcileParagraphFormat(nextElement);
reconcileParagraphStyle(nextElement);
- subTreeDirectionedTextContent = previousSubTreeDirectionTextContent;
}
function createChildrenArray(
subTreeTextContent += previousSubTreeTextContent;
editorTextContent += previousSubTreeTextContent;
}
-
- // @ts-expect-error: internal field
- const previousSubTreeDirectionTextContent = dom.__lexicalDirTextContent;
-
- if (previousSubTreeDirectionTextContent !== undefined) {
- subTreeDirectionedTextContent += previousSubTreeDirectionTextContent;
- }
} else {
const text = prevNode.getTextContent();
- if ($isTextNode(prevNode) && !prevNode.isDirectionless()) {
- subTreeDirectionedTextContent += text;
- }
-
editorTextContent += text;
subTreeTextContent += text;
}
if (decorator !== null) {
reconcileDecorator(key, decorator);
}
- } else if ($isTextNode(nextNode) && !nextNode.isDirectionless()) {
- // Handle text content, for LTR, LTR cases.
- subTreeDirectionedTextContent += text;
}
subTreeTextContent += text;
// The cache must be rebuilt during reconciliation to account for any changes.
subTreeTextContent = '';
editorTextContent = '';
- subTreeDirectionedTextContent = '';
// Rather than pass around a load of arguments through the stack recursively
// we instead set them as bindings within the scope of the module.
treatAllNodesAsDirty = dirtyType === FULL_RECONCILE;
- activeTextDirection = null;
activeEditor = editor;
activeEditorConfig = editor._config;
activeEditorNodes = editor._nodes;