11import { ViewColumn , window } from "vscode" ;
2+ import * as vscode from 'vscode' ;
3+ import { ContextProvider } from "../../contextProvider" ;
24
3- type Styles = { [ key : string ] : string } ;
5+ export type Styles = { [ key : string ] : string } ;
46
57export interface Element {
68 data : { id : string , label : string } ,
7- style : Styles
9+ style : Styles ,
10+ classes : string
811}
912
1013export interface Edge {
@@ -36,7 +39,8 @@ export class CytoscapeGraph {
3639
3740 this . elements . push ( {
3841 data : { id, label : node . label } ,
39- style : node . styles || { }
42+ style : node . styles || { } ,
43+ classes : ".l1"
4044 } ) ;
4145
4246 if ( node . parent ) {
@@ -50,7 +54,7 @@ export class CytoscapeGraph {
5054
5155 createView ( title : string , onNodeSelected : ( data : unknown ) => void ) : any {
5256 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 ) ;
5458
5559 webview . webview . onDidReceiveMessage ( ( message ) => {
5660 if ( message . command === 'selected' ) {
@@ -62,98 +66,33 @@ export class CytoscapeGraph {
6266 return webview ;
6367 }
6468
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+
6678 return /*html*/ `
6779 <!DOCTYPE html>
6880 <html lang="en">
6981
7082 <head>
7183 <meta charset="UTF-8">
7284 <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>
9492 </head>
95-
9693 <body>
9794 <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>
15595 </body>
156-
15796 </html>
15897 ` ;
15998 }
0 commit comments