Another approach to preventing selected shapes from shifting (#2735)

* Revert "Fix selected shapes shifting to canvas origin"

This reverts commit d81b96fed0adb7db2fb9847c52d7cb73f83b4b32.

This introduced a regression where the selection of objects was cleared
when it should not have been. For example, if multiple objects were
selected and the mouse was released while moving them around, the
selection would be cleared.

* Another approach to preventing selected shapes from shifting

Prevent fabric objects from shifting to the wrong position when the
active selection contains multiple objects by calculating their
coordinates relative to the canvas, as in the case of the fabric.Group.
This commit is contained in:
Hikaru Y 2023-10-15 09:11:23 +09:00 committed by GitHub
parent bd55149846
commit bea4307d78
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -36,18 +36,25 @@ export function exportShapesToClozeDeletions(occludeInactive: boolean): {
*/
function baseShapesFromFabric(occludeInactive: boolean): ShapeOrShapes[] {
const canvas = globalThis.canvas as Canvas;
// Prevents multiple shapes in 'activeSelection' from shifting to the canvas origin
canvas.discardActiveObject();
makeMaskTransparent(canvas, false);
const activeObject = canvas.getActiveObject();
const selectionContainingMultipleObjects = activeObject instanceof fabric.ActiveSelection
&& (activeObject.size() > 1)
? activeObject
: null;
const objects = canvas.getObjects() as FabricObject[];
return objects
.map((object) => {
// If the object is in the active selection containing multiple objects,
// we need to calculate its x and y coordinates relative to the canvas.
const parent = selectionContainingMultipleObjects?.contains(object)
? selectionContainingMultipleObjects
: undefined;
return fabricObjectToBaseShapeOrShapes(
canvas,
object,
occludeInactive,
parent,
);
})
.filter((o): o is ShapeOrShapes => o !== null);