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

fxy060608's avatar
fxy060608 已提交
33 34 35 36 37 38 39 40
import {
  ACTION_MINIFY,
  ACTION_TYPE_DICT,
  DictAction,
  Dictionary,
  Value,
  VD_SYNC,
} from '../../../constants'
fxy060608's avatar
fxy060608 已提交
41
import { getPageById } from '../page/getCurrentPages'
fxy060608's avatar
fxy060608 已提交
42 43 44 45

export default class UniPageNode extends UniNode implements IUniPageNode {
  pageId: number
  private _id: number = 1
fxy060608's avatar
fxy060608 已提交
46
  private _created: boolean = false
fxy060608's avatar
fxy060608 已提交
47
  private options: PageNodeOptions
fxy060608's avatar
fxy060608 已提交
48 49
  private createAction: PageCreateAction
  private createdAction: PageCreatedAction
fxy060608's avatar
fxy060608 已提交
50
  private scrollAction?: PageScrollAction
fxy060608's avatar
fxy060608 已提交
51
  private _createActionMap = new Map<number, CreateAction>()
fxy060608's avatar
fxy060608 已提交
52 53 54 55 56 57 58 59
  public updateActions: (PageAction | DictAction)[] = []

  public dicts: Dictionary = []

  public normalizeDict: (
    value: unknown,
    normalizeValue?: boolean
  ) => any | number
fxy060608's avatar
fxy060608 已提交
60

fxy060608's avatar
fxy060608 已提交
61 62
  public isUnmounted: boolean

fxy060608's avatar
fxy060608 已提交
63 64
  private _update: () => void

fxy060608's avatar
fxy060608 已提交
65 66 67 68 69 70 71 72 73
  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
fxy060608's avatar
fxy060608 已提交
74
    this.options = options
fxy060608's avatar
fxy060608 已提交
75

fxy060608's avatar
fxy060608 已提交
76 77
    this.isUnmounted = false

fxy060608's avatar
fxy060608 已提交
78 79 80
    this.createAction = [ACTION_TYPE_PAGE_CREATE, options]
    this.createdAction = [ACTION_TYPE_PAGE_CREATED]

fxy060608's avatar
fxy060608 已提交
81 82
    this.normalizeDict = this._normalizeDict.bind(this)

fxy060608's avatar
fxy060608 已提交
83 84
    this._update = this.update.bind(this)

fxy060608's avatar
fxy060608 已提交
85 86
    setup && this.setup()
  }
fxy060608's avatar
fxy060608 已提交
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
  _normalizeDict(value: unknown, normalizeValue: boolean = true) {
    if (!ACTION_MINIFY) {
      return value
    }
    if (!isPlainObject(value)) {
      return this.addDict(value as Value)
    }
    const dictArray: [number, number][] = []
    Object.keys(value).forEach((n) => {
      const dict = [this.addDict(n) as number]
      const v = value[n as keyof typeof value] as Value
      if (normalizeValue) {
        dict.push(this.addDict(v) as number)
      } else {
        dict.push(v as number)
      }
      dictArray.push(dict as [number, number])
    })
    return dictArray
  }
  addDict<T extends Value>(value: T): T | number {
    if (!ACTION_MINIFY) {
      return value
    }
    const { dicts } = this
    const index = dicts.indexOf(value)
    if (index > -1) {
      return index
    }
    return dicts.push(value) - 1
  }
fxy060608's avatar
fxy060608 已提交
118 119 120 121 122 123 124 125 126 127 128 129
  onInjectHook(hook: string) {
    if (
      (hook === ON_PAGE_SCROLL || hook === ON_REACH_BOTTOM) &&
      !this.scrollAction
    ) {
      this.scrollAction = [
        ACTION_TYPE_PAGE_SCROLL,
        this.options.onReachBottomDistance,
      ]
      this.push(this.scrollAction)
    }
  }
fxy060608's avatar
fxy060608 已提交
130 131 132 133
  onCreate(thisNode: UniNode, nodeName: string | number) {
    pushCreateAction(this, thisNode.nodeId!, nodeName)
    return thisNode
  }
fxy060608's avatar
fxy060608 已提交
134 135 136 137 138 139 140 141 142 143 144
  onInsertBefore(
    thisNode: UniNode,
    newChild: UniNode,
    refChild: UniNode | null
  ) {
    pushInsertAction(
      this,
      newChild as UniBaseNode,
      thisNode.nodeId!,
      (refChild && refChild.nodeId!) || -1
    )
fxy060608's avatar
fxy060608 已提交
145 146
    return newChild
  }
fxy060608's avatar
fxy060608 已提交
147 148
  onRemoveChild(oldChild: UniNode) {
    pushRemoveAction(this, oldChild.nodeId!)
fxy060608's avatar
fxy060608 已提交
149 150
    return oldChild
  }
fxy060608's avatar
fxy060608 已提交
151 152 153 154 155
  onAddEvent(thisNode: UniNode, name: string, flag: number) {
    if (thisNode.parentNode) {
      pushAddEventAction(this, thisNode.nodeId!, name, flag)
    }
  }
fxy060608's avatar
fxy060608 已提交
156 157 158 159 160 161 162 163 164 165
  onAddWxsEvent(
    thisNode: UniNode,
    name: string,
    wxsEvent: string,
    flag: number
  ) {
    if (thisNode.parentNode) {
      pushAddWxsEventAction(this, thisNode.nodeId!, name, wxsEvent, flag)
    }
  }
fxy060608's avatar
fxy060608 已提交
166 167 168 169 170
  onRemoveEvent(thisNode: UniNode, name: string) {
    if (thisNode.parentNode) {
      pushRemoveEventAction(this, thisNode.nodeId!, name)
    }
  }
fxy060608's avatar
fxy060608 已提交
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
  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++
  }
fxy060608's avatar
fxy060608 已提交
194
  push(action: PageAction, extras?: unknown) {
fxy060608's avatar
fxy060608 已提交
195 196 197 198 199 200
    if (this.isUnmounted) {
      if (__DEV__) {
        console.log(formatLog('PageNode', 'push.prevent', action))
      }
      return
    }
fxy060608's avatar
fxy060608 已提交
201 202 203 204 205 206 207
    switch (action[0]) {
      case ACTION_TYPE_CREATE:
        this._createActionMap.set(action[1], action)
        break
      case ACTION_TYPE_INSERT:
        const createAction = this._createActionMap.get(action[1])
        if (createAction) {
fxy060608's avatar
fxy060608 已提交
208
          createAction[3] = action[2] // parentNodeId
fxy060608's avatar
fxy060608 已提交
209
          createAction[4] = action[3] // anchorId
fxy060608's avatar
fxy060608 已提交
210
          if (extras) {
fxy060608's avatar
fxy060608 已提交
211
            createAction[5] = extras as UniNodeJSON
fxy060608's avatar
fxy060608 已提交
212
          }
fxy060608's avatar
fxy060608 已提交
213 214 215 216 217 218 219
        } else {
          if (__DEV__) {
            console.error(formatLog(`Insert`, action, 'not found createAction'))
          }
        }
        break
    }
fxy060608's avatar
fxy060608 已提交
220 221 222 223
    // insert 被合并进 create
    if (action[0] !== ACTION_TYPE_INSERT) {
      this.updateActions.push(action)
    }
fxy060608's avatar
fxy060608 已提交
224
    queuePostFlushCb(this._update)
fxy060608's avatar
fxy060608 已提交
225 226
  }
  restore() {
fxy060608's avatar
fxy060608 已提交
227
    this.clear()
fxy060608's avatar
fxy060608 已提交
228
    this.push(this.createAction)
fxy060608's avatar
fxy060608 已提交
229 230 231
    if (this.scrollAction) {
      this.push(this.scrollAction)
    }
fxy060608's avatar
fxy060608 已提交
232 233 234 235 236 237 238 239
    const restoreNode = (node: UniNode) => {
      this.onCreate(node, node.nodeName)
      this.onInsertBefore(node.parentNode!, node, null)
      node.childNodes.forEach((childNode) => {
        restoreNode(childNode)
      })
    }
    this.childNodes.forEach((childNode) => restoreNode(childNode))
fxy060608's avatar
fxy060608 已提交
240 241 242 243 244 245
    this.push(this.createdAction)
  }
  setup() {
    this.send([this.createAction])
  }
  update() {
fxy060608's avatar
fxy060608 已提交
246
    const { dicts, updateActions, _createActionMap } = this
fxy060608's avatar
fxy060608 已提交
247
    if (__DEV__) {
fxy060608's avatar
fxy060608 已提交
248 249 250 251 252 253 254 255
      console.log(
        formatLog(
          'PageNode',
          'update',
          updateActions.length,
          _createActionMap.size
        )
      )
fxy060608's avatar
fxy060608 已提交
256
    }
fxy060608's avatar
fxy060608 已提交
257 258 259 260 261
    // 首次
    if (!this._created) {
      this._created = true
      updateActions.push(this.createdAction)
    }
fxy060608's avatar
fxy060608 已提交
262
    if (updateActions.length) {
fxy060608's avatar
fxy060608 已提交
263 264 265
      if (dicts.length) {
        updateActions.unshift([ACTION_TYPE_DICT, dicts])
      }
fxy060608's avatar
fxy060608 已提交
266 267
      this.send(updateActions)
    }
fxy060608's avatar
fxy060608 已提交
268 269 270 271 272 273
    this.clear()
  }
  clear() {
    this.dicts.length = 0
    this.updateActions.length = 0
    this._createActionMap.clear()
fxy060608's avatar
fxy060608 已提交
274
  }
fxy060608's avatar
fxy060608 已提交
275
  send(action: (PageAction | DictAction)[]) {
fxy060608's avatar
fxy060608 已提交
276
    UniServiceJSBridge.publishHandler(VD_SYNC, action, this.pageId)
fxy060608's avatar
fxy060608 已提交
277
  }
fxy060608's avatar
fxy060608 已提交
278 279 280 281 282 283 284 285 286
  fireEvent(id: number, evt: UniEvent) {
    const node = findNodeById(id, this)
    if (node) {
      node.dispatchEvent(evt)
    } else if (__DEV__) {
      console.error(formatLog('PageNode', 'fireEvent', id, 'not found', evt))
    }
  }
}
fxy060608's avatar
fxy060608 已提交
287 288 289 290 291
export function getPageNode(pageId: number): UniPageNode | null {
  const page = getPageById(pageId)
  if (!page) return null
  return (page as any).__page_container__ as UniPageNode
}
fxy060608's avatar
fxy060608 已提交
292

fxy060608's avatar
fxy060608 已提交
293 294 295 296 297 298 299 300 301
function findNode(
  name: 'nodeId' | 'nodeName',
  value: string | number,
  uniNode: UniNode | number
): UniNode | null {
  if (typeof uniNode === 'number') {
    uniNode = getPageNode(uniNode) as UniNode
  }
  if (uniNode[name] === value) {
fxy060608's avatar
fxy060608 已提交
302 303 304 305
    return uniNode
  }
  const { childNodes } = uniNode
  for (let i = 0; i < childNodes.length; i++) {
fxy060608's avatar
fxy060608 已提交
306
    const uniNode = findNode(name, value, childNodes[i])
fxy060608's avatar
fxy060608 已提交
307 308 309 310 311
    if (uniNode) {
      return uniNode
    }
  }
  return null
fxy060608's avatar
fxy060608 已提交
312 313
}

fxy060608's avatar
fxy060608 已提交
314 315 316 317 318 319 320 321 322 323 324
export function findNodeById(nodeId: number, uniNode: UniNode | number) {
  return findNode('nodeId', nodeId, uniNode)
}

export function findNodeByTagName(
  tagName: string,
  uniNode: UniNode | number
): UniNode | null {
  return findNode('nodeName', tagName.toUpperCase(), uniNode)
}

fxy060608's avatar
fxy060608 已提交
325 326 327 328 329
function pushCreateAction(
  pageNode: UniPageNode,
  nodeId: number,
  nodeName: string | number
) {
fxy060608's avatar
fxy060608 已提交
330 331 332 333 334 335 336
  pageNode.push([
    ACTION_TYPE_CREATE,
    nodeId,
    pageNode.addDict(nodeName),
    -1,
    -1,
  ])
fxy060608's avatar
fxy060608 已提交
337 338 339 340 341 342
}

function pushInsertAction(
  pageNode: UniPageNode,
  newChild: UniBaseNode,
  parentNodeId: number,
fxy060608's avatar
fxy060608 已提交
343
  refChildId: number
fxy060608's avatar
fxy060608 已提交
344
) {
fxy060608's avatar
fxy060608 已提交
345 346 347 348
  const nodeJson = newChild.toJSON({
    attr: true,
    normalize: pageNode.normalizeDict,
  })
fxy060608's avatar
fxy060608 已提交
349 350 351 352
  pageNode.push(
    [ACTION_TYPE_INSERT, newChild.nodeId!, parentNodeId, refChildId],
    Object.keys(nodeJson).length ? nodeJson : undefined
  )
fxy060608's avatar
fxy060608 已提交
353 354
}

fxy060608's avatar
fxy060608 已提交
355 356
function pushRemoveAction(pageNode: UniPageNode, nodeId: number) {
  pageNode.push([ACTION_TYPE_REMOVE, nodeId])
fxy060608's avatar
fxy060608 已提交
357 358
}

fxy060608's avatar
fxy060608 已提交
359 360 361 362 363 364
function pushAddEventAction(
  pageNode: UniPageNode,
  nodeId: number,
  name: string,
  value: number
) {
fxy060608's avatar
fxy060608 已提交
365
  pageNode.push([ACTION_TYPE_ADD_EVENT, nodeId, pageNode.addDict(name), value])
fxy060608's avatar
fxy060608 已提交
366 367
}

fxy060608's avatar
fxy060608 已提交
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383
function pushAddWxsEventAction(
  pageNode: UniPageNode,
  nodeId: number,
  name: string,
  wxsEvent: string,
  value: number
) {
  pageNode.push([
    ACTION_TYPE_ADD_WXS_EVENT,
    nodeId,
    pageNode.addDict(name),
    pageNode.addDict(wxsEvent),
    value,
  ])
}

fxy060608's avatar
fxy060608 已提交
384 385 386 387 388
function pushRemoveEventAction(
  pageNode: UniPageNode,
  nodeId: number,
  name: string
) {
fxy060608's avatar
fxy060608 已提交
389
  pageNode.push([ACTION_TYPE_REMOVE_EVENT, nodeId, pageNode.addDict(name)])
fxy060608's avatar
fxy060608 已提交
390 391
}

fxy060608's avatar
fxy060608 已提交
392 393 394 395 396 397 398 399 400 401
function normalizeAttrValue(
  pageNode: UniPageNode,
  name: string,
  value: unknown
) {
  return name === 'style' && isPlainObject(value)
    ? pageNode.normalizeDict(value)
    : pageNode.addDict(value as Value)
}

fxy060608's avatar
fxy060608 已提交
402 403 404 405 406 407
function pushSetAttributeAction(
  pageNode: UniPageNode,
  nodeId: number,
  name: string,
  value: unknown
) {
fxy060608's avatar
fxy060608 已提交
408 409 410 411
  pageNode.push([
    ACTION_TYPE_SET_ATTRIBUTE,
    nodeId,
    pageNode.addDict(name),
fxy060608's avatar
fxy060608 已提交
412
    normalizeAttrValue(pageNode, name, value),
fxy060608's avatar
fxy060608 已提交
413
  ])
fxy060608's avatar
fxy060608 已提交
414 415 416 417 418 419 420
}

function pushRemoveAttributeAction(
  pageNode: UniPageNode,
  nodeId: number,
  name: string
) {
fxy060608's avatar
fxy060608 已提交
421
  pageNode.push([ACTION_TYPE_REMOVE_ATTRIBUTE, nodeId, pageNode.addDict(name)])
fxy060608's avatar
fxy060608 已提交
422 423 424 425 426 427 428
}

function pushSetTextAction(
  pageNode: UniPageNode,
  nodeId: number,
  text: string
) {
fxy060608's avatar
fxy060608 已提交
429
  pageNode.push([ACTION_TYPE_SET_TEXT, nodeId, pageNode.addDict(text)])
fxy060608's avatar
fxy060608 已提交
430 431 432 433 434 435 436 437 438
}

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