Last active
April 28, 2020 17:32
-
-
Save chrisryana/ba3ebce0927130878d9d59dd85419267 to your computer and use it in GitHub Desktop.
Полезная функция когда надо скопировать из блочного элемента
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// функция убирает переносы строк при копировании из блочных элементов | |
// пригождалась при попытке копировать из ячейки таблицы библиотеки react-virtualized | |
// и вставить в инпут поиска | |
onSelection = () => { | |
const selectionParams = document.getSelection(); | |
if (selectionParams !== null) { | |
const selectionText = selectionParams.toString(); | |
const selectionWithoutBrakes = selectionText.replace(/\n/, ' '); | |
const div = document.createElement('div'); | |
div.style.position = 'absolute'; | |
div.style.left = '-99999px'; | |
document.body.appendChild(div); | |
div.innerHTML = selectionWithoutBrakes; | |
selectionParams.selectAllChildren(div); | |
window.setTimeout(() => { | |
document.body.removeChild(div); | |
}, 0); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment