-
Notifications
You must be signed in to change notification settings - Fork 1
/
editor.js
85 lines (79 loc) · 3.57 KB
/
editor.js
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
const codeEditor = document.getElementById('code-editor');
const cursor = document.getElementById('text-cursor'); // Get the cursor element
let currentLine = document.createElement('span');
codeEditor.appendChild(currentLine);
// Initialize cursor position
let cursorX = 0; // Horizontal position within the line
let cursorY = 0; // Line number
// List of keys to exclude
const excludedKeys = ['Shift', 'Meta', 'Control', 'Alt', 'Escape', 'CapsLock', 'F1', 'F2', 'F3', 'Tab'];
document.addEventListener('keydown', (event) => {
const key = event.key;
// Check if the key is not in the list of excluded keys
if (!excludedKeys.includes(key)) {
if (key === 'Enter') {
// Create a new line (span) on Enter key press
currentLine = document.createElement('span');
codeEditor.appendChild(currentLine);
// Move the cursor to the start of the new line
cursorX = 0;
cursorY++;
} else if (key === 'Backspace') {
// Move the cursor to the left within the current line and delete the character
const text = currentLine.textContent;
if (cursorX > 0) {
cursorX--;
const newText = text.slice(0, cursorX) + text.slice(cursorX + 1);
currentLine.textContent = newText;
} else if (codeEditor.children.length > 1) {
// Move to the previous line and delete the last character
codeEditor.removeChild(currentLine);
cursorY--;
currentLine = codeEditor.lastElementChild;
const prevText = currentLine.textContent;
cursorX = prevText.length;
const newText = prevText + text;
currentLine.textContent = newText;
}
} else if (key === 'ArrowLeft') {
// Move the cursor left within the current line
if (cursorX > 0) {
cursorX--;
}
} else if (key === 'ArrowRight') {
// Move the cursor right within the current line
const text = currentLine.textContent;
if (cursorX < text.length) {
cursorX++;
}
} else if (key === 'ArrowUp') {
// Move the cursor up to the previous line
if (cursorY > 0) {
cursorY--;
const lines = codeEditor.children;
currentLine = lines[cursorY];
const text = currentLine.textContent;
cursorX = Math.min(cursorX, text.length);
}
} else if (key === 'ArrowDown') {
// Move the cursor down to the next line
const lines = codeEditor.children;
if (cursorY < lines.length - 1) {
cursorY++;
currentLine = lines[cursorY];
const text = currentLine.textContent;
cursorX = Math.min(cursorX, text.length);
}
} else {
// Append the pressed key to the current line at the cursor position
const text = currentLine.textContent;
const newText = text.slice(0, cursorX) + key + text.slice(cursorX);
currentLine.textContent = newText;
cursorX++;
}
// Update the cursor position
const lineHeight = 16; // Adjust the line height if needed
cursor.style.top = (cursorY * lineHeight) + 'px';
cursor.style.left = (cursorX * 7.15) + 'px'; // Adjust the character width as needed
}
});