|
| 1 | +var cy; |
| 2 | + |
| 3 | +function generate_cy_stylesheet(cy) { |
| 4 | + // This is a bit of a hack but it works |
| 5 | + html_style = document.getElementsByTagName('html')[0].style; |
| 6 | + |
| 7 | + // From https://js.cytoscape.org/#core/style |
| 8 | + cy.style() |
| 9 | + .fromJson([ |
| 10 | + { |
| 11 | + selector: 'node', |
| 12 | + style: { |
| 13 | + label: "data(id)", |
| 14 | + shape: "round-rectangle", |
| 15 | + "background-color": html_style.getPropertyValue('--vscode-button-background'), |
| 16 | + "background-width": "90%", |
| 17 | + "background-height": "90%", |
| 18 | + width: "228", |
| 19 | + height: "128", |
| 20 | + "border-width": "0", |
| 21 | + } |
| 22 | + }, |
| 23 | + { |
| 24 | + selector: 'label', |
| 25 | + style: { |
| 26 | + color: html_style.getPropertyValue('--vscode-button-foreground'), |
| 27 | + 'font-family': '"Segoe UI", Arial, Helvetica, sans-serif', |
| 28 | + 'font-size': '28vh', |
| 29 | + 'text-valign': 'center', |
| 30 | + 'text-halign': 'center' |
| 31 | + } |
| 32 | + }, |
| 33 | + { |
| 34 | + selector: ':selected', |
| 35 | + style: { |
| 36 | + 'border-width': '4', |
| 37 | + 'border-color': html_style.getPropertyValue('--vscode-editor-hoverHighlightBackground'), |
| 38 | + 'background-color': html_style.getPropertyValue('--vscode-button-hoverBackground'), |
| 39 | + 'line-color': html_style.getPropertyValue('--vscode-minimap-errorHighlight'), |
| 40 | + 'target-arrow-color': html_style.getPropertyValue('--vscode-minimap-errorHighlight'), |
| 41 | + 'source-arrow-color': html_style.getPropertyValue('--vscode-minimap-errorHighlight') |
| 42 | + } |
| 43 | + }, |
| 44 | + { |
| 45 | + selector: 'edge', |
| 46 | + style: { |
| 47 | + 'target-arrow-shape': 'triangle', |
| 48 | + 'curve-style': 'bezier', |
| 49 | + 'control-point-step-size': 40, |
| 50 | + width: 10 |
| 51 | + } |
| 52 | + } |
| 53 | + ]) |
| 54 | + .update(); // indicate the end of your new stylesheet so that it can be updated on elements |
| 55 | +} |
| 56 | + |
| 57 | +function init() { |
| 58 | + vscode = acquireVsCodeApi(); |
| 59 | + |
| 60 | + cy = cytoscape({ |
| 61 | + container: document.getElementById('cy'), |
| 62 | + wheelSensitivity: 0.15, |
| 63 | + maxZoom: 5, |
| 64 | + minZoom: 0.2, |
| 65 | + selectionType: 'single' |
| 66 | + }); |
| 67 | + |
| 68 | + generate_cy_stylesheet(cy); |
| 69 | + |
| 70 | + vscode.postMessage({ command: 'initialized' }); |
| 71 | +} |
| 72 | + |
| 73 | +window.addEventListener('message', event => { |
| 74 | + const message = event.data; |
| 75 | + if(message.redraw == true){ |
| 76 | + cy.remove('*'); |
| 77 | + } |
| 78 | + |
| 79 | + nodeGraph = message.content; |
| 80 | + |
| 81 | + try { |
| 82 | + nodeGraph.vertices.forEach(element => { |
| 83 | + cy.add({ |
| 84 | + data: { id: element.label } |
| 85 | + }); |
| 86 | + }); |
| 87 | + |
| 88 | + nodeGraph.edges.forEach(element => { |
| 89 | + cy.add({ |
| 90 | + data: { |
| 91 | + id: element.source + element.target, |
| 92 | + source: element.source, |
| 93 | + target: element.target |
| 94 | + } |
| 95 | + }); |
| 96 | + }); |
| 97 | + } catch (error) { |
| 98 | + vscode.postMessage({ |
| 99 | + command: 'error', |
| 100 | + errorMsg: `Error building node graph from json graph data: ${error}` |
| 101 | + }); |
| 102 | + } |
| 103 | + |
| 104 | + cy.layout({ |
| 105 | + avoidOverlap: true, |
| 106 | + name: 'breadthfirst', |
| 107 | + circle: false, |
| 108 | + nodeDimensionsIncludeLabels: true, |
| 109 | + spacingFactor: 1.5, |
| 110 | + animate: true |
| 111 | + }).run(); |
| 112 | +}); |
0 commit comments