forked from siliushi/artEditor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathartEditor.js
139 lines (138 loc) · 4.46 KB
/
artEditor.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/**
* 移动端富文本编辑器
* @author [email protected]
* @url https://github.com/baixuexiyang/artEditor
*/
$.fn.extend({
_opt: {
placeholader: '<p>请输入文章正文内容</p>',
validHtml: [],
limitSize: 3,
showServer: false
},
artEditor: function(options) {
var _this = this,
styles = {
"-webkit-user-select": "text",
"user-select": "text",
"overflow-y": "auto",
"text-break": "brak-all",
"outline": "none",
"cursor": "text"
};
$(this).css(styles).attr("contenteditable", true);
_this._opt = $.extend(_this._opt, options);
try{
$(_this._opt.imgTar).on('change', function(e) {
var file = e.target.files[0];
if(Math.ceil(file.size/1024/1024) > _this._opt.limitSize) {
console.error('文件太大');
return;
}
var reader = new FileReader();
reader.readAsDataURL(file);
reader.onload = function (f) {
if(_this._opt.showServer) {
_this.upload(f.target.result);
return ;
}
var img = '<img src="'+ f.target.result +'" style="width:90%;" />';
_this.insertImage(img);
};
});
_this.placeholderHandler();
_this.pasteHandler();
} catch(e) {
console.log(e);
}
if(_this._opt.formInputId && $('#'+_this._opt.formInputId)[0]) {
$(_this).on('input', function() {
$('#'+_this._opt.formInputId).val(_this.getValue());
});
}
},
upload: function(data) {
var _this = this, filed = _this._opt.uploadField;
$.ajax({
url: _this._opt.uploadUrl,
type: 'post',
data: $.extend(_this._opt.data, {filed: data}),
cache: false
})
.then(function(res) {
var src = _this._opt.uploadSuccess(res);
if(src) {
var img = '<img src="'+ src +'" style="width:90%;" />';
_this.insertImage(img);
} else {
_this._opt.uploadError(res);
}
});
},
insertImage: function(src) {
$(this).focus();
var selection = window.getSelection ? window.getSelection() : document.selection;
var range = selection.createRange ? selection.createRange() : selection.getRangeAt(0);
if (!window.getSelection) {
range.pasteHTML(src);
range.collapse(false);
range.select();
} else {
range.collapse(false);
var hasR = range.createContextualFragment(src);
var hasLastChild = hasR.lastChild;
while (hasLastChild && hasLastChild.nodeName.toLowerCase() == "br" && hasLastChild.previousSibling && hasLastChild.previousSibling.nodeName.toLowerCase() == "br") {
var e = hasLastChild;
hasLastChild = hasLastChild.previousSibling;
hasR.removeChild(e);
}
range.insertNode(range.createContextualFragment("<br/>"));
range.insertNode(hasR);
if (hasLastChild) {
range.setEndAfter(hasLastChild);
range.setStartAfter(hasLastChild);
}
selection.removeAllRanges();
selection.addRange(range);
}
if(this._opt.formInputId && $('#'+this._opt.formInputId)[0]) {
$('#'+this._opt.formInputId).val(this.getValue());
}
},
pasteHandler: function() {
var _this = this;
$(this).on("paste", function(e) {
console.log(e.clipboardData.items);
var content = $(this).html();
console.log(content);
valiHTML = _this._opt.validHtml;
content = content.replace(/_moz_dirty=""/gi, "").replace(/\[/g, "[[-").replace(/\]/g, "-]]").replace(/<\/ ?tr[^>]*>/gi, "[br]").replace(/<\/ ?td[^>]*>/gi, " ").replace(/<(ul|dl|ol)[^>]*>/gi, "[br]").replace(/<(li|dd)[^>]*>/gi, "[br]").replace(/<p [^>]*>/gi, "[br]").replace(new RegExp("<(/?(?:" + valiHTML.join("|") + ")[^>]*)>", "gi"), "[$1]").replace(new RegExp('<span([^>]*class="?at"?[^>]*)>', "gi"), "[span$1]").replace(/<[^>]*>/g, "").replace(/\[\[\-/g, "[").replace(/\-\]\]/g, "]").replace(new RegExp("\\[(/?(?:" + valiHTML.join("|") + "|img|span)[^\\]]*)\\]", "gi"), "<$1>");
if (!/firefox/.test(navigator.userAgent.toLowerCase())) {
content = content.replace(/\r?\n/gi, "<br>");
}
$(this).html(content);
});
},
placeholderHandler: function() {
var _this = this;
$(this).on('focus', function() {
if($.trim($(this).html()) === _this._opt.placeholader) {
$(this).html('');
}
})
.on('blur', function() {
if(!$(this).html()) {
$(this).html(_this._opt.placeholader);
}
});
if(!$.trim($(this).html())) {
$(this).html(_this._opt.placeholader);
}
},
getValue: function() {
return $(this).html();
},
setValue: function(str) {
$(this).html(str);
}
});