refactor: Move wrap_text_with_markdown into compose_ui.js.

This commit makes it possible to use wrap_text_with_markdown_outside
compose.js.

Part of #18692
This commit is contained in:
Signior-X
2021-06-22 02:54:03 +05:30
committed by Tim Abbott
parent 2cb1be096e
commit 3bdc7432b9
2 changed files with 19 additions and 9 deletions

View File

@@ -344,30 +344,32 @@ export function handle_keydown(event, textarea) {
if ((isBold || isItalic || isLink) && isCmdOrCtrl) {
const range = textarea.range();
function wrap_text_with_markdown(prefix, suffix) {
if (!document.execCommand("insertText", false, prefix + range.text + suffix)) {
textarea.range(range.start, range.end).range(prefix + range.text + suffix);
}
event.preventDefault();
}
if (isBold) {
// Ctrl + B: Convert selected text to bold text
wrap_text_with_markdown("**", "**");
compose_ui.wrap_text_with_markdown(textarea, "**", "**");
event.preventDefault();
if (!range.length) {
textarea.caret(textarea.caret() - 2);
}
}
if (isItalic) {
// Ctrl + I: Convert selected text to italic text
wrap_text_with_markdown("*", "*");
compose_ui.wrap_text_with_markdown(textarea, "*", "*");
event.preventDefault();
if (!range.length) {
textarea.caret(textarea.caret() - 1);
}
}
if (isLink) {
// Ctrl + L: Insert a link to selected text
wrap_text_with_markdown("[", "](url)");
compose_ui.wrap_text_with_markdown(textarea, "[", "](url)");
event.preventDefault();
const position = textarea.caret();
const txt = textarea[0];