2025-09-06 01:26:54 +00:00
|
|
|
import {ProcessText} from "../wailsjs/go/main/App";
|
|
|
|
|
|
|
|
|
|
class TextProcessor {
|
|
|
|
|
setCodeMirrorEditor(editor) {
|
|
|
|
|
this._editor = editor;
|
|
|
|
|
window.runtime.EventsOn("process-text-response", (data) => {
|
2026-01-25 00:00:40 +00:00
|
|
|
const changes = data.output.map(span => {
|
|
|
|
|
if (span.append) {
|
|
|
|
|
return {
|
|
|
|
|
from: span.pos + span.len,
|
|
|
|
|
to: span.pos + span.len,
|
|
|
|
|
insert: span.text,
|
|
|
|
|
};
|
|
|
|
|
} else {
|
|
|
|
|
return {
|
|
|
|
|
from: span.pos,
|
|
|
|
|
to: span.pos + span.len,
|
|
|
|
|
insert: span.text,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
});
|
2025-09-06 01:26:54 +00:00
|
|
|
this._editor.dispatch({ changes: changes });
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
runTextCommand(command) {
|
|
|
|
|
if (this._editor === undefined) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-14 11:58:30 +00:00
|
|
|
let ranges = this._editor.state.selection.ranges;
|
|
|
|
|
let shouldBeAll = ranges.reduce((a, r) => a && r.from === r.to, true);
|
|
|
|
|
|
|
|
|
|
let inputs = [];
|
|
|
|
|
if (shouldBeAll) {
|
|
|
|
|
inputs.push({
|
|
|
|
|
text: this._editor.state.doc.toString(),
|
|
|
|
|
pos: 0,
|
|
|
|
|
len: this._editor.state.doc.length,
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
inputs = ranges.map(r => ({
|
|
|
|
|
text: this._editor.state.doc.slice(r.from, r.to).toString(),
|
|
|
|
|
pos: r.from,
|
|
|
|
|
len: r.to - r.from,
|
|
|
|
|
}))
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-06 01:26:54 +00:00
|
|
|
ProcessText({
|
|
|
|
|
action: command,
|
2025-09-14 11:58:30 +00:00
|
|
|
input: inputs,
|
2025-09-06 01:26:54 +00:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export const textProcessor = new TextProcessor();
|