FIX: replaceMarkdown should be smart about current caret position

This commit is contained in:
Régis Hanol 2015-09-25 17:21:44 +02:00
parent d5712f051e
commit 9e2934f635
1 changed files with 12 additions and 7 deletions

View File

@ -528,18 +528,23 @@ const ComposerView = Ember.View.extend(Ember.Evented, {
addMarkdown(text) { addMarkdown(text) {
const ctrl = this.$('.wmd-input').get(0), const ctrl = this.$('.wmd-input').get(0),
caretPosition = Discourse.Utilities.caretPosition(ctrl), reply = this.get('model.reply'),
current = this.get('model.reply'); caretPosition = Discourse.Utilities.caretPosition(ctrl);
this.set('model.reply', current.substring(0, caretPosition) + text + current.substring(caretPosition, current.length));
Em.run.schedule('afterRender', function() { this.set('model.reply', reply.substring(0, caretPosition) + text + reply.substring(caretPosition, reply.length));
Discourse.Utilities.setCaretPosition(ctrl, caretPosition + text.length);
}); Em.run.schedule('afterRender', () => Discourse.Utilities.setCaretPosition(ctrl, caretPosition + text.length));
}, },
replaceMarkdown(old, text) { replaceMarkdown(old, text) {
const reply = this.get("model.reply"); const ctrl = this.$(".wmd-input").get(0),
reply = this.get("model.reply"),
beforeCaretPosition = Discourse.Utilities.caretPosition(ctrl),
afterCaretPosition = beforeCaretPosition <= reply.indexOf(old) ? beforeCaretPosition : beforeCaretPosition - old.length + text.length;
this.set("model.reply", reply.replace(old, text)); this.set("model.reply", reply.replace(old, text));
Ember.run.schedule("afterRender", () => Discourse.Utilities.setCaretPosition(ctrl, afterCaretPosition));
}, },
// Uses javascript to get the image sizes from the preview, if present // Uses javascript to get the image sizes from the preview, if present