解决百度富文本编辑器Ueditor自定义上传视频封面

版权声明:遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
原文出处链接:https://blog.csdn.net/m0_37826725/article/details/99094029

原文处作者留了一个不明显的巨坑,所以再此处修改一下。原文处的"onclick" 不要复制

思路就是:选中上传的视频-点击上传封面-调用ueditor内部的图片上传-更改保存的html内容-更改显示的背景图

首先说明是要更改源码的!!!!

我是在ueditor.config.js里面没有xss过滤白名单的情况下完成下面的操作的。这个东西可以不要,可以直接把whitList这个节点注释掉。

全是在ueditor.all.js里面改的源码

第一步:增加添加封面的按钮

搜索popup.formatHtml(str)代码把str赋值的内容替换成下面内容

var coverButton = "";
if (dialogName == "insertvideoDialog") {
    coverButton = "  " +
    '<span onclick=$$._onImgCoverEditButtonClick(\'' +
    dialogName +
    '\') class="edui-clickable"><input id="coverImgUrl" style="display:none;" type="file"  />添加封面' +
    "</span>";
}
str = '<nobr>' + editor.getLang("property") + ': '+
 '<span onclick=$$._onImgSetFloat("none") class="edui-clickable">' + editor.getLang("default") + '</span>&nbsp;&nbsp;' +
   '<span onclick=$$._onImgSetFloat("left") class="edui-clickable">' + editor.getLang("justifyleft") + '</span>&nbsp;&nbsp;' +
   '<span onclick=$$._onImgSetFloat("right") class="edui-clickable">' + editor.getLang("justifyright") + '</span>&nbsp;&nbsp;' +
   '<span onclick=$$._onImgSetFloat("center") class="edui-clickable">' + editor.getLang("justifycenter") + '</span>&nbsp;&nbsp;'+
   '<span onclick="$$._onImgEditButtonClick(\'' + dialogName + '\');" class="edui-clickable">' + editor.getLang("modify") + '</span>'+
   coverButton + '</nobr>';

这样写是因为只是视频会显示添加封面的按钮。

第二步,添加按钮的点击事件,搜索var popup = new baidu.editor.ui.Popup({

在下面方法中增加一个方法_onImgCoverEditButtonClick,就是上面增加添加封面按钮的点击事件的方法。

我会把baidu.editor.ui.Popup这个里面的内容全部贴出来可以全部替换。

var popup = new baidu.editor.ui.Popup({
		editor: editor,
		content: "",
		className: "edui-bubble",
		_onEditButtonClick: function() {
			this.hide();
			editor.ui._dialogs.linkDialog.open();
		},
		_onImgEditButtonClick: function(name) {
			this.hide();
			editor.ui._dialogs[name] && editor.ui._dialogs[name].open();
 
		},
		_onImgCoverEditButtonClick: function (name) {
			//this.hide();
			imgsCover = editor.ui._dialogs[name];
			editor.ready(function () {
				editor.addListener("beforeInsertImage", _coverInsertImage);
			});
			var urlcover = editor.getDialog("insertimage");
			urlcover.title = '视频封面';
			urlcover.render();
			urlcover.open();
			
		},
		_onImgSetFloat: function(value) {
			this.hide();
			editor.execCommand("imagefloat", value);
 
		},
		_setIframeAlign: function(value) {
			var frame = popup.anchorEl;
			var newFrame = frame.cloneNode(true);
			switch (value) {
			case -2:
				newFrame.setAttribute("align", "");
				break;
			case -1:
				newFrame.setAttribute("align", "left");
				break;
			case 1:
				newFrame.setAttribute("align", "right");
				break;
			}
			frame.parentNode.insertBefore(newFrame, frame);
			domUtils.remove(frame);
			popup.anchorEl = newFrame;
			popup.showAnchor(popup.anchorEl);
		},
		_updateIframe: function() {
			var frame = editor._iframe = popup.anchorEl;
			if (domUtils.hasClass(frame, "ueditor_baidumap")) {
				editor.selection.getRange().selectNode(frame).select();
				editor.ui._dialogs.mapDialog.open();
				popup.hide();
			} else {
				editor.ui._dialogs.insertframeDialog.open();
				popup.hide();
			}
		},
		_onRemoveButtonClick: function(cmdName) {
			editor.execCommand(cmdName);
			this.hide();
		},
		queryAutoHide: function(el) {
			if (el && el.ownerDocument == editor.document) {
				if (el.tagName.toLowerCase() == "img" || domUtils.findParentByTagName(el, "a", true)) {
					return el !== popup.anchorEl;
				}
			}
			return baidu.editor.ui.Popup.prototype.queryAutoHide.call(this, el);
		}
	});

在文件开头(function() {下面声明变量存放数据用

 
var imgCover = {};
var imgsCover = {}, posteres;

在if (editor.options.imagePopup)这个if语句下面增加一个处理上传视频封面的方法

// 视频封面上传动作
function _coverInsertImage(t, result) {
	var imageHtml = '';
	for (var i in result) {
		posteres = imageHtml = result[i].src;
	}
	var img = imgsCover.editor.selection.getRange().getClosedNode(), url;
	url = img.getAttribute("_url");
	var align;
	utils.each(imgCover.getNodesByTagName("embed video"),
		function (node) {
			align = node.getStyle("float") || "";
 
		});
	
	editor.execCommand('insertvideo', {
		url: url + "+" + imageHtml,
		width: img.width,
		height: img.height,
		align: align
	}, 'upload');
}

修改creatInsertStr 我贴出这个方法的代码

/**
 * 创建插入视频字符窜
 * @param url 视频地址
 * @param width 视频宽度
 * @param height 视频高度
 * @param align 视频对齐
 * @param toEmbed 是否以flash代替显示
 * @param addParagraph  是否需要添加P 标签
 */
function creatInsertStr(url, width, height, id, align, classname, type, poster) {
	var str;
	switch (type) {
	case "image":
		str = "<img " +
			(id ? 'id="' + id + '"' : "") +
			' width="' +
			width +
			'" height="' +
			height +
			'" _url="' +
			url +
			'" class="' +
			classname.replace(/\bvideo-js\b/, "") +
			'"' +
			' src="' +
			poster +
			'" style="background:url(' +
			poster +
			") no-repeat center center; border:1px solid gray;" +
			(align ? "float:" + align + ";" : "") +
			'" />';
		break;
	case "embed":
		str = '<embed type="application/x-shockwave-flash" class="' +
			classname +
			'" pluginspage="http://www.macromedia.com/go/getflashplayer"' +
			' src="' +
			utils.html(url) +
			'" width="' +
			width +
			'" height="' +
			height +
			'"' +
			(align ? ' style="float:' + align + '"' : "") +
			' wmode="transparent" play="true" loop="false" menu="false" allowscriptaccess="never" allowfullscreen="true" >';
		break;
	case "video":
		var ext = url.substr(url.lastIndexOf(".") + 1);
		if (ext == "ogv") ext = "ogg";
		str = "<video" +
			(id ? ' id="' + id + '"' : "") +
			' class="' +
			classname +
			' video-js" ' +
			(align ? ' style="float:' + align + '"' : "") +
			' controls poster="' +
			poster +
			'" preload="none" width="' +
			width +
			'" height="' +
			height +
			'" src="' +
			url +
			'" data-setup="{}">' +
			'<source src="' +
			url +
			'" type="video/' +
			ext +
			'" /></video>';
		break;
	}
	return str;
}

修改switchImgAndVideo 我贴出这个方法的代码

function switchImgAndVideo(root, img2video) {
	imgCover = root;
	utils.each(root.getNodesByTagName(img2video ? "img" : "embed video"),
		function (node) {
			var className = node.getAttr("class");
			var _urles = node.getAttr("_url");
			posteres = node.attrs.poster;
			if (typeof (node.getAttr("_url")) != "undefined") {
				if (node.getAttr("_url").indexOf("+") != -1) {
					var _urlsposters = node.getAttr("_url").split("+");
					_urles = _urlsposters[0];
					posteres = _urlsposters[1];
				}
			}
			if (img2video) {
				posteres = node.getAttr("src");
			}
			if (className && className.indexOf("edui-faked-video") != -1) {
				var html = creatInsertStr(img2video ? node.getAttr("_url") : node.getAttr("src"),
					node.getAttr("width"),
					node.getAttr("height"),
					null,
					node.getStyle("float") || "",
					className,
					img2video ? "embed" : "image");
				node.parentNode.replaceChild(UE.uNode.createElement(html), node);
			}
			if (className && className.indexOf("edui-upload-video") != -1) {
				var html = creatInsertStr(img2video ? _urles : node.getAttr("src"),
					node.getAttr("width"),
					node.getAttr("height"),
					null,
					node.getStyle("float") || "",
					className,
					img2video ? "video" : "image", posteres);
				node.parentNode.replaceChild(UE.uNode.createElement(html), node);
			}
		});
}

最后在搜索这个me.commands[“insertvideo”] = {下面for循环里面这一句替换一下

html.push(creatInsertStr(vi.url, vi.width || 420, vi.height || 280, id + i, null, cl, "image", posteres));
百度富文本编辑器UEditor是一款基于JavaScript的所见即所得富文本编辑器,可以轻松地将其集成到Java Web项目中,为用户提供更好的编辑体验。下面简单介绍一下在Java Web项目中使用百度富文本编辑器UEditor的方法。 1.下载UEditor 首先,需要从百度UEditor官网上下载UEditor的源代码。下载地址为:http://ueditor.baidu.com/website/download.html 2.将UEditor集成到Java Web项目中 下载完成后,将UEditor的源代码解压缩,并将解压后的文件夹复制到Java Web项目的WebContent目录下的任意一个子目录中(比如说WebContent/ueditor)。然后,将UEditor中的jsp目录下的所有文件复制到Java Web项目的WebContent目录下的任意一个子目录中(比如说WebContent/jsp)。 3.配置UEditor 在Java Web项目的WebContent目录下新建一个名为config.json的文件,该文件的内容如下: { "imageActionName": "uploadimage", "imageFieldName": "upfile", "imageMaxSize": 2048000, "imageAllowFiles": [".png", ".jpg", ".jpeg", ".gif", ".bmp"], "imageCompressEnable": true, "imageCompressBorder": 1600, "imageInsertAlign": "none", "imageUrlPrefix": "", "imagePathFormat": "/ueditor/jsp/upload/image/{yyyy}{mm}{dd}/{time}{rand:6}", "scrawlActionName": "uploadscrawl", "scrawlFieldName": "upfile", "scrawlPathFormat": "/ueditor/jsp/upload/image/{yyyy}{mm}{dd}/{time}{rand:6}", "scrawlMaxSize": 2048000, "scrawlUrlPrefix": "", "scrawlInsertAlign": "none", "snapscreenActionName": "uploadimage", "snapscreenPathFormat": "/ueditor/jsp/upload/image/{yyyy}{mm}{dd}/{time}{rand:6}", "snapscreenUrlPrefix": "", "snapscreenInsertAlign": "none", "catcherLocalDomain": ["127.0.0.1", "localhost", "img.baidu.com"], "catcherActionName": "catchimage", "catcherFieldName": "source", "catcherPathFormat": "/ueditor/jsp/upload/image/{yyyy}{mm}{dd}/{time}{rand:6}", "catcherUrlPrefix": "", "catcherMaxSize": 2048000, "catcherAllowFiles": [".png", ".jpg", ".jpeg", ".gif", ".bmp"], "videoActionName": "uploadvideo", "videoFieldName": "upfile", "videoPathFormat": "/ueditor/jsp/upload/video/{yyyy}{mm}{dd}/{time}{rand:6}", "videoUrlPrefix": "", "videoMaxSize": 102400000, "videoAllowFiles": [ ".flv", ".swf", ".mkv", ".avi", ".rm", ".rmvb", ".mpeg", ".mpg", ".ogg", ".ogv", ".mov", ".wmv", ".mp4", ".webm", ".mp3", ".wav", ".mid" ], "fileActionName": "uploadfile", "fileFieldName": "upfile", "filePathFormat": "/ueditor/jsp/upload/file/{yyyy}{mm}{dd}/{time}{rand:6}", "fileUrlPrefix": "", "fileMaxSize": 51200000, "fileAllowFiles": [ ".png", ".jpg", ".jpeg", ".gif", ".bmp", ".flv", ".swf", ".mkv", ".avi", ".rm", ".rmvb", ".mpeg", ".mpg", ".ogg", ".ogv", ".mov", ".wmv", ".mp4", ".webm", ".mp3", ".wav", ".mid", ".rar", ".zip", ".tar", ".gz", ".7z", ".bz2", ".cab", ".iso", ".doc", ".docx", ".xls", ".xlsx", ".ppt", ".pptx", ".pdf", ".txt", ".md", ".xml" ], "imageManagerActionName": "listimage", "imageManagerListPath": "/ueditor/jsp/upload/image/", "imageManagerListSize": 20, "imageManagerUrlPrefix": "", "imageManagerInsertAlign": "none", "imageManagerAllowFiles": [".png", ".jpg", ".jpeg", ".gif", ".bmp"], "fileManagerActionName": "listfile", "fileManagerListPath": "/ueditor/jsp/upload/file/", "fileManagerUrlPrefix": "", "fileManagerListSize": 20, "fileManagerAllowFiles": [ ".png", ".jpg", ".jpeg", ".gif", ".bmp", ".flv", ".swf", ".mkv", ".avi", ".rm", ".rmvb", ".mpeg", ".mpg", ".ogg", ".ogv", ".mov", ".wmv", ".mp4", ".webm", ".mp3", ".wav", ".mid", ".rar", ".zip", ".tar", ".gz", ".7z", ".bz2", ".cab", ".iso", ".doc", ".docx", ".xls", ".xlsx", ".ppt", ".pptx", ".pdf", ".txt", ".md", ".xml" ] } 该文件是UEditor的配置文件,主要配置了上传文件的相关设置。 4.在JSP页面中使用UEditor 在需要使用UEditor的JSP页面中,添加如下代码: <!-- 加载ueditor编辑器js文件 --> <script type="text/javascript" src="${pageContext.request.contextPath}/ueditor/ueditor.config.js"></script> <script type="text/javascript" src="${pageContext.request.contextPath}/ueditor/ueditor.all.min.js"></script> <!-- 创建编辑器 --> <script type="text/javascript"> var editor = UE.getEditor('editor'); </script> 其中,ueditor.config.js和ueditor.all.min.js是UEditor的核心文件,需要在页面中引入。editor是页面中一个div元素的id,表示要将该div元素转化为UEditor编辑器。 至此,就完成了在Java Web项目中使用百度富文本编辑器UEditor的集成。
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值