cache tree desc

This commit is contained in:
zhandouxiaojiji committed 2021-01-11 18:24:38 +08:00
1 parent 45d01c4c73
commit dd3f2e0fb4
3 files changed
+51 -47

No files matched your search

+32 -1
View File
@@ -11,6 +11,7 @@ export interface SettingsModel {
recentFiles?: string[];
serverName?: string;
nodeClassify?: BehaviorNodeClassify[];
treesDesc?: { [name: string]: string };
}
const settingPath = "settings.json";
@@ -23,10 +24,18 @@ const sampleNodeClassify: BehaviorNodeClassify[] = [
export default class Settings {
private model: SettingsModel;
private dirty: boolean = false;
curWorkspace?: Workspace;
constructor() {
this.load();
setInterval(() => {
if (this.dirty) {
fs.writeFileSync(settingPath, JSON.stringify(this.model, null, 2));
this.dirty = false;
}
}, 1000)
}
get nodeConfPath() {
@@ -88,6 +97,10 @@ export default class Settings {
if (fs.existsSync(settingPath)) {
const str = fs.readFileSync(settingPath, "utf8");
this.model = JSON.parse(str);
if (!this.model.treesDesc) {
this.model.treesDesc = {};
this.save();
}
} else {
this.model = {
recentWorkspaces: ["sample/workspace.json"],
@@ -101,7 +114,7 @@ export default class Settings {
}
save() {
fs.writeFileSync(settingPath, JSON.stringify(this.model, null, 2));
this.dirty = true;
}
getNodeConf(name: string) {
@@ -120,4 +133,22 @@ export default class Settings {
this.model.recentWorkspaces = list;
this.save();
}
getTreeDesc(name: string) {
const key = this.curWorkspace.getFilepath() + ' ' + name;
var desc = this.model.treesDesc[key];
if (!desc) {
const str = fs.readFileSync(name, "utf8");
const tree = JSON.parse(str);
desc = tree.desc ? tree.desc : '';
this.model.treesDesc[key] = desc;
this.save();
}
return desc;
}
setTreeDesc(name: string, desc?: string) {
const key = this.curWorkspace.getFilepath() + ' ' + name;
this.model.treesDesc[key] = desc;
}
}
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "behavior3editor",
"version": "0.1.7",
"version": "0.1.8",
"description": "行为树编辑器",
"main": "./dist/main.js",
"scripts": {
+18 -45
View File
@@ -6,6 +6,8 @@ import * as fs from "fs";
import * as path from "path";
import { ipcRenderer } from "electron";
import MainEventType from "../common/MainEventType";
import Settings from "../main-process/Settings";
import * as Utils from "../common/Utils";
const { Search } = Input;
const DirectoryTree = Tree;
@@ -13,15 +15,19 @@ const DirectoryTree = Tree;
class FileDataNode {
name: string; //display name
desc: string; //describe
filepath: string; //file full path
isFolder: boolean;
settings: Settings;
parent: FileDataNode;
visible: boolean = true;
children: FileDataNode[];
public constructor(init?: Partial<FileDataNode>) {
public constructor(init?: Partial<FileDataNode>, settings?: Settings) {
Object.assign(this, init);
this.settings = settings;
}
get id() {
@@ -29,8 +35,11 @@ class FileDataNode {
}
get text() {
if (!this.desc) {
return this.name;
}
return `${this.name}(${this.desc})`;
}
get path() {
return this.filepath;
@@ -42,10 +51,11 @@ class FileDataNode {
}
let ret = new FileDataNode({
name: this.name,
desc: this.isFolder ? undefined : this.settings.getTreeDesc(this.filepath),
filepath: this.filepath,
isFolder: this.isFolder,
children: new Array(),
});
}, this.settings);
for (const child of this.children) {
const data = child.getRenderData();
@@ -70,10 +80,11 @@ class FileDataNode {
const name = isFolder ? filename : filename.slice(0, -5);
const node = new FileDataNode({
name: name,
desc: isFolder ? undefined : this.settings.getTreeDesc(fullPath),
filepath: fullPath,
isFolder: isFolder,
parent: this,
});
}, this.settings);
(node.children =
isFolder && recursive ? (node.loadChilds(recursive) as FileDataNode[]) : []),
@@ -87,46 +98,6 @@ class FileDataNode {
return list;
}
// async updateChilds(recursive?: boolean): Promise<FileDataNode[]> {
// const folder = this.path;
// if (folder == "" || !fs.existsSync(folder)) {
// return [];
// }
// this.loading = true;
// await fs.promises.readdir(folder).then((files) => {
// this.loading = false;
// let list = new Array<FileDataNode>();
// files.forEach((filename) => {
// const fullPath = path.join(folder, filename);
// const stat = fs.statSync(fullPath);
// const isFolder = stat.isDirectory();
// const name = isFolder ? filename : filename.slice(0, -5);
// const node = new FileDataNode({
// name: name,
// filepath: fullPath,
// isFolder: isFolder,
// parent: this,
// children: [],
// });
// if (isFolder && recursive) {
// node.updateChilds(recursive).then((children)=>{
// node.children = children;
// });
// }
// list.push(node);
// });
// list.sort((a, b) => {
// const av = a.isFolder ? 100 : 0;
// const bv = b.isFolder ? 100 : 0;
// return bv - av;
// });
// this.children = list;
// });
// return this.children;
// }
addChild(filePath: string) {
const isDir = fs.statSync(filePath).isDirectory();
const newNode = new FileDataNode({
@@ -135,7 +106,7 @@ class FileDataNode {
isFolder: isDir,
parent: this,
children: [],
});
}, this.settings);
this.children = [newNode, ...this.children];
}
@@ -281,6 +252,7 @@ export default class Explorer extends Component<ExplorerProps> {
};
curWorkdir: string = "";
settings: Settings;
shouldComponentUpdate(nextProps: ExplorerProps) {
const shouldUpdate = this.curWorkdir != nextProps.workdir;
@@ -289,6 +261,7 @@ export default class Explorer extends Component<ExplorerProps> {
}
componentDidMount() {
this.settings = Utils.getRemoteSettings();
ipcRenderer.on(MainEventType.CREATE_TREE, (event: any, path: string) => {
console.log("on Create tree", path);
this.props.onOpenTree(path);
@@ -329,7 +302,7 @@ export default class Explorer extends Component<ExplorerProps> {
isFolder: true,
parent: null,
children: []
});
}, this.settings);
} else {
return null;
}