Added imageUpload function to Lexical Editor (#15634)

no issue

- A basic image upload function for testing the image card of the new Lexical Koenig Editor.
This commit is contained in:
Ronald Langeveld 2022-10-17 15:31:55 +07:00 committed by GitHub
parent 378a6f9326
commit 7a0c6de2d7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,6 +1,7 @@
import * as Sentry from '@sentry/ember';
import Component from '@glimmer/component';
import React, {Suspense} from 'react';
import ghostPaths from 'ghost-admin/utils/ghost-paths';
import {action} from '@ember/object';
import {inject as service} from '@ember/service';
@ -108,11 +109,44 @@ export default class KoenigLexicalEditor extends Component {
}
ReactComponent = () => {
const [uploadProgressPercentage] = React.useState(0); // not in use right now, but will need to decide how to handle the percentage state and pass to the Image Cards
// const uploadProgress = (event) => {
// const percentComplete = (event.loaded / event.total) * 100;
// setUploadProgressPercentage(percentComplete);
// };
async function imageUploader(files) {
function uploadToUrl(formData, url) {
return new Promise((resolve, reject) => {
const xhr = new XMLHttpRequest();
xhr.open('POST', url);
// xhr.upload.onprogress = (event) => {
// uploadProgress(event);
// };
xhr.onload = () => resolve(xhr.response);
xhr.onerror = () => reject(xhr.statusText);
xhr.send(formData);
});
}
const formData = new FormData();
formData.append('file', files[0]);
const url = `${ghostPaths().apiRoot}/images/upload/`;
const response = await uploadToUrl(formData, url);
const dataset = JSON.parse(response);
const imageUrl = dataset?.images?.[0].url;
return {
src: imageUrl
};
}
return (
<div className={['koenig-react-editor', this.args.className].filter(Boolean).join(' ')}>
<ErrorHandler>
<Suspense fallback={<p className="koenig-react-editor-loading">Loading editor...</p>}>
<KoenigComposer initialEditorState={this.args.lexical} onError={this.onError}>
<KoenigComposer
initialEditorState={this.args.lexical}
onError={this.onError}
imageUploadFunction={{imageUploader, uploadProgressPercentage}} >
<KoenigEditor onChange={this.args.onChange} />
</KoenigComposer>
</Suspense>