mirror of
https://github.com/discourse/discourse.git
synced 2025-05-24 03:36:18 +08:00
FEATURE: Quote reply at cursor
This commit is contained in:
@ -70,6 +70,11 @@ export default Discourse.Controller.extend({
|
|||||||
if (c) { c.appendText(text); }
|
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() {
|
categories: function() {
|
||||||
return Discourse.Category.list();
|
return Discourse.Category.list();
|
||||||
}.property(),
|
}.property(),
|
||||||
|
@ -119,7 +119,7 @@ export default Discourse.Controller.extend({
|
|||||||
var quotedText = Discourse.Quote.build(post, buffer);
|
var quotedText = Discourse.Quote.build(post, buffer);
|
||||||
composerOpts.quote = quotedText;
|
composerOpts.quote = quotedText;
|
||||||
if (composerController.get('content.viewOpen') || composerController.get('content.viewDraft')) {
|
if (composerController.get('content.viewOpen') || composerController.get('content.viewDraft')) {
|
||||||
composerController.appendText(quotedText);
|
composerController.appendBlockAtCursor(quotedText.trim());
|
||||||
} else {
|
} else {
|
||||||
composerController.open(composerOpts);
|
composerController.open(composerOpts);
|
||||||
}
|
}
|
||||||
|
@ -19,6 +19,10 @@ getCaret = function(el) {
|
|||||||
|
|
||||||
clone = null;
|
clone = null;
|
||||||
|
|
||||||
|
$.fn.caret = function(){
|
||||||
|
return getCaret(this[0]);
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
This is a jQuery plugin to retrieve the caret position in a textarea
|
This is a jQuery plugin to retrieve the caret position in a textarea
|
||||||
|
|
||||||
|
@ -266,8 +266,36 @@ Discourse.Composer = Discourse.Model.extend({
|
|||||||
@method appendText
|
@method appendText
|
||||||
@param {String} text the text to append
|
@param {String} text the text to append
|
||||||
**/
|
**/
|
||||||
appendText: function(text) {
|
appendText: function(text,position,opts) {
|
||||||
this.set('reply', (this.get('reply') || '') + text);
|
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() {
|
togglePreview: function() {
|
||||||
|
@ -57,6 +57,23 @@ test("appendText", function() {
|
|||||||
composer.appendText(" world");
|
composer.appendText(" world");
|
||||||
equal(composer.get('reply'), "hello world", "it appends text to existing text");
|
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() {
|
test("Title length for regular topics", function() {
|
||||||
|
Reference in New Issue
Block a user