philomena/assets/js/markdowntoolbar.js

261 lines
7.5 KiB
JavaScript
Raw Normal View History

/**
* Markdown toolbar
*/
import { $, $$, showEl } from './utils/dom';
const markdownSyntax = {
bold: {
action: wrapSelection,
options: { prefix: '**', shortcutKey: 'b' }
},
italics: {
action: wrapSelection,
options: { prefix: '*', shortcutKey: 'i' }
},
under: {
action: wrapSelection,
options: { prefix: '__', shortcutKey: 'u' }
},
spoiler: {
action: wrapSelection,
options: { prefix: '||', shortcutKey: 's' }
},
code: {
action: wrapSelection,
options: { prefix: '`', shortcutKey: 'e' }
},
strike: {
action: wrapSelection,
options: { prefix: '~~' }
},
superscript: {
action: wrapSelection,
options: { prefix: '^' }
},
subscript: {
action: wrapSelection,
options: { prefix: '%' }
},
quote: {
action: wrapLines,
options: { prefix: '> ' }
},
link: {
action: insertLink,
options: { shortcutKey: 'l' }
},
image: {
action: insertLink,
options: { image: true, shortcutKey: 'k' }
},
noParse: {
action: escapeSelection,
options: { escapeChar: '\\' }
2021-09-13 22:26:01 +02:00
}
};
function getSelections(textarea, linesOnly = false) {
let { selectionStart, selectionEnd } = textarea,
selection = textarea.value.substring(selectionStart, selectionEnd),
leadingSpace = '',
2021-09-13 22:26:01 +02:00
trailingSpace = '',
caret;
if (linesOnly) {
let startNewlineIndex = 0,
2021-09-13 22:26:01 +02:00
endNewlineIndex = textarea.value.length;
const explorer = /\n/g;
while (explorer.exec(textarea.value)) {
const { lastIndex } = explorer;
if (lastIndex < selectionStart) {
startNewlineIndex = lastIndex + 1;
2021-09-13 22:26:01 +02:00
}
else if (lastIndex > selectionEnd) {
endNewlineIndex = lastIndex;
break;
}
}
selectionStart = startNewlineIndex;
selectionEnd = endNewlineIndex;
selection = textarea.value.substring(selectionStart, selectionEnd);
2021-09-13 22:26:01 +02:00
}
else {
// Deselect trailing space and line break
for (caret = selection.length - 1; caret > 0; caret--) {
if (selection[caret] !== ' ' && selection[caret] !== '\n') break;
trailingSpace = selection[caret] + trailingSpace;
}
selection = selection.substring(0, caret + 1);
// Deselect leading space and line break
for (caret = 0; caret < selection.length; caret++) {
if (selection[caret] !== ' ' && selection[caret] !== '\n') break;
leadingSpace += selection[caret];
}
selection = selection.substring(caret);
}
return {
selectedText: selection,
beforeSelection: textarea.value.substring(0, selectionStart) + leadingSpace,
2021-09-13 22:26:01 +02:00
afterSelection: trailingSpace + textarea.value.substring(selectionEnd)
};
}
function getSurroundingTwoLines(beforeText, afterText) {
// Selection typically includes the new line right before it
// therefore you need to include two lines before and after
return {
twoLinesBefore: beforeText.split('\n').slice(-2).join('\n'),
2021-09-13 22:26:01 +02:00
twoLinesAfter: afterText.split('\n').slice(0, 2).join('\n')
};
}
function transformSelection(textarea, transformer, eachLine) {
const { selectedText, beforeSelection, afterSelection } = getSelections(textarea, eachLine),
// For long comments, record scrollbar position to restore it later
{ scrollTop } = textarea;
const { newText, caretOffset } = transformer(selectedText, beforeSelection, afterSelection);
textarea.value = beforeSelection + newText + afterSelection;
const newSelectionStart = caretOffset >= 1
? beforeSelection.length + caretOffset
: textarea.value.length - afterSelection.length - caretOffset;
textarea.selectionStart = newSelectionStart;
textarea.selectionEnd = newSelectionStart;
textarea.scrollTop = scrollTop;
textarea.dispatchEvent(new Event('keydown'));
}
function insertLink(textarea, options) {
let hyperlink = window.prompt(options.image ? 'Image link:' : 'Link:');
if (!hyperlink || hyperlink === '') return;
// Change on-site link to use relative url
if (!options.image && hyperlink.startsWith(window.location.origin)) {
hyperlink = hyperlink.substring(window.location.origin.length);
}
const prefix = options.image ? '![' : '[',
2021-09-13 22:26:01 +02:00
suffix = `](${escapeHyperlink(hyperlink)})`;
wrapSelection(textarea, { prefix, suffix });
}
function wrapSelection(textarea, options) {
transformSelection(textarea, selectedText => {
const { text = selectedText, prefix = '', suffix = options.prefix } = options,
emptyText = text === '';
let newText = text;
if (!emptyText) {
newText = text.replace(/(\n{2,})/g, match => {
return suffix + match + prefix;
});
}
return {
newText: prefix + newText + suffix,
2021-09-13 22:26:01 +02:00
caretOffset: emptyText ? prefix.length : -suffix.length
};
2021-09-13 22:26:01 +02:00
});
}
function wrapLines(textarea, options) {
transformSelection(textarea, (selectedText, before, after) => {
const { text = selectedText, prefix = '', suffix = '' } = options,
{ twoLinesBefore, twoLinesAfter } = getSurroundingTwoLines(before, after),
emptyText = text === '';
let newText = prefix;
if (!emptyText) {
2021-09-13 22:26:01 +02:00
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
2021-09-13 22:26:01 +02:00
if (isNotBlank(twoLinesBefore)) newText = `\n${newText}`;
if (isNotBlank(twoLinesAfter)) newText += '\n';
return { newText, caretOffset: newText.length - suffix.length };
2021-09-13 22:26:01 +02:00
});
}
function escapeSelection(textarea, options) {
transformSelection(textarea, selectedText => {
const { text = selectedText } = options,
emptyText = text === '';
if (emptyText) return;
2021-09-13 22:26:01 +02:00
const newText = text.replace(/([[\]()*_`\\~<>^])/g, '\\$1').replace(/\|\|/g, '\\|\\|');
return {
2021-09-13 22:26:01 +02:00
newText,
caretOffset: newText.length
};
2021-09-13 22:26:01 +02:00
});
}
function escapeHyperlink(url) {
return typeof url === 'string' ? url.replace(/([()])/g, '\\$1') : url;
}
function isNotBlank(string) {
return /\S/.test(string);
}
function clickHandler(event) {
const button = event.target.closest('.communication__toolbar__button');
if (!button) return;
2021-09-13 22:26:01 +02:00
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),
2021-09-13 22:26:01 +02:00
id = button.dataset.syntaxId;
markdownSyntax[id].action(textarea, markdownSyntax[id].options);
textarea.focus();
}
function shortcutHandler(event) {
if (!event.ctrlKey || (window.navigator.platform === 'MacIntel' && !event.metaKey) || event.shiftKey || event.altKey) return;
const textarea = event.target,
2021-09-13 22:26:01 +02:00
key = event.key.toLowerCase();
for (const id in markdownSyntax) {
if (key === markdownSyntax[id].options.shortcutKey) {
markdownSyntax[id].action(textarea, markdownSyntax[id].options);
event.preventDefault();
}
}
}
function setupToolbar() {
$$('.communication__toolbar').forEach(toolbar => {
toolbar.addEventListener('click', clickHandler);
});
$$('.js-toolbar-input').forEach(textarea => {
textarea.addEventListener('keydown', shortcutHandler);
});
// Transform non-JS basic editor to two-column layout with preview
$$('.js-preview-input-wrapper').forEach(wrapper => {
wrapper.classList.remove('block__column--full');
wrapper.classList.add('block__column--half');
});
$$('.js-preview-output-wrapper').forEach(wrapper => {
2021-09-13 22:26:01 +02:00
showEl(wrapper);
});
}
export { setupToolbar };