Skip to content

Commit

Permalink
Handle case for array of nodes in buildImportMap
Browse files Browse the repository at this point in the history
  • Loading branch information
citruscai committed Oct 31, 2024
1 parent c4e909d commit db114b7
Showing 1 changed file with 15 additions and 11 deletions.
26 changes: 15 additions & 11 deletions packages/lexical-playground/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,6 @@ function $prepopulatedRichText() {

function buildImportMap(): DOMConversionMap {
const importMap: DOMConversionMap = {};
// Create a conversion map that overrides every conversion from
// TextNode with a higher priority version that does the same thing
// and then adds extra styles to the node if applicable

for (const [tag, fn] of Object.entries(TextNode.importDOM()!)) {
importMap[tag] = (node) => {
Expand All @@ -137,14 +134,8 @@ function buildImportMap(): DOMConversionMap {
conversion: (element) => {
const output = importer.conversion(element);

if (
output &&
output.node &&
!Array.isArray(output.node) &&
$isTextNode(output.node)
) {
const applyExtraStyles = (textNode: TextNode) => {
let extraStyles = '';
// read extra styles from elmeent
if (element.style.cssText.includes('font-size')) {
extraStyles += `font-size: ${element.style.fontSize};`;
}
Expand All @@ -155,7 +146,19 @@ function buildImportMap(): DOMConversionMap {
extraStyles += `color: ${element.style.color};`;
}
if (extraStyles.length > 0) {
output.node.setStyle(output.node.getStyle() + extraStyles);
textNode.setStyle(textNode.getStyle() + extraStyles);
}
};

if (output && output.node) {
if (Array.isArray(output.node)) {
output.node.forEach((textNode) => {
if ($isTextNode(textNode)) {
applyExtraStyles(textNode);
}
});
} else if ($isTextNode(output.node)) {
applyExtraStyles(output.node);
}
}
return output;
Expand All @@ -167,6 +170,7 @@ function buildImportMap(): DOMConversionMap {
};
};
}

return importMap;
}

Expand Down

0 comments on commit db114b7

Please sign in to comment.