Skip to content

Commit ba1accd

Browse files
author
lishiwen
committed
feat: vue3 onEvent
1 parent cfc5045 commit ba1accd

File tree

4 files changed

+69
-28
lines changed

4 files changed

+69
-28
lines changed

example/simple/App.js

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,46 @@
11
import { h } from "../../lib/m-vue.esm.js";
22

3-
// debugger instance
4-
window.vueInstance = null;
5-
63
export const App = {
7-
// vue template -> render
4+
// TODO: vue template -> render
85
render() {
9-
window.vueInstance = this;
10-
// return h("div", {}, "shi-wen" + this.msg);
116
return h(
127
"div",
138
{
14-
id: "mini-vue-page",
15-
class: "first-layer",
9+
class: "red",
10+
onClick() {
11+
console.log("click");
12+
},
13+
onMousemove() {
14+
console.log("mousemove");
15+
},
1616
},
17-
[
18-
h(
19-
"div",
20-
{
21-
class: "red",
22-
},
23-
`${this.msg}`
24-
),
25-
h(
26-
"div",
27-
{
28-
class: "blue",
29-
},
30-
`Hello Branlice ${this.subMessage}`
31-
),
32-
]
17+
this.msg
3318
);
34-
},
3519

20+
// return h(
21+
// "div",
22+
// {
23+
// id: "mini-vue-page",
24+
// class: "first-layer",
25+
// },
26+
// [
27+
// h(
28+
// "div",
29+
// {
30+
// class: "red",
31+
// },
32+
// `${this.msg}`
33+
// ),
34+
// h(
35+
// "div",
36+
// {
37+
// class: "blue",
38+
// },
39+
// `Hello Branlice ${this.subMessage}`
40+
// ),
41+
// ]
42+
// );
43+
},
3644
setup() {
3745
return {
3846
msg: "hello world 2022",

lib/m-vue.cjs.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,12 +131,23 @@ function mountElement(vnode, container) {
131131
function mountChildren(children = [], container) {
132132
children.forEach(child => { patch(child, container); });
133133
}
134+
function isOnEvent(propertyName) {
135+
return /^on[A-Z]/.test(propertyName);
136+
}
137+
isOnEvent.getEventName = function onEventName(propertyName) {
138+
return propertyName.slice(2).toLowerCase();
139+
};
134140
function addAttrs(vnode, container) {
135141
const props = vnode.props || {};
136142
for (const key in props) {
137143
if (Object.prototype.hasOwnProperty.call(props, key)) {
138144
const value = props[key];
139-
container.setAttribute(key, value);
145+
if (isOnEvent(key)) {
146+
container.addEventListener(isOnEvent.getEventName(key), value, false);
147+
}
148+
else {
149+
container.setAttribute(key, value);
150+
}
140151
}
141152
}
142153
}

lib/m-vue.esm.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,12 +127,23 @@ function mountElement(vnode, container) {
127127
function mountChildren(children = [], container) {
128128
children.forEach(child => { patch(child, container); });
129129
}
130+
function isOnEvent(propertyName) {
131+
return /^on[A-Z]/.test(propertyName);
132+
}
133+
isOnEvent.getEventName = function onEventName(propertyName) {
134+
return propertyName.slice(2).toLowerCase();
135+
};
130136
function addAttrs(vnode, container) {
131137
const props = vnode.props || {};
132138
for (const key in props) {
133139
if (Object.prototype.hasOwnProperty.call(props, key)) {
134140
const value = props[key];
135-
container.setAttribute(key, value);
141+
if (isOnEvent(key)) {
142+
container.addEventListener(isOnEvent.getEventName(key), value, false);
143+
}
144+
else {
145+
container.setAttribute(key, value);
146+
}
136147
}
137148
}
138149
}

src/runtime-core/render.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,23 @@ function mountChildren(children = [], container) {
5050
children.forEach(child => { patch(child, container) })
5151
}
5252

53+
function isOnEvent(propertyName) {
54+
return /^on[A-Z]/.test(propertyName)
55+
}
56+
isOnEvent.getEventName = function onEventName(propertyName) {
57+
return propertyName.slice(2).toLowerCase()
58+
}
59+
5360
function addAttrs(vnode, container) {
5461
const props = vnode.props || {};
5562
for (const key in props) {
5663
if (Object.prototype.hasOwnProperty.call(props, key)) {
5764
const value = props[key];
58-
container.setAttribute(key, value);
65+
if (isOnEvent(key)) {
66+
container.addEventListener(isOnEvent.getEventName(key), value, false);
67+
} else {
68+
container.setAttribute(key, value);
69+
}
5970
}
6071
}
6172
}

0 commit comments

Comments
 (0)