Add option in math dropdown to toggle MathJax rendering (#2014)

* Add option in math dropdown to toggle MathJax rendering

Closes #1942

* Hackily redraw the page when toggling MathJax

* Add Fluent string
This commit is contained in:
Damien Elmes 2022-08-18 12:06:06 +10:00 committed by GitHub
parent 678c354fed
commit 75723d7c9c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 40 additions and 4 deletions

View file

@ -58,6 +58,7 @@ editing-toggle-visual-editor = Toggle Visual Editor
editing-underline-text = Underline text editing-underline-text = Underline text
editing-unordered-list = Unordered list editing-unordered-list = Unordered list
editing-warning-cloze-deletions-will-not-work = Warning, cloze deletions will not work until you switch the type at the top to Cloze. editing-warning-cloze-deletions-will-not-work = Warning, cloze deletions will not work until you switch the type at the top to Cloze.
editing-toggle-mathjax-rendering = Toggle MathJax Rendering
## You don't need to translate these strings, as they will be replaced with different ones soon. ## You don't need to translate these strings, as they will be replaced with different ones soon.

View file

@ -125,6 +125,7 @@ class Editor:
self.card: Card | None = None self.card: Card | None = None
self._init_links() self._init_links()
self.setupOuter() self.setupOuter()
self.add_webview()
self.setupWeb() self.setupWeb()
self.setupShortcuts() self.setupShortcuts()
gui_hooks.editor_did_init(self) gui_hooks.editor_did_init(self)
@ -139,11 +140,12 @@ class Editor:
self.widget.setLayout(l) self.widget.setLayout(l)
self.outerLayout = l self.outerLayout = l
def setupWeb(self) -> None: def add_webview(self) -> None:
self.web = EditorWebView(self.widget, self) self.web = EditorWebView(self.widget, self)
self.web.set_bridge_command(self.onBridgeCmd, self) self.web.set_bridge_command(self.onBridgeCmd, self)
self.outerLayout.addWidget(self.web, 1) self.outerLayout.addWidget(self.web, 1)
def setupWeb(self) -> None:
if self.editorMode == EditorMode.ADD_CARDS: if self.editorMode == EditorMode.ADD_CARDS:
file = "note_creator" file = "note_creator"
elif self.editorMode == EditorMode.BROWSER: elif self.editorMode == EditorMode.BROWSER:
@ -519,7 +521,7 @@ require("anki/ui").loaded.then(() => require("anki/NoteEditor").instances[0].too
text_color = self.mw.pm.profile.get("lastTextColor", "#00f") text_color = self.mw.pm.profile.get("lastTextColor", "#00f")
highlight_color = self.mw.pm.profile.get("lastHighlightColor", "#00f") highlight_color = self.mw.pm.profile.get("lastHighlightColor", "#00f")
js = "setFields({}); setDescriptions({}); setFonts({}); focusField({}); setNoteId({}); setColorButtons({}); setTags({}); ".format( js = "setFields({}); setDescriptions({}); setFonts({}); focusField({}); setNoteId({}); setColorButtons({}); setTags({}); setMathjaxEnabled({});".format(
json.dumps(data), json.dumps(data),
json.dumps(descriptions), json.dumps(descriptions),
json.dumps(self.fonts()), json.dumps(self.fonts()),
@ -527,6 +529,7 @@ require("anki/ui").loaded.then(() => require("anki/NoteEditor").instances[0].too
json.dumps(self.note.id), json.dumps(self.note.id),
json.dumps([text_color, highlight_color]), json.dumps([text_color, highlight_color]),
json.dumps(self.note.tags), json.dumps(self.note.tags),
json.dumps(self.mw.col.get_config("renderMathjax", True)),
) )
if self.addMode: if self.addMode:
@ -1130,6 +1133,14 @@ require("anki/ui").loaded.then(() => require("anki/NoteEditor").instances[0].too
def insertMathjaxChemistry(self) -> None: def insertMathjaxChemistry(self) -> None:
self.web.eval("wrap('\\\\(\\\\ce{', '}\\\\)');") self.web.eval("wrap('\\\\(\\\\ce{', '}\\\\)');")
def toggleMathjax(self) -> None:
self.mw.col.set_config(
"renderMathjax", not self.mw.col.get_config("renderMathjax", False)
)
# hackily redraw the page
self.setupWeb()
self.loadNoteKeepingFocus()
# Links from HTML # Links from HTML
###################################################################### ######################################################################
@ -1156,6 +1167,7 @@ require("anki/ui").loaded.then(() => require("anki/NoteEditor").instances[0].too
mathjaxInline=Editor.insertMathjaxInline, mathjaxInline=Editor.insertMathjaxInline,
mathjaxBlock=Editor.insertMathjaxBlock, mathjaxBlock=Editor.insertMathjaxBlock,
mathjaxChemistry=Editor.insertMathjaxChemistry, mathjaxChemistry=Editor.insertMathjaxChemistry,
toggleMathjax=Editor.toggleMathjax,
) )

View file

@ -20,6 +20,10 @@ function trimBreaks(text: string): string {
.replace(/\n*$/, ""); .replace(/\n*$/, "");
} }
export const mathjaxConfig = {
enabled: true,
};
export const Mathjax: DecoratedElementConstructor = class Mathjax export const Mathjax: DecoratedElementConstructor = class Mathjax
extends HTMLElement extends HTMLElement
implements DecoratedElement implements DecoratedElement
@ -41,6 +45,9 @@ export const Mathjax: DecoratedElementConstructor = class Mathjax
} }
static toUndecorated(stored: string): string { static toUndecorated(stored: string): string {
if (!mathjaxConfig.enabled) {
return stored;
}
return stored return stored
.replace(mathjaxBlockDelimiterPattern, (_match: string, text: string) => { .replace(mathjaxBlockDelimiterPattern, (_match: string, text: string) => {
const trimmed = trimBreaks(text); const trimmed = trimBreaks(text);

View file

@ -109,6 +109,10 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
fieldNames = newFieldNames; fieldNames = newFieldNames;
} }
function setMathjaxEnabled(enabled: boolean): void {
mathjaxConfig.enabled = enabled;
}
let fieldDescriptions: string[] = []; let fieldDescriptions: string[] = [];
export function setDescriptions(fs: string[]): void { export function setDescriptions(fs: string[]): void {
fieldDescriptions = fs; fieldDescriptions = fs;
@ -223,6 +227,7 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
const toolbar: Partial<EditorToolbarAPI> = {}; const toolbar: Partial<EditorToolbarAPI> = {};
import { mathjaxConfig } from "../editable/mathjax-element";
import { wrapInternal } from "../lib/wrap"; import { wrapInternal } from "../lib/wrap";
import * as oldEditorAdapter from "./old-editor-adapter"; import * as oldEditorAdapter from "./old-editor-adapter";
@ -250,6 +255,7 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
getNoteId, getNoteId,
setNoteId, setNoteId,
wrap, wrap,
setMathjaxEnabled,
...oldEditorAdapter, ...oldEditorAdapter,
}); });

View file

@ -10,6 +10,8 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
import Popover from "../../components/Popover.svelte"; import Popover from "../../components/Popover.svelte";
import Shortcut from "../../components/Shortcut.svelte"; import Shortcut from "../../components/Shortcut.svelte";
import WithFloating from "../../components/WithFloating.svelte"; import WithFloating from "../../components/WithFloating.svelte";
import { mathjaxConfig } from "../../editable/mathjax-element";
import { bridgeCommand } from "../../lib/bridgecommand";
import * as tr from "../../lib/ftl"; import * as tr from "../../lib/ftl";
import { getPlatformString } from "../../lib/shortcuts"; import { getPlatformString } from "../../lib/shortcuts";
import { wrapInternal } from "../../lib/wrap"; import { wrapInternal } from "../../lib/wrap";
@ -50,6 +52,11 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
surround("[$$]", "[/$$]"); surround("[$$]", "[/$$]");
} }
function toggleShowMathjax(): void {
mathjaxConfig.enabled = !mathjaxConfig.enabled;
bridgeCommand("toggleMathjax");
}
type LatexItem = [() => void, string, string]; type LatexItem = [() => void, string, string];
const dropdownItems: LatexItem[] = [ const dropdownItems: LatexItem[] = [
@ -93,6 +100,9 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
</DropdownItem> </DropdownItem>
<Shortcut {keyCombination} on:action={callback} /> <Shortcut {keyCombination} on:action={callback} />
{/each} {/each}
<DropdownItem on:click={toggleShowMathjax}>
<span>{tr.editingToggleMathjaxRendering()}</span>
</DropdownItem>
</Popover> </Popover>
</WithFloating> </WithFloating>

View file

@ -5,7 +5,7 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
<script lang="ts"> <script lang="ts">
import { onMount } from "svelte"; import { onMount } from "svelte";
import Checkbox from "../../components/CheckBox.svelte"; import CheckBox from "../../components/CheckBox.svelte";
import DropdownItem from "../../components/DropdownItem.svelte"; import DropdownItem from "../../components/DropdownItem.svelte";
import DropdownMenu from "../../components/DropdownMenu.svelte"; import DropdownMenu from "../../components/DropdownMenu.svelte";
import { withButton } from "../../components/helpers"; import { withButton } from "../../components/helpers";
@ -128,7 +128,7 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
<DropdownMenu on:mousedown={(event) => event.preventDefault()}> <DropdownMenu on:mousedown={(event) => event.preventDefault()}>
{#each showFormats as format (format.name)} {#each showFormats as format (format.name)}
<DropdownItem on:click={(event) => onItemClick(event, format)}> <DropdownItem on:click={(event) => onItemClick(event, format)}>
<Checkbox bind:value={format.active} /> <CheckBox bind:value={format.active} />
<span class="d-flex-inline ps-3">{format.name}</span> <span class="d-flex-inline ps-3">{format.name}</span>
</DropdownItem> </DropdownItem>
{/each} {/each}