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
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
Copyright 2025-present The maxGraph project Contributors

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 { describe, expect, test } from '@jest/globals';
import { StencilShapeRegistry } from '../../../../src';

describe('getStencil', () => {
test.each([null, undefined, 'unknown'])('pass %s, return undefined', (name) => {
expect(StencilShapeRegistry.getStencil(name)).toBeUndefined();
});
});
5 changes: 2 additions & 3 deletions packages/core/src/view/cell/CellRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,9 +178,8 @@ class CellRenderer {
createShape(state: CellState) {
let shape = null;

// Checks if there is a stencil for the name and creates
// a shape instance for the stencil if one exists
const stencil = StencilShapeRegistry.getStencil(<string>state.style.shape);
// Checks if there is a stencil for the name and creates a shape instance for the stencil if one exists
const stencil = StencilShapeRegistry.getStencil(state.style.shape);

if (stencil) {
shape = new Shape(stencil);
Expand Down
4 changes: 1 addition & 3 deletions packages/core/src/view/geometry/node/StencilShape.ts
Original file line number Diff line number Diff line change
Expand Up @@ -558,9 +558,7 @@ class StencilShape extends Shape {
);
}
} else if (name === 'include-shape') {
const stencil = StencilShapeRegistry.getStencil(
node.getAttribute('name') as string
);
const stencil = StencilShapeRegistry.getStencil(node.getAttribute('name'));

if (stencil) {
const x = x0 + Number(node.getAttribute('x')) * sx;
Expand Down
33 changes: 12 additions & 21 deletions packages/core/src/view/geometry/node/StencilShapeRegistry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,48 +23,39 @@ type Stencils = {
};

/**
* A singleton class that provides a registry for stencils and the methods
* for painting those stencils onto a canvas or into a DOM.
* A singleton class that provides a registry for stencils and the methods for painting those stencils onto a canvas or into a DOM.
*
* Code to add stencils:
* ```javascript
* let req = mxUtils.load('test/stencils.xml');
* let root = req.getDocumentElement();
* const response = load('test/stencils.xml');
* const root = response.getDocumentElement();
* let shape = root.firstChild;
*
* while (shape != null)
* {
* if (shape.nodeType === mxConstants.NODETYPE_ELEMENT)
* {
* mxStencilRegistry.addStencil(shape.getAttribute('name'), new mxStencil(shape));
* while (shape) {
* if (shape.nodeType === mxConstants.NODETYPE_ELEMENT) {
* StencilShapeRegistry.addStencil(shape.getAttribute('name'), new mxStencil(shape));
* }
*
* shape = shape.nextSibling;
* }
* ```
* @class StencilShapeRegistry
*/
class StencilShapeRegistry {
static stencils: Stencils = {};

/**
* Adds the given {@link Stencil}.
* @static
* @param {string} name
* @param {StencilShape} stencil
* Adds the given {@link StencilShape}.
*/
static addStencil(name: string, stencil: StencilShape) {
static addStencil(name: string, stencil: StencilShape): void {
StencilShapeRegistry.stencils[name] = stencil;
}

/**
* Returns the {@link Stencil} for the given name.
* @static
* @param {string} name
* @returns {StencilShape}
* Returns the {@link StencilShape} for the given name.
*/
static getStencil(name: string) {
return StencilShapeRegistry.stencils[name];
static getStencil(name?: string | null): StencilShape | undefined {
// Tests are validating that using the non-null assertion (!) is safe
return StencilShapeRegistry.stencils[name!];
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/html/stories/Stencils.stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ const Template = ({ label, ...args }: Record<string, string>) => {

// Uses the shape for resize previews
createSelectionShape(bounds: Rectangle) {
const stencil = StencilShapeRegistry.getStencil(this.state.style.shape ?? '');
const stencil = StencilShapeRegistry.getStencil(this.state.style.shape);
let shape: Shape;

if (stencil) {
Expand Down