optimize break word

This commit is contained in:
codetypes committed 2024-04-25 23:59:49 +08:00
1 parent 543c5b09c3
commit 7dbea171bb
2 files changed
+38 -16

No files matched your search

+9 -5
View File
@@ -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",
+29 -11
View File
@@ -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",