Fixed caret position for append transforms
All checks were successful
Build / build (push) Successful in 39s
Release Build / build (push) Successful in 3m59s

This commit is contained in:
Leon Mika 2026-05-04 22:00:12 +10:00
parent 44a5b664a0
commit 9f14a895fa
3 changed files with 46 additions and 6 deletions

View file

@ -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,