2022-09-24 14:25:00 +03:00
|
|
|
import * as Sentry from '@sentry/ember';
|
2022-08-23 13:45:50 +03:00
|
|
|
import Component from '@glimmer/component';
|
|
|
|
import React, {Suspense} from 'react';
|
2022-10-17 11:31:55 +03:00
|
|
|
import ghostPaths from 'ghost-admin/utils/ghost-paths';
|
2022-09-16 17:13:50 +03:00
|
|
|
import {action} from '@ember/object';
|
2022-11-03 14:14:36 +03:00
|
|
|
import {inject} from 'ghost-admin/decorators/inject';
|
2023-02-13 16:38:19 +03:00
|
|
|
import {inject as service} from '@ember/service';
|
|
|
|
|
|
|
|
export const fileTypes = {
|
|
|
|
image: {
|
|
|
|
mimeTypes: ['image/gif', 'image/jpg', 'image/jpeg', 'image/png', 'image/svg+xml', 'image/webp'],
|
|
|
|
extensions: ['gif', 'jpg', 'jpeg', 'png', 'svg', 'svgz', 'webp'],
|
|
|
|
endpoint: '/images/upload/',
|
|
|
|
resourceName: 'images'
|
|
|
|
},
|
|
|
|
video: {
|
|
|
|
mimeTypes: ['video/mp4', 'video/webm', 'video/ogg'],
|
|
|
|
extensions: ['mp4', 'webm', 'ogv'],
|
|
|
|
endpoint: '/media/upload/',
|
|
|
|
resourceName: 'media'
|
|
|
|
},
|
|
|
|
audio: {
|
|
|
|
mimeTypes: ['audio/mp3', 'audio/mpeg', 'audio/ogg', 'audio/wav', 'audio/vnd.wav', 'audio/wave', 'audio/x-wav', 'audio/mp4', 'audio/x-m4a'],
|
|
|
|
extensions: ['mp3', 'wav', 'ogg', 'm4a'],
|
|
|
|
endpoint: '/media/upload/',
|
|
|
|
resourceName: 'media'
|
|
|
|
},
|
|
|
|
mediaThumbnail: {
|
|
|
|
mimeTypes: ['image/gif', 'image/jpg', 'image/jpeg', 'image/png', 'image/webp'],
|
|
|
|
extensions: ['gif', 'jpg', 'jpeg', 'png', 'webp'],
|
|
|
|
endpoint: '/media/thumbnail/upload/',
|
2023-02-13 18:00:10 +03:00
|
|
|
requestMethod: 'put',
|
|
|
|
resourceName: 'media'
|
2023-02-13 16:38:19 +03:00
|
|
|
}
|
|
|
|
};
|
2022-08-23 13:45:50 +03:00
|
|
|
|
|
|
|
class ErrorHandler extends React.Component {
|
|
|
|
state = {
|
|
|
|
hasError: false
|
|
|
|
};
|
|
|
|
|
|
|
|
static getDerivedStateFromError() {
|
|
|
|
return {hasError: true};
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
if (this.state.hasError) {
|
|
|
|
return (
|
|
|
|
<p className="koenig-react-editor-error">Loading has failed. Try refreshing the browser!</p>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.props.children;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const fetchKoenig = function () {
|
|
|
|
let status = 'pending';
|
|
|
|
let response;
|
|
|
|
|
|
|
|
const fetchPackage = async () => {
|
2022-09-13 20:55:28 +03:00
|
|
|
if (window['@tryghost/koenig-lexical']) {
|
|
|
|
return window['@tryghost/koenig-lexical'];
|
2022-08-23 13:45:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// the manual specification of the protocol in the import template string is
|
|
|
|
// required to work around ember-auto-import complaining about an unknown dynamic import
|
|
|
|
// during the build step
|
|
|
|
const GhostAdmin = window.GhostAdmin || window.Ember.Namespace.NAMESPACES.find(ns => ns.name === 'ghost-admin');
|
2022-11-04 12:25:52 +03:00
|
|
|
const urlTemplate = GhostAdmin.__container__.lookup('config:main').editor?.url;
|
|
|
|
const urlVersion = GhostAdmin.__container__.lookup('config:main').editor?.version;
|
2022-09-13 13:30:07 +03:00
|
|
|
|
|
|
|
const url = new URL(urlTemplate.replace('{version}', urlVersion));
|
2022-08-23 13:45:50 +03:00
|
|
|
|
|
|
|
if (url.protocol === 'http:') {
|
|
|
|
await import(`http://${url.host}${url.pathname}`);
|
|
|
|
} else {
|
|
|
|
await import(`https://${url.host}${url.pathname}`);
|
|
|
|
}
|
|
|
|
|
2022-09-13 13:30:07 +03:00
|
|
|
return window['@tryghost/koenig-lexical'];
|
2022-08-23 13:45:50 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
const suspender = fetchPackage().then(
|
|
|
|
(res) => {
|
|
|
|
status = 'success';
|
|
|
|
response = res;
|
|
|
|
},
|
|
|
|
(err) => {
|
|
|
|
status = 'error';
|
|
|
|
response = err;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
const read = () => {
|
|
|
|
switch (status) {
|
|
|
|
case 'pending':
|
|
|
|
throw suspender;
|
|
|
|
case 'error':
|
|
|
|
throw response;
|
|
|
|
default:
|
|
|
|
return response;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return {read};
|
|
|
|
};
|
|
|
|
|
|
|
|
const editorResource = fetchKoenig();
|
|
|
|
|
|
|
|
const KoenigComposer = (props) => {
|
|
|
|
const {KoenigComposer: _KoenigComposer} = editorResource.read();
|
|
|
|
return <_KoenigComposer {...props} />;
|
|
|
|
};
|
|
|
|
|
|
|
|
const KoenigEditor = (props) => {
|
|
|
|
const {KoenigEditor: _KoenigEditor} = editorResource.read();
|
|
|
|
return <_KoenigEditor {...props} />;
|
|
|
|
};
|
|
|
|
|
|
|
|
export default class KoenigLexicalEditor extends Component {
|
2023-02-13 16:38:19 +03:00
|
|
|
@service ajax;
|
|
|
|
|
2022-11-03 14:14:36 +03:00
|
|
|
@inject config;
|
2022-09-16 17:13:50 +03:00
|
|
|
|
|
|
|
@action
|
|
|
|
onError(error) {
|
|
|
|
// ensure we're still showing errors in development
|
|
|
|
console.error(error); // eslint-disable-line
|
|
|
|
|
2022-10-07 17:24:03 +03:00
|
|
|
if (this.config.sentry_dsn) {
|
2022-09-16 17:13:50 +03:00
|
|
|
Sentry.captureException(error, {
|
|
|
|
tags: {
|
|
|
|
lexical: true
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// don't rethrow, Lexical will attempt to gracefully recover
|
|
|
|
}
|
|
|
|
|
2022-08-23 13:45:50 +03:00
|
|
|
ReactComponent = () => {
|
2023-02-10 14:13:57 +03:00
|
|
|
const cardConfig = {
|
|
|
|
unsplash: {
|
|
|
|
defaultHeaders: {
|
|
|
|
Authorization: `Client-ID 8672af113b0a8573edae3aa3713886265d9bb741d707f6c01a486cde8c278980`,
|
|
|
|
'Accept-Version': 'v1',
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
'App-Pragma': 'no-cache',
|
|
|
|
'X-Unsplash-Cache': true
|
|
|
|
}
|
|
|
|
}
|
2022-12-07 05:20:11 +03:00
|
|
|
};
|
|
|
|
|
2023-02-13 18:00:10 +03:00
|
|
|
const useFileUpload = (type = 'image') => {
|
2023-02-13 16:38:19 +03:00
|
|
|
const [progress, setProgress] = React.useState(0);
|
|
|
|
const [isLoading, setLoading] = React.useState(false);
|
|
|
|
const [errors, setErrors] = React.useState([]);
|
|
|
|
const [filesNumber, setFilesNumber] = React.useState(0);
|
|
|
|
|
|
|
|
const progressTracker = React.useRef(new Map());
|
|
|
|
|
|
|
|
function updateProgress() {
|
|
|
|
if (progressTracker.current.size === 0) {
|
|
|
|
setProgress(0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let totalProgress = 0;
|
|
|
|
|
|
|
|
progressTracker.current.forEach(value => totalProgress += value);
|
2022-10-17 11:31:55 +03:00
|
|
|
|
2023-02-13 16:38:19 +03:00
|
|
|
setProgress(Math.round(totalProgress / progressTracker.current.size));
|
2022-11-21 06:52:37 +03:00
|
|
|
}
|
2022-10-17 11:31:55 +03:00
|
|
|
|
2023-02-13 16:38:19 +03:00
|
|
|
// we only check the file extension by default because IE doesn't always
|
|
|
|
// expose the mime-type, we'll rely on the API for final validation
|
|
|
|
function defaultValidator(file) {
|
|
|
|
let extensions = fileTypes[type].extensions;
|
|
|
|
let [, extension] = (/(?:\.([^.]+))?$/).exec(file.name);
|
|
|
|
|
|
|
|
// if extensions is falsy exit early and accept all files
|
|
|
|
if (!extensions) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!Array.isArray(extensions)) {
|
|
|
|
extensions = extensions.split(',');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!extension || extensions.indexOf(extension.toLowerCase()) === -1) {
|
|
|
|
let validExtensions = `.${extensions.join(', .').toUpperCase()}`;
|
|
|
|
return `The file type you uploaded is not supported. Please use ${validExtensions}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2022-10-17 11:31:55 +03:00
|
|
|
}
|
2023-02-13 16:38:19 +03:00
|
|
|
|
|
|
|
const validate = (files = []) => {
|
|
|
|
const validationResult = [];
|
|
|
|
|
|
|
|
for (let i = 0; i < files.length; i += 1) {
|
|
|
|
let file = files[i];
|
|
|
|
let result = defaultValidator(file);
|
|
|
|
if (result === true) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
validationResult.push({fileName: file.name, message: result});
|
|
|
|
}
|
|
|
|
|
|
|
|
return validationResult;
|
2022-10-17 11:31:55 +03:00
|
|
|
};
|
2023-02-13 16:38:19 +03:00
|
|
|
|
2023-02-13 18:00:10 +03:00
|
|
|
const _uploadFile = async (file, {formData = {}} = {}) => {
|
2023-02-13 16:38:19 +03:00
|
|
|
progressTracker.current[file] = 0;
|
|
|
|
|
|
|
|
const fileFormData = new FormData();
|
|
|
|
fileFormData.append('file', file, file.name);
|
|
|
|
|
|
|
|
Object.keys(formData || {}).forEach((key) => {
|
|
|
|
fileFormData.append(key, formData[key]);
|
|
|
|
});
|
|
|
|
|
|
|
|
const url = `${ghostPaths().apiRoot}${fileTypes[type].endpoint}`;
|
|
|
|
|
|
|
|
try {
|
2023-02-13 18:00:10 +03:00
|
|
|
const requestMethod = fileTypes[type].requestMethod || 'post';
|
2023-02-13 16:38:19 +03:00
|
|
|
const response = await this.ajax[requestMethod](url, {
|
|
|
|
data: fileFormData,
|
|
|
|
processData: false,
|
|
|
|
contentType: false,
|
|
|
|
dataType: 'text',
|
|
|
|
xhr: () => {
|
|
|
|
const xhr = new window.XMLHttpRequest();
|
|
|
|
|
|
|
|
xhr.upload.addEventListener('progress', (event) => {
|
|
|
|
if (event.lengthComputable) {
|
|
|
|
progressTracker.current.set(file, (event.loaded / event.total) * 100);
|
|
|
|
updateProgress();
|
|
|
|
}
|
|
|
|
}, false);
|
|
|
|
|
|
|
|
return xhr;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// force tracker progress to 100% in case we didn't get a final event
|
|
|
|
progressTracker.current.set(file, 100);
|
|
|
|
updateProgress();
|
|
|
|
|
|
|
|
let uploadResponse;
|
|
|
|
let responseUrl;
|
|
|
|
|
|
|
|
try {
|
|
|
|
uploadResponse = JSON.parse(response);
|
|
|
|
} catch (error) {
|
|
|
|
if (!(error instanceof SyntaxError)) {
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (uploadResponse) {
|
|
|
|
const resource = uploadResponse[fileTypes[type].resourceName];
|
|
|
|
if (resource && Array.isArray(resource) && resource[0]) {
|
|
|
|
responseUrl = resource[0].url;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
url: responseUrl,
|
|
|
|
fileName: file.name
|
|
|
|
};
|
|
|
|
} catch (error) {
|
|
|
|
console.error(error); // eslint-disable-line
|
|
|
|
|
|
|
|
// grab custom error message if present
|
|
|
|
let message = error.payload?.errors?.[0]?.message || '';
|
|
|
|
let context = error.payload?.errors?.[0]?.context || '';
|
|
|
|
|
|
|
|
// fall back to EmberData/ember-ajax default message for error type
|
|
|
|
if (!message) {
|
|
|
|
message = error.message;
|
|
|
|
}
|
|
|
|
|
2023-02-27 11:49:49 +03:00
|
|
|
// TODO: check for or expose known error types?
|
2023-02-13 16:38:19 +03:00
|
|
|
const errorResult = {
|
|
|
|
message,
|
|
|
|
context,
|
|
|
|
fileName: file.name
|
|
|
|
};
|
|
|
|
|
2023-02-27 11:49:49 +03:00
|
|
|
throw errorResult;
|
2023-02-13 16:38:19 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-02-13 18:00:10 +03:00
|
|
|
const upload = async (files = [], options = {}) => {
|
2023-02-13 16:38:19 +03:00
|
|
|
setFilesNumber(files.length);
|
|
|
|
setLoading(true);
|
|
|
|
|
|
|
|
const validationResult = validate(files);
|
|
|
|
|
|
|
|
if (validationResult.length) {
|
|
|
|
setErrors(validationResult);
|
|
|
|
setLoading(false);
|
|
|
|
setProgress(100);
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
const uploadPromises = [];
|
|
|
|
|
|
|
|
for (let i = 0; i < files.length; i += 1) {
|
|
|
|
const file = files[i];
|
2023-02-13 18:00:10 +03:00
|
|
|
uploadPromises.push(_uploadFile(file, options));
|
2023-02-13 16:38:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
const uploadResult = await Promise.all(uploadPromises);
|
|
|
|
setProgress(100);
|
|
|
|
progressTracker.current.clear();
|
|
|
|
|
|
|
|
setLoading(false);
|
|
|
|
|
|
|
|
setErrors([]); // components expect array of objects: { fileName: string, message: string }[]
|
|
|
|
|
|
|
|
return uploadResult;
|
|
|
|
} catch (error) {
|
|
|
|
console.error(error); // eslint-disable-line no-console
|
|
|
|
|
2023-02-27 11:49:49 +03:00
|
|
|
setErrors([...errors, error]);
|
2023-02-13 16:38:19 +03:00
|
|
|
setLoading(false);
|
|
|
|
setProgress(100);
|
|
|
|
progressTracker.current.clear();
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return {progress, isLoading, upload, errors, filesNumber};
|
|
|
|
};
|
|
|
|
|
2022-08-23 13:45:50 +03:00
|
|
|
return (
|
|
|
|
<div className={['koenig-react-editor', this.args.className].filter(Boolean).join(' ')}>
|
|
|
|
<ErrorHandler>
|
|
|
|
<Suspense fallback={<p className="koenig-react-editor-loading">Loading editor...</p>}>
|
2022-11-03 14:14:36 +03:00
|
|
|
<KoenigComposer
|
2023-02-10 14:13:57 +03:00
|
|
|
cardConfig={cardConfig}
|
2022-11-03 14:14:36 +03:00
|
|
|
initialEditorState={this.args.lexical}
|
|
|
|
onError={this.onError}
|
2023-02-13 16:38:19 +03:00
|
|
|
fileUploader={{useFileUpload, fileTypes}}
|
2022-12-01 19:46:00 +03:00
|
|
|
>
|
|
|
|
<KoenigEditor
|
|
|
|
onChange={this.args.onChange}
|
|
|
|
registerAPI={this.args.registerAPI}
|
|
|
|
cursorDidExitAtTop={this.args.cursorDidExitAtTop}
|
|
|
|
/>
|
2022-08-23 13:45:50 +03:00
|
|
|
</KoenigComposer>
|
|
|
|
</Suspense>
|
|
|
|
</ErrorHandler>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
}
|