fix liveview links

This commit is contained in:
Evan Almloff 2023-04-23 12:07:38 -05:00
parent 73a2beb327
commit 28ef3f8968
4 changed files with 37 additions and 21 deletions

View file

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

View file

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

View file

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

View file

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