-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
97 lines (91 loc) · 2.79 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
var $ = function(id) {
return typeof id === "string" ? document.getElementById(id) : id;
}
/*
* 获取
*/
function getCommand(fullClassName) {
var className = fullClassName.split(" ");
if(className.length > 0) {
var commandClassName = className[1];
if(typeof commandClassName == "string") {
return commandClassName;
}
}
return;
}
function richEditor() {
var frame = $(richedit);
frame.document.designMode = "on";
frame.document.contenteditable = true;
var buttons = document.getElementsByClassName("toolbar-icon"),
fontSelection = document.getElementById("font"),
fontSizeSelection = document.getElementById("fontSize"),
imageUpload = document.getElementById("upload");
frame.focus();
for(var i = 0, length = buttons.length ; i < length; i++) {
buttons[i].onclick = function() {
var command = getCommand.call(this,this.className),
that = this;
return function() {
var newClassName = that.className.split(" ");
if(newClassName.length > 2) {
that.className = newClassName[0] + " " + newClassName[1];
}
else {
if(newClassName[1] != "fontColor") {
that.className = that.className.concat(" toolbar-icon-checked");
}
}
if(newClassName[1] == "createlink") {
var value = prompt("请输入链接:","http://");
frame.document.execCommand(command,false,value);
frame.focus();
that.className = newClassName[0] + " " + newClassName[1];
}
else if(newClassName[1] == "pic" && newClassName[2] != "toolbar-icon-checked") {
document.forms[0].file.click();
that.className = newClassName[0] + " " + newClassName[1];
}
else {
frame.document.execCommand(command,false,null);
frame.focus();
}
}(i);
};
}
imageUpload.onchange = function() {
var file = this.files[0],
reader = new FileReader();
if(file) {
reader.onload = function(e) {
var dataUri = e.target.result;
img = document.createElement("img");
img.src = dataUri;
frame.document.execCommand("insertimage",false,img.src);
}
reader.readAsDataURL(file);
frame.focus();
}
}
fontSizeSelection.onchange = function() {
var that = this;
return function() {
var value = that.options[that.selectedIndex].value;
frame.document.execCommand("fontsize",false,value);
frame.focus();
}();
};
fontSelection.onchange = function() {
var that = this;
var fontname = ["宋体","微软雅黑","楷体","黑体","隶书"];
return function() {
var index = that.options.selectedIndex;
frame.document.execCommand("fontname",false,fontname[index]);
frame.focus();
}();
};
}
window.onload = function() {
richEditor();
}