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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ All notable changes to the Reactodia will be documented in this document.
The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]
#### 🐛 Fixed
- Fix incorrect canvas viewport position when `zoomToFit()` or similar operation is called immediately after element position changes;

## [0.31.0] - 2025-11-15
#### 🚀 New Features
Expand Down
1 change: 0 additions & 1 deletion examples/stressTest.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ function StressTestExample() {

const canvas = view.findAnyCanvas();
if (canvas) {
canvas.renderingState.syncUpdate();
await canvas.zoomToFit();
}
}
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@reactodia/workspace",
"version": "0.31.0",
"version": "0.31.1-dev",
"description": "Reactodia Workspace -- library for visual interaction with graphs in a form of a diagram.",
"repository": {
"type": "git",
Expand Down
31 changes: 19 additions & 12 deletions src/diagram/paperArea.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -689,34 +689,40 @@ export class PaperArea extends React.Component<PaperAreaProps, State> implements
}

centerTo(paperPosition?: Vector, options: CenterToOptions = {}): Promise<void> {
this.renderingState.updateLayersUpTo(RenderingLayer.PaperArea);
const {width, height} = this.state.transform;
const paperCenter = paperPosition || {x: width / 2, y: height / 2};
return this.centerToPosition(paperCenter, options);
}

centerContent(options: ViewportOptions = {}): Promise<void> {
this.renderingState.updateLayersUpTo(RenderingLayer.PaperArea);
const bbox = this.getContentFittingBox();
return this.centerTo({
x: bbox.x + bbox.width / 2,
y: bbox.y + bbox.height / 2,
}, options);
}

private centerToPosition(paperPosition: Vector, options: CenterToOptions) {
if (typeof options.scale === 'number') {
const {min, max} = this.zoomOptions;
let scale = options.scale;
scale = Math.max(scale, min);
scale = Math.min(scale, max);
const viewportState: Partial<ViewportState> = {
center: paperCenter,
center: paperPosition,
scale: {x: scale, y: scale},
};
return this.setViewportState(viewportState, options);
} else {
const viewportState: Partial<ViewportState> = {
center: paperCenter,
center: paperPosition,
};
return this.setViewportState(viewportState, options);
}
}

centerContent(options: ViewportOptions = {}): Promise<void> {
const bbox = this.getContentFittingBox();
return this.centerTo({
x: bbox.x + bbox.width / 2,
y: bbox.y + bbox.height / 2,
}, options);
}

getScale() {
return this.state.transform.scale;
}
Expand Down Expand Up @@ -766,11 +772,12 @@ export class PaperArea extends React.Component<PaperAreaProps, State> implements

zoomToFit(options: ViewportOptions = {}): Promise<void> {
const {model, renderingState} = this.props;
const {elements} = model;
const {elements, links} = model;
if (elements.length === 0) {
return this.centerTo();
}
const bbox = getContentFittingBox(elements, [], renderingState);
this.renderingState.updateLayersUpTo(RenderingLayer.PaperArea);
const bbox = getContentFittingBox(elements, links, renderingState);
return this.zoomToFitRect(bbox, options);
}

Expand Down
16 changes: 9 additions & 7 deletions src/diagram/renderingState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,14 +92,14 @@ export enum RenderingLayer {
* Layer to measure rendered elements to get their sizes.
*/
ElementSize,
/**
* Layer to adjust scrollable area for the underlying canvas.
*/
PaperArea,
/**
* Layer to route links (compute link geometry).
*/
LinkRoutes,
/**
* Layer to adjust scrollable area for the underlying canvas.
*/
PaperArea,
/**
* Layer to render link templates.
*/
Expand Down Expand Up @@ -278,7 +278,7 @@ export class MutableRenderingState implements RenderingState {

syncUpdate() {
this.layerUpdater.dispose();
this.runLayerUpdate();
this.updateLayersUpTo(LAST_LAYER);
}

scheduleOnLayerUpdate(layer: RenderingLayer, callback: () => void): void {
Expand All @@ -293,9 +293,11 @@ export class MutableRenderingState implements RenderingState {
}
}

private runLayerUpdate = () => {
private runLayerUpdate = () => this.updateLayersUpTo(LAST_LAYER);

updateLayersUpTo(lastLayer: RenderingLayer): void {
const toRun = new Set<() => void>();
for (let layer = FIRST_LAYER; layer <= LAST_LAYER; layer++) {
for (let layer = FIRST_LAYER; layer <= lastLayer; layer++) {
const callbackSet = this.scheduledByLayer.get(layer);
if (callbackSet && callbackSet.size > 0) {
for (const callback of callbackSet) {
Expand Down