From 45d01c4c737ee1b1add1e411957a60a08e57788d Mon Sep 17 00:00:00 2001 From: zhandouxiaojiji Date: Fri, 8 Jan 2021 16:15:20 +0800 Subject: [PATCH] auto break words --- common/Utils.ts | 36 +++++++++++++++++++++++++--------- render-process/RegisterNode.ts | 23 +++++++++++++--------- 2 files changed, 41 insertions(+), 18 deletions(-) diff --git a/common/Utils.ts b/common/Utils.ts index f6890cb..55f3100 100644 --- a/common/Utils.ts +++ b/common/Utils.ts @@ -10,28 +10,28 @@ export const cloneNodeData = (nodeData: GraphNodeModel) => { name: nodeData.name, desc: nodeData.desc, }; - if(nodeData.input) { + if (nodeData.input) { newData.input = []; - for(let v of nodeData.input){ + for (let v of nodeData.input) { newData.input.push(v || ""); } } - if(nodeData.output) { + if (nodeData.output) { newData.output = []; - for(let v of nodeData.output){ + for (let v of nodeData.output) { newData.output.push(v || ""); } } - if(nodeData.args) { + if (nodeData.args) { newData.args = {}; - for(let k in nodeData.args) { + for (let k in nodeData.args) { let v = nodeData.args[k]; newData.args[k] = v; } } - if(nodeData.children) { + if (nodeData.children) { newData.children = [] - for(let child of nodeData.children) { + for (let child of nodeData.children) { newData.children.push(cloneNodeData(child)); } } @@ -55,7 +55,7 @@ export const calcTreeNodeSize = (treeNode: GraphNodeModel) => { var height = 40; const updateHeight = (obj: any) => { if (Array.isArray(obj) || (obj && Object.keys(obj).length > 0)) { - height += 25; + height += 30; } }; updateHeight(treeNode.conf.args); @@ -155,3 +155,21 @@ export const createNewTree = (filename: string) => { }; return tree; }; + +export const toBreakWord = (str: string, len: number, char='\n') => { + var strTemp = ""; + var line = 1; + if(str.length <= len) { + return {str, line}; + } + while (str.length > len) { + strTemp += str.substr(0, len) + char; + str = str.substr(len, str.length); + line ++; + } + strTemp += str; + return { + str: strTemp, + line, + }; +} \ No newline at end of file diff --git a/render-process/RegisterNode.ts b/render-process/RegisterNode.ts index 7a92467..bda884e 100644 --- a/render-process/RegisterNode.ts +++ b/render-process/RegisterNode.ts @@ -1,4 +1,5 @@ import G6 from "@antv/g6"; +import { toBreakWord } from "../common/Utils"; import Settings from "../main-process/Settings"; const NODE_COLORS: any = { @@ -165,50 +166,54 @@ export default function (settings: Settings) { const args: any = cfg.args; if (nodeConf.args && args && Object.keys(args).length > 0) { - y += 20; + const { str, line } = toBreakWord(`参数:${JSON.stringify(args)}`, 35); group.addShape("text", { attrs: { textBaseline: "top", x, - y, + y: y + 20, + w, lineHeight: 20, - text: `参数:${JSON.stringify(args)}`, + text: str, fill: "black", }, name: "args-text", }); + y += 20 * line; } const input: [] = cfg.input ? (cfg.input as []) : []; if (nodeConf.input && input.length > 0) { - y += 20; + const { str, line } = toBreakWord(`输入:${JSON.stringify(input)}`, 35); group.addShape("text", { attrs: { textBaseline: "top", x, - y, + y: y + 20, lineHeight: 20, - text: `输入:${JSON.stringify(input)}`, + text: str, fill: "black", }, name: "input-text", }); + y += 20 * line; } const output: [] = cfg.output ? (cfg.output as []) : []; if (nodeConf.output && output.length > 0) { - y += 20; + const { str, line } = toBreakWord(`输出:${JSON.stringify(output)}`, 35); group.addShape("text", { attrs: { textBaseline: "top", x, - y, + y: y + 20, lineHeight: 20, - text: `输出:${JSON.stringify(output)}`, + text: str, fill: "black", }, name: "output-text", }); + y += 20 * line; } if (Array.isArray(cfg.children) && cfg.children.length > 0) {