bootstrap.js
9.5 KB
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
require.config({
paths: {
'nkeditor': '../addons/nkeditor/js/customplugin',
'nkeditor-core': '../addons/nkeditor/nkeditor',
'nkeditor-lang': '../addons/nkeditor/lang/zh-CN',
},
shim: {
'nkeditor': {
deps: [
'nkeditor-core',
'nkeditor-lang'
]
},
'nkeditor-core': {
deps: [
'css!../addons/nkeditor/themes/black/editor.min.css',
'css!../addons/nkeditor/css/common.css'
],
exports: 'window.KindEditor'
},
'nkeditor-lang': {
deps: [
'nkeditor-core'
]
}
}
});
require(['form'], function (Form) {
var _bindevent = Form.events.bindevent;
Form.events.bindevent = function (form) {
_bindevent.apply(this, [form]);
if ($(Config.nkeditor.classname || '.editor', form).size() > 0) {
require(['nkeditor', 'upload'], function (Nkeditor, Upload) {
var getFileFromBase64, uploadFiles;
uploadFiles = async function (files) {
var self = this;
for (var i = 0; i < files.length; i++) {
try {
await new Promise((resolve) => {
var url, html, file;
file = files[i];
Upload.api.send(file, function (data) {
url = Fast.api.cdnurl(data.url, true);
if (file.type.indexOf("image") !== -1) {
self.exec("insertimage", url);
} else {
html = '<a class="ke-insertfile" href="' + url + '" data-ke-src="' + url + '" target="_blank">' + (file.name || url) + '</a>';
self.exec("inserthtml", html);
}
resolve();
}, function () {
resolve();
});
});
} catch (e) {
}
}
};
getFileFromBase64 = function (data, url) {
var arr = data.split(','), mime = arr[0].match(/:(.*?);/)[1],
bstr = atob(arr[1]), n = bstr.length, u8arr = new Uint8Array(n);
while (n--) {
u8arr[n] = bstr.charCodeAt(n);
}
var filename, suffix;
if (typeof url != 'undefined') {
var urlArr = url.split('.');
filename = url.substr(url.lastIndexOf('/') + 1);
suffix = urlArr.pop();
} else {
filename = Math.random().toString(36).substring(5, 15);
}
if (!suffix) {
suffix = data.substring("data:image/".length, data.indexOf(";base64"));
}
var exp = new RegExp("\\." + suffix + "$", "i");
filename = exp.test(filename) ? filename : filename + "." + suffix;
var file = new File([u8arr], filename, {type: mime});
return file;
};
//上传Word图片
Nkeditor.uploadwordimage = function (index, image) {
var that = this;
(function () {
var file = getFileFromBase64(image);
var placeholder = new RegExp("##" + index + "##", "g");
Upload.api.send(file, function (data) {
that.html(that.html().replace(placeholder, Fast.api.cdnurl(data.url)));
}, function (data) {
that.html(that.html().replace(placeholder, ""));
});
}(index, image));
};
$(Config.nkeditor.classname || '.editor', form).each(function () {
var that = this;
var options = $(this).data("nkeditor-options");
var editor = Nkeditor.create(that, $.extend({}, {
width: '100%',
filterMode: false,
wellFormatMode: false,
allowMediaUpload: true, //是否允许媒体上传
allowFileManager: true,
allowImageUpload: true,
fontSizeTable: ['9px', '10px', '12px', '14px', '16px', '18px', '21px', '24px', '32px'],
wordImageServer: typeof Config.nkeditor != 'undefined' && Config.nkeditor.wordimageserver ? "127.0.0.1:10101" : "", //word图片替换服务器的IP和端口
formulaPreviewUrl: typeof Config.nkeditor != 'undefined' && Config.nkeditor.formulapreviewurl ? Config.nkeditor.formulapreviewurl : "", //数学公式的预览地址
cssPath: Config.site.cdnurl + '/assets/addons/nkeditor/plugins/code/prism.css',
cssData: "body {font-size: 13px}",
fillDescAfterUploadImage: false, //是否在上传后继续添加描述信息
themeType: typeof Config.nkeditor != 'undefined' ? Config.nkeditor.theme : 'black', //编辑器皮肤,这个值从后台获取
fileManagerJson: Fast.api.fixurl("/addons/nkeditor/index/attachment/module/" + Config.modulename),
items: [
'source', 'undo', 'redo', 'preview', 'print', 'template', 'code', 'quote', 'cut', 'copy', 'paste',
'plainpaste', 'wordpaste', 'justifyleft', 'justifycenter', 'justifyright',
'justifyfull', 'insertorderedlist', 'insertunorderedlist', 'indent', 'outdent', 'subscript',
'superscript', 'clearhtml', 'quickformat', 'selectall',
'formatblock', 'fontname', 'fontsize', 'forecolor', 'hilitecolor', 'bold',
'italic', 'underline', 'strikethrough', 'lineheight', 'removeformat', 'image', 'multiimage', 'graft',
'media', 'insertfile', 'table', 'hr', 'emoticons', 'baidumap', 'pagebreak',
'anchor', 'link', 'unlink', 'remoteimage', 'search', 'math', 'about', 'fullscreen'
],
afterCreate: function () {
var self = this;
//Ctrl+回车提交
Nkeditor.ctrl(document, 13, function () {
self.sync();
$(that).closest("form").submit();
});
Nkeditor.ctrl(self.edit.doc, 13, function () {
self.sync();
$(that).closest("form").submit();
});
//粘贴上传
$("body", self.edit.doc).bind('paste', function (event) {
var originalEvent;
originalEvent = event.originalEvent;
if (originalEvent.clipboardData && originalEvent.clipboardData.files.length > 0) {
uploadFiles.call(self, originalEvent.clipboardData.files);
return false;
}
});
//拖拽上传
$("body", self.edit.doc).bind('drop', function (event) {
var originalEvent;
originalEvent = event.originalEvent;
if (originalEvent.dataTransfer && originalEvent.dataTransfer.files.length > 0) {
uploadFiles.call(self, originalEvent.dataTransfer.files);
return false;
}
});
},
afterChange: function () {
$(this.srcElement[0]).trigger("change");
},
//自定义处理
beforeUpload: function (callback, file) {
var file = file ? file : $("input.ke-upload-file", this.form).prop('files')[0];
Upload.api.send(file, function (data) {
var data = {code: '000', data: {url: Fast.api.cdnurl(data.url, true)}, title: '', width: '', height: '', border: '', align: ''};
callback(data);
});
},
//错误处理 handler
errorMsgHandler: function (message, type) {
try {
console.log(message, type);
} catch (Error) {
alert(message);
}
},
uploadFiles: uploadFiles
}, options || {}));
$(this).data("nkeditor", editor);
});
});
}
}
});