1
1
import { ViewColumn , window } from "vscode" ;
2
+ import * as vscode from 'vscode' ;
3
+ import { ContextProvider } from "../../contextProvider" ;
2
4
3
- type Styles = { [ key : string ] : string } ;
5
+ export type Styles = { [ key : string ] : string } ;
4
6
5
7
export interface Element {
6
8
data : { id : string , label : string } ,
7
- style : Styles
9
+ style : Styles ,
10
+ classes : string
8
11
}
9
12
10
13
export interface Edge {
@@ -36,7 +39,8 @@ export class CytoscapeGraph {
36
39
37
40
this . elements . push ( {
38
41
data : { id, label : node . label } ,
39
- style : node . styles || { }
42
+ style : node . styles || { } ,
43
+ classes : ".l1"
40
44
} ) ;
41
45
42
46
if ( node . parent ) {
@@ -50,7 +54,7 @@ export class CytoscapeGraph {
50
54
51
55
createView ( title : string , onNodeSelected : ( data : unknown ) => void ) : any {
52
56
const webview = window . createWebviewPanel ( `c` , title , { viewColumn : ViewColumn . One } , { enableScripts : true , retainContextWhenHidden : true } ) ;
53
- webview . webview . html = this . getHtml ( ) ;
57
+ webview . webview . html = this . getHtml ( webview . webview ) ;
54
58
55
59
webview . webview . onDidReceiveMessage ( ( message ) => {
56
60
if ( message . command === 'selected' ) {
@@ -62,98 +66,33 @@ export class CytoscapeGraph {
62
66
return webview ;
63
67
}
64
68
65
- private getHtml ( ) : string {
69
+ private getHtml ( webview : vscode . Webview ) : string {
70
+ const data = JSON . stringify ( [ ...this . elements , ...this . edges ] )
71
+ const context = ContextProvider . getContext ( )
72
+ const cssUri = webview . asWebviewUri ( vscode . Uri . joinPath ( context . extensionUri , 'src' , 'views' , 'cytoscape' , 'media' , 'explain.css' ) )
73
+ const cytoscapeUri = webview . asWebviewUri ( vscode . Uri . joinPath ( context . extensionUri , 'src' , 'views' , 'cytoscape' , 'media' , 'cytoscape.min.js' ) )
74
+ const cytoscapeHtmlLabelUri = webview . asWebviewUri ( vscode . Uri . joinPath ( context . extensionUri , 'src' , 'views' , 'cytoscape' , 'media' , 'cytoscape-node-html-label.min.js' ) )
75
+ const explainUri = webview . asWebviewUri ( vscode . Uri . joinPath ( context . extensionUri , 'src' , 'views' , 'cytoscape' , 'media' , 'explain.js' ) )
76
+
77
+
66
78
return /*html*/ `
67
79
<!DOCTYPE html>
68
80
<html lang="en">
69
81
70
82
<head>
71
83
<meta charset="UTF-8">
72
84
<meta name="viewport" content="width=device-width, initial-scale=1.0">
73
- <script src="https://cdnjs.cloudflare.com/ajax/libs/cytoscape/3.23.0/cytoscape.min.js"></script>
74
- <style>
75
- /* html,
76
- body {
77
- margin: 0;
78
- padding: 0;
79
- height: 100%;
80
- width: 100%;
81
- overflow: hidden;
82
- } */
83
-
84
- .diagram-container {
85
- position: absolute;
86
- top: 0;
87
- left: 0;
88
- width: 100%;
89
- height: 100%;
90
- border: none;
91
- margin: 0;
92
- }
93
- </style>
85
+ <link href="${ cssUri } " rel="stylesheet" />
86
+ <script src="${ cytoscapeUri } "></script>
87
+ <script src="${ cytoscapeHtmlLabelUri } "></script>
88
+ <script src="${ explainUri } " defer></script>
89
+ <script>
90
+ window.data = ${ data } ;
91
+ </script>
94
92
</head>
95
-
96
93
<body>
97
94
<div class="diagram-container" id="diagramContainer"></div>
98
-
99
- <script>
100
- const vscode = acquireVsCodeApi();
101
- document.addEventListener("DOMContentLoaded", function () {
102
- // Initialize Cytoscape
103
- const cy = cytoscape({
104
- container: document.getElementById('diagramContainer'),
105
-
106
- elements: ${ JSON . stringify ( [ ...this . elements , ...this . edges ] ) } ,
107
-
108
- style: [
109
- {
110
- selector: 'node',
111
- style: {
112
- 'width': '120px',
113
- 'height': '60px',
114
- 'background-color': 'var(--vscode-list-activeSelectionBackground)',
115
- 'color': 'var(--vscode-list-activeSelectionForeground)',
116
- 'label': 'data(label)',
117
- 'text-valign': 'center',
118
- 'text-halign': 'center',
119
- 'font-size': '14px',
120
- 'text-wrap': 'wrap',
121
- 'text-max-width': '100px'
122
- }
123
- },
124
- {
125
- selector: 'edge',
126
- style: {
127
- 'width': 2,
128
- 'line-color': '#5c96bc',
129
- 'target-arrow-color': '#5c96bc',
130
- 'target-arrow-shape': 'triangle',
131
- 'curve-style': 'bezier'
132
- }
133
- }
134
- ],
135
-
136
- // Layout options
137
- layout: {
138
- name: 'breadthfirst',
139
- directed: true, // Directional tree
140
- padding: 10, // Padding around the graph
141
- spacingFactor: 1.5 // Spacing between nodes
142
- }
143
- });
144
-
145
- // Add click event to show alert for nodes
146
- cy.on('tap', 'node', function (evt) {
147
- const id = evt.target.id();
148
- vscode.postMessage({
149
- command: 'selected',
150
- nodeId: id
151
- });
152
- });
153
- });
154
- </script>
155
95
</body>
156
-
157
96
</html>
158
97
` ;
159
98
}
0 commit comments