Add toggle sticky shortcuts F9 and Shift+F9

This commit is contained in:
Henrik Giesel 2021-08-03 05:52:07 +02:00
parent 87f9bd8021
commit 357a6c5cc6
4 changed files with 61 additions and 22 deletions

View File

@ -58,6 +58,7 @@ class AddCards(QDialog):
def setupEditor(self) -> None:
self.editor = aqt.editor.Editor(self.mw, self.form.fieldsArea, self, True)
self.editor.web.eval("activateStickyShortcuts();")
def setup_choosers(self) -> None:
defaults = self.col.defaults_for_adding(

View File

@ -389,6 +389,22 @@ $editorToolbar.then(({{ toolbar }}) => toolbar.appendGroup({{
self.currentField = int(num)
gui_hooks.editor_did_focus_field(self.note, self.currentField)
elif cmd.startswith("toggleStickyAll"):
model = self.note.note_type()
flds = model["flds"]
any_sticky = any([fld["sticky"] for fld in flds])
result = []
for fld in flds:
if not any_sticky or fld["sticky"]:
fld["sticky"] = not fld["sticky"]
result.append(fld["sticky"])
update_notetype_legacy(parent=self.mw, notetype=model).run_in_background()
return result
elif cmd.startswith("toggleSticky"):
(type, num) = cmd.split(":", 1)
ord = int(num)

View File

@ -28,6 +28,7 @@ export { setNoteId, getNoteId } from "./note-id";
export { saveNow } from "./change-timer";
export { wrap, wrapIntoText } from "./wrap";
export { editorToolbar } from "./toolbar";
export { activateStickyShortcuts } from "./label-container";
export { components } from "./Components.svelte";
declare global {

View File

@ -1,17 +1,32 @@
// Copyright: Ankitects Pty Ltd and contributors
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
import type { EditorField } from "./editor-field";
import { registerShortcut } from "lib/shortcuts";
import { bridgeCommand } from "./lib";
import { getCurrentField, forEditorField } from ".";
import pinIcon from "./pin-angle.svg";
function removeHoverIcon(evt: Event): void {
const icon = evt.currentTarget as HTMLElement;
icon.classList.remove("icon--hover");
function toggleStickyCurrentField() {
const currentField = getCurrentField();
if (currentField) {
const labelContainer = (currentField.parentElement as EditorField)
.labelContainer;
labelContainer.toggleSticky();
}
}
function hoverIcon(evt: Event): void {
const icon = evt.currentTarget as HTMLElement;
icon.classList.add("icon--hover");
function toggleStickyAll() {
bridgeCommand("toggleStickyAll", (values: boolean[]) =>
forEditorField(values, (field, value) => field.labelContainer.setSticky(value))
);
}
export function activateStickyShortcuts() {
registerShortcut(toggleStickyCurrentField, "F9");
registerShortcut(toggleStickyAll, "Shift+F9");
}
export class LabelContainer extends HTMLDivElement {
@ -32,41 +47,47 @@ export class LabelContainer extends HTMLDivElement {
this.sticky.hidden = true;
this.appendChild(this.sticky);
this.setSticky = this.setSticky.bind(this);
this.hoverIcon = this.hoverIcon.bind(this);
this.removeHoverIcon = this.removeHoverIcon.bind(this);
this.toggleSticky = this.toggleSticky.bind(this);
}
connectedCallback(): void {
this.sticky.addEventListener("click", this.toggleSticky);
this.sticky.addEventListener("mouseenter", hoverIcon);
this.sticky.addEventListener("mouseleave", removeHoverIcon);
this.sticky.addEventListener("mouseenter", this.hoverIcon);
this.sticky.addEventListener("mouseleave", this.removeHoverIcon);
}
disconnectedCallback(): void {
this.sticky.removeEventListener("click", this.toggleSticky);
this.sticky.removeEventListener("mouseenter", hoverIcon);
this.sticky.removeEventListener("mouseleave", removeHoverIcon);
this.sticky.removeEventListener("mouseenter", this.hoverIcon);
this.sticky.removeEventListener("mouseleave", this.removeHoverIcon);
}
initialize(labelName: string): void {
this.label.innerText = labelName;
}
setSticky(state: boolean): void {
this.sticky.classList.toggle("is-inactive", !state);
}
activateSticky(initialState: boolean): void {
this.setSticky(initialState);
this.sticky.hidden = false;
}
toggleSticky(evt: Event): void {
bridgeCommand(
`toggleSticky:${this.getAttribute("ord")}`,
(newState: boolean): void => {
this.setSticky(newState);
}
);
removeHoverIcon(evt);
setSticky(state: boolean): void {
this.sticky.classList.toggle("is-inactive", !state);
}
hoverIcon(): void {
this.sticky.classList.add("icon--hover");
}
removeHoverIcon(): void {
this.sticky.classList.remove("icon--hover");
}
toggleSticky(): void {
bridgeCommand(`toggleSticky:${this.getAttribute("ord")}`, this.setSticky);
this.removeHoverIcon();
}
}