modify helper files to support launcher

This commit is contained in:
llama 2025-10-14 09:20:23 +08:00
parent 8f6b1497e0
commit 581618610e
No known key found for this signature in database
GPG key ID: 0B7543854B9413C3
2 changed files with 10 additions and 4 deletions

View file

@ -4,21 +4,25 @@
export interface PostProtoOptions { export interface PostProtoOptions {
/** True by default. Shows a dialog with the error message, then rethrows. */ /** True by default. Shows a dialog with the error message, then rethrows. */
alertOnError?: boolean; alertOnError?: boolean;
customProtocol?: boolean;
} }
const CUSTOM_PROTOCOL_URI = (navigator.platform.indexOf("Win") == -1) ? "anki://localhost" : "http://anki.localhost";
export async function postProto<T>( export async function postProto<T>(
method: string, method: string,
input: { toBinary(): Uint8Array; getType(): { typeName: string } }, input: { toBinary(): Uint8Array; getType(): { typeName: string } },
outputType: { fromBinary(arr: Uint8Array): T }, outputType: { fromBinary(arr: Uint8Array): T },
options: PostProtoOptions = {}, options: PostProtoOptions = {},
): Promise<T> { ): Promise<T> {
const { alertOnError = true, customProtocol = false } = options;
try { try {
const inputBytes = input.toBinary(); const inputBytes = input.toBinary();
const path = `/_anki/${method}`; const backendUrl = customProtocol ? CUSTOM_PROTOCOL_URI : "/_anki";
const path = `${backendUrl}/${method}`;
const outputBytes = await postProtoInner(path, inputBytes); const outputBytes = await postProtoInner(path, inputBytes);
return outputType.fromBinary(outputBytes); return outputType.fromBinary(outputBytes);
} catch (err) { } catch (err) {
const { alertOnError = true } = options;
if (alertOnError && !(err instanceof Error && err.message === "500: Interrupted")) { if (alertOnError && !(err instanceof Error && err.message === "500: Interrupted")) {
alert(err); alert(err);
} }

View file

@ -4,6 +4,7 @@
import "intl-pluralrules"; import "intl-pluralrules";
import { i18nResources } from "@generated/backend"; import { i18nResources } from "@generated/backend";
import { i18nResources as launcherI18nResources } from "@generated/backend-launcher";
import type { ModuleName } from "@generated/ftl"; import type { ModuleName } from "@generated/ftl";
import { FluentBundle, FluentResource } from "@generated/ftl"; import { FluentBundle, FluentResource } from "@generated/ftl";
import { firstLanguage, setBundles } from "@generated/ftl"; import { firstLanguage, setBundles } from "@generated/ftl";
@ -77,8 +78,9 @@ export function withoutUnicodeIsolation(s: string): string {
return s.replace(/[\u2068-\u2069]+/g, ""); return s.replace(/[\u2068-\u2069]+/g, "");
} }
export async function setupI18n(args: { modules: ModuleName[] }): Promise<void> { export async function setupI18n(args: { modules: ModuleName[] }, launcher = false): Promise<void> {
const resources = await i18nResources(args); const fn = launcher ? launcherI18nResources : i18nResources;
const resources = await fn(args);
const json = JSON.parse(new TextDecoder().decode(resources.json)); const json = JSON.parse(new TextDecoder().decode(resources.json));
const newBundles: FluentBundle[] = []; const newBundles: FluentBundle[] = [];