-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
195 lines (173 loc) · 7.26 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Mindmap App - Version 7 (With PNG Export)</title>
<script src="https://html2canvas.hertzen.com/dist/html2canvas.min.js"></script>
<style>
body {
font-family: Arial, sans-serif;
display: flex;
flex-direction: column;
align-items: center;
padding: 20px;
}
#input-area {
width: 100%;
max-width: 600px;
height: 200px;
margin-bottom: 20px;
}
#render-button, #generate-png {
margin-bottom: 20px;
padding: 10px 20px;
font-size: 16px;
cursor: pointer;
}
#mindmap-container {
width: 600px;
height: 600px;
display: grid;
grid-template-columns: repeat(4, 1fr);
grid-template-rows: repeat(4, 1fr);
gap: 10px;
padding: 10px;
background-color: #f0f0f0;
}
.node {
display: flex;
justify-content: center;
align-items: center;
border: 2px solid #333;
border-radius: 5px;
padding: 5px;
cursor: pointer;
transition: all 0.3s ease;
text-align: center;
}
.root {
background-color: #ff9966;
grid-area: 1 / 1 / 3 / 3;
}
.level-1 {
background-color: #66cc99;
display: none;
}
.level-2 {
background-color: #6699cc;
display: none;
}
#level-1-1 { grid-area: 3 / 1 / 4 / 2; }
#level-1-2 { grid-area: 3 / 3 / 4 / 4; }
#level-1-3 { grid-area: 1 / 3 / 2 / 4; }
</style>
</head>
<body>
<textarea id="input-area" placeholder="Enter your markdown here..."></textarea>
<button id="render-button">Render Mindmap</button>
<button id="generate-png">Generate PNG</button>
<div id="mindmap-container"></div>
<script>
function parseMarkdown(markdown) {
const lines = markdown.split('\n');
const root = { content: lines[0].replace('# ', ''), children: [] };
let currentLevel1 = null;
for (let i = 1; i < lines.length; i++) {
const line = lines[i].trim();
if (line.startsWith('## ')) {
currentLevel1 = { content: line.replace('## ', ''), children: [] };
root.children.push(currentLevel1);
} else if (line.startsWith('### ') && currentLevel1) {
currentLevel1.children.push({ content: line.replace('### ', '') });
}
}
return root;
}
function createNode(content, level, id = '') {
const node = document.createElement('div');
node.className = `node level-${level}`;
node.textContent = content;
if (id) node.id = id;
return node;
}
function renderMindmap(data) {
const container = document.getElementById('mindmap-container');
container.innerHTML = '';
const rootNode = createNode(data.content, 0, 'root');
rootNode.classList.add('root');
container.appendChild(rootNode);
const level2Positions = [
[[4, 1], [4, 2], [3, 2]], // For level-1-1
[[4, 3], [4, 4], [3, 4]], // For level-1-2
[[1, 4], [2, 4], [2, 3]] // For level-1-3
];
data.children.forEach((child, index) => {
if (index >= 3) return; // Limit to 3 level 1 nodes
const childNode = createNode(child.content, 1, `level-1-${index + 1}`);
container.appendChild(childNode);
child.children.forEach((grandchild, grandchildIndex) => {
if (grandchildIndex >= 3) return; // Limit to 3 level 2 nodes
const position = level2Positions[index][grandchildIndex];
const grandchildNode = createNode(grandchild.content, 2);
grandchildNode.style.gridArea = `${position[0]} / ${position[1]} / span 1 / span 1`;
container.appendChild(grandchildNode);
});
childNode.addEventListener('click', (e) => {
e.stopPropagation();
toggleLevel2Nodes(index);
});
});
rootNode.addEventListener('click', toggleLevel1Nodes);
}
function toggleLevel1Nodes() {
const level1Nodes = document.querySelectorAll('.level-1');
const level2Nodes = document.querySelectorAll('.level-2');
const displayStyle = level1Nodes[0].style.display === 'none' ? 'flex' : 'none';
level1Nodes.forEach(node => node.style.display = displayStyle);
if (displayStyle === 'none') {
level2Nodes.forEach(node => node.style.display = 'none');
}
}
function toggleLevel2Nodes(index) {
const level2Positions = [
[[4, 1], [4, 2], [3, 2]], // For level-1-1
[[4, 3], [4, 4], [3, 4]], // For level-1-2
[[1, 4], [2, 4], [2, 3]] // For level-1-3
];
const l2Nodes = document.querySelectorAll('.level-2');
l2Nodes.forEach(node => {
if (level2Positions[index].some(pos => node.style.gridArea.startsWith(`${pos[0]} / ${pos[1]}`))) {
node.style.display = node.style.display === 'none' ? 'flex' : 'none';
}
});
}
function generatePNG() {
// Store current visibility state
const level1Nodes = document.querySelectorAll('.level-1');
const level2Nodes = document.querySelectorAll('.level-2');
const level1State = Array.from(level1Nodes).map(node => node.style.display);
const level2State = Array.from(level2Nodes).map(node => node.style.display);
// Make all nodes visible
level1Nodes.forEach(node => node.style.display = 'flex');
level2Nodes.forEach(node => node.style.display = 'flex');
html2canvas(document.getElementById('mindmap-container')).then(canvas => {
const image = canvas.toDataURL("image/png").replace("image/png", "image/octet-stream");
const link = document.createElement('a');
link.download = 'mindmap.png';
link.href = image;
link.click();
// Restore original visibility state
level1Nodes.forEach((node, index) => node.style.display = level1State[index]);
level2Nodes.forEach((node, index) => node.style.display = level2State[index]);
});
}
document.getElementById('render-button').addEventListener('click', () => {
const markdown = document.getElementById('input-area').value;
const data = parseMarkdown(markdown);
renderMindmap(data);
});
document.getElementById('generate-png').addEventListener('click', generatePNG);
</script>
</body>
</html>