All files / src/notebook metadata.tsx

97.72% Statements 86/88
85.71% Branches 36/42
100% Functions 21/21
97.72% Lines 86/88

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                                                              34x 34x 34x 34x 34x 34x 34x     1028x       202x                               1128x 1128x 1128x   1128x   1128x                 1128x                                                               1128x                                                 1128x         1128x 7896x           1128x       1128x   1128x 5640x           1128x       40x 40x   34x 34x 34x       1124x       1124x     34x 24x   24x     24x 24x 24x 4x 4x 20x 20x 20x       34x 4x     4x       4x 24x 20x   4x 4x     4x 4x 4x     4x   4x 4x     4x 4x 4x   4x 4x 4x 4x 4x         4x 4x     4x   4x   4x       1128x       1128x     34x 4x 4x       24x 20x   4x         1128x       1028x 106x   922x 922x     1128x     34x 34x 34x        
import { ISettings, Stylist } from '@deathbeds/jupyterlab-fonts';
import {
  deleteCellMetadata,
  setCellMetadata,
  getCellMetadata,
  getPanelMetadata,
} from '@deathbeds/jupyterlab-fonts/lib/labcompat';
import { VDomModel, VDomRenderer } from '@jupyterlab/apputils';
import { Cell, ICellModel } from '@jupyterlab/cells';
import { INotebookTools, NotebookTools } from '@jupyterlab/notebook';
import { JSONExt } from '@lumino/coreutils';
import { PanelLayout } from '@lumino/widgets';
import React from 'react';
 
import { ICONS } from '../icons';
import {
  CSS,
  ICellDeckMetadata,
  ID,
  IDeckManager,
  IStylePreset,
  LAYER_SCOPES,
  LAYER_TITLES,
  META,
  TLayerScope,
} from '../tokens';
 
type __ = IDeckManager['__'];
 
export class NotebookMetaTools extends NotebookTools.Tool {
  constructor(options: NotebookMetaTools.IOptions) {
    super();
    const layout = new PanelLayout();
    this._model = new DeckCellEditor.Model(options);
    this._notebookTools = options.notebookTools;
    layout.addWidget(new DeckCellEditor(this._model));
    this.layout = layout;
    this.addClass(CSS.metaTool);
  }
  protected onActiveCellChanged(): void {
    this._model.activeCell = this._notebookTools.activeCell;
  }
 
  protected onActiveCellMetadataChanged(): void {
    this._model.update();
  }
 
  protected _model: DeckCellEditor.Model;
  protected _notebookTools: INotebookTools;
}
 
export namespace NotebookMetaTools {
  export interface IOptions {
    manager: IDeckManager;
    notebookTools: INotebookTools;
  }
}
 
export class DeckCellEditor extends VDomRenderer<DeckCellEditor.Model> {
  protected render(): JSX.Element {
    const m = this.model;
    const { activeMeta, manager } = m;
    const { __ } = manager;
 
    const layer = activeMeta?.layer || '-';
 
    return (
      <div>
        {this.layerTool(layer, __)}
        {this.presetTool(__)}
      </div>
    );
  }
 
  presetTool(__: __) {
    return (
      <div className={CSS.toolPreset}>
        <label
          title={__('Choose from pre-defined style templates.')}
          htmlFor={ID.layerSelect}
        >
          {__('Slide Style')}
          <ICONS.deckStart.react tag="span" width={16} />
        </label>
        <div className={CSS.selectSplit}>
          <div className={CSS.selectWrapper}>
            <select
              className={CSS.styled}
              value={this.model.selectedPreset}
              onChange={this.model.onPresetSelect}
              id={ID.presetSelect}
            >
              {this.presetOptions(__)}
            </select>
          </div>
          <button
            className={`${CSS.styled} ${CSS.apply}`}
            onClick={this.model.applyPreset}
          >
            {__('Add Style')}
          </button>
        </div>
      </div>
    );
  }
 
  layerTool(layer: TLayerScope | '-', __: __) {
    return (
      <div className={CSS.toolLayer}>
        <label
          title={__('Display this cell as an out-of-order layer.')}
          htmlFor={ID.layerSelect}
        >
          {__('Layer')}
          <ICONS.deckStart.react tag="span" width={16} />
          <div className={CSS.selectWrapper}>
            <select
              className={CSS.styled}
              value={layer}
              onChange={this.model.onLayerChange}
              id={ID.layerSelect}
            >
              {this.layerOptions(__)}
            </select>
          </div>
        </label>
      </div>
    );
  }
 
  presetOptions(__: __): JSX.Element[] {
    // todo: get options from... elsewhere
    const presetOptions: JSX.Element[] = [
      <option key="-" value="">
        -
      </option>,
    ];
    for (const preset of this.model.stylePresets) {
      presetOptions.push(
        <option value={preset.key} key={preset.key}>
          {__(preset.label)}
        </option>,
      );
    }
    return presetOptions;
  }
 
  layerOptions(__: __): JSX.Element[] {
    const layerOptions: JSX.Element[] = [];
 
    for (const [layerValue, layerTitle] of Object.entries(LAYER_TITLES)) {
      layerOptions.push(
        <option value={layerValue} key={layerValue} title={__(layerTitle)}>
          {__(layerValue)}
        </option>,
      );
    }
    return layerOptions;
  }
}
 
export namespace DeckCellEditor {
  export class Model extends VDomModel {
    constructor(options: NotebookMetaTools.IOptions) {
      super();
      this._manager = options.manager;
      this._notebookTools = options.notebookTools;
    }
 
    update() {
      this._activeMeta =
        (this._activeCell?.model
          ? (getCellMetadata(this._activeCell?.model, META.deck) as ICellDeckMetadata)
          : null) || JSONExt.emptyObject;
      this.stateChanged.emit(void 0);
    }
 
    onLayerChange = (change: React.ChangeEvent<HTMLSelectElement>) => {
      const { _activeCell } = this;
      /* istanbul ignore if */
      if (!_activeCell) {
        return;
      }
      let layer = change.target.value;
      let newMeta = { ...this._activeMeta } || {};
      if (layer === '-') {
        delete newMeta.layer;
        this._setDeckMetadata(newMeta, _activeCell);
      } else if (LAYER_SCOPES.includes(layer as any)) {
        newMeta.layer = layer as TLayerScope;
        this._setDeckMetadata(newMeta, _activeCell);
      }
    };
 
    applyPreset = () => {
      Iif (!this._activeCell || !this._selectedPreset) {
        return;
      }
      let meta = {
        ...((getCellMetadata(this._activeCell.model, META.fonts) ||
          JSONExt.emptyObject) as ISettings),
      };
      for (const preset of this._manager.stylePresets) {
        if (preset.key != this._selectedPreset) {
          continue;
        }
        if (!meta['styles']) {
          meta['styles'] = {};
        }
 
        let metaStyles = meta['styles'] as any;
        if (!metaStyles[META.nullSelector]) {
          metaStyles[META.nullSelector] = {};
        }
 
        let metaNull = metaStyles[META.nullSelector] as any;
 
        if (!metaNull[META.presentingCell]) {
          metaNull[META.presentingCell] = {};
        }
 
        let presenting = metaNull[META.presentingCell];
        for (let [key, value] of Object.entries(preset.styles)) {
          presenting[key] = value;
        }
        deleteCellMetadata(this._activeCell.model, META.fonts);
        setCellMetadata(this._activeCell.model, META.fonts, meta);
        this.forceStyle();
        this._notebookTools.update();
        return;
      }
    };
 
    forceStyle() {
      let panel = this._notebookTools.activeNotebookPanel;
      Iif (!panel) {
        return;
      }
      let stylist = (this._manager.fonts as any)._stylist as Stylist;
      let meta =
        (panel.model ? getPanelMetadata(panel.model, META.fonts) : null) ||
        JSONExt.emptyObject;
      stylist.stylesheet(meta as ISettings, panel);
    }
 
    get selectedPreset() {
      return this._selectedPreset;
    }
 
    get stylePresets(): IStylePreset[] {
      return this._manager.stylePresets;
    }
 
    onPresetSelect = (change: React.ChangeEvent<HTMLSelectElement>) => {
      this._selectedPreset = change.target.value;
      this.stateChanged.emit(void 0);
    };
 
    protected _setDeckMetadata(newMeta: ICellDeckMetadata, cell: Cell<ICellModel>) {
      if (Object.keys(newMeta).length) {
        setCellMetadata(cell.model, META.deck, newMeta);
      } else {
        deleteCellMetadata(cell.model, META.deck);
      }
    }
 
    get manager() {
      return this._manager;
    }
 
    set activeCell(activeCell: Cell<ICellModel> | null) {
      if (this._activeCell === activeCell) {
        return;
      }
      this._activeCell = activeCell;
      this.update();
    }
    get activeMeta() {
      return this._activeMeta;
    }
    private _manager: IDeckManager;
    private _activeCell: Cell<ICellModel> | null = null;
    private _activeMeta: ICellDeckMetadata | null = null;
    private _selectedPreset: string = '';
    private _notebookTools: INotebookTools;
  }
}