Skip to content

Commit

Permalink
Merge pull request #7153 from NervJS/refactor/template
Browse files Browse the repository at this point in the history
使模板逻辑与小程序平台解耦
  • Loading branch information
Chen-jj authored Aug 19, 2020
2 parents 31971ba + eee6411 commit 6aeda35
Show file tree
Hide file tree
Showing 47 changed files with 3,139 additions and 5,739 deletions.
3 changes: 2 additions & 1 deletion packages/shared/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
"url": "git+https://github.com/NervJS/taro.git"
},
"scripts": {
"build": "rollup -c"
"build": "rollup -c",
"dev": "rollup -c -w"
},
"bugs": {
"url": "https://github.com/NervJS/taro/issues"
Expand Down
86 changes: 11 additions & 75 deletions packages/shared/src/components.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import { Shortcuts } from './shortcuts'
import { toDashed, hasOwn, toCamelCase } from './utils'
import { isBooleanStringLiteral, isNumber } from './is'

const styles = {
export const styles = {
style: `i.${Shortcuts.Style}`,
class: `i.${Shortcuts.Class}`
}

const events = {
export const events = {
bindtap: 'eh'
}

Expand All @@ -19,12 +17,13 @@ const touchEvents = {
bindLongTap: ''
}

const alipayEvents = {
onTap: 'eh',
onTouchMove: 'eh',
onTouchEnd: 'eh',
onTouchCancel: 'eh',
onLongTap: 'eh'
export const specialEvents = new Set([
'htouchmove',
'vtouchmove'
])

export function singleQuote (s: string) {
return `'${s}'`
}

const View = {
Expand Down Expand Up @@ -371,8 +370,8 @@ const ScrollView = {
bindRefresherRefresh: '',
bindRefresherRestore: '',
bindRefresherAbort: '',
bindScrolltoUpper: '',
bindScrolltoLower: '',
bindScrollToUpper: '',
bindScrollToLower: '',
bindScroll: '',
animation: '',
bindTransitionEnd: '',
Expand All @@ -382,10 +381,6 @@ const ScrollView = {
...touchEvents
}

function singleQuote (s: string) {
return `'${s}'`
}

const Swiper = {
'indicator-dots': 'false',
'indicator-color': singleQuote('rgba(0, 0, 0, .3)'),
Expand Down Expand Up @@ -453,11 +448,6 @@ const Audio = {
bindEnded: ''
}

const specialEvents = new Set([
'htouchmove',
'vtouchmove'
])

const Camera = {
mode: singleQuote('normal'),
'device-position': singleQuote('back'),
Expand Down Expand Up @@ -671,60 +661,6 @@ const Slot = {
name: ''
}

interface Components {
[key: string]: Record<string, string>;
}

export function createMiniComponents (components: Components, buildType: string) {
const result: Components = Object.create(null)
const isAlipay = buildType === 'alipay'

for (const key in components) {
if (hasOwn(components, key)) {
const component = components[key]
const compName = toDashed(key)
const newComp: Record<string, string> = Object.create(null)
for (let prop in component) {
if (hasOwn(component, prop)) {
let propValue = component[prop]
if (prop.startsWith('bind') || specialEvents.has(prop)) {
prop = isAlipay ? prop.replace('bind', 'on') : prop.toLowerCase()
if ((buildType === 'weapp' || buildType === 'qq') && prop === 'bindlongtap') {
prop = 'bindlongpress'
}
propValue = 'eh'
} else if (propValue === '') {
propValue = `i.${toCamelCase(prop)}`
} else if (isBooleanStringLiteral(propValue) || isNumber(+propValue)) {
propValue = `i.${toCamelCase(prop)} === undefined ? ${propValue} : i.${toCamelCase(prop)}`
} else {
propValue = `i.${toCamelCase(prop)} || ${propValue || singleQuote('')}`
}

newComp[prop] = propValue
}
}
if (compName !== 'block') {
Object.assign(newComp, styles, isAlipay ? alipayEvents : events)
}

if (compName === 'swiper-item') {
delete newComp.style
}

if (compName === 'slot' || compName === 'slot-view') {
result[compName] = {
slot: 'i.name'
}
} else {
result[compName] = newComp
}
}
}

return result
}

export const internalComponents = {
View,
Icon,
Expand Down
1 change: 1 addition & 0 deletions packages/shared/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export * from './is'
export { Shortcuts } from './shortcuts'
export * from './components'
export * from './utils'
export * from './template'
Loading

0 comments on commit 6aeda35

Please sign in to comment.