2023-07-19 11:00:58 +00:00
|
|
|
|
import { Accessor, createMemo, createSignal, onCleanup, onMount, Show } from 'solid-js'
|
2023-02-17 09:21:02 +00:00
|
|
|
|
import { useLocalize } from '../../context/localize'
|
2023-03-12 20:16:20 +00:00
|
|
|
|
import { clsx } from 'clsx'
|
2023-03-13 12:26:25 +00:00
|
|
|
|
import { Title } from '@solidjs/meta'
|
2023-04-11 13:57:48 +00:00
|
|
|
|
import type { Shout, Topic } from '../../graphql/types.gen'
|
2023-05-07 15:15:30 +00:00
|
|
|
|
import { useRouter } from '../../stores/router'
|
2023-08-04 15:59:36 +00:00
|
|
|
|
import { ShoutForm, useEditorContext } from '../../context/editor'
|
2023-08-13 15:51:02 +00:00
|
|
|
|
import { Editor, Panel } from '../Editor'
|
2023-05-06 15:04:50 +00:00
|
|
|
|
import { Icon } from '../_shared/Icon'
|
2023-05-08 18:23:51 +00:00
|
|
|
|
import styles from './Edit.module.scss'
|
2023-05-10 01:46:39 +00:00
|
|
|
|
import { imageProxy } from '../../utils/imageProxy'
|
2023-05-11 11:33:01 +00:00
|
|
|
|
import { GrowingTextarea } from '../_shared/GrowingTextarea'
|
2023-06-10 14:10:05 +00:00
|
|
|
|
import { VideoUploader } from '../Editor/VideoUploader'
|
2023-07-14 13:06:21 +00:00
|
|
|
|
import { AudioUploader } from '../Editor/AudioUploader'
|
2023-06-10 14:10:05 +00:00
|
|
|
|
import { slugify } from '../../utils/slugify'
|
2023-07-02 05:08:42 +00:00
|
|
|
|
import { SolidSwiper } from '../_shared/SolidSwiper'
|
2023-07-14 13:06:21 +00:00
|
|
|
|
import { DropArea } from '../_shared/DropArea'
|
|
|
|
|
import { LayoutType, MediaItem } from '../../pages/types'
|
2023-08-04 15:59:36 +00:00
|
|
|
|
import { clone } from '../../utils/clone'
|
|
|
|
|
import deepEqual from 'fast-deep-equal'
|
|
|
|
|
import { AutoSaveNotice } from '../Editor/AutoSaveNotice'
|
2023-08-12 07:48:43 +00:00
|
|
|
|
import { PublishSettings } from './PublishSettings'
|
2023-08-13 15:51:02 +00:00
|
|
|
|
import { createStore } from 'solid-js/store'
|
2023-03-23 17:15:50 +00:00
|
|
|
|
|
2023-06-10 14:10:05 +00:00
|
|
|
|
type Props = {
|
2023-04-11 13:57:48 +00:00
|
|
|
|
shout: Shout
|
|
|
|
|
}
|
2023-08-12 07:48:43 +00:00
|
|
|
|
|
|
|
|
|
export const EMPTY_TOPIC: Topic = {
|
|
|
|
|
id: -1,
|
|
|
|
|
slug: ''
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-04 15:59:36 +00:00
|
|
|
|
const AUTO_SAVE_INTERVAL = 5000
|
2023-05-12 13:03:46 +00:00
|
|
|
|
const handleScrollTopButtonClick = (e) => {
|
|
|
|
|
e.preventDefault()
|
2023-05-09 05:05:06 +00:00
|
|
|
|
window.scrollTo({
|
|
|
|
|
top: 0,
|
|
|
|
|
behavior: 'smooth'
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-10 14:10:05 +00:00
|
|
|
|
export const EditView = (props: Props) => {
|
2023-02-17 09:21:02 +00:00
|
|
|
|
const { t } = useLocalize()
|
2023-05-07 19:33:20 +00:00
|
|
|
|
const [isScrolled, setIsScrolled] = createSignal(false)
|
2023-07-02 05:08:42 +00:00
|
|
|
|
|
2023-03-26 18:31:34 +00:00
|
|
|
|
const { page } = useRouter()
|
2023-08-04 15:59:36 +00:00
|
|
|
|
|
2023-05-04 19:57:02 +00:00
|
|
|
|
const {
|
|
|
|
|
form,
|
2023-05-05 20:05:50 +00:00
|
|
|
|
formErrors,
|
2023-08-04 15:59:36 +00:00
|
|
|
|
actions: { setForm, setFormErrors, saveDraft, saveDraftToLocalStorage, getDraftFromLocalStorage }
|
2023-05-04 19:57:02 +00:00
|
|
|
|
} = useEditorContext()
|
2023-03-23 17:15:50 +00:00
|
|
|
|
|
2023-05-10 20:20:53 +00:00
|
|
|
|
const shoutTopics = props.shout.topics || []
|
|
|
|
|
|
2023-08-04 15:59:36 +00:00
|
|
|
|
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
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-13 15:51:02 +00:00
|
|
|
|
const [prevForm, setPrevForm] = createStore<ShoutForm>(clone(form))
|
2023-08-04 15:59:36 +00:00
|
|
|
|
const [saving, setSaving] = createSignal(false)
|
2023-03-23 17:15:50 +00:00
|
|
|
|
|
2023-07-14 13:06:21 +00:00
|
|
|
|
const mediaItems: Accessor<MediaItem[]> = createMemo(() => {
|
2023-07-02 05:08:42 +00:00
|
|
|
|
return JSON.parse(form.media || '[]')
|
|
|
|
|
})
|
|
|
|
|
|
2023-05-07 19:33:20 +00:00
|
|
|
|
onMount(() => {
|
|
|
|
|
const handleScroll = () => {
|
|
|
|
|
setIsScrolled(window.scrollY > 0)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
window.addEventListener('scroll', handleScroll, { passive: true })
|
|
|
|
|
onCleanup(() => {
|
|
|
|
|
window.removeEventListener('scroll', handleScroll)
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
2023-08-13 15:51:02 +00:00
|
|
|
|
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))
|
|
|
|
|
})
|
|
|
|
|
|
2023-05-11 11:33:01 +00:00
|
|
|
|
const handleTitleInputChange = (value) => {
|
|
|
|
|
setForm('title', value)
|
2023-06-10 14:10:05 +00:00
|
|
|
|
setForm('slug', slugify(value))
|
2023-05-11 11:33:01 +00:00
|
|
|
|
if (value) {
|
2023-05-05 20:05:50 +00:00
|
|
|
|
setFormErrors('title', '')
|
2023-03-26 18:31:34 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-14 13:06:21 +00:00
|
|
|
|
const handleAddMedia = (data) => {
|
|
|
|
|
const newMedia = [...mediaItems(), ...data]
|
|
|
|
|
setForm('media', JSON.stringify(newMedia))
|
2023-07-02 05:08:42 +00:00
|
|
|
|
}
|
2023-07-18 19:11:00 +00:00
|
|
|
|
const handleSortedMedia = (data) => {
|
2023-07-02 05:08:42 +00:00
|
|
|
|
setForm('media', JSON.stringify(data))
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-19 11:00:58 +00:00
|
|
|
|
const handleMediaDelete = (index) => {
|
2023-07-02 05:08:42 +00:00
|
|
|
|
const copy = [...mediaItems()]
|
|
|
|
|
copy.splice(index, 1)
|
|
|
|
|
setForm('media', JSON.stringify(copy))
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-14 13:06:21 +00:00
|
|
|
|
const handleMediaChange = (index, value) => {
|
2023-07-02 05:08:42 +00:00
|
|
|
|
const updated = mediaItems().map((item, idx) => (idx === index ? value : item))
|
|
|
|
|
setForm('media', JSON.stringify(updated))
|
2023-06-10 14:10:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-07-15 21:51:38 +00:00
|
|
|
|
const [baseAudioFields, setBaseAudioFields] = createSignal({
|
|
|
|
|
artist: '',
|
|
|
|
|
date: '',
|
|
|
|
|
genre: ''
|
|
|
|
|
})
|
|
|
|
|
|
2023-07-14 13:06:21 +00:00
|
|
|
|
const handleBaseFieldsChange = (key, value) => {
|
2023-07-15 21:51:38 +00:00
|
|
|
|
if (mediaItems().length > 0) {
|
|
|
|
|
const updated = mediaItems().map((media) => ({ ...media, [key]: value }))
|
|
|
|
|
setForm('media', JSON.stringify(updated))
|
|
|
|
|
} else {
|
|
|
|
|
setBaseAudioFields({ ...baseAudioFields(), [key]: value })
|
|
|
|
|
}
|
2023-07-14 13:06:21 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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')
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-04 15:59:36 +00:00
|
|
|
|
let autoSaveTimeOutId
|
|
|
|
|
|
|
|
|
|
const autoSaveRecursive = () => {
|
|
|
|
|
autoSaveTimeOutId = setTimeout(async () => {
|
2023-08-13 15:51:02 +00:00
|
|
|
|
const hasChanges = !deepEqual(form, prevForm)
|
2023-08-04 15:59:36 +00:00
|
|
|
|
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()
|
|
|
|
|
})
|
|
|
|
|
|
2022-09-09 11:53:35 +00:00
|
|
|
|
return (
|
2023-04-06 21:40:34 +00:00
|
|
|
|
<>
|
|
|
|
|
<div class={styles.container}>
|
2023-07-14 13:06:21 +00:00
|
|
|
|
<Title>{pageTitle()}</Title>
|
2023-05-05 20:05:50 +00:00
|
|
|
|
<form>
|
2023-04-06 21:40:34 +00:00
|
|
|
|
<div class="wide-container">
|
2023-08-04 15:59:36 +00:00
|
|
|
|
<AutoSaveNotice active={saving()} />
|
2023-05-11 11:52:56 +00:00
|
|
|
|
<button
|
|
|
|
|
class={clsx(styles.scrollTopButton, {
|
|
|
|
|
[styles.visible]: isScrolled()
|
|
|
|
|
})}
|
2023-05-12 13:03:46 +00:00
|
|
|
|
onClick={handleScrollTopButtonClick}
|
2023-05-11 11:52:56 +00:00
|
|
|
|
>
|
|
|
|
|
<Icon name="up-button" class={styles.icon} />
|
|
|
|
|
<span class={styles.scrollTopButtonLabel}>{t('Scroll up')}</span>
|
|
|
|
|
</button>
|
|
|
|
|
|
2023-03-29 08:51:27 +00:00
|
|
|
|
<div class="row">
|
2023-04-06 21:40:34 +00:00
|
|
|
|
<div class="col-md-19 col-lg-18 col-xl-16 offset-md-5">
|
2023-08-12 07:48:43 +00:00
|
|
|
|
<Show when={page().route === 'edit'}>
|
|
|
|
|
<>
|
|
|
|
|
<div class={clsx({ [styles.audioHeader]: props.shout.layout === 'audio' })}>
|
|
|
|
|
<div class={styles.inputContainer}>
|
2023-07-14 13:06:21 +00:00
|
|
|
|
<GrowingTextarea
|
2023-08-12 07:48:43 +00:00
|
|
|
|
allowEnterKey={true}
|
|
|
|
|
value={(value) => handleTitleInputChange(value)}
|
|
|
|
|
class={styles.titleInput}
|
|
|
|
|
placeholder={articleTitle()}
|
|
|
|
|
initialValue={form.title}
|
2023-07-14 13:06:21 +00:00
|
|
|
|
maxLength={100}
|
|
|
|
|
/>
|
2023-08-12 07:48:43 +00:00
|
|
|
|
|
|
|
|
|
<Show when={formErrors.title}>
|
|
|
|
|
<div class={styles.validationError}>{formErrors.title}</div>
|
|
|
|
|
</Show>
|
|
|
|
|
|
|
|
|
|
<Show when={props.shout.layout === 'audio'}>
|
|
|
|
|
<div class={styles.additional}>
|
|
|
|
|
<input
|
|
|
|
|
type="text"
|
|
|
|
|
placeholder={t('Artist...')}
|
|
|
|
|
class={styles.additionalInput}
|
|
|
|
|
value={mediaItems()[0]?.artist || ''}
|
|
|
|
|
onChange={(event) => handleBaseFieldsChange('artist', event.target.value)}
|
|
|
|
|
/>
|
|
|
|
|
<input
|
|
|
|
|
type="number"
|
|
|
|
|
min="1900"
|
|
|
|
|
max={new Date().getFullYear()}
|
|
|
|
|
step="1"
|
|
|
|
|
class={styles.additionalInput}
|
|
|
|
|
placeholder={t('Release date...')}
|
|
|
|
|
value={mediaItems()[0]?.date || ''}
|
|
|
|
|
onChange={(event) => handleBaseFieldsChange('date', event.target.value)}
|
|
|
|
|
/>
|
|
|
|
|
<input
|
|
|
|
|
type="text"
|
|
|
|
|
placeholder={t('Genre...')}
|
|
|
|
|
class={styles.additionalInput}
|
|
|
|
|
value={mediaItems()[0]?.genre || ''}
|
|
|
|
|
onChange={(event) => handleBaseFieldsChange('genre', event.target.value)}
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
</Show>
|
|
|
|
|
|
|
|
|
|
<Show when={props.shout.layout !== 'audio'}>
|
|
|
|
|
<GrowingTextarea
|
|
|
|
|
allowEnterKey={false}
|
|
|
|
|
value={(value) => setForm('subtitle', value)}
|
|
|
|
|
class={styles.subtitleInput}
|
|
|
|
|
placeholder={t('Subheader')}
|
|
|
|
|
initialValue={form.subtitle}
|
|
|
|
|
maxLength={100}
|
2023-07-15 21:51:38 +00:00
|
|
|
|
/>
|
2023-08-12 07:48:43 +00:00
|
|
|
|
</Show>
|
|
|
|
|
</div>
|
|
|
|
|
<Show when={props.shout.layout === 'audio'}>
|
|
|
|
|
<Show
|
|
|
|
|
when={form.coverImageUrl}
|
|
|
|
|
fallback={
|
|
|
|
|
<DropArea
|
|
|
|
|
isSquare={true}
|
|
|
|
|
placeholder={t('Add cover')}
|
|
|
|
|
description={
|
|
|
|
|
<>
|
|
|
|
|
{t('min. 1400×1400 pix')}
|
|
|
|
|
<br />
|
|
|
|
|
{t('jpg, .png, max. 10 mb.')}
|
|
|
|
|
</>
|
|
|
|
|
}
|
|
|
|
|
isMultiply={false}
|
|
|
|
|
fileType={'image'}
|
|
|
|
|
onUpload={(val) => setForm('coverImageUrl', val[0].url)}
|
|
|
|
|
/>
|
|
|
|
|
}
|
|
|
|
|
>
|
|
|
|
|
<div
|
|
|
|
|
class={styles.cover}
|
|
|
|
|
style={{ 'background-image': `url(${imageProxy(form.coverImageUrl)})` }}
|
|
|
|
|
/>
|
|
|
|
|
</Show>
|
2023-05-11 11:06:29 +00:00
|
|
|
|
</Show>
|
|
|
|
|
</div>
|
2023-08-12 07:48:43 +00:00
|
|
|
|
|
|
|
|
|
<Show when={props.shout.layout === 'image'}>
|
|
|
|
|
<SolidSwiper
|
|
|
|
|
editorMode={true}
|
|
|
|
|
images={mediaItems()}
|
|
|
|
|
onImageChange={handleMediaChange}
|
|
|
|
|
onImageDelete={(index) => handleMediaDelete(index)}
|
|
|
|
|
onImagesAdd={(value) => handleAddMedia(value)}
|
|
|
|
|
onImagesSorted={(value) => handleSortedMedia(value)}
|
|
|
|
|
/>
|
2023-03-29 08:51:27 +00:00
|
|
|
|
</Show>
|
2023-08-12 07:48:43 +00:00
|
|
|
|
|
|
|
|
|
<Show when={props.shout.layout === 'video'}>
|
|
|
|
|
<VideoUploader
|
|
|
|
|
video={mediaItems()}
|
|
|
|
|
onVideoAdd={(data) => handleAddMedia(data)}
|
|
|
|
|
onVideoDelete={(index) => handleMediaDelete(index)}
|
2023-05-10 14:07:41 +00:00
|
|
|
|
/>
|
2023-05-09 04:58:00 +00:00
|
|
|
|
</Show>
|
2023-08-12 07:48:43 +00:00
|
|
|
|
|
|
|
|
|
<Show when={props.shout.layout === 'audio'}>
|
|
|
|
|
<AudioUploader
|
|
|
|
|
audio={mediaItems()}
|
|
|
|
|
baseFields={baseAudioFields()}
|
|
|
|
|
onAudioAdd={(value) => handleAddMedia(value)}
|
|
|
|
|
onAudioChange={handleMediaChange}
|
|
|
|
|
onAudioSorted={(value) => handleSortedMedia(value)}
|
|
|
|
|
/>
|
|
|
|
|
</Show>
|
|
|
|
|
</>
|
|
|
|
|
</Show>
|
|
|
|
|
<Show when={page().route === 'editSettings'}>
|
|
|
|
|
<PublishSettings shoutId={props.shout.id} form={form} />
|
|
|
|
|
</Show>
|
2023-03-12 20:16:20 +00:00
|
|
|
|
</div>
|
|
|
|
|
</div>
|
2023-08-12 07:48:43 +00:00
|
|
|
|
<Show when={page().route === 'edit'}>
|
|
|
|
|
<Editor
|
|
|
|
|
shoutId={form.shoutId}
|
|
|
|
|
initialContent={form.body}
|
|
|
|
|
onChange={(body) => setForm('body', body)}
|
|
|
|
|
/>
|
|
|
|
|
</Show>
|
2023-03-12 20:16:20 +00:00
|
|
|
|
</div>
|
2023-04-06 21:40:34 +00:00
|
|
|
|
</form>
|
|
|
|
|
</div>
|
2023-08-12 07:48:43 +00:00
|
|
|
|
|
2023-05-08 17:21:06 +00:00
|
|
|
|
<Panel shoutId={props.shout.id} />
|
2023-04-06 21:40:34 +00:00
|
|
|
|
</>
|
2022-09-09 11:53:35 +00:00
|
|
|
|
)
|
|
|
|
|
}
|
2022-11-01 19:27:43 +00:00
|
|
|
|
|
2023-04-11 13:57:48 +00:00
|
|
|
|
export default EditView
|