Retrospring/app/javascript/retrospring/controllers/cropper_controller.ts

50 lines
1.5 KiB
TypeScript
Raw Normal View History

2023-01-22 08:43:40 -08:00
import { Controller } from '@hotwired/stimulus';
import Croppr from 'croppr';
export default class extends Controller {
static targets = ['input', 'controls', 'cropper', 'x', 'y', 'w', 'h'];
declare readonly inputTarget: HTMLInputElement;
declare readonly controlsTarget: HTMLElement;
declare readonly cropperTarget: HTMLImageElement;
declare readonly xTarget: HTMLInputElement;
declare readonly yTarget: HTMLInputElement;
declare readonly wTarget: HTMLInputElement;
declare readonly hTarget: HTMLInputElement;
static values = {
aspectRatio: String
};
declare readonly aspectRatioValue: string;
2023-01-22 08:58:23 -08:00
readImage(file: File, callback: (string) => void): void {
2023-01-22 08:43:40 -08:00
callback((window.URL || window.webkitURL).createObjectURL(file));
}
2023-01-22 08:58:23 -08:00
updateValues(data: Record<string, string>): void {
2023-01-22 08:43:40 -08:00
this.xTarget.value = data.x;
this.yTarget.value = data.y;
this.wTarget.value = data.width;
this.hTarget.value = data.height;
}
2023-01-22 08:58:23 -08:00
change(): void {
2023-01-22 08:43:40 -08:00
this.controlsTarget.classList.toggle('d-none');
if (this.inputTarget.files && this.inputTarget.files[0]) {
this.readImage(this.inputTarget.files[0], (src) => {
this.cropperTarget.src = src;
new Croppr(this.cropperTarget, {
aspectRatio: parseFloat(this.aspectRatioValue),
startSize: [100, 100, '%'],
onCropStart: this.updateValues.bind(this),
onCropMove: this.updateValues.bind(this),
onCropEnd: this.updateValues.bind(this)
});
});
}
}
}