feat: move rpc to handler

This commit is contained in:
YuKun Liu 2022-01-18 14:52:14 +08:00
parent 9e04ce5342
commit f006f50317
2 changed files with 16 additions and 29 deletions

View file

@ -16,7 +16,7 @@ fn app(cx: Scope) -> Element {
p {
a {
href: "http://dioxuslabs.com/",
"browser-open": "false",
prevent_default: "onclick",
onclick: |_| {
println!("Hello Dioxus");
},

View file

@ -343,6 +343,7 @@ class Interpreter {
this.listeners[event_name] = true;
this.root.addEventListener(event_name, (event) => {
const target = event.target;
const real_id = target.getAttribute(`dioxus-id`);
@ -356,6 +357,20 @@ class Interpreter {
event.preventDefault();
}
console.log(event);
if (event.type == "click") {
event.preventDefault();
if (should_prevent_default !== `onclick`) {
console.log(event.target.getAttribute("href"));
if(element.tagName == "A") {
rpc.call("browser_open", {
mounted_dom_id: parseInt(real_id),
href: event.target.getAttribute("href")
})
}
}
}
if (real_id == null) {
return;
}
@ -401,34 +416,6 @@ class Interpreter {
break;
case "dangerous_inner_html":
node.innerHTML = value;
break;
case "href":
if (node.tagName == "A") {
// open the <a> tag in browser
node.setAttribute("browser-href", value);
node.setAttribute("href", "#");
node.addEventListener("click", function(event) {
const target = event.target;
const real_id = target.getAttribute(`dioxus-id`);
if (target.getAttribute("browser-open") != "false") {
rpc.call("browser_open", {
mounted_dom_id: parseInt(real_id),
href: target.getAttribute("browser-href"),
});
} else {
window.location = target.getAttribute("browser-href");
}
})
} else {
node.setAttribute(name, value);
}
break;
default:
// https://github.com/facebook/react/blob/8b88ac2592c5f555f315f9440cbb665dd1e7457a/packages/react-dom/src/shared/DOMProperty.js#L352-L364