feat:新增json转table工具

pull/425/head
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 $isFunction = typeof $callback === "function";
var arr = []; var arr = [];
for(var $i = 0,$len = this.length; $i < $len;$i ++){ for (var $i = 0, $len = this.length; $i < $len; $i++) {
if($isFunction){ if ($isFunction) {
if($callback(this[$i])){ if ($callback(this[$i])) {
arr.push($i); arr.push($i);
} }
}else if(this[$i] == $callback){ } else if (this[$i] == $callback) {
arr.push($i); arr.push($i);
} }
} }
for($i = 0,$len = arr.length; $i < $len;$i++){ for ($i = 0, $len = arr.length; $i < $len; $i++) {
this.slice($i,1); 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) { function formatBytes($size) {
if (typeof $size === "number") { if (typeof $size === "number") {
@ -30,3 +36,50 @@ function formatBytes($size) {
} }
return $size; return $size;
} }
/**
* jsonjson
* @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"); $("#documentTemplateModal").modal("show");
} else if(name === "save-template"){ } else if(name === "save-template"){
$("#saveTemplateModal").modal("show"); $("#saveTemplateModal").modal("show");
}else if (name === "sidebar") { } else if(name === 'json'){
$("#convertJsonToTableModal").modal("show");
} else if (name === "sidebar") {
$("#manualCategory").toggle(0, "swing", function () { $("#manualCategory").toggle(0, "swing", function () {
var $then = $("#manualEditorContainer"); var $then = $("#manualEditorContainer");
var left = parseInt($then.css("left")); 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");
});
}); });

View File

@ -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="引用"><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="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="附件"><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> <a href="javascript:;" data-toggle="tooltip" data-title="模板"><i class="fa fa-tachometer last" name="template"></i></a>
</div> </div>
@ -390,6 +391,29 @@
</div> </div>
</div> </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 id="template-normal">
{{template "document/template_normal.tpl"}} {{template "document/template_normal.tpl"}}
</template> </template>