Merge pull request #367 from agentydragon/ref1

Small TS style changes: Replace 'var' with 'let' or const as appropriate
This commit is contained in:
Damien Elmes 2019-12-22 08:09:24 +10:00 committed by GitHub
commit 70ca4fe557
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 68 additions and 74 deletions

View File

@ -25,8 +25,8 @@ function init() {
} }
function handleDropEvent(event, ui) { function handleDropEvent(event, ui) {
var draggedDeckId = ui.draggable.attr("id"); const draggedDeckId = ui.draggable.attr("id");
var ontoDeckId = $(this).attr("id") || ""; const ontoDeckId = $(this).attr("id") || "";
pycmd("drag:" + draggedDeckId + "," + ontoDeckId); pycmd("drag:" + draggedDeckId + "," + ontoDeckId);
} }

View File

@ -3,10 +3,10 @@
import DragOverEvent = JQuery.DragOverEvent; import DragOverEvent = JQuery.DragOverEvent;
var currentField = null; let currentField = null;
var changeTimer = null; let changeTimer = null;
var dropTarget = null; let dropTarget = null;
var currentNoteId = null; let currentNoteId = null;
declare interface String { declare interface String {
format(...args): string; format(...args): string;
@ -14,7 +14,7 @@ declare interface String {
/* kept for compatibility with add-ons */ /* kept for compatibility with add-ons */
String.prototype.format = function() { String.prototype.format = function() {
var args = arguments; const args = arguments;
return this.replace(/\{\d+\}/g, function(m) { return this.replace(/\{\d+\}/g, function(m) {
return args[m.match(/\d+/)]; return args[m.match(/\d+/)];
}); });
@ -73,14 +73,14 @@ function insertNewline() {
// differently. so in such cases we note the height has not // differently. so in such cases we note the height has not
// changed and insert an extra newline. // changed and insert an extra newline.
var r = window.getSelection().getRangeAt(0); const r = window.getSelection().getRangeAt(0);
if (!r.collapsed) { if (!r.collapsed) {
// delete any currently selected text first, making // delete any currently selected text first, making
// sure the delete is undoable // sure the delete is undoable
setFormat("delete"); setFormat("delete");
} }
var oldHeight = currentField.clientHeight; const oldHeight = currentField.clientHeight;
setFormat("inserthtml", "\n"); setFormat("inserthtml", "\n");
if (currentField.clientHeight === oldHeight) { if (currentField.clientHeight === oldHeight) {
setFormat("inserthtml", "\n"); setFormat("inserthtml", "\n");
@ -107,9 +107,8 @@ function onInput() {
} }
function updateButtonState() { function updateButtonState() {
var buts = ["bold", "italic", "underline", "superscript", "subscript"]; const buts = ["bold", "italic", "underline", "superscript", "subscript"];
for (var i = 0; i < buts.length; i++) { for (const name of buts) {
var name = buts[i];
if (document.queryCommandState(name)) { if (document.queryCommandState(name)) {
$("#" + name).addClass("highlighted"); $("#" + name).addClass("highlighted");
} else { } else {
@ -160,14 +159,14 @@ function onFocus(elem) {
caretToEnd(); caretToEnd();
// scroll if bottom of element off the screen // scroll if bottom of element off the screen
function pos(obj) { function pos(obj) {
var cur = 0; let cur = 0;
do { do {
cur += obj.offsetTop; cur += obj.offsetTop;
} while ((obj = obj.offsetParent)); } while ((obj = obj.offsetParent));
return cur; return cur;
} }
var y = pos(elem); const y = pos(elem);
if ( if (
window.pageYOffset + window.innerHeight < y + elem.offsetHeight || window.pageYOffset + window.innerHeight < y + elem.offsetHeight ||
window.pageYOffset > y window.pageYOffset > y
@ -187,14 +186,14 @@ function focusPrevious() {
if (!currentField) { if (!currentField) {
return; return;
} }
var previous = currentFieldOrdinal() - 1; const previous = currentFieldOrdinal() - 1;
if (previous >= 0) { if (previous >= 0) {
focusField(previous); focusField(previous);
} }
} }
function onDragOver(elem) { function onDragOver(elem) {
var e = (window.event as unknown) as DragOverEvent; const e = (window.event as unknown) as DragOverEvent;
//e.dataTransfer.dropEffect = "copy"; //e.dataTransfer.dropEffect = "copy";
e.preventDefault(); e.preventDefault();
// if we focus the target element immediately, the drag&drop turns into a // if we focus the target element immediately, the drag&drop turns into a
@ -215,10 +214,10 @@ function onPaste(elem) {
} }
function caretToEnd() { function caretToEnd() {
var r = document.createRange(); const r = document.createRange();
r.selectNodeContents(currentField); r.selectNodeContents(currentField);
r.collapse(false); r.collapse(false);
var s = document.getSelection(); const s = document.getSelection();
s.removeAllRanges(); s.removeAllRanges();
s.addRange(r); s.addRange(r);
} }
@ -261,7 +260,7 @@ function currentFieldOrdinal() {
} }
function wrappedExceptForWhitespace(text, front, back) { function wrappedExceptForWhitespace(text, front, back) {
var match = text.match(/^(\s*)([^]*?)(\s*)$/); const match = text.match(/^(\s*)([^]*?)(\s*)$/);
return match[1] + front + match[2] + back + match[3]; return match[1] + front + match[2] + back + match[3];
} }
@ -300,16 +299,16 @@ function wrapInternal(front, back, plainText) {
front = "&#8235;" + front + "&#8236;"; front = "&#8235;" + front + "&#8236;";
back = "&#8235;" + back + "&#8236;"; back = "&#8235;" + back + "&#8236;";
} }
var s = window.getSelection(); const s = window.getSelection();
var r = s.getRangeAt(0); let r = s.getRangeAt(0);
var content = r.cloneContents(); const content = r.cloneContents();
var span = document.createElement("span"); const span = document.createElement("span");
span.appendChild(content); span.appendChild(content);
if (plainText) { if (plainText) {
var new_ = wrappedExceptForWhitespace(span.innerText, front, back); const new_ = wrappedExceptForWhitespace(span.innerText, front, back);
setFormat("inserttext", new_); setFormat("inserttext", new_);
} else { } else {
var new_ = wrappedExceptForWhitespace(span.innerHTML, front, back); const new_ = wrappedExceptForWhitespace(span.innerHTML, front, back);
setFormat("inserthtml", new_); setFormat("inserthtml", new_);
} }
if (!span.innerHTML) { if (!span.innerHTML) {
@ -328,10 +327,10 @@ function onCutOrCopy() {
} }
function setFields(fields) { function setFields(fields) {
var txt = ""; let txt = "";
for (var i = 0; i < fields.length; i++) { for (let i = 0; i < fields.length; i++) {
var n = fields[i][0]; const n = fields[i][0];
var f = fields[i][1]; let f = fields[i][1];
if (!f) { if (!f) {
f = "<br>"; f = "<br>";
} }
@ -353,14 +352,14 @@ function setFields(fields) {
} }
function setBackgrounds(cols) { function setBackgrounds(cols) {
for (var i = 0; i < cols.length; i++) { for (let i = 0; i < cols.length; i++) {
$("#f" + i).css("background", cols[i]); $("#f" + i).css("background", cols[i]);
} }
} }
function setFonts(fonts) { function setFonts(fonts) {
for (var i = 0; i < fonts.length; i++) { for (let i = 0; i < fonts.length; i++) {
var 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";
} }
@ -378,7 +377,7 @@ function hideDupes() {
$("#dupes").hide(); $("#dupes").hide();
} }
var pasteHTML = function(html, internal, extendedMode) { let pasteHTML = function(html, internal, extendedMode) {
html = filterHTML(html, internal, extendedMode); html = filterHTML(html, internal, extendedMode);
if (html !== "") { if (html !== "") {
// remove trailing <br> in empty field // remove trailing <br> in empty field
@ -389,7 +388,7 @@ var pasteHTML = function(html, internal, extendedMode) {
} }
}; };
var filterHTML = function(html, internal, extendedMode) { let filterHTML = function(html, internal, extendedMode) {
// 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) {
@ -408,13 +407,12 @@ var filterHTML = function(html, internal, extendedMode) {
return outHtml; return outHtml;
}; };
var allowedTagsBasic = {}; let allowedTagsBasic = {};
var allowedTagsExtended = {}; let allowedTagsExtended = {};
var TAGS_WITHOUT_ATTRS = ["P", "DIV", "BR", "SUB", "SUP"]; let TAGS_WITHOUT_ATTRS = ["P", "DIV", "BR", "SUB", "SUP"];
var i; for (const tag of TAGS_WITHOUT_ATTRS) {
for (i = 0; i < TAGS_WITHOUT_ATTRS.length; i++) { allowedTagsBasic[tag] = { attrs: [] };
allowedTagsBasic[TAGS_WITHOUT_ATTRS[i]] = { attrs: [] };
} }
TAGS_WITHOUT_ATTRS = [ TAGS_WITHOUT_ATTRS = [
@ -438,8 +436,8 @@ TAGS_WITHOUT_ATTRS = [
"RT", "RT",
"RP", "RP",
]; ];
for (i = 0; i < TAGS_WITHOUT_ATTRS.length; i++) { for (const tag of TAGS_WITHOUT_ATTRS) {
allowedTagsExtended[TAGS_WITHOUT_ATTRS[i]] = { attrs: [] }; allowedTagsExtended[tag] = { attrs: [] };
} }
allowedTagsBasic["IMG"] = { attrs: ["SRC"] }; allowedTagsBasic["IMG"] = { attrs: ["SRC"] };
@ -458,23 +456,21 @@ const allowedStyling = {
"text-decoration-line": true, "text-decoration-line": true,
}; };
var filterExternalSpan = function(node) { let filterExternalSpan = function(node) {
// filter out attributes // filter out attributes
var toRemove = []; let toRemove = [];
for (i = 0; i < node.attributes.length; i++) { for (const attr of node.attributes) {
var attr = node.attributes[i]; const attrName = attr.name.toUpperCase();
var attrName = attr.name.toUpperCase();
if (attrName !== "STYLE") { if (attrName !== "STYLE") {
toRemove.push(attr); toRemove.push(attr);
} }
} }
for (i = 0; i < toRemove.length; i++) { for (const attributeToRemove of toRemove) {
node.removeAttributeNode(toRemove[i]); node.removeAttributeNode(attributeToRemove);
} }
// filter styling // filter styling
toRemove = []; toRemove = [];
for (let i = 0; i < node.style.length; i++) { for (const name of node.style) {
const name = node.style[i];
if (!allowedStyling.hasOwnProperty(name)) { if (!allowedStyling.hasOwnProperty(name)) {
toRemove.push(name); toRemove.push(name);
} }
@ -494,20 +490,20 @@ allowedTagsExtended["SPAN"] = filterExternalSpan;
Object.assign(allowedTagsExtended, allowedTagsBasic); Object.assign(allowedTagsExtended, allowedTagsBasic);
// filtering from another field // filtering from another field
var filterInternalNode = function(node) { let filterInternalNode = function(node) {
if (node.style) { if (node.style) {
node.style.removeProperty("background-color"); node.style.removeProperty("background-color");
node.style.removeProperty("font-size"); node.style.removeProperty("font-size");
node.style.removeProperty("font-family"); node.style.removeProperty("font-family");
} }
// recurse // recurse
for (var i = 0; i < node.childNodes.length; i++) { for (const child of node.childNodes) {
filterInternalNode(node.childNodes[i]); filterInternalNode(child);
} }
}; };
// filtering from external sources // filtering from external sources
var filterNode = function(node, extendedMode) { let filterNode = function(node, extendedMode) {
// text node? // text node?
if (node.nodeType === 3) { if (node.nodeType === 3) {
return; return;
@ -516,20 +512,19 @@ var filterNode = function(node, extendedMode) {
// descend first, and take a copy of the child nodes as the loop will skip // descend first, and take a copy of the child nodes as the loop will skip
// elements due to node modifications otherwise // elements due to node modifications otherwise
var nodes = []; const nodes = [];
var i; for (const child of node.childNodes) {
for (i = 0; i < node.childNodes.length; i++) { nodes.push(child);
nodes.push(node.childNodes[i]);
} }
for (i = 0; i < nodes.length; i++) { for (const child of nodes) {
filterNode(nodes[i], extendedMode); filterNode(child, extendedMode);
} }
if (node.tagName === "ANKITOP") { if (node.tagName === "ANKITOP") {
return; return;
} }
var tag; let tag;
if (extendedMode) { if (extendedMode) {
tag = allowedTagsExtended[node.tagName]; tag = allowedTagsExtended[node.tagName];
} else { } else {
@ -547,28 +542,27 @@ var filterNode = function(node, extendedMode) {
tag(node); tag(node);
} else { } else {
// allowed, filter out attributes // allowed, filter out attributes
var toRemove = []; const toRemove = [];
for (i = 0; i < node.attributes.length; i++) { for (const attr of node.attributes) {
var attr = node.attributes[i]; const attrName = attr.name.toUpperCase();
var attrName = attr.name.toUpperCase();
if (tag.attrs.indexOf(attrName) === -1) { if (tag.attrs.indexOf(attrName) === -1) {
toRemove.push(attr); toRemove.push(attr);
} }
} }
for (i = 0; i < toRemove.length; i++) { for (const attributeToRemove of toRemove) {
node.removeAttributeNode(toRemove[i]); node.removeAttributeNode(attributeToRemove);
} }
} }
} }
}; };
var adjustFieldsTopMargin = function() { let adjustFieldsTopMargin = function() {
var topHeight = $("#topbuts").height(); const topHeight = $("#topbuts").height();
var margin = topHeight + 8; const margin = topHeight + 8;
document.getElementById("fields").style.marginTop = margin + "px"; document.getElementById("fields").style.marginTop = margin + "px";
}; };
var mouseDown = 0; let mouseDown = 0;
$(function() { $(function() {
document.body.onmousedown = function() { document.body.onmousedown = function() {
@ -580,10 +574,10 @@ $(function() {
}; };
document.onclick = function(evt: MouseEvent) { document.onclick = function(evt: MouseEvent) {
let 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
var 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();