All files / viewer/src settingsPanel.ts

100% Statements 25/25
100% Branches 10/10
100% Functions 6/6
100% Lines 24/24

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                                        23x 23x   23x                   23x 23x 23x 23x 23x 23x     23x 23x 23x     24x     24x 24x 23x 24x       85x 85x 85x   85x 85x     108x 108x                                          
import type { IESProfile } from '@ies-tools/core';
import { createElement, Fragment } from 'react';
import { ChartTabs, type ViewerTab } from './react/ChartTabs.js';
import { createRoot, type Root } from 'react-dom/client';
import { ViewerSettingsPanel, type SettingsPanelMode } from './react/ViewerSettingsPanel.js';
import { DEFAULT_VIEWER_SETTINGS, type ViewerSettings } from './settings.js';
export type { SettingsPanelMode } from './react/ViewerSettingsPanel.js';
export type { ViewerTab } from './react/ChartTabs.js';
/** Chart tab state; undefined renders no tabs. */
export interface ChartsState {
  tab: ViewerTab;
  profile?: IESProfile;
  onTabChange: (tab: ViewerTab) => void;
}
 
/** Custom-element bridge to the same React/Tailwind controls used by the React viewer. */
export class SettingsPanel {
  readonly root: HTMLDivElement;
  #host: HTMLElement;
  #reactRoot?: Root;
  #settings: ViewerSettings = { ...DEFAULT_VIEWER_SETTINGS };
  #panelMode: SettingsPanelMode = 'closed';
  #charts?: ChartsState;
  #generation = 0;
  #change: (patch: Partial<ViewerSettings>) => void;
  #mode: (mode: SettingsPanelMode) => void;
  #error: (error: unknown) => void;
  constructor(
    host: HTMLElement,
    change: (patch: Partial<ViewerSettings>) => void,
    mode: (mode: SettingsPanelMode) => void,
    error: (error: unknown) => void,
  ) {
    this.#host = host;
    this.#change = change;
    this.#mode = mode;
    this.#error = error;
    this.root = host.ownerDocument.createElement('div');
    this.root.className = 'viewer-controls-root';
  }
  connect(): void {
    ++this.#generation;
    this.#reactRoot ??= createRoot(this.root);
    this.#render();
  }
  disconnect(): void {
    const generation = ++this.#generation;
    // A parent React tree may currently be removing this custom element. Unmount
    // its nested root after that commit, and reuse it on synchronous reconnection.
    queueMicrotask(() => {
      if (generation !== this.#generation || this.#host.isConnected) return;
      this.#reactRoot?.unmount();
      this.#reactRoot = undefined;
    });
  }
  update(settings: ViewerSettings, mode: SettingsPanelMode, charts?: ChartsState): void {
    this.#settings = settings;
    this.#panelMode = mode;
    this.#charts = charts;
    // Exports hide controls immediately, independently of React scheduling.
    this.root.hidden = mode === 'hidden';
    this.#render();
  }
  #render(): void {
    const charts = this.#charts;
    this.#reactRoot?.render(
      createElement(
        Fragment,
        null,
        charts &&
          createElement(ChartTabs, {
            ...charts,
            mountingHeight: this.#settings.floorDistance,
          }),
        createElement(ViewerSettingsPanel, {
          settings: this.#settings,
          // The scene options apply to the 3D tab only.
          mode: charts && charts.tab !== '3d' ? 'hidden' : this.#panelMode,
          onChange: this.#change,
          onModeChange: this.#mode,
          onError: this.#error,
        }),
      ),
    );
  }
}