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
1 change: 1 addition & 0 deletions code/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- [#291](https://github.com/InditexTech/weavejs/issues/291) Group of elements moving not maintaining position (all layers)
- [#300](https://github.com/InditexTech/weavejs/issues/300) Un-grouping elements are unordered
- [#310](https://github.com/InditexTech/weavejs/issues/310) Fix zoom in / out stepping before fitting

## [0.19.0] - 2025-05-28

Expand Down
10 changes: 10 additions & 0 deletions code/packages/sdk/src/plugins/stage-zoom/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,14 @@
//
// SPDX-License-Identifier: Apache-2.0

import type { WeaveStageZoomPluginConfig } from './types';

export const WEAVE_STAGE_ZOOM_KEY = 'stageZoom';

export const WEAVE_STAGE_ZOOM_DEFAULT_CONFIG: WeaveStageZoomPluginConfig = {
zoomSteps: [
0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1, 1.25, 1.5, 1.75, 2, 3, 4, 6,
8, 10,
],
defaultZoom: 1,
};
37 changes: 20 additions & 17 deletions code/packages/sdk/src/plugins/stage-zoom/stage-zoom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ import {
} from './types';
import Konva from 'konva';
import { WeaveNodesSelectionPlugin } from '../nodes-selection/nodes-selection';
import { WEAVE_STAGE_ZOOM_KEY } from './constants';
import {
WEAVE_STAGE_ZOOM_DEFAULT_CONFIG,
WEAVE_STAGE_ZOOM_KEY,
} from './constants';

export class WeaveStageZoomPlugin extends WeavePlugin {
private isCtrlOrMetaPressed: boolean;
Expand All @@ -30,8 +33,7 @@ export class WeaveStageZoomPlugin extends WeavePlugin {
const { config } = params ?? {};

this.config = {
zoomSteps: [0.1, 0.25, 0.5, 1, 2, 4, 8],
defaultZoom: 1,
...WEAVE_STAGE_ZOOM_DEFAULT_CONFIG,
...config,
};

Expand Down Expand Up @@ -122,8 +124,9 @@ export class WeaveStageZoomPlugin extends WeavePlugin {
const actualZoomIsStep = this.config.zoomSteps.findIndex(
(scale) => scale === this.actualScale
);

if (actualZoomIsStep === -1) {
this.actualStep = this.findClosestStepIndex();
this.actualStep = this.findClosestStepIndex('zoomOut');
}

return this.actualStep - 1 > 0;
Expand All @@ -138,7 +141,7 @@ export class WeaveStageZoomPlugin extends WeavePlugin {
(scale) => scale === this.actualScale
);
if (actualZoomIsStep === -1) {
this.actualStep = this.findClosestStepIndex();
this.actualStep = this.findClosestStepIndex('zoomIn');
}

return this.actualStep + 1 < this.config.zoomSteps.length;
Expand All @@ -157,16 +160,16 @@ export class WeaveStageZoomPlugin extends WeavePlugin {
this.setZoom(this.config.zoomSteps[step]);
}

private findClosestStepIndex() {
let closestStepIndex = 0;
let actualDiff = Infinity;
for (let i = 0; i < this.config.zoomSteps.length; i++) {
if (Math.abs(this.config.zoomSteps[i] - this.actualScale) < actualDiff) {
closestStepIndex = i;
actualDiff = Math.abs(this.config.zoomSteps[i] - this.actualScale);
}
}
return closestStepIndex;
private findClosestStepIndex(direction: 'zoomIn' | 'zoomOut'): number {
const nextValue = this.config.zoomSteps
.filter((scale) =>
direction === 'zoomIn'
? scale >= this.actualScale
: scale <= this.actualScale
)
.sort((a, b) => (direction === 'zoomIn' ? a - b : b - a))[0];

return this.config.zoomSteps.findIndex((scale) => scale === nextValue);
}

zoomIn(): void {
Expand All @@ -182,7 +185,7 @@ export class WeaveStageZoomPlugin extends WeavePlugin {
(scale) => scale === this.actualScale
);
if (actualZoomIsStep === -1) {
this.actualStep = this.findClosestStepIndex();
this.actualStep = this.findClosestStepIndex('zoomIn');
} else {
this.actualStep += 1;
}
Expand All @@ -203,7 +206,7 @@ export class WeaveStageZoomPlugin extends WeavePlugin {
(scale) => scale === this.actualScale
);
if (actualZoomIsStep === -1) {
this.actualStep = this.findClosestStepIndex();
this.actualStep = this.findClosestStepIndex('zoomOut');
} else {
this.actualStep -= 1;
}
Expand Down
1 change: 1 addition & 0 deletions docs/content/docs/main/changelog/prerelease/0.20.0.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ description: Selection & grouping bugfixes

- [#291](https://github.com/InditexTech/weavejs/issues/291) Group of elements moving not maintaining position (all layers)
- [#300](https://github.com/InditexTech/weavejs/issues/300) Un-grouping elements are unordered
- [#310](https://github.com/InditexTech/weavejs/issues/310) Fix zoom in / out stepping before fitting
3 changes: 2 additions & 1 deletion docs/content/docs/sdk/api-reference/plugins/stage-zoom.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ For `WeaveStageZoomPluginConfig`:
["zoomSteps"]: {
description: "An array that defines the zooming steps of the plugin.",
type: "number[]",
default: "[0.1, 0.25, 0.5, 1, 2, 4, 8]",
default:
"[ 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1, 1.25, 1.5, 1.75, 2, 3, 4, 6, 8, 10]",
},
["defaultZoom"]: {
description: "The default zoom when loading the stage (canvas).",
Expand Down