lexical icon indicating copy to clipboard operation
lexical copied to clipboard

Bug: Error converting a node in the slot

Open levensta opened this issue 4 months ago • 5 comments

Lexical version: 0.48 latest

Steps To Reproduce

  1. Insert a node containing a slot with a bare block (containing only one block node)
  2. Convert a node from a slot to another node using $setBlocksType or the replace method

Link to code example: https://playground.lexical.dev/#doc=H4sIAAAAAAAAE82RwWrDMBBEf8UMPYrgYAen-oCeAz6WHoS1iQWyZKS1W2P870VOStOUHppD6WlZoTc7OzuDtGEfalZMkDOC95xq0xqrAznI5y_Ni4A2gRo23kG6wVqBow-dYkhAwDhNjiFzAZ56gkSvgjoF1bcQGCnEFdwKML3x04XMz23Nk00IFvGzgxmaWBm7UsdPgc7rxLr0YiEQP8TO2pCIpustZdeGLh7XD9f2lj_a8_djAo2GXm9mPETrOaa7qYFbH75d8N7IUsmMy5L-P4hruScw72_MLgJWRa7VSBpyW-13xa4qH6uiLASiH0KTuINV0yn4welrGvmm3G9yLO84jLipOgMAAA

The current behavior

The node is not converted. This is expected behavior, since the slot is isolated, and if the user wants to change the slot's node, they must do so by explicitly calling $setSlot (or a combination of $removeSlot + $setSlot ?). Please correct me if I'm wrong

However, an error is displayed in the console, which could lead to confusion

https://github.com/user-attachments/assets/9737376d-108d-41f4-aee9-ed5014110d20

The expected behavior

If everything is correct in the current model and the node in the slot should not be replaced outside the $setSlot method, then the error should not appear in the console. If it is possible to track this error, a warning can be displayed in DEV mode

Impact of fix

Improving the DX through the use of Named Slots

levensta avatar Jul 29 '26 21:07 levensta

@mayrang fyi

levensta avatar Jul 29 '26 22:07 levensta

Right, this is a bug in the playground toolbar and/or $setBlocksType (for assuming that all blocks are directly parented), neither of which are really slot-aware. $setSlot is sufficient to replace a slot, no need to remove it first.

etrepum avatar Jul 30 '26 17:07 etrepum

Sorry for the late response — just got around to this.

Opened #8901. The fix skips bare slot values in $setBlocksType — if $getSlotHost(prevNode) returns a host, the node is left alone instead of calling replace() (which crashes on parentless slot values).

mayrang avatar Jul 31 '26 16:07 mayrang

How can I restrict the conversion in this case so that only one node type is assigned to a slot? For example, in ReviewNode it would be incorrect to allow nodes for quotes, code blocks, titles, etc. in place of the author slot

levensta avatar Jul 31 '26 19:07 levensta

Would a transform work for your case? Something like:

editor.registerNodeTransform(ReviewNode, (review) => {
  const author = $getSlot(review, 'author');
  if (author && !$isParagraphNode(author)) {
    const para = $createParagraphNode();
    para.append(...author.getChildren());
    $setSlot(review, 'author', para);
  }
});

Catches paste/import/collab. #8901 already skips slot values in the toolbar path.

mayrang avatar Aug 01 '26 07:08 mayrang