From 003815239d2e6ffaccb8f2cf73479e8d4cf3e8a7 Mon Sep 17 00:00:00 2001 From: "Z.To" Date: Sat, 11 Apr 2026 01:53:29 +0800 Subject: [PATCH] feat: implement copy selected text with Ctrl+C --- src/components/NoteEditor.tsx | 18 ++++++++++++++++-- src/index.tsx | 12 +++++++++++- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/src/components/NoteEditor.tsx b/src/components/NoteEditor.tsx index 993feba..3249bb5 100644 --- a/src/components/NoteEditor.tsx +++ b/src/components/NoteEditor.tsx @@ -1,10 +1,12 @@ import { useState, useEffect, useRef } from "react"; +import type { TextareaRenderable } from "@opentui/core"; import { db, type Note } from "../db"; interface NoteEditorProps { note: Note | null; onUpdate: () => void; onDelete: (id: number) => void; + onCopySelected: () => string; } function renderMarkdown(content: string): any { @@ -58,11 +60,11 @@ function renderMarkdown(content: string): any { return elements; } -export function NoteEditor({ note, onUpdate, onDelete }: NoteEditorProps) { +export function NoteEditor({ note, onUpdate, onDelete, onCopySelected }: NoteEditorProps) { const [isEditing, setIsEditing] = useState(false); const [title, setTitle] = useState(""); const [content, setContent] = useState(""); - const textareaRef = useRef<{ plainText: string } | null>(null); + const textareaRef = useRef(null); useEffect(() => { if (note) { @@ -75,6 +77,14 @@ export function NoteEditor({ note, onUpdate, onDelete }: NoteEditorProps) { setIsEditing(false); }, [note?.id]); + useEffect(() => { + if (onCopySelected) { + (window as any).__getSelectedText = () => { + return textareaRef.current?.getSelectedText() ?? ""; + }; + } + }, [onCopySelected]); + const handleSave = async () => { if (note) { 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) { return ( diff --git a/src/index.tsx b/src/index.tsx index e798657..e8e0ea7 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -77,10 +77,20 @@ function App() { const selectedNote = selectedId ? notes.find((n) => n.id === selectedId) : null; + const handleCopySelected = (): string => { + return (window as any).__getSelectedText?.() ?? ""; + }; + useKeyboard((key) => { if (key.ctrl && key.name === "d") { process.exit(0); } + if (key.ctrl && key.name === "c") { + const text = handleCopySelected(); + if (text) { + process.stdout.write('\x1b]52;c;' + btoa(text) + '\x07'); + } + } }); return ( @@ -98,7 +108,7 @@ function App() { /> - + );