2020-07-28 01:12:59 +02:00
|
|
|
// @flow
|
|
|
|
import React, { useEffect } from 'react';
|
2022-11-04 13:42:36 +01:00
|
|
|
import { FormFieldAreaAdvanced } from 'component/common/form';
|
2020-07-28 01:12:59 +02:00
|
|
|
|
|
|
|
type Props = {
|
|
|
|
uri: ?string,
|
|
|
|
label: ?string,
|
|
|
|
disabled: ?boolean,
|
2022-09-02 18:43:35 +02:00
|
|
|
filePath: File,
|
2020-07-28 01:12:59 +02:00
|
|
|
fileText: ?string,
|
2020-07-29 04:56:07 +02:00
|
|
|
fileMimeType: ?string,
|
2020-07-30 22:00:22 +02:00
|
|
|
streamingUrl: ?string,
|
2020-07-28 01:12:59 +02:00
|
|
|
isStillEditing: boolean,
|
2021-03-20 00:48:47 +01:00
|
|
|
fetchStreamingUrl: (string) => void,
|
|
|
|
setPrevFileText: (string) => void,
|
2020-07-28 01:12:59 +02:00
|
|
|
updatePublishForm: ({}) => void,
|
2021-03-20 00:48:47 +01:00
|
|
|
setCurrentFileType: (string) => void,
|
2020-07-28 01:12:59 +02:00
|
|
|
};
|
|
|
|
|
2020-07-29 22:30:26 +02:00
|
|
|
function PostEditor(props: Props) {
|
2020-07-28 01:12:59 +02:00
|
|
|
const {
|
|
|
|
uri,
|
|
|
|
label,
|
|
|
|
disabled,
|
|
|
|
filePath,
|
|
|
|
fileText,
|
2020-07-30 22:00:22 +02:00
|
|
|
streamingUrl,
|
2020-07-29 04:56:07 +02:00
|
|
|
fileMimeType,
|
2020-07-28 01:12:59 +02:00
|
|
|
isStillEditing,
|
|
|
|
setPrevFileText,
|
2020-07-30 22:00:22 +02:00
|
|
|
fetchStreamingUrl,
|
2020-07-28 01:12:59 +02:00
|
|
|
updatePublishForm,
|
2020-07-28 04:19:00 +02:00
|
|
|
setCurrentFileType,
|
2020-07-28 01:12:59 +02:00
|
|
|
} = props;
|
|
|
|
|
2020-07-29 04:56:07 +02:00
|
|
|
const editing = isStillEditing && uri;
|
|
|
|
|
|
|
|
const [ready, setReady] = React.useState(!editing);
|
|
|
|
const [loading, setLoading] = React.useState(false);
|
2020-07-28 01:12:59 +02:00
|
|
|
|
2020-07-30 22:00:22 +02:00
|
|
|
useEffect(() => {
|
2020-08-04 17:12:57 +02:00
|
|
|
if (editing && uri) {
|
2020-07-30 22:00:22 +02:00
|
|
|
fetchStreamingUrl(uri);
|
|
|
|
}
|
2020-12-01 18:56:59 +01:00
|
|
|
}, [uri, editing, fetchStreamingUrl]);
|
2020-07-30 22:00:22 +02:00
|
|
|
|
2020-07-29 04:56:07 +02:00
|
|
|
// Ready to edit content
|
|
|
|
useEffect(() => {
|
|
|
|
if (!ready && !loading && fileText && streamingUrl) {
|
|
|
|
setReady(true);
|
|
|
|
}
|
|
|
|
}, [ready, loading, fileText, streamingUrl]);
|
|
|
|
|
2020-07-28 01:12:59 +02:00
|
|
|
useEffect(() => {
|
2020-07-29 04:56:07 +02:00
|
|
|
if (fileText && loading) {
|
|
|
|
setLoading(false);
|
|
|
|
} else if (!fileText && loading) {
|
|
|
|
setLoading(true);
|
2020-07-28 01:12:59 +02:00
|
|
|
}
|
2020-07-29 04:56:07 +02:00
|
|
|
}, [fileText, loading, setLoading]);
|
2020-07-28 01:12:59 +02:00
|
|
|
|
2020-07-28 04:19:00 +02:00
|
|
|
useEffect(() => {
|
|
|
|
function readFileStream(url) {
|
2021-03-20 00:48:47 +01:00
|
|
|
return fetch(url).then((res) => res.text());
|
2020-07-28 04:19:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2020-12-01 18:56:59 +01:00
|
|
|
console.error(error); // eslint-disable-line
|
2020-07-28 01:12:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-29 04:56:07 +02:00
|
|
|
if (editing) {
|
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-29 04:56:07 +02:00
|
|
|
if (!ready && !filePath && !fileText && streamingUrl && fileMimeType === 'text/markdown') {
|
|
|
|
setCurrentFileType(fileMimeType);
|
2020-07-28 04:19:00 +02:00
|
|
|
updateEditorText(streamingUrl);
|
2020-07-28 01:12:59 +02:00
|
|
|
}
|
|
|
|
}
|
2020-07-28 08:11:28 +02:00
|
|
|
}, [
|
2020-07-29 04:56:07 +02:00
|
|
|
ready,
|
|
|
|
editing,
|
2020-07-28 08:11:28 +02:00
|
|
|
fileText,
|
|
|
|
filePath,
|
2020-07-29 04:56:07 +02:00
|
|
|
fileMimeType,
|
2020-07-28 08:11:28 +02:00
|
|
|
streamingUrl,
|
|
|
|
setPrevFileText,
|
|
|
|
updatePublishForm,
|
|
|
|
setCurrentFileType,
|
|
|
|
]);
|
2020-07-28 01:12:59 +02:00
|
|
|
|
|
|
|
return (
|
2022-11-04 13:42:36 +01:00
|
|
|
<FormFieldAreaAdvanced
|
2020-07-30 21:31:55 +02:00
|
|
|
type={'markdown'}
|
2020-07-29 22:30:26 +02:00
|
|
|
name="content_post"
|
2020-07-28 01:12:59 +02:00
|
|
|
label={label}
|
2020-07-29 22:30:26 +02:00
|
|
|
placeholder={__('My content for this post...')}
|
2020-07-29 04:56:07 +02:00
|
|
|
value={ready ? fileText : __('Loading...')}
|
|
|
|
disabled={!ready || disabled}
|
2021-03-20 00:48:47 +01:00
|
|
|
onChange={(value) => updatePublishForm({ fileText: value })}
|
2020-07-28 01:12:59 +02:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-07-29 22:30:26 +02:00
|
|
|
export default PostEditor;
|