Page.ts 4.4 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1
import { queuePostFlushCb } from 'vue'
fxy060608's avatar
fxy060608 已提交
2 3 4 5 6
import {
  UniNode,
  NODE_TYPE_PAGE,
  UniBaseNode,
  IUniPageNode,
fxy060608's avatar
fxy060608 已提交
7
  formatLog,
fxy060608's avatar
fxy060608 已提交
8
} from '@dcloudio/uni-shared'
fxy060608's avatar
fxy060608 已提交
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
import {
  PageCreateAction,
  PageCreatedAction,
  PageAction,
  PageNodeOptions,
  ACTION_TYPE_CREATE,
  ACTION_TYPE_INSERT,
  ACTION_TYPE_REMOVE,
  ACTION_TYPE_SET_ATTRIBUTE,
  ACTION_TYPE_REMOVE_ATTRIBUTE,
  ACTION_TYPE_SET_TEXT,
  ACTION_TYPE_PAGE_CREATE,
  ACTION_TYPE_PAGE_CREATED,
} from '../../../PageAction'
import { VD_SYNC } from '../../../constants'
fxy060608's avatar
fxy060608 已提交
24 25 26 27 28 29 30

export default class UniPageNode extends UniNode implements IUniPageNode {
  pageId: number
  private _id: number = 1
  private createAction: PageCreateAction
  private createdAction: PageCreatedAction
  public updateActions: PageAction[] = []
fxy060608's avatar
fxy060608 已提交
31 32 33

  private _update: () => void

fxy060608's avatar
fxy060608 已提交
34 35 36 37 38 39 40 41 42 43 44 45 46
  constructor(
    pageId: number,
    options: PageNodeOptions,
    setup: boolean = false
  ) {
    super(NODE_TYPE_PAGE, '#page', null as unknown as IUniPageNode)
    this.nodeId = 0
    this.pageId = pageId
    this.pageNode = this

    this.createAction = [ACTION_TYPE_PAGE_CREATE, options]
    this.createdAction = [ACTION_TYPE_PAGE_CREATED]

fxy060608's avatar
fxy060608 已提交
47 48
    this._update = this.update.bind(this)

fxy060608's avatar
fxy060608 已提交
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
    setup && this.setup()
  }
  onCreate(thisNode: UniNode, nodeName: string | number) {
    pushCreateAction(this, thisNode.nodeId!, nodeName)
    return thisNode
  }
  onInsertBefore(thisNode: UniNode, newChild: UniNode, index: number) {
    pushInsertAction(this, newChild as UniBaseNode, thisNode.nodeId!, index)
    return newChild
  }
  onRemoveChild(thisNode: UniNode, oldChild: UniNode) {
    pushRemoveAction(this, oldChild.nodeId!, thisNode.nodeId!)
    return oldChild
  }
  onSetAttribute(thisNode: UniNode, qualifiedName: string, value: unknown) {
    if (thisNode.parentNode) {
      pushSetAttributeAction(this, thisNode.nodeId!, qualifiedName, value)
    }
  }
  onRemoveAttribute(thisNode: UniNode, qualifiedName: string) {
    if (thisNode.parentNode) {
      pushRemoveAttributeAction(this, thisNode.nodeId!, qualifiedName)
    }
  }
  onTextContent(thisNode: UniNode, text: string) {
    if (thisNode.parentNode) {
      pushSetTextAction(this, thisNode.nodeId!, text)
    }
  }
  onNodeValue(thisNode: UniNode, val: string | null) {
    if (thisNode.parentNode) {
      pushSetTextAction(this, thisNode.nodeId!, val as string)
    }
  }
  genId() {
    return this._id++
  }
  push(action: PageAction) {
    this.updateActions.push(action)
fxy060608's avatar
fxy060608 已提交
88 89 90 91
    if (__DEV__) {
      console.log(formatLog('PageNode', 'push', action))
    }
    queuePostFlushCb(this._update)
fxy060608's avatar
fxy060608 已提交
92 93 94 95 96 97 98 99 100
  }
  restore() {
    this.push(this.createAction)
    // TODO restore children
    this.push(this.createdAction)
  }
  setup() {
    this.send([this.createAction])
  }
fxy060608's avatar
fxy060608 已提交
101 102 103 104 105
  // mounted() {
  //   const { updateActions, createdAction } = this
  //   updateActions.unshift(createdAction)
  //   this.update()
  // }
fxy060608's avatar
fxy060608 已提交
106 107
  update() {
    const { updateActions } = this
fxy060608's avatar
fxy060608 已提交
108 109 110
    if (__DEV__) {
      console.log(formatLog('PageNode', 'update', updateActions.length))
    }
fxy060608's avatar
fxy060608 已提交
111 112 113 114 115 116
    if (updateActions.length) {
      this.send(updateActions)
      updateActions.length = 0
    }
  }
  send(action: PageAction[]) {
fxy060608's avatar
fxy060608 已提交
117
    UniServiceJSBridge.publishHandler(VD_SYNC, action, this.pageId)
fxy060608's avatar
fxy060608 已提交
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
  }
}

function pushCreateAction(
  pageNode: UniPageNode,
  nodeId: number,
  nodeName: string | number
) {
  pageNode.push([ACTION_TYPE_CREATE, nodeId, nodeName])
}

function pushInsertAction(
  pageNode: UniPageNode,
  newChild: UniBaseNode,
  parentNodeId: number,
  index: number
) {
  pageNode.push([
    ACTION_TYPE_INSERT,
    newChild.nodeId!,
    parentNodeId,
    index,
    newChild.toJSON({ attr: true }),
  ])
}

function pushRemoveAction(
  pageNode: UniPageNode,
  nodeId: number,
  parentNodeId: number
) {
  pageNode.push([ACTION_TYPE_REMOVE, nodeId, parentNodeId])
}

function pushSetAttributeAction(
  pageNode: UniPageNode,
  nodeId: number,
  name: string,
  value: unknown
) {
  pageNode.push([ACTION_TYPE_SET_ATTRIBUTE, nodeId, name, value])
}

function pushRemoveAttributeAction(
  pageNode: UniPageNode,
  nodeId: number,
  name: string
) {
  pageNode.push([ACTION_TYPE_REMOVE_ATTRIBUTE, nodeId, name])
}

function pushSetTextAction(
  pageNode: UniPageNode,
  nodeId: number,
  text: string
) {
  pageNode.push([ACTION_TYPE_SET_TEXT, nodeId, text])
}

export function createPageNode(
  pageId: number,
  pageOptions: PageNodeOptions,
  setup?: boolean
) {
  return new UniPageNode(pageId, pageOptions, setup)
}