All files / packages/tools/src/cursors elementCursor.ts

100% Statements 18/18
83.33% Branches 5/6
100% Functions 5/5
100% Lines 18/18

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70    1x                   119x 119x             277x 277x 277x 277x               74x       84x                   470x 470x 1x 1x       470x 470x 91x 91x   470x                        
import { MouseCursor } from '.';
 
const ELEMENT_CURSORS_MAP = Symbol('ElementCursorsMap');
 
/*
 * Public Methods
 */
 
function initElementCursor(
  element: HTMLDivElement,
  cursor: MouseCursor | null
): void {
  _getElementCursors(element)[0] = cursor;
  _setElementCursor(element, cursor);
}
 
function _setElementCursor(
  element: HTMLDivElement,
  cursor: MouseCursor | null
): void {
  const cursors = _getElementCursors(element);
  cursors[1] = cursors[0];
  cursors[0] = cursor;
  element.style.cursor = (
    cursor instanceof MouseCursor
      ? cursor
      : MouseCursor.getDefinedCursor('auto')
  ).getStyleProperty();
}
 
function resetElementCursor(element: HTMLDivElement): void {
  _setElementCursor(element, _getElementCursors(element)[1]);
}
 
function hideElementCursor(element: HTMLDivElement): void {
  _setElementCursor(element, MouseCursor.getDefinedCursor('none'));
}
 
/*
 * Helpers
 */
 
function _getElementCursors(
  element: HTMLDivElement
): [MouseCursor | null, MouseCursor | null] {
  let map = _getElementCursors[ELEMENT_CURSORS_MAP];
  if (!(map instanceof WeakMap)) {
    map = new WeakMap();
    Object.defineProperty(_getElementCursors, ELEMENT_CURSORS_MAP, {
      value: map,
    });
  }
  let cursors = map.get(element);
  if (!cursors) {
    cursors = [null, null];
    map.set(element, cursors);
  }
  return cursors;
}
 
/*
 * Exports
 */
export {
  initElementCursor,
  resetElementCursor,
  hideElementCursor,
  _setElementCursor as setElementCursor,
};