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

93.33% Statements 28/30
90% Branches 9/10
85.71% Functions 6/7
93.33% Lines 28/30

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                                                                                     30x 10x 10x               455x 455x 164x   291x       291x       308x         308x 308x 248x   60x 2x 2x 2x         8x 8x         8x 8x                           316x 316x 1x 1x   316x     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 };