Browse Source

加强 code 的 `tools` 和 `toolsEvent` 属性,提升可扩展性

pull/1363/head
贤心 1 year ago
parent
commit
818bfb6466
  1. 31
      src/modules/code.js

31
src/modules/code.js

@ -210,14 +210,37 @@ layui.define(['lay', 'util', 'element', 'form'], function(exports){
elemToolbar.on('click', '>i', function(){ elemToolbar.on('click', '>i', function(){
var oi = $(this); var oi = $(this);
var type = oi.data('type'); var type = oi.data('type');
// 内部 tools event
tools[type] && typeof tools[type].event === 'function' && tools[type].event(oi, type); tools[type] && typeof tools[type].event === 'function' && tools[type].event(oi, type);
typeof options.toolsEvent === 'function' && options.toolsEvent(oi, type);
// 外部 tools event
typeof options.toolsEvent === 'function' && options.toolsEvent({
elem: oi,
type: type,
rawCode: codes.join(''), // 原始 code
finalCode: util.unescape(finalCode) // 最终 code
});
}); });
// 渲染工具栏
layui.each(options.tools, function(i, v){ layui.each(options.tools, function(i, v){
var className = (tools[v] && tools[v].className) || v; var viso = typeof v === 'object'; // 若为 object 值,则可自定义更多属性
var title = (tools[v] && tools[v].title) || ['']; var tool = viso ? v : (
tools[v] || {
className: v,
title: [v]
}
);
var className = tool.className || tool.type;
var title = tool.title || [''];
var type = viso ? ( tool.type || className ) : v;
if (!type) return;
elemToolbar.append( elemToolbar.append(
'<i class="layui-icon layui-icon-'+ className +'" data-type="'+ v +'" title="'+ title[0] +'"></i>' '<i class="layui-icon layui-icon-'+ className +'" data-type="'+ type +'" title="'+ title[0] +'"></i>'
); );
}); });

Loading…
Cancel
Save