Skip to content

Commit

Permalink
feat(ir/node): 增加depth字段
Browse files Browse the repository at this point in the history
  • Loading branch information
Blore-lzn authored and GwokHiujin committed May 10, 2023
1 parent 6090aa1 commit 300dd97
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 45 deletions.
42 changes: 16 additions & 26 deletions src/IR/block/base/content/file.js
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -65,7 +55,7 @@ class TagContent extends Content {
return {
id: this.id,
name: this.name,
category: category.tag
category: CATEGORY.TAG
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/IR/block/factory/buildNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
16 changes: 5 additions & 11 deletions src/IR/component/graph.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

Expand Down Expand Up @@ -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
*/
Expand Down
8 changes: 4 additions & 4 deletions test/IR/factory/mindgraph.test.js
Original file line number Diff line number Diff line change
@@ -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)
})
Expand Down

0 comments on commit 300dd97

Please sign in to comment.