UNPKG

4.95 kBJavaScriptView Raw
1import React from 'react';
2import ReactDOM from 'react-dom';
3import EditorCore from './components/core/EditorCore.react';
4import EditorEventEmitter from './utils/EditorEventEmitter';
5import '../src/css/editor.scss';
6
7if(!Date.prototype.Format){
8 Date.prototype.Format = function(n) {
9 var i = {
10 "M+": this.getMonth() + 1,
11 "d+": this.getDate(),
12 "h+": this.getHours(),
13 "m+": this.getMinutes(),
14 "s+": this.getSeconds(),
15 "q+": Math.floor((this.getMonth() + 3) / 3),
16 S: this.getMilliseconds()
17 }, t;
18 /(y+)/.test(n) && (n = n.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length)));
19 for (t in i) new RegExp("(" + t + ")").test(n) && (n = n.replace(RegExp.$1, RegExp.$1.length == 1 ? i[t] : ("00" + i[t]).substr(("" + i[t]).length)));
20 return n
21 };
22}
23
24class Editor extends React.Component{
25 constructor(props){
26 super(props);
27 this.state = {
28 loaded: false,
29 reload: true
30 }
31 }
32 componentDidMount(){
33 this.index = EditorEventEmitter.editorSum;
34 EditorEventEmitter.addStartListener("start-"+this.index,this.handleChange.bind(this));
35 }
36 componentWillUnmount(){
37 var index = this.index;
38 EditorEventEmitter.removeStartListener("start-"+index,this.handleChange.bind(this));
39 }
40 componentDidUpdate(){
41 if(this.state.loaded && this.state.reload){
42 this.refs.editor.setContent(this.props.value || this.props.defaultValue);
43 }
44 }
45 handleChange(){
46 this.setState({
47 loaded:true
48 })
49 }
50 handleMountSuccess(){
51 EditorEventEmitter.mountEditorSuccess();
52 }
53 shouldComponentUpdate(nextProps, nextState){
54 // reload判断当前是否可以允许刷新
55 // loaded状态变化时,务必重新刷新
56 var currentValue = nextProps.value;
57 var editorValue = this.getContent();
58
59 if(this.state.loaded != nextState.loaded){
60 return true;
61 }else if(currentValue == editorValue){
62 return false;
63 }
64 return true;
65 }
66 getContent() {
67 return this.refs.editor?this.refs.editor.getContent(): "";
68 }
69 setContent(content) {
70 return this.refs.editor?this.refs.editor.setContent(content): "";
71 }
72 focusEditor() {
73 return this.refs.editor?this.refs.editor.focusEditor(): "";
74 }
75 findDOMNode(refName) {
76 return this.refs.editor?this.refs.editor.findDOMNode(refName): "";
77 }
78 render(){
79 var loaded = this.state.loaded;
80 var { value,defaultValue,...props} = this.props;
81 if(!this.state.loaded){
82 return (<div id={props.id} className="editor-contenteditable-div" style={{"minHeight":"30px","border":"1px solid #ddd"}}>正在加载...</div>)
83 }else{
84 return (<EditorCore ref="editor" {...props} onEditorMount={this.handleMountSuccess.bind(this)} />)
85 }
86 }
87}
88
89Editor.propTypes = {
90 "plugins": React.PropTypes.object,
91 "fontFamily": React.PropTypes.array,
92 "fontSize": React.PropTypes.array,
93 "paragraph": React.PropTypes.array,
94 "icons": React.PropTypes.arrayOf(React.PropTypes.string),
95 "value": React.PropTypes.string,
96 "defaultValue": React.PropTypes.string
97}
98Editor.defaultProps = {
99 "plugins":{
100 "image":{
101 "uploader":{
102 type:"default", // qiniu
103 name:"file",
104 url:"/upload",
105 qiniu:{
106 app:{
107 Bucket: "qtestbucket",
108 AK: "iN7NgwM31j4-BZacMjPrOQBs34UG1maYCAQmhdCV",
109 SK: "6QTOr2Jg1gcZEWDQXKOGZh5PziC2MCV5KsntT70j"
110 },
111 key:null,
112 upload_token:null,
113 domain:"http://o9sa2vijj.bkt.clouddn.com",
114 genKey:function(options){
115 return options.file.type +"-"+ options.file.size +"-"+ options.file.lastModifiedDate.valueOf() +"-"+ new Date().valueOf()+"-"+options.file.name;
116 }
117 }
118 }
119 }
120 },
121 "fontFamily":[
122 {"name":"宋体",value:"宋体, SimSun",defualt:true},
123 {"name":"隶书",value:"隶书, SimLi"},
124 {"name":"楷体",value:"楷体, SimKai"},
125 {"name":"微软雅黑",value:"微软雅黑, Microsoft YaHei"},
126 {"name":"黑体",value:"黑体, SimHei"},
127 {"name":"arial",value:"arial, helvetica, sans-serif"},
128 {"name":"arial black",value:"arial black, avant garde"},
129 {"name":"omic sans ms",value:"omic sans ms"},
130 {"name":"impact",value:"impact, chicago"},
131 {"name":"times new roman",value:"times new roman"},
132 {"name":"andale mono",value:"andale mono"}
133 ],
134 "fontSize": [
135 {"name":"10px",value:"1"},
136 {"name":"12px",value:"2"},
137 {"name":"16px",value:"3",defualt:true},
138 {"name":"18px",value:"4"},
139 {"name":"24px",value:"5"},
140 {"name":"32px",value:"6"},
141 {"name":"38px",value:"7"}
142 ],
143 "paragraph": [
144 {"name":"段落",value:"p",defualt:true},
145 {"name":"标题1",value:"h1"},
146 {"name":"标题2",value:"h2"},
147 {"name":"标题3",value:"h3"},
148 {"name":"标题4",value:"h4"},
149 {"name":"标题5",value:"h5"},
150 {"name":"标题6",value:"h6"}
151 ],
152 "icons":[
153 // video map print preview drafts link unlink formula
154 "undo redo bold italic underline strikethrough fontfamily fontsize forecolor ",
155 "removeformat insertorderedlist insertunorderedlist cleardoc indent outdent ",
156 "justifyleft justifycenter justifyright image inserttable"
157 ],
158 "value": "",
159 "defaultValue": "<p><br></p>"
160};
161
162module.exports = Editor;
\No newline at end of file