diff --git a/src/components/register-node.ts b/src/components/register-node.ts index cdccbba..00fb589 100644 --- a/src/components/register-node.ts +++ b/src/components/register-node.ts @@ -1,6 +1,6 @@ import { useWorkspace } from "@/contexts/workspace-context"; import { TreeGraphData, getNodeType } from "@/misc/b3type"; -import { checkTreeData, toBreakWord } from "@/misc/b3util"; +import { checkTreeData, cutWordTo, toBreakWord } from "@/misc/b3util"; import i18n from "@/misc/i18n"; import { isMacos } from "@/misc/keys"; import Path from "@/misc/path"; @@ -222,11 +222,13 @@ G6.registerNode( name: "name-text", }); - const x = 5; + const x = 6; let y = 24; // desc text - const desc = cfg.desc || nodeDef.desc; + let desc = (cfg.desc || nodeDef.desc) as string; if (desc) { + desc = i18n.t("regnode.mark") + desc; + desc = cutWordTo(desc, 31); addShape("text", { attrs: { textBaseline: "top", @@ -234,7 +236,7 @@ G6.registerNode( y, lineHeight: 20, fontWeight: 800, - text: `${i18n.t("regnode.mark")}${desc}`, + text: `${desc}`, fill: "black", }, name: "desc-text", @@ -333,13 +335,15 @@ G6.registerNode( } if (cfg.path) { + let path = (i18n.t("regnode.subtree") + cfg.path) as string; + path = cutWordTo(path, 33); addShape("text", { attrs: { textBaseline: "top", x, y: y + 20, lineHeight: 20, - text: `${i18n.t("regnode.subtree")}${cfg.path}`, + text: `${path}`, fill: "black", }, name: "subtree-text", diff --git a/src/misc/b3util.ts b/src/misc/b3util.ts index 2f8ea43..db956ac 100644 --- a/src/misc/b3util.ts +++ b/src/misc/b3util.ts @@ -368,24 +368,42 @@ export const createNewTree = (filename: string) => { return tree; }; -export const toBreakWord = (str: string, len: number, char = "\n") => { - let strTemp = ""; +const isAsciiChar = (c: number) => { + return (c >= 0x0001 && c <= 0x007e) || (0xff60 <= c && c <= 0xff9f); +}; + +export const toBreakWord = (str: string, maxlen: number, char = "\n") => { + const chars: string[] = []; let line = 1; - if (str.length <= len) { - return { str, line }; + let len = 0; + for (let i = 0; i < str.length; i++) { + const c = str.charCodeAt(i); + len += isAsciiChar(c) ? 1 : 2; + chars.push(String.fromCharCode(c)); + if (len >= maxlen) { + len = 0; + line++; + chars.push("\n"); + } } - while (str.length > len) { - strTemp += str.slice(0, len) + char; - str = str.slice(len, str.length); - line++; - } - strTemp += str; return { - str: strTemp, + str: chars.join(""), line, }; }; +export const cutWordTo = (str: string, maxlen: number) => { + let i = 0; + for (; i < str.length; i++) { + const c = str.charCodeAt(i); + maxlen -= isAsciiChar(c) ? 1 : 2; + if (maxlen <= 0) { + break; + } + } + return str.slice(0, i) + (i < str.length - 1 ? "..." : ""); +}; + export const createProject = (path: string) => { fs.writeFileSync( Path.dirname(path) + "/node-config.b3-setting",