Skip to content

Commit

Permalink
ImportDOM and ExportDOM for LayoutContainerNode (#5722)
Browse files Browse the repository at this point in the history
  • Loading branch information
ivailop7 authored Mar 18, 2024
1 parent 1475590 commit a3f7509
Showing 1 changed file with 34 additions and 1 deletion.
35 changes: 34 additions & 1 deletion packages/lexical-playground/src/nodes/LayoutContainerNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

import type {
DOMConversionMap,
DOMConversionOutput,
DOMExportOutput,
EditorConfig,
LexicalNode,
NodeKey,
Expand All @@ -25,6 +27,20 @@ export type SerializedLayoutContainerNode = Spread<
SerializedElementNode
>;

function convertLayoutContainerElement(
domNode: HTMLElement,
): DOMConversionOutput | null {
const styleAttributes = window.getComputedStyle(domNode);
const templateColumns = styleAttributes.getPropertyValue(
'grid-template-columns',
);
if (templateColumns) {
const node = $createLayoutContainerNode(templateColumns);
return {node};
}
return null;
}

export class LayoutContainerNode extends ElementNode {
__templateColumns: string;

Expand All @@ -50,6 +66,13 @@ export class LayoutContainerNode extends ElementNode {
return dom;
}

exportDOM(): DOMExportOutput {
const element = document.createElement('div');
element.style.gridTemplateColumns = this.__templateColumns;
element.setAttribute('data-lexical-layout-container', 'true');
return {element};
}

updateDOM(prevNode: LayoutContainerNode, dom: HTMLElement): boolean {
if (prevNode.__templateColumns !== this.__templateColumns) {
dom.style.gridTemplateColumns = this.__templateColumns;
Expand All @@ -58,7 +81,17 @@ export class LayoutContainerNode extends ElementNode {
}

static importDOM(): DOMConversionMap | null {
return {};
return {
div: (domNode: HTMLElement) => {
if (!domNode.hasAttribute('data-lexical-layout-container')) {
return null;
}
return {
conversion: convertLayoutContainerElement,
priority: 2,
};
},
};
}

static importJSON(json: SerializedLayoutContainerNode): LayoutContainerNode {
Expand Down

0 comments on commit a3f7509

Please sign in to comment.