-
Notifications
You must be signed in to change notification settings - Fork 57
feat(vault): add local file editing and terminal split view #42
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Vonder85
wants to merge
7
commits into
Charlie85270:main
Choose a base branch
from
Vonder85:edit-file-from-pc
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
fbbf13e
feat(vault): add local file editing and terminal split view
Vonder85 30b670a
resize && choose position
Vonder85 e3841bc
edit file
Vonder85 df279e1
fix: persist linked files across navigation using Zustand store
Vonder85 a11c752
fix: address all CodeRabbit review comments
Vonder85 1fc10ab
fix: address remaining CodeRabbit review comments (round 2)
Vonder85 ca8dceb
fix: update store content after successful save to avoid stale rehydr…
Vonder85 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
192 changes: 192 additions & 0 deletions
192
src/components/TerminalsView/components/FileEditorPanel.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,192 @@ | ||
| 'use client'; | ||
|
|
||
| import { useState, useRef, useCallback } from 'react'; | ||
| import { Save, X, Eye, Pencil, PanelTop, PanelBottom, PanelLeft, PanelRight } from 'lucide-react'; | ||
|
|
||
| export type DockPosition = 'top' | 'bottom' | 'left' | 'right'; | ||
|
|
||
| interface FileEditorPanelProps { | ||
| filePath: string; | ||
| filename: string; | ||
| content: string; | ||
| position: DockPosition; | ||
| onSave: (content: string) => void; | ||
| onClose: () => void; | ||
| onPositionChange: (position: DockPosition) => void; | ||
| } | ||
|
|
||
| type EditorTab = 'write' | 'preview'; | ||
|
|
||
| export default function FileEditorPanel({ filePath, filename, content: initialContent, position, onSave, onClose, onPositionChange }: FileEditorPanelProps) { | ||
| const [content, setContent] = useState(initialContent); | ||
| const [activeTab, setActiveTab] = useState<EditorTab>('write'); | ||
| const [saved, setSaved] = useState(true); | ||
| const textareaRef = useRef<HTMLTextAreaElement>(null); | ||
|
|
||
| const handleChange = useCallback((value: string) => { | ||
| setContent(value); | ||
| setSaved(false); | ||
| }, []); | ||
|
|
||
| const handleSave = useCallback(() => { | ||
| onSave(content); | ||
| setSaved(true); | ||
| }, [content, onSave]); | ||
|
|
||
| const handleKeyDown = useCallback((e: React.KeyboardEvent) => { | ||
| if ((e.metaKey || e.ctrlKey) && e.key === 's') { | ||
| e.preventDefault(); | ||
| handleSave(); | ||
| } | ||
| }, [handleSave]); | ||
|
|
||
| // Handle paste with image support | ||
| const handlePaste = useCallback(async (e: React.ClipboardEvent<HTMLTextAreaElement>) => { | ||
| const items = e.clipboardData?.items; | ||
| if (!items) return; | ||
|
|
||
| for (const item of Array.from(items)) { | ||
| if (item.type.startsWith('image/')) { | ||
| e.preventDefault(); | ||
| const blob = item.getAsFile(); | ||
| if (!blob) return; | ||
|
|
||
| // Convert to data URL | ||
| const reader = new FileReader(); | ||
| reader.onload = async () => { | ||
| const dataUrl = reader.result as string; | ||
| if (!window.electronAPI?.vault?.saveClipboardImage) return; | ||
|
|
||
| // Save to same directory as the file being edited | ||
| const dirPath = filePath.replace(/\\/g, '/').split('/').slice(0, -1).join('/'); | ||
| const result = await window.electronAPI.vault.saveClipboardImage({ | ||
| imageDataUrl: dataUrl, | ||
| targetDir: dirPath || undefined, | ||
| }); | ||
|
|
||
| if (result.success && result.filePath && result.filename) { | ||
| const textarea = textareaRef.current; | ||
| if (!textarea) return; | ||
| const start = textarea.selectionStart; | ||
| const end = textarea.selectionEnd; | ||
| const mdImage = ``; | ||
| setContent(prev => { | ||
| const newContent = prev.slice(0, start) + mdImage + prev.slice(end); | ||
| return newContent; | ||
| }); | ||
| setSaved(false); | ||
|
|
||
| const newPos = start + mdImage.length; | ||
| requestAnimationFrame(() => { | ||
| textarea.focus(); | ||
| textarea.setSelectionRange(newPos, newPos); | ||
| }); | ||
| } | ||
| }; | ||
| reader.readAsDataURL(blob); | ||
| return; | ||
| } | ||
| } | ||
| }, [filePath]); | ||
|
|
||
| return ( | ||
| <div | ||
| className={`flex flex-col h-full overflow-hidden ${position === 'top' || position === 'bottom' ? 'border-t border-b' : 'border-l border-r'} border-border`} | ||
| onKeyDown={handleKeyDown} | ||
| onClick={(e) => e.stopPropagation()} | ||
| onMouseDown={(e) => e.stopPropagation()} | ||
| > | ||
| {/* Header */} | ||
| <div className="flex items-center gap-1 px-2 py-1 bg-secondary border-b border-border select-none shrink-0"> | ||
| <span className="text-[10px] font-medium text-foreground truncate flex-1" title={filePath}> | ||
| {filename} | ||
| </span> | ||
| {!saved && ( | ||
| <span className="text-[9px] text-amber-400 font-medium">modified</span> | ||
| )} | ||
|
|
||
| {/* Tab toggle */} | ||
| <div className="flex items-center bg-secondary/50 rounded p-0.5"> | ||
| <button | ||
| onClick={() => setActiveTab('write')} | ||
| className={`p-0.5 rounded ${activeTab === 'write' ? 'bg-card text-foreground shadow-sm' : 'text-muted-foreground hover:text-foreground'}`} | ||
| title="Edit" | ||
| > | ||
| <Pencil className="w-2.5 h-2.5" /> | ||
| </button> | ||
| <button | ||
| onClick={() => setActiveTab('preview')} | ||
| className={`p-0.5 rounded ${activeTab === 'preview' ? 'bg-card text-foreground shadow-sm' : 'text-muted-foreground hover:text-foreground'}`} | ||
| title="Preview" | ||
| > | ||
| <Eye className="w-2.5 h-2.5" /> | ||
| </button> | ||
| </div> | ||
|
|
||
| {/* Position buttons */} | ||
| <div className="flex items-center bg-secondary/50 rounded p-0.5"> | ||
| <button | ||
| onClick={() => onPositionChange('top')} | ||
| className={`p-0.5 rounded ${position === 'top' ? 'bg-card text-foreground shadow-sm' : 'text-muted-foreground hover:text-foreground'}`} | ||
| title="Dock top" | ||
| > | ||
| <PanelTop className="w-2.5 h-2.5" /> | ||
| </button> | ||
| <button | ||
| onClick={() => onPositionChange('bottom')} | ||
| className={`p-0.5 rounded ${position === 'bottom' ? 'bg-card text-foreground shadow-sm' : 'text-muted-foreground hover:text-foreground'}`} | ||
| title="Dock bottom" | ||
| > | ||
| <PanelBottom className="w-2.5 h-2.5" /> | ||
| </button> | ||
| <button | ||
| onClick={() => onPositionChange('left')} | ||
| className={`p-0.5 rounded ${position === 'left' ? 'bg-card text-foreground shadow-sm' : 'text-muted-foreground hover:text-foreground'}`} | ||
| title="Dock left" | ||
| > | ||
| <PanelLeft className="w-2.5 h-2.5" /> | ||
| </button> | ||
| <button | ||
| onClick={() => onPositionChange('right')} | ||
| className={`p-0.5 rounded ${position === 'right' ? 'bg-card text-foreground shadow-sm' : 'text-muted-foreground hover:text-foreground'}`} | ||
| title="Dock right" | ||
| > | ||
| <PanelRight className="w-2.5 h-2.5" /> | ||
| </button> | ||
| </div> | ||
|
|
||
| <button | ||
| onClick={handleSave} | ||
| disabled={saved} | ||
| className="p-0.5 hover:bg-primary/10 transition-colors text-muted-foreground hover:text-foreground disabled:opacity-30" | ||
| title="Save (⌘S)" | ||
| > | ||
| <Save className="w-2.5 h-2.5" /> | ||
| </button> | ||
| <button | ||
| onClick={onClose} | ||
| className="p-0.5 hover:bg-primary/10 transition-colors text-muted-foreground hover:text-red-400" | ||
| title="Close file" | ||
| > | ||
| <X className="w-2.5 h-2.5" /> | ||
| </button> | ||
| </div> | ||
|
|
||
| {/* Content */} | ||
| {activeTab === 'write' ? ( | ||
| <textarea | ||
| ref={textareaRef} | ||
| value={content} | ||
| onChange={(e) => handleChange(e.target.value)} | ||
| onPaste={handlePaste} | ||
| className="flex-1 w-full text-xs bg-[#1a1a2e] text-foreground font-mono p-3 outline-none border-none resize-none leading-relaxed" | ||
| spellCheck={false} | ||
| /> | ||
| ) : ( | ||
| <div className="flex-1 overflow-y-auto p-3 text-xs text-foreground bg-[#1a1a2e]"> | ||
| <pre className="whitespace-pre-wrap font-mono">{content}</pre> | ||
| </div> | ||
| )} | ||
| </div> | ||
| ); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.