diff --git a/frontend/src/io-managers/localization.ts b/frontend/src/io-managers/localization.ts index fb841c2a9..5f5eefefd 100644 --- a/frontend/src/io-managers/localization.ts +++ b/frontend/src/io-managers/localization.ts @@ -2,24 +2,24 @@ import { type Editor } from "@graphite/wasm-communication/editor"; import { TriggerAboutGraphiteLocalizedCommitDate } from "@graphite/wasm-communication/messages"; export function createLocalizationManager(editor: Editor) { - function localizeTimestamp(utc: string): { timestamp: string; year: string } { - // Timestamp - const date = new Date(utc); - if (Number.isNaN(date.getTime())) return { timestamp: utc, year: `${new Date().getFullYear()}` }; - - const timezoneName = Intl.DateTimeFormat(undefined, { timeZoneName: "long" }) - .formatToParts(new Date()) - .find((part) => part.type === "timeZoneName"); - - const dateString = `${date.getFullYear()}-${String(date.getMonth() + 1).padStart(2, "0")}-${String(date.getDate()).padStart(2, "0")}`; - const timeString = `${String(date.getHours()).padStart(2, "0")}:${String(date.getMinutes()).padStart(2, "0")}`; - const timezoneNameString = timezoneName?.value; - return { timestamp: `${dateString} ${timeString} ${timezoneNameString}`, year: String(date.getFullYear()) }; - } - // Subscribe to process backend event editor.subscriptions.subscribeJsMessage(TriggerAboutGraphiteLocalizedCommitDate, (triggerAboutGraphiteLocalizedCommitDate) => { const localized = localizeTimestamp(triggerAboutGraphiteLocalizedCommitDate.commitDate); editor.handle.requestAboutGraphiteDialogWithLocalizedCommitDate(localized.timestamp, localized.year); }); } + +function localizeTimestamp(utc: string): { timestamp: string; year: string } { + // Timestamp + const date = new Date(utc); + if (Number.isNaN(date.getTime())) return { timestamp: utc, year: `${new Date().getFullYear()}` }; + + const timezoneName = Intl.DateTimeFormat(undefined, { timeZoneName: "long" }) + .formatToParts(new Date()) + .find((part) => part.type === "timeZoneName"); + + const dateString = `${date.getFullYear()}-${String(date.getMonth() + 1).padStart(2, "0")}-${String(date.getDate()).padStart(2, "0")}`; + const timeString = `${String(date.getHours()).padStart(2, "0")}:${String(date.getMinutes()).padStart(2, "0")}`; + const timezoneNameString = timezoneName?.value; + return { timestamp: `${dateString} ${timeString} ${timezoneNameString}`, year: String(date.getFullYear()) }; +} diff --git a/frontend/wasm/src/editor_api.rs b/frontend/wasm/src/editor_api.rs index 53d1b495c..a104ad756 100644 --- a/frontend/wasm/src/editor_api.rs +++ b/frontend/wasm/src/editor_api.rs @@ -26,6 +26,14 @@ use std::sync::atomic::Ordering; use std::time::Duration; use wasm_bindgen::prelude::*; +/// We directly interface with the updateImage JS function for massively increased performance over serializing and deserializing. +/// This avoids creating a json with a list millions of numbers long. +// #[wasm_bindgen(module = "/../src/wasm-communication/editor.ts")] +// extern "C" { +// // fn dispatchTauri(message: String) -> String; +// fn dispatchTauri(message: String); +// } + /// Set the random seed used by the editor by calling this from JS upon initialization. /// This is necessary because WASM doesn't have a random number generator. #[wasm_bindgen(js_name = setRandomSeed)] @@ -33,15 +41,7 @@ pub fn set_random_seed(seed: u64) { editor::application::set_uuid_seed(seed); } -/// We directly interface with the updateImage JS function for massively increased performance over serializing and deserializing. -/// This avoids creating a json with a list millions of numbers long. -#[wasm_bindgen(module = "/../src/wasm-communication/editor.ts")] -extern "C" { - //fn dispatchTauri(message: String) -> String; - fn dispatchTauri(message: String); -} - -/// Provides a handle to access the raw WASM memory +/// Provides a handle to access the raw WASM memory. #[wasm_bindgen(js_name = wasmMemory)] pub fn wasm_memory() -> JsValue { wasm_bindgen::memory() @@ -88,15 +88,12 @@ impl EditorHandle { } // Get the editor, dispatch the message, and store the `FrontendMessage` queue response - editor(|editor| { - // Get the editor, then dispatch the message to the backend, and return its response `FrontendMessage` queue - let frontend_messages = editor.handle_message(message.into()); + let frontend_messages = editor(|editor| editor.handle_message(message.into())); - // Send each `FrontendMessage` to the JavaScript frontend - for message in frontend_messages.into_iter() { - self.send_frontend_message_to_js(message); - } - }); + // Send each `FrontendMessage` to the JavaScript frontend + for message in frontend_messages.into_iter() { + self.send_frontend_message_to_js(message); + } } // Sends a FrontendMessage to JavaScript @@ -182,20 +179,20 @@ impl EditorHandle { } } - #[wasm_bindgen(js_name = tauriResponse)] - pub fn tauri_response(&self, _message: JsValue) { - #[cfg(feature = "tauri")] - match ron::from_str::>(&_message.as_string().unwrap()) { - Ok(response) => { - for message in response { - self.send_frontend_message_to_js(message); - } - } - Err(error) => { - log::error!("tauri response: {error:?}\n{_message:?}"); - } - } - } + // #[wasm_bindgen(js_name = tauriResponse)] + // pub fn tauri_response(&self, _message: JsValue) { + // #[cfg(feature = "tauri")] + // match ron::from_str::>(&_message.as_string().unwrap()) { + // Ok(response) => { + // for message in response { + // self.send_frontend_message_to_js(message); + // } + // } + // Err(error) => { + // log::error!("tauri response: {error:?}\n{_message:?}"); + // } + // } + // } /// Displays a dialog with an error message #[wasm_bindgen(js_name = errorDialog)]