Merge branch 'upstream' into fix-links-liveview

This commit is contained in:
Evan Almloff committed 2023-04-26 17:56:13 -05:00
commit 7e292cc2fa
18 files changed
+628 -84

No files matched your search

+47 -2
View File
@@ -34,7 +34,51 @@ pub enum LiveViewError {
SendingFailed,
}
use dioxus_interpreter_js::INTERPRETER_JS;
use once_cell::sync::Lazy;
static INTERPRETER_JS: Lazy<String> = Lazy::new(|| {
let interpreter = dioxus_interpreter_js::INTERPRETER_JS;
let serialize_file_uploads = r#"if (
target.tagName === "INPUT" &&
(event.type === "change" || event.type === "input")
) {
const type = target.getAttribute("type");
if (type === "file") {
async function read_files() {
const files = target.files;
const file_contents = {};
for (let i = 0; i < files.length; i++) {
const file = files[i];
file_contents[file.name] = Array.from(
new Uint8Array(await file.arrayBuffer())
);
}
let file_engine = {
files: file_contents,
};
contents.files = file_engine;
if (realId === null) {
return;
}
const message = serializeIpcMessage("user_event", {
name: name,
element: parseInt(realId),
data: contents,
bubbles,
});
window.ipc.postMessage(message);
}
read_files();
return;
}
}"#;
interpreter.replace("/*POST_EVENT_SERIALIZATION*/", serialize_file_uploads)
});
static MAIN_JS: &str = include_str!("./main.js");
/// This script that gets injected into your app connects this page to the websocket endpoint
@@ -42,11 +86,12 @@ static MAIN_JS: &str = include_str!("./main.js");
/// Once the endpoint is connected, it will send the initial state of the app, and then start
/// processing user events and returning edits to the liveview instance
pub fn interpreter_glue(url: &str) -> String {
let js = &*INTERPRETER_JS;
format!(
r#"
<script>
var WS_ADDR = "{url}";
{INTERPRETER_JS}
{js}
{MAIN_JS}
main();
</script>