mirror of
https://github.com/discourse/discourse.git
synced 2025-03-09 14:34:35 +00:00
FEATURE: Preserve cursor in editor upload
This commit is contained in:
parent
a82182e1e3
commit
1a5fcb72d3
@ -525,11 +525,43 @@ 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);
|
||||
const needleStart = val.indexOf(oldVal);
|
||||
|
||||
if (needleStart === -1) {
|
||||
// Nothing to replace.
|
||||
return;
|
||||
}
|
||||
|
||||
const textarea = this.$('textarea.d-editor-input')[0];
|
||||
|
||||
// Remember cursor/selection.
|
||||
const selectionStart = textarea.selectionStart;
|
||||
const selectionEnd = textarea.selectionEnd;
|
||||
const needleEnd = needleStart + oldValue.length;
|
||||
const replacementEnd = needleStart + newVal.length;
|
||||
|
||||
// Replace value (side effect: cursor at end).
|
||||
this.set('value', val.replace(oldVal, newVal));
|
||||
|
||||
// Determine cursor/selection.
|
||||
const newSelectionStart, newSelectionEnd;
|
||||
if (selectionEnd <= needleEnd) {
|
||||
// Selection before needle.
|
||||
newSelectionStart = selectionStart;
|
||||
newSelectionEnd = selectionEnd;
|
||||
} else if (selectionStart < needleEnd) {
|
||||
// Selection within needle.
|
||||
newSelectionStart = replacementEnd;
|
||||
newSelectionEnd = replacementEnd;
|
||||
} else {
|
||||
// Selection behind needle.
|
||||
const lengthDiff = replacementEnd - needleStart;
|
||||
newSelectionStart = selectionStart + lengthDiff;
|
||||
newSelectionEnd = selectionEnd + lengthDiff;
|
||||
}
|
||||
|
||||
// Restore cursor.
|
||||
this._selectText(newSelectionStart, newSelectionEnd - newSelectionStart);
|
||||
},
|
||||
|
||||
_addText(sel, text) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user