Skip to content

Commit

Permalink
WIP getDOMSlot
Browse files Browse the repository at this point in the history
  • Loading branch information
etrepum committed Oct 24, 2024
1 parent 409c65e commit 6ce96d0
Show file tree
Hide file tree
Showing 7 changed files with 268 additions and 112 deletions.
19 changes: 10 additions & 9 deletions packages/lexical/src/LexicalMutations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
*
*/

import type {TextNode} from '.';
import type {LexicalNode, TextNode} from '.';
import type {LexicalEditor} from './LexicalEditor';
import type {LexicalPrivateDOM} from './LexicalNode';
import type {BaseSelection} from './LexicalSelection';

import {IS_FIREFOX} from 'shared/environment';
Expand All @@ -27,6 +28,7 @@ import {
$getNodeFromDOMNode,
$updateTextNodeFromDOMContent,
getDOMSelection,
getNodeKeyFromDOMNode,
getWindow,
internalGetRoot,
isFirefoxClipboardEvents,
Expand All @@ -53,14 +55,12 @@ function initTextEntryListener(editor: LexicalEditor): void {

function isManagedLineBreak(
dom: Node,
target: Node,
target: Node & LexicalPrivateDOM,
editor: LexicalEditor,
): boolean {
return (
// @ts-expect-error: internal field
target.__lexicalLineBreak === dom ||
// @ts-ignore We intentionally add this to the Node.
dom[`__lexicalKey_${editor._key}`] !== undefined
getNodeKeyFromDOMNode(dom, editor) !== undefined
);
}

Expand Down Expand Up @@ -120,7 +120,7 @@ export function $flushMutations(
try {
updateEditor(editor, () => {
const selection = $getSelection() || getLastSelection(editor);
const badDOMTargets = new Map();
const badDOMTargets = new Map<Node, LexicalNode | null>();
const rootElement = editor.getRootElement();
// We use the current editor state, as that reflects what is
// actually "on screen".
Expand Down Expand Up @@ -230,7 +230,8 @@ export function $flushMutations(
for (const [targetDOM, targetNode] of badDOMTargets) {
if ($isElementNode(targetNode)) {
const childKeys = targetNode.getChildrenKeys();
let currentDOM = targetDOM.firstChild;
const slot = targetNode.getDOMSlot(targetDOM as HTMLElement);
let currentDOM = slot.getFirstChild();

for (let s = 0; s < childKeys.length; s++) {
const key = childKeys[s];
Expand All @@ -241,10 +242,10 @@ export function $flushMutations(
}

if (currentDOM == null) {
targetDOM.appendChild(correctDOM);
slot.insertChild(correctDOM);
currentDOM = correctDOM;
} else if (currentDOM !== correctDOM) {
targetDOM.replaceChild(correctDOM, currentDOM);
slot.replaceChild(correctDOM, currentDOM);
}

currentDOM = currentDOM.nextSibling;
Expand Down
8 changes: 8 additions & 0 deletions packages/lexical/src/LexicalNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@ export type SerializedLexicalNode = {
version: number;
};

/** @internal */
export interface LexicalPrivateDOM {
__lexicalTextContent?: string | undefined | null;
__lexicalLineBreak?: HTMLBRElement | undefined | null;
__lexicalDirTextContent?: string | undefined | null;
__lexicalDir?: 'ltr' | 'rtl' | null | undefined;
}

export function $removeNode(
nodeToRemove: LexicalNode,
restoreSelection: boolean,
Expand Down
Loading

0 comments on commit 6ce96d0

Please sign in to comment.