All files / packages/tools/src/drawingSvg getSvgDrawingHelper.ts

83.87% Statements 26/31
62.5% Branches 10/16
100% Functions 8/8
83.87% Lines 26/31

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        1x               209x 209x 209x 209x     209x 247x     209x                               209x 209x       209x   209x         560x       560x 247x           313x       313x         313x         247x       247x 247x           209x 53x     156x 387x   387x                
import { state } from '../store';
import { getEnabledElement } from '@cornerstonejs/core';
import { SVGDrawingHelper } from '../types';
 
const VIEWPORT_ELEMENT = 'viewport-element';
 
/**
 * Returns the SVG drawing helper for the given HTML element.
 * @param element - The HTML element to get the SVG drawing helper for.
 * @private
 */
function getSvgDrawingHelper(element: HTMLDivElement): SVGDrawingHelper {
  const enabledElement = getEnabledElement(element);
  const { viewportId, renderingEngineId } = enabledElement;
  const canvasHash = `${viewportId}:${renderingEngineId}`;
  const svgLayerElement = _getSvgLayer(element);
 
  // Reset touched
  Object.keys(state.svgNodeCache[canvasHash]).forEach((cacheKey) => {
    state.svgNodeCache[canvasHash][cacheKey].touched = false;
  });
 
  return {
    svgLayerElement: svgLayerElement,
    svgNodeCacheForCanvas: state.svgNodeCache,
    getSvgNode: getSvgNode.bind(this, canvasHash),
    appendNode: appendNode.bind(this, svgLayerElement, canvasHash),
    setNodeTouched: setNodeTouched.bind(this, canvasHash),
    clearUntouched: clearUntouched.bind(this, svgLayerElement, canvasHash),
  };
}
 
/**
 *
 * @param element
 * @private
 */
function _getSvgLayer(element) {
  const viewportElement = `.${VIEWPORT_ELEMENT}`;
  const internalDivElement = element.querySelector(viewportElement);
 
  // Using :scope to make sure the right svg layer is selected otherwise it
  // may select one from a nested viewport (eg: AdvancedMagnifyTool).
  const svgLayer = internalDivElement.querySelector(':scope > .svg-layer');
 
  return svgLayer;
}
 
function getSvgNode(canvasHash, cacheKey) {
  // If state has been reset
  Iif (!state.svgNodeCache[canvasHash]) {
    return;
  }
 
  if (state.svgNodeCache[canvasHash][cacheKey]) {
    return state.svgNodeCache[canvasHash][cacheKey].domRef;
  }
}
 
function appendNode(svgLayerElement, canvasHash, svgNode, cacheKey) {
  // If state has been reset
  Iif (!state.svgNodeCache[canvasHash]) {
    return null;
  }
 
  state.svgNodeCache[canvasHash][cacheKey] = {
    touched: true,
    domRef: svgNode,
  };
 
  svgLayerElement.appendChild(svgNode);
}
 
function setNodeTouched(canvasHash, cacheKey) {
  // If state has been reset
  Iif (!state.svgNodeCache[canvasHash]) {
    return;
  }
 
  Eif (state.svgNodeCache[canvasHash][cacheKey]) {
    state.svgNodeCache[canvasHash][cacheKey].touched = true;
  }
}
 
function clearUntouched(svgLayerElement, canvasHash) {
  // If state has been reset
  if (!state.svgNodeCache[canvasHash]) {
    return;
  }
 
  Object.keys(state.svgNodeCache[canvasHash]).forEach((cacheKey) => {
    const cacheEntry = state.svgNodeCache[canvasHash][cacheKey];
 
    Iif (!cacheEntry.touched && cacheEntry.domRef) {
      svgLayerElement.removeChild(cacheEntry.domRef);
      delete state.svgNodeCache[canvasHash][cacheKey];
    }
  });
}
 
export default getSvgDrawingHelper;