FIX: quote whote post should insert at cursor position

This commit is contained in:
Régis Hanol 2015-02-02 19:08:28 +01:00
parent f15b0d205f
commit 0a252d7785
4 changed files with 28 additions and 75 deletions

View file

@ -39,7 +39,34 @@ export default DiscourseController.extend({
// Import a quote from the post // Import a quote from the post
importQuote: function() { importQuote: function() {
this.get('model').importQuote(); var postStream = this.get('topic.postStream'),
postId = this.get('model.post.id');
// If there is no current post, use the first post id from the stream
if (!postId && postStream) {
postId = postStream.get('firstPostId');
}
// If we're editing a post, fetch the reply when importing a quote
if (this.get('model.editingPost')) {
var replyToPostNumber = this.get('model.post.reply_to_post_number');
if (replyToPostNumber) {
var replyPost = postStream.get('posts').findBy('post_number', replyToPostNumber);
if (replyPost) {
postId = replyPost.get('id');
}
}
}
if (postId) {
this.set('model.loading', true);
var composer = this;
return Discourse.Post.load(postId).then(function(post) {
var quote = Discourse.Quote.build(post, post.get("raw"))
composer.appendBlockAtCursor(quote);
composer.set('model.loading', false);
});
}
}, },
cancel: function() { cancel: function() {

View file

@ -328,36 +328,6 @@ Discourse.Composer = Discourse.Model.extend({
Discourse.KeyValueStore.set({ key: 'composer.showPreview', value: this.get('showPreview') }); Discourse.KeyValueStore.set({ key: 'composer.showPreview', value: this.get('showPreview') });
}, },
importQuote: function() {
var postStream = this.get('topic.postStream'),
postId = this.get('post.id');
if (!postId && postStream) {
postId = postStream.get('firstPostId');
}
// If we're editing a post, fetch the reply when importing a quote
if (this.get('editingPost')) {
var replyToPostNumber = this.get('post.reply_to_post_number');
if (replyToPostNumber) {
var replyPost = postStream.get('posts').findBy('post_number', replyToPostNumber);
if (replyPost) {
postId = replyPost.get('id');
}
}
}
// If there is no current post, use the post id from the stream
if (postId) {
this.set('loading', true);
var composer = this;
return Discourse.Post.load(postId).then(function(post) {
composer.appendText(Discourse.Quote.build(post, post.get('raw')));
composer.set('loading', false);
});
}
},
/* /*
Open a composer Open a composer

View file

@ -4,7 +4,6 @@ window.PagedownCustom = {
id: 'wmd-quote-post', id: 'wmd-quote-post',
description: I18n.t("composer.quote_post_title"), description: I18n.t("composer.quote_post_title"),
execute: function() { execute: function() {
// AWFUL but I can't figure out how to call a controller method from outside our app
return Discourse.__container__.lookup('controller:composer').send('importQuote'); return Discourse.__container__.lookup('controller:composer').send('importQuote');
} }
} }

View file

@ -129,19 +129,6 @@ test("Title length for private messages", function() {
ok(composer.get('titleLengthValid'), "in the range is okay"); ok(composer.get('titleLengthValid'), "in the range is okay");
}); });
test('importQuote with no data', function() {
sandbox.stub(Discourse.Post, 'load');
var composer = Discourse.Composer.create();
composer.importQuote();
blank(composer.get('reply'), 'importing with no topic adds nothing');
ok(!Discourse.Post.load.calledOnce, "load is not called");
composer = Discourse.Composer.create({topic: Discourse.Topic.create()});
composer.importQuote();
blank(composer.get('reply'), 'importing with no posts in a topic adds nothing');
ok(!Discourse.Post.load.calledOnce, "load is not called");
});
test('editingFirstPost', function() { test('editingFirstPost', function() {
var composer = Discourse.Composer.create(); var composer = Discourse.Composer.create();
ok(!composer.get('editingFirstPost'), "it's false by default"); ok(!composer.get('editingFirstPost'), "it's false by default");
@ -155,36 +142,6 @@ test('editingFirstPost', function() {
}); });
asyncTestDiscourse('importQuote with a post', function() {
expect(1);
sandbox.stub(Discourse.Post, 'load').withArgs(123).returns(new Ember.RSVP.Promise(function (resolve) {
resolve(Discourse.Post.create({raw: "let's quote"}));
}));
var composer = Discourse.Composer.create({post: Discourse.Post.create({id: 123})});
composer.importQuote().then(function () {
start();
ok(composer.get('reply').indexOf("let's quote") > -1, "it quoted the post");
});
});
asyncTestDiscourse('importQuote with no post', function() {
expect(1);
sandbox.stub(Discourse.Post, 'load').withArgs(4).returns(new Ember.RSVP.Promise(function (resolve) {
resolve(Discourse.Post.create({raw: 'quote me'}));
}));
var composer = Discourse.Composer.create({topic: Discourse.Topic.create()});
composer.set('topic.postStream.stream', [4, 5]);
composer.importQuote().then(function () {
start();
ok(composer.get('reply').indexOf('quote me') > -1, "it contains the word quote me");
});
});
test('clearState', function() { test('clearState', function() {
var composer = Discourse.Composer.create({ var composer = Discourse.Composer.create({
originalText: 'asdf', originalText: 'asdf',