Fix history scroll behavior and text selection

Related to #92
This commit is contained in:
Thomas Nordquist
2019-04-14 21:35:02 +02:00
parent 425bbb36e3
commit 499dfd1b68
7 changed files with 70 additions and 50 deletions

View File

@@ -0,0 +1,22 @@
export const selectTextWithCtrlA = (options?: {targetSelector: string}) => (e: React.KeyboardEvent<HTMLDivElement>) => {
const isCtrlA = (e.metaKey || e.ctrlKey) && e.key === 'a'
if (isCtrlA && window.getSelection) {
e.persist()
e.preventDefault()
e.stopPropagation()
const selection = window.getSelection()
const range = document.createRange()
const eventTarget = (e.target as HTMLElement)
const target: HTMLElement | null = (options) ? eventTarget.querySelector(options.targetSelector) : eventTarget
if (!target) {
console.error('Could not find matching target for Ctrl+A Event')
}
if (selection && target) {
range.selectNodeContents(target)
selection.removeAllRanges()
selection.addRange(range)
}
}
}