Add auto save in SimplifiedEditor (#164)

* Add auto save in SimplifiedEditor

* remove unused imports
This commit is contained in:
Ilya Y 2023-08-14 00:26:40 +03:00 committed by GitHub
parent b5708d26cd
commit e51cff6fb8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 28 additions and 19 deletions

View File

@ -160,10 +160,9 @@ export const PlayerPlaylist = (props: Props) => {
<div class={styles.descriptionBlock}> <div class={styles.descriptionBlock}>
<SimplifiedEditor <SimplifiedEditor
initialContent={mi.body} initialContent={mi.body}
onSubmit={(value) => handleMediaItemFieldChange('body', value)}
placeholder={t('Description')} placeholder={t('Description')}
smallHeight={true} smallHeight={true}
submitButtonText={t('Save')} onAutoSave={(value) => handleMediaItemFieldChange('body', value)}
/> />
<GrowingTextarea <GrowingTextarea
allowEnterKey={true} allowEnterKey={true}

View File

@ -30,7 +30,8 @@ import { Link } from '@tiptap/extension-link'
type Props = { type Props = {
initialContent?: string initialContent?: string
onSubmit: (text: string) => void onSubmit?: (text: string) => void
onAutoSave?: (text: string) => void
placeholder: string placeholder: string
submitButtonText?: string submitButtonText?: string
quoteEnabled?: boolean quoteEnabled?: boolean
@ -117,6 +118,7 @@ const SimplifiedEditor = (props: Props) => {
const handleClear = () => { const handleClear = () => {
editor().commands.clearContent(true) editor().commands.clearContent(true)
} }
createEffect(() => { createEffect(() => {
if (props.setClear) { if (props.setClear) {
editor().commands.clearContent(true) editor().commands.clearContent(true)
@ -124,7 +126,7 @@ const SimplifiedEditor = (props: Props) => {
}) })
const handleKeyDown = async (event) => { const handleKeyDown = async (event) => {
if (isEmpty() || !editor().isFocused) { if (isEmpty() || !isFocused()) {
return return
} }
@ -151,7 +153,14 @@ const SimplifiedEditor = (props: Props) => {
window.removeEventListener('keydown', handleKeyDown) window.removeEventListener('keydown', handleKeyDown)
}) })
if (props.onAutoSave) {
createEffect(() => {
if (isFocused()) return
props.onAutoSave(html())
})
}
const handleInsertLink = () => !editor().state.selection.empty && showModal('editorInsertLink') const handleInsertLink = () => !editor().state.selection.empty && showModal('editorInsertLink')
return ( return (
<div <div
class={clsx(styles.SimplifiedEditor, { class={clsx(styles.SimplifiedEditor, {
@ -227,15 +236,17 @@ const SimplifiedEditor = (props: Props) => {
</Popover> </Popover>
</Show> </Show>
</div> </div>
<div class={styles.buttons}> <Show when={!props.onAutoSave}>
<Button value={t('Cancel')} variant="secondary" disabled={isEmpty()} onClick={handleClear} /> <div class={styles.buttons}>
<Button <Button value={t('Cancel')} variant="secondary" disabled={isEmpty()} onClick={handleClear} />
value={props.submitButtonText ?? t('Send')} <Button
variant="primary" value={props.submitButtonText ?? t('Send')}
disabled={isEmpty()} variant="primary"
onClick={() => props.onSubmit(html())} disabled={isEmpty()}
/> onClick={() => props.onSubmit(html())}
</div> />
</div>
</Show>
</div> </div>
<Modal variant="narrow" name="editorInsertLink"> <Modal variant="narrow" name="editorInsertLink">
<InsertLinkForm editor={editor()} onClose={() => hideModal()} /> <InsertLinkForm editor={editor()} onClose={() => hideModal()} />

View File

@ -27,7 +27,8 @@ type Props = {
const MAX_LEAD_LIMIT = 400 const MAX_LEAD_LIMIT = 400
const shorten = (str: string, maxLen: number) => { const shorten = (str: string, maxLen: number) => {
if (str.length <= maxLen) return str if (str.length <= maxLen) return str
return str.slice(0, Math.max(0, str.lastIndexOf(' ', maxLen))).trim() const result = str.slice(0, Math.max(0, str.lastIndexOf(' ', maxLen))).trim()
return `${result}...`
} }
export const PublishSettings = (props: Props) => { export const PublishSettings = (props: Props) => {
@ -175,7 +176,7 @@ export const PublishSettings = (props: Props) => {
class={styles.settingInput} class={styles.settingInput}
variant="bordered" variant="bordered"
placeholder={t('Write a short introduction')} placeholder={t('Write a short introduction')}
initialValue={`${settingsForm.lead}${settingsForm.lead.length > MAX_LEAD_LIMIT - 1 && '...'}`} initialValue={`${settingsForm.lead}`}
value={(value) => setSettingsForm('lead', value)} value={(value) => setSettingsForm('lead', value)}
allowEnterKey={false} allowEnterKey={false}
maxLength={MAX_LEAD_LIMIT} maxLength={MAX_LEAD_LIMIT}

View File

@ -324,8 +324,7 @@ export const SolidSwiper = (props: Props) => {
initialContent={props.images[slideIndex()].body} initialContent={props.images[slideIndex()].body}
smallHeight={true} smallHeight={true}
placeholder={t('Enter image description')} placeholder={t('Enter image description')}
onSubmit={(value) => handleSlideDescriptionChange(slideIndex(), 'body', value)} onAutoSave={(value) => handleSlideDescriptionChange(slideIndex(), 'body', value)}
submitButtonText={t('Save')}
/> />
</div> </div>
</Show> </Show>

View File

@ -4,7 +4,6 @@ import ProfileSettingsNavigation from '../../components/Discours/ProfileSettings
import { For, createSignal, Show, onMount, onCleanup } from 'solid-js' import { For, createSignal, Show, onMount, onCleanup } from 'solid-js'
import deepEqual from 'fast-deep-equal' import deepEqual from 'fast-deep-equal'
import { clsx } from 'clsx' import { clsx } from 'clsx'
import styles from './Settings.module.scss' import styles from './Settings.module.scss'
import { useProfileForm } from '../../context/profile' import { useProfileForm } from '../../context/profile'
import { validateUrl } from '../../utils/validateUrl' import { validateUrl } from '../../utils/validateUrl'
@ -28,10 +27,10 @@ export const ProfileSettingsPage = () => {
const { const {
actions: { showSnackbar } actions: { showSnackbar }
} = useSnackbar() } = useSnackbar()
const { const {
actions: { loadSession } actions: { loadSession }
} = useSession() } = useSession()
const { form, updateFormField, submit, slugError } = useProfileForm() const { form, updateFormField, submit, slugError } = useProfileForm()
const [prevForm, setPrevForm] = createStore(clone(form)) const [prevForm, setPrevForm] = createStore(clone(form))