♻️ Refactored translation extension

This commit is contained in:
2025-11-23 18:45:49 +08:00
parent 4b0f39d747
commit ad24d3a140
16 changed files with 1101 additions and 1536 deletions

View File

@@ -0,0 +1,66 @@
import type { EditorView } from '@codemirror/view';
import { readonly, shallowRef, type ShallowRef } from 'vue';
interface TranslatorPosition {
x: number;
y: number;
}
interface TranslatorState {
visible: boolean;
position: TranslatorPosition;
sourceText: string;
view: EditorView | null;
}
class TranslatorManager {
private state: ShallowRef<TranslatorState> = shallowRef({
visible: false,
position: { x: 0, y: 0 },
sourceText: '',
view: null
});
useState() {
return readonly(this.state);
}
show(view: EditorView, clientX: number, clientY: number, text: string): void {
this.state.value = {
visible: true,
position: { x: clientX, y: clientY },
sourceText: text,
view
};
}
hide(): void {
if (!this.state.value.visible) {
return;
}
const view = this.state.value.view;
this.state.value = {
visible: false,
position: { x: 0, y: 0 },
sourceText: '',
view: null
};
if (view) {
view.focus();
}
}
destroy(): void {
this.state.value = {
visible: false,
position: { x: 0, y: 0 },
sourceText: '',
view: null
};
}
}
export const translatorManager = new TranslatorManager();