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

93.93% Statements 31/33
90% Branches 9/10
87.5% Functions 7/8
93.93% Lines 31/33

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 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 1151x 1x                                                                             1x       33x 11x 11x 1x             477x 477x 173x   304x       304x       337x         337x 337x 274x   63x 2x 2x 2x         9x 9x         9x 9x     1x                     346x 346x 1x 1x   346x     1x    
const DEFINED_CURSORS = Symbol('DefinedCursors');
const STANDARD_CURSORS = new Set([
  'alias',
  'all-scroll',
  'auto',
  'cell',
  'col-resize',
  'context-menu',
  'copy',
  'crosshair',
  'default',
  'e-resize',
  'ew-resize',
  'grab',
  'grabbing',
  'help',
  'move',
  'ne-resize',
  'nesw-resize',
  'no-drop',
  'none',
  'not-allowed',
  'n-resize',
  'ns-resize',
  'nw-resize',
  'nwse-resize',
  'pointer',
  'progress',
  'row-resize',
  'se-resize',
  's-resize',
  'sw-resize',
  'text',
  'vertical-text',
  'wait',
  'w-resize',
  'zoom-in',
  'zoom-out',
]);
 
export default class MouseCursor {
  private name: string;
  private fallback: MouseCursor | undefined;
 
  constructor(name: string, fallback?: MouseCursor | undefined) {
    this.name = name + '';
    this.fallback = fallback;
  }
 
  getName(): string {
    return this.name + '';
  }
 
  addFallbackStyleProperty(style: string): string {
    const { fallback } = this;
    if (fallback instanceof MouseCursor) {
      return `${style}, ${fallback.getStyleProperty()}`;
    }
    return style + '';
  }
 
  getStyleProperty(): string {
    return this.addFallbackStyleProperty(this.name) + '';
  }
 
  static getDefinedCursor(name: string): MouseCursor | undefined {
    const definedCursors = getDefinedCursors(
      // @ts-ignore
      MouseCursor as Record<symbol, Map<string, MouseCursor>>,
      DEFINED_CURSORS
    );
    let mouseCursor = definedCursors.get(name);
    if (mouseCursor instanceof MouseCursor) {
      return mouseCursor;
    }
    if (STANDARD_CURSORS.has(name)) {
      mouseCursor = new MouseCursor(name);
      definedCursors.set(name, mouseCursor);
      return mouseCursor;
    }
  }
 
  static setDefinedCursor(name: string, cursor: MouseCursor): boolean {
    Eif (cursor instanceof MouseCursor) {
      const definedCursors = getDefinedCursors(
        // @ts-ignore
        MouseCursor as Record<symbol, Map<string, MouseCursor>>,
        DEFINED_CURSORS
      );
      definedCursors.set(name, cursor);
      return true;
    }
    return false;
  }
}
 
/*
 * Helpers
 */
 
function getDefinedCursors(
  context: Record<symbol, Map<string, MouseCursor>>,
  symbol: symbol
): Map<string, MouseCursor> {
  let definedCursors = context[symbol];
  if (!(definedCursors instanceof Map)) {
    definedCursors = new Map();
    Object.defineProperty(context, symbol, { value: definedCursors });
  }
  return definedCursors;
}
 
const standardCursorNames = STANDARD_CURSORS.values();
export { standardCursorNames };