-
Hello, editor.registerCommand(
UPDATE_IMAGE_COMMAND,
(payload: UploadPayload) => {
const selection = $getSelection()
const node = selection?.getNodes()[0]
if (node && $isImageNode(node)) {
// Can we have something to update node properties like this?
node.setProperties({
src: payload.src,
isLoading: false,
// add new properties
srcSet: [...payload.srcSet]
})
}
return true
},
COMMAND_PRIORITY_EDITOR,
), |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 3 replies
-
Okay, I got it works, just update directly to the ImageNode object. if (node && $isImageNode(node)) {
const imageNode = node as ImageNode
imageNode.setSrc(payload.src)
} |
Beta Was this translation helpful? Give feedback.
-
This seems to be working if the node you want to change is part of the selection: const selection = $getSelection();
const node = selection?.getNodes()[0]; Wondering if there's a way to do it without the selection... |
Beta Was this translation helpful? Give feedback.
-
Managed to get it working by looking at the code behind https://playground.lexical.dev/. My usecase is that I'm rendering a Custom Node (which is a React Component) that includes a export type SerializedUserNode = Spread<
{
name: string;
description: string;
type: 'user';
version: 1;
},
SerializedDecoratorBlockNode
>;
export class UserNode extends DecoratorBlockNode {
__name: string;
__description: string;
static getType(): string {
return 'user';
}
static clone(node: UserNode): UserNode {
return new UserNode(node.__name, node.__description, node.__format, node.__key);
}
constructor(name: string, description: string, format?: ElementFormatType, key?: NodeKey) {
super(format, key);
this.__name = name;
this.__description = description;
}
createDOM(): HTMLElement {
return document.createElement('div');
}
updateDOM(): false {
return false;
}
setName(name: string): void {
const writable = this.getWritable();
writable.__name = name;
}
onNameChange = (newName: string, editor: LexicalEditor) => {
editor.update(() => {
const node = $getNodeByKey(this.getKey()) as UserNode;
if (node !== null && $isUserNode(node)) {
node.setName(newName);
}
});
}
decorate(_editor: LexicalEditor): JSX.Element {
return (
<UserComponent
name={this.__name}
description={this.__description}
onChange={(name: string) => this.onNameChange(name, _editor)} />
)
}
static importJSON(serializedNode: SerializedUserNode): UserNode {
const node = $createUserNode(serializedNode.name, serializedNode.description);
node.setFormat(serializedNode.format);
return node;
}
exportJSON(): SerializedUserNode {
return {
...super.exportJSON(),
type: 'user',
version: 1,
name: this.__name,
description: this.__description,
};
}
}
export function $createUserNode(name: string, description: string): UserNode {
return new UserNode(name, description);
}
export function $isUserNode(node?: LexicalNode): boolean {
return node instanceof UserNode;
}
|
Beta Was this translation helpful? Give feedback.
Okay, I got it works, just update directly to the ImageNode object.