-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvisual-editor.js
More file actions
296 lines (254 loc) · 8.06 KB
/
Copy pathvisual-editor.js
File metadata and controls
296 lines (254 loc) · 8.06 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
/**
* Visual Editor Module
* Handles visual mode functionality with edge list display and sync
*/
import { $, $$ } from '../utils/dom.js';
import { getState, setState, subscribe } from '../core/state.js';
import { parseEdgeInput } from '../graph/sigma-controller.js';
import { deleteSelectedElements, getGraph } from '../graph/sigma-adapter.js';
let isVisualMode = false;
let originalTextValue = ''; // Store original text when entering visual mode
let buttonClickHandler = null; // Store button handler for cleanup
let selectionChangeHandler = null; // Listen for graph selection updates
/**
* Initialize visual editor functionality
*/
export const initVisualEditor = () => {
// Subscribe to edge list update requests via state.js pub/sub
// Replaces window.updateEdgeListDisplay global
subscribe('ui.edgeListNeedsUpdate', () => {
if (isVisualMode) {
updateEdgeListDisplay();
}
});
// Subscribe to editor mode changes
subscribe('ui.editorMode', (mode) => {
isVisualMode = mode === 'visual';
if (isVisualMode) {
syncTextToVisual();
setupButtonHandler();
updateButtonContent();
} else {
syncVisualToText();
removeButtonHandler();
}
});
// Subscribe to selection changes to update button
subscribe('ui.selectedNodes', () => {
if (isVisualMode) {
updateButtonContent();
}
});
subscribe('ui.selectedEdges', () => {
if (isVisualMode) {
updateButtonContent();
}
});
if (!selectionChangeHandler) {
selectionChangeHandler = () => {
if (isVisualMode) {
updateButtonContent();
}
};
document.addEventListener('euler:selection-changed', selectionChangeHandler);
}
};
/**
* Sync text input to visual mode display
*/
const syncTextToVisual = () => {
const textInput = $('#edges');
if (!textInput) {
return;
}
// Store original text value
originalTextValue = textInput.value;
try {
const edges = parseEdgeInput(textInput.value);
displayEdgeList(edges);
} catch (error) {
displayEdgeList([]);
showParseError(error.message);
}
};
/**
* Sync visual mode changes back to text input
* Extract current graph state and convert to text format
*/
const syncVisualToText = () => {
const textInput = $('#edges');
if (!textInput) {
return;
}
try {
// Get current graph from Sigma
const graphInstance = getGraph();
if (!graphInstance) {
textInput.value = originalTextValue;
return;
}
// Extract edges from graph using labels instead of IDs
const edges = [];
graphInstance.forEachEdge((edge, attributes, source, target) => {
// Get node labels for better readability
const sourceLabel = graphInstance.getNodeAttribute(source, 'label') || source;
const targetLabel = graphInstance.getNodeAttribute(target, 'label') || target;
const weight = attributes.weight || 1;
edges.push({
source: sourceLabel,
target: targetLabel,
weight: weight
});
});
// Check if graph is weighted by looking at state
const isWeighted = getState('graph.weighted');
// Generate edge string in the correct format
let edgeStr = '';
if (isWeighted) {
edgeStr = edges.map(e => `[${e.source},${e.target},${e.weight}]`).join(',');
} else {
edgeStr = edges.map(e => `[${e.source},${e.target}]`).join(',');
}
// Update the input
textInput.value = edgeStr;
} catch (error) {
// Fallback to original text if there's an error
textInput.value = originalTextValue;
}
};
/**
* Update button content based on mode and selection
*/
const updateButtonContent = () => {
const button = $('#add-edge');
if (!button) return;
const selectedNodes = getState('ui.selectedNodes') || [];
const selectedEdges = getState('ui.selectedEdges') || [];
if (selectedNodes.length === 0 && selectedEdges.length === 0) {
// No selection: show instruction
button.innerHTML = '<i class="fas fa-mouse-pointer"></i> SELECT NODE TO BEGIN';
button.disabled = true;
button.style.opacity = '0.6';
} else if (selectedNodes.length === 1 && selectedEdges.length === 0) {
// One node selected: show selected node name and delete option
const selectedNodeId = selectedNodes[0];
button.innerHTML = `<span>"${selectedNodeId}"</span> | <i class="fas fa-trash"></i> DELETE`;
button.disabled = false;
button.style.opacity = '1';
} else {
// Multiple selections: show delete option
const parts = [];
if (selectedNodes.length > 0) {
parts.push(`${selectedNodes.length} NODE${selectedNodes.length === 1 ? '' : 'S'}`);
}
if (selectedEdges.length > 0) {
parts.push(`${selectedEdges.length} EDGE${selectedEdges.length === 1 ? '' : 'S'}`);
}
button.innerHTML = `<span>${parts.join(' / ')}</span> | <i class="fas fa-trash"></i> DELETE`;
button.disabled = false;
button.style.opacity = '1';
}
};
/**
* Update the edge list display from current Sigma graph
*/
const updateEdgeListDisplay = () => {
if (!isVisualMode) return;
try {
// Get current graph from Sigma
const graphInstance = getGraph();
if (!graphInstance) return;
// Extract edges from graph using labels instead of IDs
const edges = [];
graphInstance.forEachEdge((edge, attributes, source, target) => {
// Get node labels for better readability
const sourceLabel = graphInstance.getNodeAttribute(source, 'label') || source;
const targetLabel = graphInstance.getNodeAttribute(target, 'label') || target;
edges.push({
source: sourceLabel,
target: targetLabel,
weight: attributes.weight || 1
});
});
// Display the updated edge list
displayEdgeList(edges);
} catch (error) {
// Error updating edge list - silently ignore
}
};
/**
* Handle button click based on current state
*/
const handleButtonClick = () => {
const selectedNodes = getState('ui.selectedNodes') || [];
const selectedEdges = getState('ui.selectedEdges') || [];
if (selectedNodes.length > 0 || selectedEdges.length > 0) {
// Delete the current selection using the graph adapter
deleteSelectedElements();
// Sync the edge list display after deletion
updateEdgeListDisplay();
}
// If no selection, button is disabled so nothing happens
};
/**
* Set up button handler for visual mode
*/
const setupButtonHandler = () => {
const button = $('#add-edge');
if (button && !buttonClickHandler) {
buttonClickHandler = handleButtonClick;
button.addEventListener('click', buttonClickHandler);
}
};
/**
* Remove button handler when leaving visual mode
*/
const removeButtonHandler = () => {
const button = $('#add-edge');
if (button && buttonClickHandler) {
button.removeEventListener('click', buttonClickHandler);
buttonClickHandler = null;
// Restore original button
button.innerHTML = '<i class="fas fa-plus"></i> ADD EDGE';
button.disabled = false;
button.style.opacity = '1';
}
};
/**
* Display edge list in visual mode
*/
const displayEdgeList = (edges) => {
const container = $('#edge-list');
if (!container) {
return;
}
container.innerHTML = '';
if (edges.length === 0) {
container.innerHTML = '<div class="edge-item">No edges to display</div>';
return;
}
edges.forEach((edge, index) => {
const edgeItem = document.createElement('div');
edgeItem.className = 'edge-item';
edgeItem.innerHTML = `
<span class="source-label">FROM:</span>
<span class="edge-vertex">${edge.source}</span>
<span class="target-label">TO:</span>
<span class="edge-vertex">${edge.target}</span>
${edge.weight !== 1 ? `<span class="weight-label">WEIGHT: ${edge.weight}</span>` : ''}
`;
container.appendChild(edgeItem);
});
};
/**
* Show parse error in edge list
*/
const showParseError = (message) => {
const container = $('#edge-list');
if (!container) return;
container.innerHTML = `
<div class="edge-item error">
<span style="color: var(--error-color);">⚠️ Parse Error: ${message}</span>
</div>
`;
};