feat:新增json转table工具

This commit is contained in:
lifei6671
2018-11-21 18:07:24 +08:00
parent 34c383ea14
commit 835a8a15d0
3 changed files with 118 additions and 7 deletions

View File

@@ -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;
}

View File

@@ -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");
});
});