mirror of
https://github.com/zulip/zulip.git
synced 2025-11-18 12:54:58 +00:00
static/js/compose.js: Clean add_markdown function.
Rename `add_markdown` function to `wrap_text_with_markdown` and use closure variables in function `wrap_text_with_markdown`.
This commit is contained in:
committed by
Tim Abbott
parent
3ae5e40f8f
commit
987c4f7df3
@@ -565,7 +565,7 @@ exports.validate = function () {
|
|||||||
};
|
};
|
||||||
|
|
||||||
exports.handle_keydown = function (event) {
|
exports.handle_keydown = function (event) {
|
||||||
var textarea = $("#compose-textarea");
|
var textarea = $("#compose-textarea").expectOne();
|
||||||
|
|
||||||
// Set the rtl class if the text has an rtl direction, remove it otherwise
|
// Set the rtl class if the text has an rtl direction, remove it otherwise
|
||||||
rtl.set_rtl_class_for_textarea(textarea);
|
rtl.set_rtl_class_for_textarea(textarea);
|
||||||
@@ -580,33 +580,30 @@ exports.handle_keydown = function (event) {
|
|||||||
|
|
||||||
if ((isBold || isItalic || isLink) && isCmdOrCtrl) {
|
if ((isBold || isItalic || isLink) && isCmdOrCtrl) {
|
||||||
var range = textarea.range();
|
var range = textarea.range();
|
||||||
|
function wrap_text_with_markdown(prefix, suffix) {
|
||||||
function add_markdown(markdown) {
|
if (!document.execCommand('insertText', false, prefix + range.text + suffix)) {
|
||||||
var textarea = $("#compose-textarea");
|
textarea.range(range.start, range.end).range(prefix + range.text + suffix);
|
||||||
var range = textarea.range();
|
|
||||||
if (!document.execCommand('insertText', false, markdown)) {
|
|
||||||
textarea.range(range.start, range.end).range(markdown);
|
|
||||||
}
|
}
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isBold) {
|
if (isBold) {
|
||||||
// ctrl + b: Convert selected text to bold text
|
// ctrl + b: Convert selected text to bold text
|
||||||
add_markdown("**" + range.text + "**");
|
wrap_text_with_markdown("**", "**");
|
||||||
if (!range.length) {
|
if (!range.length) {
|
||||||
textarea.caret(textarea.caret() - 2);
|
textarea.caret(textarea.caret() - 2);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (isItalic) {
|
if (isItalic) {
|
||||||
// ctrl + i: Convert selected text to italic text
|
// ctrl + i: Convert selected text to italic text
|
||||||
add_markdown("*" + range.text + "*");
|
wrap_text_with_markdown("*", "*");
|
||||||
if (!range.length) {
|
if (!range.length) {
|
||||||
textarea.caret(textarea.caret() - 1);
|
textarea.caret(textarea.caret() - 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (isLink) {
|
if (isLink) {
|
||||||
// ctrl + l: Insert a link to selected text
|
// ctrl + l: Insert a link to selected text
|
||||||
add_markdown("[" + range.text + "](url)");
|
wrap_text_with_markdown("[", "](url)");
|
||||||
var position = textarea.caret();
|
var position = textarea.caret();
|
||||||
var txt = document.getElementById("compose-textarea");
|
var txt = document.getElementById("compose-textarea");
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user