Download ui/src/App.tsx from Kazzze/sd-text-fix: direct link, hf CLI and curl.
- Browser
- Download file 6.99 kB
-
https://huggingface.co/Kazzze/sd-text-fix/resolve/main/ui/src/App.tsx
- Command line
-
hf download hf://Kazzze/sd-text-fix/ui/src/App.tsx
-
curl -L -o App.tsx https://huggingface.co/Kazzze/sd-text-fix/resolve/main/ui/src/App.tsx
6.99 kB
| import { useEffect, useRef } from 'react' | |
| import { CircleAlert, CircleCheck, Info, TriangleAlert } from 'lucide-react' | |
| import { api } from './api' | |
| import { onHostMessage, tellHost } from './bridge' | |
| import { declareFonts } from './fonts' | |
| import { useStore } from './store' | |
| import { Canvas } from './components/Canvas' | |
| import { SidePanel } from './components/SidePanel' | |
| import { TopBar } from './components/TopBar' | |
| import { TooltipProvider } from './components/ui/controls' | |
| import { cn } from './lib/utils' | |
| const TONES = { | |
| info: { icon: Info, className: 'text-muted-foreground' }, | |
| success: { icon: CircleCheck, className: 'text-chosen' }, | |
| warning: { icon: TriangleAlert, className: 'text-warning' }, | |
| error: { icon: CircleAlert, className: 'text-destructive' }, | |
| } | |
| function StatusBar() { | |
| const status = useStore((state) => state.status) | |
| const session = useStore((state) => state.session) | |
| const tone = TONES[status.tone] | |
| const Icon = tone.icon | |
| return ( | |
| <footer className="flex h-8 shrink-0 items-center gap-2 border-t bg-card px-3 text-xs"> | |
| <Icon className={cn('size-3.5 shrink-0', tone.className)} /> | |
| <span className={cn('truncate', status.tone === 'info' ? 'text-muted-foreground' : tone.className)}> | |
| {status.text} | |
| </span> | |
| {session && ( | |
| <span className="ml-auto shrink-0 text-muted-foreground tabular-nums"> | |
| {session.name ? `${session.name} · ` : ''} | |
| {session.width} × {session.height} | |
| </span> | |
| )} | |
| </footer> | |
| ) | |
| } | |
| export default function App() { | |
| const theme = useStore((state) => state.theme) | |
| const items = useStore((state) => state.items) | |
| const look = useStore((state) => state.look) | |
| const model = useStore((state) => state.model) | |
| const reach = useStore((state) => state.reach) | |
| const showFound = useStore((state) => state.showFound) | |
| const search = useStore((state) => state.search) | |
| const readOld = useStore((state) => state.readOld) | |
| const options = useStore((state) => state.options) | |
| const fileInput = useRef<HTMLInputElement>(null) | |
| // Start: the options, then tell the host the page is ready for its image. | |
| useEffect(() => { | |
| const { init, setStatus } = useStore.getState() | |
| api | |
| .options() | |
| .then((options) => { | |
| declareFonts(options.fonts) | |
| init(options) | |
| tellHost({ type: 'TF_READY' }) | |
| }) | |
| .catch((error: Error) => setStatus(`The extension's server does not answer: ${error.message}`, 'error')) | |
| return onHostMessage((message) => { | |
| const state = useStore.getState() | |
| state.setTheme(message.theme) | |
| if (message.tab) state.setTab(message.tab) | |
| if (message.node) void state.openNode(message.node.id, message.node.state) | |
| else if (message.image?.url) void state.openUrl(message.image.url, message.image.name) | |
| }) | |
| }, []) | |
| useEffect(() => { | |
| document.documentElement.classList.toggle('dark', theme === 'dark') | |
| }, [theme]) | |
| // The preview follows every change of the texts and the look, a moment after the last one. | |
| const textKey = items | |
| .map((item) => `${item.key}:${item.erase ? 1 : 0}:${JSON.stringify(item.recipe)}:${item.text}`) | |
| .join('\n') | |
| const lookKey = JSON.stringify(look) | |
| useEffect(() => { | |
| const timer = window.setTimeout(() => void useStore.getState().preview(), 160) | |
| return () => window.clearTimeout(timer) | |
| }, [textKey, lookKey]) | |
| // The settings are kept between sessions. | |
| const settingsKey = JSON.stringify({ look, model, reach, showFound, search, readOld }) | |
| useEffect(() => { | |
| if (!options) return | |
| const timer = window.setTimeout(() => { | |
| void api.saveSettings({ look, model, reach, showFound, search, readOld }).catch(() => undefined) | |
| }, 800) | |
| return () => window.clearTimeout(timer) | |
| // eslint-disable-next-line react-hooks/exhaustive-deps | |
| }, [settingsKey, options]) | |
| // Files dropped or pasted onto the page. | |
| useEffect(() => { | |
| const openFile = (file: File | null | undefined) => { | |
| if (file && file.type.startsWith('image/')) void useStore.getState().openFile(file, file.name) | |
| } | |
| const onDrop = (event: DragEvent) => { | |
| event.preventDefault() | |
| openFile(event.dataTransfer?.files?.[0]) | |
| } | |
| const onDragOver = (event: DragEvent) => event.preventDefault() | |
| const onPaste = (event: ClipboardEvent) => { | |
| const target = event.target as HTMLElement | null | |
| if (target && /^(INPUT|TEXTAREA)$/.test(target.tagName)) return | |
| const file = Array.from(event.clipboardData?.files ?? []).find((entry) => entry.type.startsWith('image/')) | |
| if (file) { | |
| event.preventDefault() | |
| openFile(file) | |
| } | |
| } | |
| window.addEventListener('drop', onDrop) | |
| window.addEventListener('dragover', onDragOver) | |
| window.addEventListener('paste', onPaste) | |
| return () => { | |
| window.removeEventListener('drop', onDrop) | |
| window.removeEventListener('dragover', onDragOver) | |
| window.removeEventListener('paste', onPaste) | |
| } | |
| }, []) | |
| // Keys: Esc closes, Ctrl+Enter blends, O shows the original and the result in turn. | |
| useEffect(() => { | |
| const onKey = (event: KeyboardEvent) => { | |
| const state = useStore.getState() | |
| const target = event.target as HTMLElement | null | |
| const typing = !!target && /^(INPUT|TEXTAREA|SELECT)$/.test(target.tagName) | |
| if (event.key === 'Escape' && !typing && !document.querySelector('[data-radix-popper-content-wrapper]')) { | |
| if (state.mode === 'brush' || state.mode === 'path' || state.mode === 'warp') state.setMode('new') | |
| else tellHost({ type: 'TF_CLOSE' }) | |
| } else if (event.key === 'Enter' && (event.ctrlKey || event.metaKey)) { | |
| event.preventDefault() | |
| void state.blend() | |
| } else if (!typing && (event.key === 'o' || event.key === 'O') && state.resultVersion !== null) { | |
| state.setView(state.view === 'image' ? 'result' : 'image') | |
| } | |
| } | |
| window.addEventListener('keydown', onKey) | |
| return () => window.removeEventListener('keydown', onKey) | |
| }, []) | |
| const openFile = () => fileInput.current?.click() | |
| return ( | |
| <TooltipProvider delayDuration={300}> | |
| <div className="flex h-full flex-col"> | |
| <TopBar onOpenFile={openFile} /> | |
| <div className="flex min-h-0 flex-1"> | |
| <div className="flex min-w-0 flex-1 flex-col"> | |
| <Canvas onOpenFile={openFile} /> | |
| <StatusBar /> | |
| </div> | |
| <SidePanel /> | |
| </div> | |
| <input | |
| ref={fileInput} | |
| type="file" | |
| accept="image/*" | |
| className="hidden" | |
| onChange={(event) => { | |
| const file = event.target.files?.[0] | |
| if (file) void useStore.getState().openFile(file, file.name) | |
| event.target.value = '' | |
| }} | |
| /> | |
| </div> | |
| </TooltipProvider> | |
| ) | |
| } | |