看到有人对我做的WangEditor比较感兴趣,问了一些问题。但由于我并不常来,所以就没能及时答复,抱歉了。
未避免以后类似问题发生,我将我修改的wangeditor.js直接发在这里,有兴趣的可以下载后自己分析。希望能帮到需要的人。
扩展后的界面如下:
全部代码5525行。
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
typeof define === 'function' && define.amd ? define(factory) :
(global.wangEditor = factory());
}(this, (function () { 'use strict';
/*
poly-fill
*/
var polyfill = function () {
// Object.assign
if (typeof Object.assign != 'function') {
Object.assign = function (target, varArgs) {
// .length of function is 2
if (target == null) {
// TypeError if undefined or null
throw new TypeError('Cannot convert undefined or null to object');
}
var to = Object(target);
for (var index = 1; index < arguments.length; index++) {
var nextSource = arguments[index];
if (nextSource != null) {
// Skip over if undefined or null
for (var nextKey in nextSource) {
// Avoid bugs when hasOwnProperty is shadowed
if (Object.prototype.hasOwnProperty.call(nextSource, nextKey)) {
to[nextKey] = nextSource[nextKey];
}
}
}
}
return to;
};
}
// IE 中兼容 Element.prototype.matches
if (!Element.prototype.matches) {
Element.prototype.matches = Element.prototype.matchesSelector || Element.prototype.mozMatchesSelector || Element.prototype.msMatchesSelector || Element.prototype.oMatchesSelector || Element.prototype.webkitMatchesSelector || function (s) {
var matches = (this.document || this.ownerDocument).querySelectorAll(s),
i = matches.length;
while (--i >= 0 && matches.item(i) !== this) {}
return i > -1;
};
}
};
/*
DOM 操作 API
*/
// 根据 html 代码片段创建 dom 对象
function createElemByHTML(html) {
var div = void 0;
div = document.createElement('div');
div.innerHTML = html;
return div.children;
}
// 是否是 DOM List
function isDOMList(selector) {
if (!selector) {
return false;
}
if (selector instanceof HTMLCollection || selector instanceof NodeList) {
return true;
}
return false;
}
// 封装 document.querySelectorAll
function querySelectorAll(selector) {
var result = document.querySelectorAll(selector);
if (isDOMList(result)) {
return result;
} else {
return [result];
}
}
// 记录所有的事件绑定
var eventList = [];
// 创建构造函数
function DomElement(selector) {
if (!selector) {
return;
}
// selector 本来就是 DomElement 对象,直接返回
if (selector instanceof DomElement) {
return selector;
}
this.selector = selector;
var nodeType = selector.nodeType;
// 根据 selector 得出的结果(如 DOM,DOM List)
var selectorResult = [];
if (nodeType === 9) {
// document 节点
selectorResult = [selector];
} else if (nodeType === 1) {
// 单个 DOM 节点
selectorResult = [selector];
} else if (isDOMList(selector) || selector instanceof Array) {
// DOM List 或者数组
selectorResult = selector;
} else if (typeof selector === 'string') {
// 字符串
selector = selector.replace('/\n/mg', '').trim();
if (selector.indexOf('<') === 0) {
// 如 <div>
selectorResult = createElemByHTML(selector);
} else {
// 如 #id .class
selectorResult = querySelectorAll(selector);
}
}
var length = selectorResult.length;
if (!length) {
// 空数组
return this;
}
// 加入 DOM 节点
var i = void 0;
for (i = 0; i < length; i++) {
this[i] = selectorResult[i];
}
this.length = length;
}
// 修改原型
DomElement.prototype = {
constructor: DomElement,
// 类数组,forEach
forEach: function forEach(fn) {
var i = void 0;
for (i = 0; i < this.length; i++) {
var elem = this[i];
var result = fn.call(elem, elem, i);
if (result === false) {
break;
}
}
return this;
},
// clone
clone: function clone(deep) {
var cloneList = [];
this.forEach(function (elem) {
cloneList.push(elem.cloneNode(!!deep));
});
return $(cloneList);
},
// 获取第几个元素
get: function get(index) {
var length = this.length;
if (index >= length) {
index = index % length;
}
return $(this[index]);
},
// 第一个
first: function first() {
return this.get(0);
},
// 最后一个
last: function last() {
var length = this.length;
return this.get(length - 1);
},
// 绑定事件
on: function on(type, selector, fn) {
// selector 不为空,证明绑定事件要加代理
if (!fn) {
fn = selector;
selector = null;
}
// type 是否有多个
var types = [];
types = type.split(/\s+/);
return this.forEach(function (elem) {
types.forEach(function (type) {
if (!type) {
return;
}
// 记录下,方便后面解绑
eventList.push({
elem: elem,
type: type,
fn: fn
});
if (!selector) {
// 无代理
elem.addEventListener(type, fn);
return;
}
// 有代理
elem.addEventListener(type, function (e) {
var target = e.target;
if (target.matches(selector)) {
fn.call(target, e);
}
});
});
});
},
// 取消事件绑定
off: function off(type, fn) {
return this.forEach(function (elem) {
elem.removeEventListener(type, fn);
});
},
// 获取/设置 属性
attr: function attr(key, val) {
if (val == null) {
// 获取值
return this[0].getAttribute(key);
} else {
// 设置值
return this.forEach(function (elem) {
elem.setAttribute(key, val);
});
}
},
// 添加 class
addClass: function addClass(className) {
if (!className) {
return this;
}
return this.forEach(function (elem) {
var arr = void 0;
if (elem.className) {
// 解析当前 className 转换为数组
arr = elem.className.split(/\s/);
arr = arr.filter(function (item) {
return !!item.trim();
});
// 添加 class
if (arr.indexOf(className) < 0) {
arr.push(className);
}
// 修改 elem.class
elem.className = arr.join(' ');
} else {
elem.className = className;
}
});
},
// 删除 class
removeClass: function removeClass(className) {
if (!className) {
return this;
}
return this.forEach(function (elem) {
var arr = void 0;
if (elem.className) {
// 解析当前 className 转换为数组
arr = elem.className.split(/\s/);
arr = arr.filter(function (item) {
item = item.trim();
// 删除 class
if (!item || item === className) {
return false;
}
return true;
});
// 修改 elem.class
elem.className = arr.join(' ');
}
});
},
// 修改 css
css: function css(key, val) {
var currentStyle = key + ':' + val + ';';
return this.forEach(function (elem) {
var style = (elem.getAttribute('style') || '').trim();
var styleArr = void 0,
resultArr = [];
if (style) {
// 将 style 按照 ; 拆分为数组
styleArr = style.split(';');
styleArr.forEach(function (item) {
// 对每项样式,按照 : 拆分为 key 和 value
var arr = item.split(':').map(function (i) {
return i.trim();
});
if (arr.length === 2) {
resultArr.push(arr[0] + ':' + arr[1]);
}
});
// 替换或者新增
resultArr = resultArr.map(function (item) {
if (item.indexOf(key) === 0) {
return currentStyle;
} else {
return item;
}
});
if (resultArr.indexOf(currentStyle) < 0) {
resultArr.push(currentStyle);
}
// 结果
elem.setAttribute('style', resultArr.join('; '));
} else {
// style 无值
elem.setAttribute('style', currentStyle);
}
});
},
// 显示
show: function show() {
return this.css('display', 'block');
},
// 隐藏
hide: function hide() {
return this.css('display', 'none');
},
// 获取子节点
children: function children() {
var elem = this[0];
if (!elem) {
return null;
}
return $(elem.children);
},
// 获取子节点(包括文本节点)
childNodes: function childNodes() {
var elem = this[0];
if (!elem) {
return null;
}
return $(elem.childNodes);
},
// 增加子节点
append: function append($children) {
return this.forEach(function (elem) {
$children.forEach(function (child) {
elem.appendChild(child);
});
});
},
// 移除当前节点
remove: function remove() {
return this.forEach(function (elem) {
if (elem.remove) {
elem.remove();
} else {
var parent = elem.parentElement;
parent && parent.removeChild(elem);
}
});
},
// 是否包含某个子节点
isContain: function isContain($child) {
var elem = this[0];
var child = $child[0];
return elem.contains(child);
},
// 尺寸数据
getSizeData: function getSizeData() {
var elem = this[0];
return elem.getBoundingClientRect(); // 可得到 bottom height left right top width 的数据
},
// 封装 nodeName
getNodeName: function getNodeName() {
var elem = this[0];
return elem.nodeName;
},
// 从当前元素查找
find: function find(selector) {
var elem = this[0];
return $(elem.querySelectorAll(selector));
},
// 获取当前元素的 text
text: function text(val) {
if (!val) {
// 获取 text
var elem = this[0];
return elem.innerHTML.replace(/<.*?>/g, function () {
return '';
});
} else {
// 设置 text
return this.forEach(function (elem) {
elem.innerHTML = val;
});
}
},
// 获取 html
html: function html(value) {
var elem = this[0];
if (value == null) {
return elem.innerHTML;
} else {
elem.innerHTML = value;
return this;
}
},
// 获取 value
val: function val() {
var elem = this[0];
return elem.value.trim();
},
// focus
focus: function focus() {
return this.forEach(function (elem) {
elem.focus();
});
},
// parent
parent: function parent() {
var elem = this[0];
return $(elem.parentElement);
},
// parentUntil 找到符合 selector 的父节点
parentUntil: function parentUntil(selector, _currentElem) {
var results = document.querySelectorAll(selector);
var length = results.length;
if (!length) {
// 传入的 selector 无效
return null;
}
var elem = _currentElem || this[0];
if (elem.nodeName === 'BODY') {
return null;
}
var parent = elem.parentElement;
var i = void 0;
for (i = 0; i < length; i++) {
if (parent === results[i]) {
// 找到,并返回
return $(parent);
}
}
// 继续查找
return this.parentUntil(selector, parent);
},
// 判断两个 elem 是否相等
equal: function equal($elem) {
if ($elem.nodeType === 1) {
return this[0] === $elem;
} else {
return this[0] === $elem[0];
}
},
// 将该元素插入到某个元素前面
insertBefore: function insertBefore(selector) {
var $referenceNode = $(selector);
var referenceNode = $referenceNode[0];
if (!referenceNode) {
return this;
}
return this.forEach(function (elem) {
var parent = referenceNode.parentNode;
parent.insertBefore(elem, referenceNode);
});
},
// 将该元素插入到某个元素后面
insertAfter: function insertAfter(selector) {
var $referenceNode = $(selector);
var referenceNode = $referenceNode[0];
if (!referenceNode) {
return this;
}
return this.forEach(function (elem) {
var parent = referenceNode.parentNode;
if (parent.lastChild === referenceNode) {
// 最后一个元素
parent.appendChild(elem);
} else {
// 不是最后一个元素
parent.insertBefore(elem, referenceNode.nextSibling);
}
});
}
};
// new 一个对象
function $(selector) {
return new DomElement(selector);
}
// 解绑所有事件,用于销毁编辑器
$.offAll = function () {
eventList.forEach(function (item) {
var elem = item.elem;
var type = item.type;
var fn = item.fn;
// 解绑
elem.removeEventListener(type, fn);
});
};
/*
配置信息
*/
var config = {
// 默认菜单配置 (: 修改菜单顺序)
menus: [ 'bold','italic', 'underline', 'underdot', 'strikeThrough','Sup','Sub','Middle','HRline', 'head', 'fontSize', 'fontName', 'foreColor', 'backColor',
'list', 'justify', 'emoticon', 'image', 'table','mathsymbol', 'Latex', 'Bracket','Fraction','Sqrt','Answer','Folder', 'link', 'video','undo', 'redo',
'a01', 'a02', 'a03', 'a04', 'a05', 'a06', 'a07', 'a08', 'a09', 'a10', 'a11','a12','a13','a14','a15','a16','a17','a18','a19','a20','a21','a22','a23','a24'],
fontNames: ['宋体', '微软雅黑', 'Arial', 'Tahoma', 'Verdana'],
colors: ['#000000', '#666666','#cccccc','#ffffff' , '#ff0000', '#ff7e72', '#005500', '#00ff00', '#00007f', '#0000ff', '#ffff00', '#aa007f'],
// // 语言配置
// lang: {
// '设置标题': 'title',
// '正文': 'p',
// '链接文字': 'link text',
// '链接': 'link',
// '插入': 'insert',
// '创建': 'init'
// },
// 表情
emotions: [{
title: '数学',
type: 'image',
content: [ //文件名对应的图像不能改变!!!!!!!!!!
{"src": "/static/png/math/01.png","alt": "1/2",},
{"src": "/static/png/math/10.png","alt": "√2",},
{"src": "/static/png/math/11.png","alt": "√3",},
{"src": "/static/png/math/12.png","alt": "√5",},
{"src": "/static/png/math/13.png","alt": "√6",},
{"src": "/static/png/math/14.png","alt": "求根公式",},
{"src": "/static/svg/1_2.svg","alt": "1/2",},
]
},{
title: 'emoji',
type: 'emoji',
content: '
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)