@ou-imdt/utils
Version:
Utility library for interactive media development
11 lines • 512 B
JavaScript
/**
* Determines if a node currently has a text selection.
* @param {Node} node - The node to check for text selection.
* @returns {boolean} `true` if the node contains a text selection, otherwise `false`.
*/
export default function hasTextSelection(node) {
const selection = document.getSelection();
const containsSelection = node.contains(selection.focusNode); // last selected node
const hasSelection = (containsSelection && !selection.isCollapsed); // collapsed = no length
return hasSelection;
}