From cfc8e34cf019d09f7c7e4440237c37755805d046 Mon Sep 17 00:00:00 2001 From: Henrik Giesel Date: Tue, 19 Jan 2021 03:23:29 +0100 Subject: [PATCH] Remove jQuery from most top functions, and avoid waiting for jquery load --- qt/aqt/data/web/js/editor.ts | 54 +++++++++++++++++------------------- 1 file changed, 26 insertions(+), 28 deletions(-) diff --git a/qt/aqt/data/web/js/editor.ts b/qt/aqt/data/web/js/editor.ts index 54b703ac3..af89b4e7c 100644 --- a/qt/aqt/data/web/js/editor.ts +++ b/qt/aqt/data/web/js/editor.ts @@ -12,12 +12,10 @@ declare interface String { /* kept for compatibility with add-ons */ String.prototype.format = function (...args: string[]): string { return this.replace(/\{\d+\}/g, (m: string): void => { - const match = m.match(/\d+/) + const match = m.match(/\d+/); - return match - ? args[match[0]] - : ""; - }) + return match ? args[match[0]] : ""; + }); }; function setFGButton(col: string): void { @@ -393,8 +391,8 @@ function setFields(fields: [string, string][]): void { function setBackgrounds(cols: "dupe"[]) { for (let i = 0; i < cols.length; i++) { - const element = document.querySelector(`#f${i}`) - element.classList.toggle("dupe", cols[i] === "dupe") + const element = document.querySelector(`#f${i}`); + element.classList.toggle("dupe", cols[i] === "dupe"); } } @@ -436,7 +434,7 @@ let filterHTML = function ( extendedMode: boolean ): string { // wrap it in as we aren't allowed to change top level elements - const top = document.createElement("ankitop") + const top = document.createElement("ankitop"); top.innerHTML = html; if (internal) { @@ -607,31 +605,31 @@ let adjustFieldsTopMargin = function (): void { document.getElementById("fields").style.marginTop = `${margin}px`; }; -let mouseDown = 0; - -$(function (): void { - document.addEventListener("click", (evt: MouseEvent): void => { - const src = evt.target as Element; - if (src.tagName === "IMG") { - // image clicked; find contenteditable parent - let p = src; - while ((p = p.parentNode as Element)) { - if (p.className === "field") { - $(`#${p.id}`).focus(); - break; - } +document.addEventListener("click", (evt: MouseEvent): void => { + const src = evt.target as Element; + if (src.tagName === "IMG") { + // image clicked; find contenteditable parent + let p = src; + while ((p = p.parentNode as Element)) { + if (p.className === "field") { + document.getElementById(p.id).focus(); + break; } } - }); + } +}); - // prevent editor buttons from taking focus - $("button.linkb").on("mousedown", function (evt: Event) { +// prevent editor buttons from taking focus +for (const element of document.querySelectorAll("button.linkb")) { + element.addEventListener("mousedown", (evt: Event) => { evt.preventDefault(); }); +} - window.addEventListener("resize", () => { - adjustFieldsTopMargin(); - }); - +window.addEventListener("resize", () => { + adjustFieldsTopMargin(); +}); + +$(function (): void { adjustFieldsTopMargin(); });