From f006f50317f4b75fac353bc988db057a281ba7f8 Mon Sep 17 00:00:00 2001 From: YuKun Liu <41265098+mrxiaozhuox@users.noreply.github.com> Date: Tue, 18 Jan 2022 14:52:14 +0800 Subject: [PATCH] feat: move `rpc` to handler --- examples/link.rs | 2 +- packages/desktop/src/index.js | 43 ++++++++++++----------------------- 2 files changed, 16 insertions(+), 29 deletions(-) diff --git a/examples/link.rs b/examples/link.rs index 49921f98a..41b010e8d 100644 --- a/examples/link.rs +++ b/examples/link.rs @@ -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"); }, diff --git a/packages/desktop/src/index.js b/packages/desktop/src/index.js index c141f56d4..0bd767581 100644 --- a/packages/desktop/src/index.js +++ b/packages/desktop/src/index.js @@ -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 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