-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathCodeMindMapHtml.cs
More file actions
525 lines (456 loc) · 20.3 KB
/
Copy pathCodeMindMapHtml.cs
File metadata and controls
525 lines (456 loc) · 20.3 KB
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
using System;
using System.Diagnostics;
using System.IO;
using Microsoft.VisualStudio;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Interop;
namespace CodeMindMap
{
public class CodeMindMapHtml
{
public const string DefaultFileName = "CodeMindMap.html";
public const string TempDirName = "CodeMindMap_Temp";
public static readonly string DefaultHtmlContent =
@"<!DOCTYPE html>
<html lang=""en"">
<head>
<meta charset=""UTF-8"">
<meta name=""viewport"" content=""width=device-width, initial-scale=1.0"">
<title>Code Mind Map</title>
<link rel=""stylesheet"" href=""http://codemindmap.vsext/MindElixir.css"">
<style>
.mind-elixir-toolbar #fullscreen {
display: none;
}
.mind-elixir-toolbar.rb {
display: flex;
gap: 10px;
align-items: center;
}
.mind-elixir-toolbar.rb span + span {
margin-left: 0;
}
#map {
width: 100%;
height: 100vh;
}
body {
margin: 0;
padding: 0;
}
</style>
</head>
<body>
<div id=""map""></div>
<script type=""module"">
import MindElixir from ""http://codemindmap.vsext/MindElixir.js"";
let mind, themeManager, lastSelectedNode;
function initMindMap() {
const options = {
el: '#map',
allowUndo: true,
toolBar: true,
};
const LIGHT_THEME = {
name: 'Light',
// Updated color palette with more vibrant colors
palette: ['#dd7878', '#ea76cb', '#8839ef', '#e64553', '#fe640b', '#df8e1d', '#40a02b', '#209fb5', '#1e66f5', '#7287fd'],
// Enhanced CSS variables for better styling control
cssVar: {
'--node-gap-x': '30px',
'--node-gap-y': '10px',
'--main-gap-x': '32px',
'--main-gap-y': '12px',
'--root-radius': '30px',
'--main-radius': '20px',
'--root-color': '#ffffff',
'--root-bgcolor': '#4c4f69',
'--root-border-color': 'rgba(0, 0, 0, 0)',
'--main-color': '#444446',
'--main-bgcolor': '#ffffff',
'--topic-padding': '3px',
'--color': '#333333',
'--bgcolor': '#f6f6f6',
'--selected': '#4dc4ff',
'--panel-color': '#444446',
'--panel-bgcolor': '#ffffff',
'--panel-border-color': '#eaeaea',
'--map-padding': '50px 80px',
'font-size': '14px',
'font-family':'Verdana',
},
};
const DARK_THEME = {
name: 'Dark',
// Updated color palette with more vibrant colors
palette: ['#848FA0', '#748BE9', '#D2F9FE', '#4145A5', '#789AFA', '#706CF4', '#EF987F', '#775DD5', '#FCEECF', '#DA7FBC'],
// Enhanced CSS variables for better styling control
cssVar: {
'--node-gap-x': '30px',
'--node-gap-y': '10px',
'--main-gap-x': '32px',
'--main-gap-y': '12px',
'--root-radius': '30px',
'--main-radius': '20px',
'--root-color': '#ffffff',
'--root-bgcolor': '#4c4f69',
'--root-border-color': 'rgba(0, 0, 0, 0)',
'--main-color': '#ffffff',
'--main-bgcolor': '#4c4f69',
'--topic-padding': '3px',
'--color': '#E0E0E0',
'--bgcolor': '#252526',
'--selected': '#4dc4ff',
'--panel-color': '#ffffff',
'--panel-bgcolor': '#2d3748',
'--panel-border-color': '#696969',
'--map-padding': '50px 80px',
'font-size': '14px',
'font-family':'Verdana',
},
};
themeManager = {
themes: {
Light: LIGHT_THEME,
Dark: DARK_THEME,
},
getTheme(themeName) {
return this.themes[themeName];
},
contains(themeName) {
return this.themes.hasOwnProperty(themeName);
},
getAvailableThemes() {
return Object.keys(this.themes);
},
};
const data = {
nodeData: {
id: 'me-root',
topic: 'Code Mind Map',
children: [
{
topic: 'Code',
id: 'bd1f03fee1f63bc6',
direction: 0,
expanded: true,
children: [
{
topic: 'Ctrl+2, Ctrl+2 — add the selected code (line under the caret) as a node linked to the code.',
id: 'bd1f07c598e729dc',
},
{
topic: 'Ctrl+click on a node - jump to the code linked to the node.',
id: 'bd1bb4b14d6697c3',
},
],
},
{
topic: 'Mind Map',
id: 'bd1b66c4b56754d9',
direction: 1,
expanded: true,
children: [
{
topic: 'Alt/Ctrl+Scroll - Zoom in/out',
id: 'bd1c1cb51e6745d3',
},
{
topic: 'Shift+Scroll - Scroll horizontally',
id: '16710cea5cfb50712c8832676b87d2cc',
},
{
topic: 'Right Click+Drag - Move the mind map',
id: 'bd1c1e12fd603ff6',
},
{
topic: 'Tab - Create a child node',
id: 'bd1b6892bcab126a',
},
{
topic: 'Enter - Create a sibling node',
id: 'bd1b6b632a434b27',
},
{
topic: 'Del - Remove a node',
id: 'bd1b983085187c0a',
},
{
topic: 'Space - Expand/collapse nodes',
id: 'bd1bb2ac4bbab458',
},
],
},
],
expanded: true,
},
theme: themeManager.getTheme(LIGHT_THEME.name),
direction: 2
};
mind = new MindElixir(options);
mind.init(data);
// Intercept direction-change methods so autosave is triggered when the
// user switches between left tree, right tree, and flower (side) views.
['initLeft', 'initRight', 'initSide'].forEach(method => {
const original = mind[method].bind(mind);
mind[method] = function() {
original();
window.chrome.webview.postMessage({ action: 'mindMapOperation', operationName: 'changeDirection' });
};
});
mind.bus.addListener('selectNodes', nodes => {
const node = nodes.at(-1);
if (!node) return;
lastSelectedNode = node;
window.chrome.webview.postMessage({
action: 'nodeSelected',
nodeId: node.id,
nodeTopic: node.topic,
nodeData: node.data,
});
});
mind.bus.addListener('selectNewNode', () => {
// Mirror the same focus behavior for newly created nodes.
mind.map?.focus();
});
mind.bus.addListener('operation', operation => {
window.chrome.webview.postMessage({
action: 'mindMapOperation',
operationName: operation.name,
});
});
document.addEventListener('click', (e) => {
if (e.ctrlKey && e.target.tagName === 'ME-TPC') {
const currentNodeObj = mind.currentNode?.nodeObj
if (currentNodeObj) {
window.chrome.webview.postMessage({
action: 'nodeNavigate',
nodeId: currentNodeObj.id,
nodeTopic: currentNodeObj.topic,
nodeData: currentNodeObj.data,
});
}
}
});
document.addEventListener('keydown', function(e) {
if (e.key === ' ' || e.key === 'Spacebar') {
e.preventDefault();
const targetNodeId = mind.currentNode?.nodeObj?.id
if (!targetNodeId) return;
const targetNode = MindElixir.E(targetNodeId);
if (targetNode) mind.expandNode(targetNode);
}
});
}
window.addChildNode = function(topic = 'New Child Node', codeInfoObject) {
if (!mind) {
console.error(""Mind map not initialized"");
return { success: false, error: ""Mind map not initialized"" };
}
if (topic.startsWith('""') && topic.endsWith('""')) {
topic = topic.substring(1, topic.length - 1);
}
const { fileName, filePath, topLine } = codeInfoObject;
const codeInfo = { fileName, filePath, topLine };
const childData = {
id: 'child_' + Date.now(),
topic: topic,
children: [],
data: codeInfo,
tags: [codeInfo.fileName]
};
try {
const targetNodeId = mind.currentNode?.nodeObj?.id || lastSelectedNode?.id || 'me-root';
const targetNode = MindElixir.E(targetNodeId);
if (!targetNode) targetNode = MindElixir.E('me-root');
if (!targetNode) return { success: false, error: ""Target node not found"" };
mind.addChild(targetNode, childData);
mind.selectNode(MindElixir.E(childData.id));
return {
success: true,
nodeId: childData.id,
parentId: targetNodeId
};
} catch (e) {
console.error(""Error adding child node:"", e);
return { success: false, error: e.message };
}
};
window.exportData = function() {
if (!mind) return { success: false, error: ""Mind map not initialized"" };
try {
return JSON.stringify(JSON.parse(mind.getDataString()), null, 2);
} catch (e) {
console.error(""Error exporting data:"", e);
return { success: false, error: e.message };
}
};
function getThemeName(mindElixirData) {
try {
// First, check if the input is valid
if (!mindElixirData || typeof mindElixirData !== 'object') {
return '';
}
// Safely navigate through the nested structure
const theme = mindElixirData.theme ||
(mindElixirData.nodeData && mindElixirData.nodeData.theme);
// Check if theme exists and has a name property
if (theme && typeof theme === 'object' && theme.name) {
return String(theme.name);
}
return '';
} catch (error) {
// Catch any unexpected errors and return empty string
return '';
}
}
window.importData = function(mindDataString = '') {
if (!mind) return { success: false, error: ""Mind map not initialized"" };
if (mindDataString == '') return { success: false, error: ""No mind map data"" };
try {
if (mindDataString.startsWith('""') && mindDataString.endsWith('""')) {
mindDataString = mindDataString.substring(1, mindDataString.length - 1);
}
const mindData = JSON.parse(mindDataString);
// Migrate v3/v4 linkData to v5 arrows format
if (mindData.linkData && !mindData.arrows) {
mindData.arrows = Object.values(mindData.linkData);
delete mindData.linkData;
}
// mind.refresh() does not restore direction (unlike mind.init()),
// so apply it manually before calling refresh so layout() picks it up.
if (typeof mindData.direction === 'number') {
mind.direction = mindData.direction;
}
mind.refresh(mindData);
mind.clearHistory();
const dataThemeName = getThemeName(mindData);
if (dataThemeName != '' && themeManager.contains(dataThemeName) && dataThemeName != mind.theme?.name) {
mind.changeTheme(themeManager.getTheme(dataThemeName));
}
return { success: true, error: """" };
} catch (e) {
console.error(""Error importing data:"", e);
return { success: false, error: e.message };
}
};
window.toggleColorScheme = function() {
if (!mind) return { success: false, error: ""Mind map not initialized"" };
try {
const availableThemeKeys = themeManager.getAvailableThemes();
const currentThemeName = mind.theme?.name;
const themeKeyToApply = availableThemeKeys.find(key =>
themeManager.getTheme(key)?.name !== currentThemeName
);
if (themeKeyToApply) {
const themeToApply = themeManager.getTheme(themeKeyToApply);
mind.changeTheme(themeToApply);
return { success: true, error: '' };
}
return { success: false, error: 'No alternative theme found' };
} catch (e) {
console.error(""Error toggling the color scheme:"", e);
return { success: false, error: e.message };
}
};
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', initMindMap);
} else {
initMindMap();
}
</script>
</body>
</html>";
public string DirectoryPath { get; }
public string FilePath { get; }
public string DirectoryName { get; }
public CodeMindMapHtml(string vsTempPath)
: this(vsTempPath, null)
{
_vsTempPath = vsTempPath;
}
private readonly string _vsTempPath;
public CodeMindMapHtml(string vsTempPath, string directoryName)
{
_vsTempPath = vsTempPath;
DirectoryName = directoryName ?? Path.GetRandomFileName();
DirectoryPath = Path.Combine(_vsTempPath, DirectoryName);
FilePath = Path.Combine(DirectoryPath, DefaultFileName);
CreateMindMapFile(DirectoryPath, FilePath);
CopyMindElixir(DirectoryPath);
}
private static void CreateMindMapFile(string directoryPath, string filePath)
{
try
{
if (!Directory.Exists(directoryPath))
{
Directory.CreateDirectory(directoryPath);
}
File.WriteAllText(filePath, DefaultHtmlContent);
}
catch (Exception exception)
{
Debug.WriteLine($"Error creating a mind map file: {exception.Message}");
}
}
private static void CopyMindElixir(string directoryPath)
{
try
{
if (!Directory.Exists(directoryPath))
{
Directory.CreateDirectory(directoryPath);
}
string extensionAssemblyLocation = System.Reflection.Assembly.GetExecutingAssembly().Location;
string extensionDirectory = Path.GetDirectoryName(extensionAssemblyLocation);
string mindElixirDir = Path.Combine(extensionDirectory, "MindElixir");
string[] files = Directory.GetFiles(mindElixirDir);
foreach (string file in files)
{
string fileName = Path.GetFileName(file);
string destFile = Path.Combine(directoryPath, fileName);
File.Copy(file, destFile, true);
}
}
catch (Exception exception)
{
Debug.WriteLine($"Error copying mind elixir files: {exception.Message}");
}
}
public static string GetVsTempRootPath(IServiceProvider serviceProvider)
{
try
{
ThreadHelper.ThrowIfNotOnUIThread();
var shell = serviceProvider.GetService(typeof(SVsShell)) as IVsShell;
if (shell == null)
{
return string.Empty;
}
if (shell.GetProperty((int)__VSSPROPID.VSSPROPID_VirtualRegistryRoot, out object registryRoot) != VSConstants.S_OK)
{
return GetLocalApplicationDataDirPath();
}
string versionFolder = Path.GetFileName(registryRoot.ToString());
string vsTempPath = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
"Microsoft",
"VisualStudio",
versionFolder,
TempDirName);
return vsTempPath;
}
catch (Exception exception)
{
Debug.WriteLine($"Error getting VSSPROPID_VirtualRegistryRoot: {exception.Message}");
return GetLocalApplicationDataDirPath();
}
}
public static string GetLocalApplicationDataDirPath()
{
const string LocalAppDataDirName = "CodeMindMap";
return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), LocalAppDataDirName, TempDirName);
}
}
}