mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-11-24 14:43:08 +03:00
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:
parent
378a6f9326
commit
7a0c6de2d7
@ -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>
|
||||
|
Loading…
Reference in New Issue
Block a user