Skip to content

Instantly share code, notes, and snippets.

@whoisryosuke
Created April 13, 2019 20:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save whoisryosuke/cb4fd0ca6929a03503ff35e07700328c to your computer and use it in GitHub Desktop.
Save whoisryosuke/cb4fd0ca6929a03503ff35e07700328c to your computer and use it in GitHub Desktop.
React / JS - Using React with a library that requires an DOM ID to target (e.g. `#signup-btn`) and how to hook in 3rd party actions into class - via: https://github.com/stfy/react-editor.js/blob/master/src/editor.js
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import EditorJS from '@editorjs/editorjs';
import commonTools from './common-tools';
class Editor extends Component {
static defaultProps = {
holderId: 'editorjs-holder',
customTools: {},
excludeDefaultTools: [],
onChange: () => {},
onReady: () => {},
data: {},
autofocus: true,
};
static propTypes = {
holderId: PropTypes.string,
customTools: PropTypes.object,
excludeDefaultTools: PropTypes.arrayOf(PropTypes.string),
onChange: PropTypes.func,
onReady: PropTypes.func,
data: PropTypes.object,
autofocus: PropTypes.bool,
};
constructor(props) {
super(props);
this._tools = this._initTools(props.tools, props.excludeTools);
this._onChange = props.onChange;
this._onReady = props.onReady;
this._el = React.createRef();
}
componentDidMount() {
this._initEditor();
}
componentWillUnmount() {
this.editor.destroy();
}
_initEditor = () => {
const { holderId, autofocus, data } = this.props;
this.editor = new EditorJS({
holderId,
autofocus,
data,
tools: this._tools,
onChange: this._handleChange,
onReady: this._handleReady,
});
};
_initTools = () => {
const { customTools, excludeDefaultTools } = this.props;
const toolsList = { ...commonTools, ...customTools };
if (excludeDefaultTools.length !== 0) {
return Object.keys(toolsList)
.filter(tool => !excludeDefaultTools.includes(tool))
.reduce((acc, curr) => ({ ...acc, [curr]: toolsList[curr] }), {});
}
return toolsList;
};
_handleChange = async () => {
const data = await this.editor.save();
this._onChange(data);
};
_handleReady = () => {
this._onReady();
};
render() {
const { holderId } = this.props;
return React.createElement('div', {
id: holderId,
ref: this._el,
});
}
}
export default Editor;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment