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