Merge branch 'feat/screenshot-forma' into 'mod/presupuesto_admin'

add downloadPreviewImage function to download image as png from the current shape selected

See merge request jjimenez/safekat!468
This commit is contained in:
2025-01-09 17:47:58 +00:00
2 changed files with 157 additions and 99 deletions

View File

@ -67,7 +67,7 @@ class PresupuestoAdminEdit {
this.datosLibro.init();
this.comparador.init();
this.lineasPresupuesto.init();
this.previewFormasAdmin = new PreviewFormasAdmin(tipoLibro, this.tipoTapa,
{
ancho: () => this.getDimensionLibro().ancho,
@ -93,6 +93,7 @@ class PresupuestoAdminEdit {
sessionStorage.removeItem('message');
}
}
this.downloadPreviewImage();
}
#cargarPresupuesto() {
@ -257,6 +258,37 @@ class PresupuestoAdminEdit {
showBreadCrumbSaveButton(true);
});
}
downloadPreviewImage() {
$(document).on("click", ".download-shape", (event) => {
let parentDiv = $(event.currentTarget).parent().parent();
let shapeSvgElQuery = $(parentDiv).find("svg");
let shapeSvgEl = shapeSvgElQuery[0];
const filename = shapeSvgElQuery.parent().attr("id") ?? "image"
const serializer = new XMLSerializer();
const svgData = serializer.serializeToString(shapeSvgEl);
const svgBlob = new Blob([svgData], { type: 'image/svg+xml;charset=utf-8' });
const svgUrl = URL.createObjectURL(svgBlob);
const img = new Image();
img.onload = () => {
const canvas = document.createElement('canvas');
canvas.width = img.width;
canvas.height = img.height;
const ctx = canvas.getContext('2d');
ctx.drawImage(img, 0, 0);
const pngUrl = canvas.toDataURL('image/png');
const downloadLink = document.createElement('a');
downloadLink.href = pngUrl;
downloadLink.download = filename ?? '.png';
document.body.appendChild(downloadLink);
downloadLink.click();
document.body.removeChild(downloadLink);
URL.revokeObjectURL(svgUrl);
};
img.src = svgUrl;
})
}
}