Fixed caret position for append transforms
This commit is contained in:
parent
44a5b664a0
commit
9f14a895fa
3 changed files with 46 additions and 6 deletions
|
|
@ -12,11 +12,19 @@ class TextProcessor {
|
|||
setCodeMirrorEditor(editor) {
|
||||
this._editor = editor;
|
||||
window.runtime.EventsOn("process-text-response", (data) => {
|
||||
const cursorPos = this._editor.state.selection.main.head;
|
||||
const appendInsertPos = this._appendInsertPos;
|
||||
this._appendInsertPos = undefined;
|
||||
let newSelection;
|
||||
const changes = data.output.map(span => {
|
||||
if (span.append) {
|
||||
const insertAt = appendInsertPos !== undefined ? appendInsertPos : span.pos + span.len;
|
||||
if (cursorPos === insertAt) {
|
||||
newSelection = { anchor: insertAt + span.text.length };
|
||||
}
|
||||
return {
|
||||
from: span.pos + span.len,
|
||||
to: span.pos + span.len,
|
||||
from: insertAt,
|
||||
to: insertAt,
|
||||
insert: span.text,
|
||||
};
|
||||
} else {
|
||||
|
|
@ -27,7 +35,11 @@ class TextProcessor {
|
|||
};
|
||||
}
|
||||
});
|
||||
this._editor.dispatch({ changes: changes });
|
||||
const transaction = { changes: changes };
|
||||
if (newSelection) {
|
||||
transaction.selection = newSelection;
|
||||
}
|
||||
this._editor.dispatch(transaction);
|
||||
});
|
||||
window.runtime.EventsOn("request-text-process", (data) => {
|
||||
this.runTextCommand(data.action);
|
||||
|
|
@ -44,12 +56,14 @@ class TextProcessor {
|
|||
|
||||
let inputs = [];
|
||||
if (shouldBeAll) {
|
||||
this._appendInsertPos = this._editor.state.selection.main.head;
|
||||
inputs.push({
|
||||
text: this._editor.state.doc.toString(),
|
||||
pos: 0,
|
||||
len: this._editor.state.doc.length,
|
||||
});
|
||||
} else {
|
||||
this._appendInsertPos = undefined;
|
||||
inputs = ranges.map(r => ({
|
||||
text: this._editor.state.doc.slice(r.from, r.to).toString(),
|
||||
pos: r.from,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue