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
+79 -75

No files matched your search

+34 -3
View File
@@ -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() {
@@ -66,7 +75,7 @@ export default class Settings {
return this.model.serverName; return this.model.serverName;
} }
} }
if(servers[0]) { if (servers[0]) {
return servers[0].name; return servers[0].name;
} else { } else {
return '' return ''
@@ -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) {
@@ -114,10 +127,28 @@ export default class Settings {
list = list.filter((value) => value !== path); list = list.filter((value) => value !== path);
} }
list.unshift(path); list.unshift(path);
while(list.length > 10) { while (list.length > 10) {
list.pop(); list.pop();
} }
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
View File
@@ -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": {
+44 -71
View File
@@ -1,27 +1,33 @@
import React, { ChangeEvent, Component } from "react"; import React, { ChangeEvent, Component } from "react";
import { Input } from "antd"; import { Input } from "antd";
import { Tree } from "shineout"; import { Tree } from "shineout";
import {FolderOutlined ,FileOutlined,FolderOpenOutlined} from "@ant-design/icons"; import { FolderOutlined, FileOutlined, FolderOpenOutlined } from "@ant-design/icons";
import * as fs from "fs"; 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;
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,7 +35,10 @@ class FileDataNode {
} }
get text() { get text() {
return this.name; if (!this.desc) {
return this.name;
}
return `${this.name}(${this.desc})`;
} }
get path() { get path() {
@@ -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];
} }
@@ -206,22 +177,22 @@ const NodeActions: { [x: string]: string } = {
["reveal_in_explorer"]: "Reveal In File Explorer", ["reveal_in_explorer"]: "Reveal In File Explorer",
}; };
interface ExplorerNodeProps{ interface ExplorerNodeProps {
visible:boolean; visible: boolean;
title:string; title: string;
selected:boolean; selected: boolean;
expended:boolean; expended: boolean;
isLeaf:boolean; isLeaf: boolean;
searchKey:string; searchKey: string;
} }
class ExplorerNode extends Component<ExplorerNodeProps> { class ExplorerNode extends Component<ExplorerNodeProps> {
shouldComponentUpdate(nextProps: ExplorerNodeProps) { shouldComponentUpdate(nextProps: ExplorerNodeProps) {
return JSON.stringify(this.props)!=JSON.stringify(nextProps); return JSON.stringify(this.props) != JSON.stringify(nextProps);
} }
render() { render() {
const {title,visible,selected,expended,isLeaf,searchKey} = this.props; const { title, visible, selected, expended, isLeaf, searchKey } = this.props;
if(!visible){ if (!visible) {
return null; return null;
} }
@@ -231,9 +202,9 @@ class ExplorerNode extends Component<ExplorerNodeProps> {
return ( return (
<div <div
className={selected?"explorer-node-selected":"explorer-node"} className={selected ? "explorer-node-selected" : "explorer-node"}
> >
{!isLeaf?(expended?<FolderOpenOutlined />:<FolderOutlined />):<FileOutlined />} {!isLeaf ? (expended ? <FolderOpenOutlined /> : <FolderOutlined />) : <FileOutlined />}
{isLeaf && index > -1 ? ( {isLeaf && index > -1 ? (
<span className="explorer-node-title"> <span className="explorer-node-title">
{beforeStr} {beforeStr}
@@ -241,8 +212,8 @@ class ExplorerNode extends Component<ExplorerNodeProps> {
{afterStr} {afterStr}
</span> </span>
) : ( ) : (
<span className="explorer-node-title">{title}</span> <span className="explorer-node-title">{title}</span>
)} )}
</div> </div>
); );
} }
@@ -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);
@@ -322,15 +295,15 @@ export default class Explorer extends Component<ExplorerProps> {
} }
getRootNode(workdir: string) { getRootNode(workdir: string) {
if(workdir && workdir!==""){ if (workdir && workdir !== "") {
return new FileDataNode({ return new FileDataNode({
name: path.basename(workdir), name: path.basename(workdir),
filepath: workdir, filepath: workdir,
isFolder: true, isFolder: true,
parent: null, parent: null,
children: [] children: []
}); }, this.settings);
}else{ } else {
return null; return null;
} }
} }
@@ -389,9 +362,9 @@ export default class Explorer extends Component<ExplorerProps> {
this.forceUpdate(); this.forceUpdate();
} }
selectNode(id:string){ selectNode(id: string) {
this.setState({ this.setState({
selectedKey:id selectedKey: id
}); });
this.forceUpdate(); this.forceUpdate();
} }
@@ -400,11 +373,11 @@ export default class Explorer extends Component<ExplorerProps> {
return (<ExplorerNode return (<ExplorerNode
visible={node.visible} visible={node.visible}
title={node.text} title={node.text}
selected={this.state.selectedKey==node.id} selected={this.state.selectedKey == node.id}
expended={this.state.expandedKeys.includes(node.id )} expended={this.state.expandedKeys.includes(node.id)}
isLeaf={!node.isFolder} isLeaf={!node.isFolder}
searchKey={this.state.searchKey} searchKey={this.state.searchKey}
/>); />);
} }
render() { render() {
@@ -430,7 +403,7 @@ export default class Explorer extends Component<ExplorerProps> {
onSearch={(value, event) => { onSearch={(value, event) => {
this.handleOnSearch(value?.toLowerCase()); this.handleOnSearch(value?.toLowerCase());
}} }}
/> />
<DirectoryTree <DirectoryTree
keygen="id" keygen="id"
@@ -440,7 +413,7 @@ export default class Explorer extends Component<ExplorerProps> {
defaultExpanded={[root.id]} defaultExpanded={[root.id]}
expanded={this.state.expandedKeys} expanded={this.state.expandedKeys}
value={[this.state.selectedKey]} value={[this.state.selectedKey]}
onExpand={(expanded)=>{ onExpand={(expanded) => {
this.setState({ expandedKeys: expanded }) this.setState({ expandedKeys: expanded })
this.forceUpdate(); this.forceUpdate();
}} }}