Skip to content

Commit 4d8ebe2

Browse files
bluzkyDN.Wangchuk
andauthored
implement text parser (#12)
Co-authored-by: DN.Wangchuk <dung.nt@yeah1.vn>
1 parent 32cefed commit 4d8ebe2

File tree

4 files changed

+96
-3
lines changed

4 files changed

+96
-3
lines changed

src/js/index.js

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,32 @@ import MindmapLayouts from "./layouts"
33
import MindMap from "./mindmap"
44
import MindMapViewer from './viewer'
55
import sample from './sample'
6+
import { TextParser } from './parser'
67

78
// import * as $ from 'jquery'
89

10+
let text = `
11+
Root
12+
13+
- branch 1
14+
+branch 1.1
15+
16+
- branch 2
17+
branch 2.1
18+
* branch 2.2
19+
branch 2.2.1
20+
21+
-Branch 3
22+
- alo
23+
- ola
24+
- ollala
25+
26+
-Branch 4
27+
- Branch 4.1
28+
- Branch 4.1.1
29+
- Branch 4.2
30+
- Branch 4.3`
31+
932

1033
const formNode = document.getElementById('layout-props')
1134
const layoutTimeNode = document.getElementById('layout-time')
@@ -19,11 +42,12 @@ const viewer = new MindMapViewer('#drawing', {})
1942
function render() {
2043
const count = formNode.dataSize.value
2144
const layoutType = formNode.layoutType.value
22-
const root = randomTree(count)
45+
// const root = randomTree(count)
2346
const MindmapLayout = MindmapLayouts[layoutType]
47+
const test = TextParser.parse(text)
2448

2549
const t0 = window.performance.now()
26-
const mindMap = new MindMap(sample.root, MindmapLayout, {})
50+
const mindMap = new MindMap(test.root, MindmapLayout, {})
2751
mindMap.build()
2852
const t1 = window.performance.now()
2953
viewer.render(mindMap)

src/js/mindmap.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ class MindMap {
66
constructor(data, layout, options) {
77
this.data = data
88
this.options = options
9-
console.log(options)
109
this.theme = options["theme"] || new Theme(data["theme"])
1110
options["theme"] = this.theme
1211
this.rootNode = new Node(data, options)

src/js/parser/index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import TextParser from './text-parser'
2+
3+
export {
4+
TextParser
5+
}

src/js/parser/text-parser.js

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
2+
3+
function parse(text) {
4+
let lines = text.match(/[^\r\n]+/g);
5+
let root = null
6+
let levelStack = []
7+
let currentLevel = 0
8+
9+
for (let line of lines) {
10+
if (root == null) {
11+
root = {
12+
content: getContent(line),
13+
children: []
14+
}
15+
levelStack.push({ node: root, level: 0 })
16+
} else {
17+
let content = getContent(line)
18+
let level = getLevel(line)
19+
let node = {
20+
content: content,
21+
children: []
22+
}
23+
24+
if (level == 0) {
25+
break
26+
}
27+
28+
let lastIndex = levelStack.length - 1
29+
30+
while (levelStack[levelStack.length - 1].level > level) {
31+
levelStack.pop()
32+
currentLevel -= 1
33+
}
34+
35+
if (level != currentLevel) {
36+
levelStack[levelStack.length - 1].node.children.push(node)
37+
levelStack.push({ node: node, level: level })
38+
currentLevel = level
39+
} else {
40+
levelStack.pop()
41+
levelStack[levelStack.length - 1].node.children.push(node)
42+
levelStack.push({ node: node, level: level })
43+
}
44+
}
45+
}
46+
console.log(root)
47+
return {
48+
root
49+
}
50+
}
51+
52+
function getLevel(line) {
53+
line = line.replace(/\t/, " ")
54+
let leadingWs = line.match(/^\s*/)
55+
leadingWs = leadingWs[0] || ""
56+
return Math.round(leadingWs.length / 4)
57+
}
58+
59+
function getContent(line) {
60+
line = line.trim()
61+
line = line.replace(/^[+\-_*]\s*/, "")
62+
return line
63+
}
64+
65+
export default { parse: parse }

0 commit comments

Comments
 (0)