-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
index.ts
232 lines (198 loc) · 7.16 KB
/
index.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
import { internalComponents, Shortcuts, createMiniComponents, focusComponents, isArray, capitalize } from '@tarojs/shared'
import { Adapter, supportXS } from './adapters'
import { BUILD_TYPES } from '../utils/constants'
import { componentConfig } from './component'
interface Component {
nodeName: string;
attributes: Attributes;
}
type Attributes = Record<string, string>
const voidElements = new Set([
'progress',
'icon',
'rich-text',
'input',
'textarea',
'slider',
'switch',
'audio',
'camera',
'live-player',
'live-pusher',
'video'
])
const swanSpecialAttrs = {
'scroll-view': ['scrollTop', 'scrollLeft', 'scrollIntoView'],
'movable-view': ['x', 'y'],
slider: ['value'],
input: ['value'],
textarea: ['value']
}
function buildAttribute (attrs: Attributes, nodeName: string): string {
function getValue (key: string) {
if (Adapter.type === BUILD_TYPES.SWAN && isArray(swanSpecialAttrs[nodeName]) && swanSpecialAttrs[nodeName].includes(key)) {
return `= ${attrs[key]} =`
}
return `{ ${attrs[key]} }`
}
return Object.keys(attrs)
.map(k => `${k}="${k.startsWith('bind') || k.startsWith('on') ? attrs[k] : `{${getValue(k)}}`}" `)
.join('')
}
const dataKeymap = (keymap: string) => {
return Adapter.type === BUILD_TYPES.SWAN ? `{ ${keymap} }` : keymap
}
function buildStandardComponentTemplate (comp: Component, level: number, supportRecursive: boolean): string {
const nextLevel = supportRecursive ? 0 : level + 1
const child = Adapter.type === BUILD_TYPES.SWAN && comp.nodeName === 'text'
? `<block>{{ i.${Shortcuts.Childnodes}[index].${Shortcuts.Text} }}</block>`
: `<template is="tmpl_${nextLevel}_${Shortcuts.Container}" data="{{${dataKeymap('i: item')}}}" />`
const children = voidElements.has(comp.nodeName)
? ''
: `
<block ${Adapter.for}="{{i.${Shortcuts.Childnodes}}}" ${Adapter.key}="id">
${child}
</block>
`
return `
<template name="tmpl_${level}_${comp.nodeName}">
<${comp.nodeName} ${buildAttribute(comp.attributes, comp.nodeName)} id="{{ i.uid }}">${children}</${comp.nodeName}>
</template>
`
}
function buildXsTemplate () {
let xs = ''
if (Adapter.type === BUILD_TYPES.WEAPP || Adapter.type === BUILD_TYPES.QQ) {
xs = `<wxs module="xs" src="./utils.${Adapter.xs}" />`
} else if (Adapter.type === BUILD_TYPES.ALIPAY) {
xs = `<import-sjs name="xs" from="./utils.${Adapter.xs}" />`
} else if (Adapter.type === BUILD_TYPES.SWAN) {
xs = `<import-sjs module="xs" src="./utils.${Adapter.xs}" />`
}
return xs
}
export function buildXScript () {
const exportExpr = Adapter.type === BUILD_TYPES.ALIPAY ? 'export default' : 'module.exports ='
return `${exportExpr} {
c: function(i, prefix) {
var s = i.focus !== undefined ? 'focus' : 'blur'
return prefix + i.${Shortcuts.NodeName} + '_' + s
}
}`
}
function buildComponentTemplate (comp: Component, level: number, supportRecursive: boolean) {
return focusComponents.has(comp.nodeName)
? buildFocusComponentTemplte(comp, level)
: buildStandardComponentTemplate(comp, level, supportRecursive)
}
function buildFocusComponentTemplte (comp: Component, level: number) {
const attrs = { ...comp.attributes }
const templateName = supportXS()
? `xs.c(i, 'tmpl_${level}_')`
: `i.focus ? 'tmpl_${level}_${comp.nodeName}_focus' : 'tmpl_${level}_${comp.nodeName}_blur'`
delete attrs.focus
return `
<template name="tmpl_${level}_${comp.nodeName}">
<template is="{{ ${templateName} }}" data="{{${dataKeymap('i: i')}}}" />
</template>
<template name="tmpl_${level}_${comp.nodeName}_focus">
<${comp.nodeName} ${buildAttribute(comp.attributes, comp.nodeName)} id="{{ i.uid }}" />
</template>
<template name="tmpl_${level}_${comp.nodeName}_blur">
<${comp.nodeName} ${buildAttribute(attrs, comp.nodeName)} id="{{ i.uid }}" />
</template>
`
}
function buildPlainTextTemplate (level: number): string {
return `
<template name="tmpl_${level}_#text" data="{{${dataKeymap('i: i')}}}">
<block>{{i.${Shortcuts.Text}}}</block>
</template>
`
}
function buildContainerTemplate (level: number, restart = false) {
let tmpl = ''
if (restart) {
tmpl = '<comp i="{{i}}" />'
} else {
tmpl = `<template is="{{'tmpl_${level}_' + i.${Shortcuts.NodeName}}}" data="{{${dataKeymap('i: i')}}}" />`
}
return `
<template name="tmpl_${level}_${Shortcuts.Container}" data="{{${dataKeymap('i: i')}}}">
${tmpl}
</template>
`
}
function buildTemplate (level: number, supportRecursive: boolean, restart = false) {
const miniComponents = createMiniComponents(internalComponents, Adapter.type === BUILD_TYPES.ALIPAY)
const components = Object.keys(miniComponents).filter(c => componentConfig.includes.size ? componentConfig.includes.has(c) : true)
let template = ''
for (const nodeName of components) {
const attributes: Attributes = miniComponents[nodeName]
template += buildComponentTemplate({ nodeName, attributes }, level, supportRecursive)
}
template += buildPlainTextTemplate(level)
template += buildThirdPartyTemplate(level, supportRecursive)
template += buildContainerTemplate(level, restart)
return template
}
function buildThirdPartyAttr (attrs: Set<string>) {
return [...attrs].reduce((str, attr) => {
if (attr.startsWith('@')) { // vue event
if (Adapter.type === BUILD_TYPES.ALIPAY) {
return str + `on${capitalize(attr.slice(1))}="eh" `
}
return str + `bind${attr.slice(1)}="eh" `
} else if (attr.startsWith('bind')) {
return str + `${attr}="eh" `
} else if (attr.startsWith('on')) {
if (Adapter.type === BUILD_TYPES.ALIPAY) {
return str + `${attr}="eh" `
}
return str + `bind${attr.slice(2).toLowerCase()}="eh" `
}
return str + `${attr}="{{ i.${attr} }}" `
}, '')
}
function buildThirdPartyTemplate (level: number, supportRecursive: boolean) {
const nextLevel = supportRecursive ? 0 : level + 1
let template = ''
for (const [compName, attrs] of componentConfig.thirdPartyComponents) {
template += `
<template name="tmpl_${level}_${compName}">
<${compName} ${buildThirdPartyAttr(attrs)} id="{{ i.uid }}">
<block ${Adapter.for}="{{i.${Shortcuts.Childnodes}}}" ${Adapter.key}="id">
<template is="tmpl_${nextLevel}_${Shortcuts.Container}" data="{{${dataKeymap('i: item')}}}" />
</block>
</${compName}>
</template>
`
}
return template
}
export function buildBaseTemplate (maxLevel: number, supportRecursive: boolean) {
let template = `${buildXsTemplate()}
<template name="taro_tmpl">
<block ${Adapter.for}="{{root.cn}}" ${Adapter.key}="id">
<template is="tmpl_0_${Shortcuts.Container}" data="{{${dataKeymap('i: item')}}}" />
</block>
</template>
`
if (supportRecursive) {
template += buildTemplate(0, supportRecursive)
} else {
for (let i = 0; i < maxLevel; i++) {
template += buildTemplate(i, supportRecursive, maxLevel === i + 1)
}
}
return template
}
export function buildPageTemplate (baseTempPath: string) {
const template = `<import src="${baseTempPath}"/>
<template is="taro_tmpl" data="{{${dataKeymap('root: root')}}}" />`
return template
}
export function buildBaseComponentTemplate () {
return `<import src="./base.wxml" />
<template is="tmpl_0_container" data="{{${dataKeymap('i: i')}}}" />`
}