fix: get-selection-text not working on Firefox

This commit is contained in:
Tienson Qin
2020-11-25 19:57:25 +08:00
parent 320ac8ad55
commit a98239ea20
2 changed files with 23 additions and 11 deletions

View File

@@ -54,3 +54,21 @@ export var timeConversion = function (millisec) {
return days + "d"
}
}
export var getSelectionText = function() {
const selection = (window.getSelection() || '').toString().trim();
if (selection) {
return selection;
}
// Firefox fix
const activeElement = window.document.activeElement;
if (activeElement) {
if (activeElement.tagName === 'INPUT' || activeElement.tagName === 'TEXTAREA') {
const el = activeElement;
return el.value.slice(el.selectionStart || 0, el.selectionEnd || 0);
}
}
return '';
}