protected internalInsertAfter()

in packages/core/src/rendering/render-view.ts [112:165]


  protected internalInsertAfter(child: ChildType, after?: ChildType) {
    const childParentData = child.parentData!
    assert(!childParentData.nextSibling)
    assert(!childParentData.prevSibling)
    this._childCount++
    assert(this._childCount > 0)
    if (!after) {
      // 从头部插入 (_firstChild)
      childParentData.nextSibling = this._firstChild
      if (this._firstChild) {
        const firstChildParentData = this._firstChild.parentData
        assert(firstChildParentData)
        firstChildParentData.prevSibling = child
      }
      this._firstChild = child
      this._lastChild ??= child
      if (this._yogaNode && child.yogaNode) {
        assert(!child.yogaNode.getParent(), '子节点的 yogaNode 不能已有 parent')
        this._yogaNode.insertChild(child.yogaNode, 0)
      }
    } else {
      assert(this._firstChild)
      assert(this._lastChild)
      assert(this.debugUltimatePrevSiblingOf(after, this._firstChild))
      assert(this.debugUltimateNextSiblingOf(after, this._lastChild))
      const afterParentData = after.parentData!
      if (!afterParentData.nextSibling) {
        // 从尾部插入 (_lastChild)
        assert(after === this._lastChild)
        childParentData.prevSibling = after
        afterParentData.nextSibling = child
        this._lastChild = child
        if (this._yogaNode && child.yogaNode) {
          assert(!child.yogaNode.getParent(), '子节点的 yogaNode 不能已有 parent')
          this._yogaNode.insertChild(child.yogaNode, this._childCount - 1)
        }
      } else {
        // 从中间插入
        childParentData.nextSibling = afterParentData.nextSibling
        childParentData.prevSibling = after
        const childPrevSiblingParentData = childParentData.prevSibling!.parentData!
        const childNextSiblingParentData = childParentData.nextSibling!.parentData!
        childPrevSiblingParentData.nextSibling = child
        childNextSiblingParentData.prevSibling = child
        assert(afterParentData.nextSibling === child)
        if (this._yogaNode && child.yogaNode) {
          assert(!child.yogaNode.getParent(), '子节点的 yogaNode 不能已有 parent')
          const childIndex = this.findChildIndex(after)
          assert(childIndex !== -1, `父节点中没有该子节点 ${child.id}`)
          this._yogaNode.insertChild(child.yogaNode, childIndex + 1)
        }
      }
    }
  }