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

93.1% Statements 27/29
90% Branches 9/10
85.71% Functions 6/7
93.1% Lines 27/29

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                                                                                       13x 13x               647x 647x 227x   420x       420x       456x         456x 456x 347x   109x 2x 2x 2x         11x 11x         11x 11x                           467x 467x 1x 1x   467x     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 };