From 0850e612c5da8c2b1c5ad40591e606c1418cb062 Mon Sep 17 00:00:00 2001 From: Thomas Chandelle Date: Thu, 12 Jan 2017 09:53:05 +0100 Subject: [PATCH] Add link to codepen in examples --- config/examples/example.html | 10 ++++++++++ examples/resources/common.js | 37 +++++++++++++++++++++++++++++++++++ examples/resources/layout.css | 1 + 3 files changed, 48 insertions(+) diff --git a/config/examples/example.html b/config/examples/example.html index 4ef6d52fac..48b3b70770 100644 --- a/config/examples/example.html +++ b/config/examples/example.html @@ -60,6 +60,7 @@
Copy Edit + Edit
@@ -68,6 +69,15 @@
+
+ + + + + + + +
<!DOCTYPE html>
 <html>
   <head>
diff --git a/examples/resources/common.js b/examples/resources/common.js
index 46233307e2..d18dc02133 100644
--- a/examples/resources/common.js
+++ b/examples/resources/common.js
@@ -18,6 +18,43 @@
     };
   }
 
+  var codepenButton = document.getElementById('codepen-button');
+  if (codepenButton) {
+    codepenButton.onclick = function(event) {
+      event.preventDefault();
+      var form = document.getElementById('codepen-form');
+
+      // Doc : https://blog.codepen.io/documentation/api/prefill/
+
+      var resources = form.resources.value.split(',');
+
+      var data = {
+        title: form.title.value,
+        description: form.description.value,
+        layout: 'left',
+        html: form.html.value,
+        css: form.css.value,
+        js: form.js.value,
+        css_external: resources.filter(function(resource) {
+          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();
+    };
+  }
+
   if (window.location.host === 'localhost:3000') {
     return;
   }
diff --git a/examples/resources/layout.css b/examples/resources/layout.css
index e04323098e..1f839e4deb 100644
--- a/examples/resources/layout.css
+++ b/examples/resources/layout.css
@@ -106,6 +106,7 @@ pre[class*="language-"] {
   cursor: pointer;
 }
 
+#codepen-button,
 #jsfiddle-button {
   text-decoration: none;
   cursor: pointer;