-
-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
index.ts
70 lines (68 loc) · 2.4 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import {
Point,
FabricImage,
Group,
BaseFabricObject,
type FabricObject,
type TOriginX,
type TOriginY,
} from 'fabric';
/**
* Updates the fromObject function of a class to return a version that can restore old data
* with values of originX and originY that are different from 'center', 'center'
* Used to upgrade from fabric 6 to fabric 7
* @param originalFn the original fromObject function of an object,
* @param defaultOriginX optional default value for non exported originX,
* @param defaultOriginY optional default value for non exported originY,
* @returns a wrapped fromObject function for the object
*/
export const originUpdaterWrapper = <T extends FabricObject = FabricObject>(
originalFn: (...args: any[]) => Promise<T>,
defaultOriginX: TOriginX = 'left',
defaultOriginY: TOriginY = 'top',
): ((...args: any[]) => Promise<T>) =>
async function (this: T, serializedObject, ...args) {
// we default to left and top because those are defaults before deprecation
const { originX = defaultOriginX, originY = defaultOriginY } =
serializedObject;
// and we do not want to pass those properties on the object anymore
delete serializedObject.originX;
delete serializedObject.originY;
const originalObject = await originalFn.call(
this,
serializedObject,
...args,
);
const actualPosition = new Point(originalObject.left, originalObject.top);
originalObject.setPositionByOrigin(actualPosition, originX, originY);
return originalObject;
};
/**
* Wraps and override the current fabricJS fromObject static functions
* Used to upgrade from fabric 6 to fabric 7
* @param defaultOriginX optional default value for non exported originX,
* @param defaultOriginY optional default value for non exported originY,
* @returns a wrapped fromObject function for the object
*/
export const installOriginWrapperUpdater = (
originX?: TOriginX,
originY?: TOriginY,
) => {
// @ts-expect-error the _fromObject parameter could be instantiated differently
BaseFabricObject._fromObject = originUpdaterWrapper<FabricObject>(
BaseFabricObject._fromObject,
originX,
originY,
);
// FabricImage and Group do not use _fromObject
FabricImage.fromObject = originUpdaterWrapper<FabricImage>(
FabricImage.fromObject,
originX,
originY,
);
Group.fromObject = originUpdaterWrapper<Group>(
Group.fromObject,
originX,
originY,
);
};