Fix breakage of About Graphite dialog from editor instances refactor

This commit is contained in:
Keavon Chambers 2024-05-07 00:22:25 -07:00
parent 7845302c50
commit 1ce3d59e0f
2 changed files with 43 additions and 46 deletions

View File

@ -2,24 +2,24 @@ import { type Editor } from "@graphite/wasm-communication/editor";
import { TriggerAboutGraphiteLocalizedCommitDate } from "@graphite/wasm-communication/messages"; import { TriggerAboutGraphiteLocalizedCommitDate } from "@graphite/wasm-communication/messages";
export function createLocalizationManager(editor: Editor) { 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 // Subscribe to process backend event
editor.subscriptions.subscribeJsMessage(TriggerAboutGraphiteLocalizedCommitDate, (triggerAboutGraphiteLocalizedCommitDate) => { editor.subscriptions.subscribeJsMessage(TriggerAboutGraphiteLocalizedCommitDate, (triggerAboutGraphiteLocalizedCommitDate) => {
const localized = localizeTimestamp(triggerAboutGraphiteLocalizedCommitDate.commitDate); const localized = localizeTimestamp(triggerAboutGraphiteLocalizedCommitDate.commitDate);
editor.handle.requestAboutGraphiteDialogWithLocalizedCommitDate(localized.timestamp, localized.year); 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()) };
}

View File

@ -26,6 +26,14 @@ use std::sync::atomic::Ordering;
use std::time::Duration; use std::time::Duration;
use wasm_bindgen::prelude::*; 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. /// 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. /// This is necessary because WASM doesn't have a random number generator.
#[wasm_bindgen(js_name = setRandomSeed)] #[wasm_bindgen(js_name = setRandomSeed)]
@ -33,15 +41,7 @@ pub fn set_random_seed(seed: u64) {
editor::application::set_uuid_seed(seed); editor::application::set_uuid_seed(seed);
} }
/// We directly interface with the updateImage JS function for massively increased performance over serializing and deserializing. /// Provides a handle to access the raw WASM memory.
/// 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
#[wasm_bindgen(js_name = wasmMemory)] #[wasm_bindgen(js_name = wasmMemory)]
pub fn wasm_memory() -> JsValue { pub fn wasm_memory() -> JsValue {
wasm_bindgen::memory() wasm_bindgen::memory()
@ -88,15 +88,12 @@ impl EditorHandle {
} }
// Get the editor, dispatch the message, and store the `FrontendMessage` queue response // Get the editor, dispatch the message, and store the `FrontendMessage` queue response
editor(|editor| { let frontend_messages = editor(|editor| editor.handle_message(message.into()));
// Get the editor, then dispatch the message to the backend, and return its response `FrontendMessage` queue
let frontend_messages = editor.handle_message(message.into());
// Send each `FrontendMessage` to the JavaScript frontend // Send each `FrontendMessage` to the JavaScript frontend
for message in frontend_messages.into_iter() { for message in frontend_messages.into_iter() {
self.send_frontend_message_to_js(message); self.send_frontend_message_to_js(message);
} }
});
} }
// Sends a FrontendMessage to JavaScript // Sends a FrontendMessage to JavaScript
@ -182,20 +179,20 @@ impl EditorHandle {
} }
} }
#[wasm_bindgen(js_name = tauriResponse)] // #[wasm_bindgen(js_name = tauriResponse)]
pub fn tauri_response(&self, _message: JsValue) { // pub fn tauri_response(&self, _message: JsValue) {
#[cfg(feature = "tauri")] // #[cfg(feature = "tauri")]
match ron::from_str::<Vec<FrontendMessage>>(&_message.as_string().unwrap()) { // match ron::from_str::<Vec<FrontendMessage>>(&_message.as_string().unwrap()) {
Ok(response) => { // Ok(response) => {
for message in response { // for message in response {
self.send_frontend_message_to_js(message); // self.send_frontend_message_to_js(message);
} // }
} // }
Err(error) => { // Err(error) => {
log::error!("tauri response: {error:?}\n{_message:?}"); // log::error!("tauri response: {error:?}\n{_message:?}");
} // }
} // }
} // }
/// Displays a dialog with an error message /// Displays a dialog with an error message
#[wasm_bindgen(js_name = errorDialog)] #[wasm_bindgen(js_name = errorDialog)]