mirror of
https://github.com/harttle/liquidjs.git
synced 2026-09-15 04:10:40 -07:00
docs: support data field in playground.html
This commit is contained in:
@@ -1,4 +1,3 @@
|
|||||||
|
|
||||||
{
|
{
|
||||||
"categories": [{
|
"categories": [{
|
||||||
"color": "red",
|
"color": "red",
|
||||||
|
|||||||
@@ -37,7 +37,12 @@ hexo.extend.helper.register('page_nav', function () {
|
|||||||
})
|
})
|
||||||
|
|
||||||
hexo.extend.helper.register('raw', function (filepath) {
|
hexo.extend.helper.register('raw', function (filepath) {
|
||||||
const content = readFileSync(resolve(this.view_dir, filepath))
|
const content = readFileSync(resolve(this.view_dir, filepath), 'utf8')
|
||||||
|
.replace(/&/g, '&')
|
||||||
|
.replace(/</g, '<')
|
||||||
|
.replace(/>/g, '>')
|
||||||
|
.replace(/"/g, '"')
|
||||||
|
.replace(/'/g, ''')
|
||||||
return content
|
return content
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
+5
-3
@@ -2,9 +2,11 @@
|
|||||||
<div class="wrapper">
|
<div class="wrapper">
|
||||||
<h1 class="inner">{{__('playground.title')}}</h1>
|
<h1 class="inner">{{__('playground.title')}}</h1>
|
||||||
<div class="row inner">
|
<div class="row inner">
|
||||||
<div id="editorEl">{{__('playground.loading')}}</div>
|
<div class="col">
|
||||||
<div id="previewEl">{{__('playground.loading')}}</div>
|
<div id="editorEl">{{ raw('partial/demo.liquid') }}</div>
|
||||||
|
<div id="dataEl">{}</div>
|
||||||
|
</div>
|
||||||
|
<div class="col" id="previewEl">{{__('playground.loading')}}</div>
|
||||||
</div>
|
</div>
|
||||||
<script id="sourceEl" type="text/plain">{{ raw('partial/demo.liquid') }}</script>
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
+12
-7
@@ -11,16 +11,21 @@
|
|||||||
flex-wrap: wrap;
|
flex-wrap: wrap;
|
||||||
justify-content: space-between
|
justify-content: space-between
|
||||||
margin-bottom: 30px
|
margin-bottom: 30px
|
||||||
> div
|
font-size: 1em
|
||||||
flex: 0 0 100%
|
.col
|
||||||
height: 400px
|
flex: 1 1 100%
|
||||||
margin-bottom: 30px
|
margin-bottom: 30px
|
||||||
|
|
||||||
@media mq-normal
|
@media mq-normal
|
||||||
.row
|
.row
|
||||||
flex-wrap: nowrap;
|
flex-wrap: nowrap;
|
||||||
> div
|
.col:first-child
|
||||||
flex: 0 1 49%
|
margin-right: 15px
|
||||||
|
|
||||||
#editorEl, #previewEl
|
#editorEl
|
||||||
font-size: 1em
|
height: 40vh
|
||||||
|
#dataEl
|
||||||
|
height: 28vh
|
||||||
|
margin-top: 2vh
|
||||||
|
#previewEl
|
||||||
|
height: 70vh
|
||||||
|
|||||||
Vendored
+22
-9
@@ -57,17 +57,23 @@
|
|||||||
|
|
||||||
(function() {
|
(function() {
|
||||||
// playground
|
// playground
|
||||||
/* global liquidjs, sourceEl, ace */
|
/* global liquidjs, ace */
|
||||||
if (!location.pathname.match(/playground.html$/)) return;
|
if (!location.pathname.match(/playground.html$/)) return;
|
||||||
const engine = new liquidjs.Liquid();
|
const engine = new liquidjs.Liquid();
|
||||||
const editor = createEditor('editorEl', 'liquid');
|
const editor = createEditor('editorEl', 'liquid');
|
||||||
|
const dataEditor = createEditor('dataEl', 'json');
|
||||||
const preview = createEditor('previewEl', 'html');
|
const preview = createEditor('previewEl', 'html');
|
||||||
preview.setReadOnly(true);
|
preview.setReadOnly(true);
|
||||||
preview.renderer.setShowGutter(false);
|
preview.renderer.setShowGutter(false);
|
||||||
preview.renderer.setPadding(20);
|
preview.renderer.setPadding(20);
|
||||||
|
|
||||||
editor.setValue(getInitialValue(), 1);
|
const init = parseArgs(location.hash.slice(1));
|
||||||
|
if (init) {
|
||||||
|
editor.setValue(init.tpl, 1);
|
||||||
|
dataEditor.setValue(init.data, 1);
|
||||||
|
}
|
||||||
editor.on('change', update);
|
editor.on('change', update);
|
||||||
|
dataEditor.on('change', update);
|
||||||
update();
|
update();
|
||||||
|
|
||||||
function createEditor(id, lang) {
|
function createEditor(id, lang) {
|
||||||
@@ -82,18 +88,25 @@
|
|||||||
return editor;
|
return editor;
|
||||||
}
|
}
|
||||||
|
|
||||||
function getInitialValue() {
|
function parseArgs(hash) {
|
||||||
|
if (!hash) return;
|
||||||
try {
|
try {
|
||||||
return atou(location.hash.slice(1)) || sourceEl.textContent
|
let [tpl, data] = hash.split(',').map(atou);
|
||||||
|
data = data || '{}';
|
||||||
|
return { tpl, data };
|
||||||
} catch (e) {}
|
} catch (e) {}
|
||||||
return sourceEl.textContent
|
}
|
||||||
|
|
||||||
|
function serializeArgs(obj) {
|
||||||
|
return utoa(obj.tpl) + ',' + utoa(obj.data);
|
||||||
}
|
}
|
||||||
|
|
||||||
async function update() {
|
async function update() {
|
||||||
const tpl = editor.getValue();
|
const tpl = editor.getValue();
|
||||||
history.replaceState({}, '', '#' + utoa(tpl));
|
const data = dataEditor.getValue();
|
||||||
|
history.replaceState({}, '', '#' + serializeArgs({tpl, data}));
|
||||||
try {
|
try {
|
||||||
const html = await engine.parseAndRender(tpl);
|
const html = await engine.parseAndRender(tpl, JSON.parse(data));
|
||||||
preview.setValue(html, 1);
|
preview.setValue(html, 1);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
preview.setValue(err.stack, 1);
|
preview.setValue(err.stack, 1);
|
||||||
@@ -102,11 +115,11 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
function atou(str) {
|
function atou(str) {
|
||||||
return decodeURIComponent(escape(atob(str)))
|
return decodeURIComponent(escape(atob(str)));
|
||||||
}
|
}
|
||||||
|
|
||||||
function utoa(str) {
|
function utoa(str) {
|
||||||
return btoa(unescape(encodeURIComponent(str)))
|
return btoa(unescape(encodeURIComponent(str)));
|
||||||
}
|
}
|
||||||
}());
|
}());
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user