2020-07-28 01:12:59 +02:00
|
|
|
// @flow
|
|
|
|
import React, { useEffect } from 'react';
|
|
|
|
import { SIMPLE_SITE } from 'config';
|
|
|
|
import { FF_MAX_CHARS_IN_DESCRIPTION } from 'constants/form-field';
|
|
|
|
import { FormField } from 'component/common/form';
|
|
|
|
import usePersistedState from 'effects/use-persisted-state';
|
|
|
|
|
|
|
|
type Props = {
|
|
|
|
uri: ?string,
|
|
|
|
label: ?string,
|
|
|
|
disabled: ?boolean,
|
|
|
|
fileInfo: FileListItem,
|
|
|
|
filePath: string | WebFile,
|
|
|
|
fileText: ?string,
|
2020-07-28 04:19:00 +02:00
|
|
|
streamingUrl: ?string,
|
2020-07-28 01:12:59 +02:00
|
|
|
isStillEditing: boolean,
|
|
|
|
setPrevFileText: string => void,
|
|
|
|
updatePublishForm: ({}) => void,
|
2020-07-28 04:19:00 +02:00
|
|
|
setCurrentFileType: string => void,
|
2020-07-28 01:12:59 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
function StoryEditor(props: Props) {
|
|
|
|
const {
|
|
|
|
uri,
|
|
|
|
label,
|
|
|
|
disabled,
|
|
|
|
fileInfo,
|
|
|
|
filePath,
|
|
|
|
fileText,
|
2020-07-28 04:19:00 +02:00
|
|
|
streamingUrl,
|
2020-07-28 01:12:59 +02:00
|
|
|
isStillEditing,
|
|
|
|
setPrevFileText,
|
|
|
|
updatePublishForm,
|
2020-07-28 04:19:00 +02:00
|
|
|
setCurrentFileType,
|
2020-07-28 01:12:59 +02:00
|
|
|
} = props;
|
|
|
|
|
2020-07-28 04:19:00 +02:00
|
|
|
const [isLoadign, setIsLoadign] = React.useState(false);
|
2020-07-28 01:12:59 +02:00
|
|
|
const [advancedEditor, setAdvancedEditor] = usePersistedState('publish-form-story-mode', false);
|
|
|
|
|
|
|
|
function toggleMarkdown() {
|
|
|
|
setAdvancedEditor(!advancedEditor);
|
|
|
|
}
|
|
|
|
|
|
|
|
useEffect(() => {
|
2020-07-28 04:19:00 +02:00
|
|
|
if (fileText && isLoadign) {
|
|
|
|
setIsLoadign(false);
|
2020-07-28 01:12:59 +02:00
|
|
|
}
|
2020-07-28 04:19:00 +02:00
|
|
|
}, [fileText, isLoadign, setIsLoadign]);
|
2020-07-28 01:12:59 +02:00
|
|
|
|
2020-07-28 04:19:00 +02:00
|
|
|
useEffect(() => {
|
|
|
|
function readFileStream(url) {
|
|
|
|
setIsLoadign(true);
|
|
|
|
return fetch(url).then(res => res.text());
|
|
|
|
}
|
|
|
|
|
|
|
|
async function updateEditorText(url) {
|
|
|
|
try {
|
|
|
|
const text = await readFileStream(url);
|
|
|
|
if (text) {
|
|
|
|
// Store original content
|
|
|
|
setPrevFileText(text);
|
|
|
|
// Update text editor form
|
|
|
|
updatePublishForm({ fileText: text });
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
// Handle error..
|
2020-07-28 01:12:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const isEditingFile = isStillEditing && uri && fileInfo;
|
|
|
|
|
|
|
|
if (isEditingFile) {
|
2020-07-28 04:19:00 +02:00
|
|
|
const { mime_type: mimeType } = fileInfo;
|
2020-07-28 01:12:59 +02:00
|
|
|
// Editing same file (previously published)
|
|
|
|
// User can use a different file to replace the content
|
2020-07-28 08:11:28 +02:00
|
|
|
if (!filePath && !fileText && streamingUrl && mimeType === 'text/markdown') {
|
2020-07-28 04:19:00 +02:00
|
|
|
setCurrentFileType(mimeType);
|
|
|
|
updateEditorText(streamingUrl);
|
2020-07-28 01:12:59 +02:00
|
|
|
}
|
|
|
|
}
|
2020-07-28 08:11:28 +02:00
|
|
|
}, [
|
|
|
|
uri,
|
|
|
|
fileText,
|
|
|
|
filePath,
|
|
|
|
fileInfo,
|
|
|
|
streamingUrl,
|
|
|
|
isStillEditing,
|
|
|
|
setPrevFileText,
|
|
|
|
updatePublishForm,
|
|
|
|
setCurrentFileType,
|
|
|
|
]);
|
2020-07-28 01:12:59 +02:00
|
|
|
|
|
|
|
return (
|
|
|
|
<FormField
|
|
|
|
type={!SIMPLE_SITE && advancedEditor ? 'markdown' : 'textarea'}
|
|
|
|
name="content_story"
|
|
|
|
label={label}
|
2020-07-28 04:19:00 +02:00
|
|
|
placeholder={isLoadign ? __('Loadign...') : __('My content for this story...')}
|
2020-07-28 01:12:59 +02:00
|
|
|
value={fileText}
|
2020-07-28 04:19:00 +02:00
|
|
|
disabled={isLoadign || disabled}
|
2020-07-28 01:12:59 +02:00
|
|
|
onChange={value => updatePublishForm({ fileText: advancedEditor ? value : value.target.value })}
|
2020-07-28 04:19:00 +02:00
|
|
|
quickActionLabel={!SIMPLE_SITE && (advancedEditor ? __('Simple Editor') : __('Advanced Editor'))}
|
2020-07-28 01:12:59 +02:00
|
|
|
quickActionHandler={toggleMarkdown}
|
|
|
|
textAreaMaxLength={FF_MAX_CHARS_IN_DESCRIPTION}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export default StoryEditor;
|