kindeditor web文本编辑器加入video标签

修改思路 :在已有的media标签扩展基础上继续扩展

关键修改

1 增加扩展插件plugins\video  目录结构跟media(可以复制media的整个目录),然后修改media.js为video.js,然后修改video.js里面相关的内容,具体细节参考开源项目 kindeditor-video

2 适配文件格式并添加video标签

  在原来增加medie标签相关位置增加 video标签的判断,代码片段如下:


function _mediaType(src) {
	if (/\.(rm|rmvb)(\?|$)/i.test(src)) {
		return 'audio/x-pn-realaudio-plugin';
	}
	if (/\.(mp3)(\?|$)/i.test(src)) {
		return 'audio/mpeg';
	}
	if (/\.(mp4)(\?|$)/i.test(src)) {
		return '"video/mp4';
	}
	if (/\.(swf|flv)(\?|$)/i.test(src)) {
		return 'application/x-shockwave-flash';
	}
	return 'video/x-ms-asf-plugin';
}

function _mediaClass(type) {
	if (/realaudio/i.test(type)) {
		return 'ke-rm';
	}
	if (/flash/i.test(type)) {
		return 'ke-flash';
	}
	if (/audio|mp4/i.test(type)) {
		return 'ke-video';
	}
	return 'ke-media';
}
function _mediaAttrs(srcTag) {
	return _getAttrList(unescape(srcTag));
}

function _videoEmbed(attrs) {
	var html = '<video controls="controls"  ';
	_each(attrs, function(key, val) {
		html += key + '="' + val + '" ';
	});
	html += '/>';
	return html;
}

function _mediaEmbed(attrs) {
	var html = '<embed ';
	_each(attrs, function(key, val) {
		html += key + '="' + val + '" ';
	});
	html += '/>';
	return html;
}
function _mediaImg(blankPath, attrs) {
	var width = attrs.width,
		height = attrs.height,
		type = attrs.type || _mediaType(attrs.src),
		style = '';
	
	
	if(type=="audio/mpeg" || type=="video/mp4"){
		srcTag = _videoEmbed(attrs);
	}else{
		srcTag = _mediaEmbed(attrs);
	}
	if (/\D/.test(width)) {
		style += 'width:' + width + ';';
	} else if (width > 0) {
		style += 'width:' + width + 'px;';
	}
	if (/\D/.test(height)) {
		style += 'height:' + height + ';';
	} else if (height > 0) {
		style += 'height:' + height + 'px;';
	}
	var html = '<img class="' + _mediaClass(type) + '" src="' + blankPath + '" ';
	if (style !== '') {
		html += 'style="' + style + '" ';
	}
	html += 'data-ke-tag="' + escape(srcTag) + '" alt="" />';
	return html;
}

    3 查看源代码的地方增加查看video标签的功能,代码片段如下:

       查看源码事件

self.beforeGetHtml(function(html) {
		if (_IE && _V <= 8) {
			html = html.replace(/<div\s+[^>]*data-ke-input-tag="([^"]*)"[^>]*>([\s\S]*?)<\/div>/ig, function(full, tag) {
				return unescape(tag);
			});
			html = html.replace(/(<input)((?:\s+[^>]*)?>)/ig, function($0, $1, $2) {
				if (!/\s+type="[^"]+"/i.test($0)) {
					return $1 + ' type="text"' + $2;
				}
				return $0;
			});
		}
		return html.replace(/(<(?:noscript|noscript\s[^>]*)>)([\s\S]*?)(<\/noscript>)/ig, function($0, $1, $2, $3) {
			return $1 + _unescape($2).replace(/\s+/g, ' ') + $3;
		})
		.replace(/<img[^>]*class="?ke-(flash|rm|media)"?[^>]*>/ig, function(full) {
			var imgAttrs = _getAttrList(full);
			var styles = _getCssList(imgAttrs.style || '');
			var attrs = _mediaAttrs(imgAttrs['data-ke-tag']);
			var width = _undef(styles.width, '');
			var height = _undef(styles.height, '');
			if (/px/i.test(width)) {
				width = _removeUnit(width);
			}
			if (/px/i.test(height)) {
				height = _removeUnit(height);
			}
			attrs.width = _undef(imgAttrs.width, width);
			attrs.height = _undef(imgAttrs.height, height);
			
			return _mediaEmbed(attrs);
		})
		.replace(/<img[^>]*class="?ke-video"?[^>]*>/ig, function(full) {
			var imgAttrs = _getAttrList(full);
			var styles = _getCssList(imgAttrs.style || '');
			var attrs = _mediaAttrs(imgAttrs['data-ke-tag']);
			var width = _undef(styles.width, '');
			var height = _undef(styles.height, '');
			if (/px/i.test(width)) {
				width = _removeUnit(width);
			}
			if (/px/i.test(height)) {
				height = _removeUnit(height);
			}
			attrs.width = _undef(imgAttrs.width, width);
			attrs.height = _undef(imgAttrs.height, height);
			
			return _videoEmbed(attrs);
		})
		.replace(/<img[^>]*class="?ke-anchor"?[^>]*>/ig, function(full) {
			var imgAttrs = _getAttrList(full);
			return '<a name="' + unescape(imgAttrs['data-ke-name']) + '"></a>';
		})
		.replace(/<div\s+[^>]*data-ke-script-attr="([^"]*)"[^>]*>([\s\S]*?)<\/div>/ig, function(full, attr, code) {
			return '<script' + unescape(attr) + '>' + unescape(code) + '</script>';
		})
		.replace(/<div\s+[^>]*data-ke-noscript-attr="([^"]*)"[^>]*>([\s\S]*?)<\/div>/ig, function(full, attr, code) {
			return '<noscript' + unescape(attr) + '>' + unescape(code) + '</noscript>';
		})
		.replace(/(<[^>]*)data-ke-src="([^"]*)"([^>]*>)/ig, function(full, start, src, end) {
			full = full.replace(/(\s+(?:href|src)=")[^"]*(")/i, function($0, $1, $2) {
				return $1 + _unescape(src) + $2;
			});
			full = full.replace(/\s+data-ke-src="[^"]*"/i, '');
			return full;
		})
		.replace(/(<[^>]+\s)data-ke-(on\w+="[^"]*"[^>]*>)/ig, function(full, start, end) {
			return start + end;
		});
	});

   源码到html代码的切换事件:

self.beforeSetHtml(function(html) {
		if (_IE && _V <= 8) {
			html = html.replace(/<input[^>]*>|<(select|button)[^>]*>[\s\S]*?<\/\1>/ig, function(full) {
				var attrs = _getAttrList(full);
				var styles = _getCssList(attrs.style || '');
				if (styles.display == 'none') {
					return '<div class="ke-display-none" data-ke-input-tag="' + escape(full) + '"></div>';
				}
				return full;
			});
		}
		return html.replace(/<embed[^>]*type="([^"]+)"[^>]*>(?:<\/embed>)?/ig, function(full) {
			var attrs = _getAttrList(full);
			attrs.src = _undef(attrs.src, '');
			attrs.width = _undef(attrs.width, 0);
			attrs.height = _undef(attrs.height, 0);
			return _mediaImg(self.themesPath + 'common/blank.gif', attrs);
		})
		.replace(/<video[^>]*[^>]*>(?:<\/video>)?/ig, function(full) {
			var attrs = _getAttrList(full);
			attrs.src = _undef(attrs.src, '');
			attrs.width = _undef(attrs.width, 0);
			attrs.height = _undef(attrs.height, 0);
			return _mediaImg(self.themesPath + 'common/blank.gif', attrs);
		})
		
		.replace(/<a[^>]*name="([^"]+)"[^>]*>(?:<\/a>)?/ig, function(full) {
			var attrs = _getAttrList(full);
			if (attrs.href !== undefined) {
				return full;
			}
			return '<img class="ke-anchor" src="' + self.themesPath + 'common/anchor.gif" data-ke-name="' + escape(attrs.name) + '" />';
		})
		.replace(/<script([^>]*)>([\s\S]*?)<\/script>/ig, function(full, attr, code) {
			return '<div class="ke-script" data-ke-script-attr="' + escape(attr) + '">' + escape(code) + '</div>';
		})
		.replace(/<noscript([^>]*)>([\s\S]*?)<\/noscript>/ig, function(full, attr, code) {
			return '<div class="ke-noscript" data-ke-noscript-attr="' + escape(attr) + '">' + escape(code) + '</div>';
		})
		.replace(/(<[^>]*)(href|src)="([^"]*)"([^>]*>)/ig, function(full, start, key, src, end) {
			if (full.match(/\sdata-ke-src="[^"]*"/i)) {
				return full;
			}
			full = start + key + '="' + src + '"' + ' data-ke-src="' + _escape(src) + '"' + end;
			return full;
		})
		.replace(/(<[^>]+\s)(on\w+="[^"]*"[^>]*>)/ig, function(full, start, end) {
			return start + 'data-ke-' + end;
		})
		.replace(/<table[^>]*\s+border="0"[^>]*>/ig, function(full) {
			if (full.indexOf('ke-zeroborder') >= 0) {
				return full;
			}
			return _addClassToTag(full, 'ke-zeroborder');
		});
	});

 

4  然后就是添加video标签的相关css样式(样式文件位置:themes\default\default.css)

.ke-icon-video {
	background:url("../common/video_16.png") no-repeat;
    width: 16px;
    height: 16px;
}

 

完整的代码请查看开源项目 kindeditor-video

转载于:https://my.oschina.net/zb0423/blog/1511602

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
首先,我们需要在 Spring Boot 项目中引入 kindeditor 的相关依赖。可以通过在 Maven 或 Gradle 配置文件中添加以下依赖来实现: Maven: ```xml <dependency> <groupId>com.github.kindeditor</groupId> <artifactId>kindeditor</artifactId> <version>4.1.11</version> </dependency> ``` Gradle: ```groovy compile group: 'com.github.kindeditor', name: 'kindeditor', version: '4.1.11' ``` 接着,在 Spring Boot 项目中添加一个控制器,用于处理 kindeditor 的上传请求和获取文件列表请求。示例代码如下: ```java @RestController @RequestMapping("/kindeditor") public class KindEditorController { @Autowired private Environment env; /** * KindEditor上传文件接口 * @param file * @param request * @return */ @PostMapping("/upload") public Map<String, Object> upload(@RequestParam("imgFile") MultipartFile file, HttpServletRequest request) { Map<String, Object> result = new HashMap<>(); String url = ""; try { // 获取上传文件目录 String uploadDir = env.getProperty("kindeditor.upload-dir"); if (StringUtils.isBlank(uploadDir)) { uploadDir = request.getServletContext().getRealPath("/upload"); } File dir = new File(uploadDir); if (!dir.exists()) { dir.mkdirs(); } // 获取上传文件名 String fileName = file.getOriginalFilename(); // 生成新的文件名 String newFileName = UUID.randomUUID().toString() + "." + StringUtils.substringAfterLast(fileName, "."); // 保存文件 File dest = new File(dir, newFileName); file.transferTo(dest); // 返回文件访问URL url = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath() + "/upload/" + newFileName; result.put("error", 0); result.put("url", url); } catch (Exception e) { result.put("error", 1); result.put("message", "文件上传失败"); } return result; } /** * KindEditor获取文件列表接口 * @param request * @return */ @GetMapping("/filemanager") public Map<String, Object> fileManager(HttpServletRequest request) { Map<String, Object> result = new HashMap<>(); try { // 获取上传文件目录 String uploadDir = env.getProperty("kindeditor.upload-dir"); if (StringUtils.isBlank(uploadDir)) { uploadDir = request.getServletContext().getRealPath("/upload"); } File dir = new File(uploadDir); if (!dir.exists()) { dir.mkdirs(); } // 遍历目录,获取文件列表 List<Map<String, Object>> fileList = new ArrayList<>(); File[] files = dir.listFiles(); if (files != null && files.length > 0) { for (File file : files) { Map<String, Object> fileInfo = new HashMap<>(); fileInfo.put("is_dir", file.isDirectory()); fileInfo.put("has_file", file.isFile()); fileInfo.put("filesize", file.length()); fileInfo.put("filename", file.getName()); fileInfo.put("datetime", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(file.lastModified()))); fileList.add(fileInfo); } } result.put("error", 0); result.put("file_list", fileList); } catch (Exception e) { result.put("error", 1); result.put("message", "文件列表获取失败"); } return result; } } ``` 其中,`upload` 方法用于处理 kindeditor 的上传请求,`filemanager` 方法用于获取文件列表请求。在 `upload` 方法中,我们通过 `MultipartFile` 接口获取上传的文件信息,然后将文件保存至指定目录,并返回文件访问 URL。在 `filemanager` 方法中,我们遍历指定目录下的所有文件,并将文件信息封装成列表返回。 最后,在前端页面中引入 kindeditor 的相关资源文件,并在页面中添加文本编辑器元素。示例代码如下: ```html <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>KindEditor文本编辑器</title> <link rel="stylesheet" href="http://cdn.staticfile.org/kindeditor/4.1.11/themes/default/default.css" /> <script src="http://cdn.staticfile.org/kindeditor/4.1.11/kindeditor.js"></script> <script src="http://cdn.staticfile.org/kindeditor/4.1.11/lang/zh-CN.js"></script> </head> <body> <textarea id="editor"></textarea> <script> KindEditor.ready(function(K) { K.create('#editor', { uploadJson: '/kindeditor/upload', fileManagerJson: '/kindeditor/filemanager' }); }); </script> </body> </html> ``` 在以上代码中,我们引入了 kindeditor 的相关资源文件,并在页面中添加了一个 ID 为 `editor` 的 textarea 元素,然后通过 JavaScript 代码初始化了 kindeditor,并设置了上传文件和获取文件列表的 URL。 至此,我们就可以在 Spring Boot 项目中轻松实现 kindeditor 的富文本编辑器功能了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值