GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

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 #418

New issue
Copy link
New issue
Copy link
Open
Bug
Open
Redefinition of prototypes introduce side effects in Stories#418
Bug
Copy link
Assignees
alxveres
Labels
good first issueGood for newcomershelp wantedExtra attention is needed
Milestone
0.26.0

Description

@tbouffard
tbouffard
opened on May 25, 2024
Issue body actions

Describe 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:

  1. Go to the 'PerimeterLabelBounds' story
  2. Go to the 'SecondLabels' story, a second label is displayed on top of the rectangle, displaying the id of the cell
  3. Go back to the 'PerimeterLabelBounds' story
  4. The second label is now displayed in the first story

Expected behavior

The specific behavior of a story must not impact other stories.

Screenshots

Impact_of_usage_of_prototype_in_stories

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

  • directly set value on instance: feat: allow to drag edge in the Toolbar story #416
  • override class: feat: add a story to demonstrate all Perimeter features #317. In particular, the Graph class includes factory methods that can be used to use custom implementation : feat: restore remaining non-"Editor" examples to Storybook #150
  • define function and set it directly on instances: fix(stories): correctly set custom style for collapsed swimlanes #411

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

Activity

  1. tbouffard
    added
    bugSomething isn't working
    triageIssue/PR under review for categorization
    and removed
    triageIssue/PR under review for categorization
    on May 25, 2024
  2. tbouffard
    added a parent issue on Jan 20, 2025
    • Ensure that the stories work as the mxGraph examples #505
  3. tbouffard
    changed the title [-][BUG] Redefinition of prototypes introduce side effects in Stories[/-] [+]Redefinition of prototypes introduce side effects in Stories[/+] on Feb 14, 2025
  4. tbouffard
    removed
    bugSomething isn't working
    on May 15, 2025
  5. tbouffard
    added
    good first issueGood for newcomers
    help wantedExtra attention is needed
    on Jul 30, 2025
  6. redfish4ktc commented on Sep 29, 2026

    @redfish4ktc
    redfish4ktc
    on Sep 29, 2026
    Member
    More actions

    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:

    Story Patch Field declared at Default that actually applies
    Guides.stories.ts:73 EdgeHandler.prototype.snapToTerminals = true EdgeHandler.ts:184 false
    Markers.stories.ts:46 SelectionHandler.prototype.guidesEnabled = true SelectionHandler.ts:212 false
    Markers.stories.ts:47 EdgeHandler.prototype.snapToTerminals = true EdgeHandler.ts:184 false
    Stencils.stories.ts:84 ConnectionHandler.prototype.outlineConnect = true ConnectionHandler.ts:368 false
    Stencils.stories.ts:85 EdgeHandler.prototype.manageLabelHandle = true EdgeHandler.ts:213 false
    Stencils.stories.ts:86 EdgeHandler.prototype.outlineConnect = true EdgeHandler.ts:206 false
    Stencils.stories.ts:87 CellHighlight.prototype.keepOnTop = true CellHighlight.ts:58 false
    DynamicLoading.stories.ts:48 TextShape.prototype.useSvgBoundingBox = false Shape.ts:276, on the base class true

    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.

  7. redfish4ktc
    marked this as blocked by 2 issues on Sep 29, 2026
    • Add a global configuration entry for EdgeHandler.snapToTerminals #1200
    • Add a global configuration entry for CellHighlight.keepOnTop #1201
  8. alxveres commented on Oct 1, 2026

    @alxveres
    alxveres
    on Oct 1, 2026
    More actions

    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?

  9. tbouffard
    assigned
    alxveres
    on Oct 1, 2026
  10. tbouffard commented on Oct 1, 2026

    @tbouffard
    tbouffard
    on Oct 1, 2026
    MemberAuthor
    More actions

    Hi @alxveres and thank you for offering to contribute to maxGraph 👍🏿

    You are now assigned to this issue.

    Reacted by Alex Vereš
  11. alxveres commented on Oct 1, 2026

    @alxveres
    alxveres
    on Oct 1, 2026
    More actions

    Thanks for assigning me @tbouffard! I'm planning to split this work into several small PRs, grouped by the fix they need:

    1. Custom shapes: SecondLabel (the example from the issue), FixedIcons and DynamicLoading.
    2. Per-instance fixes: Markers (guidesEnabled), Stencils (ConnectionHandler.outlineConnect) and Toolbar (DragSource.getDropTarget).
    3. EdgeHandler flags in Stencils (outlineConnect, manageLabelHandle), using a subclass installed through the SelectionCellsHandler factory.
    4. Groups: it overrides several Graph, SelectionHandler and PopupMenuHandler methods.
    5. 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?

  12. redfish4ktc commented on Oct 2, 2026

    @redfish4ktc
    redfish4ktc
    on Oct 2, 2026
    Member
    More actions

    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!

  13. redfish4ktc
    added this to the 0.26.0 milestone on Oct 2, 2026
  14. alxveres commented on Oct 5, 2026

    @alxveres
    alxveres
    on Oct 5, 2026
    More actions

    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.

  15. alxveres commented on Oct 5, 2026

    @alxveres
    alxveres
    on Oct 5, 2026
    More actions

    I have opened the PR: #1214

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

  • @alxveres
    alxveres

Labels

good first issueGood for newcomershelp wantedExtra attention is needed

Type

Bug

Projects

No projects

    Milestone

    • 0.26.0No due date

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

    You are visitor 01402775 — GeezerVison does not store the pages you look up.
    Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.