Browse Source

Round tripping code edits with UIWebView Ace editor

Josh Engebretson 10 years ago
parent
commit
d9c853be10

+ 15 - 2
Data/AtomicEditor/CodeEditor/Editor.html

@@ -29,9 +29,20 @@
 
 
   var editor;
   var editor;
 
 
-  function codeLoaded(value) {
+  function saveCode() {
+
+    var data = {
+        message : "saveCode",
+        payload : editor.getValue()
+    }
 
 
-    document.getElementsByTagName('pre')[0].innerHTML = value;
+    window.atomicQuery({request: JSON.stringify(data),
+    persistent: false,
+    onSuccess: function(response) { },
+    onFailure: function(error_code, error_message) {console.log("Error getting code")}});
+  }
+
+  function codeLoaded(value) {
 
 
     editor = ace.edit("editor");
     editor = ace.edit("editor");
 
 
@@ -44,6 +55,8 @@
     editor.setTheme("ace/theme/monokai");
     editor.setTheme("ace/theme/monokai");
     editor.session.setMode("ace/mode/typescript");
     editor.session.setMode("ace/mode/typescript");
 
 
+    editor.setValue(value);
+
     editor.getSession().on('change', function(e) {
     editor.getSession().on('change', function(e) {
       window.atomicQuery({request: 'change',
       window.atomicQuery({request: 'change',
       persistent: false,
       persistent: false,

+ 4 - 2
Source/Atomic/Resource/JSONFile.cpp

@@ -140,12 +140,14 @@ bool JSONFile::BeginLoad(Deserializer& source)
     return true;
     return true;
 }
 }
 
 
-bool JSONFile::ParseJSON(const String& json, JSONValue& value)
+bool JSONFile::ParseJSON(const String& json, JSONValue& value, bool reportError)
 {
 {
     rapidjson::Document document;
     rapidjson::Document document;
     if (document.Parse<0>(json.CString()).HasParseError())
     if (document.Parse<0>(json.CString()).HasParseError())
     {
     {
-        LOGERROR("Could not parse JSON data from string");
+        if (reportError)
+            LOGERROR("Could not parse JSON data from string");
+
         return false;
         return false;
     }
     }
 
 

+ 1 - 1
Source/Atomic/Resource/JSONFile.h

@@ -55,7 +55,7 @@ public:
 
 
     // ATOMIC BEGIN
     // ATOMIC BEGIN
 
 
-    static bool ParseJSON(const String& json, JSONValue& value);
+    static bool ParseJSON(const String& json, JSONValue& value, bool reportError = true);
 
 
     // ATOMIC END
     // ATOMIC END
 
 

+ 19 - 0
Source/AtomicEditor/Editors/JSResourceEditor.cpp

@@ -11,6 +11,8 @@
 #include <Atomic/IO/File.h>
 #include <Atomic/IO/File.h>
 #include <Atomic/IO/FileSystem.h>
 #include <Atomic/IO/FileSystem.h>
 #include <Atomic/Resource/ResourceCache.h>
 #include <Atomic/Resource/ResourceCache.h>
+#include <Atomic/Resource/JSONFile.h>
+
 #include <Atomic/Core/CoreEvents.h>
 #include <Atomic/Core/CoreEvents.h>
 #include <AtomicJS/Javascript/JSVM.h>
 #include <AtomicJS/Javascript/JSVM.h>
 
 
@@ -82,6 +84,21 @@ void JSResourceEditor::HandleWebMessage(StringHash eventType, VariantMap& eventD
     {
     {
         SetModified(true);
         SetModified(true);
     }
     }
+    else
+    {
+        JSONValue jvalue;
+        if (JSONFile::ParseJSON(request, jvalue, false))
+        {
+            String message = jvalue["message"].GetString();
+            if (message == "saveCode")
+            {
+                String code = jvalue["payload"].GetString();
+                File file(context_, fullpath_, FILE_WRITE);
+                file.Write((void*) code.CString(), code.Length());
+                file.Close();
+            }
+        }
+    }
 
 
     handler->Success();
     handler->Success();
 
 
@@ -135,6 +152,8 @@ bool JSResourceEditor::Save()
     if (!modified_)
     if (!modified_)
         return true;
         return true;
 
 
+    webClient_->ExecuteJavaScript("saveCode();");
+
     SetModified(false);
     SetModified(false);
 
 
     return true;
     return true;