|
| 1 | +// https://www.notion.so/Frontend-e12411d8ddab4184b47efb6158a839d1 |
| 2 | + |
| 3 | +import { useEffect, useRef, useState } from "react"; |
| 4 | +import "./styles.css"; |
| 5 | + |
| 6 | +function moveElement(originalArr, fromIndex, toIndex) { |
| 7 | + const arr = originalArr.slice(); |
| 8 | + const element = arr[fromIndex]; |
| 9 | + arr.splice(fromIndex, 1); |
| 10 | + arr.splice(toIndex, 0, element); |
| 11 | + return arr; |
| 12 | +} |
| 13 | + |
| 14 | +const CURSOR_CHAR = "CURSOR_CHAR"; |
| 15 | + |
| 16 | +const Cursor = () => <span className="blinking-cursor">|</span>; |
| 17 | + |
| 18 | +const Input = () => { |
| 19 | + const inputRef = useRef(); |
| 20 | + const [value, setValue] = useState([CURSOR_CHAR]); |
| 21 | + const [isCursorShown, setIsCursorShown] = useState(false); |
| 22 | + |
| 23 | + useEffect(() => { |
| 24 | + const handleKeyPress = (ev) => { |
| 25 | + if (document.activeElement !== inputRef.current) { |
| 26 | + return; |
| 27 | + } |
| 28 | + |
| 29 | + const nextValue = value.slice(); |
| 30 | + const cursorIdx = nextValue.findIndex((v) => v === CURSOR_CHAR); |
| 31 | + |
| 32 | + switch (ev.key) { |
| 33 | + case "Backspace": |
| 34 | + if (nextValue.length !== 1) { |
| 35 | + nextValue.splice(cursorIdx - 1, 1); |
| 36 | + setValue(nextValue); |
| 37 | + } |
| 38 | + break; |
| 39 | + case "Delete": |
| 40 | + if (nextValue.length !== 1) { |
| 41 | + nextValue.splice(cursorIdx + 1, 1); |
| 42 | + setValue(nextValue); |
| 43 | + } |
| 44 | + break; |
| 45 | + case "ArrowRight": |
| 46 | + if (cursorIdx === value.length - 1) { |
| 47 | + return; |
| 48 | + } |
| 49 | + setValue(moveElement(nextValue, cursorIdx, cursorIdx + 1)); |
| 50 | + break; |
| 51 | + case "ArrowLeft": |
| 52 | + if (cursorIdx === 0) { |
| 53 | + return; |
| 54 | + } |
| 55 | + setValue(moveElement(nextValue, cursorIdx, cursorIdx - 1)); |
| 56 | + break; |
| 57 | + case "Enter": |
| 58 | + break; |
| 59 | + default: |
| 60 | + nextValue.splice(cursorIdx, 0, ev.key); |
| 61 | + setValue(nextValue); |
| 62 | + } |
| 63 | + }; |
| 64 | + |
| 65 | + const handleClick = (ev) => { |
| 66 | + setIsCursorShown(document.activeElement === inputRef.current); |
| 67 | + }; |
| 68 | + |
| 69 | + window.addEventListener("keydown", handleKeyPress); |
| 70 | + window.addEventListener("click", handleClick); |
| 71 | + |
| 72 | + return () => { |
| 73 | + window.removeEventListener("keydown", handleKeyPress); |
| 74 | + window.addEventListener("click", handleClick); |
| 75 | + }; |
| 76 | + }, [value, setValue]); |
| 77 | + |
| 78 | + return ( |
| 79 | + <div className="Input" ref={inputRef} tabIndex="0"> |
| 80 | + {value.map((char, idx) => { |
| 81 | + if (char === CURSOR_CHAR) { |
| 82 | + return isCursorShown ? <Cursor key={char + idx} /> : null; |
| 83 | + } |
| 84 | + return ( |
| 85 | + <span key={char + idx} data-key={char}> |
| 86 | + {char} |
| 87 | + </span> |
| 88 | + ); |
| 89 | + })} |
| 90 | + </div> |
| 91 | + ); |
| 92 | +}; |
| 93 | + |
| 94 | +export default function App() { |
| 95 | + return ( |
| 96 | + <div className="App"> |
| 97 | + <Input /> |
| 98 | + <h1>Hello CodeSandbox</h1> |
| 99 | + <h2>Start editing to see some magic happen!</h2> |
| 100 | + </div> |
| 101 | + ); |
| 102 | +} |
0 commit comments