Skip to content

Commit

Permalink
Merge pull request #1493 from didi/feat-process-event-mini
Browse files Browse the repository at this point in the history
fix: 小程序事件绑定
  • Loading branch information
hiyuki authored Jun 3, 2024
2 parents 276def8 + ef312ea commit 095c2d7
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 30 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function directiveHelperMixin () {
return {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@ const useInnerProps = (
capture: null
},
needPress: {
bubble: true,
capture: true
bubble: false,
capture: false
},
mpxPressInfo: {}
})
Expand Down
65 changes: 37 additions & 28 deletions packages/webpack-plugin/lib/template-compiler/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -1086,39 +1086,43 @@ function processEventReact (el, options, meta) {
el.attrsList.forEach(function ({ name, value }) {
const parsedEvent = config[mode].event.parseEvent(name)
if (parsedEvent) {
const type = parsedEvent.prefix + parsedEvent.eventName
const type = parsedEvent.eventName
const parsedFunc = parseFuncStr(value)
if (parsedFunc) {
if (!eventConfigMap[type]) {
eventConfigMap[type] = {
eventName: parsedEvent.eventName,
rawName: name,
configs: []
}
}
eventConfigMap[type].configs.push(parsedFunc)
eventConfigMap[type].configs.push(Object.assign({ name }, parsedFunc))
}
}
})

let wrapper

for (const type in eventConfigMap) {
let { configs, rawName, eventName } = eventConfigMap[type]
if (rawName) {
// 清空原始事件绑定
let has
do {
has = getAndRemoveAttr(el, rawName).has
} while (has)
// 清除修饰符
rawName = rawName.replace(/\..*/, '')
}
let { configs } = eventConfigMap[type]

let resultName
configs.forEach(({ name }) => {
if (name) {
// 清空原始事件绑定
let has
do {
has = getAndRemoveAttr(el, name).has
} while (has)

if (!resultName) {
// 清除修饰符
resultName = name.replace(/\..*/, '')
}
}
})
configs = configs.map((item) => {
return item.expStr
})
const name = rawName || config[mode].event.getEvent(eventName)
const name = resultName || config[mode].event.getEvent(type)
const value = `{{(e)=>this.__invoke(e, [${configs}])}}`
addAttrs(el, [
{
Expand Down Expand Up @@ -1169,11 +1173,10 @@ function processEvent (el, options) {
if (parsedFunc) {
if (!eventConfigMap[type]) {
eventConfigMap[type] = {
rawName: name,
configs: []
}
}
eventConfigMap[type].configs.push(parsedFunc)
eventConfigMap[type].configs.push(Object.assign({ name }, parsedFunc))
if (modifiers.indexOf('proxy') > -1 || options.forceProxyEvent) {
eventConfigMap[type].proxy = true
}
Expand Down Expand Up @@ -1232,7 +1235,7 @@ function processEvent (el, options) {

for (const type in eventConfigMap) {
let needBind = false
let { configs, rawName, proxy } = eventConfigMap[type]
const { configs, proxy } = eventConfigMap[type]
delete eventConfigMap[type]
if (proxy) {
needBind = true
Expand All @@ -1250,19 +1253,25 @@ function processEvent (el, options) {
}

if (needBind) {
if (rawName) {
// 清空原始事件绑定
let has
do {
has = getAndRemoveAttr(el, rawName).has
} while (has)
// 清除修饰符
rawName = rawName.replace(/\..*/, '')
}
let resultName
configs.forEach(({ name }) => {
if (name) {
// 清空原始事件绑定
let has
do {
has = getAndRemoveAttr(el, name).has
} while (has)

if (!resultName) {
// 清除修饰符
resultName = name.replace(/\..*/, '')
}
}
})

addAttrs(el, [
{
name: rawName || config[mode].event.getEvent(type),
name: resultName || config[mode].event.getEvent(type),
value: '__invoke'
}
])
Expand Down

0 comments on commit 095c2d7

Please sign in to comment.