1

For example, I will have an editor.children layout like so:

  [
    {
      type: 'paragraph',
      children: [
        {
          text: 'A line of text!',
        },
      ],
    },
    {
      type: 'variable',
      id: '12345',
      children: [
        {
          text: 'Text to be changed',
        },
      ],
    },
    {
      type: 'paragraph',
      children: [
        {
          text: 'A line of text!',
        },
      ],
    },
  ],

And I'm trying to change the text at [0,1,0] ("Text to be changed") to a new value. I'm using this function:

const updateVariableName = (editor, variableId, updatedName) => {
  let variableChipPath = null;

  for (const [node, path] of Node.nodes(editor)) {
    if (node.variableId === variableId && Element.isElement(node)) {
      variableChipPath = path;
      break;
    }
  }

  if (variableChipPath) {
    const textNodePath = [...variableChipPath, 0];
    Transforms.setNodes(editor, { text: updatedName }, { at: textNodePath });
  }
}

From everything I've read on the docs this seems like it should work. I'm getting the correct variableChipPath as [0, 1]. If I instead write the last line as:

Transforms.setNodes(editor, { id: "67890" }, { at: variableChipPath});

The id updates appropriately. I just can't seem to target children[0].text. Any ideas?

1 Answer 1

1

Slate doesn't support modifying children or text using the setNodes transform. This is an intentional limitation to ensure that the editor's value is updated using the correct operations.

To replace the content of a text node, you should use Transforms.insertText. If you pass the text node's path as the at option of insertText, then the existing text will be removed automatically (demo - you may need to click the link twice).

Transforms.insertText(editor, updatedName, { at: textNodePath });
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.