Skip to content

Commit

Permalink
misc (#1497)
Browse files Browse the repository at this point in the history
Reviewed-by: Hunter Turcin <[email protected]>
  • Loading branch information
charmander authored Dec 19, 2024
2 parents 2ccbca3 + 0894102 commit 6829153
Show file tree
Hide file tree
Showing 12 changed files with 243 additions and 260 deletions.
135 changes: 54 additions & 81 deletions assets/js/scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,17 @@
return false;
}

var empty = containerNode => {
var child;

while (child = containerNode.firstChild) {
containerNode.removeChild(child);
}
};

var hasModifierKeys = e =>
e.ctrlKey || e.shiftKey || e.altKey || e.metaKey;

// thumbnails: config
var thumbnailContainers = document.getElementsByClassName('thumbnail-grid'),
thumbnailOptions = {
Expand Down Expand Up @@ -161,14 +172,14 @@
}

function closeLoginIfEscape(e) {
if (e.key === 'Escape' && !e.metaKey && !e.altKey && !e.ctrlKey && !e.shiftKey) {
if (e.key === 'Escape' && !hasModifierKeys(e)) {
e.preventDefault();
closeLogin();
}
}

$('#hg-login').on('click', function (ev) {
if (ev.metaKey || ev.altKey || ev.ctrlKey || ev.shiftKey) {
if (hasModifierKeys(ev)) {
return;
}

Expand Down Expand Up @@ -393,7 +404,7 @@
}

if (!isBody && allowedTags.indexOf(node.nodeName.toLowerCase()) === -1) {
while (node.childNodes.length) {
while (node.hasChildNodes()) {
node.parentNode.insertBefore(node.firstChild, node);
}

Expand Down Expand Up @@ -630,19 +641,15 @@
weasylMarkdown(fragment);
defang(fragment, true);

while (fragment.childNodes.length) {
while (fragment.hasChildNodes()) {
container.appendChild(fragment.firstChild);
}
}

function updateMarkdownPreview(input) {
if (markedLoadState === 2) {
var preview = input.nextSibling.nextSibling;

while (preview.childNodes.length) {
preview.removeChild(preview.firstChild);
}

var preview = input.nextSibling;
empty(preview);
renderMarkdown(input.value, preview);
} else {
loadMarked();
Expand All @@ -659,40 +666,16 @@

input.parentNode.insertBefore(preview, input.nextSibling);

input.addEventListener('input', updateMarkdownPreviewListener, false);
input.addEventListener('input', updateMarkdownPreviewListener);

if (input.value === '') {
input.addEventListener('focus', loadMarked, false);
input.addEventListener('focus', loadMarked);
} else {
updateMarkdownPreview(input);
}
}

function addMarkdownWarning(input) {
var helpLink = document.createElement('a');
helpLink.href = '/help/markdown';
helpLink.target = '_blank';
helpLink.appendChild(document.createTextNode('Read about Markdown!'));

var warning = document.createElement('div');
warning.className = 'bbcode-warning';
warning.appendChild(document.createTextNode('Trying to use BBCode? '));
warning.appendChild(helpLink);

function showWarning() {
warning.style.display = ATTEMPTED_BBCODE.test(input.value) ? 'block' : 'none';
}

input.addEventListener('input', showWarning, false);

showWarning();
input.parentNode.insertBefore(warning, input.nextSibling);
}

forEach(document.getElementsByClassName('markdown'), function (input) {
addMarkdownPreview(input);
addMarkdownWarning(input);
});
forEach(document.getElementsByClassName('markdown'), addMarkdownPreview);

function getCommentInfo(commentActionLink) {
var comment = commentActionLink;
Expand Down Expand Up @@ -837,8 +820,7 @@
var targetId = parseInt(targetIdField.value, 10);
var contentField = newFormContent.getElementsByClassName('form-content')[0];

// Remove the original form’s non-functional Markdown preview and warning elements
contentField.parentNode.removeChild(contentField.nextSibling);
// Remove the original form’s non-functional Markdown preview element
contentField.parentNode.removeChild(contentField.nextSibling);
contentField.value = '';

Expand All @@ -857,22 +839,21 @@
e.stopPropagation();

target.textContent = 'Reply';
target.removeEventListener('click', cancelReply, false);
target.removeEventListener('click', cancelReply);

if (children.childNodes.length === 1) {
children.removeChild(newListItem);
if (!children.hasChildNodes()) {
children.parentNode.removeChild(children);
} else {
children.removeChild(newListItem);
}

target.focus();
};

var handleShortcuts = function handleShortcuts(e) {
if (e.keyCode === 27 && !contentField.value) {
contentField.removeEventListener('keydown', handleShortcuts, false);
if (e.key === 'Escape' && !contentField.value) {
contentField.removeEventListener('keydown', handleShortcuts);
cancelReply(e);
} else if (e.keyCode === 13 && e.ctrlKey) {
} else if (e.key === 'Enter' && e.ctrlKey) {
e.preventDefault();
submitComment();
}
Expand Down Expand Up @@ -1029,8 +1010,8 @@
);

target.textContent = 'Reply';
target.removeEventListener('click', cancelReply, false);
contentField.removeEventListener('keydown', handleShortcuts, false);
target.removeEventListener('click', cancelReply);
contentField.removeEventListener('keydown', handleShortcuts);

newForm.style.display = 'none';
newForm.parentNode.insertBefore(newComment, newForm);
Expand All @@ -1043,44 +1024,45 @@
};

target.textContent = 'Cancel (esc)';
target.addEventListener('click', cancelReply, false);
contentField.addEventListener('keydown', handleShortcuts, false);
target.addEventListener('click', cancelReply);
contentField.addEventListener('keydown', handleShortcuts);

newForm.addEventListener('submit', function (e) {
submitComment();
e.preventDefault();
}, false);
});

e.preventDefault();

addMarkdownPreview(contentField);
addMarkdownWarning(contentField);

$(contentField).autosize();
contentField.focus();
}
}, false);
});

function addLocationChangerForKeyCodeAndHref(keyCode, href) {
document.addEventListener('keydown', function (e) {
if (e.ctrlKey || e.shiftKey || e.altKey || e.metaKey ||
e.target.nodeName === 'INPUT' || e.target.nodeName === 'TEXTAREA') {
return;
}
var canTriggerShortcut = e =>
['INPUT', 'SELECT', 'TEXTAREA'].indexOf(e.target.nodeName) === -1;

if (e.keyCode === keyCode) {
document.location = href;
function addShortcut(key, action) {
document.addEventListener('keydown', function (e) {
if (e.key === key && !hasModifierKeys(e) && canTriggerShortcut(e)) {
e.preventDefault();
action();
}
});
}

var clickShortcut = element => element.click.bind(element);
var focusShortcut = element => element.focus.bind(element);

(function () {
var folderNavPrev, folderNavNext;
if ((folderNavPrev = document.getElementById('folder-nav-prev'))) {
addLocationChangerForKeyCodeAndHref(37, folderNavPrev.href);
addShortcut('ArrowLeft', clickShortcut(folderNavPrev));
}
if ((folderNavNext = document.getElementById('folder-nav-next'))) {
addLocationChangerForKeyCodeAndHref(39, folderNavNext.href);
addShortcut('ArrowRight', clickShortcut(folderNavNext));
}

if (!document.getElementsByClassName) {
Expand All @@ -1097,30 +1079,21 @@

// ctrl+enter comment submit
rootCommentBox.addEventListener('keydown', function (e) {
if (e.keyCode === 13 && e.ctrlKey) {
if (e.key === 'Enter' && e.ctrlKey) {
e.preventDefault();
rootCommentForm.submit();
}
}, false);
});

// 'c' to focus comment box
document.addEventListener('keydown', function (e) {
if (e.keyCode === 67 && e.target === document.body &&
!e.ctrlKey && !e.shiftKey && !e.altKey && !e.metaKey) {
e.preventDefault();
rootCommentBox.focus();
}
});
addShortcut('c', focusShortcut(rootCommentBox));

// 'f' to favorite
var faveForm = document.getElementById('submission-favorite-form');
if (faveForm) {
document.addEventListener('keydown', function (e) {
if (e.keyCode === 70 && e.target === document.body &&
!e.ctrlKey && !e.shiftKey && !e.altKey && !e.metaKey) {
e.preventDefault();
faveForm.getElementsByTagName('button')[0].click();
}
var faveButton = document.querySelector('#submission-favorite-form button');
if (faveButton) {
addShortcut('f', () => {
faveButton.focus();
faveButton.click();
});
}

Expand Down Expand Up @@ -1231,7 +1204,7 @@
favoriteButton.classList.add('pending');

e.preventDefault();
}, false);
});
})();

// Home tabs
Expand Down
6 changes: 3 additions & 3 deletions assets/js/zxcvbn-check.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,13 @@
passwordStrength.textContent = strengthMessages[result.score];
}

passwordField.addEventListener("input", check, false);
passwordField.addEventListener("input", check);

personalFields.forEach(function (personalField) {
if (personalField.nodeName === "SELECT") {
personalField.addEventListener("change", check, false);
personalField.addEventListener("change", check);
} else {
personalField.addEventListener("input", check, false);
personalField.addEventListener("input", check);
}
});

Expand Down
Loading

0 comments on commit 6829153

Please sign in to comment.