Explorar o código

feat:新增json转table工具

lifei6671 %!s(int64=7) %!d(string=hai) anos
pai
achega
835a8a15d0
Modificáronse 3 ficheiros con 118 adicións e 7 borrados
  1. 59 6
      static/js/array.js
  2. 35 1
      static/js/markdown.js
  3. 24 0
      views/document/markdown_edit_template.tpl

+ 59 - 6
static/js/array.js

@@ -6,19 +6,25 @@ Array.prototype.remove = function ($callback) {
     var $isFunction = typeof $callback === "function";
 
     var arr = [];
-    for(var $i = 0,$len = this.length; $i < $len;$i ++){
-        if($isFunction){
-            if($callback(this[$i])){
+    for (var $i = 0, $len = this.length; $i < $len; $i++) {
+        if ($isFunction) {
+            if ($callback(this[$i])) {
                 arr.push($i);
             }
-        }else if(this[$i] == $callback){
+        } else if (this[$i] == $callback) {
             arr.push($i);
         }
     }
-    for($i = 0,$len = arr.length; $i < $len;$i++){
-        this.slice($i,1);
+    for ($i = 0, $len = arr.length; $i < $len; $i++) {
+        this.slice($i, 1);
     }
 };
+String.prototype.endWith = function (endStr) {
+    var d = this.length - endStr.length;
+
+    return (d >= 0 && this.lastIndexOf(endStr) === d)
+};
+
 //格式化文件大小
 function formatBytes($size) {
     if (typeof $size === "number") {
@@ -30,3 +36,50 @@ function formatBytes($size) {
     }
     return $size;
 }
+
+/**
+ * 将多维的json转换为一维的json
+ * @param $json
+ * @param $parentKey
+ */
+function foreachJson($json, $parentKey) {
+    var data = {};
+
+    $.each($json, function (key, item) {
+        var cKey = $parentKey;
+
+        if (Array.isArray($json)) {
+            key = "[";
+        }
+
+        if ($parentKey !== undefined && $parentKey !== "" && key !== "") {
+            if($parentKey.endsWith("[")) {
+                cKey = $parentKey + key + "]";
+            } else if (key === "[") {
+                cKey = $parentKey + key;
+            } else {
+                cKey = $parentKey + "." + key;
+            }
+        } else {
+            cKey = key;
+        }
+
+
+        var node = {};
+        node["key"] = cKey;
+        node["type"] = Array.isArray(item) ? "array" : typeof item;
+        node["value"] = item;
+        if (typeof key === "string" && key !== "[") {
+            data[cKey] = node;
+        }
+
+        if (typeof item === "object") {
+            var items = foreachJson(item, cKey);
+            $.each(items,function (k,v) {
+                data[k] = v;
+            });
+        }
+    });
+    return data;
+
+}

+ 35 - 1
static/js/markdown.js

@@ -64,6 +64,20 @@ $(function () {
         }
     });
 
+    function insertToMarkdown(body) {
+        window.isLoad = true;
+        window.editor.insertValue(body);
+        window.editor.setCursor({ line: 0, ch: 0 });
+        resetEditorChanged(true);
+    }
+    function insertAndClearToMarkdown(body) {
+        window.isLoad = true;
+        window.editor.clear();
+        window.editor.insertValue(body);
+        window.editor.setCursor({ line: 0, ch: 0 });
+        resetEditorChanged(true);
+    }
+
     /**
      * 实现标题栏操作
      */
@@ -79,7 +93,9 @@ $(function () {
            $("#documentTemplateModal").modal("show");
        } else if(name === "save-template"){
            $("#saveTemplateModal").modal("show");
-       }else if (name === "sidebar") {
+       } else if(name === 'json'){
+           $("#convertJsonToTableModal").modal("show");
+       } else if (name === "sidebar") {
             $("#manualCategory").toggle(0, "swing", function () {
                 var $then = $("#manualEditorContainer");
                 var left = parseInt($then.css("left"));
@@ -521,4 +537,22 @@ $(function () {
         })
     });
 
+    $("#btnInsertTable").on("click",function () {
+       var content = $("#jsonContent").val();
+       if(content !== "") {
+           try {
+               var jsonObj = $.parseJSON(content);
+               var data = foreachJson(jsonObj,"");
+               var table = "| 参数名称  | 参数类型  | 示例值  |  备注 |\n| ------------ | ------------ | ------------ | ------------ |\n";
+               $.each(data,function (i,item) {
+                    table += "|" + item.key + "|" + item.type + "|" + item.value +"| |\n";
+               });
+                insertToMarkdown(table);
+           }catch (e) {
+               showError("Json 格式错误:" + e.toString(),"#json-error-message");
+               return;
+           }
+       }
+       $("#convertJsonToTableModal").modal("hide");
+    });
 });

+ 24 - 0
views/document/markdown_edit_template.tpl

@@ -94,6 +94,7 @@
             <a href="javascript:;" data-toggle="tooltip" data-title="引用"><i class="fa fa-quote-right item" name="quote" unselectable="on"></i></a>
             <a href="javascript:;" data-toggle="tooltip" data-title="GFM 任务列表"><i class="fa fa-tasks item" name="tasks" aria-hidden="true"></i></a>
             <a href="javascript:;" data-toggle="tooltip" data-title="附件"><i class="fa fa-paperclip item" aria-hidden="true" name="attachment"></i></a>
+            <a href="javascript:;" data-toggle="tooltip" data-title="Json转换为表格"><i class="fa fa-wrench item" aria-hidden="true" name="json"></i></a>
             <a href="javascript:;" data-toggle="tooltip" data-title="模板"><i class="fa fa-tachometer last" name="template"></i></a>
 
         </div>
@@ -390,6 +391,29 @@
         </div>
     </div>
 </div>
+<!--- json转换为表格 -->
+<div class="modal fade" id="convertJsonToTableModal" tabindex="-1" role="dialog" aria-labelledby="convertJsonToTableModalLabel">
+    <div class="modal-dialog" role="document">
+        <div class="modal-content">
+            <form method="post" id="convertJsonToTableForm" class="form-horizontal">
+                <div class="modal-header">
+                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
+                    <h4 class="modal-title">Json转换为表格</h4>
+                </div>
+                <div class="modal-body text-center">
+                        <textarea type="text" name="jsonContent" id="jsonContent" placeholder="Json" class="form-control" style="height: 300px;resize: none"></textarea>
+
+                </div>
+                <div class="modal-footer">
+                    <span id="json-error-message"></span>
+                    <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
+                    <button type="button" class="btn btn-primary" id="btnInsertTable" data-loading-text="保存中...">插入</button>
+                </div>
+            </form>
+        </div>
+    </div>
+</div>
+
 <template id="template-normal">
 {{template "document/template_normal.tpl"}}
 </template>