Comments (7)
You're missing the importDOM function on your custom node. This is what tells lexical how to interpret that node from HTML.
from lexical.
Thank you for the reply. I tried it, but I can't seem to make it work correctly. I don't see any option to add classs to the node, just styles. Here is the code.
static importDOM(): DOMConversionMap | null {
const importers = TextNode.importDOM();
return {
...importers,
span: (node: HTMLElement) => {
if (isMainTextColourNode(node as HTMLElement)) {
return {
conversion: styleConversion(importers?.span),
priority: 1,
};
}
return null;
},
};
}
function styleConversion(
originalDOMConverter?: (node: HTMLElement) => DOMConversion | null
): (node: HTMLElement) => DOMConversionOutput | null {
return (node) => {
const original = originalDOMConverter?.(node);
if (!original) {
return null;
}
const originalOutput = original.conversion(node);
if (!originalOutput) {
return originalOutput;
}
const backgroundColor = "red";
const color = node.style.color;
const fontFamily = node.style.fontFamily;
const fontWeight = node.style.fontWeight;
const fontSize = node.style.fontSize;
const textDecoration = node.style.textDecoration;
const className = node.className;
return {
...originalOutput,
forChild: (lexicalNode, parent) => {
const originalForChild = originalOutput?.forChild ?? ((x) => x);
const result = originalForChild(lexicalNode, parent);
if ($isTextNode(result)) {
const style = [
backgroundColor ? `background-color: ${backgroundColor}` : null,
color ? `color: ${color}` : null,
fontFamily ? `font-family: ${fontFamily}` : null,
fontWeight ? `font-weight: ${fontWeight}` : null,
fontSize ? `font-size: ${fontSize}` : null,
textDecoration ? `text-decoration: ${textDecoration}` : null,
]
.filter((value) => value != null)
.join("; ");
//Need to set className
if (style.length) {
return result.setStyle(style);
}
}
return result;
},
};
};
}
function isMainTextColourNode(node: HTMLSpanElement): node is HTMLSpanElement {
return node.classList.contains("editor-mainTextColour");
}
from lexical.
How about
node.classList.add('my-class')
?
from lexical.
But I need to add the class to the result, right? I want it to have the class in the editor.
So I want something like
result.classList.add('my-class')
//or
result.setClassName('my-class')
but that does not exist.
from lexical.
But I need to add the class to the result, right? I want it to have the class in the editor. So I want something like
result.classList.add('my-class') //or result.setClassName('my-class')
but that does not exist.
Oh, sorry - yes, of course - I misunderstood.
In that case you can just add it in the node's createDOM method, right? I mean, you have a custom node so if you always want to add that class, you can just do it in createDOM, otherwise if there's some condition you can add a property on the custom node class, set that in importDOM, and read it in createDOM, right?
from lexical.
But I am already doing that in createDOM.
createDOM(_config: EditorConfig, _editor: LexicalEditor): HTMLElement {
const element = super.createDOM(_config);
element.className = _config.theme.mainTextColour;
return element;
}
from lexical.
So I think I finally figured it out by doing this:
static importDOM(): DOMConversionMap | null {
const importers = TextNode.importDOM();
return {
...importers,
span: (node: HTMLElement) => {
if (isMainTextColourNode(node as HTMLElement)) {
return {
conversion: (domNode: HTMLElement) => {
return {
node: $createMainTextColourNode(domNode.textContent || ""),
};
},
priority: 1,
};
}
return null;
},
};
}
from lexical.
Related Issues (20)
- Bug: ComponentPickerPlugin scrolling is buggy - dropdown disappears onScroll HOT 1
- Bug: CMD+Delete in a shadow root fails to delete the last paragraph
- Bug: Failed to upgrade Laxical from `0.13.1` to `0.14.x` (React) HOT 1
- Feature: Extend error boundary to all editor listeners
- Bug: Updating to version 0.14 esm error HOT 5
- Is this an AI generated library? HOT 3
- Bug: OnChangePlugin does not fire onChange while typing on Sticky note
- Bug: Numbered list does not increment in ImageNode caption
- Bug: caret jumping to the beginning after Node Transforms HOT 2
- Bug: Double input of text including composing, such as Japanese, at TextNode Boundaries with in a MarkNode HOT 1
- Feature: Use markdown plugin without bundling Prismjs HOT 1
- Bug: Delete all hangs for a long time on big documents. HOT 5
- Bug: AutoLink doesn't replace URLs containing multiple periods HOT 1
- Feature: Bundle on React pack with svgs HOT 1
- Bug: unable to save styles after importing html to editor HOT 1
- Bug: the minified version of ESM build of @lexical/text doesn't work with vite SSR HOT 4
- Bug: peerDependencies from monorepo packages cause npm resolution errors HOT 1
- Bug: Text is not inserted correctly when using Google Input Tools HOT 1
- Bug: block cursor shown instead of normal one HOT 1
- Feature: Can we create forms using Lexical Plugins? HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from lexical.