Skip to content

Commit

Permalink
Editable: Remove unneeded setTimeout from setSelectionToCursorOffset. F…
Browse files Browse the repository at this point in the history
  • Loading branch information
raineorshine committed Nov 20, 2020
1 parent 812fc51 commit 0463a17
Showing 1 changed file with 1 addition and 23 deletions.
24 changes: 1 addition & 23 deletions src/components/Editable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import {
// util
import {
addEmojiSpace,
asyncFocus,
clearSelection,
parentOf,
ellipsize,
Expand Down Expand Up @@ -289,13 +288,6 @@ const Editable = ({ disabled, isEditing, simplePath, path, cursorOffset, showCon
// using useRef hook to store throttled function so that it can persist even between component re-renders, so that throttle.flush method can be used properly
const throttledChangeRef = useRef(_.throttle(thoughtChangeHandler, EDIT_THROTTLE, { leading: false }))

/** Set the selection to the current Editable at the cursor offset. */
const setSelectionToCursorOffset = () => {
if (contentRef.current) {
setSelection(contentRef.current, { offset: cursorOffset })
}
}

useEffect(() => {
const { editing, noteFocus, dragHold } = state
const editMode = !isMobile || editing
Expand All @@ -317,21 +309,7 @@ const Editable = ({ disabled, isEditing, simplePath, path, cursorOffset, showCon
(cursorOffset !== null || !window.getSelection()?.focusNode) &&
!dragHold
) {
/*
Mobile Safari: Auto-Capitalization broken if selection is set synchronously.
When a new thought is created, the Shift key should be on for Auto-Capitalization.
Only occurs on Enter or Backspace, not gesture.
Even stranger, the issue only showed up when newThought was converted to a reducer (ecc3b3be).
For some reason, setTimeout fixes it.
*/
if (isMobile) {
// NOTE: asyncFocus() needs to be called on mobile BEFORE the action that triggers the re-render is dispatched
asyncFocus()
setTimeout(setSelectionToCursorOffset)
}
else {
setSelectionToCursorOffset()
}
setSelection(contentRef.current, { offset: cursorOffset })
}

/** Flushes pending edits. */
Expand Down

0 comments on commit 0463a17

Please sign in to comment.