用树状图展示对象

This commit is contained in:
fofolee 2022-04-28 13:12:03 +08:00
parent 77bd51acdb
commit 715b4bc296
4 changed files with 105 additions and 33 deletions

View File

@ -8,7 +8,6 @@ const axios = require('axios');
const http = require('http'); const http = require('http');
const url = require('url') const url = require('url')
const kill = require('tree-kill') const kill = require('tree-kill')
const beautifyLog = require('./lib/beautifyLog')
require('ses') require('ses')
window._ = require("lodash") window._ = require("lodash")
@ -414,8 +413,6 @@ window.convertFilePathToUtoolsPayload = files => files.map(file => {
} }
}) })
let parseStdout = stdout => stdout.map(x => beautifyLog(x)).join("\n")
let getSandboxFuns = () => { let getSandboxFuns = () => {
var sandbox = { var sandbox = {
fetch: fetch.bind(window), fetch: fetch.bind(window),
@ -466,7 +463,7 @@ window.runCodeInSandbox = (code, callback, addVars = {}) => {
callback(stdout, null) callback(stdout, null)
}, },
error: (...stderr) => { error: (...stderr) => {
callback(null, parseStdout(stderr)) callback(null, stderr)
} }
} }
let sandboxWithAD = Object.assign(addVars, sandbox) let sandboxWithAD = Object.assign(addVars, sandbox)

View File

@ -128,7 +128,9 @@ export default {
? alert(stderr) ? alert(stderr)
: this.showRunResult(stderr, false, action); : this.showRunResult(stderr, false, action);
} }
!outPlugin && this.showRunResult(stdout, true, action); outPlugin
? action(stdout.toString())
: this.showRunResult(stdout, true);
}, },
{ enterData: this.$root.enterData } { enterData: this.$root.enterData }
); );
@ -151,7 +153,9 @@ export default {
? alert(stderr) ? alert(stderr)
: this.showRunResult(stderr, false, action); : this.showRunResult(stderr, false, action);
} }
!outPlugin && this.showRunResult(stdout, true, action); outPlugin
? action(stdout.toString())
: this.showRunResult(stdout, true);
} }
); );
} }
@ -228,20 +232,11 @@ export default {
}; };
}, },
// //
showRunResult(content, isSuccess, action) { showRunResult(content, isSuccess) {
this.isResultShow = true; this.isResultShow = true;
this.timeStamp = new Date().getTime(); this.timeStamp = new Date().getTime();
this.runResultStatus = isSuccess; this.runResultStatus = isSuccess;
// let contlength = content?.length || 0; if (!_.isArray(content)) content = [content];
// if (contlength > this.resultMaxLength)
// content =
// content.slice(0, this.resultMaxLength - 100) +
// `\n\n...\n${
// contlength - this.resultMaxLength - 100
// } \n...\n\n` +
// content.slice(contlength - 100);
// let pretreatment = action(content);
// pretreatment && (this.runResult += pretreatment);
this.runResult = this.runResult.concat(content); this.runResult = this.runResult.concat(content);
this.outputAutoHeight(this.fromUtools); this.outputAutoHeight(this.fromUtools);
}, },

View File

@ -9,14 +9,19 @@
@load="frameLoad" @load="frameLoad"
v-if="showFrame" v-if="showFrame"
></iframe> ></iframe>
<div v-else v-show="!!runResult" :class="{ 'text-red': !runResultStatus }"> <div
v-else
v-show="!!runResult"
:class="{ 'text-red': !runResultStatus }"
class="text q-pa-md"
>
<div v-for="item in runResult" :key="item"> <div v-for="item in runResult" :key="item">
<ObjectTree <ObjectTree
:obj="item" :obj="item"
v-if="typeof item === 'object'" v-if="typeof item === 'object'"
@expandTrees="$emit('expandTrees')" @expandTrees="$emit('expandTrees')"
/> />
<pre class="q-pa-md result" v-text="item" v-else></pre> <pre class="result" v-text="item" v-else></pre>
</div> </div>
</div> </div>
</div> </div>
@ -103,12 +108,14 @@ export default {
</script> </script>
<style scoped> <style scoped>
.text {
font-family: Consolas, Monaco, "Courier New";
}
.result { .result {
white-space: pre-wrap; white-space: pre-wrap;
word-wrap: break-word; word-wrap: break-word;
max-width: 100%; max-width: 100%;
margin: 0; margin: 0;
font-family: Consolas, Monaco, "Courier New";
} }
iframe { iframe {
width: 100%; width: 100%;

View File

@ -1,20 +1,40 @@
<template> <template>
<div class="q-pa-xs q-gutter-sm"> <div class="q-gutter-sm">
<q-tree :nodes="trees" node-key="label" @lazy-load="showChildren" /> <q-tree
:nodes="trees"
no-connectors
node-key="id"
@lazy-load="showChildren"
>
<template v-slot:default-header="prop">
<div class="text-primary">{{ prop.node.key }}</div>
<div v-show="!!prop.node.key" v-text="':'" style="margin-right: 5px" />
<div class="text-italic">{{ prop.node.summary }}</div>
</template></q-tree
>
</div> </div>
</template> </template>
<script> <script>
import { toRaw } from "vue"; import { toRaw } from "vue";
const maxSize = {
txt: 80,
obj: 3,
ary: 3,
buff: 50,
};
export default { export default {
data() { data() {
return { return {
trees: [ trees: [
{ {
label: "object", key: "",
summary: this.liteItem(this.obj),
lazy: true, lazy: true,
item: this.obj, value: this.obj,
id: "root",
}, },
], ],
}; };
@ -24,39 +44,92 @@ export default {
}, },
methods: { methods: {
liteItem(item) { liteItem(item) {
if (item === null) return "null";
if (typeof item === "undefined") return "undefined"; if (typeof item === "undefined") return "undefined";
if (typeof item === "string")
return _.truncate(item, { length: maxSize.txt });
if (typeof item === "number") return item; if (typeof item === "number") return item;
if (typeof item === "function") if (typeof item === "function")
return `[Function: ${item.name ? item.name : "(anonymous)"}]`; return `f ${item.name ? item.name + "()" : "anonymous()"}`;
if (typeof item !== "object") return item.toString(); if (typeof item !== "object") return item.toString();
if (_.isBuffer(item)) { if (_.isBuffer(item)) {
var bufferString = `[Buffer ${item var bufferString = `[Buffer ${item
.slice(0, 50) .slice(0, maxSize.buff)
.toString("hex") .toString("hex")
.match(/\w{1,2}/g) .match(/\w{1,2}/g)
.map((x) => parseInt(x, 16))
.join(" ")}`; .join(" ")}`;
if (item.length > 50) if (item.length > maxSize.buff)
bufferString += `...${(item.length / 1000).toFixed(2)} kb `; bufferString += `...${(item.length / 1000).toFixed(2)} kb `;
return bufferString + "]"; return bufferString + "]";
} }
if (item instanceof ArrayBuffer) return `ArrayBuffer(${item.byteLength})`; if (item instanceof ArrayBuffer) return `ArrayBuffer(${item.byteLength})`;
if (item instanceof Blob) if (item instanceof Blob)
return `Blob { size: ${item.size}, type: "${item.type}" }`; return `Blob { size: ${item.size}, type: "${item.type}" }`;
return "object"; if (item instanceof Array) {
return (
"[" +
item
.map((i) => {
if (typeof i === "function") return `f`;
if (i instanceof Array) return `Array(${i.length})`;
if (i instanceof Object) return `{...}`;
return i;
})
.slice(0, maxSize.ary)
.join(", ") +
(item.length > maxSize.ary ? "..." : "") +
"]"
);
}
let keys = this.getObjKeys(item);
if (keys.length === 0 && item.toString() !== "[object Object]")
return item.toString();
return (
"{" +
keys.slice(0, maxSize.obj).join(", ") +
(keys.length > maxSize.obj ? "..." : "") +
"}"
);
},
getObjKeys(item) {
// Object.keys()
let keys = [];
for (const key in item) {
keys.push(key);
}
return keys;
}, },
showChildren({ node, key, done, fail }) { showChildren({ node, key, done, fail }) {
let children = []; let children = [];
for (let key in node.item) { if (typeof node.value === "string") {
let value = toRaw(node.item)[key];
children.push({ children.push({
label: `${key}: ${this.liteItem(value)}`, summary: node.value,
lazy: typeof value === "object", id: node.id + "." + key,
item: value,
}); });
} else {
for (let key in node.value) {
let value = toRaw(node.value)[key];
children.push({
key: key,
summary: this.liteItem(value),
lazy: this.hasChildren(value),
value: value,
id: node.id + "." + key,
});
}
} }
done(children); done(children);
this.$emit("expandTrees"); this.$emit("expandTrees");
}, },
hasChildren(item) {
if (typeof item === "object")
return item !== null && this.getObjKeys(item).length > 0;
if (typeof item === "string") {
return item.length > maxSize.txt;
}
return false;
},
}, },
}; };
</script> </script>