瀏覽代碼

very shitty state

Benoît Hubert 8 年之前
父節點
當前提交
c0707054f3
共有 8 個文件被更改,包括 325 次插入287 次删除
  1. 二進制
      css/fonts/icomoon.eot
  2. 1 0
      css/fonts/icomoon.svg
  3. 二進制
      css/fonts/icomoon.ttf
  4. 二進制
      css/fonts/icomoon.woff
  5. 32 4
      css/styles.css
  6. 13 16
      html/index.mustache.html
  7. 263 263
      js/editor.js
  8. 16 4
      sandboxApp.js

二進制
css/fonts/icomoon.eot


文件差異過大導致無法顯示
+ 1 - 0
css/fonts/icomoon.svg


二進制
css/fonts/icomoon.ttf


二進制
css/fonts/icomoon.woff


+ 32 - 4
css/styles.css

@@ -80,7 +80,9 @@ body {
 
 /* jQuery resizable: https://codepen.io/rstrahl/pen/eJZQej */
 /* horizontal panel*/
-
+html, body {
+  height: 100%;
+}
 .panel-container {
   display: flex;
   flex-direction: row;
@@ -94,6 +96,7 @@ body {
 }
 
 .panel-left {
+  position: relative;
   flex: 0 0 auto;
   /* only manually resize */
   padding: 10px;
@@ -103,7 +106,7 @@ body {
   min-width: 150px;
   white-space: nowrap;
   background: #fff;
-  color: white;
+  color: #555;
 }
 
 .splitter {
@@ -197,10 +200,11 @@ button:hover {
 #editor {
   font-size: 90%;
   position: absolute;
-  top: 390px;
-  right: 500px;
+  top: 110px;
+  /*right: 500px;*/
   bottom: 0;
   left: 0;
+  width: 40%;
   border-top: 1px solid #aaa;
   border-bottom: 1px solid #aaa;
 }
@@ -248,3 +252,27 @@ button:hover {
 .nav-menu a:hover {
   color: #7af;
 }
+
+
+#tabs li {
+  display: inline;
+  box-sizing: border-box;
+  padding: 4px;
+  border-top: 1px solid #ddd;
+  border-left: 1px solid #ddd;
+  border-right: 1px solid #ddd;
+  margin-left: 5px;
+  font-size: 16px;
+  position: absolute;
+  top: 90px;
+  left: 0;
+}
+.tab-html {
+  background: #cef;
+}
+.tab-js {
+  background: #ffb;
+}
+.tab-css {
+  background: #fce;
+}

+ 13 - 16
html/index.mustache.html

@@ -1,7 +1,7 @@
 <!DOCTYPE html>
 <html lang="en">
 <head>
-<title>{{title}} - Web sandbox</title>
+<title>Web sandbox</title>
     <link rel="stylesheet" href="/css/normalize.css">
     <link rel="stylesheet" href="/css/main.css">
 <!--     <link rel="stylesheet" href="css/qunit-2.4.1.css"> -->
@@ -41,22 +41,19 @@
                 <button type="button" id="add-example-cancel" class="icon-cross rounded red"></button><!--
                 --><button type="submit" id="add-example-save" class="icon-checkmark rounded green"></button>
             </form>
-            <nav id="site-menu">
-
-            </nav>
-            <nav id="selector">
-                <select id="file-select">
-                    <option value="-">&mdash;</option>
-                </select>
-            </nav>
-            <nav id="tabs">
-                <ul>
-                    <li><button id="show-html">HTML</button></li>
-                    <li><button id="show-javascript">JS</button></li>
-                    <li><button id="show-css">CSS</button></li>
-                </ul>
-            </nav>
             <button id="save-changes" class="icon-cloud-upload green"></button>
+
+
+            <ul id="tabs">{{#html}}
+                <li class="tab-html">{{name}}</li>
+            {{/html}}
+            {{#js}}
+                <li class="tab-js">{{name}}</li>
+            {{/js}}
+            {{#css}}
+                <li class="tab-css">{{name}}</li>
+            {{/css}}
+            </ul>
         </div>
         <div id="editor"></div>
         <script type="text/html" id="editor-javascript"></script>

+ 263 - 263
js/editor.js

@@ -28,267 +28,267 @@ $(document).ready(function() {
   var exampleList;
 
 
-  $('#menu-btn').click(() => {
-    $('#nav-menus').toggleClass('in');
-  });
-
-
-  /**
-   * Make the left panel resizable
-   */
-  $panelLeft.resizable({
-    handleSelector: ".splitter",
-    resizeHeight: false,
-    onDrag: function(e) {
-      $editor.width($panelLeft.width());
-    }
-  });
-
-
-  /**
-   * Handle window resize
-   */
-  function onResize() {
-    $editor.width($panelLeft.width());
-    $panelWrap.height($window.height());
-  }
-  $window.resize(onResize);
-  onResize();
-
-
-  editor = ace.edit("editor");
-  editor.setTheme("ace/theme/eclipse");
-  editor.$blockScrolling = Infinity;
-  editor.getSession().setUseWrapMode(true);
-
-
-  /**
-   * Set the current hash. If none given, take it from lococation.hash
-   */
-  function setCurrentHash(slug) {
-    if(slug) {
-      window.location.hash = currentHash = slug;
-    }
-    else {
-      currentHash = window.location.hash ?
-          window.location.hash.substr(1) : undefined;
-    }
-  }
-
-
-  /**
-   * Set editor mode (html, javascript, css)
-   */
-  function setEditorMode(mode) {
-    editor.getSession().setMode("ace/mode/" + mode);
-  }
-
-
-  /**
-   * Save a copy in localStorage
-   */
-  function saveToLocalStorage() {
-    var editorContent =  editor.getSession().getValue();
-    editorStorage.saveSource(activeMode, editorContent);
-    saveTimeout1 = undefined;
-  }
-
-
-  /**
-   * React to changes in editor by saving a copy
-   */
-  function editorContentChanged() {
-    // console.log('editorContent changed')
-    if(saveTimeout1 || saveTimeout2) {
-      clearTimeout(saveTimeout1);
-      clearTimeout(saveTimeout2);
-    }
-    saveTimeout1 = setTimeout(saveToLocalStorage, 500);
-    // saveTimeout2 = setTimeout(saveChanges, 1000);
-  }
-
-  editor.getSession().on('change', editorContentChanged);
-
-  function setActiveTab(mode) {
-    console.log('setting mode', mode);
-    var elementId = 'show-' + mode;
-    $('#show-' + activeMode).removeClass('active');
-    activeMode = mode;
-    $('#' + elementId).addClass('active');
-    var ed = $('#editor-' + mode);
-    setEditorMode(mode);
-    editor.getSession().off('change');
-    editor.getSession().setValue(ed[0].innerHTML);
-    editor.getSession().on('change', editorContentChanged);
-  }
-
-  $('#tabs button').click(function() {
-    saveToLocalStorage();
-    var mode = $(this).prop('id').substr(5);
-    setActiveTab(mode);
-  })
-
-
-
-  function loadExample(exampleSlug) {
-    // console.log('loadExample', exampleSlug);
-    var serverPath = 'exemples/jquery/' + exampleSlug + '/';
-    rp.get(serverPath + 'script.js', 'text')
-    .then(javascript => $editorJs.html(javascript))
-    .then(() => rp.get(serverPath + 'example.html', 'text'))
-    .then(() => rp.get(serverPath + 'styles.css', 'text')
-      .then(css => $editorCss.html(css))
-      .catch(err => {
-        return '';
-      })
-    )
-    .then(html => {
-      $editorHtml.html(html);
-      $('iframe.panel-right')
-      .prop('src', '/examples/' + exampleSlug)
-      setActiveTab('html');
-      setCurrentHash(exampleSlug);
-      var sources = {
-        html: $editorHtml.html(),
-        javascript: $editorJs.html()
-      };
-      editorStorage.init(exampleSlug, sources);
-    })
-    .then(() => {
-      var item = _.find(exampleList, { slug: exampleSlug });
-      // console.log(item.test ? 'test' : 'no test');
-      // loadJS('exemples/' + item.slug + '/test.js', function() {
-      //   $('#tests').show();
-      // });
-    });
-
-  }
-
-
-  /**
-   * Build select option string
-   */
-  function makeFileSelectOption(item) {
-    return '<option value="' + item.slug + '">' +
-        item.title +
-      '</option>'
-  }
-
-
-  /**
-   * Populate file selector from JSON list content
-   */
-  function resetFileSelect(exampleList) {
-    $fileSelect.html(
-      '<option value="-">&mdash;</option>' +
-      exampleList.map(makeFileSelectOption)
-    );
-  }
-
-  function loadExampleList() {
-    rp.get('/list/jquery', 'json')
-    .then(function(_exampleList) {
-      exampleList = _exampleList;
-      var restoredDraft;
-      resetFileSelect(exampleList);
-      if(currentHash) {
-        $fileSelect.val(currentHash);
-        var item = _.find(exampleList, { slug: currentHash });
-        if( ! item) {
-          return;
-        }
-        restoredDraft = editorStorage.restore(item.slug);
-        if(! restoredDraft) {
-          loadExample(item.slug);
-        }
-        else {
-          console.log('restore', item.slug, restoredDraft.sources);
-          $editorHtml.html(restoredDraft.sources.html);
-          $editorCss.html(restoredDraft.sources.css);
-          $editorJs.html(restoredDraft.sources.javascript);
-          $('iframe.panel-right')
-          .prop('src', '/examples/' + item.slug)
-
-          // loadJS('exemples/' + item.slug + '/script.js');
-          // if(item.test) {
-          //   loadJS('exemples/' + item.slug + '/test.js', function() {
-          //     $('#tests').show();
-          //   });
-          // }
-          setActiveTab('html');
-        }
-      }
-    });
-  }
-
-  function notify(type, text) {
-    $notification
-    .addClass(type)
-    .addClass('active');
-    $notification.html(text);
-    setTimeout(function() {
-      $notification.removeClass('active');
-    }, 2000);
-    setTimeout(function() {
-      $notification.removeClass(type);
-    }, 3000);
-  }
-
-  function toggleEditor() {
-    $addExampleBtn.toggle();
-    $selectorNav.toggle();
-    $exampleForm.toggle();
-  }
-
-
-  /**
-   * Save new example
-   */
-  function saveExample(e) {
-    e.preventDefault();
-    var title = $(this).find('input[name="title"]').val();
-    rp.post('/examples', { title: title })
-    .then(function(newExample) {
-      clearAndCloseEditor();
-      $fileSelect.append(makeFileSelectOption(newExample));
-      $fileSelect.val(newExample.slug);
-      notify('success', "Exemple créé !");
-    })
-    .catch(errText => notify('error', 'Erreur: ' + errText));
-  }
-
-  function clearAndCloseEditor() {
-    $exampleForm.find('input').val('');
-    toggleEditor();
-  }
-
-  function revertEditor() {
-    editorStorage.reset();
-    location.reload();
-  }
-
-  function saveChanges() {
-    var payload = editorStorage.getSources();
-    rp.put('/examples/' + currentHash, payload)
-    .then(function(newExample) {
-      notify('success', "Exemple sauvegardé !");
-      loadExample(currentHash);
-    })
-    .catch(function(err) {
-      notify('error', 'Erreur: ' + jqXHR.responseText);
-    });
-  }
-
-  setCurrentHash();
-
-  $fileSelect.change(function() {
-    loadExample($(this).val());
-  });
-  $addExampleBtn.click(toggleEditor);
-  $exampleCancel.click(clearAndCloseEditor);
-  $saveChanges.click(saveChanges);
-  $exampleForm.submit(saveExample);
-  $revertEditor.click(revertEditor);
-  $.get('/menu', menu => $('#site-menu').html(menu), 'html');
-  loadExampleList();
+  // $('#menu-btn').click(() => {
+  //   $('#nav-menus').toggleClass('in');
+  // });
+
+
+  // /**
+  //  * Make the left panel resizable
+  //  */
+  // $panelLeft.resizable({
+  //   handleSelector: ".splitter",
+  //   resizeHeight: false,
+  //   onDrag: function(e) {
+  //     $editor.width($panelLeft.width());
+  //   }
+  // });
+
+
+  // /**
+  //  * Handle window resize
+  //  */
+  // function onResize() {
+  //   $editor.width($panelLeft.width());
+  //   $panelWrap.height($window.height());
+  // }
+  // $window.resize(onResize);
+  // onResize();
+
+
+  // editor = ace.edit("editor");
+  // editor.setTheme("ace/theme/eclipse");
+  // editor.$blockScrolling = Infinity;
+  // editor.getSession().setUseWrapMode(true);
+
+
+  // /**
+  //  * Set the current hash. If none given, take it from lococation.hash
+  //  */
+  // function setCurrentHash(slug) {
+  //   if(slug) {
+  //     window.location.hash = currentHash = slug;
+  //   }
+  //   else {
+  //     currentHash = window.location.hash ?
+  //         window.location.hash.substr(1) : undefined;
+  //   }
+  // }
+
+
+  // /**
+  //  * Set editor mode (html, javascript, css)
+  //  */
+  // function setEditorMode(mode) {
+  //   editor.getSession().setMode("ace/mode/" + mode);
+  // }
+
+
+  // /**
+  //  * Save a copy in localStorage
+  //  */
+  // function saveToLocalStorage() {
+  //   var editorContent =  editor.getSession().getValue();
+  //   editorStorage.saveSource(activeMode, editorContent);
+  //   saveTimeout1 = undefined;
+  // }
+
+
+  // /**
+  //  * React to changes in editor by saving a copy
+  //  */
+  // function editorContentChanged() {
+  //   // console.log('editorContent changed')
+  //   if(saveTimeout1 || saveTimeout2) {
+  //     clearTimeout(saveTimeout1);
+  //     clearTimeout(saveTimeout2);
+  //   }
+  //   saveTimeout1 = setTimeout(saveToLocalStorage, 500);
+  //   // saveTimeout2 = setTimeout(saveChanges, 1000);
+  // }
+
+  // editor.getSession().on('change', editorContentChanged);
+
+  // function setActiveTab(mode) {
+  //   console.log('setting mode', mode);
+  //   var elementId = 'show-' + mode;
+  //   $('#show-' + activeMode).removeClass('active');
+  //   activeMode = mode;
+  //   $('#' + elementId).addClass('active');
+  //   var ed = $('#editor-' + mode);
+  //   setEditorMode(mode);
+  //   editor.getSession().off('change');
+  //   editor.getSession().setValue(ed[0].innerHTML);
+  //   editor.getSession().on('change', editorContentChanged);
+  // }
+
+  // $('#tabs button').click(function() {
+  //   saveToLocalStorage();
+  //   var mode = $(this).prop('id').substr(5);
+  //   setActiveTab(mode);
+  // })
+
+
+
+  // function loadExample(exampleSlug) {
+  //   // console.log('loadExample', exampleSlug);
+  //   var serverPath = 'exemples/jquery/' + exampleSlug + '/';
+  //   rp.get(serverPath + 'script.js', 'text')
+  //   .then(javascript => $editorJs.html(javascript))
+  //   .then(() => rp.get(serverPath + 'example.html', 'text'))
+  //   .then(() => rp.get(serverPath + 'styles.css', 'text')
+  //     .then(css => $editorCss.html(css))
+  //     .catch(err => {
+  //       return '';
+  //     })
+  //   )
+  //   .then(html => {
+  //     $editorHtml.html(html);
+  //     $('iframe.panel-right')
+  //     .prop('src', '/examples/' + exampleSlug)
+  //     setActiveTab('html');
+  //     setCurrentHash(exampleSlug);
+  //     var sources = {
+  //       html: $editorHtml.html(),
+  //       javascript: $editorJs.html()
+  //     };
+  //     editorStorage.init(exampleSlug, sources);
+  //   })
+  //   .then(() => {
+  //     var item = _.find(exampleList, { slug: exampleSlug });
+  //     // console.log(item.test ? 'test' : 'no test');
+  //     // loadJS('exemples/' + item.slug + '/test.js', function() {
+  //     //   $('#tests').show();
+  //     // });
+  //   });
+
+  // }
+
+
+  // /**
+  //  * Build select option string
+  //  */
+  // function makeFileSelectOption(item) {
+  //   return '<option value="' + item.slug + '">' +
+  //       item.title +
+  //     '</option>'
+  // }
+
+
+  // /**
+  //  * Populate file selector from JSON list content
+  //  */
+  // function resetFileSelect(exampleList) {
+  //   $fileSelect.html(
+  //     '<option value="-">&mdash;</option>' +
+  //     exampleList.map(makeFileSelectOption)
+  //   );
+  // }
+
+  // function loadExampleList() {
+  //   rp.get('/list/jquery', 'json')
+  //   .then(function(_exampleList) {
+  //     exampleList = _exampleList;
+  //     var restoredDraft;
+  //     resetFileSelect(exampleList);
+  //     if(currentHash) {
+  //       $fileSelect.val(currentHash);
+  //       var item = _.find(exampleList, { slug: currentHash });
+  //       if( ! item) {
+  //         return;
+  //       }
+  //       restoredDraft = editorStorage.restore(item.slug);
+  //       if(! restoredDraft) {
+  //         loadExample(item.slug);
+  //       }
+  //       else {
+  //         console.log('restore', item.slug, restoredDraft.sources);
+  //         $editorHtml.html(restoredDraft.sources.html);
+  //         $editorCss.html(restoredDraft.sources.css);
+  //         $editorJs.html(restoredDraft.sources.javascript);
+  //         $('iframe.panel-right')
+  //         .prop('src', '/examples/' + item.slug)
+
+  //         // loadJS('exemples/' + item.slug + '/script.js');
+  //         // if(item.test) {
+  //         //   loadJS('exemples/' + item.slug + '/test.js', function() {
+  //         //     $('#tests').show();
+  //         //   });
+  //         // }
+  //         setActiveTab('html');
+  //       }
+  //     }
+  //   });
+  // }
+
+  // function notify(type, text) {
+  //   $notification
+  //   .addClass(type)
+  //   .addClass('active');
+  //   $notification.html(text);
+  //   setTimeout(function() {
+  //     $notification.removeClass('active');
+  //   }, 2000);
+  //   setTimeout(function() {
+  //     $notification.removeClass(type);
+  //   }, 3000);
+  // }
+
+  // function toggleEditor() {
+  //   $addExampleBtn.toggle();
+  //   $selectorNav.toggle();
+  //   $exampleForm.toggle();
+  // }
+
+
+  // /**
+  //  * Save new example
+  //  */
+  // function saveExample(e) {
+  //   e.preventDefault();
+  //   var title = $(this).find('input[name="title"]').val();
+  //   rp.post('/examples', { title: title })
+  //   .then(function(newExample) {
+  //     clearAndCloseEditor();
+  //     $fileSelect.append(makeFileSelectOption(newExample));
+  //     $fileSelect.val(newExample.slug);
+  //     notify('success', "Exemple créé !");
+  //   })
+  //   .catch(errText => notify('error', 'Erreur: ' + errText));
+  // }
+
+  // function clearAndCloseEditor() {
+  //   $exampleForm.find('input').val('');
+  //   toggleEditor();
+  // }
+
+  // function revertEditor() {
+  //   editorStorage.reset();
+  //   location.reload();
+  // }
+
+  // function saveChanges() {
+  //   var payload = editorStorage.getSources();
+  //   rp.put('/examples/' + currentHash, payload)
+  //   .then(function(newExample) {
+  //     notify('success', "Exemple sauvegardé !");
+  //     loadExample(currentHash);
+  //   })
+  //   .catch(function(err) {
+  //     notify('error', 'Erreur: ' + jqXHR.responseText);
+  //   });
+  // }
+
+  // setCurrentHash();
+
+  // $fileSelect.change(function() {
+  //   loadExample($(this).val());
+  // });
+  // $addExampleBtn.click(toggleEditor);
+  // $exampleCancel.click(clearAndCloseEditor);
+  // $saveChanges.click(saveChanges);
+  // $exampleForm.submit(saveExample);
+  // $revertEditor.click(revertEditor);
+  // $.get('/menu', menu => $('#site-menu').html(menu), 'html');
+  // loadExampleList();
 });

+ 16 - 4
sandboxApp.js

@@ -7,6 +7,7 @@ var slug         = require('slug');
 var beautify     = require("json-beautify");
 var _            = require('lodash');
 var fs           = require('fs');
+// var path         = require('path');
 var Promise      = require('bluebird');
 var Mustache     = require('mustache');
 var app          = express();
@@ -39,10 +40,18 @@ function readFileAsync(file) {
   .then(buf => (buf.toString()));
 }
 
-function readFilesAsync(path, files) {
+function readFilesAsync(fullPath, files) {
   // console.log('reading files', files, 'from path', path);
-  return Promise.map(files,
-    f => readFileAsync(path + '/' + f)
+  return Promise.reduce(files,
+    (carry, f) => readFileAsync(fullPath + '/' + f)
+      .then(content => 
+        (carry.concat([{
+          // type: path.extname(f).substr(1),
+          name: f,
+          content
+        }]))
+      ),
+    []
   );
 }
 
@@ -119,7 +128,10 @@ app.get('/:repoSlug/:exampleSlug',
       res.send(Mustache.render(indexTpml, {
         // title,
         menuRepo,
-        menuExample
+        menuExample,
+        html,
+        js,
+        css
       }));
     });
 });