refactor: introduce AbstractPathShape base class for shape hierarchy by tbouffard · Pull Request #953 · maxGraph/maxGraph · GitHub
Skip to content
Merged
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
4 changes: 2 additions & 2 deletions packages/core/src/view/shape/Shape.ts
83 changes: 83 additions & 0 deletions packages/core/src/view/shape/node/AbstractPathShape.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
Copyright 2021-present The maxGraph project Contributors
Copyright (c) 2006-2015, JGraph Ltd
Copyright (c) 2006-2015, Gaudenz Alder

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

import type Rectangle from '../../geometry/Rectangle.js';
import Shape from '../Shape.js';
import type AbstractCanvas2D from '../../canvas/AbstractCanvas2D.js';
import type { ColorValue } from '../../../types.js';
import { NONE } from '../../../util/Constants.js';

/**
* Base {@link Shape} for vertex shapes rendered from a single path.
*
* If a custom shape with one filled area is needed, override {@link redrawPath} as in the following example:
*
* ```typescript
* class SampleShape extends AbstractPathShape {
* redrawPath(c: AbstractCanvas2D, x: number, y: number, w: number, h: number) {
* c.moveTo(0, 0);
* c.lineTo(w, h);
* // ...
* c.close();
* }
* }
* ```
*
* @category Vertex Shapes
*/
export abstract class AbstractPathShape extends Shape {
constructor(
bounds: Rectangle | null = null,
fill: ColorValue = NONE,
stroke: ColorValue = NONE,
strokeWidth = 1
) {
super();
this.bounds = bounds;
this.fill = fill;
this.stroke = stroke;
this.strokeWidth = strokeWidth;
}

/**
* Redirects to redrawPath for subclasses to work.
*/
override paintVertexShape(
c: AbstractCanvas2D,
x: number,
y: number,
w: number,
h: number
) {
c.translate(x, y);
c.begin();
this.redrawPath(c, x, y, w, h);
c.fillAndStroke();
}

/**
* Draws the path for this shape.
*/
abstract redrawPath(
c: AbstractCanvas2D,
x: number,
y: number,
w: number,
h: number
): void;
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
54 changes: 4 additions & 50 deletions packages/core/src/view/shape/node/ActorShape.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,67 +16,21 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

import type Rectangle from '../../geometry/Rectangle.js';
import Shape from '../Shape.js';
import { AbstractPathShape } from './AbstractPathShape.js';
import type AbstractCanvas2D from '../../canvas/AbstractCanvas2D.js';
import { ColorValue } from '../../../types.js';
import { NONE } from '../../../util/Constants.js';

/**
* Extends {@link Shape} to implement an actor shape.
* Path-based actor vertex shape built on {@link AbstractPathShape}.
*
* This shape is registered under `actor` in {@link CellRenderer} when using {@link Graph} or calling {@link registerDefaultShapes}.
*
* If a custom shape with one filled area is needed, then this shape's {@link redrawPath} method should be overridden
* like in the following example:
*
* ```typescript
* class SampleShape extends ActorShape {
* redrawPath(c: AbstractCanvas2D, x: number, y: number, w: number, h: number) {
* path.moveTo(0, 0);
* path.lineTo(w, h);
* // ...
* path.close();
* }
* }
* ```
*
* @category Vertex Shapes
*/
class ActorShape extends Shape {
constructor(
bounds: Rectangle | null = null,
fill: ColorValue = NONE,
stroke: ColorValue = NONE,
strokeWidth = 1
) {
super();
this.bounds = bounds;
this.fill = fill;
this.stroke = stroke;
this.strokeWidth = strokeWidth;
}

/**
* Redirects to redrawPath for subclasses to work.
*/
override paintVertexShape(
c: AbstractCanvas2D,
x: number,
y: number,
w: number,
h: number
) {
c.translate(x, y);
c.begin();
this.redrawPath(c, x, y, w, h);
c.fillAndStroke();
}

class ActorShape extends AbstractPathShape {
/**
* Draws the path for this shape.
*/
redrawPath(c: AbstractCanvas2D, x: number, y: number, w: number, h: number) {
override redrawPath(c: AbstractCanvas2D, x: number, y: number, w: number, h: number) {
const width = w / 3;
c.moveTo(0, h);
c.curveTo(0, (3 * h) / 5, 0, (2 * h) / 5, w / 2, (2 * h) / 5);
Expand Down
17 changes: 4 additions & 13 deletions packages/core/src/view/shape/node/CloudShape.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,17 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

import ActorShape from './ActorShape.js';
import AbstractCanvas2D from '../../canvas/AbstractCanvas2D.js';
import Rectangle from '../../geometry/Rectangle.js';
import { AbstractPathShape } from './AbstractPathShape.js';
import type AbstractCanvas2D from '../../canvas/AbstractCanvas2D.js';

/**
* Extends {@link ActorShape} to implement a cloud shape.
* Path-based cloud vertex shape built on {@link AbstractPathShape}.
*
* This shape is registered under `cloud` in {@link CellRenderer} when using {@link Graph} or calling {@link registerDefaultShapes}.
*
* @category Vertex Shapes
*/
class CloudShape extends ActorShape {
constructor(bounds: Rectangle, fill: string, stroke: string, strokeWidth = 1) {
super();
this.bounds = bounds;
this.fill = fill;
this.stroke = stroke;
this.strokeWidth = strokeWidth;
}

class CloudShape extends AbstractPathShape {
/**
* Draws the path for this shape.
*/
Expand Down
10 changes: 3 additions & 7 deletions packages/core/src/view/shape/node/HexagonShape.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

import ActorShape from './ActorShape.js';
import { AbstractPathShape } from './AbstractPathShape.js';
import Point from '../../geometry/Point.js';
import AbstractCanvas2D from '../../canvas/AbstractCanvas2D.js';
import type AbstractCanvas2D from '../../canvas/AbstractCanvas2D.js';

/**
* Implementation of the hexagon shape.
Expand All @@ -27,11 +27,7 @@ import AbstractCanvas2D from '../../canvas/AbstractCanvas2D.js';
*
* @category Vertex Shapes
*/
class HexagonShape extends ActorShape {
constructor() {
super();
}

class HexagonShape extends AbstractPathShape {
/**
* Draws the path for this shape.
*/
Expand Down
10 changes: 3 additions & 7 deletions packages/core/src/view/shape/node/TriangleShape.ts
Loading