2021-12-01 02:14:25 +00:00
|
|
|
// Copyright 2021 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
import { fabric } from 'fabric';
|
2024-11-04 23:25:02 +00:00
|
|
|
import { loadImage } from '../util/loadImage';
|
2021-12-01 02:14:25 +00:00
|
|
|
import { customFabricObjectControls } from './util/customFabricObjectControls';
|
|
|
|
|
|
|
|
export class MediaEditorFabricSticker extends fabric.Image {
|
|
|
|
constructor(
|
|
|
|
element: string | HTMLImageElement | HTMLVideoElement,
|
|
|
|
options: fabric.IImageOptions = {}
|
|
|
|
) {
|
|
|
|
// Fabric seems to have issues when passed a string, but not an Image.
|
|
|
|
let normalizedElement: undefined | HTMLImageElement | HTMLVideoElement;
|
|
|
|
if (typeof element === 'string') {
|
|
|
|
normalizedElement = new Image();
|
|
|
|
normalizedElement.src = element;
|
|
|
|
} else {
|
|
|
|
normalizedElement = element;
|
|
|
|
}
|
|
|
|
|
|
|
|
super(normalizedElement, options);
|
2021-12-07 15:56:02 +00:00
|
|
|
|
|
|
|
this.on('modified', () => this.canvas?.bringToFront(this));
|
2021-12-01 02:14:25 +00:00
|
|
|
}
|
|
|
|
|
2024-11-04 23:25:02 +00:00
|
|
|
static async fromObject(
|
2021-12-01 02:14:25 +00:00
|
|
|
// eslint-disable-next-line max-len
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types
|
|
|
|
options: any,
|
2024-11-04 23:25:02 +00:00
|
|
|
callback: (_: MediaEditorFabricSticker | null, isError: boolean) => unknown
|
|
|
|
): Promise<void> {
|
|
|
|
let { src } = options;
|
|
|
|
try {
|
|
|
|
if (typeof src === 'string') {
|
|
|
|
src = await loadImage(src);
|
|
|
|
}
|
|
|
|
callback(new MediaEditorFabricSticker(src, options), false);
|
|
|
|
} catch {
|
|
|
|
callback(null, true);
|
|
|
|
}
|
2021-12-01 02:14:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
MediaEditorFabricSticker.prototype.type = 'MediaEditorFabricSticker';
|
|
|
|
MediaEditorFabricSticker.prototype.borderColor = '#ffffff';
|
|
|
|
MediaEditorFabricSticker.prototype.controls = customFabricObjectControls;
|