feat: implement copy selected text with Ctrl+C

This commit is contained in:
2026-04-11 01:53:29 +08:00
parent d885ca1317
commit 003815239d
2 changed files with 27 additions and 3 deletions

View File

@@ -1,10 +1,12 @@
import { useState, useEffect, useRef } from "react"; import { useState, useEffect, useRef } from "react";
import type { TextareaRenderable } from "@opentui/core";
import { db, type Note } from "../db"; import { db, type Note } from "../db";
interface NoteEditorProps { interface NoteEditorProps {
note: Note | null; note: Note | null;
onUpdate: () => void; onUpdate: () => void;
onDelete: (id: number) => void; onDelete: (id: number) => void;
onCopySelected: () => string;
} }
function renderMarkdown(content: string): any { function renderMarkdown(content: string): any {
@@ -58,11 +60,11 @@ function renderMarkdown(content: string): any {
return elements; return elements;
} }
export function NoteEditor({ note, onUpdate, onDelete }: NoteEditorProps) { export function NoteEditor({ note, onUpdate, onDelete, onCopySelected }: NoteEditorProps) {
const [isEditing, setIsEditing] = useState(false); const [isEditing, setIsEditing] = useState(false);
const [title, setTitle] = useState(""); const [title, setTitle] = useState("");
const [content, setContent] = useState(""); const [content, setContent] = useState("");
const textareaRef = useRef<{ plainText: string } | null>(null); const textareaRef = useRef<TextareaRenderable | null>(null);
useEffect(() => { useEffect(() => {
if (note) { if (note) {
@@ -75,6 +77,14 @@ export function NoteEditor({ note, onUpdate, onDelete }: NoteEditorProps) {
setIsEditing(false); setIsEditing(false);
}, [note?.id]); }, [note?.id]);
useEffect(() => {
if (onCopySelected) {
(window as any).__getSelectedText = () => {
return textareaRef.current?.getSelectedText() ?? "";
};
}
}, [onCopySelected]);
const handleSave = async () => { const handleSave = async () => {
if (note) { if (note) {
const finalContent = textareaRef.current?.plainText ?? content; const finalContent = textareaRef.current?.plainText ?? content;
@@ -85,6 +95,10 @@ export function NoteEditor({ note, onUpdate, onDelete }: NoteEditorProps) {
} }
}; };
const getSelectedText = (): string => {
return textareaRef.current?.getSelectedText() ?? "";
};
if (!note) { if (!note) {
return ( return (
<box flexGrow={1} justifyContent="center" alignItems="center"> <box flexGrow={1} justifyContent="center" alignItems="center">

View File

@@ -77,10 +77,20 @@ function App() {
const selectedNote = selectedId ? notes.find((n) => n.id === selectedId) : null; const selectedNote = selectedId ? notes.find((n) => n.id === selectedId) : null;
const handleCopySelected = (): string => {
return (window as any).__getSelectedText?.() ?? "";
};
useKeyboard((key) => { useKeyboard((key) => {
if (key.ctrl && key.name === "d") { if (key.ctrl && key.name === "d") {
process.exit(0); process.exit(0);
} }
if (key.ctrl && key.name === "c") {
const text = handleCopySelected();
if (text) {
process.stdout.write('\x1b]52;c;' + btoa(text) + '\x07');
}
}
}); });
return ( return (
@@ -98,7 +108,7 @@ function App() {
/> />
</box> </box>
<box flexGrow={1}> <box flexGrow={1}>
<NoteEditor note={selectedNote || null} onUpdate={handleUpdate} onDelete={handleDelete} /> <NoteEditor note={selectedNote || null} onUpdate={handleUpdate} onDelete={handleDelete} onCopySelected={handleCopySelected} />
</box> </box>
</box> </box>
); );