craft.js icon indicating copy to clipboard operation
craft.js copied to clipboard

Docs or Types are wrong for editor.actions.add

Open Oudwins opened this issue 1 year ago • 1 comments

The docs specify one type and the real type is different. Unsure if we should update the docs or the types. addNodeTree is, however, consistent.

Example given below, but please note the only difference is that by the docs the add function takes in an optional parentId and by the type the parentId is required. This is not the case for addNodeTree for which it is always an optional param.

DOCS add(nodes: Node, parentId?: NodeId, index?: number) => void Add a Node to the given parent node ID at the specified index. By default the parentId is the id of the Root Node addNodeTree(tree: NodeTree, parentId?: NodeId) => void Add a NodeTree to the given parent node ID at the specified index. By default the parentId is the id of the Root Node

TYPES

(property) add: (nodeToAdd: Node | Node[], parentId: string, index?: number | undefined) => void
(property) addNodeTree: (tree: NodeTree, parentId?: string | undefined, index?: number | undefined) => void

Oudwins avatar Dec 01 '23 09:12 Oudwins

Well, the addNodeTree function also has a problem. Even though the type says parentId is optional if called without it the error "Error: Invariant failed: Cannot add non-root Node without a parent" is thrown.

This happens using the same code from the docs

      <a onClick={() => {
        const nodeTree = query.parseReactElement(<h2>Hi</h2>).toNodeTree();
        actions.addNodeTree(nodeTree);
      }}>
        Add a new Node from a React Element
      </a>

Oudwins avatar Dec 01 '23 09:12 Oudwins