FIX: After uploading an image the selection was not in the correct spot

This commit is contained in:
Robin Ward 2016-07-25 15:31:21 -04:00
parent 215eae9972
commit 3200d836f7
3 changed files with 29 additions and 6 deletions

View file

@ -189,7 +189,7 @@ export default Ember.Component.extend({
this.setProperties({ uploadProgress: 0, isUploading: false, isCancellable: false });
}
if (removePlaceholder) {
this.set('composer.reply', this.get('composer.reply').replace(this.get('uploadPlaceholder'), ""));
this.appEvents.trigger('composer:replace-text', this.get('uploadPlaceholder'), "");
}
},
@ -219,7 +219,6 @@ export default Ember.Component.extend({
$element.on("fileuploadsend", (e, data) => {
this._validUploads++;
// add upload placeholders
this.appEvents.trigger('composer:insert-text', uploadPlaceholder);
if (data.xhr && data.originalFiles.length === 1) {
@ -244,7 +243,7 @@ export default Ember.Component.extend({
if (upload && upload.url) {
if (!this._xhr || !this._xhr._userCancelled) {
const markdown = getUploadMarkdown(upload);
this.set('composer.reply', this.get('composer.reply').replace(uploadPlaceholder, markdown));
this.appEvents.trigger('composer:replace-text', uploadPlaceholder, markdown);
this._resetUpload(false);
} else {
this._resetUpload(true);

View file

@ -215,9 +215,8 @@ export default Ember.Component.extend({
return false;
});
this.appEvents.on('composer:insert-text', text => {
this._addText(this._getSelected(), text);
});
this.appEvents.on('composer:insert-text', text => this._addText(this._getSelected(), text));
this.appEvents.on('composer:replace-text', (oldVal, newVal) => this._replaceText(oldVal, newVal));
this._mouseTrap = mouseTrap;
},
@ -225,6 +224,7 @@ export default Ember.Component.extend({
@on('willDestroyElement')
_shutDown() {
this.appEvents.off('composer:insert-text');
this.appEvents.off('composer:replace-text');
const mouseTrap = this._mouseTrap;
Object.keys(this.get('toolbar.shortcuts')).forEach(sc => mouseTrap.unbind(sc));
@ -475,6 +475,15 @@ export default Ember.Component.extend({
}
},
_replaceText(oldVal, newVal) {
const val = this.get('value');
const loc = val.indexOf(oldVal);
if (loc !== -1) {
this.set('value', val.replace(oldVal, newVal));
this._selectText(loc + newVal.length, 0);
}
},
_addText(sel, text) {
const $textarea = this.$('textarea.d-editor-input');
const insert = `${sel.pre}${text}`;

View file

@ -596,3 +596,18 @@ componentTest('emoji', {
});
}
});
testCase("replace-text event", function(assert, textarea) {
this.set('value', "red green blue");
andThen(() => {
this.container.lookup('app-events:main').trigger('composer:replace-text', 'green', 'yellow');
});
andThen(() => {
assert.equal(this.get('value'), 'red yellow blue');
assert.equal(textarea.selectionStart, 10);
assert.equal(textarea.selectionEnd, 10);
});
});