2019-10-05 02:09:52 +02:00
|
|
|
/**
|
|
|
|
* Textile toolbar
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
import { $, $$ } from './utils/dom';
|
|
|
|
|
|
|
|
const textileSyntax = {
|
|
|
|
bold: {
|
|
|
|
action: wrapSelection,
|
2020-11-16 03:37:49 +01:00
|
|
|
options: { prefix: '*', suffix: '*', shortcutKey: 'b', type: 'inline' }
|
2019-10-05 02:09:52 +02:00
|
|
|
},
|
|
|
|
italics: {
|
|
|
|
action: wrapSelection,
|
2020-11-16 03:37:49 +01:00
|
|
|
options: { prefix: '_', suffix: '_', shortcutKey: 'i', type: 'inline' }
|
2019-10-05 02:09:52 +02:00
|
|
|
},
|
|
|
|
under: {
|
|
|
|
action: wrapSelection,
|
2020-11-16 03:37:49 +01:00
|
|
|
options: { prefix: '+', suffix: '+', shortcutKey: 'u', type: 'inline' }
|
2019-10-05 02:09:52 +02:00
|
|
|
},
|
|
|
|
spoiler: {
|
|
|
|
action: wrapSelection,
|
|
|
|
options: { prefix: '[spoiler]', suffix: '[/spoiler]', shortcutKey: 's' }
|
|
|
|
},
|
|
|
|
code: {
|
|
|
|
action: wrapSelection,
|
2020-11-16 03:37:49 +01:00
|
|
|
options: { prefix: '@', suffix: '@', shortcutKey: 'e', type: 'inline' }
|
2019-10-05 02:09:52 +02:00
|
|
|
},
|
|
|
|
strike: {
|
|
|
|
action: wrapSelection,
|
2020-11-16 03:37:49 +01:00
|
|
|
options: { prefix: '-', suffix: '-', type: 'inline' }
|
2019-10-05 02:09:52 +02:00
|
|
|
},
|
|
|
|
superscript: {
|
|
|
|
action: wrapSelection,
|
2020-11-16 03:37:49 +01:00
|
|
|
options: { prefix: '^', suffix: '^', type: 'inline' }
|
2019-10-05 02:09:52 +02:00
|
|
|
},
|
|
|
|
subscript: {
|
|
|
|
action: wrapSelection,
|
2020-11-16 03:37:49 +01:00
|
|
|
options: { prefix: '~', suffix: '~', type: 'inline' }
|
2019-10-05 02:09:52 +02:00
|
|
|
},
|
|
|
|
quote: {
|
|
|
|
action: wrapSelection,
|
|
|
|
options: { prefix: '[bq]', suffix: '[/bq]' }
|
|
|
|
},
|
|
|
|
link: {
|
|
|
|
action: insertLink,
|
|
|
|
options: { prefix: '"', suffix: '":', shortcutKey: 'l' }
|
|
|
|
},
|
|
|
|
image: {
|
|
|
|
action: insertImage,
|
|
|
|
options: { prefix: '!', suffix: '!', shortcutKey: 'k' }
|
|
|
|
},
|
|
|
|
noParse: {
|
|
|
|
action: wrapSelection,
|
|
|
|
options: { prefix: '[==', suffix: '==]' }
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
function getSelections(textarea) {
|
|
|
|
let selection = textarea.value.substring(textarea.selectionStart, textarea.selectionEnd),
|
|
|
|
leadingSpace = '',
|
|
|
|
trailingSpace = '',
|
|
|
|
caret;
|
|
|
|
|
|
|
|
// 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, textarea.selectionStart) + leadingSpace,
|
|
|
|
afterSelection: trailingSpace + textarea.value.substring(textarea.selectionEnd),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function wrapSelection(textarea, options) {
|
|
|
|
const { selectedText, beforeSelection, afterSelection } = getSelections(textarea),
|
2020-11-16 03:37:49 +01:00
|
|
|
{ text = selectedText, prefix = '', suffix = '', type } = options,
|
2019-10-05 02:09:52 +02:00
|
|
|
// For long comments, record scrollbar position to restore it later
|
|
|
|
scrollTop = textarea.scrollTop,
|
|
|
|
emptyText = text === '';
|
|
|
|
|
2021-02-25 21:03:39 +01:00
|
|
|
const newText = text;
|
2019-10-05 02:09:52 +02:00
|
|
|
|
2020-11-16 03:37:49 +01:00
|
|
|
if (type === 'inline' && newText.includes('\n')) {
|
|
|
|
textarea.value = `${beforeSelection}[${prefix}${newText}${suffix}]${afterSelection}`;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
textarea.value = `${beforeSelection}${prefix}${newText}${suffix}${afterSelection}`;
|
|
|
|
}
|
2019-10-05 02:09:52 +02:00
|
|
|
|
|
|
|
// If no text were highlighted, place the caret inside
|
|
|
|
// the formatted section, otherwise place it at the end
|
|
|
|
if (emptyText) {
|
|
|
|
textarea.selectionEnd = textarea.value.length - afterSelection.length - suffix.length;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
textarea.selectionEnd = textarea.value.length - afterSelection.length;
|
|
|
|
}
|
|
|
|
textarea.selectionStart = textarea.selectionEnd;
|
|
|
|
textarea.scrollTop = scrollTop;
|
|
|
|
}
|
|
|
|
|
|
|
|
function insertLink(textarea, options) {
|
|
|
|
let hyperlink = window.prompt('Link:');
|
|
|
|
if (!hyperlink || hyperlink === '') return;
|
|
|
|
|
|
|
|
// Change on-site link to use relative url
|
|
|
|
if (hyperlink.startsWith(window.location.origin)) hyperlink = hyperlink.substring(window.location.origin.length);
|
|
|
|
|
|
|
|
const prefix = options.prefix,
|
|
|
|
suffix = options.suffix + hyperlink;
|
|
|
|
|
|
|
|
wrapSelection(textarea, { prefix, suffix });
|
|
|
|
}
|
|
|
|
|
|
|
|
function insertImage(textarea, options) {
|
|
|
|
const hyperlink = window.prompt('Image link:');
|
|
|
|
const { prefix, suffix } = options;
|
|
|
|
|
|
|
|
if (!hyperlink || hyperlink === '') return;
|
|
|
|
|
|
|
|
wrapSelection(textarea, { text: hyperlink, prefix, suffix });
|
|
|
|
}
|
|
|
|
|
|
|
|
function clickHandler(event) {
|
|
|
|
const button = event.target.closest('.communication__toolbar__button');
|
|
|
|
if (!button) return;
|
|
|
|
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;
|
|
|
|
|
|
|
|
textileSyntax[id].action(textarea, textileSyntax[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,
|
|
|
|
key = event.key.toLowerCase();
|
|
|
|
|
|
|
|
for (const id in textileSyntax) {
|
|
|
|
if (key === textileSyntax[id].options.shortcutKey) {
|
|
|
|
textileSyntax[id].action(textarea, textileSyntax[id].options);
|
|
|
|
event.preventDefault();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function setupToolbar() {
|
|
|
|
$$('.communication__toolbar').forEach(toolbar => {
|
|
|
|
toolbar.addEventListener('click', clickHandler);
|
|
|
|
});
|
|
|
|
$$('.js-toolbar-input').forEach(textarea => {
|
|
|
|
textarea.addEventListener('keydown', shortcutHandler);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export { setupToolbar };
|