Przeglądaj źródła

[GinR] Provide error hints for JSON and YAML files

cgars 7 lat temu
rodzic
commit
03e2e3c93f
2 zmienionych plików z 19 dodań i 2 usunięć
  1. 18 2
      public/js/gogs.js
  2. 1 0
      templates/repo/editor/edit.tmpl

+ 18 - 2
public/js/gogs.js

@@ -648,10 +648,26 @@ function setCodeMirror($editArea) {
 
 	codeMirrorEditor.on("keyup", function (cm, change) {
 		if (typeof (jsonEditor)!== 'undefined') {
-			jsoneditor.set(JSON.parse(cm.getValue()));
+			try {
+				jsonEditor.set(JSON.parse(cm.getValue()));
+				document.getElementById("parseerr").innerHTML = "Valid JSON";
+				document.getElementById("parseerr").className="ui positive message"
+			}
+			catch(err) {
+				document.getElementById("parseerr").innerHTML = err.message;
+				document.getElementById("parseerr").className="ui negative message"
+			}
 		}
 		if (typeof (yamleditor)!== 'undefined') {
-			yamleditor.set(YAML.parse(cm.getValue()));
+			try {
+				yamleditor.set(YAML.parse(cm.getValue()));
+				document.getElementById("parseerr").innerHTML = "Valid YAML";
+				document.getElementById("parseerr").className="ui positive message"
+			}
+			catch(err) {
+				document.getElementById("parseerr").innerHTML = err.message;
+				document.getElementById("parseerr").className="ui negative message"
+			}
 		}
 	});
 

+ 1 - 0
templates/repo/editor/edit.tmpl

@@ -41,6 +41,7 @@
 					{{end}}
 				</div>
 				<div class="ui bottom attached active tab segment codewriter" data-tab="write">
+					{{if or .IsYAML .IsJSON}}<div class="ui message" id="parseerr">Change content to check for validity</div>{{end}}
 					<textarea id="edit_area" name="content" data-id="repo-{{.Repository.Name}}-{{.TreePath}}"
 						data-url="{{AppSubURL}}/api/v1/markdown"
 						data-context="{{.RepoLink}}"