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
+37 -15

No files matched your search

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