File size: 35,124 Bytes
59bb0ce 541216d 59bb0ce | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 | import React, { useState, useRef, useEffect, useCallback } from "react";
import ReactMarkdown from "react-markdown";
import remarkGfm from "remark-gfm";
import remarkMath from "remark-math";
import rehypeKatex from "rehype-katex";
import rehypeSanitize, { defaultSchema } from "rehype-sanitize";
import { motion, AnimatePresence } from "framer-motion";
import "katex/dist/katex.min.css";
import {
Eye,
Edit3,
Copy,
Check,
Sparkles,
X,
Loader2,
Send,
Bold,
Italic,
Heading1,
Heading2,
List,
ListOrdered,
Code,
Link,
Quote,
Undo2,
Redo2
} from "lucide-react";
import { clsx } from "clsx";
import { twMerge } from "tailwind-merge";
function cn(...inputs) {
return twMerge(clsx(inputs));
}
const markdownSchema = {
...defaultSchema,
attributes: {
...defaultSchema.attributes,
a: [
...(defaultSchema.attributes?.a || []),
["target", "_blank"],
["rel", "noopener noreferrer"]
],
code: [...(defaultSchema.attributes?.code || []), "className"],
// Allow KaTeX elements and attributes
span: [
...(defaultSchema.attributes?.span || []),
"className",
"style",
"aria-hidden"
],
annotation: ["encoding"],
semantics: []
},
tagNames: [
...(defaultSchema.tagNames || []),
"math",
"annotation",
"semantics",
"mtext",
"mn",
"mo",
"mi",
"mspace",
"mover",
"munder",
"munderover",
"msup",
"msub",
"msubsup",
"mfrac",
"mroot",
"msqrt",
"mtable",
"mtr",
"mtd",
"mlabeledtr",
"mrow",
"menclose",
"mstyle",
"mpadded",
"mphantom"
]
};
/**
* Rich Text Formatting Toolbar
*/
function FormattingToolbar({ onFormat, disabled }) {
const tools = [
{ icon: Bold, action: "bold", title: "Bold (Ctrl+B)", syntax: ["**", "**"] },
{ icon: Italic, action: "italic", title: "Italic (Ctrl+I)", syntax: ["*", "*"] },
{ icon: Heading1, action: "h1", title: "Heading 1", syntax: ["# ", ""] },
{ icon: Heading2, action: "h2", title: "Heading 2", syntax: ["## ", ""] },
{ icon: List, action: "ul", title: "Bullet List", syntax: ["- ", ""] },
{ icon: ListOrdered, action: "ol", title: "Numbered List", syntax: ["1. ", ""] },
{ icon: Code, action: "code", title: "Inline Code", syntax: ["`", "`"] },
{ icon: Link, action: "link", title: "Link", syntax: ["[", "](url)"] },
{ icon: Quote, action: "quote", title: "Blockquote", syntax: ["> ", ""] },
];
return (
<div className="flex items-center gap-0.5 px-2 py-1.5 border-b border-border bg-background/30">
{tools.map(({ icon: Icon, action, title, syntax }) => (
<button
key={action}
onClick={() => onFormat(syntax[0], syntax[1])}
disabled={disabled}
className={cn(
"p-1.5 rounded-md text-muted-foreground transition-colors",
disabled
? "opacity-40 cursor-not-allowed"
: "hover:text-white hover:bg-foreground/10"
)}
title={title}
>
<Icon size={16} />
</button>
))}
</div>
);
}
/**
* Undo/Redo Controls
*/
function HistoryControls({ canUndo, canRedo, onUndo, onRedo, historyCount }) {
return (
<div className="flex items-center gap-1">
<button
onClick={onUndo}
disabled={!canUndo}
className={cn(
"p-1.5 rounded-md transition-colors flex items-center gap-1",
canUndo
? "text-muted-foreground hover:text-white hover:bg-foreground/10"
: "text-muted-foreground/30 cursor-not-allowed"
)}
title="Undo"
>
<Undo2 size={14} />
</button>
<button
onClick={onRedo}
disabled={!canRedo}
className={cn(
"p-1.5 rounded-md transition-colors",
canRedo
? "text-muted-foreground hover:text-white hover:bg-foreground/10"
: "text-muted-foreground/30 cursor-not-allowed"
)}
title="Redo"
>
<Redo2 size={14} />
</button>
{historyCount > 0 && (
<span className="text-xs text-muted-foreground/50 ml-1">
{historyCount} snapshot{historyCount !== 1 ? 's' : ''}
</span>
)}
</div>
);
}
/**
* Markdown preview component using same styling as chat
*/
function MarkdownPreview({ content }) {
const [copiedCode, setCopiedCode] = useState(null);
const handleCopyCode = (code, index) => {
navigator.clipboard?.writeText(code);
setCopiedCode(index);
setTimeout(() => setCopiedCode(null), 2000);
};
if (!content) {
return (
<div className="flex items-center justify-center h-full text-muted-foreground">
<p>No content yet. Start typing or use AI to generate.</p>
</div>
);
}
return (
<div className="markdown-content prose prose-invert max-w-none px-6 py-4">
<ReactMarkdown
remarkPlugins={[remarkGfm, remarkMath]}
rehypePlugins={[rehypeKatex, [rehypeSanitize, markdownSchema]]}
components={{
a: (props) => (
<a
{...props}
target="_blank"
rel="noopener noreferrer"
className="text-white underline decoration-border hover:text-white/90 hover:decoration-muted-foreground/40 transition-colors"
/>
),
pre: ({ children, ...props }) => {
const codeContent = React.Children.toArray(children)
.map(child => {
if (React.isValidElement(child) && child.props?.children) {
return typeof child.props.children === 'string'
? child.props.children
: '';
}
return '';
})
.join('');
const index = Math.random().toString(36).substr(2, 9);
return (
<div className="relative group my-4">
<pre
{...props}
className="bg-popover border border-border rounded-lg p-4 overflow-x-auto text-sm"
>
{children}
</pre>
<button
onClick={() => handleCopyCode(codeContent, index)}
className="absolute top-2 right-2 p-1.5 rounded-md bg-foreground/10 hover:bg-foreground/20 text-muted-foreground hover:text-white opacity-0 group-hover:opacity-100 transition-all"
aria-label="Copy code"
>
{copiedCode === index ? (
<Check size={14} className="text-green-400" />
) : (
<Copy size={14} />
)}
</button>
</div>
);
},
code: ({ inline, className, children, ...props }) => {
if (inline) {
return (
<code
className="px-1.5 py-0.5 rounded-md bg-foreground/10 text-white text-sm font-mono"
{...props}
>
{children}
</code>
);
}
return (
<code className={cn("text-white/90 font-mono text-sm", className)} {...props}>
{children}
</code>
);
},
table: ({ children, ...props }) => (
<div className="my-4 overflow-x-auto rounded-lg border border-border">
<table className="min-w-full divide-y divide-border" {...props}>
{children}
</table>
</div>
),
thead: ({ children, ...props }) => (
<thead className="bg-foreground/5" {...props}>
{children}
</thead>
),
th: ({ children, ...props }) => (
<th
className="px-4 py-3 text-left text-xs font-semibold text-white uppercase tracking-wider border-r border-border last:border-r-0"
{...props}
>
{children}
</th>
),
td: ({ children, ...props }) => (
<td
className="px-4 py-3 text-sm text-white/80 border-r border-border last:border-r-0"
{...props}
>
{children}
</td>
),
tr: ({ children, ...props }) => (
<tr
className="border-b border-border last:border-b-0 hover:bg-foreground/5 transition-colors"
{...props}
>
{children}
</tr>
),
ul: ({ children, ...props }) => (
<ul className="my-3 ml-1 space-y-2 list-none" {...props}>
{children}
</ul>
),
ol: ({ children, ...props }) => (
<ol className="my-3 ml-1 space-y-2 list-none counter-reset-item" {...props}>
{children}
</ol>
),
li: ({ children, ordered, ...props }) => (
<li className="relative pl-6 text-white/90" {...props}>
<span className="absolute left-0 text-muted-foreground">β’</span>
{children}
</li>
),
h1: ({ children, ...props }) => (
<h1 className="text-2xl font-bold text-white mt-6 mb-4 pb-2 border-b border-border" {...props}>
{children}
</h1>
),
h2: ({ children, ...props }) => (
<h2 className="text-xl font-semibold text-white mt-5 mb-3" {...props}>
{children}
</h2>
),
h3: ({ children, ...props }) => (
<h3 className="text-lg font-semibold text-white mt-4 mb-2" {...props}>
{children}
</h3>
),
p: ({ children, ...props }) => (
<p className="my-[0.7em] text-white/90 leading-[1.65]" {...props}>
{children}
</p>
),
strong: ({ children, ...props }) => (
<strong className="font-bold text-white" {...props}>
{children}
</strong>
),
em: ({ children, ...props }) => (
<em className="italic text-white/90" {...props}>
{children}
</em>
),
blockquote: ({ children, ...props }) => (
<blockquote
className="my-4 pl-4 border-l-4 border-border bg-foreground/5 py-2 pr-4 rounded-r-lg italic text-white/80"
{...props}
>
{children}
</blockquote>
),
hr: (props) => (
<hr className="my-6 border-t border-border" {...props} />
)
}}
>
{content}
</ReactMarkdown>
</div>
);
}
/**
* AI Edit Toolbar β bottom sheet on mobile, floating popover on desktop.
* Inspired by Google Docs / Notion mobile editing UX.
*/
function SelectionToolbar({
position,
selectedText,
onClose,
onSubmit,
isProcessing
}) {
const [instruction, setInstruction] = useState("");
const inputRef = useRef(null);
const [isMobile, setIsMobile] = useState(window.innerWidth < 768);
useEffect(() => {
const check = () => setIsMobile(window.innerWidth < 768);
window.addEventListener("resize", check);
return () => window.removeEventListener("resize", check);
}, []);
// Auto-focus the input
useEffect(() => {
// Small delay to let animation complete
const t = setTimeout(() => inputRef.current?.focus(), 200);
return () => clearTimeout(t);
}, []);
const handleSubmit = () => {
if (instruction.trim() && !isProcessing) {
onSubmit(instruction.trim());
setInstruction("");
}
};
const handleKeyDown = (e) => {
if (e.key === "Enter" && !e.shiftKey) {
e.preventDefault();
handleSubmit();
} else if (e.key === "Escape") {
onClose();
}
};
const preview = selectedText.length > 60
? selectedText.slice(0, 60) + "β¦"
: selectedText;
/* ββ Inner content (shared by both layouts) ββ */
const innerContent = (
<div className="selection-toolbar-inner">
{/* Header with selected text preview */}
<div className="flex items-center gap-2 mb-3">
<div className="flex items-center gap-2 flex-1 min-w-0">
<div className="w-7 h-7 rounded-lg bg-primary/15 flex items-center justify-center shrink-0">
<Sparkles size={14} className="text-primary" />
</div>
<div className="min-w-0">
<div className="text-xs font-semibold text-white">AI Edit Selection</div>
<div className="text-[11px] text-muted-foreground truncate">
"{preview}"
</div>
</div>
</div>
<button
onClick={onClose}
className="p-1.5 hover:bg-foreground/10 rounded-lg text-muted-foreground hover:text-white transition-colors shrink-0"
aria-label="Close"
>
<X size={16} />
</button>
</div>
{/* Instruction input */}
<div className="flex items-center gap-2">
<input
ref={inputRef}
type="text"
value={instruction}
onChange={(e) => setInstruction(e.target.value)}
onKeyDown={handleKeyDown}
placeholder="Describe the changeβ¦"
disabled={isProcessing}
className="flex-1 bg-foreground/5 border border-border rounded-xl px-3.5 py-2.5 text-sm text-white placeholder:text-muted-foreground/60 outline-none focus:border-primary/50 focus:ring-1 focus:ring-primary/20 transition-all"
autoComplete="off"
/>
<button
onClick={handleSubmit}
disabled={!instruction.trim() || isProcessing}
className={cn(
"p-2.5 rounded-xl transition-all",
instruction.trim() && !isProcessing
? "bg-primary text-primary-foreground hover:bg-primary/90 shadow-md shadow-primary/20"
: "bg-foreground/10 text-muted-foreground/50 cursor-not-allowed"
)}
>
{isProcessing ? (
<Loader2 size={16} className="animate-spin" />
) : (
<Send size={16} />
)}
</button>
</div>
{/* Quick action chips */}
<div className="flex flex-wrap gap-1.5 mt-2.5">
{["Fix grammar", "Make shorter", "Make formal", "Simplify"].map(label => (
<button
key={label}
onClick={() => {
if (!isProcessing) onSubmit(label);
}}
disabled={isProcessing}
className="px-2.5 py-1 rounded-full text-[11px] font-medium border border-border bg-foreground/5 text-muted-foreground hover:bg-foreground/10 hover:text-white transition-colors disabled:opacity-50"
>
{label}
</button>
))}
</div>
</div>
);
/* ββ Mobile: bottom sheet with backdrop ββ */
if (isMobile) {
return (
<>
{/* Backdrop */}
<motion.div
initial={{ opacity: 0 }}
animate={{ opacity: 1 }}
exit={{ opacity: 0 }}
transition={{ duration: 0.15 }}
className="fixed inset-0 z-[9998] bg-black/40"
onClick={onClose}
/>
{/* Bottom sheet */}
<motion.div
initial={{ opacity: 0, y: 80 }}
animate={{ opacity: 1, y: 0 }}
exit={{ opacity: 0, y: 80 }}
transition={{ duration: 0.25, ease: [0.22, 1, 0.36, 1] }}
className="fixed bottom-0 left-0 right-0 z-[9999] selection-toolbar-sheet"
>
{/* Drag handle */}
<div className="flex justify-center pt-2 pb-1">
<div className="w-8 h-1 rounded-full bg-foreground/20" />
</div>
{innerContent}
</motion.div>
</>
);
}
/* ββ Desktop: floating popover ββ */
const clampedX = Math.min(Math.max(position.x, 180), window.innerWidth - 180);
const clampedY = Math.min(Math.max(position.y, 60), window.innerHeight - 200);
return (
<motion.div
initial={{ opacity: 0, y: 8, scale: 0.96 }}
animate={{ opacity: 1, y: 0, scale: 1 }}
exit={{ opacity: 0, y: 8, scale: 0.96 }}
transition={{ duration: 0.18, ease: [0.22, 1, 0.36, 1] }}
className="fixed z-[9999] selection-toolbar-popover"
style={{
left: `${clampedX}px`,
top: `${clampedY}px`,
transform: "translateX(-50%)",
}}
>
{innerContent}
</motion.div>
);
}
// Maximum history snapshots to keep
const MAX_HISTORY = 50;
/**
* Document editor with edit/preview modes, formatting toolbar, undo/redo, and AI editing
*/
export default function LabsEditor({
content,
onChange,
isProcessing,
onSelectionEdit
}) {
const [mode, setMode] = useState("edit");
const textareaRef = useRef(null);
const containerRef = useRef(null);
const [localContent, setLocalContent] = useState(content || "");
const debounceRef = useRef(null);
// Undo/Redo history
const [history, setHistory] = useState([]);
const [historyIndex, setHistoryIndex] = useState(-1);
const isUndoRedoRef = useRef(false);
// Selection state
const [selection, setSelection] = useState(null);
const [toolbarPosition, setToolbarPosition] = useState(null);
const [isEditing, setIsEditing] = useState(false);
const [showToolbar, setShowToolbar] = useState(false);
// Sync external content changes
useEffect(() => {
if (content !== localContent && !isUndoRedoRef.current) {
setLocalContent(content || "");
}
isUndoRedoRef.current = false;
}, [content]);
// Save snapshot to history (called before AI edits and periodically)
const saveSnapshot = useCallback(() => {
setHistory(prev => {
// Remove any "future" states if we're not at the end
const newHistory = prev.slice(0, historyIndex + 1);
// Add current state
newHistory.push({
content: localContent,
timestamp: Date.now()
});
// Limit history size
if (newHistory.length > MAX_HISTORY) {
newHistory.shift();
}
return newHistory;
});
setHistoryIndex(prev => Math.min(prev + 1, MAX_HISTORY - 1));
}, [localContent, historyIndex]);
// Undo function
const handleUndo = useCallback(() => {
if (historyIndex > 0) {
isUndoRedoRef.current = true;
const prevState = history[historyIndex - 1];
setLocalContent(prevState.content);
onChange(prevState.content);
setHistoryIndex(prev => prev - 1);
}
}, [history, historyIndex, onChange]);
// Redo function
const handleRedo = useCallback(() => {
if (historyIndex < history.length - 1) {
isUndoRedoRef.current = true;
const nextState = history[historyIndex + 1];
setLocalContent(nextState.content);
onChange(nextState.content);
setHistoryIndex(prev => prev + 1);
}
}, [history, historyIndex, onChange]);
// Debounced save with snapshot
const handleChange = useCallback((e) => {
const value = e.target.value;
setLocalContent(value);
if (debounceRef.current) {
clearTimeout(debounceRef.current);
}
debounceRef.current = setTimeout(() => {
onChange(value);
// Save snapshot every few seconds of inactivity
saveSnapshot();
}, 1000);
}, [onChange, saveSnapshot]);
// Cleanup debounce on unmount
useEffect(() => {
return () => {
if (debounceRef.current) {
clearTimeout(debounceRef.current);
}
};
}, []);
// Auto-resize textarea
useEffect(() => {
const textarea = textareaRef.current;
if (textarea && mode === "edit") {
textarea.style.height = "auto";
textarea.style.height = `${textarea.scrollHeight}px`;
}
}, [localContent, mode]);
// Handle formatting toolbar actions
const handleFormat = useCallback((prefix, suffix) => {
const textarea = textareaRef.current;
if (!textarea) return;
const start = textarea.selectionStart;
const end = textarea.selectionEnd;
const selectedText = textarea.value.substring(start, end);
let newText;
let newCursorPos;
if (selectedText) {
// Wrap selected text
newText = textarea.value.substring(0, start) +
prefix + selectedText + suffix +
textarea.value.substring(end);
newCursorPos = end + prefix.length + suffix.length;
} else {
// Insert at cursor
newText = textarea.value.substring(0, start) +
prefix + suffix +
textarea.value.substring(end);
newCursorPos = start + prefix.length;
}
setLocalContent(newText);
onChange(newText);
// Restore cursor position
requestAnimationFrame(() => {
textarea.focus();
textarea.setSelectionRange(newCursorPos, newCursorPos);
});
}, [onChange]);
// Handle text selection
const handleSelect = useCallback(() => {
const textarea = textareaRef.current;
if (!textarea) return;
const start = textarea.selectionStart;
const end = textarea.selectionEnd;
const selectedText = textarea.value.substring(start, end);
if (selectedText.length > 3) {
const rect = textarea.getBoundingClientRect();
const textBeforeSelection = textarea.value.substring(0, start);
const lines = textBeforeSelection.split('\n');
const currentLine = lines.length;
const lineHeight = parseInt(getComputedStyle(textarea).lineHeight) || 20;
const scrollTop = textarea.scrollTop;
const rawX = rect.left + rect.width / 2;
// Account for scroll position when calculating y
const rawY = rect.top + (currentLine * lineHeight) - scrollTop - 40;
// Clamp x so toolbar stays within viewport
const x = Math.min(Math.max(rawX, 160), window.innerWidth - 160);
// Clamp y to stay within the textarea's visible bounds
const y = Math.min(
Math.max(rawY, rect.top + 10),
rect.bottom - 50
);
setSelection({
start,
end,
text: selectedText
});
setToolbarPosition({ x, y });
} else {
closeToolbar();
}
}, []);
const closeToolbar = () => {
setSelection(null);
setToolbarPosition(null);
setShowToolbar(false);
setIsEditing(false);
};
// Handle selection edit submission
const handleSelectionEdit = async (instruction) => {
if (!selection || !onSelectionEdit) return;
// Save snapshot before AI edit
saveSnapshot();
setIsEditing(true);
try {
const contextBefore = localContent.substring(
Math.max(0, selection.start - 100),
selection.start
);
const contextAfter = localContent.substring(
selection.end,
Math.min(localContent.length, selection.end + 100)
);
const replacement = await onSelectionEdit({
selectedText: selection.text,
instruction,
contextBefore,
contextAfter
});
if (replacement) {
const newContent =
localContent.substring(0, selection.start) +
replacement +
localContent.substring(selection.end);
setLocalContent(newContent);
onChange(newContent);
}
} catch (error) {
console.error("[Labs] Selection edit failed:", error);
} finally {
setIsEditing(false);
closeToolbar();
}
};
// Keyboard shortcuts
useEffect(() => {
const handleKeyboard = (e) => {
if ((e.ctrlKey || e.metaKey) && e.key === 'z') {
e.preventDefault();
if (e.shiftKey) {
handleRedo();
} else {
handleUndo();
}
}
if ((e.ctrlKey || e.metaKey) && e.key === 'y') {
e.preventDefault();
handleRedo();
}
};
document.addEventListener('keydown', handleKeyboard);
return () => document.removeEventListener('keydown', handleKeyboard);
}, [handleUndo, handleRedo]);
// Close toolbar when clicking outside
useEffect(() => {
const handleClickOutside = (e) => {
if (containerRef.current && !containerRef.current.contains(e.target)) {
closeToolbar();
}
};
document.addEventListener("mousedown", handleClickOutside);
return () => document.removeEventListener("mousedown", handleClickOutside);
}, []);
const canUndo = historyIndex > 0;
const canRedo = historyIndex < history.length - 1;
return (
<div ref={containerRef} className="h-full flex flex-col relative">
{/* Mode Toggle & History Controls */}
<div className="flex items-center gap-1 px-4 py-2 border-b border-border bg-background/50">
<button
onClick={() => setMode("edit")}
className={cn(
"flex items-center gap-2 px-3 py-1.5 rounded-lg text-sm font-medium transition-colors",
mode === "edit"
? "bg-foreground/10 text-white"
: "text-muted-foreground hover:text-white hover:bg-foreground/5"
)}
>
<Edit3 size={14} />
Edit
</button>
<button
onClick={() => { setMode("preview"); closeToolbar(); }}
className={cn(
"flex items-center gap-2 px-3 py-1.5 rounded-lg text-sm font-medium transition-colors",
mode === "preview"
? "bg-foreground/10 text-white"
: "text-muted-foreground hover:text-white hover:bg-foreground/5"
)}
>
<Eye size={14} />
Preview
</button>
{/* Spacer */}
<div className="flex-1" />
{/* AI Edit button β always visible, enabled when text selected */}
{mode === "edit" && (
<button
onClick={() => {
if (selection) setShowToolbar(true);
}}
disabled={!selection || isProcessing || isEditing}
className={cn(
"flex items-center gap-1.5 px-3 py-1.5 rounded-lg text-xs font-semibold transition-all mr-1",
selection && !isProcessing && !isEditing
? "bg-primary/15 text-primary border border-primary/30 hover:bg-primary/25 shadow-sm shadow-primary/10"
: "bg-foreground/5 text-muted-foreground/40 border border-transparent cursor-not-allowed"
)}
title={selection ? "Edit selected text with AI" : "Select text to enable AI editing"}
>
<Sparkles size={13} />
<span className="hidden sm:inline">AI Edit</span>
</button>
)}
{/* Undo/Redo */}
<HistoryControls
canUndo={canUndo}
canRedo={canRedo}
onUndo={handleUndo}
onRedo={handleRedo}
historyCount={history.length}
/>
{/* Status */}
{(isProcessing || isEditing) && (
<div className="flex items-center gap-2 text-primary text-sm ml-2">
<motion.div
animate={{ rotate: 360 }}
transition={{ duration: 1, repeat: Infinity, ease: "linear" }}
className="w-4 h-4 border-2 border-primary border-t-transparent rounded-full"
/>
<span>{isEditing ? "Editing..." : "AI working..."}</span>
</div>
)}
</div>
{/* Formatting Toolbar (only in edit mode) */}
{mode === "edit" && (
<FormattingToolbar
onFormat={handleFormat}
disabled={isProcessing || isEditing}
/>
)}
{/* Editor/Preview Area */}
<div className="flex-1 overflow-auto custom-scrollbar">
{mode === "edit" ? (
<textarea
ref={textareaRef}
value={localContent}
onChange={handleChange}
onSelect={handleSelect}
onMouseUp={handleSelect}
placeholder="Start writing your document here...
You can use Markdown formatting:
# Heading 1
## Heading 2
**bold** and *italic*
- Bullet lists
1. Numbered lists
> Blockquotes
`inline code`
Tip: Use the toolbar above or select text for AI editing!"
disabled={isProcessing || isEditing}
className={cn(
"w-full min-h-full p-6 bg-transparent outline-none resize-none text-white font-mono text-sm leading-relaxed",
"placeholder:text-muted-foreground/50",
(isProcessing || isEditing) && "opacity-50"
)}
spellCheck="true"
/>
) : (
<MarkdownPreview content={localContent} />
)}
</div>
{/* Selection Toolbar β opens via AI Edit button */}
<AnimatePresence>
{showToolbar && selection && mode === "edit" && (
<SelectionToolbar
position={toolbarPosition}
selectedText={selection.text}
onClose={closeToolbar}
onSubmit={handleSelectionEdit}
isProcessing={isEditing}
/>
)}
</AnimatePresence>
</div>
);
}
|