mirror of
https://github.com/zulip/zulip.git
synced 2025-11-12 09:58:06 +00:00
compose_ui: Create the compose_ui.replace_syntax function.
`replace_syntax` will replace text inside of a compose textbox. Also, add tests to the `compose_ui` Node tests for this function.
This commit is contained in:
committed by
Tim Abbott
parent
e204fe3948
commit
fa22cf18f6
@@ -43,8 +43,12 @@ function make_textbox(s) {
|
|||||||
widget.focused = false;
|
widget.focused = false;
|
||||||
};
|
};
|
||||||
|
|
||||||
widget.val = function () {
|
widget.val = function (new_val) {
|
||||||
|
if (new_val) {
|
||||||
|
widget.s = new_val;
|
||||||
|
} else {
|
||||||
return widget.s;
|
return widget.s;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
widget.trigger = function () {
|
widget.trigger = function () {
|
||||||
@@ -107,3 +111,12 @@ run_test('smart_insert', () => {
|
|||||||
// like emojis and file links.
|
// like emojis and file links.
|
||||||
});
|
});
|
||||||
|
|
||||||
|
run_test('replace_syntax', () => {
|
||||||
|
$('#compose-textarea').val('abcabc');
|
||||||
|
|
||||||
|
compose_ui.replace_syntax('a', 'A');
|
||||||
|
assert.equal($('#compose-textarea').val(), 'Abcabc');
|
||||||
|
|
||||||
|
compose_ui.replace_syntax(/b/g, 'B');
|
||||||
|
assert.equal($('#compose-textarea').val(), 'ABcaBc');
|
||||||
|
});
|
||||||
|
|||||||
@@ -49,6 +49,19 @@ exports.insert_syntax_and_focus = function (syntax, textarea) {
|
|||||||
exports.smart_insert(textarea, syntax);
|
exports.smart_insert(textarea, syntax);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
exports.replace_syntax = function (old_syntax, new_syntax, textarea) {
|
||||||
|
// Replaces `old_syntax` with `new_syntax` text in the compose box. Due to
|
||||||
|
// the way that JavaScript handles string replacements, if `old_syntax` is
|
||||||
|
// a string it will only replace the first instance. If `old_syntax` is
|
||||||
|
// a RegExp with a global flag, it will replace all instances.
|
||||||
|
|
||||||
|
if (textarea === undefined) {
|
||||||
|
textarea = $('#compose-textarea');
|
||||||
|
}
|
||||||
|
|
||||||
|
textarea.val(textarea.val().replace(old_syntax, new_syntax));
|
||||||
|
};
|
||||||
|
|
||||||
return exports;
|
return exports;
|
||||||
|
|
||||||
}());
|
}());
|
||||||
|
|||||||
Reference in New Issue
Block a user