Node plugins get re-initialized and forget their state when editing another piece of text? #6792
Replies: 1 comment
-
Good old ChatGPT educated me about the need to serialize the label: import { EditorConfig, NodeKey, SerializedTextNode, Spread, TextNode } from 'lexical'
import { spotlight } from '@mantine/spotlight'
import { setLastBtnFileId } from '@/state/selectedChatBtn'
import styles from './FileAttachmentNode.module.css'
export type SerializedFileAttachmentNode = Spread<
{
unifiedID: string
label: string
},
SerializedTextNode
>
export class FileAttachmentNode extends TextNode {
__unifiedID: string
__label: string
static getType(): string {
return 'fileattachment'
}
static clone(node: FileAttachmentNode): FileAttachmentNode {
return new FileAttachmentNode(node.__unifiedID, node.__label, node.__key)
}
constructor(unifiedID: string, label = 'Select a file', key?: NodeKey) {
super(label, key)
this.__unifiedID = unifiedID.toLowerCase()
this.__label = label
}
createDOM(_config: EditorConfig): HTMLElement {
const btn = document.createElement('button')
btn.type = 'button'
btn.className = styles.fileBtn
btn.innerText = this.__label
btn.setAttribute('data-node-key', this.getKey())
btn.onclick = () => {
setLastBtnFileId(this.getKey())
spotlight.open()
}
return btn
}
updateDOM(prevNode: FileAttachmentNode, dom: HTMLElement): boolean {
if (prevNode.__label !== this.__label) {
dom.innerText = this.__label
return true
}
return false
}
setButtonLabel(newLabel: string) {
this.getWritable().__label = newLabel
this.setTextContent(newLabel)
}
static importJSON(serializedNode: SerializedFileAttachmentNode): FileAttachmentNode {
return $createFileAttachmentNode(serializedNode.unifiedID)
}
exportJSON(): SerializedFileAttachmentNode {
return {
...super.exportJSON(),
type: 'fileattachment',
unifiedID: this.__unifiedID,
label: this.__label,
}
}
}
export function $createFileAttachmentNode(
unifiedID: string,
label: string = 'Select a file'
): FileAttachmentNode {
const node = new FileAttachmentNode(unifiedID, label).setMode('token')
return node
} |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I have an issue where if I have multiple TextNode plugins inserted and I have changed the text of these with
this.setTextContent
, if I try and insert a new one it re-initializes one or more of them and resets the text content?Here's a demo:
Screen.Recording.2024-11-05.at.5.34.03.PM.mov
As you can see when I insert a new one, the old is re-initialized and loses it's state.
FileAttachmentNode.ts
FileAttachmentPlugin.ts
Beta Was this translation helpful? Give feedback.
All reactions