-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
166 lines (134 loc) · 6.23 KB
/
main.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
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
//Try this, not certain it will work cause I’m having some electrical work done and all I have is my phone
const ty = `<span style='color:var(--attributeColor)'></span>`
const tAColor = '<span style=\'color:var\\(--attributeColor\\)\'>';
const endHigh = '</span>';
const attriRt = new RegExp("(?<=" + tAColor + ')[\\s\\S]*?' + "(?=" + endHigh + ")", "g");
//console.log(ty.match(attriRt))
const editorCon = document.getElementById('editor-container')
const editor = document.getElementById('code-area');
const keyx = document.getElementById('keyx')
const editorRenderer = document.getElementById('code-area-render');
const consoleOutput = document.getElementById('console-output');
const lineNumberCon = document.getElementById('line-number-container')
let lineNumber = 1;
editor.addEventListener('scroll', e => {
lineNumberConShadSync()
lineNumber_Editor_Vertical_Sync_Scroll()
editorRenderer.scrollTop = editor.scrollTop
editorRenderer.scrollLeft = editor.scrollLeft
})
function lineNumberAdjuster(event) {
if (event.inputType === "insertLineBreak") {
// Enter key is pressed
//console.log(lineNumber++)
lineNumberCon.innerText += String(++lineNumber + '\n')
//console.log(lineNumber.lastElementChild.innerText)
//lineNumberCon.appendChild(number)
}
const totalRealLineBreaks = editor.value.split('').filter(e => e === '\n').length + 1 //+1 due to 1 put there even without any content
/*if(totalRealLineBreaks > 15) editorCon.style.paddingBottom = "10em"
else editorCon.style.paddingBottom = "0"*/
if (lineNumber !== totalRealLineBreaks) {
const arrFilledWithCorrectLineCount = Array(totalRealLineBreaks).fill(0).map((_, index) => index + 1);
// console.log(totalRealLineBreaks, arrFilledWithCorrectLineCount)
lineNumberCon.innerText = arrFilledWithCorrectLineCount.join('\n')
// console.log(lineNumberCon.style.paddingLeft);
updateTextareaPadding()
}
}
function debounce(func, delay) {
let timer;
return (...args) => {
clearTimeout(timer);
timer = setTimeout(() => { func.apply(this, args); }, delay);
};
}
const debouncedUpdate = debounce(lineNumberAdjuster, 0) //wait 300 milliseconds.
editor.addEventListener('input', (event) => {
updateRenderElem(event.target.value)
debouncedUpdate(event)
}) //Debounce only runs when continuous action pends for an amount of time, this helps not to run the function everytime.
editor.addEventListener('paste', (event) => {
if (isCursorAtEndOfLastLine()) autoScroll()
})
function autoScroll() {
editor.scrollTop = editor.scrollHeight
}
const updateTextareaPadding = () => {
const lineNumberWidth = lineNumberCon.offsetWidth;
editor.style.paddingLeft = lineNumberWidth + 10 + 'px';
editorRenderer.style.paddingLeft = lineNumberWidth + 10 + 'px';
}
function lineNumberConShadSync() {
const scrollAmount = editor.scrollLeft;
const threshold = 10; // Change this value to the amount of pixels you want to detect
if (scrollAmount >= threshold) lineNumberCon.style.boxShadow = '0 0 5px black' //Element has been scrolled', threshold, 'pixels to the right.
else lineNumberCon.style.boxShadow = '0 0 0 transparent'
}
function lineNumber_Editor_Vertical_Sync_Scroll() {
let scrollTop = editor.scrollTop;
if (scrollTop !== editor.dataset.lastScrollTop) lineNumberCon.scrollTop = editor.scrollTop //Vertical scroll detected
editor.dataset.lastScrollTop = scrollTop;
}
updateTextareaPadding()
window.addEventListener('resize', updateTextareaPadding);
function updateRenderElem(text) {
if (text[text.length - 1] == "\n") { // If the last character is a newline character
text += " "; // Add a placeholder space character to the final line
}
//console.log(encryptText)
editorRenderer.innerHTML = text.replace(new RegExp("&", "g"), "&").replace(new RegExp("<", "g"), "<").replace(new RegExp("<", "g"), ">");
// Text containing color names
//const text = "The sky is blue and the grass is green.";
// Define color names and their corresponding hex values
const colors = {
blue: '#0000FF',
green: '#008000',
red: 'red',
orange: 'orange',
purple: 'purple',
};
// Create a regular expression to match color names
const colorReg = new RegExp(`\\b(${Object.keys(colors).join('|')})\\b`, 'gi');
// Replace color names with spans containing the same color
const formattedText = text.replace(colorReg, (match, colorName) => {
const colorHex = colors[colorName.toLowerCase()];
return `<span style="background: ${colorHex}">${match}</span>`;
});
new Falloy().highlight(editorRenderer)
// editorRenderer.innerHTML = new Falloy().highlight(editorRenderer, "html")
//console.log(Prism)
//Prism.highlightElement(editorRenderer)
editorRenderer.scrollHeight = editor.offsetHeight
//console.log(editorRenderer)
}
function isCursorAtEndOfLastLine(textarea) {
const cursorPos = textarea.selectionStart;
const textareaValue = textarea.value;
// Find the index of the last newline character before the cursor position
const lastNewlineIndex = textareaValue.lastIndexOf('\n', cursorPos);
// If there are no newline characters before the cursor, it's not at the end of the last line
if (lastNewlineIndex === -1) {
return false;
}
// Check if the cursor is at the end of the last line
return cursorPos === textareaValue.length || cursorPos === lastNewlineIndex + 1;
}
// Function to change color of clicked box
let activeKeyx = false
function changeColor(event) {
event.target.classList.remove('flash-bg') //remove if any
void this.offsetWidth
event.target.classList.add('flash-bg') //trigger again
if (event.target.classList.contains('joint')) {
if (event.target.classList.contains('active-key')) event.target.classList.remove('active-key')
else event.target.classList.add('active-key')
}
if (!event.target.classList.contains('joint')) Array.from(keyx.children).forEach(child => child.classList.remove('active-key'))
// if (event.target.classList.contains('active-key')) event.target.classList.remove('active-key')
// else event.target.classList.add('active-key')
}
Array.from(keyx.children).forEach(child => child.addEventListener('click', changeColor))
document.addEventListener('click', (e) => {
if (!keyx.contains(e.target)) Array.from(keyx.children).forEach(child => child.classList.remove('active-key'))
})