Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: registerMutationListener on replaced node #4498

Closed
bilobom opened this issue May 15, 2023 · 0 comments · Fixed by #6189
Closed

Bug: registerMutationListener on replaced node #4498

bilobom opened this issue May 15, 2023 · 0 comments · Fixed by #6189
Labels
all-platforms-bug core Reconciler, DOM, Selection, Node, Events, Composition

Comments

@bilobom
Copy link

bilobom commented May 15, 2023

I have a CustomTableNode that replaces the internal TableNode, the TableSelection isn't working on this CustomTableNode. I had to rewrite the mutation listener to listen for CustomTableNode mutation . Is there a way to support mutation listeners for node override ?

link to reproduce : https://codesandbox.io/s/infallible-mendeleev-lsn5fz?file=/src/Editor.js

here is my custom table node:

import { TableNode } from '@lexical/table'

import {
  DOMConversionMap,
  DOMExportOutput,
  EditorConfig,
  LexicalEditor,
  NodeKey,
  SerializedElementNode
} from 'lexical'

export type SerializedTableNode = SerializedElementNode & {
  type: 'custom-table'
  version: 1
}

export class CustomTableNode extends TableNode {
  static getType(): string {
    return 'custom-table'
  }
  constructor(key?: NodeKey) {
    super(key)
  }
  static clone(node: TableNode): CustomTableNode {
    return new CustomTableNode(node.__key)
  }
  createDOM(config: EditorConfig, editor?: LexicalEditor): HTMLElement {
    return super.createDOM(config, editor)
  }
  static importDOM(): DOMConversionMap | null {
    return super.importDOM()
  }
  static importJSON(_serializedNode: SerializedTableNode): CustomTableNode {
    return new CustomTableNode()
  }
  exportJSON(): SerializedTableNode {
    return {
      ...super.exportJSON(),
      type: 'custom-table',
      version: 1
    }
  }
  exportDOM(editor: LexicalEditor): DOMExportOutput {
    const { element, after } = super.exportDOM(editor)
    return {
      element,
      after(generatedElement) {
        const tableElement = after?.(generatedElement) || ''
        const container = document.createElement('div')
        container.classList.add('editor__table-container')
        container.append(tableElement)
        return container
      }
    }
  }
}

I had to rewrite the same logic that attaches TableSelection for CustomTableNode so that selection works.

registerMutationListener(CustomTableNode, (nodeMutations) => {
  // initializeTableNode and applyHandlers for selection
}

Lexical version:1.10.0

Steps To Reproduce

  1. create a custom node and replace it with node that has a mutation listener builtin .
  2. this listeners won't take effect.

The current behavior

registerMutationObserver for original node won't work for the replaced node

The expected behavior

registerMutationObserver for original node should work for the replaced node

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
all-platforms-bug core Reconciler, DOM, Selection, Node, Events, Composition
Projects
None yet
2 participants