From ee90fad05bc89a4b7d4768f80c8ce59768acffd1 Mon Sep 17 00:00:00 2001 From: Max Base Date: Sun, 15 Sep 2024 20:18:46 +0330 Subject: [PATCH] clean and log ops --- editor.html | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/editor.html b/editor.html index 2391558..a63a884 100644 --- a/editor.html +++ b/editor.html @@ -86,7 +86,6 @@ const editor_lines = editor.querySelector('.editor-lines'); const handleBlur = () => { - // Remove active class when editor loses focus editor_code.querySelectorAll('.line').forEach(line => line.classList.remove('active')); editor_lines.querySelectorAll('li').forEach(li => li.classList.remove('active')); }; @@ -98,6 +97,7 @@ editor_code.querySelectorAll('.line').forEach(line => line.classList.remove('active')); editor_lines.querySelectorAll('li').forEach(li => li.classList.remove('active')); + if (activeLine) { activeLine.classList.add('active'); @@ -113,6 +113,7 @@ lines.forEach((_, index) => { const lineItem = document.createElement('li'); + lineItem.textContent = index + 1; editor_lines.appendChild(lineItem); }); @@ -121,6 +122,7 @@ const getLeadingSpaces = (lineElement) => { const lineText = lineElement.textContent; const leadingSpaces = lineText.match(/^\s*/)[0].length; + return leadingSpaces; }; @@ -178,11 +180,11 @@ scrollIntoViewIfNeeded(newLine); const newRange = document.createRange(); - newRange.setStart(newLine, 1); - newRange.setEnd(newLine, 1); + // newRange.setStart(newLine, 1); + // newRange.setEnd(newLine, 1); // or? - // newRange.setStart(newLine, 0); - // newRange.setEnd(newLine, 0); + newRange.setStart(newLine, 0); + newRange.setEnd(newLine, 0); const newSelection = window.getSelection(); newSelection.removeAllRanges(); @@ -195,12 +197,16 @@ if (event.key === 'Enter') { event.preventDefault(); + handleKeyLine(); + updateLineNumbers(); } else if (event.key === 'Tab') { event.preventDefault(); + handleKeyTab(); + // updateLineNumbers(); } }; @@ -217,6 +223,7 @@ const opsReg = `(${ops.join('|')})`; const opsRegex = new RegExp(opsReg, 'g'); + console.log(opsReg, opsRegex); code = code.replace(opsRegex, '$&'); return code; @@ -250,6 +257,13 @@ const handleFocus = () => { updateLineNumbers(); + + const selection = window.getSelection(); + const activeLine = selection.anchorNode.closest('.line'); + + if (activeLine) { + activeLine.classList.add('active'); + } }; editor_code.addEventListener('paste', handlePaste); @@ -259,7 +273,6 @@ editor_code.addEventListener('keydown', handleKey); editor_code.addEventListener('input', splitLines); - editor_code.innerHTML = '

'; editor_code.focus();