diff --git a/src-tauri/yaak-models/guest-js/store.ts b/src-tauri/yaak-models/guest-js/store.ts index b1d30fd6..5514683e 100644 --- a/src-tauri/yaak-models/guest-js/store.ts +++ b/src-tauri/yaak-models/guest-js/store.ts @@ -53,9 +53,10 @@ let _activeWorkspaceId: string | null = null; export async function changeModelStoreWorkspace(workspaceId: string | null) { console.log('Syncing models with new workspace', workspaceId); - const workspaceModels = await invoke('plugin:yaak-models|workspace_models', { + const workspaceModelsStr = await invoke('plugin:yaak-models|workspace_models', { workspaceId, // NOTE: if no workspace id provided, it will just fetch global models }); + const workspaceModels = JSON.parse(workspaceModelsStr) as AnyModel[]; const data = newStoreData(); for (const model of workspaceModels) { data[model.model][model.id] = model; diff --git a/src-tauri/yaak-models/src/commands.rs b/src-tauri/yaak-models/src/commands.rs index 3e75d0de..ff5124d4 100644 --- a/src-tauri/yaak-models/src/commands.rs +++ b/src-tauri/yaak-models/src/commands.rs @@ -94,7 +94,7 @@ pub(crate) fn get_settings(app_handle: AppHandle) -> Result( window: WebviewWindow, workspace_id: Option<&str>, -) -> Result> { +) -> Result { let db = window.db(); let mut l: Vec = Vec::new(); @@ -120,5 +120,36 @@ pub(crate) fn workspace_models( l.append(&mut db.list_workspace_metas(wid)?.into_iter().map(Into::into).collect()); } - Ok(l) + let j = serde_json::to_string(&l)?; + + // NOTE: There's something weird that happens on Linux. If we send Cyrillic (or maybe other) + // unicode characters in this response (doesn't matter where) then the following bug happens: + // https://feedback.yaak.app/p/editing-the-url-sometimes-freezes-the-app + // + // It's as if every string resulting from the JSON.parse of the models gets encoded slightly + // wrong or something, causing the above bug where Codemirror can't calculate the cursor + // position anymore (even when none of the characters are included directly in the input). + // + // For some reason using escape sequences works, but it's a hacky fix. Hopefully the Linux + // webview dependency updates to a version where this bug doesn't exist, or we can use CEF + // (Chromium) for Linux in the future, which Tauri is working on. + Ok(escape_str_for_webview(&j)) } + +fn escape_str_for_webview(input: &str) -> String { + input.chars().map(|c| { + let code = c as u32; + // ASCII + if code <= 0x7F { + c.to_string() + // BMP characters encoded normally + } else if code < 0xFFFF { + format!("\\u{:04X}", code) + // Beyond BMP encoded a surrogate pairs + } else { + let high = ((code - 0x10000) >> 10) + 0xD800; + let low = ((code - 0x10000) & 0x3FF) + 0xDC00; + format!("\\u{:04X}\\u{:04X}", high, low) + } + }).collect() +} \ No newline at end of file