Passa al contenuto principale

How to make 1 million € in 10 daysSkip to content (Si apre in una nuova finestra)

   (Si apre in una nuova finestra)

   (Si apre in una nuova finestra)

Explore (Si apre in una nuova finestra)      Pull requests (Si apre in una nuova finestra) Issues (Si apre in una nuova finestra)Marketplace (Si apre in una nuova finestra)

     (Si apre in una nuova finestra)

   (Si apre in una nuova finestra)

steady-media (Si apre in una nuova finestra)  /  main_app (Si apre in una nuova finestra)  Private

More

main_app (Si apre in una nuova finestra)/assets (Si apre in una nuova finestra)/js (Si apre in una nuova finestra)/components (Si apre in una nuova finestra)/multimedia_text_editor (Si apre in una nuova finestra)/editor_form.jsx /

jiegillet (Si apre in una nuova finestra)  [STE-1709] Remove lodash dependency ( (Si apre in una nuova finestra)#6023 (Si apre in una nuova finestra)) (Si apre in una nuova finestra)

Latest commit 00a3890 (Si apre in una nuova finestra) on 30 Aug     (Si apre in una nuova finestra)

   History   (Si apre in una nuova finestra)

6                 contributors            (Si apre in una nuova finestra)    (Si apre in una nuova finestra)    (Si apre in una nuova finestra)    (Si apre in una nuova finestra)    (Si apre in una nuova finestra)    (Si apre in una nuova finestra)

140 lines (119 sloc)           4.77 KB

import React, { useRef, useEffect, useState, useCallback, useMemo } from 'react'   import { convertToRaw } from 'draft-js'   import { convertToHtml } from './html_export.jsx'   import { isPostNew, stripUploadingEntities } from './helpers'   import { debounce } from '../../util/function'   import { paywallSettingKeysToCamelCase } from './api'   import { PostContext } from './post_context.jsx'   import Editor from './editor.jsx'   import { AUTOSAVE_STATES } from './constants'   import AutoSaveStatus from './autosave_status.jsx'   import { CaretRight } from './icons.jsx'   const DEFAULT_AUTOSAVE_TIMEOUT = 2000       export default function EditorForm ({     autosave,     autosaveTimeout,     revert,     rawOutputSelector,     htmlOutputSelector,     referenceHtmlOutput,     submitButtonText   }) {    const postContext = React.useContext(PostContext)    const [saveState, setSaveState] = useState(AUTOSAVE_STATES.NO_CHANGES)    const rawOutputField = useRef(document.querySelector(rawOutputSelector))    const htmlOutputField = useRef(document.querySelector(htmlOutputSelector))        const getShowRevert = () => {    const paywallSettingsKeyPairs = [    ['image', 'draftImage'],    ['pitch', 'draftPitch'],    ['ctaText', 'draftCtaText']    ]        return htmlOutputField.current.value !== referenceHtmlOutput ||    paywallSettingsKeyPairs.some(keyPair =>    postContext.paywallSettings[keyPair[0]] !==    postContext.paywallSettings[keyPair[1]]    )    }    const [showRevert, setShowRevert] = useState(getShowRevert())        const initialValue = rawOutputField.current.value        // We keep the editorState also in a ref to avoid having to re-attach the event handler    // on every render, which would be needed to keep editorState up-to-date    // https://reactjs.org/docs/hooks-faq.html#what-can-i-do-if-my-effect-dependencies-change-too-often (Si apre in una nuova finestra)    const latestEditorState = useRef(null)        const persistEditorStateToForm = useCallback(() => {    pushEditURLToHistory()    const contentState = stripUploadingEntities(latestEditorState.current).getCurrentContent()        rawOutputField.current.value = JSON.stringify(convertToRaw(contentState))    htmlOutputField.current.value = convertToHtml(contentState)    setShowRevert(getShowRevert())    }, []) // eslint-disable-line react-hooks/exhaustive-deps        const handleSubmit = persistEditorStateToForm        const handleChange = (editorState, { force } = {}) => {    const stateHasChanged =    latestEditorState.current && (latestEditorState.current.getCurrentContent() !== editorState.getCurrentContent())    latestEditorState.current = editorState        if ((force || stateHasChanged) && editorState.getCurrentContent().hasText()) {    setSaveState(AUTOSAVE_STATES.UNSAVED)    debouncedAutosave()    }    }        const pushEditURLToHistory = () => {    if (isPostNew()) {    const newLocation = window.location.href.replace(/\/posts\/new$/, `/posts/${postContext.id}/edit`)    up.history.replace(newLocation)    }    }        const immediateAutosave = async () => {    persistEditorStateToForm()    setSaveState(AUTOSAVE_STATES.SAVING)    try {    const payload = await autosave()        if (payload.paywall_settings) {    postContext.setPaywallSettings(paywallSettingKeysToCamelCase(payload.paywall_settings))    }        pushEditURLToHistory()        setSaveState(AUTOSAVE_STATES.SAVED)    } catch {    setSaveState(AUTOSAVE_STATES.ERROR)    }    }        const debouncedAutosave = useMemo(() =>    debounce(immediateAutosave, autosaveTimeout || DEFAULT_AUTOSAVE_TIMEOUT),    [autosaveTimeout, autosave, persistEditorStateToForm, postContext] // eslint-disable-line react-hooks/exhaustive-deps    )        useEffect(() => {    htmlOutputField.current.form.addEventListener('submit', handleSubmit)    const form = htmlOutputField.current.form    return () => {    form.removeEventListener('submit', handleSubmit)    }    }, [handleSubmit])        useEffect(() => {    // update revert button visibility when paywall settings change    setShowRevert(getShowRevert())    }, [postContext, getShowRevert])        const renderedAutoSaveStatus =    <AutoSaveStatus saveState={saveState}>    <button type="submit" className="editor_controls__submit_button">    {submitButtonText} <CaretRight />    </button>    </AutoSaveStatus>        const onRevert = async () => {    const { body, rawBody, paywallSettings } = await revert(postContext.id)    rawOutputField.current.value = rawBody    htmlOutputField.current.value = body    setShowRevert(false)    postContext.setPaywallSettings(paywallSettingKeysToCamelCase(paywallSettings))    return { body, rawBody }    }        return <Editor    onRevert={onRevert}    showRevert={showRevert}    initialValue={initialValue}    onChange={handleChange}    immediateAutosave={immediateAutosave}    autoSaveStatus={renderedAutoSaveStatus}    submitButtonText={submitButtonText}    />

}

Footer

   (Si apre in una nuova finestra)

   (Si apre in una nuova finestra)         © 2022 GitHub, Inc.

Footer navigation

I don't know. Do you know? Please tell me.

0 commenti

Vuoi essere la prima persona a commentare?
Abbonati a Angelika's Cow World 🐄 e avvia una conversazione.
Sostieni