Skip to content

Commit

Permalink
Add getShape to draw factories
Browse files Browse the repository at this point in the history
  • Loading branch information
ivmartel committed Nov 8, 2024
1 parent 49d83ee commit 65710ef
Show file tree
Hide file tree
Showing 8 changed files with 145 additions and 114 deletions.
37 changes: 18 additions & 19 deletions src/tools/arrow.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ import {
import {Point2D} from '../math/point';
import {defaults} from '../app/defaults';
import {logger} from '../utils/logger';
import {DRAW_DEBUG, getDefaultAnchor} from './drawBounds';
import {
getLineShape,
DRAW_DEBUG,
getDefaultAnchor
} from './drawBounds';
import {LabelFactory} from './labelFactory';

// external
Expand Down Expand Up @@ -237,12 +241,7 @@ export class ArrowFactory {
return;
}
// associated shape
const kline = group.getChildren(function (node) {
return node.name() === 'shape';
})[0];
if (!(kline instanceof Konva.Line)) {
return;
}
const kline = this.#getShape(group);
// find anchors
const begin = group.getChildren(function (node) {
return node.id() === 'anchor0';
Expand Down Expand Up @@ -312,12 +311,7 @@ export class ArrowFactory {
* @param {Konva.Group} group The shape group.
*/
updateConnector(group) {
const kshape = group.getChildren(function (node) {
return node.name() === 'shape';
})[0];
if (!(kshape instanceof Konva.Line)) {
return;
}
const kshape = this.#getShape(group);
const connectorsPos = this.#getConnectorsPositions(kshape);
this.#labelFactory.updateConnector(group, connectorsPos);
}
Expand Down Expand Up @@ -386,6 +380,16 @@ export class ArrowFactory {
return kshape;
}

/**
* Get the associated shape from a group.
*
* @param {Konva.Group} group The group to look into.
* @returns {Konva.Line|undefined} The shape.
*/
#getShape(group) {
return getLineShape(group);
}

/**
* Creates the konva shape extras.
*
Expand Down Expand Up @@ -453,12 +457,7 @@ export class ArrowFactory {
return;
}
// associated shape
const kline = group.getChildren(function (node) {
return node.name() === 'shape';
})[0];
if (!(kline instanceof Konva.Line)) {
return;
}
const kline = this.#getShape(group);

// reset position after possible shape drag
kline.position({x: 0, y: 0});
Expand Down
34 changes: 21 additions & 13 deletions src/tools/circle.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ import {Circle} from '../math/circle';
import {Point2D} from '../math/point';
import {logger} from '../utils/logger';
import {defaults} from '../app/defaults';
import {DRAW_DEBUG, getDefaultAnchor} from './drawBounds';
import {
isNodeNameShape,
DRAW_DEBUG,
getDefaultAnchor
} from './drawBounds';
import {LabelFactory} from './labelFactory';

// external
Expand Down Expand Up @@ -316,12 +320,7 @@ export class CircleFactory {
* @param {Konva.Group} group The shape group.
*/
updateConnector(group) {
const kshape = group.getChildren(function (node) {
return node.name() === 'shape';
})[0];
if (!(kshape instanceof Konva.Circle)) {
return;
}
const kshape = this.#getShape(group);
const connectorsPos = this.#getConnectorsPositions(kshape);
this.#labelFactory.updateConnector(group, connectorsPos);
}
Expand Down Expand Up @@ -372,6 +371,20 @@ export class CircleFactory {
});
}

/**
* Get the associated shape from a group.
*
* @param {Konva.Group} group The group to look into.
* @returns {Konva.Circle|undefined} The shape.
*/
#getShape(group) {
const kshape = group.getChildren(isNodeNameShape)[0];
if (!(kshape instanceof Konva.Circle)) {
return;
}
return kshape;
}

/**
* Get the default annotation label position.
*
Expand Down Expand Up @@ -407,12 +420,7 @@ export class CircleFactory {
return;
}
// associated shape
const kcircle = group.getChildren(function (node) {
return node.name() === 'shape';
})[0];
if (!(kcircle instanceof Konva.Circle)) {
return;
}
const kcircle = this.#getShape(group);
// update shape: just update the radius
kcircle.radius(radius);

Expand Down
14 changes: 14 additions & 0 deletions src/tools/drawBounds.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,20 @@ export function isPositionNode(node) {
return node.name() === 'position-group';
}

/**
* Get a Konva.Line shape from a group.
*
* @param {Konva.Group} group The group to look into.
* @returns {Konva.Line|undefined} The shape.
*/
export function getLineShape(group) {
const kshape = group.getChildren(isNodeNameShape)[0];
if (!(kshape instanceof Konva.Line)) {
return;
}
return kshape;
}

/**
* @callback testFn
* @param {Konva.Node} node The node.
Expand Down
34 changes: 21 additions & 13 deletions src/tools/ellipse.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ import {Ellipse} from '../math/ellipse';
import {Point2D} from '../math/point';
import {logger} from '../utils/logger';
import {defaults} from '../app/defaults';
import {DRAW_DEBUG, getDefaultAnchor} from './drawBounds';
import {
isNodeNameShape,
DRAW_DEBUG,
getDefaultAnchor
} from './drawBounds';
import {LabelFactory} from './labelFactory';

// external
Expand Down Expand Up @@ -336,12 +340,7 @@ export class EllipseFactory {
* @param {Konva.Group} group The shape group.
*/
updateConnector(group) {
const kshape = group.getChildren(function (node) {
return node.name() === 'shape';
})[0];
if (!(kshape instanceof Konva.Ellipse)) {
return;
}
const kshape = this.#getShape(group);
const connectorsPos = this.#getConnectorsPositions(kshape);
this.#labelFactory.updateConnector(group, connectorsPos);
}
Expand Down Expand Up @@ -397,6 +396,20 @@ export class EllipseFactory {
});
}

/**
* Get the associated shape from a group.
*
* @param {Konva.Group} group The group to look into.
* @returns {Konva.Ellipse|undefined} The shape.
*/
#getShape(group) {
const kshape = group.getChildren(isNodeNameShape)[0];
if (!(kshape instanceof Konva.Ellipse)) {
return;
}
return kshape;
}

/**
* Get the default annotation label position.
*
Expand Down Expand Up @@ -431,12 +444,7 @@ export class EllipseFactory {
return;
}
// associated shape
const kellipse = group.getChildren(function (node) {
return node.name() === 'shape';
})[0];
if (!(kellipse instanceof Konva.Ellipse)) {
return;
}
const kellipse = this.#getShape(group);
// update shape: just update radius
kellipse.radius({
x: radiusX,
Expand Down
37 changes: 18 additions & 19 deletions src/tools/protractor.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ import {Line, getAngle} from '../math/line';
import {Protractor} from '../math/protractor';
import {Point2D} from '../math/point';
import {defaults} from '../app/defaults';
import {DRAW_DEBUG, getDefaultAnchor} from './drawBounds';
import {
getLineShape,
DRAW_DEBUG,
getDefaultAnchor
} from './drawBounds';
import {LabelFactory} from './labelFactory';

// external
Expand Down Expand Up @@ -239,12 +243,7 @@ export class ProtractorFactory {
return;
}
// associated shape
const kline = group.getChildren(function (node) {
return node.name() === 'shape';
})[0];
if (!(kline instanceof Konva.Line)) {
return;
}
const kline = this.#getShape(group);
// find special points
const begin = group.getChildren(function (node) {
return node.id() === 'anchor0';
Expand Down Expand Up @@ -313,12 +312,7 @@ export class ProtractorFactory {
* @param {Konva.Group} group The shape group.
*/
updateConnector(group) {
const kshape = group.getChildren(function (node) {
return node.name() === 'shape';
})[0];
if (!(kshape instanceof Konva.Line)) {
return;
}
const kshape = this.#getShape(group);
const connectorsPos = this.#getConnectorsPositions(kshape);
this.#labelFactory.updateConnector(group, connectorsPos);
}
Expand Down Expand Up @@ -384,6 +378,16 @@ export class ProtractorFactory {
return kshape;
}

/**
* Get the associated shape from a group.
*
* @param {Konva.Group} group The group to look into.
* @returns {Konva.Line|undefined} The shape.
*/
#getShape(group) {
return getLineShape(group);
}

/**
* Creates the konva shape extras.
*
Expand Down Expand Up @@ -467,12 +471,7 @@ export class ProtractorFactory {
return;
}
// associated shape
const kline = group.getChildren(function (node) {
return node.name() === 'shape';
})[0];
if (!(kline instanceof Konva.Line)) {
return;
}
const kline = this.#getShape(group);

// reset position after possible shape drag
kline.position({x: 0, y: 0});
Expand Down
34 changes: 21 additions & 13 deletions src/tools/rectangle.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ import {Rectangle} from '../math/rectangle';
import {Point2D} from '../math/point';
import {logger} from '../utils/logger';
import {defaults} from '../app/defaults';
import {DRAW_DEBUG, getDefaultAnchor} from './drawBounds';
import {
isNodeNameShape,
DRAW_DEBUG,
getDefaultAnchor
} from './drawBounds';
import {LabelFactory} from './labelFactory';

// external
Expand Down Expand Up @@ -296,12 +300,7 @@ export class RectangleFactory {
* @param {Konva.Group} group The shape group.
*/
updateConnector(group) {
const kshape = group.getChildren(function (node) {
return node.name() === 'shape';
})[0];
if (!(kshape instanceof Konva.Rect)) {
return;
}
const kshape = this.#getShape(group);
const connectorsPos = this.#getConnectorsPositions(kshape);
this.#labelFactory.updateConnector(group, connectorsPos);
}
Expand Down Expand Up @@ -347,6 +346,20 @@ export class RectangleFactory {
});
}

/**
* Get the associated shape from a group.
*
* @param {Konva.Group} group The group to look into.
* @returns {Konva.Rect|undefined} The shape.
*/
#getShape(group) {
const kshape = group.getChildren(isNodeNameShape)[0];
if (!(kshape instanceof Konva.Rect)) {
return;
}
return kshape;
}

/**
* Get the default annotation label position.
*
Expand Down Expand Up @@ -378,12 +391,7 @@ export class RectangleFactory {
return;
}
// associated shape
const krect = group.getChildren(function (node) {
return node.name() === 'shape';
})[0];
if (!(krect instanceof Konva.Rect)) {
return;
}
const krect = this.#getShape(group);
// update shape
krect.position({
x: begin.getX(),
Expand Down
Loading

0 comments on commit 65710ef

Please sign in to comment.