-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnodes.js
125 lines (103 loc) · 3.05 KB
/
nodes.js
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
const vscode = require('vscode');
const fs = require('fs');
const path = require('path');
async function nodes(context) {
let disposable = vscode.commands.registerCommand('marlin.showNodes', async () => {
const panel = vscode.window.createWebviewPanel(
'nodesView',
'Nodes',
vscode.ViewColumn.Beside,
{
enableScripts: true
}
);
const editor = vscode.window.activeTextEditor;
const document = editor.document;
const gcodeContent = document.getText();
const nodesData = parseGcodeToNodes(gcodeContent);
panel.webview.html = getNodesHtml(nodesData);
});
panel.webview.onDidReceiveMessage(
(message) => {
switch (message.command) {
case 'navigateToLine':
navigateToLine(message.lineNumber);
return;
}
},
undefined,
context.subscriptions
);
context.subscriptions.push(disposable);
}
function navigateToLine(lineNumber) {
const editor = vscode.window.activeTextEditor;
const position = new vscode.Position(lineNumber, 0);
editor.selection = new vscode.Selection(position, position);
editor.revealRange(new vscode.Range(position, position));
}
function parseGcodeToNodes(gcodeContent) {
const lines = gcodeContent.split('\n');
const nodes = [];
lines.forEach((line, index) => {
if (line.startsWith(';')) {
const matchParam = line.match(/;(.+):(.+)/);
if (matchParam) {
nodes.push({
type: 'parameter',
name: matchParam[1].trim(),
value: matchParam[2].trim(),
lineNumber: index,
x: 0,
y: nodes.length * 30 + 20
});
} else {
nodes.push({
type: 'comment',
value: line.substr(1).trim(),
lineNumber: index,
x: 0,
y: nodes.length * 30 + 20
});
}
}
});
return nodes;
}
function getNodesHtml(nodesData) {
// Read the content of the nodes.html file
const nodesHtml = fs.readFileSync(
path.join(__dirname, 'nodes.html'),
'utf-8'
);
// Replace the NODES_DATA_PLACEHOLDER with the actual nodesData
const filledNodesHtml = nodesHtml.replace(
'NODES_DATA_PLACEHOLDER',
JSON.stringify(nodesData)
);
// Include the webview.js script in the nodes.html content
const scriptTag =
'<script src="vscode-resource:' +
path.join(__dirname, 'webview.js') +
'"></script>';
const nodesHtmlWithClickEvent = filledNodesHtml.replace(
'</head>',
`${scriptTag}</head>`
);
// Add the click event to the nodes using the onNodeClick() function
const clickEventCode = `
<script>
const nodes = d3.selectAll('.node');
nodes.on('click', function(event, d) {
window.onNodeClick(d.lineNumber);
});
</script>`;
const finalNodesHtml = nodesHtmlWithClickEvent.replace(
'</body>',
`${clickEventCode}</body>`
);
return finalNodesHtml;
}
module.exports = {
nodes
};