lexical icon indicating copy to clipboard operation
lexical copied to clipboard

Bug: Pasting into an inline custom node seems to fail

Open s0rta opened this issue 1 year ago • 3 comments

Currently, the check to paste inside a lexical node uses the predicate INTERNAL_$isBlock to find an ancestor for a check to see if you're pasting into a code block, but if there isn't a block in the ancestors, the call fails cause it checks for '__language in firstBlock.

Lexical version: 0.12.6 - 14.6 (tested)

Steps To Reproduce

This simple custom Element node causes this problem, I can't get a paste event to not error without flipping isInline to true

import { EditorConfig, ElementNode } from 'lexical';
import { styles } from './formula.styles';
import { SerializedFormulaNode } from './formula.types';
import { $createFormulaNode } from './formula.utils';

export default class FormulaNode extends ElementNode {
  static getType(): string {
    return 'formula';
  }

  static clone(node: FormulaNode): FormulaNode {
    return new FormulaNode(node.__key);
  }

  createDOM(config: EditorConfig): HTMLElement {
    const dom = document.createElement('p');
    dom.setAttribute('type', 'formula');
    dom.spellcheck = false;
    dom.style.cssText = styles.toString();
    return dom;
  }

  updateDOM(prevNode: FormulaNode, dom: HTMLElement): boolean {
    return false;
  }

  exportJSON(): SerializedFormulaNode {
    return {
      ...super.exportJSON(),
      type: 'formula',

      version: 1,
    };
  }

  isInline() {
    return true;
  }

  static importJSON(serializedNode: SerializedFormulaNode): FormulaNode {
    const node = $createFormulaNode();
    return node;
  }
}

Link to code example: CodeSandbox isn't loading but when I get home I'll put one together

The current behavior

text-editor.comp.tsx:67 TypeError: Cannot use 'in' operator to search for '__language' in null
    at RangeSelection.insertNodes (Lexical.dev.mjs:6273:1)
    at RangeSelection.insertRawText (Lexical.dev.mjs:5863:1)
    at $insertDataTransferForPlainText (LexicalClipboard.dev.mjs:90:1)
    at editor.update.tag (LexicalPlainText.dev.mjs:76:1)
    at processNestedUpdates (Lexical.dev.mjs:7928:1)
    at beginUpdate (Lexical.dev.mjs:7979:1)
    at updateEditor (Lexical.dev.mjs:8054:1)
    at LexicalEditor.update (Lexical.dev.mjs:9587:1)
    at triggerCommandListeners (Lexical.dev.mjs:7851:1)
    at dispatchCommand (Lexical.dev.mjs:1258:1)

The expected behavior

The code in the copy buffer should be pasted

s0rta avatar May 16 '24 20:05 s0rta

could you resolved?

Alesso-Chiavarino avatar Jul 22 '24 21:07 Alesso-Chiavarino

i have the same error

p3x-robot avatar Oct 22 '24 09:10 p3x-robot

i fixed it. i mean with chatgpt, but now it works.

p3x-robot avatar Oct 23 '24 06:10 p3x-robot

Pretty sure that this was fixed

etrepum avatar Dec 03 '24 04:12 etrepum