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,7 +52,7 @@ const markdownSyntax = {
noParse: { noParse: {
action: escapeSelection, action: escapeSelection,
options: { escapeChar: '\\' } options: { escapeChar: '\\' }
}, }
}; };
function getSelections(textarea, linesOnly = false) { function getSelections(textarea, linesOnly = false) {
@ -64,13 +64,14 @@ function getSelections(textarea, linesOnly = false) {
if (linesOnly) { if (linesOnly) {
let startNewlineIndex = 0, let startNewlineIndex = 0,
endNewlineIndex = textarea.value.length, endNewlineIndex = textarea.value.length;
explorer = /\n/g; const explorer = /\n/g;
while (explorer.exec(textarea.value)) { while (explorer.exec(textarea.value)) {
const { lastIndex } = explorer; const { lastIndex } = explorer;
if (lastIndex < selectionStart) { if (lastIndex < selectionStart) {
startNewlineIndex = lastIndex + 1; startNewlineIndex = lastIndex + 1;
} else if (lastIndex > selectionEnd) { }
else if (lastIndex > selectionEnd) {
endNewlineIndex = lastIndex; endNewlineIndex = lastIndex;
break; break;
} }
@ -79,7 +80,8 @@ function getSelections(textarea, linesOnly = false) {
selectionStart = startNewlineIndex; selectionStart = startNewlineIndex;
selectionEnd = endNewlineIndex; selectionEnd = endNewlineIndex;
selection = textarea.value.substring(selectionStart, selectionEnd); selection = textarea.value.substring(selectionStart, selectionEnd);
} else { }
else {
// Deselect trailing space and line break // Deselect trailing space and line break
for (caret = selection.length - 1; caret > 0; caret--) { for (caret = selection.length - 1; caret > 0; caret--) {
if (selection[caret] !== ' ' && selection[caret] !== '\n') break; if (selection[caret] !== ' ' && selection[caret] !== '\n') break;
@ -98,7 +100,7 @@ function getSelections(textarea, linesOnly = false) {
return { return {
selectedText: selection, selectedText: selection,
beforeSelection: textarea.value.substring(0, selectionStart) + leadingSpace, 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 // therefore you need to include two lines before and after
return { return {
twoLinesBefore: beforeText.split('\n').slice(-2).join('\n'), 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) { function transformSelection(textarea, transformer, eachLine) {
@ -140,7 +142,7 @@ function insertLink(textarea, options) {
} }
const prefix = options.image ? '![' : '[', const prefix = options.image ? '![' : '[',
suffix = '](' + escapeHyperlink(hyperlink) + ')'; suffix = `](${escapeHyperlink(hyperlink)})`;
wrapSelection(textarea, { prefix, suffix }); wrapSelection(textarea, { prefix, suffix });
} }
@ -159,9 +161,9 @@ function wrapSelection(textarea, options) {
return { return {
newText: prefix + newText + suffix, newText: prefix + newText + suffix,
caretOffset: emptyText ? prefix.length : -suffix.length, caretOffset: emptyText ? prefix.length : -suffix.length
}; };
}) });
} }
function wrapLines(textarea, options) { function wrapLines(textarea, options) {
@ -172,17 +174,18 @@ function wrapLines(textarea, options) {
let newText = prefix; let newText = prefix;
if (!emptyText) { if (!emptyText) {
newText = text.split(/\n/g).map(line => prefix + (line.trim()) + suffix).join('\n'); newText = text.split(/\n/g).map(line => prefix + line.trim() + suffix).join('\n');
} else { }
else {
newText += suffix; newText += suffix;
} }
// Add blank lines before/after if surrounding line are not empty // 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'; if (isNotBlank(twoLinesAfter)) newText += '\n';
return { newText, caretOffset: newText.length - suffix.length }; return { newText, caretOffset: newText.length - suffix.length };
}) });
} }
function escapeSelection(textarea, options) { function escapeSelection(textarea, options) {
@ -192,13 +195,13 @@ function escapeSelection(textarea, options) {
if (emptyText) return; if (emptyText) return;
let newText = text.replace(/([\[\]()*_`\\~<>^])/g, '\\$1').replace(/\|\|/g, '\\|\\|'); const newText = text.replace(/([[\]()*_`\\~<>^])/g, '\\$1').replace(/\|\|/g, '\\|\\|');
return { return {
newText: newText, newText,
caretOffset: newText.length, caretOffset: newText.length
}; };
}) });
} }
function escapeHyperlink(url) { function escapeHyperlink(url) {
@ -250,7 +253,7 @@ function setupToolbar() {
wrapper.classList.add('block__column--half'); wrapper.classList.add('block__column--half');
}); });
$$('.js-preview-output-wrapper').forEach(wrapper => { $$('.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) { function handleError(response) {
const errorMessage = '<div>Preview failed to load!</div>'; const errorMessage = '<div>Preview failed to load!</div>';
if (!response.ok){ if (!response.ok) {
return errorMessage; return errorMessage;
} }
@ -24,7 +24,7 @@ function commentReply(user, url, textarea, quote) {
if (newval && /\n$/.test(newval)) newval += '\n'; if (newval && /\n$/.test(newval)) newval += '\n';
newval += `${text}\n`; newval += `${text}\n`;
if (quote){ if (quote) {
newval += `[bq="${user.replace('"', '\'')}"] ${quote} [/bq]\n`; newval += `[bq="${user.replace('"', '\'')}"] ${quote} [/bq]\n`;
} }
@ -44,12 +44,12 @@ function commentReply(user, url, textarea, quote) {
let previewAbortController = null; let previewAbortController = null;
function getPreview(body, anonymous, previewLoading, previewContent) { function getPreview(body, anonymous, previewLoading, previewContent) {
let path = '/posts/preview'; const path = '/posts/preview';
if (typeof body !== 'string') return; if (typeof body !== 'string') return;
const trimmedBody = body.trim(); const trimmedBody = body.trim();
if (trimmedBody.length < 1){ if (trimmedBody.length < 1) {
previewContent.innerHTML = ''; previewContent.innerHTML = '';
return; return;
} }
@ -95,7 +95,7 @@ function resizeTextarea(e) {
function setupPreviews() { function setupPreviews() {
let textarea = document.querySelector('.js-preview-input'); let textarea = document.querySelector('.js-preview-input');
if (!textarea){ if (!textarea) {
textarea = document.querySelector('.js-preview-description'); textarea = document.querySelector('.js-preview-description');
} }
@ -103,7 +103,7 @@ function setupPreviews() {
const previewContent = document.querySelector('.communication-preview__content'); const previewContent = document.querySelector('.communication-preview__content');
const previewAnon = document.querySelector('.js-preview-anonymous') || false; const previewAnon = document.querySelector('.js-preview-anonymous') || false;
if (!textarea || !previewContent){ if (!textarea || !previewContent) {
return; return;
} }
@ -129,7 +129,7 @@ function setupPreviews() {
previewAnon && previewAnon.addEventListener('click', updatePreview); previewAnon && previewAnon.addEventListener('click', updatePreview);
document.addEventListener('click', event => { 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'); const link = event.target.closest('.post-reply');
commentReply(link.dataset.author, link.getAttribute('href'), textarea, link.dataset.post); commentReply(link.dataset.author, link.getAttribute('href'), textarea, link.dataset.post);
event.preventDefault(); event.preventDefault();