@atlaskit/editor-core
Version:
A package contains Atlassian editor core functionality
90 lines • 4.61 kB
JavaScript
import * as tslib_1 from "tslib";
import * as React from 'react';
import { PropTypes } from 'react';
import { createEditor, getUiComponent } from './create-editor';
import { createPluginsList } from './create-editor';
import ProviderFactory from '../providerFactory';
import { moveCursorToTheEnd } from '../utils';
var Editor = (function (_super) {
tslib_1.__extends(Editor, _super);
function Editor(props) {
var _this = _super.call(this, props) || this;
_this.initEditor = function (place) {
if (!place) {
return;
}
var plugins = createPluginsList(_this.props);
var editor = createEditor(place, plugins, _this.props, _this.providerFactory);
_this.registerEditorForActions(editor);
_this.setState({ editor: editor });
// Focus editor first time we create it if shouldFocus prop is set to true.
if (_this.props.shouldFocus) {
if (!editor.editorView.hasFocus()) {
editor.editorView.focus();
}
moveCursorToTheEnd(editor.editorView);
}
};
_this.providerFactory = new ProviderFactory();
_this.state = {};
return _this;
}
Editor.prototype.componentDidMount = function () {
this.initUi();
this.handleProviders(this.props);
};
Editor.prototype.componentWillReceiveProps = function (nextProps) {
this.handleProviders(nextProps);
};
Editor.prototype.componentWillUnmount = function () {
if (!this.state.editor) {
return;
}
this.unregisterEditorFromActions();
this.state.editor.editorView.destroy();
if (this.state.editor.eventDispatcher) {
this.state.editor.eventDispatcher.destroy();
}
};
Editor.prototype.registerEditorForActions = function (editor) {
if (this.context && this.context.editorActions) {
this.context.editorActions._privateRegisterEditor(editor.editorView, editor.contentTransformer);
}
};
Editor.prototype.unregisterEditorFromActions = function () {
if (this.context && this.context.editorActions) {
this.context.editorActions._privateUnregisterEditor();
}
};
Editor.prototype.initUi = function () {
var component = getUiComponent(this.props.appearance);
this.setState({ component: component });
};
Editor.prototype.handleProviders = function (props) {
var emojiProvider = props.emojiProvider, mentionProvider = props.mentionProvider, mediaProvider = props.mediaProvider, collabEditProvider = props.collabEditProvider, activityProvider = props.activityProvider, presenceProvider = props.presenceProvider;
this.providerFactory.setProvider('emojiProvider', emojiProvider);
this.providerFactory.setProvider('mentionProvider', mentionProvider);
this.providerFactory.setProvider('mediaProvider', mediaProvider);
this.providerFactory.setProvider('collabEditProvider', collabEditProvider);
this.providerFactory.setProvider('activityProvider', activityProvider);
this.providerFactory.setProvider('presenceProvider', presenceProvider);
};
Editor.prototype.render = function () {
// tslint:disable-next-line:variable-name
var _a = this.state, Component = _a.component, _b = _a.editor, editor = _b === void 0 ? {} : _b;
if (!Component) {
return null;
}
var _c = editor, editorView = _c.editorView, contentComponents = _c.contentComponents, primaryToolbarComponents = _c.primaryToolbarComponents, secondaryToolbarComponents = _c.secondaryToolbarComponents, eventDispatcher = _c.eventDispatcher;
return (React.createElement(Component, { onUiReady: this.initEditor, editorView: editorView, providerFactory: this.providerFactory, eventDispatcher: eventDispatcher, maxHeight: this.props.maxHeight, contentComponents: contentComponents, primaryToolbarComponents: primaryToolbarComponents, secondaryToolbarComponents: secondaryToolbarComponents, customContentComponents: this.props.contentComponents, customPrimaryToolbarComponents: this.props.primaryToolbarComponents, customSecondaryToolbarComponents: this.props.secondaryToolbarComponents, addonToolbarComponents: this.props.addonToolbarComponents }));
};
return Editor;
}(React.Component));
export default Editor;
Editor.defaultProps = {
appearance: 'message'
};
Editor.contextTypes = {
editorActions: PropTypes.object
};
//# sourceMappingURL=index.js.map