You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
text/web/lay/modules/code.js

59 lines
2.9 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

/** layui-v2.5.6 MIT License By https://www.layui.com */
;layui.define("jquery",function(e){"use strict";
// 引入 layui 的 $jQuery模块
var a=layui.$,
// layui 文档模块的 URL 地址
l="http://www.layui.com/doc/modules/code.html";
// 定义模块 "code",并执行传入的函数
e("code",function(e){
var t=[]; // 初始化一个空数组,用于存储每个代码块元素
// 如果 e 参数未定义,则默认为空对象
e=e||{},
// 如果 e 中没有指定 about则默认为 true
e.about=!("about"in e)||e.about,
// 获取所有指定的代码块元素,默认为 .layui-code
e.elem=a(e.elem||".layui-code"),
// 遍历所有代码块元素
e.elem.each(function(){
t.push(this); // 将每个代码块元素添加到数组 t 中
}),
// 倒序遍历每个代码块元素
layui.each(t.reverse(),function(t,i){
var c=a(i), // 获取当前代码块元素
o=c.html(); // 获取当前代码块的 HTML 内容
// 如果代码块元素有 lay-encode 属性或者传入参数 e.encode 为真,进行 HTML 转义
(c.attr("lay-encode")||e.encode)&&(o=o.replace(/&(?!#?[a-zA-Z0-9]+;)/g,"&")
.replace(/</g,"&lt;")
.replace(/>/g,"&gt;")
.replace(/'/g,"&#39;")
.replace(/"/g,"&quot;"));
// 将转义后的 HTML 内容分行显示,每行一个 <li> 元素
c.html('<ol class="layui-code-ol"><li>'+o.replace(/[\r\t\n]+/g,"</li><li>")+"</li></ol>");
// 如果没有标题插入一个标题h3标题内容可以通过 lay-title 来指定
c.find(">.layui-code-h3")[0] || c.prepend('<h3 class="layui-code-h3">'+(c.attr("lay-title")||e.title||"code")+(e.about?'<a href="'+l+'" target="_blank">layui.code</a>':"")+"</h3>");
// 获取代码块中的 <ol> 元素
var d=c.find(">.layui-code-ol");
// 为代码块添加类名,设置样式
c.addClass("layui-box layui-code-view"),
// 如果有指定 skin 或 lay-skin给代码块添加相应的样式
(c.attr("lay-skin")||e.skin) && c.addClass("layui-code-"+(c.attr("lay-skin")||e.skin)),
// 如果代码行数超过100行设置左边距使其有缩进效果
(d.find("li").length/100|0)>0 && d.css("margin-left",(d.find("li").length/100|0)+"px"),
// 如果指定了 lay-height 或者传入的参数 e.height设置最大高度
(c.attr("lay-height")||e.height) && d.css("max-height",c.attr("lay-height")||e.height)
})
})})
// 引入代码块的 CSS 样式
.addcss("modules/code.css","skincodecss");