Skip to content

Commit

Permalink
Merge pull request #94 from CanopyTax/Devin/focus
Browse files Browse the repository at this point in the history
autofocus to the end of the range
  • Loading branch information
Devin Rasmussen authored May 19, 2020
2 parents e1a689b + 58d653b commit 0f7a576
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions src/rich-text-editor.component.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,19 @@ export const RichTextEditor = forwardRef((props, editorRef) => {

function emptyEditor() {
// do it with selection and execCommand so it can be undone with Ctrl Z
const range = document.createRange()
range.selectNodeContents(divRef.current)
selectEditor()
document.execCommand('removeFormat')
document.execCommand('delete')
}

function selectEditor(collapse) {
const range = document.createRange() // start and end of nodes
range.selectNodeContents(divRef.current) // setting the start and end nodes
if (collapse) range.collapse(false) // collapsed range starts and ends on the same point (false -> end for focus())
// make this the only range - FF allows multiple ranges
const selection = window.getSelection()
selection.removeAllRanges()
selection.addRange(range)
document.execCommand('removeFormat')
document.execCommand('delete')
}

useEffect(() => {
Expand Down Expand Up @@ -238,7 +244,7 @@ export const RichTextEditor = forwardRef((props, editorRef) => {
}

function focus() {
divRef.current.focus()
selectEditor(true)
setFocused(true)
}

Expand Down

0 comments on commit 0f7a576

Please sign in to comment.