All files / src/tools layover.ts

98.8% Statements 83/84
83.33% Branches 15/18
96.42% Functions 27/28
98.8% Lines 83/84

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 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261                        4x 4x     4x 4x 4x 4x 4x 4x 4x 4x         4x     4x 4x 4x 4x 4x 4x     4x 56x           156x 156x 156x 156x     56x   156x     156x   56x               56x                   68x       4x       4x 4x     56x               4x 4x     4x 156x 156x     156x                                                       40x   40x   40x                   4x 156x 156x     4x 4x 4x                   4x 300x     4x 1248x       8x       58x 58x 58x           8x 8x     4x                   32x       320x 320x 320x   320x 80x 80x 240x 200x   320x 120x 120x 200x 120x     320x                       32x 32x     4x                 4x 156x 156x 1248x   156x     4x            
import type { GlobalStyles } from '@deathbeds/jupyterlab-fonts/lib/_schema';
import { VDomModel } from '@jupyterlab/apputils';
import { JSONExt } from '@lumino/coreutils';
import { Widget } from '@lumino/widgets';
import { drag, D3DragEvent } from 'd3-drag';
import * as d3 from 'd3-selection';
 
import { IDeckManager, CSS, DATA } from '../tokens';
 
/** An interactive layer positioner. */
export class Layover extends Widget {
  protected _model: Layover.Model;
  protected _d3: typeof d3 | null = null;
  protected _drag: typeof drag | null = null;
 
  constructor(options: Layover.IOptions) {
    super(options);
    this.addClass(CSS.layover);
    this._model = new Layover.Model();
    document.body.appendChild(this.node);
    this.model.stateChanged.connect(this.render, this);
    window.addEventListener('resize', this.render);
    document.body.dataset[DATA.layoutMode] = DATA.designing;
    this.render();
  }
 
  dispose(): void {
    /* istanbul ignore if */
    if (this.isDisposed) {
      return;
    }
    this.model.dispose();
    const { node } = this;
    super.dispose();
    document.body.removeChild(node);
    window.removeEventListener('resize', this.render);
    delete document.body.dataset[DATA.layoutMode];
  }
 
  render = () => {
    const boxes = d3
      .select(this.node)
      .selectAll(`.${CSS.layoverPart}`)
      .data(this.model.partData, Layover.getPartKey as any)
      .join('div')
      .classed(CSS.layoverPart, true)
      .style('left', ({ bounds }) => `${bounds.left}px`)
      .style('top', ({ bounds }) => `${bounds.top}px`)
      .style('height', ({ bounds }) => `${bounds.height}px`)
      .style('width', ({ bounds }) => `${bounds.width}px`)
      .call(Layover.boxDrag as any);
 
    boxes
      .selectAll(`.${CSS.layoverPartLabel}`)
      .data((d, i) => [i])
      .join('div')
      .classed(CSS.layoverPartLabel, true)
      .text((d) => d + 1);
 
    boxes
      .selectAll(`.${CSS.layoverHandle}`)
      .data(Layover.handleData)
      .join('div')
      .attr('class', Layover.getHandleClass)
      .classed(CSS.layoverHandle, true)
      .call(Layover.handleDrag.container(this.node) as any);
 
    boxes
      .selectAll(`.${CSS.layoverUnstyle}`)
      .data(Layover.resetData)
      .join('button')
      .classed(CSS.layoverUnstyle, true)
      .text('↺')
      .on('click', Layover.onReset as any);
  };
 
  get model() {
    return this._model;
  }
}
 
export namespace Layover {
  export interface IOptions extends Widget.IOptions {
    manager: IDeckManager;
  }
  export class Model extends VDomModel {
    private _parts: BasePart[] = [];
 
    get partData() {
      return this._parts.map(this._partDatum);
    }
 
    get parts() {
      return this._parts;
    }
 
    set parts(parts: BasePart[]) {
      this._parts = parts;
      this.stateChanged.emit(void 0);
    }
 
    protected _partDatum = (part: BasePart) => {
      const { left, top, width, height } = part.node.getBoundingClientRect();
      let zoom = parseFloat(
        window.getComputedStyle(part.node).getPropertyValue('zoom') || '1.0',
      );
      return {
        ...part,
        bounds: {
          left: left * zoom,
          top: top * zoom,
          width: width * zoom,
          height: height * zoom,
        },
      };
    };
  }
  export interface DOMRectLike {
    top: number;
    left: number;
    width: number;
    height: number;
  }
  export interface BasePart {
    key: string;
    node: HTMLElement;
    getStyles(): GlobalStyles | null;
    setStyles(styles: GlobalStyles | null): void;
  }
  export interface Part extends BasePart {
    bounds: DOMRectLike;
  }
 
  function setStyles(d: Layover.Part) {
    const { bounds } = d;
 
    const { innerWidth, innerHeight } = window;
 
    d.setStyles({
      ...(d.getStyles() || JSONExt.emptyObject),
      position: 'fixed',
      left: `${100 * (bounds.left / innerWidth)}%`,
      top: `${100 * (bounds.top / innerHeight)}%`,
      width: `${100 * (bounds.width / innerWidth)}%`,
      height: `${100 * (bounds.height / innerHeight)}%`,
    });
  }
 
  export function resetData(d: Part) {
    let style = d.getStyles();
    return style && style.position === 'fixed' ? [d] : [];
  }
 
  export function onReset(event: PointerEvent, d: Layover.Part) {
    let styles = d.getStyles() || JSONExt.emptyObject;
    d.setStyles({
      ...styles,
      position: null as any,
      left: null as any,
      top: null as any,
      width: null as any,
      height: null as any,
    });
  }
 
  export function getPartKey(d: Part) {
    return d.key;
  }
 
  export function getHandleClass(h: PartHandle) {
    return `${CSS.layoverHandle}-${h.handle}`;
  }
 
  function onBoxDragStart(this: HTMLDivElement, event: TPartDrag, d: Layover.Part) {
    d3.select(this).classed(CSS.dragging, true);
  }
 
  function onBoxDrag(this: HTMLDivElement, event: TPartDrag, d: Layover.Part) {
    d.bounds.left += event.dx;
    d.bounds.top += event.dy;
    d3.select(this)
      .style('left', `${d.bounds.left}px`)
      .style('top', `${d.bounds.top}px`);
  }
 
  function onBoxDragEnd(this: HTMLDivElement, event: TPartDrag, d: Layover.Part) {
    d3.select(this).classed(CSS.dragging, false);
    setStyles(d);
  }
 
  export const boxDrag = drag<HTMLDivElement, Layover.Part>()
    .on('start', onBoxDragStart)
    .on('drag', onBoxDrag)
    .on('end', onBoxDragEnd);
 
  function onHandleDragStart(
    this: HTMLDivElement,
    event: TPartDrag,
    d: Layover.PartHandle,
  ) {
    d3.select(this.parentElement).classed(CSS.dragging, true);
  }
 
  function onHandleDrag(this: HTMLDivElement, event: TPartDrag, d: Layover.PartHandle) {
    let { dx, dy } = event;
    let { bounds } = d.part;
    let h = d.handle;
 
    if (h.includes('n')) {
      bounds.top += dy;
      bounds.height -= dy;
    } else if (h.includes('s')) {
      bounds.height += dy;
    }
    if (h.includes('w')) {
      bounds.left += dx;
      bounds.width -= dx;
    } else if (h.includes('e')) {
      bounds.width += dx;
    }
 
    d3.select(this.parentElement)
      .style('left', `${bounds.left}px`)
      .style('top', `${bounds.top}px`)
      .style('height', `${bounds.height}px`)
      .style('width', `${bounds.width}px`);
  }
 
  function onHandleDragEnd(
    this: HTMLDivElement,
    event: TPartDrag,
    d: Layover.PartHandle,
  ) {
    d3.select(this.parentElement).classed(CSS.dragging, false);
    setStyles(d.part);
  }
 
  export const handleDrag = drag<HTMLDivElement, Layover.PartHandle>()
    .on('start', onHandleDragStart)
    .on('drag', onHandleDrag)
    .on('end', onHandleDragEnd);
 
  export interface PartHandle {
    part: Part;
    handle: THandle;
  }
  export function handleData(part: Part) {
    let handleData: PartHandle[] = [];
    for (const handle of HANDLES) {
      handleData.push({ part, handle });
    }
    return handleData;
  }
 
  const HANDLES = ['nw', 'n', 'ne', 'w', 'e', 'sw', 's', 'se'] as const;
 
  type THandle = (typeof HANDLES)[number];
 
  type TPartDrag = D3DragEvent<HTMLDivElement, Layover.Part, Layover.Part>;
}