GeezerVison is showing github.com — Redefinition of prototypes introduce side effects in Stories · Issue #418 · maxGraph/maxGraph
Redefinition of prototypes introduce side effects in Stories #418DescriptionDescribe the bug
There are several stories that redefine the prototype of maxGraph classes to change their default behavior.
However, after navigating to such a story, the behavior of such story is applied to any story visited afterwards.
To Reproduce
Steps to reproduce the behavior:
- Go to the 'PerimeterLabelBounds' story
- Go to the 'SecondLabels' story, a second label is displayed on top of the rectangle, displaying the id of the cell
- Go back to the 'PerimeterLabelBounds' story
- The second label is now displayed in the first story
Expected behavior
The specific behavior of a story must not impact other stories.
Screenshots

Environment
The problem is present in all environment
Additional context
The code of the stories originally comes from mxGraph examples which where provided as independent pages. So, the changes of the prototype only the page itself.
The stories in maxGraph are run in a Single Page Application, so any change of prototypes impact the rest of the application, so the other stories.
This problem was mentioned long ago in #1 (comment)
Alternatives to the usage of prototype
Most of the time, there are alternatives to prototype change, for example
There are currently some existing limitations to the alternatives, for example when dealing with VertexHandler: #83. So, changes should done in the maxGraph code to provide working alternatives
Possible fix for the SecondLabel story
Instead of directly modifying the prototype of RectangleShape, a new class can be created to override the methods.
Then, the default style can be updated to use this shape instead of the RectangleShape (it may be required to register the shape in CellRenderer with a dedicated name to use it in the default style configuration).
https://github.com/maxGraph/maxGraph/blob/v0.10.2/packages/html/stories/SecondLabel.stories.js#L55-L75 Activityadded bugSomething isn't working triageIssue/PR under review for categorization and removed triageIssue/PR under review for categorization on May 25, 2024changed the title [-][BUG] Redefinition of prototypes introduce side effects in Stories[/-] [+]Redefinition of prototypes introduce side effects in Stories[/+] on Feb 14, 2025redfish4ktc commented on Sep 29, 2026 There is a second, silent half to this problem, found while auditing the stories for the extending guide (#1207).
A prototype patch works on a method, since a method lives on the prototype and nothing shadows it. That is the case reported here, and it is why the patched behavior follows the reader from one story to the next. A patch is dead on a field declared with an initializer: the constructor installs an own property on every instance, which shadows the prototype value. TypeScript emits this.snapToTerminals = false in the constructor whichever way useDefineForClassFields is set, so the instance always wins and the assignment on the prototype is never read.
Eight patches in the stories are in that second group. Each one is a no-op, so the story silently demonstrates the default behavior instead of the one it announces:
A ninth, PolylineShape.prototype.constraints = null in Anchors.stories.ts:110, is already commented out.
Why this belongs here: whoever removes the prototype patches will walk the same lines in the same files. Fixing only the ones that leak would leave these eight in place, still announcing a behavior they never produced.
The fixes, which are not symmetrical
The three alternatives listed in the description all work, but which one applies depends on whether the story can reach the instance.
Per instance assignment, when the owner is a plugin the story already has:
graph.getPlugin<SelectionHandler>('SelectionHandler')!.guidesEnabled = true;
graph.getPlugin<ConnectionHandler>('ConnectionHandler')!.outlineConnect = true;
A subclass installed through the handler factory, for EdgeHandler, which is built per selected cell. SelectionCellsHandler exposes setEdgeHandlerFactory, setEdgeHandlerFactoryForAllKinds and setVertexHandlerFactory for that. One caveat: a subclass field initializer runs after the base constructor has finished, so this covers values read during interaction, not values the base constructor itself consumes. snapToTerminals is read in isSnapToTerminalsEvent on mouse moves, so it is fine.
class SnappingEdgeHandler extends EdgeHandler {
snapToTerminals = true;
outlineConnect = true;
manageLabelHandle = true;
}
graph.getPlugin<SelectionCellsHandler>('SelectionCellsHandler')!
.setEdgeHandlerFactoryForAllKinds((state) => new SnappingEdgeHandler(state));
The same applies to the shape case, shapes being instantiated from the registry:
class FastTextShape extends TextShape {
useSvgBoundingBox = false;
}
ShapeRegistry.add('text', FastTextShape);
A global configuration entry, which is the direction the project already took for handler flags, read at the use site rather than copied into a field, as VertexHandlerConfig.rotationEnabled is. None of the six patched flags has one today: grepping every config object of packages/core/src returns no hit for snapToTerminals, outlineConnect, manageLabelHandle, keepOnTop, guidesEnabled or useSvgBoundingBox.
Per patch, the fix I would pick:
| Patch |
Fix |
SelectionHandler.guidesEnabled, ConnectionHandler.outlineConnect |
instance assignment, the plugin is directly reachable |
EdgeHandler.snapToTerminals |
a EdgeHandlerConfig entry in core, it is a genuine global preference, with the factory subclass in the stories until that lands |
EdgeHandler.outlineConnect, EdgeHandler.manageLabelHandle |
the factory subclass, both being specific to the stencil demo |
CellHighlight.keepOnTop |
a configuration entry, CellHighlight being created deep inside the markers and not reachable from a story |
TextShape.useSvgBoundingBox |
a subclass registered in the ShapeRegistry, the flag being a per shape rendering trade off |
So two of the eight need a core addition before the story can be fixed at all.
Unrelated to the patches but met in the same audit, two stories read a value off a prototype where it does not exist and silently get undefined: Shape.stories.js sets spacingTop and spacingRight from BoxShape.prototype.extrude, an instance field, and Tree.stories.js reads this.segment where the field is declared static. Those are plain coding errors rather than patching, so they deserve their own issue. alxveres commented on Oct 1, 2026 Hi, I'd like to work on this issue. I've reproduced the problem locally and reviewed the relevant code. Could you assign it to me? tbouffard commented on Oct 1, 2026 Hi @alxveres and thank you for offering to contribute to maxGraph 👍🏿
You are now assigned to this issue. alxveres commented on Oct 1, 2026 Thanks for assigning me @tbouffard! I'm planning to split this work into several small PRs, grouped by the fix they need:
- Custom shapes: SecondLabel (the example from the issue), FixedIcons and DynamicLoading.
- Per-instance fixes: Markers (guidesEnabled), Stencils (ConnectionHandler.outlineConnect) and Toolbar (DragSource.getDropTarget).
- EdgeHandler flags in Stencils (outlineConnect, manageLabelHandle), using a subclass installed through the SelectionCellsHandler factory.
- Groups: it overrides several Graph, SelectionHandler and PopupMenuHandler methods.
- Grid: it wraps GraphView methods again on every visit.
snapToTerminals (Guides, Markers) and keepOnTop (Stencils) will follow once #1200 and #1201 land.
Each PR will reference this issue, and the last one will close it. I'm starting with SecondLabel. Does this split work for you? redfish4ktc commented on Oct 2, 2026 Thanks for sharing your plan! The split makes a lot of sense, and I fully agree with it. Grouping the stories by the kind of fix they need will make the reviews easier.
To be complete, please also have a look at the Anchors story mentioned in #418 (comment). In the original mxGraph example, the prototype of mxPolyline was changed to remove connection points on edges (see anchors.html):
// Edges have no connection points
mxPolyline.prototype.constraints = null;
In the maxGraph story, the equivalent line is commented out (see Anchors.stories.ts). So this point should be analyzed too: maybe it fits in one of your existing groups, or it needs its own PR.
A few practical things before you start.
Due to moderation limits, you won't be able to have more than 3 open PRs on the repository at the same time. I'll do my best to review your PRs in a reasonable time. But I prefer to avoid external contributions piling up: it's frustrating for everyone, for you as a contributor and for me as a reviewer (see this GitHub blog post about it).
My suggestion: start with a single PR. As it's your first contribution, we can use it to make sure all the guidelines are followed. The next ones should then go much faster. SecondLabel looks like a good candidate for that, since it's the example already described in the issue.
If you have any questions, feel free to post them in the Development category of GitHub Discussions.
Looking forward to your first PR! alxveres commented on Oct 5, 2026 Thanks, that sounds good. I'll start with SecondLabel only and wait for your review before opening the next one. I will also look at the Anchors story. alxveres commented on Oct 5, 2026 I have opened the PR: #1214
MetadataMetadataDevelopmentNo branches or pull requestsIssue actions
|