Skip to content

Commit

Permalink
Strengthen type of Page > addImage() to indicate that options are c…
Browse files Browse the repository at this point in the history
…opied to the `Object`
  • Loading branch information
pihart authored and cjquines committed Jun 30, 2021
1 parent 3f1db1d commit 842b00b
Showing 1 changed file with 18 additions and 5 deletions.
23 changes: 18 additions & 5 deletions src/lib/page.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { fabric } from "fabric";
import AssertType from "../types/assert";
import { FabricObject, isFabricCollection, ObjectId } from "../types/fabric";

export type Cursor = { x: number; y: number };
Expand Down Expand Up @@ -103,16 +104,28 @@ export default class Page extends fabric.Canvas {
});
});

addImage = async (
/**
* Create a Fabric Image from {@param imageURL},
* placed at the location defined by {@param cursor},
* with custom options given by {@param options}.
*
* @warn Make sure that {@param options} does not contain enough properties to satisfy {@link isFabricCollection}
*/
addImage = async <T extends fabric.IImageOptions>(
imageURL: string,
cursor?: Partial<Cursor>,
options?: fabric.IImageOptions
): Promise<fabric.Image> =>
new Promise<fabric.Image>((resolve) =>
options?: T
): Promise<fabric.Image & (typeof options extends undefined ? unknown : T)> =>
new Promise((resolve) =>
fabric.Image.fromURL(
imageURL,
(obj) => {
resolve(this.placeObject(obj, cursor)[0]);
AssertType<typeof options extends undefined ? unknown : T>(obj);
// We are confident that we don't need this return value because we should get the original image back
// Technically not true because `options` might be so large that it makes `obj` pass `isFabricCollection`
// so we just warn against it
this.placeObject(obj, cursor);
resolve(obj);
},
options
)
Expand Down

0 comments on commit 842b00b

Please sign in to comment.