All files / packages/tools/src/tools/annotation KeyImageTool.ts

0.92% Statements 1/108
0% Branches 0/24
0% Functions 0/18
0.93% Lines 1/107

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 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  1x      
import { Events } from '../../enums';
import { getEnabledElement, utilities as csUtils } from '@cornerstonejs/core';
import type { Types } from '@cornerstonejs/core';
 
import { AnnotationTool } from '../base';
import {
  addAnnotation,
  getAnnotations,
  removeAnnotation,
} from '../../stateManagement/annotation/annotationState';
 
import {
  triggerAnnotationCompleted,
  triggerAnnotationModified,
} from '../../stateManagement/annotation/helpers/state';
import { drawArrow as drawArrowSvg } from '../../drawingSvg';
import { state } from '../../store';
import { getViewportIdsWithToolToRender } from '../../utilities/viewportFilters';
import triggerAnnotationRenderForViewportIds from '../../utilities/triggerAnnotationRenderForViewportIds';
 
import { resetElementCursor } from '../../cursors/elementCursor';
 
import {
  EventTypes,
  ToolHandle,
  PublicToolProps,
  ToolProps,
  SVGDrawingHelper,
} from '../../types';
import { StyleSpecifier } from '../../types/AnnotationStyle';
import { Annotation } from '../../types';
 
type Point2 = Types.Point2;
 
class KeyImageTool extends AnnotationTool {
  static toolName;
 
  public touchDragCallback: any;
  public mouseDragCallback: any;
  _throttledCalculateCachedStats: any;
  editData: {
    annotation: any;
    viewportIdsToRender: string[];
    handleIndex?: number;
    movingTextBox?: boolean;
    newAnnotation?: boolean;
    hasMoved?: boolean;
  } | null;
  isDrawing: boolean;
  isHandleOutsideImage: boolean;
 
  constructor(
    toolProps: PublicToolProps = {},
    defaultToolProps: ToolProps = {
      supportedInteractionTypes: ['Mouse', 'Touch'],
      configuration: {
        getTextCallback,
        changeTextCallback,
        canvasPosition: [10, 10],
        canvasSize: 10,
      },
    }
  ) {
    super(toolProps, defaultToolProps);
  }
 
  /**
   * Based on the current position of the mouse and the current imageId to create
   * a Length Annotation and stores it in the annotationManager
   *
   * @param evt -  EventTypes.NormalizedMouseEventType
   * @returns The annotation object.
   *
   */
  addNewAnnotation = (evt: EventTypes.InteractionEventType) => {
    const eventDetail = evt.detail;
    const { currentPoints, element } = eventDetail;
    const worldPos = currentPoints.world;
    const enabledElement = getEnabledElement(element);
    const { viewport, renderingEngine } = enabledElement;
 
    const camera = viewport.getCamera();
    const { viewPlaneNormal, viewUp } = camera;
 
    const referencedImageId = this.getReferencedImageId(
      viewport,
      worldPos,
      viewPlaneNormal,
      viewUp
    );
 
    const annotation = KeyImageTool.createAnnotation({
      metadata: { ...viewport.getViewReference(), referencedImageId },
    });
 
    addAnnotation(annotation, element);
 
    const viewportIdsToRender = getViewportIdsWithToolToRender(
      element,
      this.getToolName()
    );
 
    evt.preventDefault();
 
    triggerAnnotationRenderForViewportIds(renderingEngine, viewportIdsToRender);
 
    this.configuration.getTextCallback((text) => {
      if (!text) {
        removeAnnotation(annotation.annotationUID);
        triggerAnnotationRenderForViewportIds(
          renderingEngine,
          viewportIdsToRender
        );
        this.isDrawing = false;
        return;
      }
      annotation.data.text = text;
 
      triggerAnnotationCompleted(annotation);
 
      triggerAnnotationRenderForViewportIds(
        renderingEngine,
        viewportIdsToRender
      );
    });
 
    return annotation;
  };
 
  public cancel() {
    // No op - the annotation can't be in a partial state
  }
 
  /**
   * It returns if the canvas point is near the provided length annotation in the provided
   * element or not. A proximity is passed to the function to determine the
   * proximity of the point to the annotation in number of pixels.
   *
   * @param element - HTML Element
   * @param annotation - Annotation
   * @param canvasCoords - Canvas coordinates
   * @param proximity - Proximity to tool to consider
   * @returns Boolean, whether the canvas point is near tool
   */
  isPointNearTool = (
    element: HTMLDivElement,
    annotation: Annotation,
    canvasCoords: Types.Point2,
    proximity: number
  ): boolean => {
    const enabledElement = getEnabledElement(element);
    const { viewport } = enabledElement;
    const { data } = annotation;
 
    const { canvasPosition, canvasSize } = this.configuration;
    if (!canvasPosition?.length) {
      return false;
    }
    if (
      Math.abs(canvasCoords[0] - canvasPosition[0] + canvasSize / 2) <=
        canvasSize / 2 &&
      Math.abs(canvasCoords[1] - canvasPosition[1] + canvasSize / 2) <=
        canvasSize / 2
    ) {
      return true;
    }
    return false;
  };
 
  toolSelectedCallback = (
    evt: EventTypes.InteractionEventType,
    annotation: Annotation
  ): void => {
    annotation.highlighted = true;
 
    evt.preventDefault();
  };
 
  handleSelectedCallback(
    evt: EventTypes.InteractionEventType,
    annotation: Annotation,
    handle: ToolHandle
  ): void {
    // Nothing special to do here.
  }
 
  _endCallback = (evt: EventTypes.InteractionEventType): void => {
    const eventDetail = evt.detail;
    const { element } = eventDetail;
 
    this._deactivateModify(element);
    resetElementCursor(element);
  };
 
  doubleClickCallback = (evt: EventTypes.TouchTapEventType): void => {
    const eventDetail = evt.detail;
    const { element } = eventDetail;
    let annotations = getAnnotations(this.getToolName(), element);
 
    annotations = this.filterInteractableAnnotationsForElement(
      element,
      annotations
    );
 
    if (!annotations?.length) {
      return;
    }
 
    const clickedAnnotation = annotations.find((annotation) =>
      this.isPointNearTool(
        element,
        annotation as Annotation,
        eventDetail.currentPoints.canvas,
        6 // Todo: get from configuration
      )
    );
 
    if (!clickedAnnotation) {
      return;
    }
 
    const annotation = clickedAnnotation as Annotation;
 
    this.configuration.changeTextCallback(
      clickedAnnotation,
      evt.detail,
      this._doneChangingTextCallback.bind(this, element, annotation)
    );
 
    this.isDrawing = false;
 
    // This double click was handled and the dialogue was displayed.
    // No need for any other listener to handle it too - stopImmediatePropagation
    // helps ensure this primarily so that no other listeners on the target element
    // get called.
    evt.stopImmediatePropagation();
    evt.preventDefault();
  };
 
  _doneChangingTextCallback(element, annotation, updatedText): void {
    annotation.data.text = updatedText;
 
    const enabledElement = getEnabledElement(element);
    const { renderingEngine } = enabledElement;
 
    const viewportIdsToRender = getViewportIdsWithToolToRender(
      element,
      this.getToolName()
    );
    triggerAnnotationRenderForViewportIds(renderingEngine, viewportIdsToRender);
 
    // Dispatching annotation modified
    triggerAnnotationModified(annotation, element);
  }
 
  _activateModify = (element: HTMLDivElement) => {
    state.isInteractingWithTool = true;
 
    element.addEventListener(
      Events.MOUSE_UP,
      this._endCallback as EventListener
    );
    element.addEventListener(
      Events.MOUSE_CLICK,
      this._endCallback as EventListener
    );
 
    element.addEventListener(
      Events.TOUCH_TAP,
      this._endCallback as EventListener
    );
    element.addEventListener(
      Events.TOUCH_END,
      this._endCallback as EventListener
    );
  };
 
  _deactivateModify = (element: HTMLDivElement) => {
    state.isInteractingWithTool = false;
 
    element.removeEventListener(
      Events.MOUSE_UP,
      this._endCallback as EventListener
    );
    element.removeEventListener(
      Events.MOUSE_CLICK,
      this._endCallback as EventListener
    );
 
    element.removeEventListener(
      Events.TOUCH_TAP,
      this._endCallback as EventListener
    );
    element.removeEventListener(
      Events.TOUCH_END,
      this._endCallback as EventListener
    );
  };
 
  /**
   * it is used to draw the length annotation in each
   * request animation frame. It calculates the updated cached statistics if
   * data is invalidated and cache it.
   *
   * @param enabledElement - The Cornerstone's enabledElement.
   * @param svgDrawingHelper - The svgDrawingHelper providing the context for drawing.
   */
  renderAnnotation = (
    enabledElement: Types.IEnabledElement,
    svgDrawingHelper: SVGDrawingHelper
  ): boolean => {
    let renderStatus = false;
    const { viewport } = enabledElement;
    const { element } = viewport;
 
    let annotations = getAnnotations(this.getToolName(), element);
 
    // Todo: We don't need this anymore, filtering happens in triggerAnnotationRender
    if (!annotations?.length) {
      return renderStatus;
    }
 
    annotations = this.filterInteractableAnnotationsForElement(
      element,
      annotations
    );
 
    if (!annotations?.length) {
      return renderStatus;
    }
 
    const styleSpecifier: StyleSpecifier = {
      toolGroupId: this.toolGroupId,
      toolName: this.getToolName(),
      viewportId: enabledElement.viewport.id,
    };
 
    // Draw SVG
    for (let i = 0; i < annotations.length; i++) {
      const annotation = annotations[i];
      const { annotationUID } = annotation;
 
      styleSpecifier.annotationUID = annotationUID;
 
      const { color } = this.getAnnotationStyle({
        annotation,
        styleSpecifier,
      });
 
      const { canvasPosition, canvasSize } = this.configuration;
      if (canvasPosition?.length) {
        const arrowUID = '1';
        drawArrowSvg(
          svgDrawingHelper,
          annotationUID,
          arrowUID,
          canvasPosition.map((it) => it + canvasSize) as Point2,
          canvasPosition as Point2,
          {
            color,
            width: 1,
          }
        );
      }
 
      renderStatus = true;
 
      // If rendering engine has been destroyed while rendering
      if (!viewport.getRenderingEngine()) {
        console.warn('Rendering Engine has been destroyed');
        return renderStatus;
      }
    }
 
    return renderStatus;
  };
 
  _isInsideVolume(index1, index2, dimensions) {
    return (
      csUtils.indexWithinDimensions(index1, dimensions) &&
      csUtils.indexWithinDimensions(index2, dimensions)
    );
  }
}
 
function getTextCallback(doneChangingTextCallback) {
  return doneChangingTextCallback(prompt('Enter your annotation:'));
}
 
function changeTextCallback(data, eventData, doneChangingTextCallback) {
  return doneChangingTextCallback(prompt('Enter your annotation:'));
}
 
KeyImageTool.toolName = 'KeyImage';
 
export default KeyImageTool;