Skip to content

Bug: getChildrenByPosition wrong order #554

@elsassph

Description

@elsassph

Problem/Opportunity

getChildrenByPosition may return wrong results, due to inconsistent _updateTreeOrder tagging.

Note: we don't (and won't) use zIndex and only use "natural" elements order.

Steps to reproduce

TBD. This requires a complex app tree structure which I couldn't reproduce outside of our application.

Expected Behavior

  • When using stage.getChildrenByPosition, elements are collected using ElementCore.collectAtCoord,
  • Inside this collection function, the results are sorted (at every step of the recursion!), as a flat list, using ElementCore.sortZIndexedChildren,
  • This sorting function sorts based on zIndex first, then _updateTreeOrder in case of identical zIndex,
  • We expect that collected elements are effectively in visible order.

Actual Behavior

  • _updateTreeOrder isn't always correct (elements on top may have a smaller order index than elements behind), which causes collectAtCoord to sometimes return elements in the wrong order,
  • even calling explicitly ElementCore.updateTreeOrder before collecting isn't sufficient,
  • reason may be that both functions (collectAtCoord and updateTreeOrder) do not have identical conditions for skipping/considering elements.

Notes (Optional)

We managed to work around the issue by force re-indexing the elements:

function updateTreeOrder(core: IElementCoreInternal, order: number = 0): number {
  core._updateTreeOrder = order++;
  if (core._children) {
    core._children.forEach((child) => {
      order = updateTreeOrder(child, order);
    });
  }
  return order;
}

Invoked as:

updateTreeOrder(stage.root.core);

Just before calling stage.getChildrenByPosition()

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions