import { Accessor, createMemo, createSignal, onCleanup, onMount, Show } from 'solid-js' import { useLocalize } from '../../context/localize' import { clsx } from 'clsx' import { Title } from '@solidjs/meta' import type { Shout, Topic } from '../../graphql/types.gen' import { useRouter } from '../../stores/router' import { ShoutForm, useEditorContext } from '../../context/editor' import { Editor, Panel } from '../Editor' import { Icon } from '../_shared/Icon' import styles from './Edit.module.scss' import { imageProxy } from '../../utils/imageProxy' import { GrowingTextarea } from '../_shared/GrowingTextarea' import { VideoUploader } from '../Editor/VideoUploader' import { AudioUploader } from '../Editor/AudioUploader' import { slugify } from '../../utils/slugify' import { SolidSwiper } from '../_shared/SolidSwiper' import { DropArea } from '../_shared/DropArea' import { LayoutType, MediaItem, UploadedFile } from '../../pages/types' import { clone } from '../../utils/clone' import deepEqual from 'fast-deep-equal' import { AutoSaveNotice } from '../Editor/AutoSaveNotice' import { PublishSettings } from './PublishSettings' import { createStore } from 'solid-js/store' type Props = { shout: Shout } export const EMPTY_TOPIC: Topic = { id: -1, slug: '' } const AUTO_SAVE_INTERVAL = 5000 const handleScrollTopButtonClick = (e) => { e.preventDefault() window.scrollTo({ top: 0, behavior: 'smooth' }) } export const EditView = (props: Props) => { const { t } = useLocalize() const [isScrolled, setIsScrolled] = createSignal(false) const { page } = useRouter() const { form, formErrors, actions: { setForm, setFormErrors, saveDraft, saveDraftToLocalStorage, getDraftFromLocalStorage } } = useEditorContext() const shoutTopics = props.shout.topics || [] const draft = getDraftFromLocalStorage(props.shout.id) if (draft) { setForm(draft) } else { setForm({ slug: props.shout.slug, shoutId: props.shout.id, title: props.shout.title, subtitle: props.shout.subtitle, selectedTopics: shoutTopics, mainTopic: shoutTopics.find((topic) => topic.slug === props.shout.mainTopic) || EMPTY_TOPIC, body: props.shout.body, coverImageUrl: props.shout.cover, media: props.shout.media, layout: props.shout.layout }) } const [prevForm, setPrevForm] = createStore(clone(form)) const [saving, setSaving] = createSignal(false) const mediaItems: Accessor = createMemo(() => { return JSON.parse(form.media || '[]') }) onMount(() => { const handleScroll = () => { setIsScrolled(window.scrollY > 0) } window.addEventListener('scroll', handleScroll, { passive: true }) onCleanup(() => { window.removeEventListener('scroll', handleScroll) }) }) onMount(() => { // eslint-disable-next-line unicorn/consistent-function-scoping const handleBeforeUnload = (event) => { if (!deepEqual(prevForm, form)) { event.returnValue = t( `There are unsaved changes in your publishing settings. Are you sure you want to leave the page without saving?` ) } } window.addEventListener('beforeunload', handleBeforeUnload) onCleanup(() => window.removeEventListener('beforeunload', handleBeforeUnload)) }) const handleTitleInputChange = (value) => { setForm('title', value) setForm('slug', slugify(value)) if (value) { setFormErrors('title', '') } } const handleAddMedia = (data) => { const newMedia = [...mediaItems(), ...data] setForm('media', JSON.stringify(newMedia)) } const handleSortedMedia = (data) => { setForm('media', JSON.stringify(data)) } const handleMediaDelete = (index) => { const copy = [...mediaItems()] copy.splice(index, 1) setForm('media', JSON.stringify(copy)) } const handleMediaChange = (index, value) => { const updated = mediaItems().map((item, idx) => (idx === index ? value : item)) setForm('media', JSON.stringify(updated)) } const [baseAudioFields, setBaseAudioFields] = createSignal({ artist: '', date: '', genre: '' }) const handleBaseFieldsChange = (key, value) => { if (mediaItems().length > 0) { const updated = mediaItems().map((media) => ({ ...media, [key]: value })) setForm('media', JSON.stringify(updated)) } else { setBaseAudioFields({ ...baseAudioFields(), [key]: value }) } } const articleTitle = () => { switch (props.shout.layout as LayoutType) { case 'audio': { return t('Album name') } case 'image': { return t('Gallery name') } default: { return t('Header') } } } const pageTitle = () => { switch (props.shout.layout as LayoutType) { case 'audio': { return t('Publish Album') } case 'image': { return t('Create gallery') } case 'video': { return t('Create video') } case 'literature': { return t('New literary work') } default: { return t('Write an article') } } } let autoSaveTimeOutId const autoSaveRecursive = () => { autoSaveTimeOutId = setTimeout(async () => { const hasChanges = !deepEqual(form, prevForm) if (hasChanges) { setSaving(true) if (props.shout.visibility === 'owner') { await saveDraft(form) } else { saveDraftToLocalStorage(form) } setPrevForm(clone(form)) setTimeout(() => { setSaving(false) }, 2000) } autoSaveRecursive() }, AUTO_SAVE_INTERVAL) } const stopAutoSave = () => { clearTimeout(autoSaveTimeOutId) } onMount(() => { autoSaveRecursive() }) onCleanup(() => { stopAutoSave() }) return ( <>
{pageTitle()}
<>
handleTitleInputChange(value)} class={styles.titleInput} placeholder={articleTitle()} initialValue={form.title} maxLength={100} />
{formErrors.title}
handleBaseFieldsChange('artist', event.target.value)} /> handleBaseFieldsChange('date', event.target.value)} /> handleBaseFieldsChange('genre', event.target.value)} />
setForm('subtitle', value)} class={styles.subtitleInput} placeholder={t('Subheader')} initialValue={form.subtitle} maxLength={100} />
{t('min. 1400×1400 pix')}
{t('jpg, .png, max. 10 mb.')} } isMultiply={false} fileType={'image'} onUpload={(val) => setForm('coverImageUrl', val[0].url)} /> } >
handleMediaDelete(index)} onImagesAdd={(value) => handleAddMedia(value)} onImagesSorted={(value) => handleSortedMedia(value)} /> handleAddMedia(data)} onVideoDelete={(index) => handleMediaDelete(index)} /> handleAddMedia(value)} onAudioChange={handleMediaChange} onAudioSorted={(value) => handleSortedMedia(value)} />
setForm('body', body)} />
) } export default EditView