Remove jQuery from most top functions, and avoid waiting for jquery load

This commit is contained in:
Henrik Giesel 2021-01-19 03:23:29 +01:00
parent 5b24e5c522
commit cfc8e34cf0

View file

@ -12,12 +12,10 @@ declare interface String {
/* kept for compatibility with add-ons */ /* kept for compatibility with add-ons */
String.prototype.format = function (...args: string[]): string { String.prototype.format = function (...args: string[]): string {
return this.replace(/\{\d+\}/g, (m: string): void => { return this.replace(/\{\d+\}/g, (m: string): void => {
const match = m.match(/\d+/) const match = m.match(/\d+/);
return match return match ? args[match[0]] : "";
? args[match[0]] });
: "";
})
}; };
function setFGButton(col: string): void { function setFGButton(col: string): void {
@ -393,8 +391,8 @@ function setFields(fields: [string, string][]): void {
function setBackgrounds(cols: "dupe"[]) { function setBackgrounds(cols: "dupe"[]) {
for (let i = 0; i < cols.length; i++) { for (let i = 0; i < cols.length; i++) {
const element = document.querySelector(`#f${i}`) const element = document.querySelector(`#f${i}`);
element.classList.toggle("dupe", cols[i] === "dupe") element.classList.toggle("dupe", cols[i] === "dupe");
} }
} }
@ -436,7 +434,7 @@ let filterHTML = function (
extendedMode: boolean extendedMode: boolean
): string { ): string {
// wrap it in <top> as we aren't allowed to change top level elements // wrap it in <top> as we aren't allowed to change top level elements
const top = document.createElement("ankitop") const top = document.createElement("ankitop");
top.innerHTML = html; top.innerHTML = html;
if (internal) { if (internal) {
@ -607,31 +605,31 @@ let adjustFieldsTopMargin = function (): void {
document.getElementById("fields").style.marginTop = `${margin}px`; document.getElementById("fields").style.marginTop = `${margin}px`;
}; };
let mouseDown = 0; document.addEventListener("click", (evt: MouseEvent): void => {
const src = evt.target as Element;
$(function (): void { if (src.tagName === "IMG") {
document.addEventListener("click", (evt: MouseEvent): void => { // image clicked; find contenteditable parent
const src = evt.target as Element; let p = src;
if (src.tagName === "IMG") { while ((p = p.parentNode as Element)) {
// image clicked; find contenteditable parent if (p.className === "field") {
let p = src; document.getElementById(p.id).focus();
while ((p = p.parentNode as Element)) { break;
if (p.className === "field") {
$(`#${p.id}`).focus();
break;
}
} }
} }
}); }
});
// prevent editor buttons from taking focus // prevent editor buttons from taking focus
$("button.linkb").on("mousedown", function (evt: Event) { for (const element of document.querySelectorAll("button.linkb")) {
element.addEventListener("mousedown", (evt: Event) => {
evt.preventDefault(); evt.preventDefault();
}); });
}
window.addEventListener("resize", () => { window.addEventListener("resize", () => {
adjustFieldsTopMargin(); adjustFieldsTopMargin();
}); });
$(function (): void {
adjustFieldsTopMargin(); adjustFieldsTopMargin();
}); });