-
-
Notifications
You must be signed in to change notification settings - Fork 263
feat(core): floating image action panel in inspector #148
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@open-slide/core": minor | ||
| --- | ||
|
|
||
| Show a floating action panel below the inspector selection box when an image is selected, with quick-access Replace and Crop icons. | ||
196 changes: 196 additions & 0 deletions
196
packages/core/src/app/components/inspector/asset-picker-dialog.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,196 @@ | ||
| import { ArrowDownToLine, Loader2, Upload } from 'lucide-react'; | ||
| import type React from 'react'; | ||
| import { useCallback, useId, useRef, useState } from 'react'; | ||
| import { toast } from 'sonner'; | ||
| import { | ||
| Dialog, | ||
| DialogContent, | ||
| DialogDescription, | ||
| DialogHeader, | ||
| DialogTitle, | ||
| } from '@/components/ui/dialog'; | ||
| import { Tabs, TabsList, TabsTrigger } from '@/components/ui/tabs'; | ||
| import { type AssetEntry, uploadWithAutoRename, useAssets } from '@/lib/assets'; | ||
| import { format, useLocale } from '@/lib/use-locale'; | ||
| import { cn } from '@/lib/utils'; | ||
|
|
||
| export type PickerScope = 'slide' | 'global'; | ||
| const GLOBAL_PICKER_SLIDE_ID = '@global'; | ||
|
|
||
| export function AssetPickerDialog({ | ||
| slideId, | ||
| onClose, | ||
| onPick, | ||
| }: { | ||
| slideId: string; | ||
| onClose: () => void; | ||
| onPick: (asset: AssetEntry, scope: PickerScope) => void; | ||
| }) { | ||
| const [scope, setScope] = useState<PickerScope>('slide'); | ||
| const effectiveSlideId = scope === 'global' ? GLOBAL_PICKER_SLIDE_ID : slideId; | ||
| const { assets, loading, refresh } = useAssets(effectiveSlideId); | ||
| const images = assets.filter((a) => a.mime.startsWith('image/')); | ||
| const t = useLocale(); | ||
| const path = scope === 'global' ? 'assets/' : `slides/${slideId}/assets/`; | ||
| const [descPrefix, descSuffix] = t.inspector.replaceImageDescription.split('{path}'); | ||
| const [uploading, setUploading] = useState(false); | ||
| const [dragActive, setDragActive] = useState(false); | ||
| const dragDepth = useRef(0); | ||
| const inputId = useId(); | ||
|
|
||
| const handleFile = useCallback( | ||
| async (file: File) => { | ||
| if (!file.type.startsWith('image/')) return; | ||
| setUploading(true); | ||
| try { | ||
| const { ok, status, entry } = await uploadWithAutoRename(effectiveSlideId, file); | ||
| if (!ok || !entry) { | ||
| toast.error(format(t.asset.toastUploadFailed, { status })); | ||
| return; | ||
| } | ||
| await refresh().catch(() => {}); | ||
| onPick(entry, scope); | ||
| } finally { | ||
| setUploading(false); | ||
| } | ||
| }, | ||
| [effectiveSlideId, scope, refresh, onPick, t], | ||
| ); | ||
|
|
||
| return ( | ||
| <Dialog open onOpenChange={(o) => !o && onClose()}> | ||
| <DialogContent className="sm:max-w-xl"> | ||
| <DialogHeader> | ||
| <DialogTitle>{t.inspector.replaceImageDialogTitle}</DialogTitle> | ||
| <DialogDescription> | ||
| {descPrefix} | ||
| <span className="font-mono">{path}</span> | ||
| {descSuffix} | ||
| </DialogDescription> | ||
| </DialogHeader> | ||
| <Tabs value={scope} onValueChange={(next) => setScope(next as PickerScope)}> | ||
| <TabsList> | ||
| <TabsTrigger value="slide">{t.asset.scopeSlide}</TabsTrigger> | ||
| <TabsTrigger value="global">{t.asset.scopeGlobal}</TabsTrigger> | ||
| </TabsList> | ||
| </Tabs> | ||
| <label | ||
| htmlFor={inputId} | ||
| className={cn( | ||
| 'absolute right-12 top-3.5 inline-flex h-7 cursor-pointer items-center gap-1.5 rounded-[5px] border border-border bg-card px-2 text-[12px] font-medium transition-colors', | ||
| 'hover:bg-muted/60 hover:border-foreground/20 active:translate-y-px', | ||
| uploading && 'pointer-events-none opacity-60', | ||
| )} | ||
| > | ||
| {uploading ? ( | ||
| <Loader2 className="size-3.5 animate-spin" /> | ||
| ) : ( | ||
| <Upload className="size-3.5" /> | ||
| )} | ||
| <span>{t.asset.upload}</span> | ||
| </label> | ||
| <input | ||
| id={inputId} | ||
| type="file" | ||
| accept="image/*" | ||
| className="sr-only" | ||
| disabled={uploading} | ||
| onChange={(e) => { | ||
| const file = e.target.files?.[0]; | ||
| e.target.value = ''; | ||
| if (file) handleFile(file).catch(() => {}); | ||
| }} | ||
| /> | ||
| <section | ||
| aria-label={t.inspector.replaceImageDialogTitle} | ||
| className="relative max-h-[60vh] overflow-y-auto" | ||
| onDragEnter={(e) => { | ||
| if (uploading || !hasFiles(e)) return; | ||
| e.preventDefault(); | ||
| dragDepth.current += 1; | ||
| setDragActive(true); | ||
| }} | ||
| onDragOver={(e) => { | ||
| if (uploading || !hasFiles(e)) return; | ||
| e.preventDefault(); | ||
| e.dataTransfer.dropEffect = 'copy'; | ||
| }} | ||
| onDragLeave={() => { | ||
| dragDepth.current = Math.max(0, dragDepth.current - 1); | ||
| if (dragDepth.current === 0) setDragActive(false); | ||
| }} | ||
| onDrop={(e) => { | ||
| if (uploading || !hasFiles(e)) return; | ||
| e.preventDefault(); | ||
| dragDepth.current = 0; | ||
| setDragActive(false); | ||
| const file = e.dataTransfer.files?.[0]; | ||
| if (file) handleFile(file).catch(() => {}); | ||
| }} | ||
| > | ||
| {loading ? ( | ||
| <p className="px-1 py-6 text-center text-xs text-muted-foreground"> | ||
| {t.inspector.pickerLoading} | ||
| </p> | ||
| ) : images.length === 0 ? ( | ||
| <p className="px-1 py-6 text-center text-xs text-muted-foreground"> | ||
| {t.inspector.pickerEmpty} | ||
| </p> | ||
| ) : ( | ||
| <div className="grid grid-cols-[repeat(auto-fill,minmax(120px,1fr))] gap-3"> | ||
| {images.map((asset) => ( | ||
| <button | ||
| key={asset.name} | ||
| type="button" | ||
| onClick={() => onPick(asset, scope)} | ||
| className={cn( | ||
| 'group flex flex-col overflow-hidden rounded-lg border bg-card text-left shadow-sm transition-all', | ||
| 'hover:-translate-y-0.5 hover:shadow-md focus-visible:ring-2 focus-visible:ring-ring/50 focus-visible:outline-none', | ||
| )} | ||
| > | ||
| <div className="flex aspect-square w-full items-center justify-center overflow-hidden bg-[repeating-conic-gradient(theme(colors.muted)_0_25%,transparent_0_50%)] bg-[length:12px_12px]"> | ||
| <img | ||
| src={asset.url} | ||
| alt="" | ||
| className="size-full object-contain" | ||
| draggable={false} | ||
| /> | ||
| </div> | ||
| <div className="border-t px-2 py-1.5"> | ||
| <div className="truncate text-[11px] font-medium" title={asset.name}> | ||
| {asset.name} | ||
| </div> | ||
| </div> | ||
| </button> | ||
| ))} | ||
| </div> | ||
| )} | ||
| {dragActive && ( | ||
| <div | ||
| className="pointer-events-none absolute inset-0 z-10 animate-in fade-in-0 duration-200" | ||
| aria-hidden | ||
| > | ||
| <div className="absolute inset-0 bg-brand/5" /> | ||
| <div className="absolute inset-1 rounded-[8px] border border-dashed border-brand/40" /> | ||
| <div className="absolute inset-x-0 bottom-4 flex justify-center"> | ||
| <div className="flex items-center gap-2 rounded-[6px] border border-border bg-card px-3 py-1.5 text-[12px] font-medium shadow-floating"> | ||
| <ArrowDownToLine className="size-3.5 text-brand" /> | ||
| <span>{t.asset.dropToUpload}</span> | ||
| </div> | ||
| </div> | ||
| </div> | ||
| )} | ||
| </section> | ||
| </DialogContent> | ||
| </Dialog> | ||
| ); | ||
| } | ||
|
|
||
| function hasFiles(e: React.DragEvent): boolean { | ||
| const types = e.dataTransfer?.types; | ||
| if (!types) return false; | ||
| for (let i = 0; i < types.length; i++) { | ||
| if (types[i] === 'Files') return true; | ||
| } | ||
| return false; | ||
| } |
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use present-tense descriptive wording in the changeset line.
Line 5 reads like an imperative (“Show ...”) rather than a present-tense description. Please switch to descriptive present tense.
Suggested wording
As per coding guidelines, "Changeset descriptions must be short and direct: one line, present-tense, describing what changed from a user's perspective. No paragraphs, no rationale, no 'this PR…'."
📝 Committable suggestion
🤖 Prompt for AI Agents