Add Edit button for editing in codesandbox
This commit is contained in:
@@ -1,4 +1,12 @@
|
|||||||
(function() {
|
(function() {
|
||||||
|
|
||||||
|
function compress(json) {
|
||||||
|
return LZString.compressToBase64(JSON.stringify(json))
|
||||||
|
.replace(/\+/g, `-`)
|
||||||
|
.replace(/\//g, `_`)
|
||||||
|
.replace(/=+$/, ``);
|
||||||
|
}
|
||||||
|
|
||||||
var htmlClipboard = new Clipboard('#copy-html-button');
|
var htmlClipboard = new Clipboard('#copy-html-button');
|
||||||
htmlClipboard.on('success', function(e) {
|
htmlClipboard.on('success', function(e) {
|
||||||
e.clearSelection();
|
e.clearSelection();
|
||||||
@@ -17,34 +25,22 @@
|
|||||||
codepenButton.onclick = function(event) {
|
codepenButton.onclick = function(event) {
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
var form = document.getElementById('codepen-form');
|
var form = document.getElementById('codepen-form');
|
||||||
|
const html = document.getElementById('example-html-source').innerText;
|
||||||
// Doc : https://blog.codepen.io/documentation/api/prefill/
|
const js = document.getElementById('example-js-source').innerText;
|
||||||
|
const pkgJson = document.getElementById('example-pkg-source').innerText;
|
||||||
var resources = form.resources.value.split(',');
|
form.parameters.value = compress({
|
||||||
|
files: {
|
||||||
var data = {
|
'index.html': {
|
||||||
title: form.title.value,
|
content: html
|
||||||
description: form.description.value,
|
},
|
||||||
layout: 'left',
|
'index.js': {
|
||||||
html: form.html.value,
|
content: js
|
||||||
css: form.css.value,
|
},
|
||||||
js: form.js.value,
|
"package.json": {
|
||||||
css_external: resources.filter(function(resource) {
|
content: pkgJson
|
||||||
return resource.lastIndexOf('.css') === resource.length - 4;
|
}
|
||||||
}).join(';'),
|
}
|
||||||
js_external: resources.filter(function(resource) {
|
});
|
||||||
return resource.lastIndexOf('.js') === resource.length - 3;
|
|
||||||
}).join(';')
|
|
||||||
};
|
|
||||||
|
|
||||||
// binary flags to display html, css, js and/or console tabs
|
|
||||||
data.editors = '' + Number(data.html.length > 0) +
|
|
||||||
Number(data.css.length > 0) +
|
|
||||||
Number(data.js.length > 0) +
|
|
||||||
Number(data.js.indexOf('console') > 0);
|
|
||||||
|
|
||||||
form.data.value = JSON.stringify(data);
|
|
||||||
|
|
||||||
form.submit();
|
form.submit();
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -27,6 +27,7 @@
|
|||||||
</script>
|
</script>
|
||||||
<link rel="stylesheet" type="text/css" href="//cdnjs.cloudflare.com/ajax/libs/cookieconsent2/3.1.0/cookieconsent.min.css" />
|
<link rel="stylesheet" type="text/css" href="//cdnjs.cloudflare.com/ajax/libs/cookieconsent2/3.1.0/cookieconsent.min.css" />
|
||||||
<script src="//cdnjs.cloudflare.com/ajax/libs/cookieconsent2/3.1.0/cookieconsent.min.js"></script>
|
<script src="//cdnjs.cloudflare.com/ajax/libs/cookieconsent2/3.1.0/cookieconsent.min.js"></script>
|
||||||
|
<script src="https://unpkg.com/lz-string@1.4.4/libs/lz-string.min.js"></script>
|
||||||
<script>
|
<script>
|
||||||
window.addEventListener("load", function() {
|
window.addEventListener("load", function() {
|
||||||
window.cookieconsent.initialise({
|
window.cookieconsent.initialise({
|
||||||
@@ -99,11 +100,14 @@
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="row-fluid">
|
<div class="row-fluid">
|
||||||
<!-- <a class="codepen-button pull-right"><i class="fa fa-codepen"></i> Edit</a> -->
|
<a class="codepen-button pull-right"><i class="fa fa-codepen"></i> Edit</a>
|
||||||
<div class="span12">
|
<div class="span12">
|
||||||
<h4 id="title">{{ title }}</h4>
|
<h4 id="title">{{ title }}</h4>
|
||||||
{{{ contents }}}
|
{{{ contents }}}
|
||||||
</div>
|
</div>
|
||||||
|
<form method="POST" id="codepen-form" target="_blank" action="https://codesandbox.io/api/v1/sandboxes/define">
|
||||||
|
<input id="codesandbox-params" type="hidden" name="parameters">
|
||||||
|
</form>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="row-fluid">
|
<div class="row-fluid">
|
||||||
|
|||||||
Reference in New Issue
Block a user