Can't restart autolayout on user input


I try to start an autoLayout on user button click
(The diagram needs to be editable without gojs interferring, but sometimes we need to use autolayout to reorder nodes)

    schema.startTransaction("auto layout");
    schema.layout = applyLayout();
    schema.layout = new go.Layout();
    schema.commitTransaction("auto layout");

schema is an instance of go.Diagram
autolayout is a go.LayeredDigraphLayout

Do I need to reset all height / width / pos values to NaN ?

If you only want to perform a layout upon a command or other programmatic decision, it’s commonplace to do it this way:

const myDiagram = new go.Diagram(..., {
    layout: new go.LayeredDigraphLayout({
        isInitial: false,  // don't invalidate layout when Diagram.model is replaced
        isOngoing: false,  // don't invalidate layout when Nodes or Links are added
                           // or removed or change visibility or Nodes change size
        . . . // properties specific to the layout
    . . .  // properties for initializing the diagram

Basically, if the layout is never automatically invalidated, it will never run. Until your code explicitly invalidates a layout or calls Diagram.layoutDiagram with a true argument.

So you don’t need the code that you quoted, except for the call to schema.layoutDiagram(true);

Except for the base Layout class, which never modifies any Parts except those that do not have a real location, all layouts should position all of the nodes that they are responsible for positioning. So I don’t think you need to reset any size or position.

I already tried that but it’s not working in my case (without knowing why)

I start with a broken save of a diagram (it’s intended for test purpose)

And running my script, or only the layout with your modification or my script with your modification doesn’t re-layout the schema

Diagram.layoutDiagram with a true argument will explicitly invalidate all layouts in the diagram (both Group.layout and the Diagram.layout) and then perform all layouts.

I agree, and it is working on some diagram but not in this one

Link are repositionned but not the nodes…

I should clarify that just calling Diagram.layoutDiagram should replace the code that you quoted.

What’s different about diagrams when it fails from when it works?

Not working ones have binded sizes and position