All files / packages/tools/src/store addTool.ts

38.46% Statements 5/13
25% Branches 2/8
50% Functions 1/2
38.46% Lines 5/13

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                                  88x 88x   88x       88x         88x                                                      
import { state } from './state';
 
/**
 * Adds the tool class to the cornerstoneTools to be used later. This function
 * should be called before creating the toolGroups and adding tools and setting their mode.
 * The flow is:
 * - addTool(ToolClass) // where ToolClass is the tool constructor imported from CornerstoneTools or created by a 3rd party
 * - createToolGroup(toolGroupId)
 * - toolGroup.addTool(${toolName}) // NOT THE TOOL CLASS
 * - toolGroup.setToolActive(${toolName})
 *
 * @param ToolClass - A tool calls to instantiate.
 * @param toolOptions - The tool-specific configuration options for the tool.
 * @returns
 */
export function addTool(ToolClass): void {
  // Check if tool exists and name is not undefined
  const toolName = ToolClass.toolName;
  const toolAlreadyAdded = state.tools[toolName] !== undefined;
 
  Iif (!toolName) {
    throw new Error(`No Tool Found for the ToolClass ${ToolClass.name}`);
  }
 
  Iif (toolAlreadyAdded) {
    throw new Error(`${toolName} has already been added globally`);
  }
 
  // Stores the toolNames and ToolClass to be instantiated in the toolGroup on toolGroup.addTool
  state.tools[toolName] = {
    toolClass: ToolClass,
  };
}
 
/**
 * Removes the tool class from the cornerstoneTools.
 *
 * @param ToolClass - A tool calls to instantiate.
 */
export function removeTool(ToolClass): void {
  const toolName = ToolClass.toolName;
 
  if (!toolName) {
    throw new Error(`No tool found for: ${ToolClass.name}`);
  }
 
  if (!state.tools[toolName] !== undefined) {
    delete state.tools[toolName];
  } else {
    throw new Error(
      `${toolName} cannot be removed because it has not been added`
    );
  }
}
 
export default addTool;