fix fancy text editor bug
This commit is contained in:
parent
578d348728
commit
48ea06294e
1 changed files with 6 additions and 9 deletions
|
@ -15,7 +15,7 @@ export interface TextInputKeyDownEvent {
|
|||
}
|
||||
|
||||
export default function FancyTextEditor({
|
||||
value: _value,
|
||||
value: value,
|
||||
onInput,
|
||||
onKeyDown,
|
||||
className: extraClasses = '',
|
||||
|
@ -24,10 +24,7 @@ export default function FancyTextEditor({
|
|||
const divRef = useRef<HTMLDivElement>(null)
|
||||
const [hasFocus, setHasFocus] = useState(false)
|
||||
|
||||
// the contenteditable likes to slip in newlines at the bottom of our innerText
|
||||
// which makes it bad to check for empty string because it might be "\n"
|
||||
// so we just trim it upfront and then fogeddaboudit
|
||||
const value = _value.trim()
|
||||
const trimmedValue = value.trim()
|
||||
|
||||
// The funky mechanics here are to stop the cursor from jumping back the start.
|
||||
// It probably will have the cursor jump to the start if anything changes programmatically,
|
||||
|
@ -38,12 +35,12 @@ export default function FancyTextEditor({
|
|||
return
|
||||
}
|
||||
|
||||
if (!value && !hasFocus) {
|
||||
if (!trimmedValue && !hasFocus) {
|
||||
div.innerText = placeholder
|
||||
} else if (div.innerText !== value) {
|
||||
div.innerText = value
|
||||
} else if (div.innerText.trim() !== trimmedValue) {
|
||||
div.innerText = trimmedValue
|
||||
}
|
||||
}, [hasFocus, placeholder, value])
|
||||
}, [hasFocus, placeholder, trimmedValue])
|
||||
|
||||
useEffect(() => {
|
||||
const div = divRef.current!
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue