dequoter/frontend/src/services.js

57 lines
1.6 KiB
JavaScript
Raw Normal View History

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) => {
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;
}
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,
input: inputs,
2025-09-06 01:26:54 +00:00
});
}
}
export const textProcessor = new TextProcessor();