2021-09-13 01:19:00 +02:00
|
|
|
/**
|
|
|
|
* Markdown toolbar
|
|
|
|
*/
|
|
|
|
|
2021-09-29 20:00:46 +02:00
|
|
|
import { $, $$ } from './utils/dom';
|
2021-09-13 01:19:00 +02:00
|
|
|
|
2024-08-28 23:52:01 +02:00
|
|
|
// List of options provided to the syntax handler function.
|
2024-08-29 00:19:27 +02:00
|
|
|
interface SyntaxHandlerOptions {
|
2024-08-28 23:52:01 +02:00
|
|
|
prefix: string;
|
|
|
|
shortcutKeyCode: number;
|
|
|
|
suffix: string;
|
|
|
|
prefixMultiline: string;
|
|
|
|
suffixMultiline: string;
|
|
|
|
singleWrap: boolean;
|
|
|
|
escapeChar: string;
|
|
|
|
image: boolean;
|
|
|
|
text: string;
|
2024-08-29 00:19:27 +02:00
|
|
|
}
|
2024-08-28 23:52:01 +02:00
|
|
|
|
2024-08-29 00:19:27 +02:00
|
|
|
interface SyntaxHandler {
|
2024-08-28 23:52:01 +02:00
|
|
|
action: (textarea: HTMLTextAreaElement, options: Partial<SyntaxHandlerOptions>) => void;
|
|
|
|
options: Partial<SyntaxHandlerOptions>;
|
2024-08-29 00:19:27 +02:00
|
|
|
}
|
2024-08-28 23:52:01 +02:00
|
|
|
|
|
|
|
const markdownSyntax: Record<string, SyntaxHandler> = {
|
2021-09-13 01:19:00 +02:00
|
|
|
bold: {
|
|
|
|
action: wrapSelection,
|
2024-07-22 04:08:38 +02:00
|
|
|
options: { prefix: '**', shortcutKeyCode: 66 },
|
2021-09-13 01:19:00 +02:00
|
|
|
},
|
|
|
|
italics: {
|
|
|
|
action: wrapSelection,
|
2024-07-22 04:08:38 +02:00
|
|
|
options: { prefix: '*', shortcutKeyCode: 73 },
|
2021-09-13 01:19:00 +02:00
|
|
|
},
|
|
|
|
under: {
|
|
|
|
action: wrapSelection,
|
2024-07-22 04:08:38 +02:00
|
|
|
options: { prefix: '__', shortcutKeyCode: 85 },
|
2021-09-13 01:19:00 +02:00
|
|
|
},
|
|
|
|
spoiler: {
|
|
|
|
action: wrapSelection,
|
2024-07-22 04:08:38 +02:00
|
|
|
options: { prefix: '||', shortcutKeyCode: 83 },
|
2021-09-13 01:19:00 +02:00
|
|
|
},
|
|
|
|
code: {
|
2021-09-29 20:00:46 +02:00
|
|
|
action: wrapSelectionOrLines,
|
|
|
|
options: {
|
|
|
|
prefix: '`',
|
|
|
|
suffix: '`',
|
|
|
|
prefixMultiline: '```\n',
|
|
|
|
suffixMultiline: '\n```',
|
|
|
|
singleWrap: true,
|
2024-07-22 04:08:38 +02:00
|
|
|
shortcutKeyCode: 69,
|
2024-07-04 02:27:59 +02:00
|
|
|
},
|
2021-09-13 01:19:00 +02:00
|
|
|
},
|
|
|
|
strike: {
|
|
|
|
action: wrapSelection,
|
2024-07-04 02:27:59 +02:00
|
|
|
options: { prefix: '~~' },
|
2021-09-13 01:19:00 +02:00
|
|
|
},
|
|
|
|
superscript: {
|
|
|
|
action: wrapSelection,
|
2024-07-04 02:27:59 +02:00
|
|
|
options: { prefix: '^' },
|
2021-09-13 01:19:00 +02:00
|
|
|
},
|
|
|
|
subscript: {
|
|
|
|
action: wrapSelection,
|
2024-07-04 02:27:59 +02:00
|
|
|
options: { prefix: '%' },
|
2021-09-13 01:19:00 +02:00
|
|
|
},
|
|
|
|
quote: {
|
|
|
|
action: wrapLines,
|
2024-07-04 02:27:59 +02:00
|
|
|
options: { prefix: '> ' },
|
2021-09-13 01:19:00 +02:00
|
|
|
},
|
|
|
|
link: {
|
|
|
|
action: insertLink,
|
2024-07-22 04:08:38 +02:00
|
|
|
options: { shortcutKeyCode: 76 },
|
2021-09-13 01:19:00 +02:00
|
|
|
},
|
|
|
|
image: {
|
|
|
|
action: insertLink,
|
2024-07-22 04:08:38 +02:00
|
|
|
options: { image: true, shortcutKeyCode: 75 },
|
2021-09-13 01:19:00 +02:00
|
|
|
},
|
2021-09-23 20:31:59 +02:00
|
|
|
escape: {
|
2021-09-13 01:19:00 +02:00
|
|
|
action: escapeSelection,
|
2024-07-04 02:27:59 +02:00
|
|
|
options: { escapeChar: '\\' },
|
|
|
|
},
|
2021-09-13 01:19:00 +02:00
|
|
|
};
|
|
|
|
|
2024-08-29 00:19:27 +02:00
|
|
|
interface SelectionResult {
|
2024-08-28 23:52:01 +02:00
|
|
|
processLinesOnly: boolean;
|
|
|
|
selectedText: string;
|
|
|
|
beforeSelection: string;
|
|
|
|
afterSelection: string;
|
2024-08-29 00:19:27 +02:00
|
|
|
}
|
2024-08-28 23:52:01 +02:00
|
|
|
|
|
|
|
function getSelections(textarea: HTMLTextAreaElement, linesOnly: RegExp | boolean = false): SelectionResult {
|
2021-09-13 01:19:00 +02:00
|
|
|
let { selectionStart, selectionEnd } = textarea,
|
2024-07-04 02:27:59 +02:00
|
|
|
selection = textarea.value.substring(selectionStart, selectionEnd),
|
|
|
|
leadingSpace = '',
|
|
|
|
trailingSpace = '',
|
2024-08-28 23:52:01 +02:00
|
|
|
caret: number;
|
2021-09-13 01:19:00 +02:00
|
|
|
|
2021-09-29 20:00:46 +02:00
|
|
|
const processLinesOnly = linesOnly instanceof RegExp ? linesOnly.test(selection) : linesOnly;
|
2024-08-28 23:52:01 +02:00
|
|
|
|
2021-09-29 20:00:46 +02:00
|
|
|
if (processLinesOnly) {
|
|
|
|
const explorer = /\n/g;
|
2021-09-13 01:19:00 +02:00
|
|
|
let startNewlineIndex = 0,
|
2024-07-04 02:27:59 +02:00
|
|
|
endNewlineIndex = textarea.value.length;
|
2021-09-13 01:19:00 +02:00
|
|
|
while (explorer.exec(textarea.value)) {
|
|
|
|
const { lastIndex } = explorer;
|
2021-09-29 20:00:46 +02:00
|
|
|
if (lastIndex <= selectionStart) {
|
|
|
|
startNewlineIndex = lastIndex;
|
2024-07-04 02:27:59 +02:00
|
|
|
} else if (lastIndex > selectionEnd) {
|
2021-09-29 20:00:46 +02:00
|
|
|
endNewlineIndex = lastIndex - 1;
|
2021-09-13 01:19:00 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
selectionStart = startNewlineIndex;
|
2021-09-29 20:00:46 +02:00
|
|
|
const startRemovedValue = textarea.value.substring(selectionStart);
|
|
|
|
const startsWithBlankString = startRemovedValue.match(/^[\s\n]+/);
|
|
|
|
if (startsWithBlankString) {
|
|
|
|
// Offset the selection start to the first non-blank line's first non-blank character, since
|
|
|
|
// Some browsers treat selection up to the start of the line as including the end of the
|
|
|
|
// previous line
|
|
|
|
selectionStart += startsWithBlankString[0].length;
|
|
|
|
}
|
2021-09-13 01:19:00 +02:00
|
|
|
selectionEnd = endNewlineIndex;
|
|
|
|
selection = textarea.value.substring(selectionStart, selectionEnd);
|
2024-07-04 02:27:59 +02:00
|
|
|
} else {
|
2021-09-13 01:19:00 +02:00
|
|
|
// 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 {
|
2021-09-29 20:00:46 +02:00
|
|
|
processLinesOnly,
|
2021-09-13 01:19:00 +02:00
|
|
|
selectedText: selection,
|
|
|
|
beforeSelection: textarea.value.substring(0, selectionStart) + leadingSpace,
|
2024-07-04 02:27:59 +02:00
|
|
|
afterSelection: trailingSpace + textarea.value.substring(selectionEnd),
|
2021-09-13 01:19:00 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2024-08-29 00:19:27 +02:00
|
|
|
interface TransformResult {
|
2024-08-28 23:52:01 +02:00
|
|
|
newText: string;
|
|
|
|
caretOffset: number;
|
2024-08-29 00:19:27 +02:00
|
|
|
}
|
2024-08-28 23:52:01 +02:00
|
|
|
|
|
|
|
type TransformCallback = (selectedText: string, processLinesOnly: boolean) => TransformResult;
|
|
|
|
|
|
|
|
function transformSelection(
|
|
|
|
textarea: HTMLTextAreaElement,
|
|
|
|
transformer: TransformCallback,
|
|
|
|
eachLine: RegExp | boolean = false,
|
|
|
|
) {
|
2021-09-29 20:00:46 +02:00
|
|
|
const { selectedText, beforeSelection, afterSelection, processLinesOnly } = getSelections(textarea, eachLine),
|
2024-07-04 02:27:59 +02:00
|
|
|
// For long comments, record scrollbar position to restore it later
|
|
|
|
{ scrollTop } = textarea;
|
2021-09-13 01:19:00 +02:00
|
|
|
|
2021-09-29 20:00:46 +02:00
|
|
|
const { newText, caretOffset } = transformer(selectedText, processLinesOnly);
|
2021-09-13 01:19:00 +02:00
|
|
|
|
|
|
|
textarea.value = beforeSelection + newText + afterSelection;
|
|
|
|
|
2024-07-04 02:27:59 +02:00
|
|
|
const newSelectionStart =
|
|
|
|
caretOffset >= 1
|
|
|
|
? beforeSelection.length + caretOffset
|
|
|
|
: textarea.value.length - afterSelection.length - caretOffset;
|
2021-09-13 01:19:00 +02:00
|
|
|
|
|
|
|
textarea.selectionStart = newSelectionStart;
|
|
|
|
textarea.selectionEnd = newSelectionStart;
|
|
|
|
textarea.scrollTop = scrollTop;
|
2021-09-29 20:00:46 +02:00
|
|
|
// Needed for automatic textarea resizing
|
|
|
|
textarea.dispatchEvent(new Event('change'));
|
2021-09-13 01:19:00 +02:00
|
|
|
}
|
|
|
|
|
2024-08-28 23:52:01 +02:00
|
|
|
function insertLink(textarea: HTMLTextAreaElement, options: Partial<SyntaxHandlerOptions>) {
|
2021-09-13 01:19:00 +02:00
|
|
|
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 ? '![' : '[',
|
2024-07-04 02:27:59 +02:00
|
|
|
suffix = `](${hyperlink})`;
|
2021-09-13 01:19:00 +02:00
|
|
|
|
|
|
|
wrapSelection(textarea, { prefix, suffix });
|
|
|
|
}
|
|
|
|
|
2024-08-28 23:52:01 +02:00
|
|
|
function wrapSelection(textarea: HTMLTextAreaElement, options: Partial<SyntaxHandlerOptions>) {
|
|
|
|
transformSelection(textarea, (selectedText: string): TransformResult => {
|
2021-09-13 01:19:00 +02:00
|
|
|
const { text = selectedText, prefix = '', suffix = options.prefix } = options,
|
2024-07-04 02:27:59 +02:00
|
|
|
emptyText = text === '';
|
2024-08-28 23:52:01 +02:00
|
|
|
|
2021-09-13 01:19:00 +02:00
|
|
|
let newText = text;
|
|
|
|
|
|
|
|
if (!emptyText) {
|
|
|
|
newText = text.replace(/(\n{2,})/g, match => {
|
|
|
|
return suffix + match + prefix;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-10-09 21:58:07 +02:00
|
|
|
newText = prefix + newText + suffix;
|
2021-09-29 20:00:46 +02:00
|
|
|
|
2021-09-13 01:19:00 +02:00
|
|
|
return {
|
2021-09-29 20:00:46 +02:00
|
|
|
newText,
|
2024-07-04 02:27:59 +02:00
|
|
|
caretOffset: emptyText ? prefix.length : newText.length,
|
2021-09-13 01:19:00 +02:00
|
|
|
};
|
2021-09-13 22:26:01 +02:00
|
|
|
});
|
2021-09-13 01:19:00 +02:00
|
|
|
}
|
|
|
|
|
2024-08-28 23:52:01 +02:00
|
|
|
function wrapLines(
|
|
|
|
textarea: HTMLTextAreaElement,
|
|
|
|
options: Partial<SyntaxHandlerOptions>,
|
|
|
|
eachLine: RegExp | boolean = true,
|
|
|
|
) {
|
2024-07-04 02:27:59 +02:00
|
|
|
transformSelection(
|
|
|
|
textarea,
|
2024-08-28 23:52:01 +02:00
|
|
|
(selectedText: string, processLinesOnly: boolean): TransformResult => {
|
2024-07-04 02:27:59 +02:00
|
|
|
const { text = selectedText, singleWrap = false } = options,
|
|
|
|
prefix = (processLinesOnly && options.prefixMultiline) || options.prefix || '',
|
|
|
|
suffix = (processLinesOnly && options.suffixMultiline) || options.suffix || '',
|
|
|
|
emptyText = text === '';
|
|
|
|
let newText = singleWrap
|
|
|
|
? prefix + text.trim() + suffix
|
|
|
|
: text
|
|
|
|
.split(/\n/g)
|
|
|
|
.map(line => prefix + line.trim() + suffix)
|
|
|
|
.join('\n');
|
|
|
|
|
|
|
|
// Force a space at the end of lines with only blockquote markers
|
|
|
|
newText = newText.replace(/^((?:>\s+)*)>$/gm, '$1> ');
|
|
|
|
|
|
|
|
return { newText, caretOffset: emptyText ? prefix.length : newText.length };
|
|
|
|
},
|
|
|
|
eachLine,
|
|
|
|
);
|
2021-09-29 20:00:46 +02:00
|
|
|
}
|
2021-09-13 01:19:00 +02:00
|
|
|
|
2024-08-28 23:52:01 +02:00
|
|
|
function wrapSelectionOrLines(textarea: HTMLTextAreaElement, options: Partial<SyntaxHandlerOptions>) {
|
2021-09-29 20:00:46 +02:00
|
|
|
wrapLines(textarea, options, /\n/);
|
2021-09-13 01:19:00 +02:00
|
|
|
}
|
|
|
|
|
2024-08-28 23:52:01 +02:00
|
|
|
function escapeSelection(textarea: HTMLTextAreaElement, options: Partial<SyntaxHandlerOptions>) {
|
|
|
|
transformSelection(textarea, (selectedText: string): TransformResult => {
|
2021-09-13 01:19:00 +02:00
|
|
|
const { text = selectedText } = options,
|
2024-07-04 02:27:59 +02:00
|
|
|
emptyText = text === '';
|
2021-09-13 01:19:00 +02:00
|
|
|
|
2024-08-29 00:20:22 +02:00
|
|
|
// Nothing to escape, so do nothing
|
2024-08-28 23:52:01 +02:00
|
|
|
if (emptyText) {
|
|
|
|
return {
|
|
|
|
newText: text,
|
|
|
|
caretOffset: text.length,
|
|
|
|
};
|
|
|
|
}
|
2021-09-13 01:19:00 +02:00
|
|
|
|
2021-09-23 20:31:59 +02:00
|
|
|
const newText = text.replace(/([*_[\]()^`%\\~<>#|])/g, '\\$1');
|
2021-09-13 01:19:00 +02:00
|
|
|
|
|
|
|
return {
|
2021-09-13 22:26:01 +02:00
|
|
|
newText,
|
2024-07-04 02:27:59 +02:00
|
|
|
caretOffset: newText.length,
|
2021-09-13 01:19:00 +02:00
|
|
|
};
|
2021-09-13 22:26:01 +02:00
|
|
|
});
|
2021-09-13 01:19:00 +02:00
|
|
|
}
|
|
|
|
|
2024-08-28 23:52:01 +02:00
|
|
|
function clickHandler(event: MouseEvent) {
|
|
|
|
if (!(event.target instanceof HTMLElement)) return;
|
|
|
|
|
2024-08-29 00:21:14 +02:00
|
|
|
const button = event.target.closest<HTMLElement>('.communication__toolbar__button');
|
2024-08-28 23:52:01 +02:00
|
|
|
const toolbar = button?.closest<HTMLElement>('.communication__toolbar');
|
|
|
|
|
|
|
|
if (!button || !toolbar?.parentElement) return;
|
|
|
|
|
|
|
|
// 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 toolbar
|
|
|
|
const textarea = $<HTMLTextAreaElement>('.js-toolbar-input', toolbar.parentElement),
|
2024-07-04 02:27:59 +02:00
|
|
|
id = button.dataset.syntaxId;
|
2021-09-13 01:19:00 +02:00
|
|
|
|
2024-08-28 23:52:01 +02:00
|
|
|
if (!textarea || !id) return;
|
|
|
|
|
2021-09-13 01:19:00 +02:00
|
|
|
markdownSyntax[id].action(textarea, markdownSyntax[id].options);
|
|
|
|
textarea.focus();
|
|
|
|
}
|
|
|
|
|
2024-08-28 23:52:01 +02:00
|
|
|
function canAcceptShortcut(event: KeyboardEvent): boolean {
|
|
|
|
let ctrl: boolean, otherModifier: boolean;
|
2024-07-04 02:27:59 +02:00
|
|
|
|
|
|
|
switch (window.navigator.platform) {
|
|
|
|
case 'MacIntel':
|
|
|
|
ctrl = event.metaKey;
|
|
|
|
otherModifier = event.ctrlKey || event.shiftKey || event.altKey;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ctrl = event.ctrlKey;
|
|
|
|
otherModifier = event.metaKey || event.shiftKey || event.altKey;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ctrl && !otherModifier;
|
|
|
|
}
|
|
|
|
|
2024-08-28 23:52:01 +02:00
|
|
|
function shortcutHandler(event: KeyboardEvent) {
|
2024-07-04 02:27:59 +02:00
|
|
|
if (!canAcceptShortcut(event)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-09-13 01:19:00 +02:00
|
|
|
const textarea = event.target,
|
2024-07-22 04:08:38 +02:00
|
|
|
keyCode = event.keyCode;
|
2021-09-13 01:19:00 +02:00
|
|
|
|
2024-08-28 23:52:01 +02:00
|
|
|
if (!(textarea instanceof HTMLTextAreaElement)) return;
|
|
|
|
|
2021-09-13 01:19:00 +02:00
|
|
|
for (const id in markdownSyntax) {
|
2024-07-22 04:08:38 +02:00
|
|
|
if (keyCode === markdownSyntax[id].options.shortcutKeyCode) {
|
2021-09-13 01:19:00 +02:00
|
|
|
markdownSyntax[id].action(textarea, markdownSyntax[id].options);
|
|
|
|
event.preventDefault();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function setupToolbar() {
|
2024-08-28 23:52:01 +02:00
|
|
|
$$<HTMLElement>('.communication__toolbar').forEach(toolbar => {
|
2021-09-13 01:19:00 +02:00
|
|
|
toolbar.addEventListener('click', clickHandler);
|
|
|
|
});
|
2024-08-28 23:52:01 +02:00
|
|
|
$$<HTMLTextAreaElement>('.js-toolbar-input').forEach(textarea => {
|
2021-09-13 01:19:00 +02:00
|
|
|
textarea.addEventListener('keydown', shortcutHandler);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export { setupToolbar };
|