From ce59fad160a94417b8db30915fc0535eb01b2c93 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20Pokorn=C3=BD=20=28Rai=29?= Date: Fri, 20 Dec 2019 22:33:15 +0100 Subject: [PATCH 1/2] Replace 'var' with 'let' or const as appropriate Also replaces some for-loops over arrays with for..of loops. --- ts/src/deckbrowser.ts | 4 +- ts/src/editor.ts | 136 ++++++++++++++++++++---------------------- 2 files changed, 67 insertions(+), 73 deletions(-) diff --git a/ts/src/deckbrowser.ts b/ts/src/deckbrowser.ts index d4dcceb04..40be7459e 100644 --- a/ts/src/deckbrowser.ts +++ b/ts/src/deckbrowser.ts @@ -25,8 +25,8 @@ function init() { } function handleDropEvent(event, ui) { - var draggedDeckId = ui.draggable.attr("id"); - var ontoDeckId = $(this).attr("id") || ""; + const draggedDeckId = ui.draggable.attr("id"); + const ontoDeckId = $(this).attr("id") || ""; pycmd("drag:" + draggedDeckId + "," + ontoDeckId); } diff --git a/ts/src/editor.ts b/ts/src/editor.ts index bb8ff0c9a..8c9714c78 100644 --- a/ts/src/editor.ts +++ b/ts/src/editor.ts @@ -3,10 +3,10 @@ import DragOverEvent = JQuery.DragOverEvent; -var currentField = null; -var changeTimer = null; -var dropTarget = null; -var currentNoteId = null; +let currentField = null; +let changeTimer = null; +let dropTarget = null; +let currentNoteId = null; declare interface String { format(...args): string; @@ -14,7 +14,7 @@ declare interface String { /* kept for compatibility with add-ons */ String.prototype.format = function() { - var args = arguments; + let args = arguments; return this.replace(/\{\d+\}/g, function(m) { return args[m.match(/\d+/)]; }); @@ -73,14 +73,14 @@ function insertNewline() { // differently. so in such cases we note the height has not // changed and insert an extra newline. - var r = window.getSelection().getRangeAt(0); + const r = window.getSelection().getRangeAt(0); if (!r.collapsed) { // delete any currently selected text first, making // sure the delete is undoable setFormat("delete"); } - var oldHeight = currentField.clientHeight; + const oldHeight = currentField.clientHeight; setFormat("inserthtml", "\n"); if (currentField.clientHeight === oldHeight) { setFormat("inserthtml", "\n"); @@ -107,9 +107,8 @@ function onInput() { } function updateButtonState() { - var buts = ["bold", "italic", "underline", "superscript", "subscript"]; - for (var i = 0; i < buts.length; i++) { - var name = buts[i]; + const buts = ["bold", "italic", "underline", "superscript", "subscript"]; + for (const name of buts) { if (document.queryCommandState(name)) { $("#" + name).addClass("highlighted"); } else { @@ -160,14 +159,14 @@ function onFocus(elem) { caretToEnd(); // scroll if bottom of element off the screen function pos(obj) { - var cur = 0; + let cur = 0; do { cur += obj.offsetTop; } while ((obj = obj.offsetParent)); return cur; } - var y = pos(elem); + const y = pos(elem); if ( window.pageYOffset + window.innerHeight < y + elem.offsetHeight || window.pageYOffset > y @@ -187,14 +186,14 @@ function focusPrevious() { if (!currentField) { return; } - var previous = currentFieldOrdinal() - 1; + const previous = currentFieldOrdinal() - 1; if (previous >= 0) { focusField(previous); } } function onDragOver(elem) { - var e = (window.event as unknown) as DragOverEvent; + let e = (window.event as unknown) as DragOverEvent; //e.dataTransfer.dropEffect = "copy"; e.preventDefault(); // if we focus the target element immediately, the drag&drop turns into a @@ -215,10 +214,10 @@ function onPaste(elem) { } function caretToEnd() { - var r = document.createRange(); + let r = document.createRange(); r.selectNodeContents(currentField); r.collapse(false); - var s = document.getSelection(); + let s = document.getSelection(); s.removeAllRanges(); s.addRange(r); } @@ -261,7 +260,7 @@ function currentFieldOrdinal() { } 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]; } @@ -300,16 +299,16 @@ function wrapInternal(front, back, plainText) { front = "‫" + front + "‬"; back = "‫" + back + "‬"; } - var s = window.getSelection(); - var r = s.getRangeAt(0); - var content = r.cloneContents(); - var span = document.createElement("span"); + let s = window.getSelection(); + let r = s.getRangeAt(0); + let content = r.cloneContents(); + let span = document.createElement("span"); span.appendChild(content); if (plainText) { - var new_ = wrappedExceptForWhitespace(span.innerText, front, back); + const new_ = wrappedExceptForWhitespace(span.innerText, front, back); setFormat("inserttext", new_); } else { - var new_ = wrappedExceptForWhitespace(span.innerHTML, front, back); + const new_ = wrappedExceptForWhitespace(span.innerHTML, front, back); setFormat("inserthtml", new_); } if (!span.innerHTML) { @@ -328,10 +327,10 @@ function onCutOrCopy() { } function setFields(fields) { - var txt = ""; - for (var i = 0; i < fields.length; i++) { - var n = fields[i][0]; - var f = fields[i][1]; + let txt = ""; + for (let i = 0; i < fields.length; i++) { + let n = fields[i][0]; + let f = fields[i][1]; if (!f) { f = "
"; } @@ -353,14 +352,14 @@ function setFields(fields) { } 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]); } } function setFonts(fonts) { - for (var i = 0; i < fonts.length; i++) { - var n = $("#f" + i); + for (let i = 0; i < fonts.length; i++) { + let n = $("#f" + i); n.css("font-family", fonts[i][0]).css("font-size", fonts[i][1]); n[0].dir = fonts[i][2] ? "rtl" : "ltr"; } @@ -378,7 +377,7 @@ function hideDupes() { $("#dupes").hide(); } -var pasteHTML = function(html, internal, extendedMode) { +const pasteHTML = function(html, internal, extendedMode) { html = filterHTML(html, internal, extendedMode); if (html !== "") { // remove trailing
in empty field @@ -389,7 +388,7 @@ var pasteHTML = function(html, internal, extendedMode) { } }; -var filterHTML = function(html, internal, extendedMode) { +const filterHTML = function(html, internal, extendedMode) { // wrap it in as we aren't allowed to change top level elements const top = $.parseHTML("" + html + "")[0] as Element; if (internal) { @@ -408,13 +407,12 @@ var filterHTML = function(html, internal, extendedMode) { return outHtml; }; -var allowedTagsBasic = {}; -var allowedTagsExtended = {}; +let allowedTagsBasic = {}; +let allowedTagsExtended = {}; -var TAGS_WITHOUT_ATTRS = ["P", "DIV", "BR", "SUB", "SUP"]; -var i; -for (i = 0; i < TAGS_WITHOUT_ATTRS.length; i++) { - allowedTagsBasic[TAGS_WITHOUT_ATTRS[i]] = { attrs: [] }; +let TAGS_WITHOUT_ATTRS = ["P", "DIV", "BR", "SUB", "SUP"]; +for (const tag of TAGS_WITHOUT_ATTRS) { + allowedTagsBasic[tag] = { attrs: [] }; } TAGS_WITHOUT_ATTRS = [ @@ -438,8 +436,8 @@ TAGS_WITHOUT_ATTRS = [ "RT", "RP", ]; -for (i = 0; i < TAGS_WITHOUT_ATTRS.length; i++) { - allowedTagsExtended[TAGS_WITHOUT_ATTRS[i]] = { attrs: [] }; +for (const tag of TAGS_WITHOUT_ATTRS) { + allowedTagsExtended[tag] = { attrs: [] }; } allowedTagsBasic["IMG"] = { attrs: ["SRC"] }; @@ -458,23 +456,21 @@ const allowedStyling = { "text-decoration-line": true, }; -var filterExternalSpan = function(node) { +const filterExternalSpan = function(node) { // filter out attributes - var toRemove = []; - for (i = 0; i < node.attributes.length; i++) { - var attr = node.attributes[i]; - var attrName = attr.name.toUpperCase(); + let toRemove = []; + for (const attr of node.attributes) { + const attrName = attr.name.toUpperCase(); if (attrName !== "STYLE") { toRemove.push(attr); } } - for (i = 0; i < toRemove.length; i++) { - node.removeAttributeNode(toRemove[i]); + for (const attributeToRemove of toRemove) { + node.removeAttributeNode(attributeToRemove); } // filter styling toRemove = []; - for (let i = 0; i < node.style.length; i++) { - const name = node.style[i]; + for (const name of node.style) { if (!allowedStyling.hasOwnProperty(name)) { toRemove.push(name); } @@ -494,20 +490,20 @@ allowedTagsExtended["SPAN"] = filterExternalSpan; Object.assign(allowedTagsExtended, allowedTagsBasic); // filtering from another field -var filterInternalNode = function(node) { +const filterInternalNode = function(node) { if (node.style) { node.style.removeProperty("background-color"); node.style.removeProperty("font-size"); node.style.removeProperty("font-family"); } // recurse - for (var i = 0; i < node.childNodes.length; i++) { - filterInternalNode(node.childNodes[i]); + for (const child of node.childNodes) { + filterInternalNode(child); } }; // filtering from external sources -var filterNode = function(node, extendedMode) { +const filterNode = function(node, extendedMode) { // text node? if (node.nodeType === 3) { 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 // elements due to node modifications otherwise - var nodes = []; - var i; - for (i = 0; i < node.childNodes.length; i++) { - nodes.push(node.childNodes[i]); + let nodes = []; + for (const child of node.childNodes) { + nodes.push(child); } - for (i = 0; i < nodes.length; i++) { - filterNode(nodes[i], extendedMode); + for (const child of nodes) { + filterNode(child, extendedMode); } if (node.tagName === "ANKITOP") { return; } - var tag; + let tag; if (extendedMode) { tag = allowedTagsExtended[node.tagName]; } else { @@ -547,28 +542,27 @@ var filterNode = function(node, extendedMode) { tag(node); } else { // allowed, filter out attributes - var toRemove = []; - for (i = 0; i < node.attributes.length; i++) { - var attr = node.attributes[i]; - var attrName = attr.name.toUpperCase(); + let toRemove = []; + for (const attr of node.attributes) { + const attrName = attr.name.toUpperCase(); if (tag.attrs.indexOf(attrName) === -1) { toRemove.push(attr); } } - for (i = 0; i < toRemove.length; i++) { - node.removeAttributeNode(toRemove[i]); + for (const attributeToRemove of toRemove) { + node.removeAttributeNode(attributeToRemove); } } } }; -var adjustFieldsTopMargin = function() { - var topHeight = $("#topbuts").height(); - var margin = topHeight + 8; +const adjustFieldsTopMargin = function() { + const topHeight = $("#topbuts").height(); + const margin = topHeight + 8; document.getElementById("fields").style.marginTop = margin + "px"; }; -var mouseDown = 0; +let mouseDown = 0; $(function() { document.body.onmousedown = function() { @@ -583,7 +577,7 @@ $(function() { let src = evt.target as Element; if (src.tagName === "IMG") { // image clicked; find contenteditable parent - var p = src; + let p = src; while ((p = p.parentNode as Element)) { if (p.className === "field") { $("#" + p.id).focus(); From 16d97c8e0aac46f2da6c560c62cdb92722ac348b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20Pokorn=C3=BD=20=28Rai=29?= Date: Sat, 21 Dec 2019 11:18:58 +0100 Subject: [PATCH 2/2] Replace 'const' with 'let' on global scope functions Also use const where possible. --- ts/src/editor.ts | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/ts/src/editor.ts b/ts/src/editor.ts index 8c9714c78..e5f230c45 100644 --- a/ts/src/editor.ts +++ b/ts/src/editor.ts @@ -14,7 +14,7 @@ declare interface String { /* kept for compatibility with add-ons */ String.prototype.format = function() { - let args = arguments; + const args = arguments; return this.replace(/\{\d+\}/g, function(m) { return args[m.match(/\d+/)]; }); @@ -193,7 +193,7 @@ function focusPrevious() { } function onDragOver(elem) { - let e = (window.event as unknown) as DragOverEvent; + const e = (window.event as unknown) as DragOverEvent; //e.dataTransfer.dropEffect = "copy"; e.preventDefault(); // if we focus the target element immediately, the drag&drop turns into a @@ -214,10 +214,10 @@ function onPaste(elem) { } function caretToEnd() { - let r = document.createRange(); + const r = document.createRange(); r.selectNodeContents(currentField); r.collapse(false); - let s = document.getSelection(); + const s = document.getSelection(); s.removeAllRanges(); s.addRange(r); } @@ -299,10 +299,10 @@ function wrapInternal(front, back, plainText) { front = "‫" + front + "‬"; back = "‫" + back + "‬"; } - let s = window.getSelection(); + const s = window.getSelection(); let r = s.getRangeAt(0); - let content = r.cloneContents(); - let span = document.createElement("span"); + const content = r.cloneContents(); + const span = document.createElement("span"); span.appendChild(content); if (plainText) { const new_ = wrappedExceptForWhitespace(span.innerText, front, back); @@ -329,7 +329,7 @@ function onCutOrCopy() { function setFields(fields) { let txt = ""; for (let i = 0; i < fields.length; i++) { - let n = fields[i][0]; + const n = fields[i][0]; let f = fields[i][1]; if (!f) { f = "
"; @@ -359,7 +359,7 @@ function setBackgrounds(cols) { function setFonts(fonts) { for (let i = 0; i < fonts.length; i++) { - let n = $("#f" + i); + const n = $("#f" + i); n.css("font-family", fonts[i][0]).css("font-size", fonts[i][1]); n[0].dir = fonts[i][2] ? "rtl" : "ltr"; } @@ -377,7 +377,7 @@ function hideDupes() { $("#dupes").hide(); } -const pasteHTML = function(html, internal, extendedMode) { +let pasteHTML = function(html, internal, extendedMode) { html = filterHTML(html, internal, extendedMode); if (html !== "") { // remove trailing
in empty field @@ -388,7 +388,7 @@ const pasteHTML = function(html, internal, extendedMode) { } }; -const filterHTML = function(html, internal, extendedMode) { +let filterHTML = function(html, internal, extendedMode) { // wrap it in as we aren't allowed to change top level elements const top = $.parseHTML("" + html + "")[0] as Element; if (internal) { @@ -456,7 +456,7 @@ const allowedStyling = { "text-decoration-line": true, }; -const filterExternalSpan = function(node) { +let filterExternalSpan = function(node) { // filter out attributes let toRemove = []; for (const attr of node.attributes) { @@ -490,7 +490,7 @@ allowedTagsExtended["SPAN"] = filterExternalSpan; Object.assign(allowedTagsExtended, allowedTagsBasic); // filtering from another field -const filterInternalNode = function(node) { +let filterInternalNode = function(node) { if (node.style) { node.style.removeProperty("background-color"); node.style.removeProperty("font-size"); @@ -503,7 +503,7 @@ const filterInternalNode = function(node) { }; // filtering from external sources -const filterNode = function(node, extendedMode) { +let filterNode = function(node, extendedMode) { // text node? if (node.nodeType === 3) { return; @@ -512,7 +512,7 @@ const filterNode = function(node, extendedMode) { // descend first, and take a copy of the child nodes as the loop will skip // elements due to node modifications otherwise - let nodes = []; + const nodes = []; for (const child of node.childNodes) { nodes.push(child); } @@ -542,7 +542,7 @@ const filterNode = function(node, extendedMode) { tag(node); } else { // allowed, filter out attributes - let toRemove = []; + const toRemove = []; for (const attr of node.attributes) { const attrName = attr.name.toUpperCase(); if (tag.attrs.indexOf(attrName) === -1) { @@ -556,7 +556,7 @@ const filterNode = function(node, extendedMode) { } }; -const adjustFieldsTopMargin = function() { +let adjustFieldsTopMargin = function() { const topHeight = $("#topbuts").height(); const margin = topHeight + 8; document.getElementById("fields").style.marginTop = margin + "px"; @@ -574,7 +574,7 @@ $(function() { }; document.onclick = function(evt: MouseEvent) { - let src = evt.target as Element; + const src = evt.target as Element; if (src.tagName === "IMG") { // image clicked; find contenteditable parent let p = src;