2023-04-18 10:41:48 +03:00
|
|
|
import * as Sentry from '@sentry/ember';
|
|
|
|
import Component from '@glimmer/component';
|
|
|
|
import React, {Suspense} from 'react';
|
|
|
|
import {action} from '@ember/object';
|
|
|
|
import {inject} from 'ghost-admin/decorators/inject';
|
|
|
|
import {inject as service} from '@ember/service';
|
|
|
|
|
|
|
|
class ErrorHandler extends React.Component {
|
|
|
|
state = {
|
|
|
|
hasError: false
|
|
|
|
};
|
|
|
|
|
|
|
|
static getDerivedStateFromError() {
|
|
|
|
return {hasError: true};
|
|
|
|
}
|
|
|
|
|
2023-12-07 17:11:07 +03:00
|
|
|
componentDidCatch(error) {
|
|
|
|
if (this.props.config.sentry_dsn) {
|
|
|
|
Sentry.captureException(error, {
|
|
|
|
tags: {
|
|
|
|
lexical: true
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-01-17 16:39:15 +03:00
|
|
|
console.error(error); // eslint-disable-line
|
2023-12-07 17:11:07 +03:00
|
|
|
}
|
|
|
|
|
2023-04-18 10:41:48 +03:00
|
|
|
render() {
|
|
|
|
if (this.state.hasError) {
|
|
|
|
return (
|
|
|
|
<p className="koenig-react-editor-error">Loading has failed. Try refreshing the browser!</p>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.props.children;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-04 19:36:24 +03:00
|
|
|
const KoenigComposer = ({editorResource, ...props}) => {
|
2023-04-18 10:41:48 +03:00
|
|
|
const {KoenigComposer: _KoenigComposer, MINIMAL_NODES: _MINIMAL_NODES} = editorResource.read();
|
|
|
|
return <_KoenigComposer nodes={_MINIMAL_NODES} {...props} />;
|
|
|
|
};
|
|
|
|
|
2023-10-04 19:36:24 +03:00
|
|
|
const KoenigComposableEditor = ({editorResource, ...props}) => {
|
2023-04-18 10:41:48 +03:00
|
|
|
const {KoenigComposableEditor: _KoenigComposableEditor, MINIMAL_TRANSFORMERS: _MINIMAL_TRANSFORMERS} = editorResource.read();
|
|
|
|
return <_KoenigComposableEditor markdownTransformers={_MINIMAL_TRANSFORMERS} {...props} />;
|
|
|
|
};
|
|
|
|
|
2023-10-04 19:36:24 +03:00
|
|
|
const HtmlOutputPlugin = ({editorResource, ...props}) => {
|
2023-04-18 10:41:48 +03:00
|
|
|
const {HtmlOutputPlugin: _HtmlOutputPlugin} = editorResource.read();
|
|
|
|
return <_HtmlOutputPlugin {...props} />;
|
|
|
|
};
|
|
|
|
|
2023-11-02 00:22:56 +03:00
|
|
|
const EmojiPickerPlugin = ({editorResource, ...props}) => {
|
|
|
|
const {EmojiPickerPlugin: _EmojiPickerPlugin} = editorResource.read();
|
|
|
|
return <_EmojiPickerPlugin {...props} />;
|
|
|
|
};
|
|
|
|
|
2023-12-07 18:46:23 +03:00
|
|
|
const TKCountPlugin = ({editorResource, ...props}) => {
|
|
|
|
const {TKCountPlugin: _TKCountPlugin} = editorResource.read();
|
|
|
|
return <_TKCountPlugin {...props} />;
|
|
|
|
};
|
|
|
|
|
2023-04-18 10:41:48 +03:00
|
|
|
export default class KoenigLexicalEditorInput extends Component {
|
|
|
|
@service ajax;
|
|
|
|
@service feature;
|
2023-10-04 19:36:24 +03:00
|
|
|
@service koenig;
|
2023-04-18 10:41:48 +03:00
|
|
|
@service session;
|
|
|
|
|
|
|
|
@inject config;
|
|
|
|
|
2023-10-04 19:36:24 +03:00
|
|
|
editorResource = this.koenig.resource;
|
|
|
|
|
2023-11-02 00:22:56 +03:00
|
|
|
get emojiPicker() {
|
|
|
|
return this.args.emojiPicker ?? true;
|
|
|
|
}
|
|
|
|
|
2023-04-18 10:41:48 +03:00
|
|
|
@action
|
|
|
|
onError(error) {
|
|
|
|
// ensure we're still showing errors in development
|
|
|
|
console.error(error); // eslint-disable-line
|
|
|
|
|
|
|
|
if (this.config.sentry_dsn) {
|
|
|
|
Sentry.captureException(error, {
|
|
|
|
tags: {
|
|
|
|
lexical: true
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// don't rethrow, Lexical will attempt to gracefully recover
|
|
|
|
}
|
|
|
|
|
2023-05-08 11:15:37 +03:00
|
|
|
ReactComponent = (props) => {
|
2023-04-18 10:41:48 +03:00
|
|
|
return (
|
|
|
|
<div className={['koenig-react-editor', this.args.className].filter(Boolean).join(' ')}>
|
2023-12-07 17:11:07 +03:00
|
|
|
<ErrorHandler config={this.config}>
|
2023-04-18 10:41:48 +03:00
|
|
|
<Suspense fallback={<p className="koenig-react-editor-loading">Loading editor...</p>}>
|
|
|
|
<KoenigComposer
|
2023-10-04 19:36:24 +03:00
|
|
|
editorResource={this.editorResource}
|
2023-04-18 10:41:48 +03:00
|
|
|
initialEditorState={this.args.lexical}
|
|
|
|
onError={this.onError}
|
2023-12-07 18:46:23 +03:00
|
|
|
isTKEnabled={this.args.onTKCountChange ? true : false}
|
2023-04-18 10:41:48 +03:00
|
|
|
>
|
|
|
|
<KoenigComposableEditor
|
2023-10-04 19:36:24 +03:00
|
|
|
editorResource={this.editorResource}
|
2023-04-18 10:41:48 +03:00
|
|
|
darkMode={this.feature.nightShift}
|
2023-04-20 12:21:55 +03:00
|
|
|
onChange={props.onChange}
|
|
|
|
onBlur={props.onBlur}
|
2023-10-10 20:30:47 +03:00
|
|
|
onFocus={props.onFocus}
|
2023-04-18 10:41:48 +03:00
|
|
|
isSnippetsEnabled={false}
|
|
|
|
singleParagraph={true}
|
2023-10-25 13:19:16 +03:00
|
|
|
className={`koenig-lexical-editor-input ${this.feature.nightShift ? 'dark' : ''}`}
|
2023-04-18 10:41:48 +03:00
|
|
|
placeholderText={props.placeholderText}
|
2023-04-18 12:12:37 +03:00
|
|
|
placeholderClassName="koenig-lexical-editor-input-placeholder"
|
2023-12-07 18:46:23 +03:00
|
|
|
registerAPI={this.args.registerAPI}
|
2023-04-18 10:41:48 +03:00
|
|
|
>
|
2023-10-04 19:36:24 +03:00
|
|
|
<HtmlOutputPlugin editorResource={this.editorResource} html={props.html} setHtml={props.onChangeHtml} />
|
2023-11-02 00:22:56 +03:00
|
|
|
{this.emojiPicker ? <EmojiPickerPlugin editorResource={this.editorResource} /> : null}
|
2023-12-07 18:46:23 +03:00
|
|
|
{this.args.onTKCountChange ? <TKCountPlugin editorResource={this.editorResource} onChange={this.args.onTKCountChange} /> : null}
|
2023-04-18 10:41:48 +03:00
|
|
|
</KoenigComposableEditor>
|
|
|
|
</KoenigComposer>
|
|
|
|
</Suspense>
|
|
|
|
</ErrorHandler>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
}
|