1
0
mirror of https://github.com/osmarks/mycorrhiza.git synced 2025-11-04 17:43:01 +00:00

Javascript related code changes (#112)

* Add if statement for case where shortcutsGroup can be undefined
* Use "===" instead of "==". Comparison prefix "==" may cause unexpected type coercion
* Remove anonymous function wrapping
This commit is contained in:
Mysh!
2021-10-29 21:13:59 +08:00
committed by GitHub
parent 838f9be1c8
commit a3fe98cf49
3 changed files with 335 additions and 337 deletions

View File

@@ -1,21 +1,19 @@
(function () {
window.hyphaChanged = false;
let textarea = document.querySelector('.edit-form__textarea');
let form = document.querySelector('.edit-form');
window.hyphaChanged = false;
let textarea = document.querySelector('.edit-form__textarea');
let form = document.querySelector('.edit-form');
let warnBeforeClosing = function (ev) {
let warnBeforeClosing = function (ev) {
if (!window.hyphaChanged) return;
ev.preventDefault();
return ev.returnValue = 'Are you sure you want to exit? You have unsaved changes.';
};
};
textarea.addEventListener('input', function () {
textarea.addEventListener('input', function () {
window.hyphaChanged = true;
});
});
form.addEventListener('submit', function () {
form.addEventListener('submit', function () {
window.hyphaChanged = false;
});
});
window.addEventListener('beforeunload', warnBeforeClosing);
})();
window.addEventListener('beforeunload', warnBeforeClosing);

View File

@@ -1,19 +1,18 @@
(() => {
const $ = document.querySelector.bind(document);
const $$ = (...args) => Array.prototype.slice.call(document.querySelectorAll(...args));
const $ = document.querySelector.bind(document);
const $$ = (...args) => Array.prototype.slice.call(document.querySelectorAll(...args));
const isMac = /Macintosh/.test(window.navigator.userAgent);
const isMac = /Macintosh/.test(window.navigator.userAgent);
function keyEventToShortcut(event) {
function keyEventToShortcut(event) {
let elideShift = event.key.toUpperCase() === event.key && event.shiftKey;
return (event.ctrlKey ? 'Ctrl+' : '') +
(event.altKey ? 'Alt+' : '') +
(event.metaKey ? 'Meta+' : '') +
(!elideShift && event.shiftKey ? 'Shift+' : '') +
(event.key === ',' ? 'Comma' : event.key === ' ' ? 'Space' : event.key);
}
}
function prettifyShortcut(shortcut) {
function prettifyShortcut(shortcut) {
let keys = shortcut.split('+');
if (isMac) {
@@ -58,22 +57,22 @@
}
return keys.join(isMac ? '' : ' + ');
}
}
function isTextField(element) {
function isTextField(element) {
let name = element.nodeName.toLowerCase();
return name === 'textarea' ||
name === 'select' ||
(name === 'input' && !['submit', 'reset', 'checkbox', 'radio'].includes(element.type)) ||
element.isContentEditable;
}
}
let notTextField = event => !(event.target instanceof Node && isTextField(event.target));
let notTextField = event => !(event.target instanceof Node && isTextField(event.target));
let allShortcuts = [];
let shortcutsGroup = null;
let allShortcuts = [];
let shortcutsGroup = null;
class ShortcutHandler {
class ShortcutHandler {
constructor(element, override, filter = () => true) {
this.element = element;
this.map = {};
@@ -158,7 +157,7 @@
}
for (let i = 1; i <= elements.length && i < 10; i++) {
this.bindElement(`${prefix} ${i}`, elements[i-1], `${itemDescription} #${i}`, false);
this.bindElement(`${prefix} ${i}`, elements[i - 1], `${itemDescription} #${i}`, false);
}
}
@@ -202,9 +201,9 @@
this.timeout = null;
}
}
}
}
class ShortcutsHelpDialog {
class ShortcutsHelpDialog {
constructor() {
let template = $('#dialog-template');
let clonedTemplate = template.content.cloneNode(true);
@@ -263,10 +262,12 @@
listItem.appendChild(shortcutColumn);
}
if (shortcutsGroup) {
shortcutsGroup.appendChild(list);
}
}
}
}
open() {
this.prevActiveElement = document.activeElement;
@@ -287,9 +288,9 @@
this.prevActiveElement = null;
}
}
}
}
window.addEventListener('load', () => {
window.addEventListener('load', () => {
let helpDialog = null;
let openHelp = () => {
if (!helpDialog) helpDialog = new ShortcutsHelpDialog();
@@ -363,5 +364,4 @@
this.bindElement(isMac ? 'Meta+Enter' : 'Ctrl+Enter', $('.edit-form__save'), 'Save changes');
});
}
});
})();
});

View File

@@ -44,8 +44,8 @@ function selectionWrapper(cursorPosition, prefix, postfix = null, el = editTexta
// selection is decorated, so we just cut it
removing = true
result = text.substring(cursorPosition, text.length - cursorPosition)
} else if ( (prefix == el.value.slice(start-cursorPosition, start)) &&
(postfix == el.value.slice(end, end+cursorPosition)) ) {
} else if ( (prefix === el.value.slice(start-cursorPosition, start)) &&
(postfix === el.value.slice(end, end+cursorPosition)) ) {
// selection is surrounded by decorations
removing = true
result = text