Prefer template string and addEventListener over string concatenation and .on<event>

This commit is contained in:
Henrik Giesel 2021-01-18 17:42:29 +01:00
parent 150de7a683
commit 4deeb798ca

View file

@ -6,15 +6,12 @@ let changeTimer = null;
let currentNoteId = null; let currentNoteId = null;
declare interface String { declare interface String {
format(...args): string; format(...args: string[]): string;
} }
/* kept for compatibility with add-ons */ /* kept for compatibility with add-ons */
String.prototype.format = function (): string { String.prototype.format = function (...args: string[]): string {
const args = arguments; return this.replace(/\{\d+\}/g, (m) => args[m.match(/\d+/)]);
return this.replace(/\{\d+\}/g, function (m) {
return args[m.match(/\d+/)];
});
}; };
function setFGButton(col: string): void { function setFGButton(col: string): void {
@ -165,9 +162,9 @@ function updateButtonState(): void {
const buts = ["bold", "italic", "underline", "superscript", "subscript"]; const buts = ["bold", "italic", "underline", "superscript", "subscript"];
for (const name of buts) { for (const name of buts) {
if (document.queryCommandState(name)) { if (document.queryCommandState(name)) {
$("#" + name).addClass("highlighted"); $(`#${name}`).addClass("highlighted");
} else { } else {
$("#" + name).removeClass("highlighted"); $(`#${name}`).removeClass("highlighted");
} }
} }
@ -205,7 +202,7 @@ function onFocus(elem: HTMLElement): void {
return; return;
} }
currentField = elem; currentField = elem;
pycmd("focus:" + currentFieldOrdinal()); pycmd(`focus:${currentFieldOrdinal()}`);
enableButtons(); enableButtons();
// don't adjust cursor on mouse clicks // don't adjust cursor on mouse clicks
if (mouseDown) { if (mouseDown) {
@ -236,7 +233,7 @@ function focusField(n: number): void {
if (n === null) { if (n === null) {
return; return;
} }
$("#f" + n).focus(); $(`#f${n}`).focus();
} }
function focusIfField(x: number, y: number): boolean { function focusIfField(x: number, y: number): boolean {
@ -290,15 +287,7 @@ function saveField(type: "blur" | "key"): void {
return; return;
} }
// type is either 'blur' or 'key' // type is either 'blur' or 'key'
pycmd( pycmd(`${type}:${currentFieldOrdinal()}:${currentNoteId}:${currentField.innerHTML}`)
type +
":" +
currentFieldOrdinal() +
":" +
currentNoteId +
":" +
currentField.innerHTML
);
} }
function currentFieldOrdinal(): string { function currentFieldOrdinal(): string {
@ -399,26 +388,23 @@ function setFields(fields: [string, string][]): void {
</td> </td>
</tr>`; </tr>`;
} }
$("#fields").html(` $("#fields").html(`<table cellpadding=0 width=100% style='table-layout: fixed;'>${txt}</table>`);
<table cellpadding=0 width=100% style='table-layout: fixed;'>
${txt}
</table>`);
maybeDisableButtons(); maybeDisableButtons();
} }
function setBackgrounds(cols) { function setBackgrounds(cols: ("dupe")[]) {
for (let i = 0; i < cols.length; i++) { for (let i = 0; i < cols.length; i++) {
if (cols[i] == "dupe") { if (cols[i] === "dupe") {
$("#f" + i).addClass("dupe"); $(`#f${i}`).addClass("dupe");
} else { } else {
$("#f" + i).removeClass("dupe"); $(`#f${i}`).removeClass("dupe");
} }
} }
} }
function setFonts(fonts: [string, number, boolean][]): void { function setFonts(fonts: [string, number, boolean][]): void {
for (let i = 0; i < fonts.length; i++) { for (let i = 0; i < fonts.length; i++) {
const n = $("#f" + i); const n = $(`#f${i}`);
n.css("font-family", fonts[i][0]).css("font-size", fonts[i][1]); n.css("font-family", fonts[i][0]).css("font-size", fonts[i][1]);
n[0].dir = fonts[i][2] ? "rtl" : "ltr"; n[0].dir = fonts[i][2] ? "rtl" : "ltr";
} }
@ -446,7 +432,7 @@ let pasteHTML = function (html: string, internal: boolean, extendedMode: boolean
let filterHTML = function (html: string, internal: boolean, extendedMode: boolean): string { let filterHTML = function (html: string, internal: boolean, extendedMode: boolean): 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 = $.parseHTML("<ankitop>" + html + "</ankitop>")[0] as Element; const top = $.parseHTML(`<ankitop>${html}</ankitop>`)[0] as Element;
if (internal) { if (internal) {
filterInternalNode(top); filterInternalNode(top);
} else { } else {
@ -616,42 +602,37 @@ let filterNode = function (node: Node, extendedMode: boolean): void {
let adjustFieldsTopMargin = function (): void { let adjustFieldsTopMargin = function (): void {
const topHeight = $("#topbuts").height(); const topHeight = $("#topbuts").height();
const margin = topHeight + 8; const margin = topHeight + 8;
document.getElementById("fields").style.marginTop = margin + "px"; document.getElementById("fields").style.marginTop = `${margin}px`;
}; };
let mouseDown = 0; let mouseDown = 0;
$(function (): void { $(function (): void {
document.body.onmousedown = function () { document.body.addEventListener("mousedown", () => (mouseDown++));
mouseDown++; document.body.addEventListener("mouseup", () => (mouseDown--));
};
document.body.onmouseup = function () { document.addEventListener("click", (evt: MouseEvent): void => {
mouseDown--;
};
document.onclick = function (evt: MouseEvent): void {
const src = evt.target as Element; const src = evt.target as Element;
if (src.tagName === "IMG") { if (src.tagName === "IMG") {
// image clicked; find contenteditable parent // image clicked; find contenteditable parent
let p = src; let p = src;
while ((p = p.parentNode as Element)) { while ((p = p.parentNode as Element)) {
if (p.className === "field") { if (p.className === "field") {
$("#" + p.id).focus(); $(`#${p.id}`).focus();
break; break;
} }
} }
} }
};
// prevent editor buttons from taking focus
$("button.linkb").on("mousedown", function (e) {
e.preventDefault();
}); });
window.onresize = function () { // prevent editor buttons from taking focus
$("button.linkb").on("mousedown", function (evt: Event) {
evt.preventDefault();
});
window.addEventListener("resize", () => {
adjustFieldsTopMargin(); adjustFieldsTopMargin();
}; });
adjustFieldsTopMargin(); adjustFieldsTopMargin();
}); });