Have got text transformations working

This commit is contained in:
Leon Mika 2025-09-06 11:26:54 +10:00
parent 9f2fa96b92
commit 41daf7cfc9
14 changed files with 178 additions and 17 deletions

View file

@ -15,10 +15,8 @@
data-action="keyup.enter->commands#runCommand keydown.esc->commands#dismissDialog keyup->commands#handleKeyup">
</div>
<select multiple class="command-options" data-commands-target="commandSelect">
<option value="double"><span class="option-label">Double quotes</span></option>
<option value="single"><span class="option-label">Single</span></option>
<option value="backtick"><span class="option-label">Backtick quotes</span></option>
<option value="none"><span class="option-label">None</span></option>
<option value="unquote"><span class="option-label">Unquote</span></option>
<option value="format-json"><span class="option-label">Format JSON</span></option>
</select>
</div>
</dialog>

View file

@ -1,5 +1,6 @@
.cm-editor {
height: 100%;
font-size: 1.1em;
}
dialog#command-dialog {

View file

@ -1,6 +1,8 @@
import { Controller } from "@hotwired/stimulus"
export default class extends Controller {
import { textProcessor } from "../services.js";
export class CommandsController extends Controller {
static targets = [
"commandInput",
"commandSelect",
@ -30,7 +32,7 @@ export default class extends Controller {
runCommand(ev) {
ev.preventDefault();
console.log("Do this: " + this.commandSelectTarget.value);
textProcessor.runTextCommand(this.commandSelectTarget.value);
this.element.close();
}
@ -49,4 +51,4 @@ export default class extends Controller {
this.commandSelectTarget.selectedIndex = 0;
}
}
}
}

View file

@ -5,7 +5,8 @@ import {EditorView, basicSetup} from "codemirror";
import {keymap} from "@codemirror/view";
import { Application } from "@hotwired/stimulus";
import CommandsController from "./controllers/commands_controller.js";
import { textProcessor } from "./services.js";
import {CommandsController} from "./controllers/commands_controller.js";
const view = new EditorView({
parent: document.querySelector("#app"),
@ -27,4 +28,6 @@ const view = new EditorView({
window.Stimulus = Application.start()
Stimulus.register("commands", CommandsController);
textProcessor.setCodeMirrorEditor(view);
view.focus();

31
frontend/src/services.js Normal file
View file

@ -0,0 +1,31 @@
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 => ({
from: span.pos,
to: span.pos + span.len,
insert: span.text,
}));
this._editor.dispatch({ changes: changes });
});
}
runTextCommand(command) {
if (this._editor === undefined) {
return;
}
ProcessText({
action: command,
input: [
{text: this._editor.state.doc.toString(), pos: 0, len: this._editor.state.doc.length}
],
});
}
}
export const textProcessor = new TextProcessor();

View file

@ -1,4 +1,5 @@
// Cynhyrchwyd y ffeil hon yn awtomatig. PEIDIWCH Â MODIWL
// This file is automatically generated. DO NOT EDIT
import {main} from '../models';
export function Greet(arg1:string):Promise<string>;
export function ProcessText(arg1:main.ProcessTextRequest):Promise<void>;

View file

@ -2,6 +2,6 @@
// Cynhyrchwyd y ffeil hon yn awtomatig. PEIDIWCH Â MODIWL
// This file is automatically generated. DO NOT EDIT
export function Greet(arg1) {
return window['go']['main']['App']['Greet'](arg1);
export function ProcessText(arg1) {
return window['go']['main']['App']['ProcessText'](arg1);
}

53
frontend/wailsjs/go/models.ts Executable file
View file

@ -0,0 +1,53 @@
export namespace main {
export class TextSpan {
text: string;
pos: number;
len: number;
static createFrom(source: any = {}) {
return new TextSpan(source);
}
constructor(source: any = {}) {
if ('string' === typeof source) source = JSON.parse(source);
this.text = source["text"];
this.pos = source["pos"];
this.len = source["len"];
}
}
export class ProcessTextRequest {
action: string;
input: TextSpan[];
static createFrom(source: any = {}) {
return new ProcessTextRequest(source);
}
constructor(source: any = {}) {
if ('string' === typeof source) source = JSON.parse(source);
this.action = source["action"];
this.input = this.convertValues(source["input"], TextSpan);
}
convertValues(a: any, classs: any, asMap: boolean = false): any {
if (!a) {
return a;
}
if (a.slice && a.map) {
return (a as any[]).map(elem => this.convertValues(elem, classs));
} else if ("object" === typeof a) {
if (asMap) {
for (const key of Object.keys(a)) {
a[key] = new classs(a[key]);
}
return a;
}
return new classs(a);
}
return a;
}
}
}