All files / src/tools design.tsx

100% Statements 116/116
96.49% Branches 55/57
100% Functions 27/27
100% Lines 116/116

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 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367                                        78x 78x 78x       56x 56x 56x       1498x       1498x 1498x 1498x   1498x 176x     1322x 1178x       20x         144x       16x       144x   144x 144x         8x 8x           144x 144x 144x   144x 144x 864x 864x 144x   720x     144x 144x   144x             144x 144x 144x   144x 144x 720x 720x 144x   576x     144x 144x   144x             144x 144x 144x             144x     78x       864x 864x 864x 864x 864x     20x       864x             78x       720x 720x 720x 720x 720x     20x       720x         78x       432x 432x   432x   432x 432x 12x 8x   12x 12x                   432x 432x   432x                                   78x 36x 36x 12x 12x     24x 24x 24x                     3050x                           40x 40x   78x     78x 78x 78x 78x       56x 56x 56x       6324x       1498x       36x 36x 36x         1498x       144x       144x       180x 180x       36x 36x 36x 36x     78x 80x       1634x                                               40x                                              
import type { GlobalStyles } from '@deathbeds/jupyterlab-fonts/lib/_schema';
import { VDomRenderer, VDomModel } from '@jupyterlab/apputils';
import { LabIcon, ellipsesIcon, caretLeftIcon } from '@jupyterlab/ui-components';
import { JSONExt } from '@lumino/coreutils';
import React from 'react';
 
import { ICONS } from '../icons';
import {
  CSS,
  IDeckManager,
  INCAPABLE,
  IPresenterCapbilities,
  LAYER_SCOPES,
  SLIDE_TYPES,
  TLayerScope,
  TSlideType,
} from '../tokens';
 
export class DesignTools extends VDomRenderer<DesignTools.Model> {
  constructor(options: DesignTools.IOptions) {
    super(new DesignTools.Model(options));
    this.addClass(CSS.designTools);
    document.body.appendChild(this.node);
  }
 
  dispose() {
    this.model.dispose();
    super.dispose();
    document.body.removeChild(this.node);
  }
 
  protected render(): JSX.Element {
    return <div>{this.more()}</div>;
  }
 
  more(): JSX.Element[] {
    const { model } = this;
    const { __ } = model.manager;
    const { capabilities, showMore } = model;
 
    if (!capabilities.layout && !capabilities.slideType && !capabilities.layerScope) {
      return [];
    }
 
    if (!showMore) {
      return [
        this.makeButton(
          ellipsesIcon,
          __('Show Design Tools'),
          () => (model.showMore = true),
        ),
      ];
    }
 
    let items: JSX.Element[] = [
      this.makeButton(
        caretLeftIcon,
        __('Hide Design Tools'),
        () => (model.showMore = false),
      ),
    ];
 
    const { layover } = this.model.manager;
 
    if (capabilities.layout) {
      items.push(
        this.makeButton(
          layover ? ICONS.transformStop : ICONS.transformStart,
          layover ? __('Hide Layout') : __('Show Layout'),
          () => {
            let { manager } = this.model;
            manager.layover ? manager.hideLayover() : manager.showLayover();
          },
        ),
      );
    }
 
    if (capabilities.slideType) {
      const { currentSlideType } = model;
      let slideTypes: JSX.Element[] = [];
      let slideType: TSlideType;
      let activeItem: JSX.Element | null = null;
      for (slideType of SLIDE_TYPES) {
        let item = this.makeSlideTypeItem(slideType, currentSlideType);
        if (currentSlideType === slideType) {
          activeItem = item;
        } else {
          slideTypes.push(item);
        }
      }
      if (activeItem) {
        slideTypes.push(activeItem);
      }
      items.push(
        <ul key="slide-type" className={`${CSS.selector} ${CSS.slideType}`}>
          {slideTypes}
        </ul>,
      );
    }
 
    if (capabilities.layerScope) {
      const { currentLayerScope } = model;
      let layerScopes: JSX.Element[] = [];
      let layerScope: TLayerScope | null;
      let activeItem: JSX.Element | null = null;
      for (layerScope of [...LAYER_SCOPES, null]) {
        let item = this.makeLayerScopeItem(layerScope, currentLayerScope);
        if (currentLayerScope === layerScope) {
          activeItem = item;
        } else {
          layerScopes.push(item);
        }
      }
      if (activeItem) {
        layerScopes.push(activeItem);
      }
      items.push(
        <ul key="layer" className={`${CSS.selector} ${CSS.layerScope}`}>
          {layerScopes}
        </ul>,
      );
    }
 
    if (capabilities.stylePart) {
      let { currentPartStyles } = model;
      items.push(
        this.makeSlider('z-index', currentPartStyles),
        this.makeSlider('zoom', currentPartStyles),
        this.makeSlider('opacity', currentPartStyles),
      );
    }
 
    return items;
  }
 
  makeSlideTypeItem = (
    slideType: TSlideType,
    currentSlideType: TSlideType,
  ): JSX.Element => {
    let { __ } = this.model.manager;
    let slideTypeKey = slideType == null ? 'null' : slideType;
    let icon = ICONS.slideshow[slideTypeKey];
    let label = __(slideTypeKey);
    let button = this.makeButton(
      icon,
      label,
      () => this.model.manager.setSlideType(slideType),
      '',
      [<label key="label">{label}</label>],
    );
    return (
      <li key={slideType} className={currentSlideType === slideType ? CSS.active : ''}>
        {button}
      </li>
    );
  };
 
  makeLayerScopeItem = (
    layerScope: TLayerScope | null,
    currentLayerScope: TLayerScope | null,
  ): JSX.Element => {
    let { __ } = this.model.manager;
    let layerScopeKey = layerScope == null ? 'null' : layerScope;
    let icon = ICONS.layer[layerScopeKey];
    let label = __(layerScopeKey);
    let button = this.makeButton(
      icon,
      label,
      () => this.model.manager.setLayerScope(layerScope),
      '',
      [<label key="label">{label}</label>],
    );
    return (
      <li className={currentLayerScope === layerScope ? CSS.active : ''}>{button}</li>
    );
  };
 
  makeSlider = (
    attr: DesignTools.TSliderAttr,
    styles: GlobalStyles | null,
  ): JSX.Element => {
    const config = DesignTools.SLIDER_CONFIG[attr];
    const { suffix, className, icon } = config;
 
    let value = styles ? (styles[attr] as any) : null;
 
    let checkbox: JSX.Element | null = null;
    if (value != null) {
      if (suffix) {
        value = value.replace(suffix, '');
      }
      value = parseFloat(value);
      checkbox = (
        <input
          type="checkbox"
          name={`${attr}-enabled`}
          checked
          onChange={this.onSliderChange}
        ></input>
      );
    }
 
    let valueAttr = { value: value == null ? config.defaultValue : value };
    let finalClassName = value == null ? className : `${className} ${CSS.active}`;
 
    return (
      <div className={`${CSS.slider} ${finalClassName}`} title={attr} key={attr}>
        {checkbox ? [checkbox] : []}
        <input
          name={attr}
          type="range"
          {...config.attrs}
          {...valueAttr}
          onChange={this.onSliderChange}
        ></input>
        <label>
          <icon.react width={32} />
          <span>{attr}</span>
        </label>
      </div>
    );
  };
 
  onSliderChange = (event: React.ChangeEvent<HTMLInputElement>) => {
    const { value, name, checked } = event.currentTarget;
    if (name.endsWith('-enabled')) {
      if (!checked) {
        this.model.setPartStyles(name.replace('-enabled', ''), null);
      }
    } else {
      const config = DesignTools.SLIDER_CONFIG[name as DesignTools.TSliderAttr];
      const suffix = config.suffix || '';
      this.model.setPartStyles(name, `${value}${suffix}`);
    }
  };
 
  makeButton(
    icon: LabIcon,
    title: string,
    onClick: () => void,
    className: string = '',
    children: JSX.Element[] = [],
  ) {
    return (
      <button
        className={className}
        onClick={onClick}
        title={this.model.manager.__(title)}
        key={title}
      >
        <icon.react width={32} />
        {children}
      </button>
    );
  }
}
 
export namespace DesignTools {
  export class Model extends VDomModel {
    private _manager: IDeckManager;
    private _showMore = false;
 
    constructor(options: IOptions) {
      super();
      this._manager = options.manager;
      this._manager.activeChanged.connect(this._onActiveChanged, this);
      this._manager.layoverChanged.connect(this._emit);
    }
 
    dispose() {
      this._manager.activeChanged.disconnect(this._onActiveChanged, this);
      this._manager.layoverChanged.disconnect(this._emit);
      super.dispose();
    }
 
    get manager(): IDeckManager {
      return this._manager;
    }
 
    get showMore() {
      return this._showMore;
    }
 
    set showMore(showMore: boolean) {
      if (this._showMore !== showMore) {
        this._showMore = showMore;
        this._emit();
      }
    }
 
    get capabilities(): IPresenterCapbilities {
      return this._manager.activePresenter?.capabilities || INCAPABLE;
    }
 
    get currentSlideType(): TSlideType | null {
      return this._manager.getSlideType();
    }
 
    get currentLayerScope(): TLayerScope | null {
      return this._manager.getLayerScope();
    }
 
    get currentPartStyles(): GlobalStyles | null {
      let styles = this._manager.getPartStyles();
      return styles;
    }
 
    setPartStyles(attr: string, value: any) {
      let styles = { ...(this.currentPartStyles || JSONExt.emptyObject) };
      styles[attr] = value;
      this._manager.setPartStyles(styles as GlobalStyles);
      this._emit();
    }
 
    private _emit = () => {
      this.stateChanged.emit(void 0);
    };
 
    private _onActiveChanged() {
      this.stateChanged.emit(void 0);
    }
  }
  export interface IOptions {
    manager: IDeckManager;
  }
 
  export type TSliderAttr = 'z-index' | 'zoom' | 'opacity';
 
  export interface ISliderBounds {
    attrs: {
      min: number;
      max: number;
      step: number;
    };
    defaultValue: number;
    suffix?: string;
    icon: LabIcon;
    className: string;
  }
  export type TSliders = {
    [key in TSliderAttr]: ISliderBounds;
  };
 
  export const SLIDER_CONFIG: TSliders = {
    zoom: {
      attrs: { min: 50, max: 500, step: 1 },
      defaultValue: 100,
      suffix: '%',
      icon: ICONS.zoom,
      className: CSS.zoom,
    },
    'z-index': {
      attrs: { min: -10, max: 10, step: 1 },
      defaultValue: 0,
      icon: ICONS.zIndex,
      className: CSS.zIndex,
    },
    opacity: {
      attrs: { min: 0, max: 100, step: 1 },
      suffix: '%',
      defaultValue: 100,
      icon: ICONS.opacity,
      className: CSS.opacity,
    },
  };
}