define(['nkeditor-core'], function (Nkeditor) { Nkeditor.lang({ remoteimage: '下载远程图片', search: '查找替换', math: '插入公式', }); var getImageFromUrl = function (url, callback) { var req = new XMLHttpRequest(); req.open("GET", Fast.api.fixurl("/addons/nkeditor/index/download") + "?url=" + encodeURIComponent(url), true); req.responseType = "blob"; req.onload = function (event) { var file; if (req.status >= 200 && req.status < 300 || req.status == 304) { var blob = req.response; var mimetype = blob.type || "image/png"; var mimetypeArr = mimetype.split("/"); if (mimetypeArr[0].toLowerCase() === 'image') { console.log(mimetypeArr, mimetype); var suffix = ['jpeg', 'jpg', 'bmp', 'gif', 'png', 'webp', 'svg+xml'].indexOf(mimetypeArr[1]) > -1 ? mimetypeArr[1] : 'png'; suffix = suffix === 'svg+xml' ? 'svg' : suffix; var filename = Math.random().toString(36).substring(5, 15) + "." + suffix; file = new File([blob], filename, {type: mimetype}); } } callback.call(this, file); }; req.send(); return; }; Nkeditor.plugin('multiimage', function (K) { var self = this, name = 'multiimage', lang = self.lang(name + '.'), allowImages = K.undef(self.allowImages, false); var click = function () { var html = [ '
" + i + "
", j));
});
Toastr.info("成功:" + completed + " 失败:" + failured);
}
};
html.replace(/([\s\S]*?)<\/code>/g, function (code) {
staging[index] = code;
return "" + index + "
";
}
);
html = html.replace(/
/g, function () {
images++;
var url = arguments[3];
var placeholder = '
';
//如果是云存储的链接或本地的链接,则忽略
if ((Config.upload.cdnurl && url.indexOf(Config.upload.cdnurl) > -1) || url.indexOf(location.origin) > -1) {
completed++;
return arguments[0];
} else {
orgined[index] = arguments[0];
}
var attributes = arguments[1] + " " + arguments[8];
attributes = attributes.replace(/'/g, '"').replace(/\sdata\-(src|original|actualsrc|ke\-src)\s*=\s*('|")(.*?)('|")/g, '');
//下载远程图片
(function (index, url, placeholder, attributes) {
placeholder = new RegExp('\
', 'g');
getImageFromUrl(url, function (file) {
if (!file) {
failured++;
that.html(that.html().replace(placeholder, orgined[index]));
checkrestore();
} else {
Upload.api.send(file, function (data) {
completed++;
var replaceValue = '
';
that.html(that.html().replace(placeholder, replaceValue));
checkrestore();
}, function (data) {
failured++;
that.html(that.html().replace(placeholder, orgined[index]));
checkrestore();
});
}
});
})(index, url, placeholder, attributes);
index++;
return placeholder;
});
if (index > 0) {
that.html(html);
} else {
Toastr.info("没有需要下载的远程图片");
}
}
};
// 点击图标时执行
editor.clickToolbar(name, editor.plugin.remoteimage.download);
});
//查找替换
Nkeditor.plugin('search', function (K) {
var self = this, name = 'search', lang = self.lang(name + '.');
var click = function () {
var html = [
'',
'',
'',
'',
''
].join('');
var dialog = self.createDialog({
name: name,
width: Math.min(document.body.clientWidth, 450),
height: 260,
title: self.lang(name),
body: html,
yesBtn: {
name: self.lang('yes'),
click: function (e) {
var search = $("input[name=search]", self.bodyDiv).val();
var replace = $("input[name=replace]", self.bodyDiv).val();
if (search === '') {
Layer.msg("查找的字符不能为空");
return false;
}
if (search === replace) {
Layer.msg("查找的字符不能等于替换的字符");
return false;
}
var html = self.html();
if (html === '') {
Layer.msg("暂无可替换的文本");
}
var searchExp = new RegExp("(" + search + ")(?!([^<]+)?>)", "gi");
var matches = html.match(searchExp);
if (matches && matches.length > 0) {
self._firstAddBookmark = true;
self.addBookmark(false);
self.html(html.replace(searchExp, replace));
Toastr.success("共完成" + matches.length + "处文本替换");
} else {
Layer.msg("暂未找到可替换的文本");
}
self.hideDialog().focus();
}
},
noBtn: {
name: self.lang('no'),
click: function (e) {
self.hideDialog().focus();
}
}
}),
div = dialog.div;
};
self.clickToolbar(name, click);
});
return Nkeditor;
});