FEATURE: Quote reply at cursor

This commit is contained in:
Sam 2014-06-18 15:17:32 +10:00
parent 6cab36bc13
commit 806ddb4ccb
5 changed files with 57 additions and 3 deletions

View file

@ -70,6 +70,11 @@ export default Discourse.Controller.extend({
if (c) { c.appendText(text); }
},
appendBlockAtCursor: function(text) {
var c = this.get('model');
if (c) { c.appendText(text, $('#wmd-input').caret(), {block: true}); }
},
categories: function() {
return Discourse.Category.list();
}.property(),

View file

@ -119,7 +119,7 @@ export default Discourse.Controller.extend({
var quotedText = Discourse.Quote.build(post, buffer);
composerOpts.quote = quotedText;
if (composerController.get('content.viewOpen') || composerController.get('content.viewDraft')) {
composerController.appendText(quotedText);
composerController.appendBlockAtCursor(quotedText.trim());
} else {
composerController.open(composerOpts);
}

View file

@ -19,6 +19,10 @@ getCaret = function(el) {
clone = null;
$.fn.caret = function(){
return getCaret(this[0]);
};
/**
This is a jQuery plugin to retrieve the caret position in a textarea

View file

@ -266,8 +266,36 @@ Discourse.Composer = Discourse.Model.extend({
@method appendText
@param {String} text the text to append
**/
appendText: function(text) {
this.set('reply', (this.get('reply') || '') + text);
appendText: function(text,position,opts) {
var reply = (this.get('reply') || '');
position = typeof(position) === "number" ? position : reply.length;
var before = reply.slice(0, position) || '';
var after = reply.slice(position) || '';
var stripped, i;
if(opts && opts.block){
if(before.trim() !== ""){
stripped = before.replace(/\r/g, "");
for(i=0; i<2; i++){
if(stripped[stripped.length - 1 - i] !== "\n"){
before += "\n";
position++;
}
}
}
if(after.trim() !== ""){
stripped = after.replace(/\r/g, "");
for(i=0; i<2; i++){
if(stripped[i] !== "\n"){
after = "\n" + after;
}
}
}
}
this.set('reply', before + text + after);
},
togglePreview: function() {

View file

@ -57,6 +57,23 @@ test("appendText", function() {
composer.appendText(" world");
equal(composer.get('reply'), "hello world", "it appends text to existing text");
composer.clearState();
composer.appendText("a\n\n\n\nb");
composer.appendText("c",3,{block: true});
equal(composer.get("reply"), "a\n\nc\n\nb");
composer.clearState();
composer.appendText("ab");
composer.appendText("c",1,{block: true});
equal(composer.get("reply"), "a\n\nc\n\nb");
composer.clearState();
composer.appendText("\nab");
composer.appendText("c",0,{block: true});
equal(composer.get("reply"), "c\n\nab");
});
test("Title length for regular topics", function() {