murashit_codex-panel/src/editor-rewrite/command.ts
2026-05-18 11:55:43 +09:00

55 lines
1.5 KiB
TypeScript

import { MarkdownView, Notice, type Editor, type Plugin } from "obsidian";
import { RewriteSelectionModal } from "./modal";
import type { RewriteSession } from "./model";
export interface RewriteSelectionCommandHost extends Plugin {
settings: {
codexPath: string;
};
vaultPath: string;
}
export function registerRewriteSelectionCommand(plugin: RewriteSelectionCommandHost): void {
plugin.addCommand({
id: "rewrite-selection",
name: "Rewrite selection",
editorCallback: (editor, view) => {
if (!(view instanceof MarkdownView) || !view.file) {
new Notice("Rewrite selection requires an active markdown note.");
return;
}
const originalText = editor.getSelection();
if (!originalText.trim()) {
new Notice("Select text to rewrite first.");
return;
}
const session: RewriteSession = {
filePath: view.file.path,
targetRange: {
from: clonePosition(editor.getCursor("from")),
to: clonePosition(editor.getCursor("to")),
},
originalText,
noteText: editor.getValue(),
instruction: "",
status: "editing-prompt",
streamText: "",
replacementText: null,
};
new RewriteSelectionModal(plugin.app, {
codexPath: plugin.settings.codexPath,
cwd: plugin.vaultPath,
editor,
session,
}).open();
},
});
}
function clonePosition(position: ReturnType<Editor["getCursor"]>): ReturnType<Editor["getCursor"]> {
return { line: position.line, ch: position.ch };
}