Skip to content

Commit

Permalink
[文档] 修复 layui 的 tpl 写法与 vue 渲染冲突的问题的
Browse files Browse the repository at this point in the history
  • Loading branch information
yelog committed Jul 19, 2020
1 parent 8faca81 commit 277186b
Show file tree
Hide file tree
Showing 57 changed files with 311 additions and 197 deletions.
4 changes: 3 additions & 1 deletion build/md-loader/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const {
stripScript,
stripTemplate,
stripTpl,
genInlineComponentText
} = require('./util')
const md = require('./config')
Expand All @@ -26,7 +27,8 @@ module.exports = function(source) {
const commentContent = content.slice(commentStart + startTagLen, commentEnd)
const html = stripTemplate(commentContent)
const script = stripScript(commentContent)
const demoComponentContent = genInlineComponentText(html, script)
const tpl = stripTpl(commentContent)
const demoComponentContent = genInlineComponentText(html, script, tpl)
const demoComponentName = `element-demo${id}`
output.push(`<template slot="source"><${demoComponentName} /></template>`)
componenetsString += `${JSON.stringify(demoComponentName)}: ${demoComponentContent},`
Expand Down
25 changes: 24 additions & 1 deletion build/md-loader/util.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const { compileTemplate } = require('@vue/component-compiler-utils')
const compiler = require('vue-template-compiler')
var escape = require('escape-html');

function stripScript(content) {
const result = content.match(/<(script)>([\s\S]+)<\/\1>/)
Expand All @@ -20,14 +21,33 @@ function stripTemplate(content) {
return content.replace(/<(script|style)[\s\S]+<\/\1>/g, '').trim()
}

// 编写例子时不一定有 template。所以采取的方案是剔除其他的内容
function stripTpl(content) {
content = content.trim()
if (!content) {
return content
}
const matches = content.match(/<(script)[\s\S]+?<\/\1>/g, '')
if (matches && matches.length>0) {
const result = []
for (let i = 0; i < matches.length; i++) {
if (matches[i].indexOf('text/html') !== -1) {
result.push(matches[i])
}
}
return result.join('')
}
return ''
}

function pad(source) {
return source
.split(/\r?\n/)
.map(line => ` ${line}`)
.join('\n')
}

function genInlineComponentText(template, script) {
function genInlineComponentText(template, script, tpl) {
// https://github.com/vuejs/vue-loader/blob/423b8341ab368c2117931e909e2da9af74503635/lib/loaders/templateLoader.js#L46
const finalOptions = {
source: `<div>${template}</div>`,
Expand Down Expand Up @@ -57,6 +77,8 @@ function genInlineComponentText(template, script) {
if (script) {
script = `export default {
mounted () {
var tpl = '${escape(tpl.replace(/\n/g, '--enter--'))}'
layui.$('#commonTpl').append(HTMLDecode(tpl.replace(/--enter--/g, '\\n')))
${script}
}
}`
Expand All @@ -80,5 +102,6 @@ module.exports = {
stripScript,
stripStyle,
stripTemplate,
stripTpl,
genInlineComponentText
}
2 changes: 1 addition & 1 deletion docs/10.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/11.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/12.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/13.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/14.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/15.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/16.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/17.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/18.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/19.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/20.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/21.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/22.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/23.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/24.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/25.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/26.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/27.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/28.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/29.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/30.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/31.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/32.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/33.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/34.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/35.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/36.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/37.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/38.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/39.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/4.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/40.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/41.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/42.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/43.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/44.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/45.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/46.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions docs/47.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/5.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/6.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/7.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/8.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/9.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/app.d5f576d.js → docs/app.233ed18.js

Large diffs are not rendered by default.

160 changes: 86 additions & 74 deletions docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,89 @@
height: 100%;
border: none;
}
#commonTpl {
display: none
}
</style>
<link rel="shortcut icon" href="favicon.ico"></head>
<body>
<script src="layui/layui.js"></script>
<script>
if (!window.Promise) {
document.write('<script src="//cdn.jsdelivr.net/npm/[email protected]/dist/es6-promise.min.js"><\/script><script>ES6Promise.polyfill()<\/script>')
}
// 自定义模块
layui.config({
base: 'ext/', // 模块目录
version: 'v1.5.15'
}).extend({ // 模块别名
soulTable: 'soulTable'
});
var $, layer;
layui.use(['jquery', 'layer'], function() {
$ = layui.$;
layer = layui.layer;
$.ajax({
url: 'runjs.html',
dataType: 'html',
success: function(res) {
$('#runjs textarea').val(res)
}
})
$('#LAY_demo_run').on('click', function() {
var ifr = document.getElementById("runjsDemo");
var code = window.editor.getValue();
ifr.contentWindow.document.body.innerHTML = "";
ifr.contentWindow.document.write(code);
})

// 在线运行 resize
var dict = {}, _BODY=$('body'), _DOC = $(document), _HANDLE = $('.resize-handle'), _COVER = $('.resize-cover');
$('.editArea').on('mousemove', function(e){
var othis = $(this)
,oLeft = othis.offset().left
,pLeft = e.clientX - oLeft;
dict.allowResize = othis.width() - pLeft <= 15; //是否处于拖拽允许区域
_BODY.css('cursor', (dict.allowResize ? 'col-resize' : ''));
_HANDLE.css('display', (dict.allowResize ? 'block' : 'none'))

}).on('mouseleave', function(){
var othis = $(this);
if(dict.resizeStart) return;
_BODY.css('cursor', '');
_HANDLE.hide();
_COVER.hide();
}).on('mousedown', function(e){
var othis = $(this);
if(dict.allowResize){
e.preventDefault();
_COVER.show();
_HANDLE.show();
dict.resizeStart = true; //开始拖拽
dict.offset = [e.clientX, e.clientY]; //记录初始坐标
dict.ruleWidth = othis.width()
dict.othis = othis
}
});

//拖拽中
_DOC.on('mousemove', function(e){
if(dict.resizeStart){
e.preventDefault();
if(dict.othis){
var setWidth = dict.ruleWidth + e.clientX - dict.offset[0];
dict.othis.css('width', setWidth + 'px');
}
}
}).on('mouseup', function(e){
if(dict.resizeStart){
dict = {};
_BODY.css('cursor', '');
_HANDLE.hide();
_COVER.hide();
}
});
})
</script>
<div id="app"></div>
<div id="runjsParent">
Expand All @@ -99,87 +175,23 @@
</div>
</div>
</div>
<script src="layui/layui.js"></script>
<div id="commonTpl"></div>

<script src="codemirror/codemirror.js"></script>
<script src="codemirror/selection-pointer.js"></script>
<script src="codemirror/xml.js"></script>
<script src="codemirror/javascript.js"></script>
<script src="codemirror/css.js"></script>
<script src="codemirror/htmlmixed.js"></script>
<script type="text/javascript" src="app.d5f576d.js"></script></body>
<script type="text/javascript" src="app.233ed18.js"></script></body>
<script>
// 自定义模块
layui.config({
base: 'ext/', // 模块目录
version: 'v1.5.15'
}).extend({ // 模块别名
soulTable: 'soulTable'
});
var $, layer;
layui.use(['jquery', 'layer'], function() {
$ = layui.$;
layer = layui.layer;
$.ajax({
url: 'runjs.html',
dataType: 'html',
success: function(res) {
$('#runjs textarea').val(res)
}
})
$('#LAY_demo_run').on('click', function() {
var ifr = document.getElementById("runjsDemo");
var code = window.editor.getValue();
ifr.contentWindow.document.body.innerHTML = "";
ifr.contentWindow.document.write(code);
})

// 在线运行 resize
var dict = {}, _BODY=$('body'), _DOC = $(document), _HANDLE = $('.resize-handle'), _COVER = $('.resize-cover');
$('.editArea').on('mousemove', function(e){
var othis = $(this)
,oLeft = othis.offset().left
,pLeft = e.clientX - oLeft;
dict.allowResize = othis.width() - pLeft <= 15; //是否处于拖拽允许区域
_BODY.css('cursor', (dict.allowResize ? 'col-resize' : ''));
_HANDLE.css('display', (dict.allowResize ? 'block' : 'none'))

}).on('mouseleave', function(){
var othis = $(this);
if(dict.resizeStart) return;
_BODY.css('cursor', '');
_HANDLE.hide();
_COVER.hide();
}).on('mousedown', function(e){
var othis = $(this);
if(dict.allowResize){
e.preventDefault();
_COVER.show();
_HANDLE.show();
dict.resizeStart = true; //开始拖拽
dict.offset = [e.clientX, e.clientY]; //记录初始坐标
dict.ruleWidth = othis.width()
dict.othis = othis
}
});

//拖拽中
_DOC.on('mousemove', function(e){
if(dict.resizeStart){
e.preventDefault();
if(dict.othis){
var setWidth = dict.ruleWidth + e.clientX - dict.offset[0];
dict.othis.css('width', setWidth + 'px');
}
}
}).on('mouseup', function(e){
if(dict.resizeStart){
dict = {};
_BODY.css('cursor', '');
_HANDLE.hide();
_COVER.hide();
}
});
})
function HTMLDecode(text) {
var temp = document.createElement("div");
temp.innerHTML = text;
var output = temp.innerText || temp.textContent;
temp = null;
return output;
}
function showRunJs(runJsTpl) {
layer.open({
type: 1,
Expand Down
1 change: 1 addition & 0 deletions docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"cross-env": "^5.2.0",
"css-loader": "^3.0.0",
"element-ui": "^2.10.1",
"escape-html": "^1.0.3",
"file-loader": "^4.0.0",
"gulp": "^4.0.2",
"gulp-uglify": "^3.0.2",
Expand Down
4 changes: 4 additions & 0 deletions documents/docs/zh-CN/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@

## 更新日志

### **1.5.16** <small>`2020-07-19`</small>

[文档] 修复 layui 的 `tpl` 写法与 vue 渲染冲突的问题的

### **1.5.16** <small>`2020-07-18`</small>

[修复] 显示/隐藏列 修复显示隐藏列时会递归到子表,导致子表相同 `field` 的列会被同步隐藏
Expand Down
37 changes: 37 additions & 0 deletions documents/docs/zh-CN/child/custom.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
## 自定义展开内容

### 1.展示父表当前行数据
:::demo
```html
<table id="myTable" lay-filter="myTable"></table>
<script type="text/html" id="titleTpl">
<a href="/detail/{{d.id}}" class="layui-table-link">{{d.title}}</a>
</script>
<script>
layui.use(['form', 'table','soulTable'], function () {
var table = layui.table,
soulTable = layui.soulTable;
table.render({
elem: '#myTable'
,url: 'data-1.json'
,height: 500
,page: false
,cols: [[
{title: '#', width: 50, children: '#expandAll'},
{field: 'title', title: '诗词', width: 200, sort: true, filter: true},
{field: 'dynasty', title: '朝代', width: 100, sort: true, filter: true},
{field: 'author', title: '作者', width: 165 , filter: true},
{field: 'content', title: '内容', width: 123, filter: true},
{field: 'type', title: '类型', width: 112, filter: {split:','}, sort:true},
{field: 'heat', title: '点赞数', width: 112, filter: true, sort:true},
{field: 'createTime', title: '录入时间', width: 165, filter: {type: 'date[yyyy-MM-dd HH:mm:ss]'}, sort:true},
]]
,done: function () {
soulTable.render(this)
}
});
})
</script>
```
:::
5 changes: 4 additions & 1 deletion documents/docs/zh-CN/filter/basic.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
:::demo 通过 `filter: true` 开启筛选
```html
<table id="myTable" lay-filter="myTable"></table>
<script type="text/html" id="toolbar">
<a class="layui-btn layui-btn-sm" lay-event="clearFilter">清除所有筛选条件</a>
</script>
<script>
layui.use(['form', 'table','soulTable'], function () {
var table = layui.table,
Expand All @@ -14,7 +17,7 @@ layui.use(['form', 'table','soulTable'], function () {
,id: 'myTable'
,url: 'data.json'
,height: 500
,toolbar: '<div><a class="layui-btn layui-btn-sm" lay-event="clearFilter">清除所有筛选条件</a></div>'
,toolbar: '#toolbar'
,page: false
,cols: [[
{type: 'checkbox', fixed: 'left'},
Expand Down
11 changes: 9 additions & 2 deletions documents/docs/zh-CN/filter/page.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
:::demo
```html
<table id="myTable" lay-filter="myTable"></table>
<script type="text/html" id="toolbar">
<a class="layui-btn layui-btn-sm" lay-event="refresh">重载</a>
<a class="layui-btn layui-btn-sm" lay-event="clearFilter">清除所有筛选条件</a>
</script>
<script>
layui.use(['form', 'table','soulTable'], function () {
var table = layui.table,
Expand All @@ -17,7 +21,7 @@ layui.use(['form', 'table','soulTable'], function () {
,height: 500
,limit: 20
,page: true
,toolbar: '<div><a class="layui-btn layui-btn-sm" lay-event="refresh">重载</a><a class="layui-btn layui-btn-sm" lay-event="clearFilter">清除所有筛选条件</a></div>'
,toolbar: '#toolbar'
,cols: [[
{type: 'checkbox', fixed: 'left'},
{field: 'title', title: '诗词', width: 200, sort: true, filter: true},
Expand Down Expand Up @@ -103,6 +107,9 @@ layui.use(['form', 'table','soulTable'], function () {
</div>
</div>
<table id="myTable2" lay-filter="myTable2"></table>
<script type="text/html" id="toolbar2">
<a class="layui-btn layui-btn-sm" lay-event="clearFilter">清除所有筛选条件</a>
</script>
<script>
layui.use(['form', 'table','soulTable'], function () {
var table = layui.table,
Expand All @@ -113,7 +120,7 @@ layui.use(['form', 'table','soulTable'], function () {
id: 'myTable2'
,elem: '#myTable2'
,url: 'https://soultable.saodiyang.com/back/poetry/dataGrid'
,toolbar: '<div><a class="layui-btn layui-btn-sm" lay-event="clearFilter">清除所有筛选条件</a></div>'
,toolbar: '#toolbar2'
,height: 500
,limit: 20
,page: true
Expand Down
1 change: 1 addition & 0 deletions documents/entry.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ const router = new VueRouter({
})
router.beforeEach(async(to, from, next) => {
if (layui.tableFilter) {
layui.$('#commonTpl').html('')
layui.tableFilter.destroy([{
config:{id: 'myTable'}
},{
Expand Down
Loading

0 comments on commit 277186b

Please sign in to comment.