Skip to content
This repository has been archived by the owner on May 22, 2023. It is now read-only.

Commit

Permalink
Preserve selection after clipboard copy (#43)
Browse files Browse the repository at this point in the history
Fixes #42
  • Loading branch information
walfie authored Sep 11, 2016
1 parent 041ab73 commit 3290372
Showing 1 changed file with 13 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,14 @@ object HtmlHelpers {
}
}

/** Create an empty textarea, select the text inside, and copy to clipboard */
def copy(stringToCopy: String): Boolean = {
// If the user has already selected something, store the selection
val selection = document.getSelection()
val selectedRange =
if (selection.rangeCount > 0) Some(selection.getRangeAt(0))
else None

val textArea = createInvisibleTextArea()
textArea.value = stringToCopy
document.body.appendChild(textArea)
Expand All @@ -31,6 +38,12 @@ object HtmlHelpers {
document.body.removeChild(textArea)
}

// Restore the user's previous selection, if any
selectedRange.foreach { range =>
selection.removeAllRanges()
selection.addRange(range)
}

result
}

Expand Down

0 comments on commit 3290372

Please sign in to comment.