Sfoglia il codice sorgente

fix liveview links

Evan Almloff 2 anni fa
parent
commit
28ef3f8968

+ 1 - 1
packages/desktop/src/protocol.rs

@@ -17,7 +17,7 @@ fn module_loader(root_name: &str) -> String {
     let rootname = "{root_name}";
     let root = window.document.getElementById(rootname);
     if (root != null) {{
-        window.interpreter = new Interpreter(root);
+        window.interpreter = new Interpreter(root, new InterpreterConfig(true));
         window.ipc.postMessage(serializeIpcMessage("initialize"));
     }}
 </script>

+ 5 - 1
packages/interpreter/src/bindings.rs

@@ -6,10 +6,14 @@ use web_sys::Element;
 
 #[wasm_bindgen(module = "/src/interpreter.js")]
 extern "C" {
+    pub type InterpreterConfig;
+    #[wasm_bindgen(constructor)]
+    pub fn new(intercept_link_redirects: bool) -> InterpreterConfig;
+
     pub type Interpreter;
 
     #[wasm_bindgen(constructor)]
-    pub fn new(arg: Element) -> Interpreter;
+    pub fn new(arg: Element, config: InterpreterConfig) -> Interpreter;
 
     #[wasm_bindgen(method)]
     pub fn SaveTemplate(this: &Interpreter, template: JsValue);

+ 30 - 18
packages/interpreter/src/interpreter.js

@@ -17,8 +17,7 @@ class ListenerMap {
       } else {
         this.global[event_name].active++;
       }
-    }
-    else {
+    } else {
       const id = element.getAttribute("data-dioxus-id");
       if (!this.local[id]) {
         this.local[id] = {};
@@ -32,11 +31,13 @@ class ListenerMap {
     if (bubbles) {
       this.global[event_name].active--;
       if (this.global[event_name].active === 0) {
-        this.root.removeEventListener(event_name, this.global[event_name].callback);
+        this.root.removeEventListener(
+          event_name,
+          this.global[event_name].callback
+        );
         delete this.global[event_name];
       }
-    }
-    else {
+    } else {
       const id = element.getAttribute("data-dioxus-id");
       delete this.local[id][event_name];
       if (this.local[id].length === 0) {
@@ -52,8 +53,15 @@ class ListenerMap {
   }
 }
 
+class InterpreterConfig {
+  constructor(intercept_link_redirects) {
+    this.intercept_link_redirects = intercept_link_redirects;
+  }
+}
+
 class Interpreter {
-  constructor(root) {
+  constructor(root, config) {
+    this.config = config;
     this.root = root;
     this.listeners = new ListenerMap(root);
     this.nodes = [root];
@@ -143,8 +151,7 @@ class Interpreter {
   SetAttribute(id, field, value, ns) {
     if (value === null) {
       this.RemoveAttribute(id, field, ns);
-    }
-    else {
+    } else {
       const node = this.nodes[id];
       this.SetAttributeInner(node, field, value, ns);
     }
@@ -342,7 +349,6 @@ class Interpreter {
         this.RemoveEventListener(edit.id, edit.name);
         break;
       case "NewEventListener":
-
         let bubbles = event_bubbles(edit.name);
 
         // this handler is only provided on desktop implementations since this
@@ -357,15 +363,21 @@ class Interpreter {
 
             if (event.type === "click") {
               // todo call prevent default if it's the right type of event
-              let a_element = target.closest("a");
-              if (a_element != null) {
-                event.preventDefault();
-                if (shouldPreventDefault !== `onclick` && a_element.getAttribute(`dioxus-prevent-default`) !== `onclick`) {
-                  const href = a_element.getAttribute("href");
-                  if (href !== "" && href !== null && href !== undefined) {
-                    window.ipc.postMessage(
-                      serializeIpcMessage("browser_open", { href })
-                    );
+              if (this.config.intercept_link_redirects) {
+                let a_element = target.closest("a");
+                if (a_element != null) {
+                  event.preventDefault();
+                  if (
+                    shouldPreventDefault !== `onclick` &&
+                    a_element.getAttribute(`dioxus-prevent-default`) !==
+                      `onclick`
+                  ) {
+                    const href = a_element.getAttribute("href");
+                    if (href !== "" && href !== null && href !== undefined) {
+                      window.ipc.postMessage(
+                        serializeIpcMessage("browser_open", { href })
+                      );
+                    }
                   }
                 }
               }

+ 1 - 1
packages/liveview/src/main.js

@@ -8,7 +8,7 @@ function main() {
 class IPC {
   constructor(root) {
     // connect to the websocket
-    window.interpreter = new Interpreter(root);
+    window.interpreter = new Interpreter(root, new InterpreterConfig(false));
 
     let ws = new WebSocket(WS_ADDR);