完善ice富文本粘贴图片本地化的问题,更新ice版本

This commit is contained in:
HuYang 2020-12-18 17:28:39 +08:00
parent 730013251f
commit c6db8a5118
4 changed files with 78 additions and 48 deletions

View File

@ -23,11 +23,11 @@ def upload_ice_img(request):
# formData.append('upload_type', "files");
##################
try:
up_type = request.POST.get('upload_type')
up_num = request.POST.get('upload_num')
up_type = request.POST.get('upload_type','')
up_num = request.POST.get('upload_num','')
iceEditor_img = str(request.POST.get('iceEditor-img',''))
except:
up_num = -1
pass
if up_type == "files":
# 多文件上传功能需要修改js文件
res_dic = {'length':int(up_num)}
@ -35,11 +35,13 @@ def upload_ice_img(request):
file_obj = request.FILES.get('file_' + str(i))
result = ice_save_file(file_obj,request.user)
res_dic[i] = result
elif iceEditor_img.lower().startswith('http'):
res_dic = ice_url_img_upload(iceEditor_img,request.user)
else:
# 粘贴上传和单文件上传
file_obj = request.FILES.get('file[]')
result = ice_save_file(file_obj,request.user)
res_dic = {0:result,"length":1} #一个文件,直接把文件数量固定了
res_dic = {0:result,"length":1,'other_msg':iceEditor_img} #一个文件,直接把文件数量固定了
return HttpResponse(json.dumps(res_dic), content_type="application/json")
def ice_save_file(file_obj,user):
@ -88,7 +90,35 @@ def ice_save_file(file_obj,user):
return {"error":0, "name": str(file_obj),'url':file_url}
return {"error": "文件存储异常"}
# ice_url图片上传
def ice_url_img_upload(url,user):
relative_path = upload_generation_dir()
name_time = time.strftime("%Y-%m-%d_%H%M%S_")
name_join = ""
name_rand = name_join.join(random.sample('zyxwvutsrqponmlkjihgfedcba',10) )
file_name = name_time + name_rand + '.png' # 日期时间_随机字符串命名
path_file = os.path.join(relative_path, file_name)
path_file = settings.MEDIA_ROOT + path_file
# print('文件路径:', path_file)
file_url = settings.MEDIA_URL + relative_path + file_name
# print("文件URL", file_url)
header = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36"
}
r = requests.get(url, headers=header, stream=True)
if r.status_code == 200:
with open(path_file, 'wb') as f:
f.write(r.content) # 保存文件
Image.objects.create(
user=user,
file_path=file_url,
file_name=file_name,
remark='iceurl粘贴上传',
)
resp_data = {"error":0, "name": file_name,'url':file_url}
return resp_data

View File

@ -6,7 +6,7 @@
* MIT License By www.iceui.net
+ 作者ice
+ 官方www.iceui.net
+ 时间2020-11-20
+ 时间2020-12-17
+------------------------------------------------------------------------------------+
+ 版权声明该版权完全归iceUI官方所有可转载使用和学习但请务必保留版权信息
+------------------------------------------------------------------------------------+
@ -27,6 +27,8 @@
'subscript','createLink','unlink','line','hr','face','table','files','music','video','insertImage',
'removeFormat','paste','line','code'
];
// 不需要的工具栏菜单
this.notMenu=[];
// 文字背景颜色
this.backColor = [
'#ffffff','#000000','#eeece1','#1f497d','#4f81bd','#c0504d','#9bbb59','#8064a2','#4bacc6','#f79646',
@ -855,7 +857,7 @@ ice.editor.prototype.menuHTML=function(){
}
});
//添加视频
this.createMenu({title:'添加视频',name:'video',icon:'video',id:this.videoId,popup:{width:320,height:170,title:'添加视频',content:'<div class="iceEditor-video"><div><label><input name="iceEditor-video" type="radio" checked value="1"/>自定义</label><label><input name="iceEditor-video" type="radio" value="2"/>B站</label><label><input name="iceEditor-video" type="radio" value="3"/>优酷</label></div><div>URL<input type="text" class="iceEditor-videoUrl" placeholder="网络图片地址" value=""/></div><div><label>宽:</label><input type="text" class="iceEditor-inputWidth" placeholder="px" value=""/><label>高:</label><input type="text" class="iceEditor-inputHeight" placeholder="px" value=""/><a href="javascript:;" class="iceEditor-btn">确定</a></div></div>'},
this.createMenu({title:'添加视频',name:'video',icon:'video',id:this.videoId,popup:{width:320,height:170,title:'添加视频',content:'<div class="iceEditor-video"><div><label>默认支持bilibili、优酷</label></div><div>URL<input type="text" class="iceEditor-videoUrl" placeholder="网络图片地址" value=""/></div><div><label>宽:</label><input type="text" class="iceEditor-inputWidth" placeholder="px" value=""/><label>高:</label><input type="text" class="iceEditor-inputHeight" placeholder="px" value=""/><a href="javascript:;" class="iceEditor-btn">确定</a></div></div>'},
success:function(e,z){
z.video = z.id(z.videoId);
var type;
@ -866,40 +868,24 @@ ice.editor.prototype.menuHTML=function(){
var btn = z.video.getElementsByClassName('iceEditor-btn')[0];
btn.onclick=function(){
if(!url.value.length) return alert('视频地址不能为空');
var obj = z.video.getElementsByTagName('input');
//获取单选按钮的值
for(var i=0;i<obj.length;i++) {
if (obj[i].checked) type = Number(obj[i].value);
}
if(type === 1){ //自定义
var v = z.c('video');
v.src=url.value;
v.width=width.value.length?width.value:510;
v.height=height.value.length?height.value:498;
v.controls='controls';
}else{
var v = z.c('iframe');
v.width=width.value.length?width.value:510;
v.height=height.value.length?height.value:498;
v.setAttribute('frameborder',0);
v.setAttribute('allowfullscreen',true);
var error = '抱歉,无法处理该链接!';
if(type === 2){ //b站
//源地址https://www.bilibili.com/video/BV1xk4y1R7Vd?spm_id_from=333.851.b_7265706f7274466972737431.7
//处理地址https://player.bilibili.com/player.html?bvid=BV1xk4y1R7Vd
var id = url.value.split('?');
if(id.length>1){
id = id[0].split('video/');
if(id.length>1 && id[1].length){
v.src='https://player.bilibili.com/player.html?bvid='+id[1];
}else{
return alert('b站'+error);
}
}else{
return alert('b站'+error);
var v = z.c('iframe');
v.width=width.value.length?width.value:510;
v.height=height.value.length?height.value:498;
v.setAttribute('frameborder',0);
v.setAttribute('allowfullscreen',true);
var error = '抱歉,无法处理该链接!';
var domain = /^http(s)?:\/\/(.*?)\//.exec(url.value) // 正则出域名
if (domain.length == 3) {
if (domain[2] == 'www.bilibili.com' || domain[2] == 'bilibili.com' ) { // bilibili
//源地址https://www.bilibili.com/video/BV1UZ4y1g7De?spm_id_from=333.851.b_7265706f7274466972737431.11
//处理地址https://player.bilibili.com/player.html?bvid=BV1UZ4y1g7De
id = /video\/(.*?)\?/g.exec(url.value)
if (!id) {
id = /video\/(.*)/g.exec(url.value)
}
}else if(type === 3){ //优酷
//源地址https://v.youku.com/v_show/id_XMjM0ODA3NjIw.html
v.src='https://player.bilibili.com/player.html?bvid='+id[1];
} else if (domain[2] == 'v.youku.com') { // youku
//源地址https://v.youku.com/v_show/id_XNTAwOTI4MzUxNg==.html
//处理地址https://player.youku.com/embed/XMjM0ODA3NjIw
var id = url.value.split('.html');
if(id.length>1){
@ -909,10 +895,21 @@ ice.editor.prototype.menuHTML=function(){
}else{
return alert('优酷:'+error);
}
}else{
} else {
return alert('优酷:'+error);
}
} else if (domain[2] == 'www.ixigua.com') { //西瓜视频
id = /[0-9]{6,}/g.exec(url.value)
v.src='https://www.ixigua.com/iframe/'+id[0];
} else {
v = z.c('video');
v.src=url.value;
v.width=width.value.length?width.value:510;
v.height=height.value.length?height.value:498;
v.controls='controls';
}
} else {
return alert('URL地址不合法');
}
z.setHTML(v,true);
close.style.display='none';
@ -943,9 +940,11 @@ ice.editor.prototype.menuFormat=function() {
ul.appendChild(line);
continue;
}
ul.appendChild(this.menuList[this.menu[i]]);
if(this.menuList[this.menu[i]].success){
this.menuList[this.menu[i]].success(this.menuList[this.menu[i]],_z);
if (this.notMenu.indexOf(this.menu[i]) == -1) {
ul.appendChild(this.menuList[this.menu[i]]);
if(this.menuList[this.menu[i]].success){
this.menuList[this.menu[i]].success(this.menuList[this.menu[i]],_z);
}
}
}
if(this.maxWindow){

File diff suppressed because one or more lines are too long

View File

@ -94,6 +94,7 @@
//'files', //ice内置多文件上传,需要配合修改js才能用
]
editor.uploadUrl="{% url 'upload_ice_img' %}"; //粘贴上传请求路径
editor.imgAutoUpload=true;
editor.create();
autoCacheDoc();
</script>