fix code formatting

This commit is contained in:
SeinopSys 2021-09-13 22:26:01 +02:00
parent 71fa95e462
commit 9ac7abdf54
No known key found for this signature in database
GPG key ID: 9BFB053C1BA6C5C4
2 changed files with 34 additions and 31 deletions

View file

@ -52,25 +52,26 @@ const markdownSyntax = {
noParse: {
action: escapeSelection,
options: { escapeChar: '\\' }
},
}
};
function getSelections(textarea, linesOnly = false) {
let { selectionStart, selectionEnd } = textarea,
selection = textarea.value.substring(selectionStart, selectionEnd),
leadingSpace = '',
trailingSpace = '',
trailingSpace = '',
caret;
if (linesOnly) {
let startNewlineIndex = 0,
endNewlineIndex = textarea.value.length,
explorer = /\n/g;
endNewlineIndex = textarea.value.length;
const explorer = /\n/g;
while (explorer.exec(textarea.value)) {
const { lastIndex } = explorer;
if (lastIndex < selectionStart) {
startNewlineIndex = lastIndex + 1;
} else if (lastIndex > selectionEnd) {
}
else if (lastIndex > selectionEnd) {
endNewlineIndex = lastIndex;
break;
}
@ -79,7 +80,8 @@ function getSelections(textarea, linesOnly = false) {
selectionStart = startNewlineIndex;
selectionEnd = endNewlineIndex;
selection = textarea.value.substring(selectionStart, selectionEnd);
} else {
}
else {
// Deselect trailing space and line break
for (caret = selection.length - 1; caret > 0; caret--) {
if (selection[caret] !== ' ' && selection[caret] !== '\n') break;
@ -98,7 +100,7 @@ function getSelections(textarea, linesOnly = false) {
return {
selectedText: selection,
beforeSelection: textarea.value.substring(0, selectionStart) + leadingSpace,
afterSelection: trailingSpace + textarea.value.substring(selectionEnd),
afterSelection: trailingSpace + textarea.value.substring(selectionEnd)
};
}
@ -107,8 +109,8 @@ function getSurroundingTwoLines(beforeText, afterText) {
// therefore you need to include two lines before and after
return {
twoLinesBefore: beforeText.split('\n').slice(-2).join('\n'),
twoLinesAfter: afterText.split('\n').slice(0, 2).join('\n'),
}
twoLinesAfter: afterText.split('\n').slice(0, 2).join('\n')
};
}
function transformSelection(textarea, transformer, eachLine) {
@ -140,7 +142,7 @@ function insertLink(textarea, options) {
}
const prefix = options.image ? '![' : '[',
suffix = '](' + escapeHyperlink(hyperlink) + ')';
suffix = `](${escapeHyperlink(hyperlink)})`;
wrapSelection(textarea, { prefix, suffix });
}
@ -159,9 +161,9 @@ function wrapSelection(textarea, options) {
return {
newText: prefix + newText + suffix,
caretOffset: emptyText ? prefix.length : -suffix.length,
caretOffset: emptyText ? prefix.length : -suffix.length
};
})
});
}
function wrapLines(textarea, options) {
@ -172,17 +174,18 @@ function wrapLines(textarea, options) {
let newText = prefix;
if (!emptyText) {
newText = text.split(/\n/g).map(line => prefix + (line.trim()) + suffix).join('\n');
} else {
newText = text.split(/\n/g).map(line => prefix + line.trim() + suffix).join('\n');
}
else {
newText += suffix;
}
// Add blank lines before/after if surrounding line are not empty
if (isNotBlank(twoLinesBefore)) newText = '\n' + newText;
if (isNotBlank(twoLinesBefore)) newText = `\n${newText}`;
if (isNotBlank(twoLinesAfter)) newText += '\n';
return { newText, caretOffset: newText.length - suffix.length };
})
});
}
function escapeSelection(textarea, options) {
@ -192,13 +195,13 @@ function escapeSelection(textarea, options) {
if (emptyText) return;
let newText = text.replace(/([\[\]()*_`\\~<>^])/g, '\\$1').replace(/\|\|/g, '\\|\\|');
const newText = text.replace(/([[\]()*_`\\~<>^])/g, '\\$1').replace(/\|\|/g, '\\|\\|');
return {
newText: newText,
caretOffset: newText.length,
newText,
caretOffset: newText.length
};
})
});
}
function escapeHyperlink(url) {
@ -212,12 +215,12 @@ function isNotBlank(string) {
function clickHandler(event) {
const button = event.target.closest('.communication__toolbar__button');
if (!button) return;
const toolbar = button.closest('.communication__toolbar'),
const toolbar = button.closest('.communication__toolbar'),
// There may be multiple toolbars present on the page,
// in the case of image pages with description edit active
// we target the textarea that shares the same parent as the toolabr
textarea = $('.js-toolbar-input', toolbar.parentNode),
id = button.dataset.syntaxId;
id = button.dataset.syntaxId;
markdownSyntax[id].action(textarea, markdownSyntax[id].options);
textarea.focus();
@ -226,7 +229,7 @@ function clickHandler(event) {
function shortcutHandler(event) {
if (!event.ctrlKey || (window.navigator.platform === 'MacIntel' && !event.metaKey) || event.shiftKey || event.altKey) return;
const textarea = event.target,
key = event.key.toLowerCase();
key = event.key.toLowerCase();
for (const id in markdownSyntax) {
if (key === markdownSyntax[id].options.shortcutKey) {
@ -250,7 +253,7 @@ function setupToolbar() {
wrapper.classList.add('block__column--half');
});
$$('.js-preview-output-wrapper').forEach(wrapper => {
showEl(wrapper)
showEl(wrapper);
});
}

View file

@ -10,7 +10,7 @@ import { hideEl, showEl } from './utils/dom.js';
function handleError(response) {
const errorMessage = '<div>Preview failed to load!</div>';
if (!response.ok){
if (!response.ok) {
return errorMessage;
}
@ -24,7 +24,7 @@ function commentReply(user, url, textarea, quote) {
if (newval && /\n$/.test(newval)) newval += '\n';
newval += `${text}\n`;
if (quote){
if (quote) {
newval += `[bq="${user.replace('"', '\'')}"] ${quote} [/bq]\n`;
}
@ -44,12 +44,12 @@ function commentReply(user, url, textarea, quote) {
let previewAbortController = null;
function getPreview(body, anonymous, previewLoading, previewContent) {
let path = '/posts/preview';
const path = '/posts/preview';
if (typeof body !== 'string') return;
const trimmedBody = body.trim();
if (trimmedBody.length < 1){
if (trimmedBody.length < 1) {
previewContent.innerHTML = '';
return;
}
@ -95,7 +95,7 @@ function resizeTextarea(e) {
function setupPreviews() {
let textarea = document.querySelector('.js-preview-input');
if (!textarea){
if (!textarea) {
textarea = document.querySelector('.js-preview-description');
}
@ -103,7 +103,7 @@ function setupPreviews() {
const previewContent = document.querySelector('.communication-preview__content');
const previewAnon = document.querySelector('.js-preview-anonymous') || false;
if (!textarea || !previewContent){
if (!textarea || !previewContent) {
return;
}
@ -129,7 +129,7 @@ function setupPreviews() {
previewAnon && previewAnon.addEventListener('click', updatePreview);
document.addEventListener('click', event => {
if (event.target && event.target.closest('.post-reply')){
if (event.target && event.target.closest('.post-reply')) {
const link = event.target.closest('.post-reply');
commentReply(link.dataset.author, link.getAttribute('href'), textarea, link.dataset.post);
event.preventDefault();