-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheditor.js
More file actions
336 lines (285 loc) · 11.4 KB
/
Copy patheditor.js
File metadata and controls
336 lines (285 loc) · 11.4 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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/**
* Editor Controller - Manages the text editor UI and interactions
*
* HARDEST BUGS FIXED:
* 1. Cursor jump bug: Local cursor jumps to wrong position after remote edits
* - Fix: Store cursor as offset from end, restore after remote updates
*
* 2. Infinite loop: setText triggers input event, which triggers setText again
* - Fix: Flag to prevent recursive updates during programmatic changes
*
* 3. Selection lost during rapid typing
* - Fix: Debounced updates with selection preservation
*/
class EditorController {
constructor(textareaElement, crdt, syncEngine) {
this.textarea = textareaElement;
this.crdt = crdt;
this.syncEngine = syncEngine;
this.isUpdating = false; // Prevent recursive updates
this.localEdits = 0;
this.remoteEdits = 0;
this.lastCursorPosition = 0;
this.lastSelectionStart = 0;
this.lastSelectionEnd = 0;
// Debounce cursor updates to reduce network traffic
this.cursorUpdateTimeout = null;
this.setupEventListeners();
this.updateLineNumbers();
}
setupEventListeners() {
// Handle text input
this.textarea.addEventListener('input', (e) => {
if (this.isUpdating) return;
this.handleInput(e);
});
// Handle cursor movement
this.textarea.addEventListener('selectionchange', () => {
this.updateCursorPosition();
});
this.textarea.addEventListener('click', () => {
this.updateCursorPosition();
});
this.textarea.addEventListener('keyup', (e) => {
// Update cursor on arrow keys, home, end, etc.
if (e.key.startsWith('Arrow') || e.key === 'Home' || e.key === 'End') {
this.updateCursorPosition();
}
});
// Prevent tab from losing focus
this.textarea.addEventListener('keydown', (e) => {
if (e.key === 'Tab') {
e.preventDefault();
this.insertText(' '); // Insert 4 spaces
}
});
}
// Handle text input events
handleInput(e) {
const currentText = this.textarea.value;
const previousText = this.crdt.getText();
if (currentText === previousText) {
return; // No actual change
}
// BUG FIX: Calculate diff to determine what changed
// This is more accurate than relying on input event data
const changes = this.calculateDiff(previousText, currentText);
changes.forEach(change => {
if (change.type === 'insert') {
for (let i = 0; i < change.text.length; i++) {
const char = change.text[i];
const position = change.position + i;
const operation = this.crdt.localInsert(position, char);
this.syncEngine.sendOperation(operation);
this.localEdits++;
}
} else if (change.type === 'delete') {
// Delete in reverse to maintain correct positions
for (let i = change.count - 1; i >= 0; i--) {
const position = change.position + i;
const operation = this.crdt.localDelete(position);
if (operation) {
this.syncEngine.sendOperation(operation);
this.localEdits++;
}
}
}
});
this.updateLineNumbers();
this.updateStats();
}
// Calculate difference between two strings
calculateDiff(oldText, newText) {
const changes = [];
// Find common prefix
let prefixLen = 0;
while (prefixLen < oldText.length &&
prefixLen < newText.length &&
oldText[prefixLen] === newText[prefixLen]) {
prefixLen++;
}
// Find common suffix
let suffixLen = 0;
while (suffixLen < (oldText.length - prefixLen) &&
suffixLen < (newText.length - prefixLen) &&
oldText[oldText.length - 1 - suffixLen] === newText[newText.length - 1 - suffixLen]) {
suffixLen++;
}
const oldMid = oldText.substring(prefixLen, oldText.length - suffixLen);
const newMid = newText.substring(prefixLen, newText.length - suffixLen);
if (oldMid.length > 0 && newMid.length > 0) {
// Both delete and insert (replacement)
changes.push({
type: 'delete',
position: prefixLen,
count: oldMid.length
});
changes.push({
type: 'insert',
position: prefixLen,
text: newMid
});
} else if (oldMid.length > 0) {
// Only delete
changes.push({
type: 'delete',
position: prefixLen,
count: oldMid.length
});
} else if (newMid.length > 0) {
// Only insert
changes.push({
type: 'insert',
position: prefixLen,
text: newMid
});
}
return changes;
}
// Insert text at current cursor position
insertText(text) {
const start = this.textarea.selectionStart;
const end = this.textarea.selectionEnd;
const currentText = this.textarea.value;
const newText = currentText.substring(0, start) + text + currentText.substring(end);
this.textarea.value = newText;
this.textarea.selectionStart = this.textarea.selectionEnd = start + text.length;
// Trigger input event
this.textarea.dispatchEvent(new Event('input'));
}
// Update cursor position and send to peers
updateCursorPosition() {
const position = this.textarea.selectionStart;
const selection = {
start: this.textarea.selectionStart,
end: this.textarea.selectionEnd
};
if (position !== this.lastCursorPosition ||
selection.start !== this.lastSelectionStart ||
selection.end !== this.lastSelectionEnd) {
this.lastCursorPosition = position;
this.lastSelectionStart = selection.start;
this.lastSelectionEnd = selection.end;
// Debounce cursor updates
clearTimeout(this.cursorUpdateTimeout);
this.cursorUpdateTimeout = setTimeout(() => {
this.syncEngine.sendCursor(position, selection);
this.updateFooterStats();
}, 100);
}
}
// Handle remote operation
handleRemoteOperation(operation) {
// BUG FIX: Save cursor position as offset from end
// This prevents cursor from jumping when remote edits happen before cursor
const cursorPos = this.textarea.selectionStart;
const cursorFromEnd = this.textarea.value.length - cursorPos;
const hadSelection = this.textarea.selectionStart !== this.textarea.selectionEnd;
const selectionStart = this.textarea.selectionStart;
const selectionEnd = this.textarea.selectionEnd;
// Update textarea with new CRDT state
this.isUpdating = true;
const newText = this.crdt.getText();
this.textarea.value = newText;
// Restore cursor position
// BUG FIX: Adjust cursor based on where the edit happened
let newCursorPos = cursorPos;
if (operation.type === 'insert') {
// If insertion happened before cursor, shift cursor right
const visiblePos = this.getVisiblePosition(operation.position);
if (visiblePos <= cursorPos) {
newCursorPos = cursorPos + 1;
}
} else if (operation.type === 'delete') {
// If deletion happened before cursor, shift cursor left
const visiblePos = this.getVisiblePosition(operation.position);
if (visiblePos < cursorPos) {
newCursorPos = Math.max(0, cursorPos - 1);
}
}
// Restore selection or cursor
if (hadSelection) {
this.textarea.selectionStart = selectionStart;
this.textarea.selectionEnd = selectionEnd;
} else {
this.textarea.selectionStart = newCursorPos;
this.textarea.selectionEnd = newCursorPos;
}
this.isUpdating = false;
this.remoteEdits++;
this.updateLineNumbers();
this.updateStats();
}
// Get visible position in text (excluding tombstones)
getVisiblePosition(crdtPosition) {
let visibleCount = 0;
for (let i = 0; i <= crdtPosition && i < this.crdt.characters.length; i++) {
if (this.crdt.characters[i].visible) {
visibleCount++;
}
}
return visibleCount - 1;
}
// Update line numbers
updateLineNumbers() {
const lineNumbersDiv = document.getElementById('lineNumbers');
if (!lineNumbersDiv) return;
const lineCount = this.textarea.value.split('\n').length;
const numbers = Array.from({ length: lineCount }, (_, i) => i + 1).join('\n');
lineNumbersDiv.textContent = numbers;
}
// Update stats in sidebar
updateStats() {
const text = this.textarea.value;
const lineCount = text.split('\n').length;
const charCount = text.length;
const lineCountEl = document.getElementById('lineCount');
const charCountEl = document.getElementById('charCount');
const editCountEl = document.getElementById('editCount');
if (lineCountEl) lineCountEl.textContent = lineCount;
if (charCountEl) charCountEl.textContent = charCount;
if (editCountEl) editCountEl.textContent = this.localEdits + this.remoteEdits;
}
// Update footer stats
updateFooterStats() {
const text = this.textarea.value;
const position = this.textarea.selectionStart;
// Calculate line and column
const beforeCursor = text.substring(0, position);
const lines = beforeCursor.split('\n');
const currentLine = lines.length;
const currentCol = lines[lines.length - 1].length + 1;
const currentLineEl = document.getElementById('currentLine');
const currentColEl = document.getElementById('currentCol');
const otOpsEl = document.getElementById('otOps');
if (currentLineEl) currentLineEl.textContent = currentLine;
if (currentColEl) currentColEl.textContent = currentCol;
if (otOpsEl) otOpsEl.textContent = this.crdt.operationHistory.length;
}
// Clear editor
clear() {
this.textarea.value = '';
this.crdt.characters = [];
this.crdt.operationHistory = [];
this.localEdits = 0;
this.remoteEdits = 0;
this.updateLineNumbers();
this.updateStats();
}
// Get editor value
getValue() {
return this.textarea.value;
}
// Set editor value
setValue(text) {
this.isUpdating = true;
this.textarea.value = text;
this.isUpdating = false;
this.updateLineNumbers();
this.updateStats();
}
}
// Export for use in other modules
if (typeof module !== 'undefined' && module.exports) {
module.exports = { EditorController };
}