Skip to content

Commit 9cfd8af

Browse files
committed
Fix add-on buttons not working in the editor (#3941)
* Fix add-on buttons not working in the editor * Ensure old listeners are cleaned up Thanks to iamllama: #3941 (comment) (cherry picked from commit 1e74e8e)
1 parent e249b92 commit 9cfd8af

File tree

2 files changed

+38
-5
lines changed

2 files changed

+38
-5
lines changed

qt/aqt/editor.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -320,19 +320,18 @@ def _addButton(
320320
label_element = cmd
321321

322322
title_attribute = shortcut(title_attribute)
323-
cmd_to_toggle_button = "toggleEditorButton(this);" if toggleable else ""
324323
id_attribute_assignment = f"id={id}" if id else ""
325324
class_attribute = "linkb" if rightside else "rounded"
326325
if not disables:
327326
class_attribute += " perm"
328327

329328
return f"""<button tabindex=-1
330329
{id_attribute_assignment}
331-
class="{class_attribute}"
330+
class="anki-addon-button {class_attribute}"
332331
type="button"
333332
title="{title_attribute}"
334-
onclick="pycmd('{cmd}');{cmd_to_toggle_button}return false;"
335-
onmousedown="window.event.preventDefault();"
333+
data-cantoggle="{int(toggleable)}"
334+
data-command="{cmd}"
336335
>
337336
{image_element}
338337
{label_element}

ts/editor/editor-toolbar/AddonButtons.svelte

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,42 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
44
-->
55
<script lang="ts">
66
import ButtonGroup from "$lib/components/ButtonGroup.svelte";
7+
import { bridgeCommand } from "@tslib/bridgecommand";
8+
import { toggleEditorButton } from "../old-editor-adapter";
9+
import { singleCallback } from "@tslib/typing";
10+
import { on } from "@tslib/events";
711
8-
export let buttons: string[];
12+
const { buttons } = $props<{ buttons: string[] }>();
13+
14+
$effect(() => {
15+
// Each time the buttons are changed...
16+
buttons;
17+
18+
// Add event handlers to each button
19+
const addonButtons = document.querySelectorAll(".anki-addon-button");
20+
const cbs = [...addonButtons].map((button) =>
21+
singleCallback(
22+
on(button, "click", () => {
23+
const command = button.getAttribute("data-command");
24+
if (command) {
25+
bridgeCommand(command);
26+
}
27+
const toggleable = button.getAttribute("data-cantoggle");
28+
if (toggleable === "1") {
29+
toggleEditorButton(button as HTMLButtonElement);
30+
}
31+
32+
return false;
33+
}),
34+
on(button as HTMLButtonElement, "mousedown", (evt) => {
35+
evt.preventDefault();
36+
evt.stopPropagation();
37+
}),
38+
),
39+
);
40+
41+
return singleCallback(...cbs);
42+
});
943
1044
const radius = "5px";
1145
function getBorderRadius(index: number, length: number): string {

0 commit comments

Comments
 (0)