-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
node.ts
173 lines (142 loc) · 4.19 KB
/
node.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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
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
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
import { NodeType } from './node_types'
import { incrementId } from '../utils'
import { TaroEventTarget } from './event_target'
import { eventSource } from './event'
import { TaroRootElement } from './root'
import { Shortcuts, ensure } from '@tarojs/shared'
import { hydrate, HydratedData } from '../render'
import { TaroElement } from './element'
const nodeId = incrementId()
export interface UpdatePayload {
path: string;
value: UpdatePayloadValue
}
export type UpdatePayloadValue = string | boolean | HydratedData
export class TaroNode extends TaroEventTarget {
public nodeType: NodeType
public nodeName: string
public uid: string
public parentNode: TaroNode | null = null
public childNodes: TaroNode[] = []
public constructor (nodeType: NodeType, nodeName: string) {
super()
this.nodeType = nodeType
this.nodeName = nodeName
this.uid = `_n_${nodeId()}`
eventSource.set(this.uid, this)
}
public get _path () {
if (this.parentNode !== null) {
return `${this.parentNode._path}.${Shortcuts.Childnodes}.${this.parentNode.childNodes.indexOf(this)}`
}
return ''
}
protected get _root (): TaroRootElement | null {
if (this.parentNode !== null) {
return this.parentNode._root
}
return null
}
public get nextSibling () {
const parentNode = this.parentNode
if (parentNode) {
return parentNode.childNodes[this.findIndex(parentNode.childNodes, this) + 1] || null
}
return null
}
public get previousSibling () {
const parentNode = this.parentNode
if (parentNode) {
return parentNode.childNodes[this.findIndex(parentNode.childNodes, this) - 1]
}
}
public insertBefore<T extends TaroNode> (newChild: T, refChild?: TaroNode | null, isReplace?: boolean): T {
newChild.remove()
newChild.parentNode = this
let payload: UpdatePayload
if (refChild) {
const index = this.findIndex(this.childNodes, refChild)
this.childNodes.splice(index, 0, newChild)
if (isReplace === true) {
payload = {
path: newChild._path,
value: this.hydrate(newChild)
}
} else {
payload = {
path: `${this._path}.${Shortcuts.Childnodes}`,
value: () => this.childNodes.map(hydrate)
}
}
} else {
this.childNodes.push(newChild)
payload = {
path: newChild._path,
value: this.hydrate(newChild)
}
}
this.enqueueUpdate(payload)
return newChild
}
private hydrate = (node: TaroNode) => () => hydrate(node as TaroElement)
public appendChild (child: TaroNode) {
this.insertBefore(child)
}
public replaceChild (newChild: TaroNode, oldChild: TaroNode) {
if (oldChild.parentNode === this) {
this.insertBefore(newChild, oldChild, true)
oldChild.remove(true)
return oldChild
}
}
public removeChild<T extends TaroNode> (child: T, isReplace?: boolean): T {
const index = this.findIndex(this.childNodes, child)
this.childNodes.splice(index, 1)
if (isReplace !== true) {
this.enqueueUpdate({
path: `${this._path}.${Shortcuts.Childnodes}`,
value: () => this.childNodes.map(hydrate)
})
}
child.parentNode = null
eventSource.delete(this.uid)
return child
}
public remove (isReplace?: boolean) {
if (this.parentNode) {
this.parentNode.removeChild(this, isReplace)
}
}
public get firstChild () {
return this.childNodes[0]
}
public get lastChild () {
const c = this.childNodes
return c[c.length - 1]
}
public hasChildNodes () {
return this.childNodes.length > 0
}
public enqueueUpdate (payload: UpdatePayload) {
if (this._root === null) {
return
}
this._root.enqueueUpdate(payload)
}
/**
* @textContent 目前只能置空子元素
* @TODO 等待完整 innerHTML 实现
*/
public set textContent (text: string) {
if (text === '') {
while (this.childNodes.length > 0) {
this.childNodes[0].remove()
}
}
}
protected findIndex (childeNodes: TaroNode[], refChild: TaroNode) {
const index = childeNodes.indexOf(refChild)
ensure(index !== -1, 'The node to be replaced is not a child of this node.')
return index
}
}