UNPKG

uxcore-tinymce

Version:

uxcore-tinymce component for uxcore.

229 lines (185 loc) 12.7 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; var _react = require('react'); var _react2 = _interopRequireDefault(_react); var _objectAssign = require('object-assign'); var _objectAssign2 = _interopRequireDefault(_objectAssign); var _propTypes = require('prop-types'); var _propTypes2 = _interopRequireDefault(_propTypes); var _util = require('./util'); var _util2 = _interopRequireDefault(_util); var _editorConfig = require('./editorConfig'); var _editorConfig2 = _interopRequireDefault(_editorConfig); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } function _defaults(obj, defaults) { var keys = Object.getOwnPropertyNames(defaults); for (var i = 0; i < keys.length; i++) { var key = keys[i]; var value = Object.getOwnPropertyDescriptor(defaults, key); if (value && value.configurable && obj[key] === undefined) { Object.defineProperty(obj, key, value); } } return obj; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : _defaults(subClass, superClass); } /** * Tinymce Component for uxcore * Inspired by react-tinymce: https://github.com/mzabriskie/react-tinymce * @author eternalsky * * Copyright 2014-2015, Uxcore Team, Alinw. * All rights reserved. */ // Include all of the Native DOM and custom events from: // https://github.com/tinymce/tinymce/blob/master/tools/docs/tinymce.Editor.js#L5-L12 var EVENTS = ['focusin', 'focusout', 'click', 'dblclick', 'mousedown', 'mouseup', 'mousemove', 'mouseover', 'beforepaste', 'paste', 'cut', 'copy', 'selectionchange', 'mouseout', 'mouseenter', 'mouseleave', 'keydown', 'keypress', 'keyup', 'contextmenu', 'dragend', 'dragover', 'draggesture', 'dragdrop', 'drop', 'drag', 'BeforeRenderUI', 'SetAttrib', 'PreInit', 'PostRender', 'init', 'deactivate', 'activate', 'NodeChange', 'BeforeExecCommand', 'ExecCommand', 'show', 'hide', 'ProgressState', 'LoadContent', 'SaveContent', 'BeforeSetContent', 'SetContent', 'BeforeGetContent', 'GetContent', 'VisualAid', 'remove', 'submit', 'reset', 'BeforeAddUndo', 'AddUndo', 'change', 'undo', 'redo', 'ClearUndos', 'ObjectSelected', 'ObjectResizeStart', 'ObjectResized', 'PreProcess', 'PostProcess', 'focus', 'blur']; // Note: because the capitalization of the events is weird, we're going to get // some inconsistently-named handlers, for example compare: // 'onMouseleave' and 'onNodeChange' var HANDLER_NAMES = EVENTS.map(function (event) { return 'on' + _util2["default"].uc_first(event); }); var Tinymce = function (_React$Component) { _inherits(Tinymce, _React$Component); function Tinymce(props) { _classCallCheck(this, Tinymce); var _this = _possibleConstructorReturn(this, _React$Component.call(this, props)); if (_typeof(window.tinymce) !== 'object') { console.warn('TinyMCE is not found in global, init failed'); } _this.id = _this.id || _util2["default"].uuid(); return _this; } Tinymce.prototype.componentDidMount = function componentDidMount() { var _this2 = this; // 延迟初始化的过程,防止初始化后立刻被卸载导致的卸载失败问题 this.initTimer = setTimeout(function () { _this2.init(_this2.props.config); }, 200); console.log('uxcore tinymce mount, id is ' + this.id); }; Tinymce.prototype.shouldComponentUpdate = function shouldComponentUpdate(nextProps) { return !_util2["default"].isEqual(this.props.content, nextProps.content) || !_util2["default"].isEqual(this.props.config, nextProps.config); }; Tinymce.prototype.componentDidUpdate = function componentDidUpdate(prevProps) { var _props = this.props, config = _props.config, content = _props.content; if (prevProps.content !== content && window.tinymce) { if (this.isInited) { var editor = window.tinymce.get(this.id); var currentContent = editor.getContent(); if (content !== currentContent) { this.setTinymceContent(content); // 调用后会打断中文输入 } } else { this.contentToBeSet = content; } } if (!_util2["default"].isEqual(config, prevProps.config)) { this.init(config, content); } }; Tinymce.prototype.componentWillUnmount = function componentWillUnmount() { this.remove(); console.log('uxcore tinymce unmount, id is ' + this.id); }; Tinymce.prototype.setTinymceContent = function setTinymceContent(value) { var me = this; var editor = window.tinymce.get(me.id); editor.setContent(value); editor.selection.select(editor.getBody(), true); editor.selection.collapse(false); }; Tinymce.prototype.saveRef = function saveRef(refName) { var me = this; return function (c) { me[refName] = c; }; }; Tinymce.prototype.resetValue = function resetValue(value) { var me = this; if (this.setValueTimer) { clearTimeout(this.setValueTimer); } this.setValueTimer = setTimeout(function () { if (me.isInited) { me.setTinymceContent(value); } else { me.contentToBeSet = value; } }, me.props.changeDelay); }; Tinymce.prototype.init = function init(config, content) { var _this3 = this; var me = this; if (me.isInited) { me.remove(); } // hide the textarea until init finished me.root.style.visibility = 'hidden'; var external_plugins = _extends({}, _editorConfig2["default"].external_plugins, config.external_plugins || {}); var uploadConfig = _extends({}, _editorConfig2["default"].uploadConfig, config.uploadConfig || {}); var trueConfig = (0, _objectAssign2["default"])({}, _editorConfig2["default"], config, { external_plugins: external_plugins, uploadConfig: uploadConfig }); trueConfig.selector = '#' + me.id; if (!trueConfig.language) { trueConfig.language = 'zh_CN'; } trueConfig.setup = function (editor) { _this3.editor = editor; EVENTS.forEach(function (event, index) { var handler = me.props[HANDLER_NAMES[index]]; if (typeof handler !== 'function') return; editor.on(event, function (e) { // native DOM events don't have access to the editor so we pass it here handler(e, editor); }); }); // need to set content here because the textarea will still have the // old `this.props.content` editor.on('init', function () { me.isInited = true; if (me.contentToBeSet) { editor.setContent(me.contentToBeSet); } else if (content) { editor.setContent(content); } }); }; window.tinymce.baseURL = 'https://g.alicdn.com/platform/c/tinymce/4.3.12'; window.tinymce.init(trueConfig); me.root.style.visibility = ''; }; Tinymce.prototype.remove = function remove() { if (this.initTimer) { clearTimeout(this.initTimer); this.initTimer = undefined; } if (window.tinymce) { window.tinymce.EditorManager.execCommand('mceRemoveEditor', true, this.id); } this.isInited = false; }; Tinymce.prototype.render = function render() { return _react2["default"].createElement('textarea', { ref: this.saveRef('root'), id: this.id, defaultValue: this.props.content, placeholder: this.props.placeholder }); }; return Tinymce; }(_react2["default"].Component); Tinymce.defaultProps = { config: {}, changeDelay: 500 }; // http://facebook.github.io/react/docs/reusable-components.html Tinymce.propTypes = { config: _propTypes2["default"].object, placeholder: _propTypes2["default"].string, content: _propTypes2["default"].string, changeDelay: _propTypes2["default"].number // eslint-disable-line }; // add handler propTypes HANDLER_NAMES.forEach(function (name) { Tinymce.propTypes[name] = _propTypes2["default"].func; }); Tinymce.displayName = 'Tinymce'; exports["default"] = Tinymce; module.exports = exports['default'];