Synchronous prevent default (#2792)

* add prevent default methods to the event

* sync prevent default almost working

* sync prevent default working

* Move event handling into the runtime

* update core tests

* restore desktop file dialog

* implement prevent default on web

* add a hint about the new prevent default method

* fix web prevent default

* Fix CTRL+click on links

* fix values memorize in place test

* Fix a few more tests

* Add a playwright test for sync prevent default

* Fix core doc tests

* create a deprecated VirtualDom::handle_event

* fix macos imports in desktop

* Fix onmounted event

* Fix liveview support

* switch to RefCell for metadata

* Remove println

* remove prevent default attribute

* remove web specific link behavior

* Fix liveview links

* more liveview fixes for link

* Fix merge conflicts

* Fix clippy

* use the new prevent default in the file upload example
This commit is contained in:
Evan Almloff authored and GitHub committed 2024-08-13 11:57:54 -07:00
1 parent 8b62b71e2d
commit cab573eefd
54 files changed
+940 -729

No files matched your search

@@ -45,7 +45,7 @@ impl LiveviewRouter for Router {
) -> Self {
let view = crate::LiveViewPool::new();
let ws_path = format!("{}/ws", route);
let ws_path = format!("{}/ws", route.trim_start_matches('/'));
let title = crate::app_title();
let index_page_with_glue = move |glue: &str| {
@@ -53,8 +53,8 @@ impl LiveviewRouter for Router {
r#"
<!DOCTYPE html>
<html>
<head> <title>{title}</title> </head>
<body> <div id="main"></div> </body>
<head><title>{title}</title></head>
<body><div id="main"></div></body>
{glue}
</html>
"#,
+8 -2
View File
@@ -1,4 +1,4 @@
use dioxus_cli_config::CURRENT_CONFIG;
use dioxus_cli_config::{RuntimeCLIArguments, CURRENT_CONFIG};
use dioxus_core::VirtualDom;
use crate::LiveviewRouter;
@@ -19,9 +19,15 @@ pub struct Config<R: LiveviewRouter> {
impl<R: LiveviewRouter> Default for Config<R> {
fn default() -> Self {
let address = RuntimeCLIArguments::from_cli()
.map(|args| args.fullstack_address().address())
.unwrap_or(std::net::SocketAddr::V4(std::net::SocketAddrV4::new(
std::net::Ipv4Addr::new(127, 0, 0, 1),
8080,
)));
Self {
router: R::create_default_liveview_router(),
address: ([127, 0, 0, 1], 8080).into(),
address,
route: "/".to_string(),
}
}
+1 -1
View File
@@ -6,7 +6,7 @@ pub type Config = crate::Config<axum::Router>;
/// Launches the WebView and runs the event loop, with configuration and root props.
pub fn launch(
root: fn() -> Element,
contexts: Vec<Box<dyn Fn() -> Box<dyn Any> + Send + Sync>>,
contexts: Vec<Box<dyn Fn() -> Box<dyn Any + Send + Sync> + Send + Sync>>,
platform_config: Config,
) -> ! {
#[cfg(feature = "multi-thread")]
+1 -1
View File
@@ -60,7 +60,7 @@ fn handle_edits_code() -> String {
if (realId === null) {
return;
}
const message = window.interpreter.serializeIpcMessage("user_event", {
const message = window.interpreter.sendSerializedEvent({
name: name,
element: parseInt(realId),
data: contents,
+3 -3
View File
@@ -11,6 +11,7 @@ class IPC {
constructor(root) {
window.interpreter = new NativeInterpreter();
window.interpreter.initialize(root);
window.interpreter.liveview = true;
window.interpreter.ipc = this;
const ws = new WebSocket(WS_ADDR);
ws.binaryType = "arraybuffer";
@@ -32,13 +33,12 @@ class IPC {
ws.onmessage = (message) => {
const u8view = new Uint8Array(message.data);
const binaryFrame = u8view[0] == 1;
const messageData = message.data.slice(1)
const messageData = message.data.slice(1);
// The first byte tells the shim if this is a binary of text frame
if (binaryFrame) {
// binary frame
window.interpreter.run_from_bytes(messageData);
}
else {
} else {
// text frame
let decoder = new TextDecoder("utf-8");
+13 -12
View File
@@ -11,7 +11,7 @@ use dioxus_html::{EventData, HtmlEvent, PlatformEventData};
use dioxus_interpreter_js::MutationState;
use futures_util::{pin_mut, SinkExt, StreamExt};
use serde::Serialize;
use std::{rc::Rc, time::Duration};
use std::{any::Any, rc::Rc, time::Duration};
use tokio_util::task::LocalPoolHandle;
#[derive(Clone)]
@@ -177,22 +177,23 @@ pub async fn run(mut vdom: VirtualDom, ws: impl LiveViewSocket) -> Result<(), Li
match message {
IpcMessage::Event(evt) => {
// Intercept the mounted event and insert a custom element type
if let EventData::Mounted = &evt.data {
let event = if let EventData::Mounted = &evt.data {
let element = LiveviewElement::new(evt.element, query_engine.clone());
vdom.handle_event(
&evt.name,
Rc::new(PlatformEventData::new(Box::new(element))),
evt.element,
Event::new(
Rc::new(PlatformEventData::new(Box::new(element))) as Rc<dyn Any>,
evt.bubbles,
);
)
} else {
vdom.handle_event(
&evt.name,
Event::new(
evt.data.into_any(),
evt.element,
evt.bubbles,
);
}
)
};
vdom.runtime().handle_event(
&evt.name,
event,
evt.element,
);
}
IpcMessage::Query(result) => {
query_engine.send(result);