Merge pull request #1321 from hgiesel/stickyshortcuts

Sticky shortcuts
This commit is contained in:
Damien Elmes 2021-08-03 14:18:00 +10:00 committed by GitHub
commit 6a9b4cd0f1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 70 additions and 24 deletions

View file

@ -39,6 +39,7 @@ editing-subscript = Subscript
editing-superscript = Superscript editing-superscript = Superscript
editing-tags = Tags editing-tags = Tags
editing-to-make-a-cloze-deletion-on = To make a cloze deletion on an existing note, you need to change it to a cloze type first, via 'Notes>Change Note Type' editing-to-make-a-cloze-deletion-on = To make a cloze deletion on an existing note, you need to change it to a cloze type first, via 'Notes>Change Note Type'
editing-toggle-sticky = Toggle sticky
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.

View file

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

View file

@ -389,6 +389,22 @@ $editorToolbar.then(({{ toolbar }}) => toolbar.appendGroup({{
self.currentField = int(num) self.currentField = int(num)
gui_hooks.editor_did_focus_field(self.note, self.currentField) 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"): elif cmd.startswith("toggleSticky"):
(type, num) = cmd.split(":", 1) (type, num) = cmd.split(":", 1)
ord = int(num) ord = int(num)

View file

@ -55,7 +55,7 @@
cursor: pointer; cursor: pointer;
&.is-inactive { &.is-inactive {
opacity: 0.1; opacity: 0.2;
} }
&.icon--hover { &.icon--hover {

View file

@ -28,6 +28,7 @@ export { setNoteId, getNoteId } from "./note-id";
export { saveNow } from "./change-timer"; export { saveNow } from "./change-timer";
export { wrap, wrapIntoText } from "./wrap"; export { wrap, wrapIntoText } from "./wrap";
export { editorToolbar } from "./toolbar"; export { editorToolbar } from "./toolbar";
export { activateStickyShortcuts } from "./label-container";
export { components } from "./Components.svelte"; export { components } from "./Components.svelte";
declare global { declare global {
@ -190,7 +191,7 @@ export function setFormat(cmd: string, arg?: string, nosave = false): void {
} }
} }
const i18n = setupI18n({ export const i18n = setupI18n({
modules: [ modules: [
ModuleName.EDITING, ModuleName.EDITING,
ModuleName.KEYBOARD, ModuleName.KEYBOARD,

View file

@ -1,17 +1,34 @@
// Copyright: Ankitects Pty Ltd and contributors // Copyright: Ankitects Pty Ltd and contributors
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html // License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
import type { EditorField } from "./editor-field";
import * as tr from "lib/i18n";
import { registerShortcut } from "lib/shortcuts";
import { bridgeCommand } from "./lib"; import { bridgeCommand } from "./lib";
import { appendInParentheses } from "./helpers";
import { getCurrentField, forEditorField, i18n } from ".";
import pinIcon from "./pin-angle.svg"; import pinIcon from "./pin-angle.svg";
function removeHoverIcon(evt: Event): void { function toggleStickyCurrentField(): void {
const icon = evt.currentTarget as HTMLElement; const currentField = getCurrentField();
icon.classList.remove("icon--hover");
if (currentField) {
const labelContainer = (currentField.parentElement as EditorField)
.labelContainer;
labelContainer.toggleSticky();
}
} }
function hoverIcon(evt: Event): void { function toggleStickyAll(): void {
const icon = evt.currentTarget as HTMLElement; bridgeCommand("toggleStickyAll", (values: boolean[]) =>
icon.classList.add("icon--hover"); forEditorField(values, (field, value) => field.labelContainer.setSticky(value))
);
}
export function activateStickyShortcuts(): void {
registerShortcut(toggleStickyCurrentField, "F9");
registerShortcut(toggleStickyAll, "Shift+F9");
} }
export class LabelContainer extends HTMLDivElement { export class LabelContainer extends HTMLDivElement {
@ -22,6 +39,10 @@ export class LabelContainer extends HTMLDivElement {
super(); super();
this.className = "d-flex justify-content-between"; this.className = "d-flex justify-content-between";
i18n.then(() => {
this.title = appendInParentheses(tr.editingToggleSticky(), "F9");
});
this.label = document.createElement("span"); this.label = document.createElement("span");
this.label.className = "fieldname"; this.label.className = "fieldname";
this.appendChild(this.label); this.appendChild(this.label);
@ -32,41 +53,47 @@ export class LabelContainer extends HTMLDivElement {
this.sticky.hidden = true; this.sticky.hidden = true;
this.appendChild(this.sticky); 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); this.toggleSticky = this.toggleSticky.bind(this);
} }
connectedCallback(): void { connectedCallback(): void {
this.sticky.addEventListener("click", this.toggleSticky); this.sticky.addEventListener("click", this.toggleSticky);
this.sticky.addEventListener("mouseenter", hoverIcon); this.sticky.addEventListener("mouseenter", this.hoverIcon);
this.sticky.addEventListener("mouseleave", removeHoverIcon); this.sticky.addEventListener("mouseleave", this.removeHoverIcon);
} }
disconnectedCallback(): void { disconnectedCallback(): void {
this.sticky.removeEventListener("click", this.toggleSticky); this.sticky.removeEventListener("click", this.toggleSticky);
this.sticky.removeEventListener("mouseenter", hoverIcon); this.sticky.removeEventListener("mouseenter", this.hoverIcon);
this.sticky.removeEventListener("mouseleave", removeHoverIcon); this.sticky.removeEventListener("mouseleave", this.removeHoverIcon);
} }
initialize(labelName: string): void { initialize(labelName: string): void {
this.label.innerText = labelName; this.label.innerText = labelName;
} }
setSticky(state: boolean): void {
this.sticky.classList.toggle("is-inactive", !state);
}
activateSticky(initialState: boolean): void { activateSticky(initialState: boolean): void {
this.setSticky(initialState); this.setSticky(initialState);
this.sticky.hidden = false; this.sticky.hidden = false;
} }
toggleSticky(evt: Event): void { setSticky(state: boolean): void {
bridgeCommand( this.sticky.classList.toggle("is-inactive", !state);
`toggleSticky:${this.getAttribute("ord")}`,
(newState: boolean): void => {
this.setSticky(newState);
} }
);
removeHoverIcon(evt); 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();
} }
} }