fix: adapt article editor for mobile
This commit is contained in:
parent
cb3e1e0b20
commit
743b65f917
3 changed files with 193 additions and 29 deletions
|
|
@ -8,6 +8,8 @@ import {
|
|||
AlertTriangle,
|
||||
ArrowLeft,
|
||||
CheckCircle2,
|
||||
ChevronDown,
|
||||
ChevronLeft,
|
||||
Clock3,
|
||||
Copy,
|
||||
ExternalLink,
|
||||
|
|
@ -1210,6 +1212,8 @@ export default function App() {
|
|||
const [publishAt, setPublishAt] = useState("")
|
||||
const [editorDirty, setEditorDirty] = useState(false)
|
||||
const [editorBusy, setEditorBusy] = useState(false)
|
||||
const [articleCloseConfirmOpen, setArticleCloseConfirmOpen] = useState(false)
|
||||
const [articleMoreOpen, setArticleMoreOpen] = useState(false)
|
||||
const [featureImageBusy, setFeatureImageBusy] = useState(false)
|
||||
const [featureImageDragActive, setFeatureImageDragActive] = useState(false)
|
||||
const [featureImageHoverActive, setFeatureImageHoverActive] = useState(false)
|
||||
|
|
@ -1237,6 +1241,8 @@ export default function App() {
|
|||
const selectedArticleRef = useRef<ArticleDetail | null>(null)
|
||||
const editorTitleRef = useRef("")
|
||||
const titleTextareaRef = useRef<HTMLTextAreaElement | null>(null)
|
||||
const articleBackButtonRef = useRef<HTMLButtonElement | null>(null)
|
||||
const articleMoreRef = useRef<HTMLDetailsElement | null>(null)
|
||||
const articlesOffsetRef = useRef(0)
|
||||
const articlesLoadingRef = useRef(false)
|
||||
const articlesHasMoreRef = useRef(false)
|
||||
|
|
@ -1627,6 +1633,8 @@ export default function App() {
|
|||
setEditorTitle(result.title)
|
||||
setPublishAt(toDateTimeLocalValue(result.publishedAt) || currentDateTimeLocalValue())
|
||||
setEditorDirty(false)
|
||||
setArticleCloseConfirmOpen(false)
|
||||
setArticleMoreOpen(false)
|
||||
appliedImageGenerationJobsRef.current = new Set()
|
||||
try {
|
||||
const active = await fetch(`/api/articles/${id}/image-generations/active`)
|
||||
|
|
@ -1840,6 +1848,28 @@ export default function App() {
|
|||
}
|
||||
}, [selectedArticle?.id, selectedArticle?.videoSourceUrl])
|
||||
|
||||
useEffect(() => {
|
||||
if (!selectedArticle) return
|
||||
function handleArticleKeyDown(event: KeyboardEvent) {
|
||||
if (event.key !== "Escape") return
|
||||
if (articleMoreOpen) {
|
||||
event.preventDefault()
|
||||
articleMoreRef.current?.removeAttribute("open")
|
||||
setArticleMoreOpen(false)
|
||||
return
|
||||
}
|
||||
if (articleCloseConfirmOpen) {
|
||||
event.preventDefault()
|
||||
stayInArticle()
|
||||
return
|
||||
}
|
||||
event.preventDefault()
|
||||
requestArticleClose()
|
||||
}
|
||||
window.addEventListener("keydown", handleArticleKeyDown)
|
||||
return () => window.removeEventListener("keydown", handleArticleKeyDown)
|
||||
}, [selectedArticle, articleCloseConfirmOpen, articleMoreOpen, editorDirty])
|
||||
|
||||
function findImageBlockFromElement(target: EventTarget | null) {
|
||||
const editor = editorInstanceRef.current
|
||||
if (!editor || !(target instanceof Element)) return null
|
||||
|
|
@ -2114,6 +2144,31 @@ export default function App() {
|
|||
setSelectedArticle(null)
|
||||
}
|
||||
|
||||
function requestArticleClose() {
|
||||
if (!editorDirty) {
|
||||
setSelectedArticle(null)
|
||||
return
|
||||
}
|
||||
setArticleCloseConfirmOpen(true)
|
||||
}
|
||||
|
||||
function stayInArticle() {
|
||||
setArticleCloseConfirmOpen(false)
|
||||
window.requestAnimationFrame(() => articleBackButtonRef.current?.focus())
|
||||
}
|
||||
|
||||
function discardArticleChanges() {
|
||||
setArticleCloseConfirmOpen(false)
|
||||
setSelectedArticle(null)
|
||||
}
|
||||
|
||||
async function saveAndCloseArticle() {
|
||||
const saved = await saveArticle(false)
|
||||
if (!saved) return
|
||||
setArticleCloseConfirmOpen(false)
|
||||
setSelectedArticle(null)
|
||||
}
|
||||
|
||||
async function copyArticleText() {
|
||||
if (!selectedArticle) return
|
||||
let text = ""
|
||||
|
|
@ -2698,6 +2753,10 @@ export default function App() {
|
|||
<div className="article-modal-header flex min-w-0 items-start justify-between gap-4 border-b p-5">
|
||||
<div className="article-modal-intro min-w-0 flex-1">
|
||||
<div className="flex flex-wrap items-center gap-2">
|
||||
<Button ref={articleBackButtonRef} variant="ghost" size="default" className="article-back-button h-11 shrink-0 px-2" onClick={requestArticleClose}>
|
||||
<ChevronLeft className="size-5" />
|
||||
К черновикам
|
||||
</Button>
|
||||
{isScheduledForFuture(selectedArticle.publishedAt) ? (
|
||||
<Badge className="gap-1.5 border-sky-200 bg-sky-50 text-sky-700 hover:bg-sky-50" variant="outline">
|
||||
<Clock3 className="size-3.5" />
|
||||
|
|
@ -2707,13 +2766,14 @@ export default function App() {
|
|||
<StatusBadge status={selectedArticle.status} />
|
||||
)}
|
||||
<h2 className="min-w-0 flex-1 truncate text-lg font-semibold tracking-normal">Редактирование статьи</h2>
|
||||
{editorDirty ? <Badge variant="secondary">есть правки</Badge> : null}
|
||||
<Badge variant="secondary" className="article-save-state">{editorDirty ? "Есть правки" : "Сохранено"}</Badge>
|
||||
</div>
|
||||
<p className="mt-1 text-sm text-muted-foreground">Редактируйте блоки как в Medium. У изображения доступны случайный кадр и перегенерация по смыслу текста.</p>
|
||||
</div>
|
||||
<div className="article-modal-actions flex shrink-0 flex-wrap justify-end gap-2">
|
||||
<div className="article-modal-actions hidden shrink-0 flex-wrap justify-end gap-2 sm:flex">
|
||||
<input
|
||||
ref={featureImageInputRef}
|
||||
id="article-feature-image-upload"
|
||||
className="hidden"
|
||||
type="file"
|
||||
accept="image/*"
|
||||
|
|
@ -2767,25 +2827,63 @@ export default function App() {
|
|||
<Button size="icon" variant="outline" disabled={editorBusy} title="Удалить" aria-label="Удалить" onClick={() => deleteArticle(selectedArticle.id)}>
|
||||
<Trash2 className="size-4" />
|
||||
</Button>
|
||||
<Button variant="outline" size="icon" title="Закрыть" aria-label="Закрыть" onClick={() => setSelectedArticle(null)}>
|
||||
<X className="size-4" />
|
||||
</div>
|
||||
<details
|
||||
ref={articleMoreRef}
|
||||
className="article-more-actions w-full sm:hidden"
|
||||
onToggle={(event) => setArticleMoreOpen(event.currentTarget.open)}
|
||||
>
|
||||
<summary aria-expanded={articleMoreOpen} className="flex h-11 cursor-pointer list-none items-center justify-between rounded-md border bg-background px-3 text-sm font-medium shadow-sm">
|
||||
Ещё
|
||||
<ChevronDown className="size-4" aria-hidden="true" />
|
||||
</summary>
|
||||
<div className="mt-2 grid gap-2 rounded-md border bg-muted/30 p-2">
|
||||
<Button variant="outline" className="h-11 justify-start" disabled={editorBusy} onClick={copyArticleText}>
|
||||
<Copy data-icon="inline-start" />
|
||||
Скопировать текст
|
||||
</Button>
|
||||
<Button
|
||||
variant="outline"
|
||||
className="h-11 justify-start whitespace-normal text-left"
|
||||
disabled={editorBusy || imageGenerationStarting || Boolean(imageGenerationBatch && !["completed", "completed_with_errors", "failed"].includes(imageGenerationBatch.status))}
|
||||
onClick={() => startImageGeneration("all")}
|
||||
>
|
||||
{imageGenerationStarting ? <Loader2 className="animate-spin" data-icon="inline-start" /> : <ImagePlus data-icon="inline-start" />}
|
||||
Сгенерировать изображения
|
||||
</Button>
|
||||
{selectedArticle.status !== "published" || isScheduledForFuture(selectedArticle.publishedAt) ? (
|
||||
<label className="article-publish-time flex h-11 items-center gap-2 rounded-md border bg-background px-3 text-xs text-muted-foreground" title="Время выхода в RSS">
|
||||
<Clock3 className="size-4" />
|
||||
<input
|
||||
type="datetime-local"
|
||||
className="min-w-0 flex-1 bg-transparent text-sm text-foreground outline-none"
|
||||
value={publishAt}
|
||||
onChange={(event) => setPublishAt(event.target.value)}
|
||||
/>
|
||||
</label>
|
||||
) : (
|
||||
<Button variant="outline" className="h-11 justify-start" onClick={() => window.open(selectedArticle.url, "_blank")}>
|
||||
<ExternalLink data-icon="inline-start" />
|
||||
Открыть на сайте
|
||||
</Button>
|
||||
)}
|
||||
<Button variant="outline" className="h-11 justify-start text-destructive hover:text-destructive" disabled={editorBusy} onClick={() => deleteArticle(selectedArticle.id)}>
|
||||
<Trash2 data-icon="inline-start" />
|
||||
Удалить
|
||||
</Button>
|
||||
</div>
|
||||
</details>
|
||||
</div>
|
||||
<div className="article-modal-body overflow-y-auto bg-zinc-50 p-4 sm:p-6">
|
||||
<article className="article-modal-content mx-auto w-full max-w-[1024px] rounded-lg border bg-white px-5 py-7 shadow-sm sm:px-8 lg:px-10">
|
||||
<div className="mb-6">
|
||||
<div className="mb-2 flex items-center justify-between gap-3">
|
||||
<div>
|
||||
<div className="article-feature-image-heading mb-2 flex items-center justify-between gap-3">
|
||||
<div className="min-w-0">
|
||||
<p className="text-sm font-semibold text-zinc-950">Feature Image</p>
|
||||
<p className="text-xs text-muted-foreground">Главная обложка публикации для RSS и Дзена.</p>
|
||||
</div>
|
||||
<Button
|
||||
size="sm"
|
||||
variant="outline"
|
||||
disabled={featureImageBusy}
|
||||
onClick={() => featureImageInputRef.current?.click()}
|
||||
>
|
||||
<div className="article-feature-image-controls flex flex-wrap gap-2">
|
||||
<Button size="sm" variant="outline" disabled={featureImageBusy} onClick={() => featureImageInputRef.current?.click()}>
|
||||
<ImagePlus data-icon="inline-start" className={featureImageBusy ? "animate-pulse" : ""} />
|
||||
Выбрать
|
||||
</Button>
|
||||
|
|
@ -2799,10 +2897,13 @@ export default function App() {
|
|||
Перегенерировать обложку
|
||||
</Button>
|
||||
</div>
|
||||
<div
|
||||
className={`relative flex aspect-video w-full cursor-pointer items-center justify-center overflow-hidden rounded-md border border-dashed transition ${
|
||||
</div>
|
||||
<button
|
||||
type="button"
|
||||
className={`relative flex aspect-video w-full cursor-pointer items-center justify-center overflow-hidden rounded-md border border-dashed text-left transition focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 ${
|
||||
featureImageDragActive ? "border-primary bg-primary/5" : "border-zinc-300 bg-zinc-50 hover:bg-zinc-100"
|
||||
}`}
|
||||
aria-label="Выбрать обложку статьи"
|
||||
onClick={() => featureImageInputRef.current?.click()}
|
||||
onMouseEnter={() => {
|
||||
clearEditorImageHover()
|
||||
|
|
@ -2856,7 +2957,7 @@ export default function App() {
|
|||
</div>
|
||||
</div>
|
||||
) : null}
|
||||
</div>
|
||||
</button>
|
||||
</div>
|
||||
{imageGenerationBatch ? (
|
||||
<section className="image-generation-status mb-6 rounded-md border bg-zinc-50 p-3" aria-live="polite">
|
||||
|
|
@ -2903,8 +3004,36 @@ export default function App() {
|
|||
/>
|
||||
</article>
|
||||
</div>
|
||||
<div className="article-mobile-actions grid gap-2 border-t bg-background p-3 sm:hidden">
|
||||
<Button className="h-11 w-full" variant="outline" disabled={editorBusy || !editorDirty} onClick={() => saveArticle()}>
|
||||
{editorBusy ? <Loader2 className="animate-spin" data-icon="inline-start" /> : <Save data-icon="inline-start" />}
|
||||
Сохранить
|
||||
</Button>
|
||||
{selectedArticle.status !== "published" || isScheduledForFuture(selectedArticle.publishedAt) ? (
|
||||
<Button className="h-11 w-full" disabled={editorBusy} onClick={saveAndPublishArticle}>
|
||||
{editorBusy ? <Loader2 className="animate-spin" data-icon="inline-start" /> : <Radio data-icon="inline-start" />}
|
||||
{isScheduledForFuture(publishAt) ? "Запланировать" : "Опубликовать"}
|
||||
</Button>
|
||||
) : null}
|
||||
</div>
|
||||
</div>
|
||||
{articleCloseConfirmOpen ? (
|
||||
<div className="absolute inset-0 z-10 flex items-center justify-center bg-background/80 p-4 backdrop-blur-sm" role="presentation">
|
||||
<section className="w-full max-w-sm rounded-lg border bg-background p-5 shadow-lg" role="dialog" aria-modal="true" aria-labelledby="article-close-confirm-title" aria-describedby="article-close-confirm-description">
|
||||
<h3 id="article-close-confirm-title" className="text-lg font-semibold">Сохранить изменения?</h3>
|
||||
<p id="article-close-confirm-description" className="mt-2 text-sm text-muted-foreground">Несохранённые правки останутся только в этом браузере и будут потеряны при выходе.</p>
|
||||
<div className="mt-5 grid gap-2 sm:flex sm:justify-end">
|
||||
<Button variant="outline" className="h-11" onClick={stayInArticle}>Остаться</Button>
|
||||
<Button variant="outline" className="h-11" disabled={editorBusy} onClick={discardArticleChanges}>Не сохранять</Button>
|
||||
<Button className="h-11" disabled={editorBusy} onClick={() => void saveAndCloseArticle()}>
|
||||
{editorBusy ? <Loader2 className="animate-spin" data-icon="inline-start" /> : null}
|
||||
Сохранить и выйти
|
||||
</Button>
|
||||
</div>
|
||||
</section>
|
||||
</div>
|
||||
) : null}
|
||||
</div>
|
||||
) : null}
|
||||
</main>
|
||||
)
|
||||
|
|
|
|||
|
|
@ -19,6 +19,7 @@ const buttonVariants = cva(
|
|||
sm: "h-8 rounded-md px-3 text-xs",
|
||||
lg: "h-10 rounded-md px-8",
|
||||
icon: "size-9",
|
||||
touchIcon: "size-11",
|
||||
},
|
||||
},
|
||||
defaultVariants: {
|
||||
|
|
@ -40,4 +41,3 @@ const Button = React.forwardRef<HTMLButtonElement, ButtonProps>(
|
|||
Button.displayName = "Button"
|
||||
|
||||
export { Button, buttonVariants }
|
||||
|
||||
|
|
|
|||
|
|
@ -466,6 +466,10 @@
|
|||
border-right: 0;
|
||||
}
|
||||
|
||||
.modal-panel:has(.article-modal-body) {
|
||||
min-width: 0;
|
||||
}
|
||||
|
||||
.article-modal-header {
|
||||
flex-direction: column;
|
||||
gap: 0.75rem;
|
||||
|
|
@ -481,6 +485,11 @@
|
|||
line-height: 1.35;
|
||||
}
|
||||
|
||||
.article-save-state {
|
||||
max-width: 100%;
|
||||
overflow-wrap: anywhere;
|
||||
}
|
||||
|
||||
.article-modal-actions {
|
||||
width: 100%;
|
||||
justify-content: flex-start;
|
||||
|
|
@ -497,13 +506,39 @@
|
|||
}
|
||||
|
||||
.article-modal-body {
|
||||
min-height: 0;
|
||||
flex: 1 1 auto;
|
||||
padding: 0.75rem;
|
||||
}
|
||||
|
||||
.article-modal-content {
|
||||
min-width: 0;
|
||||
padding: 1rem;
|
||||
}
|
||||
|
||||
.article-feature-image-heading {
|
||||
align-items: flex-start;
|
||||
flex-direction: column;
|
||||
}
|
||||
|
||||
.article-feature-image-controls {
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
.article-feature-image-controls > button {
|
||||
min-height: 2.75rem;
|
||||
flex: 1 1 100%;
|
||||
white-space: normal;
|
||||
overflow-wrap: anywhere;
|
||||
}
|
||||
|
||||
.article-mobile-actions {
|
||||
position: sticky;
|
||||
bottom: 0;
|
||||
z-index: 2;
|
||||
padding-bottom: max(0.75rem, env(safe-area-inset-bottom));
|
||||
}
|
||||
|
||||
.article-modal-content .article-editorjs .ce-block__content,
|
||||
.article-modal-content .article-editorjs .ce-toolbar__content {
|
||||
max-width: calc(100vw - 4rem);
|
||||
|
|
|
|||
Loading…
Reference in a new issue