Improve typing of custom fabric.Object properties (#3134)

* Ensure increasing ordinals of new masks

* Add fabric.d.ts

* Revert "Ensure increasing ordinals of new masks"

This reverts commit dedfeec9ad.
This commit is contained in:
Abdo 2024-04-13 10:36:08 +03:00 committed by GitHub
parent 31439e325d
commit e486d6b513
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 12 additions and 5 deletions

12
ts/routes/image-occlusion/fabric.d.ts vendored Normal file
View file

@ -0,0 +1,12 @@
export {};
declare global {
namespace fabric {
interface Object {
id: string;
ordinal: number;
/** a custom property set on groups in the ungrouping routine to avoid adding a spurious undo entry */
destroyed: boolean;
}
}
}

View file

@ -64,12 +64,10 @@ export const groupShapes = (canvas: fabric.Canvas): void => {
const activeObject = canvas.getActiveObject() as fabric.ActiveSelection; const activeObject = canvas.getActiveObject() as fabric.ActiveSelection;
const items = activeObject.getObjects(); const items = activeObject.getObjects();
// @ts-expect-error not defined
let minOrdinal: number | undefined = Math.min(...items.map((item) => item.ordinal)); let minOrdinal: number | undefined = Math.min(...items.map((item) => item.ordinal));
minOrdinal = Number.isNaN(minOrdinal) ? undefined : minOrdinal; minOrdinal = Number.isNaN(minOrdinal) ? undefined : minOrdinal;
items.forEach((item) => { items.forEach((item) => {
// @ts-expect-error not defined
item.set({ opacity: 1, ordinal: minOrdinal }); item.set({ opacity: 1, ordinal: minOrdinal });
}); });
@ -90,13 +88,11 @@ export const unGroupShapes = (canvas: fabric.Canvas): void => {
const group = canvas.getActiveObject() as fabric.Group; const group = canvas.getActiveObject() as fabric.Group;
const items = group.getObjects(); const items = group.getObjects();
group._restoreObjectsState(); group._restoreObjectsState();
// @ts-expect-error not defined
group.destroyed = true; group.destroyed = true;
items.forEach((item) => { items.forEach((item) => {
item.set({ item.set({
opacity: get(opacityStateStore) ? 0.4 : 1, opacity: get(opacityStateStore) ? 0.4 : 1,
// @ts-expect-error not defined
ordinal: undefined, ordinal: undefined,
}); });
canvas.add(item); canvas.add(item);

View file

@ -50,7 +50,6 @@ class UndoStack {
this.canvas = canvas; this.canvas = canvas;
this.canvas.on("object:modified", (opts) => this.maybePush(opts)); this.canvas.on("object:modified", (opts) => this.maybePush(opts));
this.canvas.on("object:removed", (opts) => { this.canvas.on("object:removed", (opts) => {
// @ts-expect-error `destroyed` is a custom property set on groups in the ungrouping routine to avoid adding a spurious undo entry
if (!opts.target!.group && !opts.target!.destroyed) { if (!opts.target!.group && !opts.target!.destroyed) {
this.maybePush(opts); this.maybePush(opts);
} }