Skip to content

Commit 8c493fa

Browse files
committed
frontend/latex/gutter+ai_formula: fix context, use Localize context + locale, followup of #7877
1 parent 1526891 commit 8c493fa

File tree

5 files changed

+52
-15
lines changed

5 files changed

+52
-15
lines changed

src/packages/frontend/codemirror/extensions/ai-formula.tsx

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
useState,
1010
useTypedRedux,
1111
} from "@cocalc/frontend/app-framework";
12+
import { Localize, useLocalizationCtx } from "@cocalc/frontend/app/localize";
1213
import type { Message } from "@cocalc/frontend/client/types";
1314
import {
1415
HelpIcon,
@@ -27,6 +28,7 @@ import track from "@cocalc/frontend/user-tracking";
2728
import { webapp_client } from "@cocalc/frontend/webapp-client";
2829
import { isFreeModel } from "@cocalc/util/db-schema/llm-utils";
2930
import { unreachable } from "@cocalc/util/misc";
31+
import { Locale } from "@cocalc/util/i18n";
3032

3133
type Mode = "tex" | "md";
3234

@@ -36,23 +38,34 @@ interface Opts {
3638
mode: Mode;
3739
text?: string;
3840
project_id: string;
41+
locale?: Locale;
3942
}
4043

4144
export async function ai_gen_formula({
4245
mode,
4346
text = "",
4447
project_id,
48+
locale,
4549
}: Opts): Promise<string> {
4650
return await show_react_modal((cb) => (
47-
<AiGenFormula mode={mode} text={text} project_id={project_id} cb={cb} />
51+
<Localize>
52+
<AiGenFormula
53+
mode={mode}
54+
text={text}
55+
project_id={project_id}
56+
locale={locale}
57+
cb={cb}
58+
/>
59+
</Localize>
4860
));
4961
}
5062

5163
interface Props extends Opts {
5264
cb: (err?: string, result?: string) => void;
5365
}
5466

55-
function AiGenFormula({ mode, text = "", project_id, cb }: Props) {
67+
function AiGenFormula({ mode, text = "", project_id, locale, cb }: Props) {
68+
const { setLocale } = useLocalizationCtx();
5669
const is_cocalc_com = useTypedRedux("customize", "is_cocalc_com");
5770
const [model, setModel] = useLanguageModelSetting(project_id);
5871
const [input, setInput] = useState<string>(text);
@@ -62,6 +75,12 @@ function AiGenFormula({ mode, text = "", project_id, cb }: Props) {
6275
const [error, setError] = useState<string | undefined>(undefined);
6376
const [tokens, setTokens] = useState<number>(0);
6477

78+
useEffect(() => {
79+
if (typeof locale === "string") {
80+
setLocale(locale);
81+
}
82+
}, [locale]);
83+
6584
useAsyncEffect(
6685
debounce(
6786
async () => {

src/packages/frontend/codemirror/extensions/edit-selection.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@
55

66
import * as CodeMirror from "codemirror";
77

8+
import { redux } from "@cocalc/frontend/app-framework";
89
import {
910
commands as EDIT_COMMANDS,
1011
FONT_FACES,
1112
} from "@cocalc/frontend/editors/editor-button-bar";
13+
import { getLocale } from "@cocalc/frontend/i18n";
1214
import { markdown_to_html } from "@cocalc/frontend/markdown";
1315
import { open_new_tab, sagews_canonical_mode } from "@cocalc/frontend/misc";
1416
import { defaults, required, startswith } from "@cocalc/util/misc";
@@ -369,7 +371,15 @@ CodeMirror.defineExtension(
369371

370372
case "ai_formula":
371373
if (project_id != null) {
372-
src = await ai_gen_formula({ mode, text: src, project_id });
374+
const account_store = redux.getStore("account");
375+
const locale = getLocale(account_store.get("other_settings"));
376+
377+
src = await ai_gen_formula({
378+
mode,
379+
text: src,
380+
project_id,
381+
locale,
382+
});
373383
}
374384
done = true;
375385
break;

src/packages/frontend/editors/slate/format/commands.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,13 @@
33
* License: MS-RSL – see LICENSE.md for details
44
*/
55

6+
import { delay } from "awaiting";
67
import { isEqual } from "lodash";
8+
9+
import { redux } from "@cocalc/frontend/app-framework";
10+
import { commands } from "@cocalc/frontend/editors/editor-button-bar";
11+
import { getLocale } from "@cocalc/frontend/i18n";
12+
import { is_array, startswith } from "@cocalc/util/misc";
713
import {
814
BaseRange,
915
Editor,
@@ -15,8 +21,6 @@ import {
1521
Text,
1622
Transforms,
1723
} from "slate";
18-
import { commands } from "@cocalc/frontend/editors/editor-button-bar";
19-
import { is_array, startswith } from "@cocalc/util/misc";
2024
import { getMarks } from "../edit-bar/marks";
2125
import { SlateEditor } from "../editable-markdown";
2226
import { markdown_to_slate } from "../markdown-to-slate";
@@ -27,7 +31,6 @@ import { insertAIFormula } from "./insert-ai-formula";
2731
import { insertImage } from "./insert-image";
2832
import { insertLink } from "./insert-link";
2933
import { insertSpecialChar } from "./insert-special-char";
30-
import { delay } from "awaiting";
3134

3235
// currentWord:
3336
//
@@ -424,7 +427,9 @@ export async function formatAction(
424427

425428
if (cmd === "ai_formula") {
426429
if (project_id == null) throw new Error("ai_formula requires project_id");
427-
const formula = await insertAIFormula(project_id);
430+
const account_store = redux.getStore("account")
431+
const locale = getLocale(account_store.get("other_settings"))
432+
const formula = await insertAIFormula(project_id, locale);
428433
const value = removeDollars(removeBlankLines(formula.trim()));
429434
const node: Node = {
430435
type: "math_inline",

src/packages/frontend/editors/slate/format/insert-ai-formula.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,14 @@
55

66
import { alert_message } from "@cocalc/frontend/alerts";
77
import { ai_gen_formula } from "@cocalc/frontend/codemirror/extensions/ai-formula";
8+
import { Locale } from "@cocalc/util/i18n";
89

9-
export async function insertAIFormula(project_id: string): Promise<string> {
10+
export async function insertAIFormula(
11+
project_id: string,
12+
locale: Locale,
13+
): Promise<string> {
1014
try {
11-
return await ai_gen_formula({ mode: "md", project_id });
15+
return await ai_gen_formula({ mode: "md", project_id, locale });
1216
} catch (err) {
1317
alert_message({ type: "error", message: err.errorFields[0]?.errors });
1418
return "";

src/packages/frontend/frame-editors/latex-editor/gutters.tsx

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,15 @@
99
// one gets a gutter mark, with pref to errors. The main error log shows everything, so this should be OK.
1010

1111
import { Popover } from "antd";
12-
import { IntlProvider } from "react-intl";
1312

1413
import { Icon } from "@cocalc/frontend/components";
1514
//import { Actions } from "@cocalc/frontend/frame-editors/code-editor/actions";
15+
import { Localize } from "@cocalc/frontend/app/localize";
1616
import HelpMeFix from "@cocalc/frontend/frame-editors/llm/help-me-fix";
1717
import { capitalize } from "@cocalc/util/misc";
1818
import { Actions } from "./actions";
1919
import { SPEC, SpecItem } from "./errors-and-warnings";
2020
import { Error, IProcessedLatexLog } from "./latex-log-parser";
21-
import { DEFAULT_LOCALE } from "@cocalc/frontend/i18n";
2221

2322
export function update_gutters(opts: {
2423
log: IProcessedLatexLog;
@@ -65,10 +64,10 @@ function component(
6564
}
6665
// NOTE/BUG: despite allow_touch true below, this still does NOT work on my iPad -- we see the icon, but nothing
6766
// happens when clicking on it; this may be a codemirror issue.
68-
// NOTE: the IntlProvider is necessary, because this is mounted outside the application's overall context.
69-
// It's just a default IntlProvider to avoid a crash → TODO: make this part of the application react root.
67+
// NOTE: the IntlProvider (in Localize) is necessary, because this is mounted outside the application's overall context.
68+
// TODO: maybe make this part of the application react root.
7069
return (
71-
<IntlProvider locale="en" defaultLocale={DEFAULT_LOCALE}>
70+
<Localize>
7271
<Popover
7372
title={message}
7473
content={
@@ -108,6 +107,6 @@ function component(
108107
style={{ color: spec.color, cursor: "pointer" }}
109108
/>
110109
</Popover>
111-
</IntlProvider>
110+
</Localize>
112111
);
113112
}

0 commit comments

Comments
 (0)