2017-04-23 08:51:26 +02:00
|
|
|
var compose_ui = (function () {
|
|
|
|
|
|
|
|
var exports = {};
|
|
|
|
|
|
|
|
exports.autosize_textarea = function () {
|
2017-11-26 20:37:44 +01:00
|
|
|
$("#compose-textarea").trigger("autosize.resize");
|
2017-04-23 08:51:26 +02:00
|
|
|
};
|
|
|
|
|
2017-11-09 17:32:28 +01:00
|
|
|
exports.smart_insert = function (textarea, syntax) {
|
2017-11-09 17:47:54 +01:00
|
|
|
function is_space(c) {
|
2018-06-06 18:50:09 +02:00
|
|
|
return c === ' ' || c === '\t' || c === '\n';
|
2017-11-09 17:47:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
var pos = textarea.caret();
|
|
|
|
var before_str = textarea.val().slice(0, pos);
|
2017-11-09 17:57:59 +01:00
|
|
|
var after_str = textarea.val().slice(pos);
|
2017-11-09 17:47:54 +01:00
|
|
|
|
|
|
|
if (pos > 0) {
|
|
|
|
if (!is_space(before_str.slice(-1))) {
|
|
|
|
syntax = ' ' + syntax;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-04 18:24:41 +01:00
|
|
|
if (!(after_str.length > 0 && is_space(after_str[0]))) {
|
2018-05-06 21:43:17 +02:00
|
|
|
syntax += ' ';
|
|
|
|
}
|
2017-11-09 17:57:59 +01:00
|
|
|
|
2017-11-09 17:32:28 +01:00
|
|
|
textarea.focus();
|
2017-12-08 16:17:20 +01:00
|
|
|
|
|
|
|
// We prefer to use insertText, which supports things like undo better
|
|
|
|
// for rich-text editing features like inserting links. But we fall
|
|
|
|
// back to textarea.caret if the browser doesn't support insertText.
|
|
|
|
if (!document.execCommand("insertText", false, syntax)) {
|
|
|
|
textarea.caret(syntax);
|
|
|
|
}
|
|
|
|
|
2017-11-22 04:43:45 +01:00
|
|
|
// This should just call exports.autosize_textarea, but it's a bit
|
|
|
|
// annoying for the unit tests, so we don't do that.
|
|
|
|
textarea.trigger("autosize.resize");
|
2017-11-09 17:32:28 +01:00
|
|
|
};
|
|
|
|
|
2017-11-28 22:36:58 +01:00
|
|
|
exports.insert_syntax_and_focus = function (syntax, textarea) {
|
2017-11-09 16:57:58 +01:00
|
|
|
// Generic helper for inserting syntax into the main compose box
|
|
|
|
// where the cursor was and focusing the area. Mostly a thin
|
2017-11-09 17:32:28 +01:00
|
|
|
// wrapper around smart_insert.
|
2017-11-28 22:36:58 +01:00
|
|
|
if (textarea === undefined) {
|
|
|
|
textarea = $('#compose-textarea');
|
|
|
|
}
|
2017-11-09 17:32:28 +01:00
|
|
|
exports.smart_insert(textarea, syntax);
|
2017-11-09 16:57:58 +01:00
|
|
|
};
|
|
|
|
|
2017-04-23 08:51:26 +02:00
|
|
|
return exports;
|
|
|
|
|
|
|
|
}());
|
|
|
|
if (typeof module !== 'undefined') {
|
|
|
|
module.exports = compose_ui;
|
|
|
|
}
|
2018-05-28 08:04:36 +02:00
|
|
|
window.compose_ui = compose_ui;
|