Skip to content

refactor(Tools): Cleanup code for drawingSvg, make 'data-id' attribut… #1952

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions packages/tools/src/drawingSvg/_draw.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import type { SVGDrawingHelper } from '../types';
import setAttributesIfNecessary from './setAttributesIfNecessary';
import setNewAttributesIfValid from './setNewAttributesIfValid';

function _draw(
what: 'circle' | 'line' | 'ellipse' | 'rect' | 'path' | 'polyline' | 'g',
svgDrawingHelper: SVGDrawingHelper,
annotationUID: string,
drawingId: string,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
attributes: Record<string, any>
): {
Comment on lines +5 to +12
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you use an object instead of positional parameters?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

over my dead body ;-). this is typically over engineering in my opinion.

( we are talking of 5 positional arguments that fit into a single line of text in most cases )

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The issue is, later on, we're developing another tool that needs to use the _draw, but for some weird reason, it doesn't actually need drawingId or whatever. Then we end up using _draw like this:

_draw(null, svgDrawingHelper, null, ...)

I hope that makes sense.

element: SVGGElement;
isNew: boolean;
} {
// variable for the namespace
const svgns = 'http://www.w3.org/2000/svg';
const dataId = `${annotationUID}-${what}-${drawingId}`;
const existingElement = svgDrawingHelper.getSvgNode(dataId);

if (existingElement) {
setAttributesIfNecessary(attributes, existingElement);
svgDrawingHelper.setNodeTouched(dataId);
return { element: existingElement, isNew: false };
} else {
const newElement = document.createElementNS(svgns, what);
newElement.setAttribute('data-id', dataId);
setNewAttributesIfValid(attributes, newElement);
svgDrawingHelper.appendNode(newElement, dataId);
return { element: newElement, isNew: true };
}
}

export default _draw;
9 changes: 0 additions & 9 deletions packages/tools/src/drawingSvg/_getHash.ts

This file was deleted.

29 changes: 3 additions & 26 deletions packages/tools/src/drawingSvg/drawCircle.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
import type { Types } from '@cornerstonejs/core';
import type { SVGDrawingHelper } from '../types';

import _getHash from './_getHash';

import setAttributesIfNecessary from './setAttributesIfNecessary';
import setNewAttributesIfValid from './setNewAttributesIfValid';
import _draw from './_draw';

function drawCircle(
svgDrawingHelper: SVGDrawingHelper,
annotationUID: string,
circleUID: string,
center: Types.Point2,
radius: number,
options = {},
dataId = ''
options = {}
): void {
const {
color,
Expand All @@ -39,11 +35,6 @@ function drawCircle(
// for supporting both lineWidth and width options
const strokeWidth = lineWidth || width;

// variable for the namespace
const svgns = 'http://www.w3.org/2000/svg';
const svgNodeHash = _getHash(annotationUID, 'circle', circleUID);
const existingCircleElement = svgDrawingHelper.getSvgNode(svgNodeHash);

const attributes = {
cx: `${center[0]}`,
cy: `${center[1]}`,
Expand All @@ -56,21 +47,7 @@ function drawCircle(
'stroke-opacity': strokeOpacity, // setting stroke opacity
};

if (existingCircleElement) {
setAttributesIfNecessary(attributes, existingCircleElement);

svgDrawingHelper.setNodeTouched(svgNodeHash);
} else {
const newCircleElement = document.createElementNS(svgns, 'circle');

if (dataId !== '') {
newCircleElement.setAttribute('data-id', dataId);
}

setNewAttributesIfValid(attributes, newCircleElement);

svgDrawingHelper.appendNode(newCircleElement, svgNodeHash);
}
_draw('circle', svgDrawingHelper, annotationUID, circleUID, attributes);
}

export default drawCircle;
7 changes: 2 additions & 5 deletions packages/tools/src/drawingSvg/drawEllipse.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type { Types } from '@cornerstonejs/core';
import type { SVGDrawingHelper } from '../types';

import _getHash from './_getHash';
import drawEllipseByCoordinates from './drawEllipseByCoordinates';

function drawEllipse(
Expand All @@ -10,8 +9,7 @@ function drawEllipse(
ellipseUID: string,
corner1: Types.Point2,
corner2: Types.Point2,
options = {},
dataId = ''
options = {}
) {
const top: Types.Point2 = [(corner1[0] + corner2[0]) / 2, corner1[1]];
const bottom: Types.Point2 = [(corner1[0] + corner2[0]) / 2, corner2[1]];
Expand All @@ -23,8 +21,7 @@ function drawEllipse(
annotationUID,
ellipseUID,
[bottom, top, left, right],
(options = {}),
(dataId = '')
(options = {})
);
}

Expand Down
30 changes: 3 additions & 27 deletions packages/tools/src/drawingSvg/drawEllipseByCoordinates.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
import type { Types } from '@cornerstonejs/core';
import type { SVGDrawingHelper } from '../types';

import _getHash from './_getHash';
import setAttributesIfNecessary from './setAttributesIfNecessary';
import setNewAttributesIfValid from './setNewAttributesIfValid';
import _draw from './_draw';

function drawEllipseByCoordinates(
svgDrawingHelper: SVGDrawingHelper,
annotationUID: string,
ellipseUID: string,
canvasCoordinates: [Types.Point2, Types.Point2, Types.Point2, Types.Point2],
options = {},
dataId = ''
options = {}
): void {
const { color, width, lineWidth, lineDash } = Object.assign(
{
Expand All @@ -25,13 +21,7 @@ function drawEllipseByCoordinates(

// for supporting both lineWidth and width options
const strokeWidth = lineWidth || width;

const svgns = 'http://www.w3.org/2000/svg';
const svgNodeHash = _getHash(annotationUID, 'ellipse', ellipseUID);
const existingEllipse = svgDrawingHelper.getSvgNode(svgNodeHash);

const [bottom, top, left, right] = canvasCoordinates;

const w = Math.hypot(left[0] - right[0], left[1] - right[1]);
const h = Math.hypot(top[0] - bottom[0], top[1] - bottom[1]);
const angle =
Expand All @@ -53,21 +43,7 @@ function drawEllipseByCoordinates(
'stroke-dasharray': lineDash,
};

if (existingEllipse) {
setAttributesIfNecessary(attributes, existingEllipse);

svgDrawingHelper.setNodeTouched(svgNodeHash);
} else {
const svgEllipseElement = document.createElementNS(svgns, 'ellipse');

if (dataId !== '') {
svgEllipseElement.setAttribute('data-id', dataId);
}

setNewAttributesIfValid(attributes, svgEllipseElement);

svgDrawingHelper.appendNode(svgEllipseElement, svgNodeHash);
}
_draw('ellipse', svgDrawingHelper, annotationUID, ellipseUID, attributes);
}

export default drawEllipseByCoordinates;
34 changes: 8 additions & 26 deletions packages/tools/src/drawingSvg/drawHandle.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import type { Types } from '@cornerstonejs/core';

import _getHash from './_getHash';
import setNewAttributesIfValid from './setNewAttributesIfValid';
import setAttributesIfNecessary from './setAttributesIfNecessary';
import type { SVGDrawingHelper } from '../types';
import _draw from './_draw';

function drawHandle(
svgDrawingHelper: SVGDrawingHelper,
Expand All @@ -30,14 +27,6 @@ function drawHandle(
// for supporting both lineWidth and width options
const strokeWidth = lineWidth || width;

// variable for the namespace
const svgns = 'http://www.w3.org/2000/svg';
const svgNodeHash = _getHash(
annotationUID,
'handle',
`hg-${handleGroupUID}-index-${uniqueIndex}`
);

let attributes;
if (type === 'circle') {
attributes = {
Expand Down Expand Up @@ -69,20 +58,13 @@ function drawHandle(
} else {
throw new Error(`Unsupported handle type: ${type}`);
}

const existingHandleElement = svgDrawingHelper.getSvgNode(svgNodeHash);

if (existingHandleElement) {
setAttributesIfNecessary(attributes, existingHandleElement);

svgDrawingHelper.setNodeTouched(svgNodeHash);
} else {
const newHandleElement = document.createElementNS(svgns, type);

setNewAttributesIfValid(attributes, newHandleElement);

svgDrawingHelper.appendNode(newHandleElement, svgNodeHash);
}
_draw(
type,
svgDrawingHelper,
annotationUID,
`hg-${handleGroupUID}-index-${uniqueIndex}`,
attributes
);
}

export default drawHandle;
28 changes: 3 additions & 25 deletions packages/tools/src/drawingSvg/drawLine.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
import type { Types } from '@cornerstonejs/core';

import _getHash from './_getHash';
import setNewAttributesIfValid from './setNewAttributesIfValid';
import setAttributesIfNecessary from './setAttributesIfNecessary';
import type { SVGDrawingHelper } from '../types';
import _draw from './_draw';

export default function drawLine(
svgDrawingHelper: SVGDrawingHelper,
annotationUID: string,
lineUID: string,
start: Types.Point2,
end: Types.Point2,
options = {},
dataId = ''
options = {}
): void {
// if length is NaN return
if (isNaN(start[0]) || isNaN(start[1]) || isNaN(end[0]) || isNaN(end[1])) {
Expand Down Expand Up @@ -40,9 +36,6 @@ export default function drawLine(
// for supporting both lineWidth and width options
const strokeWidth = lineWidth || width;

const svgns = 'http://www.w3.org/2000/svg';
const svgNodeHash = _getHash(annotationUID, 'line', lineUID);
const existingLine = svgDrawingHelper.getSvgNode(svgNodeHash);
const layerId = svgDrawingHelper.svgLayerElement.id;
const dropShadowStyle = shadow ? `filter:url(#shadow-${layerId});` : '';

Expand All @@ -59,20 +52,5 @@ export default function drawLine(
'marker-end': markerEndId ? `url(#${markerEndId})` : '',
};

if (existingLine) {
// This is run to avoid re-rendering annotations that actually haven't changed
setAttributesIfNecessary(attributes, existingLine);

svgDrawingHelper.setNodeTouched(svgNodeHash);
} else {
const newLine = document.createElementNS(svgns, 'line');

if (dataId !== '') {
newLine.setAttribute('data-id', dataId);
}

setNewAttributesIfValid(attributes, newLine);

svgDrawingHelper.appendNode(newLine, svgNodeHash);
}
_draw('line', svgDrawingHelper, annotationUID, lineUID, attributes);
}
19 changes: 2 additions & 17 deletions packages/tools/src/drawingSvg/drawPath.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import type { Types } from '@cornerstonejs/core';
import _getHash from './_getHash';
import setNewAttributesIfValid from './setNewAttributesIfValid';
import setAttributesIfNecessary from './setAttributesIfNecessary';
import type { SVGDrawingHelper } from '../types';
import _draw from './_draw';

/**
* Draws an SVG path with the given points.
Expand Down Expand Up @@ -44,9 +42,6 @@ export default function drawPath(
// for supporting both lineWidth and width options
const strokeWidth = lineWidth || width;

const svgns = 'http://www.w3.org/2000/svg';
const svgNodeHash = _getHash(annotationUID, 'path', pathUID);
const existingNode = svgDrawingHelper.getSvgNode(svgNodeHash);
let pointsAttribute = '';

for (let i = 0, numArrays = pointsArrays.length; i < numArrays; i++) {
Expand Down Expand Up @@ -84,15 +79,5 @@ export default function drawPath(
'stroke-dasharray': lineDash,
};

if (existingNode) {
// This is run to avoid re-rendering annotations that actually haven't changed
setAttributesIfNecessary(attributes, existingNode);

svgDrawingHelper.setNodeTouched(svgNodeHash);
} else {
const newNode = document.createElementNS(svgns, 'path');

setNewAttributesIfValid(attributes, newNode);
svgDrawingHelper.appendNode(newNode, svgNodeHash);
}
_draw('path', svgDrawingHelper, annotationUID, pathUID, attributes);
}
23 changes: 2 additions & 21 deletions packages/tools/src/drawingSvg/drawPolyline.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import type { Types } from '@cornerstonejs/core';
import _getHash from './_getHash';
import setNewAttributesIfValid from './setNewAttributesIfValid';
import setAttributesIfNecessary from './setAttributesIfNecessary';
import type { SVGDrawingHelper } from '../types';
import _draw from './_draw';

/**
* Draws an SVG polyline with the given points.
Expand Down Expand Up @@ -45,11 +43,6 @@ export default function drawPolyline(

// for supporting both lineWidth and width options
const strokeWidth = lineWidth || width;

const svgns = 'http://www.w3.org/2000/svg';
const svgNodeHash = _getHash(annotationUID, 'polyline', polylineUID);
const existingPolyLine = svgDrawingHelper.getSvgNode(svgNodeHash);

let pointsAttribute = '';

for (const point of points) {
Expand All @@ -72,17 +65,5 @@ export default function drawPolyline(
'marker-start': markerStartId ? `url(#${markerStartId})` : '',
'marker-end': markerEndId ? `url(#${markerEndId})` : '',
};

if (existingPolyLine) {
// This is run to avoid re-rendering annotations that actually haven't changed
setAttributesIfNecessary(attributes, existingPolyLine);

svgDrawingHelper.setNodeTouched(svgNodeHash);
} else {
const newPolyLine = document.createElementNS(svgns, 'polyline');

setNewAttributesIfValid(attributes, newPolyLine);

svgDrawingHelper.appendNode(newPolyLine, svgNodeHash);
}
_draw('polyline', svgDrawingHelper, annotationUID, polylineUID, attributes);
}
8 changes: 2 additions & 6 deletions packages/tools/src/drawingSvg/drawRect.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import type { Types } from '@cornerstonejs/core';

import _getHash from './_getHash';
import type { SVGDrawingHelper } from '../types';
import drawRectByCoordinates from './drawRectByCoordinates';

Expand All @@ -12,8 +10,7 @@ export default function drawRect(
rectangleUID: string,
start: Types.Point2,
end: Types.Point2,
options = {},
dataId = ''
options = {}
): void {
const topLeft: Types.Point2 = [start[0], start[1]];
const topRight: Types.Point2 = [end[0], start[1]];
Expand All @@ -25,7 +22,6 @@ export default function drawRect(
annotationUID,
rectangleUID,
[topLeft, topRight, bottomLeft, bottomRight],
options,
dataId
options
);
}
Loading