All files / tools/src/utilities viewportPresentation.ts

66.66% Statements 10/15
50% Branches 5/10
100% Functions 2/2
66.66% Lines 10/15

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                                2x       2x       2x                     2x       2x 2x         2x         2x 2x 2x          
import { type Types, viewportProjection } from '@cornerstonejs/core';
 
type ViewPresentationViewport = {
  getViewPresentation?(selector?: Types.ViewPresentationSelector): unknown;
  setViewPresentation?(presentation?: unknown): void;
  setViewState?(viewState: unknown): void;
};
 
/**
 * Reads a viewport-family presentation through projection when available, with
 * a legacy viewport fallback for non-Next viewports.
 */
export function getViewportPresentation(
  viewport: unknown,
  selector?: Types.ViewPresentationSelector
): unknown {
  const projectionPresentation = viewportProjection.getPresentation(viewport, {
    selector,
  });
 
  Iif (projectionPresentation) {
    return projectionPresentation;
  }
 
  return (viewport as ViewPresentationViewport).getViewPresentation?.(selector);
}
 
/**
 * Applies a presentation object to a viewport without making the projection
 * registry mutate viewport state.
 */
export function applyViewportPresentation(
  viewport: unknown,
  presentation: unknown
): boolean {
  Iif (!presentation) {
    return false;
  }
 
  const target = viewport as ViewPresentationViewport;
  const nextViewState = viewportProjection.withPresentation(
    viewport,
    presentation
  );
 
  Iif (nextViewState && typeof target.setViewState === 'function') {
    target.setViewState(nextViewState);
    return true;
  }
 
  Eif (typeof target.setViewPresentation === 'function') {
    target.setViewPresentation(presentation);
    return true;
  }
 
  return false;
}