export const createSelectionRangeMemo = () => {
    let selectedRange = null;
    const save = () => {
        if (window.getSelection) {
            const sel = window.getSelection();
            if (sel.getRangeAt && sel.rangeCount) {
                selectedRange = sel.getRangeAt(0);
            }
        } else if (document.selection && document.selection.createRange) {
            selectedRange = document.selection.createRange();
        }
        return selectedRange;
    };
    const restore = () => {
        if (selectedRange) {
            if (window.getSelection) {
                let sel = window.getSelection();
                sel.removeAllRanges();
                // required for safari while addingRange element should be visible.
                selectedRange.startContainer.parentElement.style.visibility = 'visible';
                sel.addRange(selectedRange);
            } else if (document.selection && selectedRange.select) {
                selectedRange.select();
            }
        }
    };

    return {
        save,
        restore,
    };
};
