This repository has been archived by the owner on Nov 29, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
182 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
<style> | ||
* { | ||
padding: 0; | ||
margin: 0; | ||
} | ||
|
||
.editor { | ||
direction: rtl; | ||
text-align: right; | ||
width: 100%; | ||
height: 100vh; | ||
display: flex; | ||
flex-direction: row; | ||
line-height: 1.5; | ||
font-size: 1.5rem; | ||
font-family: sans-serif; | ||
} | ||
|
||
.editor-lines { | ||
width: auto; | ||
min-width: 20px; | ||
background-color: #f5f5f5; | ||
color: #6c6c6c; | ||
border-right: 1px solid #ddd; | ||
list-style: none; | ||
} | ||
|
||
.editor-code { | ||
width: 100%; | ||
height: 100%; | ||
/* color: white; */ | ||
/* background-color: black; */ | ||
overflow-y: auto; | ||
} | ||
</style> | ||
|
||
<div class="editor"> | ||
<div class="editor-lines"> | ||
<li>1</li> | ||
<li>2</li> | ||
<li>3</li> | ||
<li>3</li> | ||
<li>4</li> | ||
<li>5</li> | ||
<li>6</li> | ||
</div> | ||
<div contenteditable="true" class="editor-code"></div> | ||
</div> | ||
|
||
<script> | ||
const editor = document.querySelector('.editor'); | ||
const editor_code = editor.querySelector('.editor-code'); | ||
const editor_lines = editor.querySelector('.editor-lines'); | ||
|
||
const updateLineNumbers = () => { | ||
const lines = editor_code.querySelectorAll('.line'); | ||
editor_lines.innerHTML = ''; | ||
|
||
lines.forEach((_, index) => { | ||
const lineItem = document.createElement('li'); | ||
lineItem.textContent = index + 1; | ||
editor_lines.appendChild(lineItem); | ||
}); | ||
}; | ||
|
||
const getLeadingSpaces = (lineElement) => { | ||
const lineText = lineElement.textContent; | ||
const leadingSpaces = lineText.match(/^\u00a0*/)[0].length; | ||
return leadingSpaces; | ||
}; | ||
|
||
const handleTab = (event) => { | ||
console.log(event.key); | ||
|
||
if (event.key === 'Enter') { | ||
event.preventDefault(); | ||
|
||
const selection = window.getSelection(); | ||
const range = selection.getRangeAt(0); | ||
|
||
const newLine = document.createElement('div'); | ||
newLine.classList.add('line'); | ||
newLine.innerHTML = '​'; | ||
|
||
const currentLine = range.startContainer.parentElement; | ||
console.log("currentLine: ", currentLine); | ||
|
||
if (currentLine) { | ||
if (currentLine.classList.contains('line')) { | ||
console.log("is line"); | ||
|
||
currentLine.parentElement.insertBefore(newLine, currentLine.nextSibling); | ||
} | ||
else { | ||
console.log("is not line"); | ||
|
||
currentLine.appendChild(newLine); | ||
} | ||
|
||
const newRange = document.createRange(); | ||
newRange.setStart(newLine, 0); | ||
newRange.setEnd(newLine, 0); | ||
|
||
const newSelection = window.getSelection(); | ||
newSelection.removeAllRanges(); | ||
newSelection.addRange(newRange); | ||
} | ||
|
||
updateLineNumbers(); | ||
} | ||
else if (event.key === 'Tab') { | ||
event.preventDefault(); | ||
|
||
const selection = window.getSelection(); | ||
const range = selection.getRangeAt(0); | ||
|
||
const tabNode = document.createTextNode('\u00a0\u00a0\u00a0\u00a0'); | ||
|
||
range.insertNode(tabNode); | ||
|
||
range.setStartAfter(tabNode); | ||
range.setEndAfter(tabNode); | ||
|
||
selection.removeAllRanges(); | ||
selection.addRange(range); | ||
|
||
updateLineNumbers(); | ||
} | ||
}; | ||
|
||
const splitLines = () => { | ||
// const lines = editor_code.querySelectorAll('.line'); | ||
// lines.forEach((line) => { | ||
// const lineText = line.textContent; | ||
// const lineTextParts = lineText.split('\n'); | ||
// line.innerHTML = lineTextParts.join('<br>'); | ||
// }); | ||
|
||
updateLineNumbers(); | ||
}; | ||
|
||
editor_code.addEventListener('keydown', handleTab); | ||
editor_code.addEventListener('input', splitLines); | ||
editor_code.innerHTML = '<div class="line">​</div>'; | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters