浏览代码

File save works, still a little issue with tab state after nav

Benoît Hubert 8 年之前
父节点
当前提交
06a3f65b62
共有 3 个文件被更改,包括 30 次插入9 次删除
  1. 14 8
      js/ws-editor.js
  2. 15 0
      sandboxApp.js
  3. 1 1
      test/integration/test-examples/example-repo-1/repo1-example1/config.json

+ 14 - 8
js/ws-editor.js

@@ -119,9 +119,9 @@ $(document).ready(function() {
       if(this.activeTab) {
         this.activeTab.removeClass('bold');
       }
-      console.log('set active tab', idx, this.file);
       var idx = this.fileIdx = _ws.files.indexOf(this.file);
       this.activeTab = this.$elem.find('li:eq(' + idx + ')');
+      console.log('set active tab', idx, this.file, this.activeTab);
       this.activeTab.addClass('bold');
     },
     getActiveTab: function() {
@@ -204,15 +204,21 @@ $(document).ready(function() {
 
   function onSaveChanges() {
     console.log('Saving', this.file, editorSession.getValue())
+    var filename = this.file.name;
+    var payload = {
+      content: editorSession.getValue()
+    };
+    rp.put('/' + _ws.repo.path + '/examples/' + _ws.example.slug + '/files/' + filename, payload)
     // 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);
-    // });
+    .then(function(updatedFile) {
+      console.log(updatedFile);
+      _ws.notify('success', "Fichier " + filename + " sauvegardé !");
+    })
+    .catch(function(err) {
+      console.error(err);
+      _ws.notify('error', 'Erreur: ' + err.message);
+    });
   }
 
   // $fileSelect.change(function() {

+ 15 - 0
sandboxApp.js

@@ -229,6 +229,21 @@ app.post('/:repoSlug/examples/:exampleSlug/files',
   }
 );
 
+app.put('/:repoSlug/examples/:exampleSlug/files/:name',
+  checkRepoExists,
+  checkBodyPropsExist('content'),
+  function(req, res) {
+    const { name } = req.params;
+    const { content } = req.body;
+    const { repoSlug, exampleSlug } = req.params;
+    const re = /^[A-Za-z0-9_\-\.]+$/;
+    const targetDir = examplesDir + '/' + repoSlug + '/' + exampleSlug;
+    const fullPath = targetDir + '/' + name;
+    return fs.writeFileAsync(fullPath, content)
+    .then(() => res.json({ fullPath, content }));
+  }
+);
+
 /**
  * Create a new example for specified repo
  */

+ 1 - 1
test/integration/test-examples/example-repo-1/repo1-example1/config.json

@@ -2,7 +2,7 @@
   "title": "Test Example",
   "category": "test-stuff",
   "html": [ "example.html" ],
-  "js": [ "script.js", "script-ja09bcew.js", "script-ja09cgm1.js", "script-ja09da79.js" ],
+  "js": [ "script.js" ],
   "css": [],
   "libsJs": [],
   "libsCss": [ "styles.css" ]