1
0
mirror of https://github.com/osmarks/mycorrhiza.git synced 2025-11-06 02:23:02 +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;
window.hyphaChanged = false; let textarea = document.querySelector('.edit-form__textarea');
let textarea = document.querySelector('.edit-form__textarea'); let form = document.querySelector('.edit-form');
let form = document.querySelector('.edit-form');
let warnBeforeClosing = function (ev) { let warnBeforeClosing = function (ev) {
if (!window.hyphaChanged) return; if (!window.hyphaChanged) return;
ev.preventDefault(); ev.preventDefault();
return ev.returnValue = 'Are you sure you want to exit? You have unsaved changes.'; 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; window.hyphaChanged = true;
}); });
form.addEventListener('submit', function () { form.addEventListener('submit', function () {
window.hyphaChanged = false; window.hyphaChanged = false;
}); });
window.addEventListener('beforeunload', warnBeforeClosing); window.addEventListener('beforeunload', warnBeforeClosing);
})();

View File

@@ -1,19 +1,18 @@
(() => { const $ = document.querySelector.bind(document);
const $ = document.querySelector.bind(document); const $$ = (...args) => Array.prototype.slice.call(document.querySelectorAll(...args));
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; let elideShift = event.key.toUpperCase() === event.key && event.shiftKey;
return (event.ctrlKey ? 'Ctrl+' : '') + return (event.ctrlKey ? 'Ctrl+' : '') +
(event.altKey ? 'Alt+' : '') + (event.altKey ? 'Alt+' : '') +
(event.metaKey ? 'Meta+' : '') + (event.metaKey ? 'Meta+' : '') +
(!elideShift && event.shiftKey ? 'Shift+' : '') + (!elideShift && event.shiftKey ? 'Shift+' : '') +
(event.key === ',' ? 'Comma' : event.key === ' ' ? 'Space' : event.key); (event.key === ',' ? 'Comma' : event.key === ' ' ? 'Space' : event.key);
} }
function prettifyShortcut(shortcut) { function prettifyShortcut(shortcut) {
let keys = shortcut.split('+'); let keys = shortcut.split('+');
if (isMac) { if (isMac) {
@@ -58,22 +57,22 @@
} }
return keys.join(isMac ? '' : ' + '); return keys.join(isMac ? '' : ' + ');
} }
function isTextField(element) { function isTextField(element) {
let name = element.nodeName.toLowerCase(); let name = element.nodeName.toLowerCase();
return name === 'textarea' || return name === 'textarea' ||
name === 'select' || name === 'select' ||
(name === 'input' && !['submit', 'reset', 'checkbox', 'radio'].includes(element.type)) || (name === 'input' && !['submit', 'reset', 'checkbox', 'radio'].includes(element.type)) ||
element.isContentEditable; 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 allShortcuts = [];
let shortcutsGroup = null; let shortcutsGroup = null;
class ShortcutHandler { class ShortcutHandler {
constructor(element, override, filter = () => true) { constructor(element, override, filter = () => true) {
this.element = element; this.element = element;
this.map = {}; this.map = {};
@@ -158,7 +157,7 @@
} }
for (let i = 1; i <= elements.length && i < 10; i++) { 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; this.timeout = null;
} }
} }
} }
class ShortcutsHelpDialog { class ShortcutsHelpDialog {
constructor() { constructor() {
let template = $('#dialog-template'); let template = $('#dialog-template');
let clonedTemplate = template.content.cloneNode(true); let clonedTemplate = template.content.cloneNode(true);
@@ -263,10 +262,12 @@
listItem.appendChild(shortcutColumn); listItem.appendChild(shortcutColumn);
} }
if (shortcutsGroup) {
shortcutsGroup.appendChild(list); shortcutsGroup.appendChild(list);
} }
} }
} }
}
open() { open() {
this.prevActiveElement = document.activeElement; this.prevActiveElement = document.activeElement;
@@ -287,9 +288,9 @@
this.prevActiveElement = null; this.prevActiveElement = null;
} }
} }
} }
window.addEventListener('load', () => { window.addEventListener('load', () => {
let helpDialog = null; let helpDialog = null;
let openHelp = () => { let openHelp = () => {
if (!helpDialog) helpDialog = new ShortcutsHelpDialog(); if (!helpDialog) helpDialog = new ShortcutsHelpDialog();
@@ -363,5 +364,4 @@
this.bindElement(isMac ? 'Meta+Enter' : 'Ctrl+Enter', $('.edit-form__save'), 'Save changes'); 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 // selection is decorated, so we just cut it
removing = true removing = true
result = text.substring(cursorPosition, text.length - cursorPosition) result = text.substring(cursorPosition, text.length - cursorPosition)
} else if ( (prefix == el.value.slice(start-cursorPosition, start)) && } else if ( (prefix === el.value.slice(start-cursorPosition, start)) &&
(postfix == el.value.slice(end, end+cursorPosition)) ) { (postfix === el.value.slice(end, end+cursorPosition)) ) {
// selection is surrounded by decorations // selection is surrounded by decorations
removing = true removing = true
result = text result = text