public addChildAfter()

in src/plugin/fieldViews/RepeaterFieldView.ts [98:138]


  public addChildAfter(index: number) {
    if (index < -1 || index > this.node.childCount - 1) {
      console.error(
        `Cannot add at index ${index}: index out of range. Minimum -1, Maximum ${
          this.node.childCount - 1
        }`
      );
      return;
    }
    const tr = this.outerView.state.tr;
    const repeaterChildNodeName = getRepeaterChildNameFromParent(
      this.node.type.name
    );
    const newNode = this.node.type.schema.nodes[
      repeaterChildNodeName
    ].createAndFill({ [RepeaterFieldMapIDKey]: getRepeaterID() });
    if (!newNode) {
      console.warn(
        `[prosemirror-elements]: Could not create new repeater node of type ${this.fieldName}: createAndFill did not return a node`
      );
      return;
    }
    if (index === -1) {
      // If index is -1, add to beginning of repeater node
      const positionToAddFrom =
        this.getPos() + this.contentOffset + this.offset;
      tr.insert(positionToAddFrom, newNode);
    } else {
      // If index supplied, add after child at given index
      const nodeToAddFrom = this.node.child(index);
      const startOfNodeToAddFrom = this.getStartOfChildNode(
        this.node,
        index,
        this.getPos(),
        this.offset
      );
      const positionToAddFrom = startOfNodeToAddFrom + nodeToAddFrom.nodeSize;
      tr.insert(positionToAddFrom, newNode);
    }
    this.outerView.dispatch(tr);
  }