Skip to content

Commit

Permalink
Update editable.tsx
Browse files Browse the repository at this point in the history
* Run prettier
* Null checks for selection
  • Loading branch information
dylans committed Jan 11, 2022
1 parent d88f2ee commit 6f2e342
Showing 1 changed file with 12 additions and 14 deletions.
26 changes: 12 additions & 14 deletions packages/slate-react/src/components/editable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -250,20 +250,18 @@ export const Editable = (props: EditableProps) => {
// The autoFocus TextareaHTMLAttribute doesn't do anything on a div, so it
// needs to be manually focused.
useEffect(() => {
if (ref.current && autoFocus) {
if (window.getSelection && document.createRange) {
const range = document.createRange();
ref.current.focus();
range.setStart(ref.current, ref.current.childNodes.length);
range.setEnd(ref.current, ref.current.childNodes.length);
const sel = window.getSelection();
sel.removeAllRanges();
sel.addRange(range);
}
else
ref.current.focus();
}
}, [autoFocus])
if (ref.current && autoFocus) {
if (window.getSelection && document.createRange) {
const range = document.createRange()
ref.current.focus()
range.setStart(ref.current, ref.current.childNodes.length)
range.setEnd(ref.current, ref.current.childNodes.length)
const sel = window.getSelection()
sel?.removeAllRanges()
sel?.addRange(range)
} else ref.current.focus()
}
}, [autoFocus])

// Listen on the native `selectionchange` event to be able to update any time
// the selection changes. This is required because React's `onSelect` is leaky
Expand Down

0 comments on commit 6f2e342

Please sign in to comment.