@adaptabletools/adaptable-cjs
Version:
Powerful data-agnostic HTML5 AG Grid extension which provides advanced, cutting-edge functionality to meet all DataGrid requirements
25 lines (24 loc) • 1.04 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.useSelectionRange = void 0;
const react_1 = require("react");
function useSelectionRange() {
const [range, setRange] = (0, react_1.useState)([null, null]);
const onSelectionChange = () => {
const isActive = ref.current === document.activeElement;
const start = isActive ? ref.current.selectionStart : null;
const end = isActive ? ref.current.selectionEnd : null;
setRange([start, end]);
};
const ref = (0, react_1.useRef)(null);
const refCallback = (0, react_1.useCallback)((newNode) => {
const oldNode = ref.current;
if (oldNode)
document.removeEventListener('selectionchange', onSelectionChange);
if (newNode)
document.addEventListener('selectionchange', onSelectionChange);
ref.current = newNode;
}, []);
return { ref: refCallback, selectionStart: range[0], selectionEnd: range[1] };
}
exports.useSelectionRange = useSelectionRange;