From 09c7dc8517098ae7e8fdcbfb549a143027af0382 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B4=A4=E5=BF=83?= <3277200+sentsim@users.noreply.github.com> Date: Wed, 29 Jun 2022 20:24:51 +0800 Subject: [PATCH] improve `code` component --- examples/code.html | 228 ++++++++++++++++----------------------- src/css/modules/code.css | 29 ++--- src/modules/code.js | 122 ++++++++++++--------- 3 files changed, 182 insertions(+), 197 deletions(-) diff --git a/examples/code.html b/examples/code.html index c0a68807..c3da0008 100644 --- a/examples/code.html +++ b/examples/code.html @@ -1,174 +1,132 @@
- - -- //路由 - LAY.fn.router = function(hash){ - var hashs = (hash || location.hash).replace(/^#/, '').split('/') || []; - var item, param = { - dir: [] - }; - for(var i = 0; i < hashs.length; i++){ - item = hashs[i].split('='); - /^\w+=/.test(hashs[i]) ? function(){ - if(item[0] !== 'dir'){ - param[item[0]] = item[1]; - } - }() : param.dir.push(hashs[i]); - item = null; - } - return param; - }; ++// 在里面存放任意的文本内容 +code line +code line +code line +code line +code line +code line +code line +code line +code line +code line +code line +code line +code line +code line +code line +code line +code line--//路由 -LAY.fn.router = function(hash){ - var hashs = (hash || location.hash).replace(/^#/, '').split('/') || []; - var item, param = { - dir: [] - }; - for(var i = 0; i < hashs.length; i++){ - item = hashs[i].split('='); - /^\w+=/.test(hashs[i]) ? function(){ - if(item[0] !== 'dir'){ - param[item[0]] = item[1]; - } - }() : param.dir.push(hashs[i]); - item = null; - } - return param; -}; ++code line +code line +code line +code line +code line +code line--//路由 -LAY.fn.router = function(hash){ - var hashs = (hash || location.hash).replace(/^#/, '').split('/') || []; - var item, param = { - dir: [] - }; - for(var i = 0; i < hashs.length; i++){ - item = hashs[i].split('='); - /^\w+=/.test(hashs[i]) ? function(){ - if(item[0] !== 'dir'){ - param[item[0]] = item[1]; - } - }() : param.dir.push(hashs[i]); - item = null; - } - return param; -}; ++code line +code line +code line +code line +code line +code line--var hashs = (hash || location.hash).replace(/^#/, '').split('/') || []; -var item, param = { - dir: [] -}; --//代码中的代码 -var hashs = (hash || location.hash).replace(/^#/, '').split('/') || []; -var item, param = { - dir: [] -}; -- //代码中的代码 - var hashs = (hash || location.hash).replace(/^#/, '').split('/') || []; - var item, param = { - dir: [] - }; ++code line +code line +code line +code line +-+code line +code line +code line ++code line +code line +code line-//路由 -LAY.fn.router = function(hash){ - var hashs = (hash || location.hash).replace(/^#/, '').split('/') || []; - var item, param = { - dir: [] - }; - for(var i = 0; i < hashs.length; i++){ - item = hashs[i].split('='); - /^\w+=/.test(hashs[i]) ? function(){ - if(item[0] !== 'dir'){ - param[item[0]] = item[1]; - } - }() : param.dir.push(hashs[i]); - item = null; - } - return param; -}; --//代码中的代码 -var hashs = (hash || location.hash).replace(/^#/, '').split('/') || []; -var item, param = { - dir: [] -}; --//代码中的代码 -var hashs = (hash || location.hash).replace(/^#/, '').split('/') || []; -var item, param = { - dir: [] -}; --//代码中的代码 -var hashs = (hash || location.hash).replace(/^#/, '').split('/') || []; -var item, param = { - dir: [] -}; +- -+code line +code line +code line +code line +code line +code line ++code line +code line +code line +code line +code line +code line ++code line +code line +code line +code line +code line +code line-- 123 -++++ 123 ++ About +-- about ++ custom more aboutdiff --git a/src/css/modules/code.css b/src/css/modules/code.css index e8c709bf..56e8c6a6 100644 --- a/src/css/modules/code.css +++ b/src/css/modules/code.css @@ -1,9 +1,5 @@ /** - - @Name: layui.code - @Description:Classic modular front-end UI framework - @License:MIT - + * code */ /* 加载就绪标志 */ @@ -12,19 +8,24 @@ html #layuicss-skincodecss{display:none; position: absolute; width:1989px;} /* 默认风格 */ .layui-code-view{display: block; position: relative; margin: 10px 0; padding: 0; border: 1px solid #eee; border-left-width: 6px; background-color: #FAFAFA; color: #333; font-family: Courier New; font-size: 13px;} .layui-code-title{position: relative; padding: 0 10px; height: 40px; line-height: 40px; border-bottom: 1px solid #eee; font-size: 12px;} -.layui-code-title > .layui-code-about{position: absolute; right: 10px; top: 0; color: #999;} -.layui-code-view > .layui-code-ol, .layui-code-view > .layui-code-ul{position: relative; overflow: auto;} +.layui-code-title > .layui-code-about{position: absolute; right: 10px; top: 0; color: #B7B7B7;} +.layui-code-about > a{padding-left: 10px;} +.layui-code-view > .layui-code-ol, +.layui-code-view > .layui-code-ul{position: relative; overflow: auto;} .layui-code-view > .layui-code-ol > li{position: relative; margin-left: 45px; line-height: 20px; padding: 0 10px; border-left: 1px solid #e2e2e2; list-style-type: decimal-leading-zero; *list-style-type: decimal; background-color: #fff;} -.layui-code-view > .layui-code-ol > li:first-child, .layui-code-view > .layui-code-ul > li:first-child{padding-top: 10px;} -.layui-code-view > .layui-code-ol > li:last-child, .layui-code-view > .layui-code-ul > li:last-child{padding-bottom: 10px;} +.layui-code-view > .layui-code-ol > li:first-child, +.layui-code-view > .layui-code-ul > li:first-child{padding-top: 10px;} +.layui-code-view > .layui-code-ol > li:last-child, +.layui-code-view > .layui-code-ul > li:last-child{padding-bottom: 10px;} .layui-code-view > .layui-code-ul > li{position: relative; line-height: 20px; padding: 0 10px; list-style-type: none; *list-style-type: none; background-color: #fff;} .layui-code-view pre{margin: 0;} -/* notepadd++ 风格 */ -.layui-code-notepad{border: 1px solid #0C0C0C; border-left-color: #3F3F3F; background-color: #0C0C0C; color: #C2BE9E} -.layui-code-notepad > .layui-code-title{border-bottom: none;} -.layui-code-notepad > .layui-code-ol > li, .layui-code-notepad > .layui-code-ul > li{background-color: #3F3F3F; border-left: none;} -.layui-code-notepad > .layui-code-ul > li{margin-left: 6px;} +/* 深色风格 */ +.layui-code-dark{border: 1px solid #0C0C0C; border-left-color: #3F3F3F; background-color: #0C0C0C; color: #C2BE9E} +.layui-code-dark > .layui-code-title{border-bottom: none;} +.layui-code-dark > .layui-code-ol > li, +.layui-code-dark > .layui-code-ul > li{background-color: #3F3F3F; border-left: none;} +.layui-code-dark > .layui-code-ul > li{margin-left: 6px;} /* 代码预览 */ .layui-code-demo .layui-code{visibility: visible !important; margin: -15px; border-top: none; border-right: none; border-bottom: none;} diff --git a/src/modules/code.js b/src/modules/code.js index 920d97ec..743a0471 100644 --- a/src/modules/code.js +++ b/src/modules/code.js @@ -1,73 +1,99 @@ /** * code - * 代码区简易修饰 + * 代码块简易修饰 */ -layui.define(['util'], function(exports){ +layui.define(['lay', 'util'], function(exports){ "use strict"; var $ = layui.$; var util = layui.util; + + var ELEM_TITLE = 'layui-code-title'; + + // 默认参数项 + var config = { + elem: '.layui-code', // 元素选择器 + title: '</>', // 标题 + about: '', // 右上角信息 + ln: true // 是否显示行号 + }; + + var trim = function(str){ + return $.trim(str).replace(/^\n|\n$/, ''); + }; + // export api exports('code', function(options){ - var elems = []; - var trim = function(str){ - return $.trim(str).replace(/^\n|\n$/, ''); - } + var opts = options = $.extend({}, config, options); - options = options || {}; - options.elem = $(options.elem||'.layui-code'); + // 目标元素是否存在 + options.elem = $(options.elem); + if(!options.elem[0]) return; - options.elem.each(function(){ - elems.push(this); - }); - - layui.each(elems.reverse(), function(index, item){ + // 从内至外渲染 + layui.each(options.elem.get().reverse(), function(index, item){ var othis = $(item); var html = trim(othis.html()); - var about = othis.attr('lay-about') || options.about || othis.attr('lay-lang') || options.lang || ''; - //是否显示行号 - var lineNo; - if(othis.attr('lay-line-no')) - { - lineNo = othis.attr('lay-line-no').toLowerCase() === 'true'; - } - lineNo = options.lineNo === undefined ? lineNo === undefined ? false : lineNo : options.lineNo; + // 合并属性上的参数,并兼容旧版本属性写法 lay-* + var options = $.extend({}, opts, lay.options(item), function(obj){ + var attrs = ['title', 'height', 'encode', 'skin', 'about']; + layui.each(attrs, function(i, attr){ + var value = othis.attr('lay-'+ attr); + if(typeof value === 'string'){ + obj[attr] = value; + } + }) + return obj; + }({})); + + // 有序或无序列表 + var listTag = options.ln ? 'ol' : 'ul'; + var listElem = $('<'+ listTag +' class="layui-code-'+ listTag +'">'); + + // header + var headerElem = $(''); + + // 添加组件 clasName + othis.addClass('layui-code-view layui-box'); + + // 自定义风格 + if(options.skin){ + if(options.skin === 'notepad') options.skin = 'dark'; + othis.addClass('layui-code-'+ options.skin); + } // 转义 HTML 标签 - if(othis.attr('lay-encode') || options.encode){ - html = util.escape(html); - } - - var list = lineNo ? 'ol' : 'ul'; - othis.html('<' + list + ' class="layui-code-' + list + '">' + html.replace(/[\r\t\n]+/g, ' ') + ' ' + list + '>'); - - if(!othis.find('>.layui-code-title')[0]){ - var aboutWrapper = about === '' ? '' : '' + about + ''; - othis.prepend('' + (othis.attr('lay-title') || options.title || '</>') + aboutWrapper + ''); - } - - var ol = othis.find('>.layui-code-' + list); - othis.addClass('layui-box layui-code-view'); + if(options.encode) html = util.escape(html); + html = html.replace(/[\r\t\n]+/g, ''); // 转义换行符 - //识别皮肤 - if(othis.attr('lay-skin') || options.skin){ - othis.addClass('layui-code-' +(othis.attr('lay-skin') || options.skin)); - } + // 生成列表 + othis.html(listElem.html(' ' + html + ' ')); - //按行数适配左边距 - if((ol.find('li').length/100|0) > 0){ - ol.css('margin-left', (ol.find('li').length/100|0) + 'px'); + // 创建 header + if(!othis.children('.'+ ELEM_TITLE)[0]){ + headerElem.html(options.title + ( + options.about + ? '' + options.about + '' + : '' + )); + othis.prepend(headerElem); } - - //设置最大高度 - if(othis.attr('lay-height') || options.height){ - ol.css('max-height', othis.attr('lay-height') || options.height); + + // 按行数适配左边距 + (function(autoIncNums){ + if(autoIncNums > 0){ + listElem.css('margin-left', autoIncNums + 'px'); + } + })(Math.floor(listElem.find('li').length/100)); + + // 限制最大高度 + if(options.height){ + listElem.css('max-height', options.height); } }); }); -}).addcss('modules/code.css?v=2', 'skincodecss'); - +}).addcss('modules/code.css?v=3', 'skincodecss'); \ No newline at end of file