From 90e4d2318ad091035e47a78946e347e51ad79c8a Mon Sep 17 00:00:00 2001
From: zicla
- 1.私有文件演示:
-
- 2.公有文件演示:
- ' + title + ' ' + $keydownDivElem.html() + ' ' + $targetElem.html() + ' 标签
- if (pasteHtml) {
- resultHtml = ' ' + pasteHtml + '
-
-
-
-
-
- NbTank演示:
-
-
' 判断,其他用 !txtHtml 判断
- if (!txtHtml || txtHtml === '
') {
- // 内容空了
- $p = $('
'); - // 拼接 - resultHtml = '
' + pasteHtml + '
'; - - // 查询链接 - resultHtml = resultHtml.replace(/(https?:\/\/.*?)<\/p>/ig, function (match, link) { - return '
'; - }); - } - } - - } else if (ieData && ieData.getData) { - // IE 直接从剪切板中取出纯文本格式 - resultHtml = ieData.getData('text'); - if (!resultHtml) { - return; - } - // 拼接为标签 - resultHtml = '
' + resultHtml + '
'; - resultHtml = resultHtml.replace(new RegExp('\n', 'g'), ''); - } else { - // 其他情况 - return; - } - } - - // 执行命令 - if (resultHtml) { - editor.command(e, 'insertHtml', resultHtml); - - // 删除内容为空的 p 和嵌套的 p - self.clearEmptyOrNestP(); - } - }); - - // 处理粘贴的内容 - function handle(elem) { - if (!elem || !elem.nodeType || !elem.nodeName) { - return; - } - let $elem; - let nodeName = elem.nodeName.toLowerCase(); - let nodeType = elem.nodeType; - let childNodesClone; - - // 只处理文本和普通node标签 - if (nodeType !== 3 && nodeType !== 1) { - return; - } - - $elem = $(elem); - - // 如果是容器,则继续深度遍历 - if (nodeName === 'div') { - childNodesClone = []; - $.each(elem.childNodes, function (index, item) { - // elem.childNodes 可获取TEXT节点,而 $elem.children() 就获取不到 - // 先将 elem.childNodes 拷贝一份,一面在循环递归过程中 elem 发生变化 - childNodesClone.push(item); - }); - // 遍历子元素,执行操作 - $.each(childNodesClone, function () { - handle(this); - }); - return; - } - - if (legalTagArr.indexOf(nodeName) >= 0) { - // 如果是合法标签之内的,则根据元素类型,获取值 - resultHtml += getResult(elem); - } else if (nodeType === 3) { - // 如果是文本,则直接插入 p 标签 - resultHtml += '
' + elem.textContent + '
'; - } else if (nodeName === 'br') { - //- Txt.fn.insertEmptyP = function () { - let $txt = this.$txt; - let $children = $txt.children(); - - if ($children.length === 0) { - $txt.append($('
'); - }); - - // 处理文字 - for (i = 0; i < childrenLength; i++) { - childNode = childNodes[i]; - if (childNode.nodeType === 3 && childNode.textContent && $.trim(childNode.textContent)) { - $(childNode).wrap('
'); - } - } - }; - - // 清空内容为空的
,以及重复包裹的
(在windows下的chrome粘贴文字之后,会出现上述情况) - Txt.fn.clearEmptyOrNestP = function () { - let $txt = this.$txt; - let $pList = $txt.find('p'); - - $pList.each(function () { - let $p = $(this); - let $children = $p.children(); - let childrenLength = $children.length; - let $firstChild; - let content = $.trim($p.html()); - - // 内容为空的p - if (!content) { - $p.remove(); - return; - } - - // 嵌套的p - if (childrenLength === 1) { - $firstChild = $children.first(); - if ($firstChild.get(0) && $firstChild.get(0).nodeName === 'P') { - $p.html($firstChild.html()); - } - } - }); - }; - - // 获取 scrollTop - Txt.fn.scrollTop = function (val) { - let self = this; - let editor = self.editor; - let $txt = self.$txt; - - if (editor.useMaxHeight) { - return $txt.parent().scrollTop(val); - } else { - return $txt.scrollTop(val); - } - }; - - // 鼠标hover时候,显示p、head的高度 - Txt.fn.showHeightOnHover = function () { - let editor = this.editor; - let $editorContainer = editor.$editorContainer; - let menuContainer = editor.menuContainer; - let $txt = this.$txt; - let $tip = $(''); - let isTipInTxt = false; - - function addAndShowTip($target) { - if (!isTipInTxt) { - $editorContainer.append($tip); - isTipInTxt = true; - } - - let txtTop = $txt.position().top; - let txtHeight = $txt.outerHeight(); - - let height = $target.height(); - let top = $target.position().top; - let marginTop = parseInt($target.css('margin-top'), 10); - let paddingTop = parseInt($target.css('padding-top'), 10); - let marginBottom = parseInt($target.css('margin-bottom'), 10); - let paddingBottom = parseInt($target.css('padding-bottom'), 10); - - // 计算初步的结果 - let resultHeight = height + paddingTop + marginTop + paddingBottom + marginBottom; - let resultTop = top + menuContainer.height(); - - // let spaceValue; - - // // 判断是否超出下边界 - // spaceValue = (resultTop + resultHeight) - (txtTop + txtHeight); - // if (spaceValue > 0) { - // resultHeight = resultHeight - spaceValue; - // } - - // // 判断是否超出了下边界 - // spaceValue = txtTop > resultTop; - // if (spaceValue) { - // resultHeight = resultHeight - spaceValue; - // top = top + spaceValue; - // } - - // 按照最终结果渲染 - $tip.css({ - height: height + paddingTop + marginTop + paddingBottom + marginBottom, - top: top + menuContainer.height() - }); - } - - function removeTip() { - if (!isTipInTxt) { - return; - } - $tip.remove(); - isTipInTxt = false; - } - - $txt.on('mouseenter', 'ul,ol,blockquote,p,h1,h2,h3,h4,h5,table,pre', function (e) { - addAndShowTip($(e.currentTarget)); - }).on('mouseleave', function () { - removeTip(); - }); - }; - - }); -// 工具函数 - _e(function (E, $) { - - // IE8 [].indexOf() - if (!Array.prototype.indexOf) { - //IE低版本不支持 arr.indexOf - Array.prototype.indexOf = function (elem) { - let i = 0, - length = this.length; - for (; i < length; i++) { - if (this[i] === elem) { - return i; - } - } - return -1; - }; - //IE低版本不支持 arr.lastIndexOf - Array.prototype.lastIndexOf = function (elem) { - let length = this.length; - for (length = length - 1; length >= 0; length--) { - if (this[length] === elem) { - return length; - } - } - return -1; - }; - } - - // IE8 Date.now() - if (!Date.now) { - Date.now = function () { - return new Date().valueOf(); - }; - } - - // console.knock && console.warn && console.error - let console = window.console; - let emptyFn = function () { - }; - $.each(['info', 'log', 'warn', 'error'], function (key, value) { - if (console == null) { - E[value] = emptyFn; - } else { - E[value] = function (info) { - // 通过配置来控制打印输出 - if (E.config && E.config.printLog) { - console[value]('wangEditor提示: ' + info); - } - }; - } - }); - - // 获取随机数 - E.random = function () { - return Math.random().toString().slice(2); - }; - - // 浏览器是否支持 placeholder - E.placeholder = 'placeholder' in document.createElement('input'); - - // 兼容IE8的 input placeholder - E.placeholderForIE8 = function ($container) { - if (E.placeholder) { - return; - } - $container.find('input[placeholder]').each(function () { - let $input = $(this); - let placeholder = $input.attr('placeholder'); - - if ($input.val() === '') { - $input.css('color', '#666'); - $input.val(placeholder); - - $input.on('focus.placeholder click.placeholder', function () { - $input.val(''); - $input.css('color', '#333'); - $input.off('focus.placeholder click.placeholder'); - }); - } - }); - }; - }); -// 语言包 - _e(function (E, $) { - E.langs = {}; - - // 中文 - E.langs['zh-cn'] = { - bold: '粗体', - underline: '下划线', - italic: '斜体', - forecolor: '文字颜色', - bgcolor: '背景色', - strikethrough: '删除线', - eraser: '清空格式', - source: '源码', - quote: '引用', - fontfamily: '字体', - fontsize: '字号', - head: '标题', - orderlist: '有序列表', - unorderlist: '无序列表', - alignleft: '左对齐', - aligncenter: '居中', - alignright: '右对齐', - link: '链接', - text: '文本', - submit: '提交', - cancel: '取消', - unlink: '取消链接', - table: '表格', - emotion: '表情', - img: '图片', - video: '视频', - 'width': '宽', - 'height': '高', - location: '位置', - loading: '加载中', - searchlocation: '搜索位置', - dynamicMap: '动态地图', - clearLocation: '清除位置', - langDynamicOneLocation: '动态地图只能显示一个位置', - insertcode: '插入代码', - undo: '撤销', - redo: '重复', - fullscreen: '全屏', - openLink: '打开链接' - }; - - // 英文 - E.langs.en = { - bold: 'Bold', - underline: 'Underline', - italic: 'Italic', - forecolor: 'Color', - bgcolor: 'Backcolor', - strikethrough: 'Strikethrough', - eraser: 'Eraser', - source: 'Codeview', - quote: 'Quote', - fontfamily: 'Font family', - fontsize: 'Font size', - head: 'Head', - orderlist: 'Ordered list', - unorderlist: 'Unordered list', - alignleft: 'Align left', - aligncenter: 'Align center', - alignright: 'Align right', - link: 'Insert link', - text: 'Text', - submit: 'Submit', - cancel: 'Cancel', - unlink: 'Unlink', - table: 'Table', - emotion: 'Emotions', - img: 'Image', - video: 'Video', - 'width': 'width', - 'height': 'height', - location: 'Location', - loading: 'Loading', - searchlocation: 'search', - dynamicMap: 'Dynamic', - clearLocation: 'Clear', - langDynamicOneLocation: 'Only one location in dynamic map', - insertcode: 'Insert Code', - undo: 'Undo', - redo: 'Redo', - fullscreen: 'Full screnn', - openLink: 'open link' - }; - }); -// 全局配置 - _e(function (E, $) { - - E.config = {}; - - // 全屏时的 z-index - E.config.zindex = 10000; - - // 是否打印log - E.config.printLog = true; - - // 菜单吸顶:false - 不吸顶;number - 吸顶,值为top值 - E.config.menuFixed = 0; - - // 编辑源码时,过滤 javascript - E.config.jsFilter = true; - - // 编辑器允许的标签 - E.config.legalTags = 'p,h1,h2,h3,h4,h5,h6,blockquote,table,ul,ol,pre'; - - // 语言包 - E.config.lang = E.langs['zh-cn']; - - // 菜单配置 - E.config.menus = [ - 'source', - '|', - 'bold', - 'underline', - 'italic', - 'strikethrough', - 'eraser', - 'forecolor', - 'bgcolor', - '|', - 'quote', - 'fontfamily', - 'fontsize', - 'head', - 'unorderlist', - 'orderlist', - 'alignleft', - 'aligncenter', - 'alignright', - '|', - 'link', - 'unlink', - 'table', - 'emotion', - '|', - 'img', - 'video', - 'location', - 'insertcode', - '|', - 'undo', - 'redo', - 'fullscreen' - ]; - - // 颜色配置 - E.config.colors = { - // 'value': 'title' - '#880000': '暗红色', - '#800080': '紫色', - '#ff0000': '红色', - '#ff00ff': '鲜粉色', - '#000080': '深蓝色', - '#0000ff': '蓝色', - '#00ffff': '湖蓝色', - '#008080': '蓝绿色', - '#008000': '绿色', - '#808000': '橄榄色', - '#00ff00': '浅绿色', - '#ffcc00': '橙黄色', - '#808080': '灰色', - '#c0c0c0': '银色', - '#000000': '黑色', - '#ffffff': '白色' - }; - - // 字体 - E.config.familys = [ - '宋体', '黑体', '楷体', '微软雅黑', - 'Arial', 'Verdana', 'Georgia', - 'Times New Roman', 'Microsoft JhengHei', - 'Trebuchet MS', 'Courier New', 'Impact', 'Comic Sans MS', 'Consolas' - ]; - - // 字号 - E.config.fontsizes = { - // 格式:'value': 'title' - 1: '12px', - 2: '13px', - 3: '16px', - 4: '18px', - 5: '24px', - 6: '32px', - 7: '48px' - }; - - // 表情包 - E.config.emotionsShow = 'icon'; // 显示项,默认为'icon',也可以配置成'value' - E.config.emotions = { - // 'default': { - // title: '默认', - // data: './emotions.data' - // }, - 'weibo': { - title: '微博表情', - data: [ - { - icon: 'http://img.t.sinajs.cn/t35/style/images/common/face/ext/normal/7a/shenshou_thumb.gif', - value: '[草泥马]' - }, - { - icon: 'http://img.t.sinajs.cn/t35/style/images/common/face/ext/normal/60/horse2_thumb.gif', - value: '[神马]' - }, - { - icon: 'http://img.t.sinajs.cn/t35/style/images/common/face/ext/normal/bc/fuyun_thumb.gif', - value: '[浮云]' - }, - { - icon: 'http://img.t.sinajs.cn/t35/style/images/common/face/ext/normal/c9/geili_thumb.gif', - value: '[给力]' - }, - { - icon: 'http://img.t.sinajs.cn/t35/style/images/common/face/ext/normal/f2/wg_thumb.gif', - value: '[围观]' - }, - { - icon: 'http://img.t.sinajs.cn/t35/style/images/common/face/ext/normal/70/vw_thumb.gif', - value: '[威武]' - }, - { - icon: 'http://img.t.sinajs.cn/t35/style/images/common/face/ext/normal/6e/panda_thumb.gif', - value: '[熊猫]' - }, - { - icon: 'http://img.t.sinajs.cn/t35/style/images/common/face/ext/normal/81/rabbit_thumb.gif', - value: '[兔子]' - }, - { - icon: 'http://img.t.sinajs.cn/t35/style/images/common/face/ext/normal/bc/otm_thumb.gif', - value: '[奥特曼]' - }, - { - icon: 'http://img.t.sinajs.cn/t35/style/images/common/face/ext/normal/15/j_thumb.gif', - value: '[囧]' - }, - { - icon: 'http://img.t.sinajs.cn/t35/style/images/common/face/ext/normal/89/hufen_thumb.gif', - value: '[互粉]' - }, - { - icon: 'http://img.t.sinajs.cn/t35/style/images/common/face/ext/normal/c4/liwu_thumb.gif', - value: '[礼物]' - }, - { - icon: 'http://img.t.sinajs.cn/t35/style/images/common/face/ext/normal/ac/smilea_thumb.gif', - value: '[呵呵]' - }, - { - icon: 'http://img.t.sinajs.cn/t35/style/images/common/face/ext/normal/0b/tootha_thumb.gif', - value: '[哈哈]' - } - ] - } - }; - - // 百度地图的key - E.config.mapAk = 'TVhjYjq1ICT2qqL5LdS8mwas'; - - // 上传图片的配置 - // server地址 - E.config.uploadImgUrl = ''; - // 超时时间 - E.config.uploadTimeout = 20 * 1000; - // 用于存储上传回调事件 - E.config.uploadImgFns = {}; - // 自定义上传图片的filename - // E.config.uploadImgFileName = 'customFileName'; - - // 自定义上传,设置为 true 之后,显示上传图标 - E.config.customUpload = false; - // 自定义上传的init事件 - // E.config.customUploadInit = function () {....}; - - // 自定义上传时传递的参数(如 token) - E.config.uploadParams = { - /* token: 'abcdef12345' */ - }; - - // 自定义上传是的header参数 - E.config.uploadHeaders = { - /* 'Accept' : 'text/x-json' */ - }; - - // 隐藏网络图片,默认为 false - E.config.hideLinkImg = false; - - // 是否过滤粘贴内容 - E.config.pasteFilter = true; - - // 是否粘贴纯文本,当 editor.config.pasteFilter === false 时候,此配置将失效 - E.config.pasteText = false; - - // 插入代码时,默认的语言 - E.config.codeDefaultLang = 'javascript'; - - }); -// 全局UI - _e(function (E, $) { - - E.UI = {}; - - // 为菜单自定义配置的UI - E.UI.menus = { - // 这个 default 不加引号,在 IE8 会报错 - 'default': { - normal: '', - selected: '.selected' - }, - bold: { - normal: '', - selected: '.selected' - }, - underline: { - normal: '', - selected: '.selected' - }, - italic: { - normal: '', - selected: '.selected' - }, - forecolor: { - normal: '', - selected: '.selected' - }, - bgcolor: { - normal: '', - selected: '.selected' - }, - strikethrough: { - normal: '', - selected: '.selected' - }, - eraser: { - normal: '', - selected: '.selected' - }, - quote: { - normal: '', - selected: '.selected' - }, - source: { - normal: '', - selected: '.selected' - }, - fontfamily: { - normal: '', - selected: '.selected' - }, - fontsize: { - normal: '', - selected: '.selected' - }, - head: { - normal: '', - selected: '.selected' - }, - orderlist: { - normal: '', - selected: '.selected' - }, - unorderlist: { - normal: '', - selected: '.selected' - }, - alignleft: { - normal: '', - selected: '.selected' - }, - aligncenter: { - normal: '', - selected: '.selected' - }, - alignright: { - normal: '', - selected: '.selected' - }, - link: { - normal: '', - selected: '.selected' - }, - unlink: { - normal: '', - selected: '.selected' - }, - table: { - normal: '', - selected: '.selected' - }, - emotion: { - normal: '', - selected: '.selected' - }, - img: { - normal: '', - selected: '.selected' - }, - video: { - normal: '', - selected: '.selected' - }, - location: { - normal: '', - selected: '.selected' - }, - insertcode: { - normal: '', - selected: '.selected' - }, - undo: { - normal: '', - selected: '.selected' - }, - redo: { - normal: '', - selected: '.selected' - }, - fullscreen: { - normal: '', - selected: '' - } - }; - - }); -// 对象配置 - _e(function (E, $) { - - E.fn.initDefaultConfig = function () { - let editor = this; - editor.config = $.extend({}, E.config); - editor.UI = $.extend({}, E.UI); - }; - - }); -// 增加 container - _e(function (E, $) { - - E.fn.addEditorContainer = function () { - this.$editorContainer = $(''); - }; - - }); -// 增加编辑区域对象 - _e(function (E, $) { - - E.fn.addTxt = function () { - let editor = this; - let txt = new E.Txt(editor); - - editor.txt = txt; - }; - - }); -// 增加menuContainer对象 - _e(function (E, $) { - - E.fn.addMenuContainer = function () { - let editor = this; - editor.menuContainer = new E.MenuContainer(editor); - }; - - }); -// 增加menus - _e(function (E, $) { - - // 存储创建菜单的函数 - E.createMenuFns = []; - E.createMenu = function (fn) { - E.createMenuFns.push(fn); - }; - - // 创建所有菜单 - E.fn.addMenus = function () { - let editor = this; - let menuIds = editor.config.menus; - - // 检验 menuId 是否在配置中存在 - function check(menuId) { - if (menuIds.indexOf(menuId) >= 0) { - return true; - } - return false; - } - - // 遍历所有的菜单创建函数,并执行 - $.each(E.createMenuFns, function (k, createMenuFn) { - createMenuFn.call(editor, check); - }); - }; - - }); -// bold菜单 - _e(function (E, $) { - - E.createMenu(function (check) { - let menuId = 'bold'; - if (!check(menuId)) { - return; - } - - let editor = this; - let lang = editor.config.lang; - - // 创建 menu 对象 - let menu = new E.Menu({ - editor: editor, - id: menuId, - title: lang.bold, - commandName: 'Bold' - }); - - // 定义选中状态下的click事件 - menu.clickEventSelected = function (e) { - let isRangeEmpty = editor.isRangeEmpty(); - if (!isRangeEmpty) { - // 如果选区有内容,则执行基础命令 - editor.command(e, 'Bold'); - } else { - // 如果选区没有内容 - editor.commandForElem('b,strong,h1,h2,h3,h4,h5', e, 'Bold'); - } - }; - - // 增加到editor对象中 - editor.menus[menuId] = menu; - }); - - }); -// underline菜单 - _e(function (E, $) { - - E.createMenu(function (check) { - let menuId = 'underline'; - if (!check(menuId)) { - return; - } - - let editor = this; - let lang = editor.config.lang; - - // 创建 menu 对象 - let menu = new E.Menu({ - editor: editor, - id: menuId, - title: lang.underline, - commandName: 'Underline' - }); - - // 定义选中状态下的click事件 - menu.clickEventSelected = function (e) { - let isRangeEmpty = editor.isRangeEmpty(); - if (!isRangeEmpty) { - // 如果选区有内容,则执行基础命令 - editor.command(e, 'Underline'); - } else { - // 如果选区没有内容 - editor.commandForElem('u,a', e, 'Underline'); - } - }; - - // 增加到editor对象中 - editor.menus[menuId] = menu; - }); - - }); -// italic 菜单 - _e(function (E, $) { - - E.createMenu(function (check) { - let menuId = 'italic'; - if (!check(menuId)) { - return; - } - let editor = this; - let lang = editor.config.lang; - - // 创建 menu 对象 - let menu = new E.Menu({ - editor: editor, - id: menuId, - title: lang.italic, - commandName: 'Italic' - }); - - // 定义选中状态下的click事件 - menu.clickEventSelected = function (e) { - let isRangeEmpty = editor.isRangeEmpty(); - if (!isRangeEmpty) { - // 如果选区有内容,则执行基础命令 - editor.command(e, 'Italic'); - } else { - // 如果选区没有内容 - editor.commandForElem('i', e, 'Italic'); - } - }; - - // 增加到editor对象中 - editor.menus[menuId] = menu; - }); - - }); -// forecolor 菜单 - _e(function (E, $) { - - E.createMenu(function (check) { - let menuId = 'forecolor'; - if (!check(menuId)) { - return; - } - let editor = this; - let lang = editor.config.lang; - let configColors = editor.config.colors; - - // 创建 menu 对象 - let menu = new E.Menu({ - editor: editor, - id: menuId, - title: lang.forecolor - }); - - // 创建 dropPanel - let $content = $(''); - $.each(configColors, function (k, v) { - $content.append( - [ - '' - ].join('') - ); - }); - $content.on('click', 'a[commandValue]', function (e) { - // 执行命令 - let $elem = $(this); - let commandValue = $elem.attr('commandValue'); - - if (menu.selected && editor.isRangeEmpty()) { - // 当前处于选中状态,并且选中内容为空 - editor.commandForElem('font[color]', e, 'forecolor', commandValue); - } else { - // 当前未处于选中状态,或者有选中内容。则执行默认命令 - editor.command(e, 'forecolor', commandValue); - } - }); - menu.dropPanel = new E.DropPanel(editor, menu, { - $content: $content, - width: 125 - }); - - // 定义 update selected 事件 - menu.updateSelectedEvent = function () { - let rangeElem = editor.getRangeElem(); - rangeElem = editor.getSelfOrParentByName(rangeElem, 'font[color]'); - if (rangeElem) { - return true; - } - return false; - }; - - // 增加到editor对象中 - editor.menus[menuId] = menu; - }); - - }); -// bgcolor 菜单 - _e(function (E, $) { - - E.createMenu(function (check) { - let menuId = 'bgcolor'; - if (!check(menuId)) { - return; - } - let editor = this; - let lang = editor.config.lang; - let configColors = editor.config.colors; - - // 检查元素是否有 background-color: 内联样式 - function checkElemFn(elem) { - let cssText; - if (elem && elem.style && elem.style.cssText != null) { - cssText = elem.style.cssText; - if (cssText && cssText.indexOf('background-color:') >= 0) { - return true; - } - } - return false; - } - - // 创建 menu 对象 - let menu = new E.Menu({ - editor: editor, - id: menuId, - title: lang.bgcolor - }); - - // 创建 dropPanel - let $content = $(''); - $.each(configColors, function (k, v) { - $content.append( - [ - '' - ].join('') - ); - }); - $content.on('click', 'a[commandValue]', function (e) { - // 执行命令 - - let $elem = $(this); - let commandValue = $elem.attr('commandValue'); - - if (menu.selected && editor.isRangeEmpty()) { - // 当前处于选中状态,并且选中内容为空。使用 commandForElem 执行命令 - editor.commandForElem({ - selector: 'span,font', - check: checkElemFn - }, e, 'BackColor', commandValue); - } else { - // 当前未处于选中状态,或者有选中内容。则执行默认命令 - editor.command(e, 'BackColor', commandValue); - } - }); - menu.dropPanel = new E.DropPanel(editor, menu, { - $content: $content, - width: 125 - }); - - // 定义 update selected 事件 - menu.updateSelectedEvent = function () { - let rangeElem = editor.getRangeElem(); - rangeElem = editor.getSelfOrParentByName(rangeElem, 'span,font', checkElemFn); - - if (rangeElem) { - return true; - } - return false; - }; - - // 增加到editor对象中 - editor.menus[menuId] = menu; - }); - - }); -// strikethrough 菜单 - _e(function (E, $) { - - E.createMenu(function (check) { - let menuId = 'strikethrough'; - if (!check(menuId)) { - return; - } - let editor = this; - let lang = editor.config.lang; - - // 创建 menu 对象 - let menu = new E.Menu({ - editor: editor, - id: menuId, - title: lang.strikethrough, - commandName: 'StrikeThrough' - }); - - // 定义选中状态下的click事件 - menu.clickEventSelected = function (e) { - let isRangeEmpty = editor.isRangeEmpty(); - if (!isRangeEmpty) { - // 如果选区有内容,则执行基础命令 - editor.command(e, 'StrikeThrough'); - } else { - // 如果选区没有内容 - editor.commandForElem('strike', e, 'StrikeThrough'); - } - }; - - // 增加到editor对象中 - editor.menus[menuId] = menu; - }); - - }); -// eraser 菜单 - _e(function (E, $) { - - E.createMenu(function (check) { - let menuId = 'eraser'; - if (!check(menuId)) { - return; - } - let editor = this; - let lang = editor.config.lang; - - // 创建 menu 对象 - let menu = new E.Menu({ - editor: editor, - id: menuId, - title: lang.eraser, - commandName: 'RemoveFormat' - }); - - // 定义点击事件 - menu.clickEvent = function (e) { - let isRangeEmpty = editor.isRangeEmpty(); - - if (!isRangeEmpty) { - // 选区不是空的,则执行默认命令 - editor.command(e, 'RemoveFormat'); - return; - } - - let $clearElem; - - // 自定义的命令函数 - function commandFn() { - let editor = this; - let rangeElem; - let pElem, $pElem; - let quoteElem, $quoteElem; - let listElem, $listElem; - - // 获取选区 elem - rangeElem = editor.getRangeElem(); - // 第一步,获取 quote 父元素 - quoteElem = editor.getSelfOrParentByName(rangeElem, 'blockquote'); - if (quoteElem) { - $quoteElem = $(quoteElem); - $clearElem = $('
' + $quoteElem.text() + '
'); - $quoteElem.after($clearElem).remove(); - } - // 第二步,获取 p h 父元素 - pElem = editor.getSelfOrParentByName(rangeElem, 'p,h1,h2,h3,h4,h5'); - if (pElem) { - $pElem = $(pElem); - $clearElem = $('' + $pElem.text() + '
'); - $pElem.after($clearElem).remove(); - } - // 第三步,获取list - listElem = editor.getSelfOrParentByName(rangeElem, 'ul,ol'); - if (listElem) { - $listElem = $(listElem); - $clearElem = $('' + $listElem.text() + '
'); - $listElem.after($clearElem).remove(); - } - } - - // 自定义 callback 事件 - function callback() { - // callback中,设置range为clearElem - let editor = this; - if ($clearElem) { - editor.restoreSelectionByElem($clearElem.get(0)); - } - } - - // 执行自定义命令 - editor.customCommand(e, commandFn, callback); - }; - - // 增加到editor对象中 - editor.menus[menuId] = menu; - }); - - }); -// source 菜单 - _e(function (E, $) { - - E.createMenu(function (check) { - let menuId = 'source'; - if (!check(menuId)) { - return; - } - let editor = this; - let lang = editor.config.lang; - let txtHtml; - - // 创建 menu 对象 - let menu = new E.Menu({ - editor: editor, - id: menuId, - title: lang.source - }); - - menu.isShowCode = false; - - // 更新内容 - function updateValue() { - let $code = menu.$codeTextarea; - let $txt = editor.txt.$txt; - let value = $.trim($code.val()); // 取值 - - if (!value) { - value = '