From 83d5d72777e9cd252cd57999c9dcd7621078f979 Mon Sep 17 00:00:00 2001 From: Henrik Giesel Date: Mon, 19 Apr 2021 16:04:20 +0200 Subject: [PATCH] Generalize inListItem to getAnchorElement --- qt/aqt/editor.py | 8 +------- ts/editor-toolbar/index.ts | 2 ++ ts/editor/inputHandlers.ts | 36 ++++++++++++++++++++++++++---------- 3 files changed, 29 insertions(+), 17 deletions(-) diff --git a/qt/aqt/editor.py b/qt/aqt/editor.py index 97b416c1b..efb3ef5b9 100644 --- a/qt/aqt/editor.py +++ b/qt/aqt/editor.py @@ -184,13 +184,7 @@ $editorToolbar.addButtonGroup({{ else "" ) - self.web.eval( - f""" -$editorToolbar = document.getElementById("editorToolbar"); -{lefttopbtns_js} -{righttopbtns_js} -""" - ) + self.web.eval(f"{lefttopbtns_js} {righttopbtns_js}") # Top buttons ###################################################################### diff --git a/ts/editor-toolbar/index.ts b/ts/editor-toolbar/index.ts index b411cf8f8..ff744851b 100644 --- a/ts/editor-toolbar/index.ts +++ b/ts/editor-toolbar/index.ts @@ -58,6 +58,8 @@ class EditorToolbar extends HTMLElement { }); connectedCallback(): void { + globalThis.$editorToolbar = this; + setupI18n({ modules: [ModuleName.EDITING] }).then(() => { const buttons = writable([ getNotetypeGroup(), diff --git a/ts/editor/inputHandlers.ts b/ts/editor/inputHandlers.ts index f4a929763..a5786ffaf 100644 --- a/ts/editor/inputHandlers.ts +++ b/ts/editor/inputHandlers.ts @@ -5,18 +5,34 @@ import { EditingArea } from "./editingArea"; import { caretToEnd, nodeIsElement } from "./helpers"; import { triggerChangeTimer } from "./changeTimer"; -function inListItem(currentField: EditingArea): boolean { - const anchor = currentField.getSelection()!.anchorNode!; +const getAnchorParent = ( + predicate: (element: Element) => element is T +) => (currentField: EditingArea): T | null => { + const anchor = currentField.getSelection()?.anchorNode; - let inList = false; - let n = nodeIsElement(anchor) ? anchor : anchor.parentElement; - while (n) { - inList = inList || window.getComputedStyle(n).display == "list-item"; - n = n.parentElement; + if (!anchor) { + return null; } - return inList; -} + let anchorParent: T | null = null; + let element = nodeIsElement(anchor) ? anchor : anchor.parentElement; + + while (element) { + anchorParent = anchorParent || (predicate(element) ? element : null); + element = element.parentElement; + } + + return anchorParent; +}; + +const getListItem = getAnchorParent( + (element: Element): element is HTMLLIElement => + window.getComputedStyle(element).display === "list-item" +); + +const getParagraph = getAnchorParent( + (element: Element): element is HTMLParamElement => element.tagName === "P" +); export function onInput(event: Event): void { // make sure IME changes get saved @@ -35,7 +51,7 @@ export function onKey(evt: KeyboardEvent): void { } // prefer
instead of
- if (evt.code === "Enter" && !inListItem(currentField)) { + if (evt.code === "Enter" && !getListItem(currentField)) { evt.preventDefault(); document.execCommand("insertLineBreak"); }