All files / src/ui image-inspector.ts

0% Statements 0/33
0% Branches 0/6
0% Functions 0/8
0% Lines 0/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 115 116 117 118 119 120 121 122 123 124 125 126 127                                                                                                                                                                                                                                                             
import {DomWatcher} from '../dom/dom-watcher';
 
export interface ImageInspectorConfig {
  /**
   * The name of the css class to attached to each generated spacer item.
   */
  cssClassName: string;
 
  /**
   * The query selector of all elements you want to check for margins / paddings
   * on the page.
   */
  querySelector: string;
}
 
/**
 * A class that will visibly highlight image dimensions on your page
 *
 *
 * Setup:
 * 1) Create your image-inspector css class.
 *
 * .image-inspector
 *   align-items: center
 *   background: rgba(#1a73e8, 0.15)
 *   box-shadow: none !important
 *   display: flex
 *   font-size: 12px
 *   height: var(--height)
 *   justify-content: center
 *   left: var(--left)
 *   margin: 0 !important
 *   pointer-events: none
 *   position: fixed !important
 *   top: var(--top)
 *   width: var(--width)
 *   z-index: 9
 *
 *
 * 2) Instantiate ImageInspector.  The query selector tells to inspect
 *    all divs on the page.  Update it to scope the ImageInspector.
 *
 * new ImageInspector({
 *      cssClassName: 'image-inspector',
 *      querySelector: '.my-module > img'
 * })
 *
 */
 
export class ImageInspector {
  private watcher: DomWatcher;
  private config: ImageInspectorConfig;
 
  constructor(config: ImageInspectorConfig) {
    this.config = config;
    this.watcher = new DomWatcher();
 
    this.watcher.add({
      element: window,
      on: ['click', 'resize', 'scroll'],
      callback: this.run.bind(this),
    });
    this.run();
  }
 
  private createInspector(el: HTMLImageElement, top: string, left: number) {
    const dimensionsEl = document.createElement('div');
    dimensionsEl.classList.add(this.config.cssClassName);
 
    dimensionsEl.style.setProperty('--left', `${left}px`);
    dimensionsEl.style.setProperty('--top', top);
    dimensionsEl.style.setProperty('--width', `${dimensionsEl.offsetWidth}px`);
 
    const headingEl = document.createElement('strong');
    const renderedSizeEl = document.createElement('span');
    const intrinsicSizeEl = document.createElement('span');
 
    headingEl.innerText = `${el.alt || el.src}`;
    renderedSizeEl.innerText = `Rendered size: ${el.offsetWidth} x ${el.offsetHeight}`;
    intrinsicSizeEl.innerText = `Intrinsic size: ${el.naturalWidth} x ${el.naturalHeight}`;
 
    dimensionsEl!.appendChild(headingEl);
    dimensionsEl!.appendChild(renderedSizeEl);
    dimensionsEl!.appendChild(intrinsicSizeEl);
 
    el.parentElement!.appendChild(dimensionsEl);
 
    const rect = dimensionsEl.getBoundingClientRect();
    if (rect.width > 80) {
      dimensionsEl.style.setProperty('--max-width', `${rect.width * 1.5}px`);
    } else {
      dimensionsEl.style.setProperty('--max-width', '80px');
    }
 
    dimensionsEl.style.setProperty('--height', rect.height + 'px');
  }
 
  public run() {
    this.removeInspectors();
    this.createInspectors();
  }
 
  private createInspectors() {
    [].forEach.call(
      document.querySelectorAll(this.config.querySelector),
      (el: HTMLImageElement) => {
        const rect = el.getBoundingClientRect();
        this.createInspector(el, `${rect.top}px`, rect.left);
      }
    );
  }
 
  private removeInspectors() {
    [].forEach.call(
      document.querySelectorAll(`.${this.config.cssClassName}`),
      (el: HTMLImageElement) => {
        el.parentNode!.removeChild(el);
      }
    );
  }
 
  public dispose() {
    this.removeInspectors();
    this.watcher && this.watcher.dispose();
  }
}