|
| 1 | +import { LANGUAGES } from '@vivliostyle/cli/constants'; |
| 2 | +import { useMolecule } from 'bunshi/react'; |
| 3 | +import { useCallback, useState } from 'react'; |
| 4 | +import { useSnapshot } from 'valtio'; |
| 5 | + |
| 6 | +import { |
| 7 | + Command, |
| 8 | + CommandEmpty, |
| 9 | + CommandGroup, |
| 10 | + CommandInput, |
| 11 | + CommandItem, |
| 12 | + CommandList, |
| 13 | +} from '@v/ui/command'; |
| 14 | +import { |
| 15 | + StackedRadioGroup, |
| 16 | + StackedRadioGroupItem, |
| 17 | +} from '@v/ui/custom/stacked-radio'; |
| 18 | +import { ChevronDownIcon } from '@v/ui/icon'; |
| 19 | +import { Input } from '@v/ui/input'; |
| 20 | +import { cn } from '@v/ui/lib/utils'; |
| 21 | +import { Popover, PopoverContent, PopoverTrigger } from '@v/ui/popover'; |
| 22 | +import { useLiveInputField } from '../../../hooks/use-live-field'; |
| 23 | +import { $draftProject } from '../../../stores/accessors'; |
| 24 | +import { setupProjectFromDraft } from '../../../stores/actions/setup-project-from-draft'; |
| 25 | +import type { ProjectId } from '../../../stores/proxies/project'; |
| 26 | +import { Theme } from '../../../stores/proxies/theme'; |
| 27 | +import { TemplateStoreMolecule } from './store'; |
| 28 | + |
| 29 | +function BookTitleInput({ children }: React.PropsWithChildren) { |
| 30 | + const inputProps = useLiveInputField( |
| 31 | + () => $draftProject.valueOrThrow().bibliography.title, |
| 32 | + { |
| 33 | + onSave: (value) => { |
| 34 | + const title = value.trim(); |
| 35 | + $draftProject.valueOrThrow().bibliography.title = title; |
| 36 | + return title; |
| 37 | + }, |
| 38 | + }, |
| 39 | + ); |
| 40 | + |
| 41 | + return ( |
| 42 | + <label className="grid gap-2"> |
| 43 | + {children} |
| 44 | + <div> |
| 45 | + <Input type="text" name="bookTitle" {...inputProps} /> |
| 46 | + </div> |
| 47 | + </label> |
| 48 | + ); |
| 49 | +} |
| 50 | + |
| 51 | +function AuthorInput({ children }: React.PropsWithChildren) { |
| 52 | + const inputProps = useLiveInputField( |
| 53 | + () => $draftProject.valueOrThrow().bibliography.author, |
| 54 | + { |
| 55 | + onSave: (value) => { |
| 56 | + const author = value.trim(); |
| 57 | + $draftProject.valueOrThrow().bibliography.author = author; |
| 58 | + return author; |
| 59 | + }, |
| 60 | + }, |
| 61 | + ); |
| 62 | + |
| 63 | + return ( |
| 64 | + <label className="grid gap-2"> |
| 65 | + {children} |
| 66 | + <div> |
| 67 | + <Input type="text" name="author" {...inputProps} /> |
| 68 | + </div> |
| 69 | + </label> |
| 70 | + ); |
| 71 | +} |
| 72 | + |
| 73 | +function LanguageSelect({ children }: React.PropsWithChildren) { |
| 74 | + const snap = useSnapshot($draftProject).valueOrThrow(); |
| 75 | + const [open, setOpen] = useState(false); |
| 76 | + |
| 77 | + const handleSelect = useCallback( |
| 78 | + (value: string) => { |
| 79 | + setOpen(false); |
| 80 | + $draftProject.valueOrThrow().bibliography.language = value; |
| 81 | + }, |
| 82 | + [snap], |
| 83 | + ); |
| 84 | + |
| 85 | + return ( |
| 86 | + <div className="grid gap-2"> |
| 87 | + {children} |
| 88 | + <Popover open={open} onOpenChange={setOpen}> |
| 89 | + {/** biome-ignore lint/a11y/useSemanticElements: Combobox with search */} |
| 90 | + <PopoverTrigger |
| 91 | + role="combobox" |
| 92 | + className={cn( |
| 93 | + "border-input data-[placeholder]:text-muted-foreground [&_svg:not([class*='text-'])]:text-muted-foreground focus-visible:border-ring focus-visible:ring-ring/50 aria-invalid:ring-destructive/20 dark:aria-invalid:ring-destructive/40 aria-invalid:border-destructive dark:bg-input/30 dark:hover:bg-input/50 flex w-fit items-center justify-between gap-2 rounded-md border bg-transparent px-3 py-2 text-sm whitespace-nowrap shadow-xs transition-[color,box-shadow] outline-none focus-visible:ring-[3px] disabled:cursor-not-allowed disabled:opacity-50 data-[size=default]:h-9 data-[size=sm]:h-8 *:data-[slot=select-value]:line-clamp-1 *:data-[slot=select-value]:flex *:data-[slot=select-value]:items-center *:data-[slot=select-value]:gap-2 [&_svg]:pointer-events-none [&_svg]:shrink-0 [&_svg:not([class*='size-'])]:size-4", |
| 94 | + 'w-xs', |
| 95 | + )} |
| 96 | + > |
| 97 | + {LANGUAGES[snap.bibliography.language as keyof typeof LANGUAGES] || |
| 98 | + 'Select language'} |
| 99 | + <ChevronDownIcon className="ml-2 size-4 shrink-0 opacity-50" /> |
| 100 | + </PopoverTrigger> |
| 101 | + <PopoverContent className="w-xs p-0"> |
| 102 | + <Command> |
| 103 | + <CommandInput placeholder="Search language..." /> |
| 104 | + <CommandList> |
| 105 | + <CommandEmpty>No language found.</CommandEmpty> |
| 106 | + <CommandGroup> |
| 107 | + {Object.entries(LANGUAGES).map(([code, name]) => ( |
| 108 | + <CommandItem key={code} value={code} onSelect={handleSelect}> |
| 109 | + {name} |
| 110 | + <span aria-hidden="true" className="text-muted-foreground"> |
| 111 | + ({code}) |
| 112 | + </span> |
| 113 | + </CommandItem> |
| 114 | + ))} |
| 115 | + </CommandGroup> |
| 116 | + </CommandList> |
| 117 | + </Command> |
| 118 | + </PopoverContent> |
| 119 | + </Popover> |
| 120 | + </div> |
| 121 | + ); |
| 122 | +} |
| 123 | + |
| 124 | +function ThemeSelect({ children }: React.PropsWithChildren) { |
| 125 | + const snap = useSnapshot($draftProject).valueOrThrow(); |
| 126 | + |
| 127 | + const handleSelect = useCallback((value: string) => { |
| 128 | + $draftProject.valueOrThrow().theme.packageName = value; |
| 129 | + }, []); |
| 130 | + |
| 131 | + return ( |
| 132 | + <div className="grid gap-2"> |
| 133 | + {children} |
| 134 | + <StackedRadioGroup |
| 135 | + className="grid-cols-2" |
| 136 | + value={snap.theme.packageName} |
| 137 | + onValueChange={handleSelect} |
| 138 | + > |
| 139 | + {Object.entries(Theme.officialThemes).map(([value, { title }]) => ( |
| 140 | + <StackedRadioGroupItem key={value} value={value}> |
| 141 | + {title} |
| 142 | + </StackedRadioGroupItem> |
| 143 | + ))} |
| 144 | + </StackedRadioGroup> |
| 145 | + </div> |
| 146 | + ); |
| 147 | +} |
| 148 | + |
| 149 | +export function ProjectDetailForm({ children }: React.PropsWithChildren) { |
| 150 | + const { templateStoreProxy } = useMolecule(TemplateStoreMolecule); |
| 151 | + |
| 152 | + return ( |
| 153 | + <form |
| 154 | + className="contents" |
| 155 | + onSubmit={async (e) => { |
| 156 | + e.preventDefault(); |
| 157 | + if (!templateStoreProxy.selected) { |
| 158 | + return; |
| 159 | + } |
| 160 | + await setupProjectFromDraft({ |
| 161 | + projectId: 'alpha-v1' as ProjectId, |
| 162 | + templateValue: templateStoreProxy.selected, |
| 163 | + }); |
| 164 | + }} |
| 165 | + > |
| 166 | + <section className="grid gap-4"> |
| 167 | + <h3 className="text-xl font-bold">Project Details</h3> |
| 168 | + |
| 169 | + <p className="text-sm"> |
| 170 | + All fields are optional and can be changed later in project settings. |
| 171 | + </p> |
| 172 | + |
| 173 | + <BookTitleInput> |
| 174 | + <span className="text-l font-bold">Book title</span> |
| 175 | + </BookTitleInput> |
| 176 | + |
| 177 | + <AuthorInput> |
| 178 | + <span className="text-l font-bold">Author</span> |
| 179 | + </AuthorInput> |
| 180 | + |
| 181 | + <LanguageSelect> |
| 182 | + <span className="text-l font-bold">Language</span> |
| 183 | + </LanguageSelect> |
| 184 | + |
| 185 | + <ThemeSelect> |
| 186 | + <span className="text-l font-bold">Theme</span> |
| 187 | + </ThemeSelect> |
| 188 | + </section> |
| 189 | + |
| 190 | + {children} |
| 191 | + </form> |
| 192 | + ); |
| 193 | +} |
0 commit comments