feat: implement copy selected text with Ctrl+C
This commit is contained in:
@@ -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<TextareaRenderable | null>(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 (
|
||||
<box flexGrow={1} justifyContent="center" alignItems="center">
|
||||
|
||||
@@ -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() {
|
||||
/>
|
||||
</box>
|
||||
<box flexGrow={1}>
|
||||
<NoteEditor note={selectedNote || null} onUpdate={handleUpdate} onDelete={handleDelete} />
|
||||
<NoteEditor note={selectedNote || null} onUpdate={handleUpdate} onDelete={handleDelete} onCopySelected={handleCopySelected} />
|
||||
</box>
|
||||
</box>
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user