prevent default file input behavior on desktop

This commit is contained in:
Evan Almloff 2023-04-20 10:10:52 -05:00
parent bed1d58a77
commit d7eae79509
2 changed files with 124 additions and 102 deletions

View file

@ -9,10 +9,26 @@ use wry::{
}; };
fn module_loader(root_name: &str) -> String { fn module_loader(root_name: &str) -> String {
let js = INTERPRETER_JS.replace(
"/*POST_HANDLE_EDITS*/",
r#"// Prevent file inputs from opening the file dialog on click
let inputs = document.querySelectorAll("input");
for (let input of inputs) {
input.addEventListener("click", (event) => {
let target = event.target;
// prevent file inputs from opening the file dialog on click
const type = target.getAttribute("type");
if (type === "file") {
window.ipc.postMessage(serializeIpcMessage("file_diolog", { accept: target.getAttribute("accept"), multiple: target.hasAttribute("multiple") }));
event.preventDefault();
}
});
}"#,
);
format!( format!(
r#" r#"
<script> <script>
{INTERPRETER_JS} {js}
let rootname = "{root_name}"; let rootname = "{root_name}";
let root = window.document.getElementById(rootname); let root = window.document.getElementById(rootname);

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) {
@ -143,8 +144,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);
} }
@ -212,6 +212,8 @@ class Interpreter {
for (let edit of edits.edits) { for (let edit of edits.edits) {
this.handleEdit(edit); this.handleEdit(edit);
} }
/*POST_HANDLE_EDITS*/
} }
SaveTemplate(template) { SaveTemplate(template) {
@ -342,107 +344,111 @@ 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
// method is not used by the web implementation
let handler = (event) => {
let target = event.target;
if (target != null) {
let realId = target.getAttribute(`data-dioxus-id`);
let shouldPreventDefault = target.getAttribute(
`dioxus-prevent-default`
);
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 })
);
}
}
}
// also prevent buttons from submitting
if (target.tagName === "BUTTON" && event.type == "submit") {
event.preventDefault();
}
}
// walk the tree to find the real element
while (realId == null) {
// we've reached the root we don't want to send an event
if (target.parentElement === null) {
return;
}
target = target.parentElement;
realId = target.getAttribute(`data-dioxus-id`);
}
shouldPreventDefault = target.getAttribute(
`dioxus-prevent-default`
);
let contents = serialize_event(event);
if (shouldPreventDefault === `on${event.type}`) {
event.preventDefault();
}
if (event.type === "submit") {
event.preventDefault();
}
if (
target.tagName === "FORM" &&
(event.type === "submit" || event.type === "input")
) {
for (let x = 0; x < target.elements.length; x++) {
let element = target.elements[x];
let name = element.getAttribute("name");
if (name != null) {
if (element.getAttribute("type") === "checkbox") {
// @ts-ignore
contents.values[name] = element.checked ? "true" : "false";
} else if (element.getAttribute("type") === "radio") {
if (element.checked) {
contents.values[name] = element.value;
}
} else {
// @ts-ignore
contents.values[name] =
element.value ?? element.textContent;
}
}
}
}
if (realId === null) {
return;
}
window.ipc.postMessage(
serializeIpcMessage("user_event", {
name: edit.name,
element: parseInt(realId),
data: contents,
bubbles,
})
);
}
};
this.NewEventListener(edit.name, edit.id, bubbles, handler); this.NewEventListener(edit.name, edit.id, bubbles, handler);
break; break;
} }
} }
} }
// this handler is only provided on desktop implementations since this
// method is not used by the web implementation
function handler(event) {
console.log(event);
let target = event.target;
if (target != null) {
let shouldPreventDefault = target.getAttribute(`dioxus-prevent-default`);
if (event.type === "click") {
// Prevent redirects from links
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 })
);
}
}
}
// also prevent buttons from submitting
if (target.tagName === "BUTTON" && event.type == "submit") {
event.preventDefault();
}
}
const realId = find_real_id(target);
shouldPreventDefault = target.getAttribute(`dioxus-prevent-default`);
let contents = serialize_event(event);
if (shouldPreventDefault === `on${event.type}`) {
event.preventDefault();
}
if (event.type === "submit") {
event.preventDefault();
}
if (
target.tagName === "FORM" &&
(event.type === "submit" || event.type === "input")
) {
for (let x = 0; x < target.elements.length; x++) {
let element = target.elements[x];
let name = element.getAttribute("name");
if (name != null) {
if (element.getAttribute("type") === "checkbox") {
// @ts-ignore
contents.values[name] = element.checked ? "true" : "false";
} else if (element.getAttribute("type") === "radio") {
if (element.checked) {
contents.values[name] = element.value;
}
} else {
// @ts-ignore
contents.values[name] = element.value ?? element.textContent;
}
}
}
}
if (realId === null) {
return;
}
window.ipc.postMessage(
serializeIpcMessage("user_event", {
name: edit.name,
element: parseInt(realId),
data: contents,
bubbles,
})
);
}
}
function find_real_id(target) {
let realId = target.getAttribute(`data-dioxus-id`);
// walk the tree to find the real element
while (realId == null) {
// we've reached the root we don't want to send an event
if (target.parentElement === null) {
return;
}
target = target.parentElement;
realId = target.getAttribute(`data-dioxus-id`);
}
return realId;
}
function get_mouse_data(event) { function get_mouse_data(event) {
const { const {
altKey, altKey,