UNPKG

8.62 kBJavaScriptView Raw
1'use strict';
2
3var _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; };
4
5var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
6
7var _react = require('react');
8
9var _react2 = _interopRequireDefault(_react);
10
11var _reactDom = require('react-dom');
12
13var _reactDom2 = _interopRequireDefault(_reactDom);
14
15var _EditorCore = require('./components/core/EditorCore.react');
16
17var _EditorCore2 = _interopRequireDefault(_EditorCore);
18
19var _EditorEventEmitter = require('./utils/EditorEventEmitter');
20
21var _EditorEventEmitter2 = _interopRequireDefault(_EditorEventEmitter);
22
23require('../src/css/editor.scss');
24
25function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
26
27function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; }
28
29function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
30
31function _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; }
32
33function _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) : subClass.__proto__ = superClass; }
34
35if (!Date.prototype.Format) {
36 Date.prototype.Format = function (n) {
37 var i = {
38 "M+": this.getMonth() + 1,
39 "d+": this.getDate(),
40 "h+": this.getHours(),
41 "m+": this.getMinutes(),
42 "s+": this.getSeconds(),
43 "q+": Math.floor((this.getMonth() + 3) / 3),
44 S: this.getMilliseconds()
45 },
46 t;
47 /(y+)/.test(n) && (n = n.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length)));
48 for (t in i) {
49 new RegExp("(" + t + ")").test(n) && (n = n.replace(RegExp.$1, RegExp.$1.length == 1 ? i[t] : ("00" + i[t]).substr(("" + i[t]).length)));
50 }return n;
51 };
52}
53
54var Editor = function (_React$Component) {
55 _inherits(Editor, _React$Component);
56
57 function Editor(props) {
58 _classCallCheck(this, Editor);
59
60 var _this = _possibleConstructorReturn(this, (Editor.__proto__ || Object.getPrototypeOf(Editor)).call(this, props));
61
62 _this.state = {
63 loaded: false,
64 reload: true
65 };
66 return _this;
67 }
68
69 _createClass(Editor, [{
70 key: 'componentDidMount',
71 value: function componentDidMount() {
72 this.index = _EditorEventEmitter2.default.editorSum;
73 _EditorEventEmitter2.default.addStartListener("start-" + this.index, this.handleChange.bind(this));
74 }
75 }, {
76 key: 'componentWillUnmount',
77 value: function componentWillUnmount() {
78 var index = this.index;
79 _EditorEventEmitter2.default.removeStartListener("start-" + index, this.handleChange.bind(this));
80 }
81 }, {
82 key: 'componentDidUpdate',
83 value: function componentDidUpdate() {
84 if (this.state.loaded && this.state.reload) {
85 this.refs.editor.setContent(this.props.value || this.props.defaultValue);
86 }
87 }
88 }, {
89 key: 'handleChange',
90 value: function handleChange() {
91 this.setState({
92 loaded: true
93 });
94 }
95 }, {
96 key: 'handleMountSuccess',
97 value: function handleMountSuccess() {
98 _EditorEventEmitter2.default.mountEditorSuccess();
99 }
100 }, {
101 key: 'shouldComponentUpdate',
102 value: function shouldComponentUpdate(nextProps, nextState) {
103 // reload判断当前是否可以允许刷新
104 // loaded状态变化时,务必重新刷新
105 var currentValue = nextProps.value;
106 var editorValue = this.getContent();
107
108 if (this.state.loaded != nextState.loaded) {
109 return true;
110 } else if (currentValue == editorValue) {
111 return false;
112 }
113 return true;
114 }
115 }, {
116 key: 'getContent',
117 value: function getContent() {
118 return this.refs.editor ? this.refs.editor.getContent() : "";
119 }
120 }, {
121 key: 'setContent',
122 value: function setContent(content) {
123 return this.refs.editor ? this.refs.editor.setContent(content) : "";
124 }
125 }, {
126 key: 'focusEditor',
127 value: function focusEditor() {
128 return this.refs.editor ? this.refs.editor.focusEditor() : "";
129 }
130 }, {
131 key: 'findDOMNode',
132 value: function findDOMNode(refName) {
133 return this.refs.editor ? this.refs.editor.findDOMNode(refName) : "";
134 }
135 }, {
136 key: 'render',
137 value: function render() {
138 var loaded = this.state.loaded;
139
140 var _props = this.props,
141 value = _props.value,
142 defaultValue = _props.defaultValue,
143 props = _objectWithoutProperties(_props, ['value', 'defaultValue']);
144
145 if (!this.state.loaded) {
146 return _react2.default.createElement(
147 'div',
148 { id: props.id, className: 'editor-contenteditable-div', style: { "minHeight": "30px", "border": "1px solid #ddd" } },
149 '\u6B63\u5728\u52A0\u8F7D...'
150 );
151 } else {
152 return _react2.default.createElement(_EditorCore2.default, _extends({ ref: 'editor' }, props, { onEditorMount: this.handleMountSuccess.bind(this) }));
153 }
154 }
155 }]);
156
157 return Editor;
158}(_react2.default.Component);
159
160Editor.propTypes = {
161 "plugins": _react2.default.PropTypes.object,
162 "fontFamily": _react2.default.PropTypes.array,
163 "fontSize": _react2.default.PropTypes.array,
164 "paragraph": _react2.default.PropTypes.array,
165 "icons": _react2.default.PropTypes.arrayOf(_react2.default.PropTypes.string),
166 "value": _react2.default.PropTypes.string,
167 "defaultValue": _react2.default.PropTypes.string
168};
169Editor.defaultProps = {
170 "plugins": {
171 "image": {
172 "uploader": {
173 type: "default", // qiniu
174 name: "file",
175 url: "/upload",
176 qiniu: {
177 app: {
178 Bucket: "qtestbucket",
179 AK: "iN7NgwM31j4-BZacMjPrOQBs34UG1maYCAQmhdCV",
180 SK: "6QTOr2Jg1gcZEWDQXKOGZh5PziC2MCV5KsntT70j"
181 },
182 key: null,
183 upload_token: null,
184 domain: "http://o9sa2vijj.bkt.clouddn.com",
185 genKey: function genKey(options) {
186 return options.file.type + "-" + options.file.size + "-" + options.file.lastModifiedDate.valueOf() + "-" + new Date().valueOf() + "-" + options.file.name;
187 }
188 }
189 }
190 }
191 },
192 "fontFamily": [{ "name": "宋体", value: "宋体, SimSun", defualt: true }, { "name": "隶书", value: "隶书, SimLi" }, { "name": "楷体", value: "楷体, SimKai" }, { "name": "微软雅黑", value: "微软雅黑, Microsoft YaHei" }, { "name": "黑体", value: "黑体, SimHei" }, { "name": "arial", value: "arial, helvetica, sans-serif" }, { "name": "arial black", value: "arial black, avant garde" }, { "name": "omic sans ms", value: "omic sans ms" }, { "name": "impact", value: "impact, chicago" }, { "name": "times new roman", value: "times new roman" }, { "name": "andale mono", value: "andale mono" }],
193 "fontSize": [{ "name": "10px", value: "1" }, { "name": "12px", value: "2" }, { "name": "16px", value: "3", defualt: true }, { "name": "18px", value: "4" }, { "name": "24px", value: "5" }, { "name": "32px", value: "6" }, { "name": "38px", value: "7" }],
194 "paragraph": [{ "name": "段落", value: "p", defualt: true }, { "name": "标题1", value: "h1" }, { "name": "标题2", value: "h2" }, { "name": "标题3", value: "h3" }, { "name": "标题4", value: "h4" }, { "name": "标题5", value: "h5" }, { "name": "标题6", value: "h6" }],
195 "icons": [
196 // video map print preview drafts link unlink formula
197 "undo redo bold italic underline strikethrough fontfamily fontsize forecolor ", "removeformat insertorderedlist insertunorderedlist cleardoc indent outdent ", "justifyleft justifycenter justifyright image inserttable"],
198 "value": "",
199 "defaultValue": "<p><br></p>"
200};
201
202module.exports = Editor;
\No newline at end of file