Reimplement editor_will_munge_html callbacks

This commit is contained in:
Abdo 2025-05-26 02:50:31 +03:00
parent 8a9baca554
commit 35f0bc5af6
3 changed files with 20 additions and 28 deletions

View file

@ -499,9 +499,6 @@ require("anki/ui").loaded.then(() => require("anki/NoteEditor").instances[0].too
else:
print("uncaught cmd", cmd)
def mungeHTML(self, txt: str) -> str:
return gui_hooks.editor_will_munge_html(txt, self)
def signal_state_change(
self, new_state: EditorState, old_state: EditorState
) -> None:
@ -1648,24 +1645,7 @@ def fontMungeHack(font: str) -> str:
return re.sub(" L$", " Light", font)
def munge_html(txt: str, editor: Editor) -> str:
return "" if txt in ("<br>", "<div><br></div>") else txt
def remove_null_bytes(txt: str, editor: Editor) -> str:
# misbehaving apps may include a null byte in the text
return txt.replace("\x00", "")
def reverse_url_quoting(txt: str, editor: Editor) -> str:
# reverse the url quoting we added to get images to display
return editor.mw.col.media.escape_media_filenames(txt, unescape=True)
gui_hooks.editor_will_use_font_for_field.append(fontMungeHack)
gui_hooks.editor_will_munge_html.append(munge_html)
gui_hooks.editor_will_munge_html.append(remove_null_bytes)
gui_hooks.editor_will_munge_html.append(reverse_url_quoting)
def set_cloze_button(editor: Editor) -> None:

View file

@ -684,6 +684,7 @@ exposed_backend_list = [
"get_ignored_before_count",
# CardRenderingService
"encode_iri_paths",
"decode_iri_paths",
]

View file

@ -290,9 +290,17 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
const fieldSave = new ChangeTimer();
function transformContentBeforeSave(content: string): string {
return content.replace(/ data-editor-shrink="(true|false)"/g, "");
// TODO: mungeHTML()
async function transformContentBeforeSave(content: string): Promise<string> {
content = content.replace(/ data-editor-shrink="(true|false)"/g, "");
// misbehaving apps may include a null byte in the text
content = content.replaceAll("\0", "");
// reverse the url quoting we added to get images to display
content = (await decodeIriPaths({ val: content })).val;
if (["<br>", "<div><br></div>"].includes(content)) {
return "";
}
return content;
}
async function updateCurrentNote() {
@ -304,10 +312,10 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
}
}
function updateField(index: number, content: string): void {
fieldSave.schedule(() => {
async function updateField(index: number, content: string): Promise<void> {
fieldSave.schedule(async () => {
bridgeCommand(`key:${index}`);
note!.fields[index] = transformContentBeforeSave(content);
note!.fields[index] = await transformContentBeforeSave(content);
updateCurrentNote();
}, 600);
}
@ -428,6 +436,7 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
encodeIriPaths,
newNote,
editorUpdateNote,
decodeIriPaths,
} from "@generated/backend";
import { wrapInternal } from "@tslib/wrap";
@ -790,11 +799,13 @@ components and functionality for general note editing.
setAddonButtonsDisabled(false);
bridgeCommand(`focus:${index}`);
}}
on:focusout={() => {
on:focusout={async () => {
$focusedField = null;
setAddonButtonsDisabled(true);
bridgeCommand(`blur:${index}`);
note!.fields[index] = transformContentBeforeSave(get(content));
note!.fields[index] = await transformContentBeforeSave(
get(content),
);
updateCurrentNote();
}}
on:mouseenter={() => {