it looks when you type random chars rapidly and press many enter keys to switch to a new…
- User
it looks when you type random chars rapidly and press many enter keys to switch to a new line, this block caret tracing effect got stucks at the right permanently, maybe because it doesn't get specifically handled for in one iteration in animation?: ```
.container { display: grid; width: 100%; height: 100%; padding-top: 0px; place-items: center; } .textarea { width: 100%; height: auto; max-height: 50vh; font-family: 'Courier New', monospace; font-size: 18px; font-weight: bold; color: cyan; background: rgba(1, 1, 3, 0.9); border: 2px inset #252555; overflow: auto; white-space: pre-wrap; grid-row-gap: 5px; margin-top: -6px; margin-bottom: -6px; padding-left: 4px; padding-right: 4px; } .highlight-line { position: relative; height: 9px; background: linear-gradient(to right, rgba(155, 0, 0, 0.1), rgba(0, 0, 155, 0.2)); animation: shot 2.5s forwards, blink 0.05s 0.1s infinite; } .highlight-line:before { padding: 5px; content: '█'; color: white; position: absolute; left: 100%; top: 0; width: 0; height: 100%; background: linear-gradient(to right, rgba(155, 200, 55, 0.2), rgba(155, 0, 0, 0.1)); pointer-events: none; animation: shot 0.5s backwards, blink 0.05s 0.1s infinite; } @keyframes shot { from { left: 0; width: 0; } to { left: 100%; width: 100%; } } @keyframes blink { 0%, 100% { opacity: 1; } 50% { opacity: 0; } }function insertLine() { const range = window.getSelection().getRangeAt(0); const textNode = range.startContainer; if (textNode.nodeType === 3) { // Check if startContainer is a Text node const currentPosition = range.startOffset; const lineText = textNode.textContent.trim().slice(currentPosition); const highlightedLine = document.createElement('span'); highlightedLine.classList.add('highlight-line'); highlightedLine.textContent = '█'; range.deleteContents(); const lineBreak = document.createElement('br'); const newLine = document.createElement('span'); newLine.appendChild(lineBreak); newLine.appendChild(highlightedLine); const nextNode = currentPosition > 0 ? textNode.splitText(currentPosition) : textNode; nextNode.parentNode.insertBefore(newLine, nextNode); range.deleteContents(); range.insertNode(highlightedLine); range.collapse(); updateTimeout(); } } function updateTimeout() { clearTimeout(resizeTimeout); resizeTimeout = setTimeout(() => { const container = document.querySelector('.container'); const containerWidth = container.offsetWidth; const highlightedLine = document.querySelector('.highlight-line'); if (highlightedLine !== null) { // Check if highlightedLine is not null const caretPosition = highlightedLine.offsetLeft + highlightedLine.offsetWidth; const emptySpace = containerWidth - caretPosition; const timeoutDuration = emptySpace * 1; // Adjust the multiplier to control the timeout duration setTimeout(() => { highlightedLine.remove(); }, timeoutDuration); } }, 0.1); }
function handleKeyPress(event) { const range = window.getSelection().getRangeAt(0); if (!range.collapsed && range.toString().trim().length > 0) {} if (event.key === 'Enter' || event.key === 'Return') { event.preventDefault(); const lineBreak = document.createElement('br'); insertLine(); range.deleteContents(); range.insertNode(lineBreak); range.collapse(false); } } textarea.addEventListener('keydown', handleKeyPress); window.addEventListener('resize', updateTimeout); - Assistant
The issue you described is likely occurring due to the way the
updateTimeoutfunction is implemented.In the
updateTimeoutfunction, there is a line that calculates thetimeoutDurationbased on the empty space between the caret position and the container width. This is done to control the timeout duration for removing the highlighted line.However, the
setTimeoutcallback function, which removes the highlighted line, is called after a fixed delay oftimeoutDurationmilliseconds. This can cause issues when rapidly typing and pressing the enter key multiple times, as the highlighted line may not be removed in time and can get stuck at the right permanently. - User
well, we need to handle two timeouts then for animation itself, when you press enter, it should throws a block from current caret position to the most-right side available space in line and then simply disappear, but based on that exact range in space inbetween in actual timeout. and also, keep that rapid key press handling as well. any ideas to simplify all this overall problem here?
- Assistant
- User
fix that part without backticks in template literals, because chat ui cannot handle backticks here.:
highlightedLine.style.animationDuration = $`{timeoutDuration}`ms; animationTimeout = setTimeout(onAnimationEnd, timeoutDuration); } }, 0.1); } - Assistant
Certainly! Here's the updated code without backticks in template literals:
highlightedLine.style.animationDuration = timeoutDuration + 'ms'; animationTimeout = setTimeout(onAnimationEnd, timeoutDuration);Make sure to replace those two lines in your existing code with the updated version.
- User
no, something crazy wrong here. the more rapidly you type and press enters, the more it adds overall timeouts and eventually permanently stucking multiple blocks at the most-right of length-end.
- Assistant
Apologies for the confusion. To address the issue where multiple blocks get permanently stuck at the most-right end when rapidly typing and pressing enter keys, you can modify the
updateTimeoutfunction to clear any existing animation timeouts before setting a new one. Here's the updated version:function updateTimeout() { clearTimeout(resizeTimeout); clearTimeout(animationTimeout); resizeTimeout = setTimeout(() => { const highlightedLine = document.querySelector('.highlight-line'); if (highlightedLine !== null) { const container = document.querySelector('.container'); const emptySpace = container.offsetWidth - highlightedLine.offsetLeft - highlightedLine.offsetWidth; const timeoutDuration = emptySpace * 3; highlightedLine.style.animationDuration = timeoutDuration + 'ms'; animationTimeout = setTimeout(onAnimationEnd, timeoutDuration); } }, 0.1); }With this modification, any existing animation timeouts will be cleared before setting a new one. This ensures that the animation duration is correctly calculated based on the current empty space without accumulating extra timeouts.
Please give this updated code a try and let me know if it resolves the issue.
- User
also, need to do that for in actual viewport size updating, because it has the same stucking behaviour.
- Assistant
Apologies for the oversight. To prevent the stucking behavior when updating the viewport size, you can modify the
updateTimeoutfunction to clear the animation timeout when the viewport size changes. Here's the updated version:function updateTimeout() { clearTimeout(resizeTimeout); clearTimeout(animationTimeout); resizeTimeout = setTimeout(() => { const highlightedLine = document.querySelector('.highlight-line'); if (highlightedLine !== null) { const container = document.querySelector('.container'); const emptySpace = container.offsetWidth - highlightedLine.offsetLeft - highlightedLine.offsetWidth; const timeoutDuration = emptySpace * 3; highlightedLine.style.animationDuration = timeoutDuration + 'ms'; animationTimeout = setTimeout(onAnimationEnd, timeoutDuration); } }, 0.1); clearTimeout(animationTimeout); }In this updated code, the
clearTimeout(animationTimeout)line is added at the end of theupdateTimeoutfunction to clear the animation timeout when the function is called due to a viewport size change.This ensures that any existing animation timeouts are cleared before setting a new one, preventing the stucking behavior.
- User
again, something crazy wrong here. it adds some empty space bellow in textarea and don't actually solves that block stucking issue. rethink overall concept and return with a solution.
- Assistant
- User
maybe problem that we don't request an animation frame in time while rapidly typing? because when you resize viewport, it updates normally content inside textarea.
- Assistant
You're correct, requesting an animation frame can help address the issue when rapidly typing. By using , we can ensure that the updates to the textarea content happen at the appropriate time. Here's an updated version of the code: