run prettier

This commit is contained in:
Damien Elmes 2019-12-18 16:12:39 +10:00
parent d7d0d9bf88
commit 40418c3712
6 changed files with 137 additions and 91 deletions

View file

@ -4,30 +4,29 @@
$(init); $(init);
function init() { function init() {
$("tr.deck").draggable({ $("tr.deck").draggable({
scroll: false, scroll: false,
// can't use "helper: 'clone'" because of a bug in jQuery 1.5 // can't use "helper: 'clone'" because of a bug in jQuery 1.5
helper: function (event) { helper: function(event) {
return $(this).clone(false); return $(this).clone(false);
}, },
delay: 200, delay: 200,
opacity: 0.7 opacity: 0.7,
}); });
$("tr.deck").droppable({ $("tr.deck").droppable({
drop: handleDropEvent, drop: handleDropEvent,
hoverClass: 'drag-hover' hoverClass: "drag-hover",
}); });
$("tr.top-level-drag-row").droppable({ $("tr.top-level-drag-row").droppable({
drop: handleDropEvent, drop: handleDropEvent,
hoverClass: 'drag-hover' hoverClass: "drag-hover",
}); });
} }
function handleDropEvent(event, ui) { function handleDropEvent(event, ui) {
var draggedDeckId = ui.draggable.attr('id'); var draggedDeckId = ui.draggable.attr("id");
var ontoDeckId = $(this).attr('id') || ''; var ontoDeckId = $(this).attr("id") || "";
pycmd("drag:" + draggedDeckId + "," + ontoDeckId); pycmd("drag:" + draggedDeckId + "," + ontoDeckId);
} }

View file

@ -9,13 +9,13 @@ var dropTarget = null;
var currentNoteId = null; var currentNoteId = null;
declare interface String { declare interface String {
format(...args) : string; format(...args): string;
} }
/* kept for compatibility with add-ons */ /* kept for compatibility with add-ons */
String.prototype.format = function () { String.prototype.format = function() {
var args = arguments; var 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+/)];
}); });
}; };
@ -41,7 +41,7 @@ function saveNow(keepFocus) {
function triggerKeyTimer() { function triggerKeyTimer() {
clearChangeTimer(); clearChangeTimer();
changeTimer = setTimeout(function () { changeTimer = setTimeout(function() {
updateButtonState(); updateButtonState();
saveField("key"); saveField("key");
}, 600); }, 600);
@ -54,8 +54,7 @@ function onKey(evt: KeyboardEvent) {
return; return;
} }
// shift+tab goes to previous field // shift+tab goes to previous field
if (navigator.platform === "MacIntel" && if (navigator.platform === "MacIntel" && evt.which === 9 && evt.shiftKey) {
evt.which === 9 && evt.shiftKey) {
evt.preventDefault(); evt.preventDefault();
focusPrevious(); focusPrevious();
return; return;
@ -119,7 +118,7 @@ function updateButtonState() {
} }
// fixme: forecolor // fixme: forecolor
// 'col': document.queryCommandValue("forecolor") // 'col': document.queryCommandValue("forecolor")
} }
function toggleEditorButton(buttonid) { function toggleEditorButton(buttonid) {
@ -133,7 +132,7 @@ function toggleEditorButton(buttonid) {
function setFormat(cmd: string, arg?: any, nosave: boolean = false) { function setFormat(cmd: string, arg?: any, nosave: boolean = false) {
document.execCommand(cmd, false, arg); document.execCommand(cmd, false, arg);
if (!nosave) { if (!nosave) {
saveField('key'); saveField("key");
updateButtonState(); updateButtonState();
} }
} }
@ -164,13 +163,15 @@ function onFocus(elem) {
var cur = 0; var 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); var y = pos(elem);
if ((window.pageYOffset + window.innerHeight) < (y + elem.offsetHeight) || if (
window.pageYOffset > y) { window.pageYOffset + window.innerHeight < y + elem.offsetHeight ||
window.pageYOffset > y
) {
window.scroll(0, y + elem.offsetHeight - window.innerHeight); window.scroll(0, y + elem.offsetHeight - window.innerHeight);
} }
} }
@ -193,7 +194,7 @@ function focusPrevious() {
} }
function onDragOver(elem) { function onDragOver(elem) {
var e = window.event as unknown as DragOverEvent; var 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
@ -244,7 +245,15 @@ function saveField(type) {
return; return;
} }
// type is either 'blur' or 'key' // type is either 'blur' or 'key'
pycmd(type + ":" + currentFieldOrdinal() + ":" + currentNoteId + ":" + currentField.innerHTML); pycmd(
type +
":" +
currentFieldOrdinal() +
":" +
currentNoteId +
":" +
currentField.innerHTML
);
} }
function currentFieldOrdinal() { function currentFieldOrdinal() {
@ -266,7 +275,10 @@ function enableButtons() {
// disable the buttons if a field is not currently focused // disable the buttons if a field is not currently focused
function maybeDisableButtons() { function maybeDisableButtons() {
if (!document.activeElement || document.activeElement.className !== "field") { if (
!document.activeElement ||
document.activeElement.className !== "field"
) {
disableButtons(); disableButtons();
} else { } else {
enableButtons(); enableButtons();
@ -325,13 +337,18 @@ function setFields(fields) {
} }
txt += `<tr><td class=fname>${n}</td></tr><tr><td width=100%>`; txt += `<tr><td class=fname>${n}</td></tr><tr><td width=100%>`;
txt += `<div id=f${i} onkeydown='onKey(window.event);' oninput='onInput()' onmouseup='onKey();'`; txt += `<div id=f${i} onkeydown='onKey(window.event);' oninput='onInput()' onmouseup='onKey();'`;
txt += " onfocus='onFocus(this);' onblur='onBlur();' class='field clearfix' "; txt +=
" onfocus='onFocus(this);' onblur='onBlur();' class='field clearfix' ";
txt += "ondragover='onDragOver(this);' onpaste='onPaste(this);' "; txt += "ondragover='onDragOver(this);' onpaste='onPaste(this);' ";
txt += "oncopy='onCutOrCopy(this);' oncut='onCutOrCopy(this);' "; txt += "oncopy='onCutOrCopy(this);' oncut='onCutOrCopy(this);' ";
txt += `contentEditable=true class=field>${f}</div>`; txt += `contentEditable=true class=field>${f}</div>`;
txt += "</td></tr>"; txt += "</td></tr>";
} }
$("#fields").html("<table cellpadding=0 width=100% style='table-layout: fixed;'>" + txt + "</table>"); $("#fields").html(
"<table cellpadding=0 width=100% style='table-layout: fixed;'>" +
txt +
"</table>"
);
maybeDisableButtons(); maybeDisableButtons();
} }
@ -344,8 +361,7 @@ function setBackgrounds(cols) {
function setFonts(fonts) { function setFonts(fonts) {
for (var i = 0; i < fonts.length; i++) { for (var i = 0; i < fonts.length; i++) {
var n = $("#f" + i); var n = $("#f" + i);
n.css("font-family", fonts[i][0]) n.css("font-family", fonts[i][0]).css("font-size", fonts[i][1]);
.css("font-size", fonts[i][1]);
n[0].dir = fonts[i][2] ? "rtl" : "ltr"; n[0].dir = fonts[i][2] ? "rtl" : "ltr";
} }
} }
@ -362,7 +378,7 @@ function hideDupes() {
$("#dupes").hide(); $("#dupes").hide();
} }
var pasteHTML = function (html, internal, extendedMode) { var 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
@ -373,7 +389,7 @@ var pasteHTML = function (html, internal, extendedMode) {
} }
}; };
var filterHTML = function (html, internal, extendedMode) { var 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) {
@ -398,29 +414,48 @@ var allowedTagsExtended = {};
var TAGS_WITHOUT_ATTRS = ["P", "DIV", "BR", "SUB", "SUP"]; var TAGS_WITHOUT_ATTRS = ["P", "DIV", "BR", "SUB", "SUP"];
var i; var i;
for (i = 0; i < TAGS_WITHOUT_ATTRS.length; i++) { for (i = 0; i < TAGS_WITHOUT_ATTRS.length; i++) {
allowedTagsBasic[TAGS_WITHOUT_ATTRS[i]] = {"attrs": []}; allowedTagsBasic[TAGS_WITHOUT_ATTRS[i]] = { attrs: [] };
} }
TAGS_WITHOUT_ATTRS = ["H1", "H2", "H3", "LI", "UL", "OL", "BLOCKQUOTE", "CODE", TAGS_WITHOUT_ATTRS = [
"PRE", "TABLE", "DD", "DT", "DL", "B", "U", "I", "RUBY", "RT", "RP"]; "H1",
"H2",
"H3",
"LI",
"UL",
"OL",
"BLOCKQUOTE",
"CODE",
"PRE",
"TABLE",
"DD",
"DT",
"DL",
"B",
"U",
"I",
"RUBY",
"RT",
"RP",
];
for (i = 0; i < TAGS_WITHOUT_ATTRS.length; i++) { for (i = 0; i < TAGS_WITHOUT_ATTRS.length; i++) {
allowedTagsExtended[TAGS_WITHOUT_ATTRS[i]] = {"attrs": []}; allowedTagsExtended[TAGS_WITHOUT_ATTRS[i]] = { attrs: [] };
} }
allowedTagsBasic["IMG"] = {"attrs": ["SRC"]}; allowedTagsBasic["IMG"] = { attrs: ["SRC"] };
allowedTagsExtended["A"] = {"attrs": ["HREF"]}; allowedTagsExtended["A"] = { attrs: ["HREF"] };
allowedTagsExtended["TR"] = {"attrs": ["ROWSPAN"]}; allowedTagsExtended["TR"] = { attrs: ["ROWSPAN"] };
allowedTagsExtended["TD"] = {"attrs": ["COLSPAN", "ROWSPAN"]}; allowedTagsExtended["TD"] = { attrs: ["COLSPAN", "ROWSPAN"] };
allowedTagsExtended["TH"] = {"attrs": ["COLSPAN", "ROWSPAN"]}; allowedTagsExtended["TH"] = { attrs: ["COLSPAN", "ROWSPAN"] };
allowedTagsExtended["FONT"] = {"attrs": ["COLOR"]}; allowedTagsExtended["FONT"] = { attrs: ["COLOR"] };
const allowedStyling = { const allowedStyling = {
'color': true, color: true,
'background-color': true, "background-color": true,
'font-weight': true, "font-weight": true,
'font-style': true, "font-style": true,
'text-decoration-line': true, "text-decoration-line": true,
}; };
var filterExternalSpan = function(node) { var filterExternalSpan = function(node) {
@ -429,7 +464,7 @@ var filterExternalSpan = function(node) {
for (i = 0; i < node.attributes.length; i++) { for (i = 0; i < node.attributes.length; i++) {
var attr = node.attributes[i]; var attr = node.attributes[i];
var attrName = attr.name.toUpperCase(); var attrName = attr.name.toUpperCase();
if (attrName !== 'STYLE') { if (attrName !== "STYLE") {
toRemove.push(attr); toRemove.push(attr);
} }
} }
@ -443,7 +478,7 @@ var filterExternalSpan = function(node) {
if (!allowedStyling.hasOwnProperty(name)) { if (!allowedStyling.hasOwnProperty(name)) {
toRemove.push(name); toRemove.push(name);
} }
if (name === 'background-color' && node.style[name] === "transparent") { if (name === "background-color" && node.style[name] === "transparent") {
// google docs adds this unnecessarily // google docs adds this unnecessarily
toRemove.push(name); toRemove.push(name);
} }
@ -451,7 +486,6 @@ var filterExternalSpan = function(node) {
for (let name of toRemove) { for (let name of toRemove) {
node.style.removeProperty(name); node.style.removeProperty(name);
} }
}; };
allowedTagsExtended["SPAN"] = filterExternalSpan; allowedTagsExtended["SPAN"] = filterExternalSpan;
@ -460,7 +494,7 @@ allowedTagsExtended["SPAN"] = filterExternalSpan;
Object.assign(allowedTagsExtended, allowedTagsBasic); Object.assign(allowedTagsExtended, allowedTagsBasic);
// filtering from another field // filtering from another field
var filterInternalNode = function (node) { var 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");
@ -473,7 +507,7 @@ var filterInternalNode = function (node) {
}; };
// filtering from external sources // filtering from external sources
var filterNode = function (node, extendedMode) { var filterNode = function(node, extendedMode) {
// text node? // text node?
if (node.nodeType === 3) { if (node.nodeType === 3) {
return; return;
@ -502,13 +536,13 @@ var filterNode = function (node, extendedMode) {
tag = allowedTagsBasic[node.tagName]; tag = allowedTagsBasic[node.tagName];
} }
if (!tag) { if (!tag) {
if (!node.innerHTML || node.tagName === 'TITLE') { if (!node.innerHTML || node.tagName === "TITLE") {
node.parentNode.removeChild(node); node.parentNode.removeChild(node);
} else { } else {
node.outerHTML = node.innerHTML; node.outerHTML = node.innerHTML;
} }
} else { } else {
if (typeof(tag) === 'function') { if (typeof tag === "function") {
// filtering function provided // filtering function provided
tag(node); tag(node);
} else { } else {
@ -536,21 +570,21 @@ var adjustFieldsTopMargin = function() {
var mouseDown = 0; var mouseDown = 0;
$(function () { $(function() {
document.body.onmousedown = function () { document.body.onmousedown = function() {
mouseDown++; mouseDown++;
}; };
document.body.onmouseup = function () { document.body.onmouseup = function() {
mouseDown--; mouseDown--;
}; };
document.onclick = function (evt: MouseEvent) { document.onclick = function(evt: MouseEvent) {
let src = evt.target as Element; let 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; var 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;
@ -560,7 +594,7 @@ $(function () {
}; };
// prevent editor buttons from taking focus // prevent editor buttons from taking focus
$("button.linkb").on("mousedown", function (e) { $("button.linkb").on("mousedown", function(e) {
e.preventDefault(); e.preventDefault();
}); });

View file

@ -1,4 +1,6 @@
/* Copyright: Ankitects Pty Ltd and contributors /* Copyright: Ankitects Pty Ltd and contributors
* License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html */ * License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html */
$(function () { $("#study").focus(); }); $(function() {
$("#study").focus();
});

View file

@ -4,16 +4,16 @@
var time: number; // set in python code var time: number; // set in python code
let maxTime = 0; let maxTime = 0;
$(function () { $(function() {
$("#ansbut").focus(); $("#ansbut").focus();
updateTime(); updateTime();
setInterval(function () { setInterval(function() {
time += 1; time += 1;
updateTime() updateTime();
}, 1000); }, 1000);
}); });
let updateTime = function () { let updateTime = function() {
let timeNode = $("#time"); let timeNode = $("#time");
if (!maxTime) { if (!maxTime) {
timeNode.text(""); timeNode.text("");

View file

@ -12,7 +12,7 @@ var onUpdateHook;
var onShownHook; var onShownHook;
function _runHook(arr) { function _runHook(arr) {
for (var i=0; i<arr.length; i++) { for (var i = 0; i < arr.length; i++) {
arr[i](); arr[i]();
} }
} }
@ -21,7 +21,9 @@ function _updateQA(html, fadeTime, onupdate, onshown) {
// if a request to update q/a comes in before the previous content // if a request to update q/a comes in before the previous content
// has been loaded, wait a while and try again // has been loaded, wait a while and try again
if (_updatingQA) { if (_updatingQA) {
setTimeout(function () { _updateQA(html, fadeTime, onupdate, onshown) }, 50); setTimeout(function() {
_updateQA(html, fadeTime, onupdate, onshown);
}, 50);
return; return;
} }
@ -36,8 +38,8 @@ function _updateQA(html, fadeTime, onupdate, onshown) {
// update text // update text
try { try {
qa.html(html); qa.html(html);
} catch(err) { } catch (err) {
qa.text("Invalid HTML on card: "+err); qa.text("Invalid HTML on card: " + err);
} }
_runHook(onUpdateHook); _runHook(onUpdateHook);
@ -48,8 +50,8 @@ function _updateQA(html, fadeTime, onupdate, onshown) {
MathJax.Hub.Queue(["Typeset", MathJax.Hub]); MathJax.Hub.Queue(["Typeset", MathJax.Hub]);
// and reveal when processing is done // and reveal when processing is done
MathJax.Hub.Queue(function () { MathJax.Hub.Queue(function() {
qa.fadeTo(fadeTime, 1, function () { qa.fadeTo(fadeTime, 1, function() {
_runHook(onShownHook); _runHook(onShownHook);
_updatingQA = false; _updatingQA = false;
}); });
@ -58,22 +60,30 @@ function _updateQA(html, fadeTime, onupdate, onshown) {
} }
function _showQuestion(q, bodyclass) { function _showQuestion(q, bodyclass) {
_updateQA(q, qFade, function() { _updateQA(
q,
qFade,
function() {
// return to top of window // return to top of window
window.scrollTo(0, 0); window.scrollTo(0, 0);
document.body.className = bodyclass; document.body.className = bodyclass;
}, function() { },
function() {
// focus typing area if visible // focus typing area if visible
typeans = document.getElementById("typeans"); typeans = document.getElementById("typeans");
if (typeans) { if (typeans) {
typeans.focus(); typeans.focus();
} }
}); }
);
} }
function _showAnswer(a, bodyclass) { function _showAnswer(a, bodyclass) {
_updateQA(a, aFade, function() { _updateQA(
a,
aFade,
function() {
if (bodyclass) { if (bodyclass) {
// when previewing // when previewing
document.body.className = bodyclass; document.body.className = bodyclass;
@ -84,15 +94,16 @@ function _showAnswer(a, bodyclass) {
if (e[0]) { if (e[0]) {
e[0].scrollIntoView(); e[0].scrollIntoView();
} }
}, function() { },
}); function() {}
);
} }
const _flagColours = { const _flagColours = {
1: "#ff6666", 1: "#ff6666",
2: "#ff9900", 2: "#ff9900",
3: "#77ff77", 3: "#77ff77",
4: "#77aaff" 4: "#77aaff",
}; };
function _drawFlag(flag) { function _drawFlag(flag) {

View file

@ -2,7 +2,7 @@
* License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html */ * License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html */
// prevent backspace key from going back a page // prevent backspace key from going back a page
document.addEventListener("keydown", function (evt: KeyboardEvent) { document.addEventListener("keydown", function(evt: KeyboardEvent) {
if (evt.keyCode !== 8) { if (evt.keyCode !== 8) {
return; return;
} }