diff --git a/src/IR/block/base/content/file.js b/src/IR/block/base/content/file.js index 048f1889..2db1017b 100644 --- a/src/IR/block/base/content/file.js +++ b/src/IR/block/base/content/file.js @@ -1,59 +1,49 @@ import { folderTypeName, tagTypeName, fileTypeName } from '../type/constant' import { Content } from './base' -const category = { - folder: 0, - file: 1, - tag: 2 +const CATEGORY = { + FOLDER: 0, + FILE: 1, + TAG: 2 } class FileContent extends Content { - constructor (id, name, path, content) { - super(fileTypeName, content) + constructor (id, name, path, depth) { + super(fileTypeName, name) this.id = id this.name = name this.path = path - } - - getFileJson () { - return { - name: this.name, - path: this.path, - content: this.text - } + this.depth = depth } getNodeJson () { return { id: this.id, name: this.name, - category: category.file + category: CATEGORY.FILE, + depth: this.depth } } } + class FolderContent extends Content { - constructor (id, name, path) { + constructor (id, name, path, depth) { super(folderTypeName, name) this.id = id this.name = name this.path = path - } - - getFileJson () { - return { - name: this.text, - path: this.path, - children: [] - } + this.depth = depth } getNodeJson () { return { id: this.id, name: this.name, - category: category.folder + category: CATEGORY.FOLDER, + depth: this.depth } } } + class TagContent extends Content { constructor (id, name) { super(tagTypeName, name) @@ -65,7 +55,7 @@ class TagContent extends Content { return { id: this.id, name: this.name, - category: category.tag + category: CATEGORY.TAG } } } diff --git a/src/IR/block/factory/buildNode.js b/src/IR/block/factory/buildNode.js index 3862aeca..8b6c1826 100644 --- a/src/IR/block/factory/buildNode.js +++ b/src/IR/block/factory/buildNode.js @@ -55,12 +55,12 @@ function buildTable (cells) { return new TreeNode(paragraphTypeName, new TableContent(cells)) } -function buildFileNode (id, name, path, content) { - return new TreeNode(fileTypeName, new FileContent(id, name, path, content)) +function buildFileNode (id, name, path, depth) { + return new TreeNode(fileTypeName, new FileContent(id, name, path, depth)) } -function buildFolderNode (id, name, path) { - return new TreeNode(folderTypeName, new FolderContent(id, name, path)) +function buildFolderNode (id, name, path, depth) { + return new TreeNode(folderTypeName, new FolderContent(id, name, path, depth)) } function buildTagNode (id, name) { diff --git a/src/IR/component/graph.js b/src/IR/component/graph.js index 10018f4d..ecfdd4df 100644 --- a/src/IR/component/graph.js +++ b/src/IR/component/graph.js @@ -21,23 +21,23 @@ export default class IRGraph { * @param {{name: string, path: string, children: [any], type: string}} files * @returns */ - parseFileTree (files) { + parseFileTree (files, depth) { let newNode if (files.type === 'folder') { - newNode = buildFolderNode(this.allocNodeID(), files.name, files.path) + newNode = buildFolderNode(this.allocNodeID(), files.name, files.path, depth) this.treenodes.push(newNode) files.children.forEach(e => { - newNode.insertAtLast(this.parseFileTree(e)) + newNode.insertAtLast(this.parseFileTree(e, depth + 1)) }) } else { - newNode = buildFileNode(this.allocNodeID(), files.name, files.path, files.content) + newNode = buildFileNode(this.allocNodeID(), files.name, files.path, depth) this.treenodes.push(newNode) } return newNode } addFiles (files) { - this.graph = this.parseFileTree(files) + this.graph = this.parseFileTree(files, 1) this.makeEdges() } @@ -120,12 +120,6 @@ export default class IRGraph { return this.edges.concat(this.relations).concat(this.aerials) } - getFileTreeJson () { - if (this.graph) { - return this.graph.toFileTreeJson() - } - } - /** * 获得一个独有的nodeID */ diff --git a/test/IR/factory/mindgraph.test.js b/test/IR/factory/mindgraph.test.js index b703b5b0..cfd843a7 100644 --- a/test/IR/factory/mindgraph.test.js +++ b/test/IR/factory/mindgraph.test.js @@ -1,11 +1,11 @@ import { buildGraphFromFileTree } from '../../../src/IR/block/factory/filesToGraph' import assert from 'assert' -import { files, res1, nodes1, link1 } from '../data/file.js' +import { files, nodes1, link1 } from '../data/file.js' describe('fileJson到IR测试', function () { const irgraph = buildGraphFromFileTree(files) - it('简单文件树结构测试', function () { - assert.deepStrictEqual(irgraph.getFileTreeJson(), res1) - }) + // it('简单文件树结构测试', function () { + // assert.deepStrictEqual(irgraph.getFileTreeJson(), res1) + // }) it('简单文件树结构到nodes', function () { assert.deepStrictEqual(irgraph.getNodes(), nodes1) })