Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(Activeselection): Activeselection default initialization #9940

Merged
merged 2 commits into from
Jun 25, 2024
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
27 changes: 18 additions & 9 deletions .codesandbox/templates/vanilla/src/testcases/clearCache.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
import * as fabric from 'fabric';

export async function testCase(canvas: fabric.Canvas) {
const utahCommonDefaults = {
originX: 'center' as const,
originY: 'center' as const,
};

fabric.ActiveSelection.ownDefaults = {
...fabric.ActiveSelection.ownDefaults,
...utahCommonDefaults,
};

fabric.Rect.ownDefaults = {
...fabric.Rect.ownDefaults,
...utahCommonDefaults,
};

const rect = new fabric.Rect({ width: 500, height: 500, fill: 'blue' });
rect.on('moving', function (opt) {
if (this.fill === 'blue') {
this.fill = 'red';
} else {
this.fill = 'blue';
}
this.dirty = true;
});
canvas.add(rect);
const rect2 = new fabric.Rect({ width: 500, height: 500, fill: 'blue' });

canvas.add(rect, rect2);
}
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## [next]

- fix(Activeselection): Activeselection default initialization [#9940](https://github.com/fabricjs/fabric.js/pull/9940)

## [6.0.0-rc3]

- fix(StaticCanvas): fully clean the cache canvas to avoid leaving trailing pixels [#9779](https://github.com/fabricjs/fabric.js/pull/9779)
Expand Down
12 changes: 8 additions & 4 deletions src/shapes/ActiveSelection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,17 @@ export class ActiveSelection extends Group {

constructor(
objects: FabricObject[] = [],
{ layoutManager, ...options }: Partial<ActiveSelectionOptions> = {}
options: Partial<ActiveSelectionOptions> = {}
) {
super(objects, {
layoutManager: layoutManager ?? new ActiveSelectionLayoutManager(),
});
super();
Object.assign(this, ActiveSelection.ownDefaults);
this.setOptions(options);
const { left, top, layoutManager } = options;
this.groupInit(objects, {
left,
top,
layoutManager: layoutManager ?? new ActiveSelectionLayoutManager(),
});
}

/**
Expand Down
17 changes: 16 additions & 1 deletion src/shapes/Group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,21 @@ export class Group
super();
Object.assign(this, Group.ownDefaults);
this.setOptions(options);
this.groupInit(objects, options);
}

/**
* Shared code between group and active selection
* Meant to be used by the constructor.
*/
protected groupInit(
objects: FabricObject[],
options: {
layoutManager?: LayoutManager;
top?: number;
left?: number;
}
) {
this._objects = [...objects]; // Avoid unwanted mutations of Collection to affect the caller

this.__objectSelectionTracker = this.__objectSelectionMonitor.bind(
Expand All @@ -156,7 +171,7 @@ export class Group
});

// perform initial layout
this.layoutManager = options.layoutManager || new LayoutManager();
this.layoutManager = options.layoutManager ?? new LayoutManager();
this.layoutManager.performLayout({
type: LAYOUT_TYPE_INITIALIZATION,
target: this,
Expand Down
Loading