2021-11-12 01:49:23 +01:00
|
|
|
var canvasEditions = [];
|
2022-01-05 02:18:36 +01:00
|
|
|
var fontCaveat = null;
|
|
|
|
var copiedObject = null;
|
|
|
|
var forceAddLock = true;
|
|
|
|
var addLock = true;
|
|
|
|
var activeCanvas = null;
|
|
|
|
var activeCanvasPointer = null;
|
|
|
|
var pdfRenderTasks = [];
|
|
|
|
var pdfPages = [];
|
|
|
|
var svgCollections = [];
|
|
|
|
var resizeTimeout;
|
|
|
|
var pdfHistory = {};
|
|
|
|
var currentScale = 1.5;
|
|
|
|
var windowWidth = window.innerWidth;
|
|
|
|
var menu = null;
|
|
|
|
var menuOffcanvas = null;
|
|
|
|
var currentCursor = null;
|
2022-01-05 02:25:39 +01:00
|
|
|
var signaturePad = null;
|
2022-04-15 00:47:36 +02:00
|
|
|
var nblayers = null;
|
2022-01-05 02:18:36 +01:00
|
|
|
|
2022-03-26 11:02:47 +01:00
|
|
|
var loadPDF = async function(pdfBlob, filename) {
|
2022-04-15 00:47:36 +02:00
|
|
|
const pdfjsLib = window['pdfjs-dist/build/pdf'];
|
2022-01-05 02:18:36 +01:00
|
|
|
pdfjsLib.GlobalWorkerOptions.workerSrc = '/vendor/pdf.worker.js?legacy';
|
|
|
|
|
2022-03-26 11:02:47 +01:00
|
|
|
let url = await URL.createObjectURL(pdfBlob);
|
2022-01-05 02:18:36 +01:00
|
|
|
|
2022-04-14 09:09:01 +02:00
|
|
|
let text_document_name = document.querySelector('#text_document_name');
|
2022-04-01 00:32:52 +02:00
|
|
|
text_document_name.querySelector('span').innerText = filename;
|
|
|
|
text_document_name.setAttribute('title', filename);
|
|
|
|
|
2022-03-28 00:57:30 +02:00
|
|
|
let dataTransfer = new DataTransfer();
|
2022-01-05 02:18:36 +01:00
|
|
|
dataTransfer.items.add(new File([pdfBlob], filename, {
|
|
|
|
type: 'application/pdf'
|
|
|
|
}));
|
2022-03-31 13:29:38 +02:00
|
|
|
if(document.getElementById('input_pdf')) {
|
|
|
|
document.getElementById('input_pdf').files = dataTransfer.files;
|
|
|
|
}
|
|
|
|
if(document.getElementById('input_pdf_share')) {
|
|
|
|
document.getElementById('input_pdf_share').files = dataTransfer.files;
|
|
|
|
}
|
2022-04-15 00:47:36 +02:00
|
|
|
let loadingTask = pdfjsLib.getDocument(url);
|
2022-01-05 02:18:36 +01:00
|
|
|
loadingTask.promise.then(function(pdf) {
|
|
|
|
|
|
|
|
if(pdf.numPages > maxPage) {
|
|
|
|
alert("Le PDF de doit pas dépasser "+maxPage+" pages");
|
|
|
|
document.location = "/";
|
|
|
|
return;
|
|
|
|
}
|
2022-04-14 09:09:01 +02:00
|
|
|
for(let pageNumber = 1; pageNumber <= pdf.numPages; pageNumber++ ) {
|
2022-01-05 02:18:36 +01:00
|
|
|
pdf.getPage(pageNumber).then(function(page) {
|
2022-04-14 09:09:01 +02:00
|
|
|
let scale = 1.5;
|
|
|
|
let viewport = page.getViewport({scale: scale});
|
2022-01-05 02:18:36 +01:00
|
|
|
if(viewport.width > document.getElementById('container-pages').clientWidth - 40) {
|
|
|
|
viewport = page.getViewport({scale: 1});
|
|
|
|
scale = (document.getElementById('container-pages').clientWidth - 40) / viewport.width;
|
|
|
|
viewport = page.getViewport({ scale: scale });
|
|
|
|
}
|
2021-11-12 01:49:23 +01:00
|
|
|
|
2022-01-05 02:18:36 +01:00
|
|
|
currentScale = scale;
|
|
|
|
|
2022-04-14 09:09:01 +02:00
|
|
|
let pageIndex = page.pageNumber - 1;
|
2022-01-05 02:18:36 +01:00
|
|
|
|
2022-03-31 13:29:38 +02:00
|
|
|
document.getElementById('form_block').insertAdjacentHTML('beforeend', '<input name="svg[' + pageIndex + ']" id="data-svg-' + pageIndex + '" type="hidden" value="" />');
|
2022-01-05 02:18:36 +01:00
|
|
|
document.getElementById('container-pages').insertAdjacentHTML('beforeend', '<div class="position-relative mt-1 ms-1 me-1 d-inline-block" id="canvas-container-' + pageIndex +'"><canvas id="canvas-pdf-'+pageIndex+'" class="shadow-sm canvas-pdf"></canvas><div class="position-absolute top-0 start-0"><canvas id="canvas-edition-'+pageIndex+'"></canvas></div></div>');
|
|
|
|
|
2022-04-14 09:09:01 +02:00
|
|
|
let canvasPDF = document.getElementById('canvas-pdf-' + pageIndex);
|
|
|
|
let canvasEditionHTML = document.getElementById('canvas-edition-' + pageIndex);
|
2022-01-05 02:18:36 +01:00
|
|
|
// Prepare canvas using PDF page dimensions
|
2022-04-14 09:09:01 +02:00
|
|
|
let context = canvasPDF.getContext('2d');
|
2022-01-05 02:18:36 +01:00
|
|
|
canvasPDF.height = viewport.height;
|
|
|
|
canvasPDF.width = viewport.width;
|
|
|
|
canvasEditionHTML.height = canvasPDF.height;
|
|
|
|
canvasEditionHTML.width = canvasPDF.width;
|
|
|
|
|
2022-04-14 09:09:01 +02:00
|
|
|
let renderContext = {
|
2022-01-05 02:18:36 +01:00
|
|
|
canvasContext: context,
|
|
|
|
viewport: viewport,
|
|
|
|
enhanceTextSelection: true
|
|
|
|
};
|
2022-04-14 09:09:01 +02:00
|
|
|
let renderTask = page.render(renderContext);
|
2022-01-05 02:18:36 +01:00
|
|
|
pdfRenderTasks.push(renderTask);
|
|
|
|
pdfPages.push(page);
|
2022-04-14 09:09:01 +02:00
|
|
|
let canvasEdition = new fabric.Canvas('canvas-edition-' + pageIndex, {
|
2022-01-05 02:18:36 +01:00
|
|
|
selection : false,
|
|
|
|
allowTouchScrolling: true
|
|
|
|
});
|
|
|
|
|
|
|
|
document.getElementById('canvas-container-' + pageIndex).addEventListener('drop', function(event) {
|
|
|
|
var input_selected = document.querySelector('input[name="svg_2_add"]:checked');
|
|
|
|
if(!input_selected) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
createAndAddSvgInCanvas(canvasEdition, input_selected.value, event.layerX, event.layerY, input_selected.dataset.height);
|
|
|
|
input_selected.checked = false;
|
|
|
|
input_selected.dispatchEvent(new Event("change"));
|
|
|
|
});
|
|
|
|
canvasEdition.on('mouse:move', function(event) {
|
|
|
|
activeCanvas = this;
|
|
|
|
activeCanvasPointer = event.pointer;
|
|
|
|
});
|
|
|
|
canvasEdition.on('mouse:down:before', function(event) {
|
|
|
|
currentCursor = this.defaultCursor;
|
|
|
|
});
|
|
|
|
canvasEdition.on('mouse:down', function(event) {
|
|
|
|
if(event.target) {
|
|
|
|
this.defaultCursor = 'default';
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var input_selected = document.querySelector('input[name="svg_2_add"]:checked');
|
|
|
|
if(currentCursor == 'default' && input_selected) {
|
|
|
|
this.defaultCursor = 'copy';
|
|
|
|
}
|
|
|
|
if(currentCursor != 'copy') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if(!input_selected) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
createAndAddSvgInCanvas(this, input_selected.value, event.pointer.x, event.pointer.y, input_selected.dataset.height);
|
|
|
|
|
|
|
|
if(addLock) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
input_selected.checked = false;
|
|
|
|
input_selected.dispatchEvent(new Event("change"));
|
|
|
|
});
|
|
|
|
canvasEdition.on('object:scaling', function(event) {
|
|
|
|
if(event.transform.action == "scaleX") {
|
|
|
|
event.target.scaleY = event.target.scaleX;
|
|
|
|
}
|
|
|
|
if(event.transform.action == "scaleY") {
|
|
|
|
event.target.scaleX = event.target.scaleY;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
canvasEdition.on('object:scaled', function(event) {
|
|
|
|
var item = getSvgItem(event.target.svgOrigin);
|
|
|
|
if(!item) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
item.scale = event.target.width * event.target.scaleX / event.target.canvas.width;
|
|
|
|
storeCollections();
|
|
|
|
});
|
|
|
|
canvasEdition.on("text:changed", function(event) {
|
|
|
|
if (!event.target instanceof fabric.IText) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const textLinesMaxWidth = event.target.textLines.reduce((max, _, i) => Math.max(max, event.target.getLineWidth(i)), 0);
|
|
|
|
event.target.set({width: textLinesMaxWidth});
|
|
|
|
});
|
|
|
|
canvasEditions.push(canvasEdition);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}, function (reason) {
|
|
|
|
console.error(reason);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2022-04-15 00:47:36 +02:00
|
|
|
var reloadPDF = async function(url) {
|
|
|
|
const pdfjsLib = window['pdfjs-dist/build/pdf'];
|
|
|
|
pdfjsLib.GlobalWorkerOptions.workerSrc = '/vendor/pdf.worker.js?legacy';
|
|
|
|
|
|
|
|
pdfjsLib.getDocument(url).promise.then(function(pdf) {
|
|
|
|
for(let pageNumber = 1; pageNumber <= pdf.numPages; pageNumber++ ) {
|
|
|
|
pdf.getPage(pageNumber).then(function(page) {
|
|
|
|
page.render({
|
|
|
|
canvasContext: document.getElementById('canvas-pdf-' + (page.pageNumber - 1)).getContext('2d'),
|
|
|
|
viewport: page.getViewport({scale: currentScale}),
|
|
|
|
enhanceTextSelection: true
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-01-05 02:18:36 +01:00
|
|
|
var is_mobile = function() {
|
|
|
|
return !(window.getComputedStyle(document.getElementById('is_mobile')).display === "none");
|
|
|
|
};
|
|
|
|
|
|
|
|
var responsiveDisplay = function() {
|
|
|
|
if(is_mobile()) {
|
2022-03-26 11:02:47 +01:00
|
|
|
document.getElementById('page-signature').style.paddingRight = "inherit";
|
2022-01-05 02:18:36 +01:00
|
|
|
menu.classList.remove('show');
|
|
|
|
menuOffcanvas.hide();
|
|
|
|
document.getElementById('container-pages').classList.remove('vh-100');
|
|
|
|
} else {
|
|
|
|
menuOffcanvas.show();
|
2022-03-26 11:02:47 +01:00
|
|
|
document.getElementById('page-signature').style.paddingRight = "350px";
|
2022-01-05 02:18:36 +01:00
|
|
|
document.getElementById('container-pages').classList.add('vh-100');
|
|
|
|
}
|
|
|
|
menu.classList.remove('d-md-block');
|
|
|
|
menu.classList.remove('d-none');
|
|
|
|
};
|
2021-05-04 00:30:20 +02:00
|
|
|
|
2022-01-05 02:18:36 +01:00
|
|
|
var storeCollections = function () {
|
|
|
|
localStorage.setItem('svgCollections', JSON.stringify(svgCollections));
|
|
|
|
};
|
2021-11-13 10:46:45 +01:00
|
|
|
|
2022-01-05 02:18:36 +01:00
|
|
|
var getSvgItem = function(svg) {
|
|
|
|
for (index in svgCollections) {
|
2022-04-14 09:09:01 +02:00
|
|
|
let svgItem = svgCollections[index];
|
2022-01-05 02:18:36 +01:00
|
|
|
if(svgItem.svg == svg) {
|
2021-05-04 00:30:20 +02:00
|
|
|
|
2022-01-05 02:18:36 +01:00
|
|
|
return svgItem;
|
|
|
|
}
|
|
|
|
}
|
2021-05-04 00:30:20 +02:00
|
|
|
|
2022-01-05 02:18:36 +01:00
|
|
|
return null;
|
|
|
|
};
|
2021-10-12 01:02:40 +02:00
|
|
|
|
2022-01-05 02:18:36 +01:00
|
|
|
var svgClick = function(label, event) {
|
|
|
|
if(event.detail == 1) {
|
|
|
|
label.dataset.lock = parseInt(addLock*1);
|
|
|
|
}
|
|
|
|
if(event.detail > 1){
|
|
|
|
stateAddLock(parseInt(label.dataset.lock*1) != 1);
|
|
|
|
}
|
|
|
|
if(event.detail > 1) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if(!document.getElementById(label.htmlFor)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if(!document.getElementById(label.htmlFor).checked) {
|
2021-11-12 10:10:32 +01:00
|
|
|
return;
|
|
|
|
}
|
2022-01-05 02:18:36 +01:00
|
|
|
document.getElementById(label.htmlFor).checked = false;
|
|
|
|
document.getElementById(label.htmlFor).dispatchEvent(new Event("change"));
|
|
|
|
event.preventDefault();
|
|
|
|
};
|
2021-11-12 10:10:32 +01:00
|
|
|
|
2022-01-05 02:18:36 +01:00
|
|
|
var svgDblClick = function(label, event) {
|
|
|
|
if(parseInt(label.dataset.lock*1) == 1) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
stateAddLock(true);
|
|
|
|
};
|
|
|
|
|
|
|
|
var svgDragStart = function(label, event) {
|
|
|
|
document.getElementById(label.htmlFor).checked = true;
|
|
|
|
document.getElementById(label.htmlFor).dispatchEvent(new Event("change"));
|
|
|
|
};
|
|
|
|
|
|
|
|
var svgChange = function(input, event) {
|
|
|
|
if(input.checked) {
|
|
|
|
document.getElementById('btn_svn_select').classList.add('d-none');
|
|
|
|
document.getElementById('svg_object_actions').classList.add('d-none');
|
|
|
|
document.getElementById('svg_selected_container').classList.remove('d-none');
|
|
|
|
if(input.value.match(/^data:/)) {
|
|
|
|
document.getElementById('svg_selected').src = input.value;
|
|
|
|
} else {
|
|
|
|
document.getElementById('svg_selected').src = input.dataset.svg;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
document.getElementById('btn_svn_select').classList.remove('d-none');
|
|
|
|
document.getElementById('svg_object_actions').classList.add('d-none');
|
|
|
|
document.getElementById('svg_selected_container').classList.add('d-none');
|
|
|
|
document.getElementById('svg_selected').src = "";
|
2021-11-10 01:15:17 +01:00
|
|
|
}
|
2021-10-12 01:02:40 +02:00
|
|
|
|
2022-01-05 02:18:36 +01:00
|
|
|
stateAddLock(false);
|
|
|
|
|
2022-04-14 09:09:01 +02:00
|
|
|
let input_selected = document.querySelector('input[name="svg_2_add"]:checked');
|
2022-01-05 02:18:36 +01:00
|
|
|
|
|
|
|
if(input_selected && !input_selected.value.match(/^data:/) && input_selected.value != "text") {
|
|
|
|
input_selected = null;
|
2021-11-01 02:28:18 +01:00
|
|
|
}
|
|
|
|
|
2022-01-05 02:18:36 +01:00
|
|
|
document.querySelectorAll('.btn-svg').forEach(function(item) {
|
|
|
|
if(input_selected && item.htmlFor == input_selected.id) {
|
|
|
|
item.style.setProperty('cursor', 'copy');
|
2021-10-11 01:17:06 +02:00
|
|
|
} else {
|
2022-01-05 02:18:36 +01:00
|
|
|
item.style.removeProperty('cursor');
|
2021-10-11 01:17:06 +02:00
|
|
|
}
|
2022-01-05 02:18:36 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
canvasEditions.forEach(function(canvasEdition, index) {
|
|
|
|
if(input_selected) {
|
|
|
|
canvasEdition.defaultCursor = 'copy';
|
|
|
|
} else {
|
|
|
|
canvasEdition.defaultCursor = 'default';
|
|
|
|
}
|
|
|
|
})
|
|
|
|
if(is_mobile()) {
|
|
|
|
menuOffcanvas.hide();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
var getHtmlSvg = function(svg, i) {
|
2022-04-14 09:09:01 +02:00
|
|
|
let inputRadio = document.createElement('input');
|
2022-01-05 02:18:36 +01:00
|
|
|
inputRadio.type = "radio";
|
|
|
|
inputRadio.classList.add("btn-check");
|
|
|
|
inputRadio.id="radio_svg_"+i;
|
|
|
|
inputRadio.name = "svg_2_add";
|
|
|
|
inputRadio.autocomplete = "off";
|
|
|
|
inputRadio.value = svg.svg;
|
|
|
|
inputRadio.addEventListener('change', function() {
|
|
|
|
svgChange(this, event);
|
|
|
|
});
|
2022-04-14 09:09:01 +02:00
|
|
|
let svgButton = document.createElement('label');
|
2022-01-05 02:18:36 +01:00
|
|
|
svgButton.id = "label_svg_"+i;
|
|
|
|
svgButton.classList.add('position-relative');
|
|
|
|
svgButton.classList.add('btn');
|
|
|
|
svgButton.classList.add('btn-svg');
|
|
|
|
svgButton.classList.add('btn-outline-secondary');
|
|
|
|
svgButton.htmlFor = "radio_svg_"+i;
|
|
|
|
if(svg.type == 'signature') {
|
|
|
|
svgButton.innerHTML += '<i class="bi bi-vector-pen text-black align-middle float-start"></i>';
|
|
|
|
}
|
|
|
|
if(svg.type == 'initials') {
|
|
|
|
svgButton.innerHTML += '<i class="bi bi-type text-black align-middle float-start"></i>';
|
|
|
|
}
|
|
|
|
if(svg.type == 'rubber_stamber') {
|
|
|
|
svgButton.innerHTML += '<i class="bi bi-card-text text-black align-middle float-start"></i>';
|
2021-10-07 01:38:44 +02:00
|
|
|
}
|
2022-01-05 02:18:36 +01:00
|
|
|
if(svg.type) {
|
|
|
|
document.querySelector('.btn-add-svg-type[data-type="'+svg.type+'"]').classList.add('d-none');
|
|
|
|
}
|
|
|
|
svgButton.innerHTML += '<a title="Supprimer" data-index="'+i+'" class="btn-svg-list-suppression opacity-50 link-dark position-absolute" style="right: 6px; top: 2px;"><i class="bi bi-trash"></i></a>';
|
|
|
|
svgButton.draggable = true;
|
|
|
|
svgButton.addEventListener('dragstart', function(event) {
|
|
|
|
svgDragStart(this, event);
|
|
|
|
});
|
|
|
|
svgButton.addEventListener('click', function(event) {
|
|
|
|
svgClick(this, event);
|
|
|
|
});
|
|
|
|
svgButton.addEventListener('dblclick', function(event) {
|
|
|
|
svgDblClick(this, event);
|
|
|
|
});
|
|
|
|
svgButton.addEventListener('mouseout', function(event) {
|
|
|
|
this.style.removeProperty('cursor');
|
|
|
|
})
|
2022-04-14 09:09:01 +02:00
|
|
|
let svgImg = document.createElement('img');
|
2022-01-05 02:18:36 +01:00
|
|
|
svgImg.src = svg.svg;
|
|
|
|
svgImg.draggable = false;
|
|
|
|
svgImg.style = "max-width: 180px;max-height: 70px;";
|
|
|
|
svgButton.appendChild(svgImg);
|
2022-04-14 09:09:01 +02:00
|
|
|
let svgContainer = document.createElement('div');
|
2022-01-05 02:18:36 +01:00
|
|
|
svgContainer.classList.add('d-grid');
|
|
|
|
svgContainer.classList.add('gap-2');
|
|
|
|
svgContainer.appendChild(inputRadio);
|
|
|
|
svgContainer.appendChild(svgButton);
|
|
|
|
|
|
|
|
return svgContainer;
|
|
|
|
};
|
|
|
|
|
|
|
|
var stateAddLock = function(state) {
|
|
|
|
if(forceAddLock) {
|
|
|
|
state = true;
|
|
|
|
}
|
2022-04-14 09:09:01 +02:00
|
|
|
let checkbox = document.getElementById('add-lock-checkbox');
|
|
|
|
let input_selected = document.querySelector('input[name="svg_2_add"]:checked');
|
2021-10-12 00:27:12 +02:00
|
|
|
|
2022-01-05 02:18:36 +01:00
|
|
|
addLock = state;
|
2021-10-12 00:27:12 +02:00
|
|
|
|
2022-01-05 02:18:36 +01:00
|
|
|
if(!input_selected) {
|
|
|
|
addLock = false;
|
|
|
|
checkbox.disabled = true;
|
|
|
|
} else {
|
|
|
|
checkbox.disabled = false;
|
2021-09-25 16:48:41 +02:00
|
|
|
}
|
2021-05-04 00:30:20 +02:00
|
|
|
|
2022-01-05 02:18:36 +01:00
|
|
|
if(addLock && input_selected) {
|
2022-04-14 09:09:01 +02:00
|
|
|
let svgButton = document.querySelector('.btn-svg[for="'+input_selected.id+'"]');
|
2022-01-05 02:18:36 +01:00
|
|
|
checkbox.checked = true;
|
|
|
|
return;
|
2021-10-12 00:27:12 +02:00
|
|
|
}
|
|
|
|
|
2022-01-05 02:18:36 +01:00
|
|
|
checkbox.checked = false;
|
|
|
|
};
|
2021-10-12 00:27:12 +02:00
|
|
|
|
2022-01-05 02:18:36 +01:00
|
|
|
var displaysSVG = function() {
|
|
|
|
document.getElementById('svg_list').innerHTML = "";
|
|
|
|
document.getElementById('svg_list_signature').innerHTML = "";
|
|
|
|
document.getElementById('svg_list_initials').innerHTML = "";
|
|
|
|
document.getElementById('svg_list_rubber_stamber').innerHTML = "";
|
|
|
|
document.querySelectorAll('.btn-add-svg-type').forEach(function(item) {
|
|
|
|
item.classList.remove('d-none');
|
|
|
|
});
|
|
|
|
svgCollections.forEach((svg, i) => {
|
2022-04-14 09:09:01 +02:00
|
|
|
let svgHtmlChild = getHtmlSvg(svg, i);
|
2022-01-05 02:18:36 +01:00
|
|
|
if(svg.type) {
|
|
|
|
document.getElementById('svg_list_'+svg.type).appendChild(svgHtmlChild);
|
|
|
|
return;
|
2021-10-12 00:27:12 +02:00
|
|
|
}
|
2022-01-05 02:18:36 +01:00
|
|
|
document.getElementById('svg_list').appendChild(svgHtmlChild);
|
|
|
|
});
|
2021-10-12 00:27:12 +02:00
|
|
|
|
2022-01-05 02:18:36 +01:00
|
|
|
if(svgCollections.length > 0) {
|
|
|
|
document.getElementById('btn-add-svg').classList.add('btn-light');
|
|
|
|
document.getElementById('btn-add-svg').classList.remove('btn-primary');
|
2021-10-12 00:27:12 +02:00
|
|
|
}
|
|
|
|
|
2022-01-05 02:18:36 +01:00
|
|
|
if(document.getElementById('btn-add-svg').classList.contains('btn-primary')) {
|
|
|
|
document.getElementById('btn-add-svg').focus();
|
|
|
|
}
|
|
|
|
|
|
|
|
document.querySelectorAll('.btn-svg-list-suppression').forEach(function(item) {
|
|
|
|
item.addEventListener('click', function() {
|
|
|
|
svgCollections.splice(this.dataset.index, 1);
|
|
|
|
displaysSVG();
|
|
|
|
storeCollections();
|
|
|
|
});
|
2021-09-20 22:49:44 +02:00
|
|
|
});
|
2022-01-05 02:18:36 +01:00
|
|
|
};
|
2021-10-12 00:27:12 +02:00
|
|
|
|
2022-01-05 02:18:36 +01:00
|
|
|
function dataURLtoBlob(dataurl) {
|
2022-04-14 09:09:01 +02:00
|
|
|
let arr = dataurl.split(','), mime = arr[0].match(/:(.*?);/)[1],
|
2022-01-05 02:18:36 +01:00
|
|
|
bstr = atob(arr[1]), n = bstr.length, u8arr = new Uint8Array(n);
|
|
|
|
while(n--){
|
|
|
|
u8arr[n] = bstr.charCodeAt(n);
|
2021-10-30 10:21:07 +02:00
|
|
|
}
|
2022-01-05 02:18:36 +01:00
|
|
|
return new Blob([u8arr], {type:mime});
|
|
|
|
};
|
2021-10-30 10:21:07 +02:00
|
|
|
|
2022-01-05 02:18:36 +01:00
|
|
|
function svgToDataUrl(svg) {
|
|
|
|
|
|
|
|
return "data:image/svg+xml;base64,"+btoa(svg);
|
|
|
|
};
|
|
|
|
|
|
|
|
function trimSvgWhitespace(svgContent) {
|
|
|
|
if(!svgContent) {
|
2021-10-30 10:21:07 +02:00
|
|
|
|
2022-01-05 02:18:36 +01:00
|
|
|
return null;
|
2021-10-30 10:21:07 +02:00
|
|
|
}
|
2022-04-14 09:09:01 +02:00
|
|
|
let svgContainer = document.createElement("div")
|
2022-01-05 02:18:36 +01:00
|
|
|
svgContainer.classList.add('invisible');
|
|
|
|
svgContainer.classList.add('position-absolute');
|
|
|
|
svgContainer.classList.add('top-0');
|
|
|
|
svgContainer.classList.add('start-0');
|
|
|
|
svgContainer.style = "z-index: -1;";
|
|
|
|
svgContainer.innerHTML = svgContent;
|
|
|
|
document.body.appendChild(svgContainer);
|
2022-04-14 09:09:01 +02:00
|
|
|
let svg = svgContainer.querySelector('svg');
|
|
|
|
let box = svg.getBBox();
|
2022-01-05 02:18:36 +01:00
|
|
|
svg.setAttribute("viewBox", [box.x, box.y, box.width, box.height].join(" "));
|
|
|
|
svgContent = svgContainer.innerHTML;
|
|
|
|
document.body.removeChild(svgContainer)
|
|
|
|
|
|
|
|
return svgContent = svgContainer.innerHTML;
|
|
|
|
};
|
|
|
|
|
|
|
|
var uploadSVG = function(formData) {
|
|
|
|
document.getElementById('btn_modal_ajouter').setAttribute('disabled', 'disabled');
|
|
|
|
document.getElementById('btn_modal_ajouter_spinner').classList.remove('d-none');
|
|
|
|
document.getElementById('btn_modal_ajouter_check').classList.add('d-none');
|
|
|
|
|
2022-04-14 09:09:01 +02:00
|
|
|
let xhr = new XMLHttpRequest();
|
2022-01-05 02:18:36 +01:00
|
|
|
|
|
|
|
xhr.open( 'POST', document.getElementById('form-image-upload').action, true );
|
|
|
|
xhr.onreadystatechange = function () {
|
|
|
|
var svgImage = svgToDataUrl(trimSvgWhitespace(this.responseText));
|
|
|
|
document.getElementById('img-upload').src = svgImage;
|
|
|
|
document.getElementById('img-upload').classList.remove("d-none");
|
|
|
|
document.getElementById('btn_modal_ajouter').removeAttribute('disabled');
|
|
|
|
document.getElementById('btn_modal_ajouter_spinner').classList.add('d-none');
|
|
|
|
document.getElementById('btn_modal_ajouter_check').classList.remove('d-none');
|
|
|
|
document.getElementById('btn_modal_ajouter').focus();
|
|
|
|
};
|
|
|
|
xhr.send( formData );
|
|
|
|
};
|
|
|
|
|
|
|
|
var deleteActiveObject = function() {
|
|
|
|
canvasEditions.forEach(function(canvasEdition, index) {
|
|
|
|
canvasEdition.getActiveObjects().forEach(function(activeObject) {
|
|
|
|
canvasEdition.remove(activeObject);
|
|
|
|
});
|
|
|
|
})
|
|
|
|
};
|
|
|
|
|
|
|
|
var addObjectInCanvas = function(canvas, item) {
|
|
|
|
item.on('selected', function(event) {
|
|
|
|
if(!is_mobile()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
document.getElementById('svg_object_actions').classList.remove('d-none');
|
|
|
|
document.getElementById('svg_selected_container').classList.add('d-none');
|
|
|
|
document.getElementById('btn_svn_select').classList.add('d-none');
|
|
|
|
});
|
2021-10-30 10:21:07 +02:00
|
|
|
|
2022-01-05 02:18:36 +01:00
|
|
|
item.on('deselected', function(event) {
|
|
|
|
if(!is_mobile()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if(document.querySelector('input[name="svg_2_add"]:checked')) {
|
2021-10-30 10:21:07 +02:00
|
|
|
document.getElementById('svg_selected_container').classList.remove('d-none');
|
|
|
|
} else {
|
|
|
|
document.getElementById('btn_svn_select').classList.remove('d-none');
|
|
|
|
}
|
2022-01-05 02:18:36 +01:00
|
|
|
document.getElementById('svg_object_actions').classList.add('d-none');
|
|
|
|
});
|
2021-10-30 10:21:07 +02:00
|
|
|
|
2022-01-05 02:18:36 +01:00
|
|
|
return canvas.add(item);
|
|
|
|
};
|
2021-10-30 10:21:07 +02:00
|
|
|
|
2022-01-05 02:18:36 +01:00
|
|
|
var createAndAddSvgInCanvas = function(canvas, item, x, y, height = null) {
|
2022-03-31 13:29:38 +02:00
|
|
|
if(document.getElementById('save')) {
|
|
|
|
document.getElementById('save').removeAttribute('disabled');
|
|
|
|
}
|
|
|
|
if(document.getElementById('save_mobile')) {
|
|
|
|
document.getElementById('save_mobile').removeAttribute('disabled');
|
|
|
|
}
|
2022-03-31 18:46:53 +02:00
|
|
|
if(document.getElementById('btn_download')) {
|
2022-04-01 15:09:13 +02:00
|
|
|
document.getElementById('btn_download').classList.remove('btn-outline-dark');
|
|
|
|
document.getElementById('btn_download').classList.add('btn-outline-secondary');
|
|
|
|
}
|
|
|
|
if(document.getElementById('btn_share')) {
|
|
|
|
document.getElementById('btn_share').classList.remove('btn-outline-dark');
|
|
|
|
document.getElementById('btn_share').classList.add('btn-outline-secondary');
|
2022-03-31 18:46:53 +02:00
|
|
|
}
|
|
|
|
|
2021-10-30 10:43:15 +02:00
|
|
|
|
2022-01-05 02:18:36 +01:00
|
|
|
if(!height) {
|
|
|
|
height = 100;
|
|
|
|
}
|
2021-10-30 10:43:15 +02:00
|
|
|
|
2022-01-05 02:18:36 +01:00
|
|
|
if(item == 'text') {
|
2022-04-14 09:09:01 +02:00
|
|
|
let textbox = new fabric.Textbox('Texte à modifier', {
|
2022-01-05 02:18:36 +01:00
|
|
|
left: x,
|
|
|
|
top: y - 20,
|
|
|
|
fontSize: 20,
|
|
|
|
fontFamily: 'Monospace'
|
|
|
|
});
|
2021-10-30 10:21:07 +02:00
|
|
|
|
2022-01-05 02:18:36 +01:00
|
|
|
addObjectInCanvas(canvas, textbox).setActiveObject(textbox);
|
|
|
|
textbox.keysMap[13] = "exitEditing";
|
|
|
|
textbox.lockScalingFlip = true;
|
|
|
|
textbox.enterEditing();
|
|
|
|
textbox.selectAll();
|
|
|
|
|
|
|
|
|
|
|
|
return;
|
2021-10-30 10:21:07 +02:00
|
|
|
}
|
|
|
|
|
2022-01-05 02:18:36 +01:00
|
|
|
fabric.loadSVGFromURL(item, function(objects, options) {
|
2022-04-14 09:09:01 +02:00
|
|
|
let svg = fabric.util.groupSVGElements(objects, options);
|
2022-01-05 02:18:36 +01:00
|
|
|
svg.svgOrigin = item;
|
|
|
|
svg.lockScalingFlip = true;
|
|
|
|
svg.scaleToHeight(height);
|
|
|
|
if(svg.getScaledWidth() > 200) {
|
|
|
|
svg.scaleToWidth(200);
|
2021-10-03 09:34:05 +02:00
|
|
|
}
|
2022-04-14 09:09:01 +02:00
|
|
|
let svgItem = getSvgItem(item);
|
2022-01-05 02:18:36 +01:00
|
|
|
if(svgItem && svgItem.scale) {
|
|
|
|
svg.scaleToWidth(canvas.width * svgItem.scale);
|
2021-10-03 09:34:05 +02:00
|
|
|
}
|
2022-01-05 02:18:36 +01:00
|
|
|
svg.top = y - (svg.getScaledHeight() / 2);
|
|
|
|
svg.left = x - (svg.getScaledWidth() / 2);
|
2021-10-12 00:27:12 +02:00
|
|
|
|
2022-01-05 02:18:36 +01:00
|
|
|
addObjectInCanvas(canvas, svg);
|
2021-10-12 09:44:50 +02:00
|
|
|
});
|
2022-01-05 02:18:36 +01:00
|
|
|
};
|
2021-10-12 09:44:50 +02:00
|
|
|
|
2022-01-05 02:18:36 +01:00
|
|
|
var autoZoom = function() {
|
|
|
|
clearTimeout(resizeTimeout);
|
|
|
|
resizeTimeout = setTimeout(resizePDF, 100);
|
|
|
|
};
|
2021-10-12 09:44:50 +02:00
|
|
|
|
2022-01-05 02:18:36 +01:00
|
|
|
var zoomChange = function (inOrOut) {
|
|
|
|
if(resizeTimeout) {
|
|
|
|
return;
|
|
|
|
}
|
2021-10-12 09:44:50 +02:00
|
|
|
|
2022-04-14 09:09:01 +02:00
|
|
|
let deltaScale = 0.2 * inOrOut;
|
2022-01-05 02:18:36 +01:00
|
|
|
|
|
|
|
if(currentScale + deltaScale < 0) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if(currentScale + deltaScale > 3) {
|
|
|
|
return
|
|
|
|
}
|
2021-10-12 09:44:50 +02:00
|
|
|
|
2022-01-05 02:18:36 +01:00
|
|
|
clearTimeout(resizeTimeout);
|
|
|
|
currentScale += deltaScale;
|
2021-11-09 00:36:10 +01:00
|
|
|
|
2022-01-05 02:18:36 +01:00
|
|
|
resizeTimeout = setTimeout(resizePDF(currentScale), 50);
|
|
|
|
};
|
|
|
|
|
|
|
|
var resizePDF = function (scale = 'auto') {
|
|
|
|
renderComplete = true;
|
|
|
|
pdfRenderTasks.forEach(function(renderTask) {
|
|
|
|
if(!renderTask) {
|
|
|
|
renderComplete = false;
|
2021-10-12 09:44:50 +02:00
|
|
|
}
|
2022-01-05 02:18:36 +01:00
|
|
|
});
|
2021-10-12 09:44:50 +02:00
|
|
|
|
2022-01-05 02:18:36 +01:00
|
|
|
if(!renderComplete) {
|
|
|
|
clearTimeout(resizeTimeout);
|
|
|
|
resizeTimeout = setTimeout(function(){ resizePDF(scale) }, 50);
|
|
|
|
return;
|
2021-10-12 09:44:50 +02:00
|
|
|
}
|
|
|
|
|
2022-01-05 02:18:36 +01:00
|
|
|
pdfPages.forEach(function(page, pageIndex) {
|
2022-04-14 09:09:01 +02:00
|
|
|
let renderTask = pdfRenderTasks[pageIndex];
|
2021-09-28 09:14:11 +02:00
|
|
|
|
2022-01-05 02:18:36 +01:00
|
|
|
if(scale == 'auto' && page.getViewport({scale: 1.5}).width > document.getElementById('container-pages').clientWidth - 40) {
|
|
|
|
scale = (document.getElementById('container-pages').clientWidth - 40) / page.getViewport({scale: 1}).width;
|
2021-10-02 00:36:44 +02:00
|
|
|
}
|
|
|
|
|
2022-01-05 02:18:36 +01:00
|
|
|
if(scale == 'auto') {
|
|
|
|
scale = 1.5;
|
2021-10-02 01:27:59 +02:00
|
|
|
}
|
|
|
|
|
2022-04-14 09:09:01 +02:00
|
|
|
let viewport = page.getViewport({scale: scale});
|
2022-01-05 02:18:36 +01:00
|
|
|
currentScale = scale;
|
|
|
|
|
2022-04-14 09:09:01 +02:00
|
|
|
let canvasPDF = document.getElementById('canvas-pdf-' + pageIndex);
|
|
|
|
let context = canvasPDF.getContext('2d');
|
2022-01-05 02:18:36 +01:00
|
|
|
canvasPDF.height = viewport.height;
|
|
|
|
canvasPDF.width = viewport.width;
|
|
|
|
canvasEdition = canvasEditions[pageIndex];
|
|
|
|
|
2022-04-14 09:09:01 +02:00
|
|
|
let scaleMultiplier = canvasPDF.width / canvasEdition.width;
|
|
|
|
let objects = canvasEdition.getObjects();
|
|
|
|
for (let i in objects) {
|
2022-01-05 02:18:36 +01:00
|
|
|
objects[i].scaleX = objects[i].scaleX * scaleMultiplier;
|
|
|
|
objects[i].scaleY = objects[i].scaleY * scaleMultiplier;
|
|
|
|
objects[i].left = objects[i].left * scaleMultiplier;
|
|
|
|
objects[i].top = objects[i].top * scaleMultiplier;
|
|
|
|
objects[i].setCoords();
|
|
|
|
}
|
|
|
|
|
|
|
|
canvasEdition.setWidth(canvasEdition.getWidth() * scaleMultiplier);
|
|
|
|
canvasEdition.setHeight(canvasEdition.getHeight() * scaleMultiplier);
|
|
|
|
canvasEdition.renderAll();
|
|
|
|
canvasEdition.calcOffset();
|
|
|
|
|
2022-04-14 09:09:01 +02:00
|
|
|
let renderContext = {
|
2022-01-05 02:18:36 +01:00
|
|
|
canvasContext: context,
|
|
|
|
viewport: viewport,
|
|
|
|
enhanceTextSelection: true
|
|
|
|
};
|
|
|
|
renderTask.cancel();
|
|
|
|
pdfRenderTasks[pageIndex] = null;
|
|
|
|
renderTask = page.render(renderContext);
|
|
|
|
renderTask.promise.then(function () {
|
|
|
|
pdfRenderTasks[pageIndex] = renderTask;
|
|
|
|
clearTimeout(resizeTimeout);
|
|
|
|
resizeTimeout = null;
|
2021-09-28 09:14:11 +02:00
|
|
|
});
|
2022-01-05 02:18:36 +01:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2022-01-05 02:25:39 +01:00
|
|
|
var createEventsListener = function() {
|
2022-01-05 02:18:36 +01:00
|
|
|
|
|
|
|
document.getElementById('add-lock-checkbox').addEventListener('change', function() {
|
|
|
|
stateAddLock(this.checked);
|
|
|
|
});
|
|
|
|
|
2021-10-03 09:34:05 +02:00
|
|
|
document.querySelectorAll('.btn-add-svg-type').forEach(function(item) {
|
|
|
|
item.addEventListener('click', function(event) {
|
|
|
|
document.getElementById('input-svg-type').value = this.dataset.type;
|
2021-10-03 20:51:41 +02:00
|
|
|
if(this.dataset.modalnav) {
|
|
|
|
bootstrap.Tab.getOrCreateInstance(document.querySelector('#modalAddSvg #nav-tab '+this.dataset.modalnav)).show();
|
|
|
|
}
|
2021-10-03 09:34:05 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2021-10-04 00:14:10 +02:00
|
|
|
document.querySelectorAll('label.btn-svg').forEach(function(item) {
|
|
|
|
item.addEventListener('dragstart', function(event) {
|
2021-10-30 10:21:07 +02:00
|
|
|
svgDragStart(this, event);
|
2021-10-04 01:08:08 +02:00
|
|
|
});
|
2021-10-12 01:37:19 +02:00
|
|
|
item.addEventListener('click', function(event) {
|
2021-10-30 10:21:07 +02:00
|
|
|
svgClick(this, event);
|
|
|
|
});
|
|
|
|
item.addEventListener('dblclick', function(event) {
|
|
|
|
svgDblClick(this, event);
|
2021-10-12 01:37:19 +02:00
|
|
|
});
|
2021-10-04 01:08:08 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
document.querySelectorAll('input[name="svg_2_add"]').forEach(function (item) {
|
2021-10-30 10:21:07 +02:00
|
|
|
item.addEventListener('change', function(event) {
|
|
|
|
svgChange(this, event);
|
2021-10-04 00:14:10 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2021-09-25 15:01:22 +02:00
|
|
|
document.getElementById('btn_modal_ajouter').addEventListener('click', function() {
|
2022-04-14 09:09:01 +02:00
|
|
|
let svgItem = {};
|
2021-10-03 09:34:05 +02:00
|
|
|
if(document.getElementById('input-svg-type').value) {
|
|
|
|
svgItem.type = document.getElementById('input-svg-type').value;
|
|
|
|
}
|
2021-09-25 15:01:22 +02:00
|
|
|
if(document.getElementById('nav-draw-tab').classList.contains('active')) {
|
2021-10-03 09:34:05 +02:00
|
|
|
svgItem.svg = document.getElementById('img-upload').src;
|
2021-09-25 15:01:22 +02:00
|
|
|
}
|
|
|
|
if(document.getElementById('nav-type-tab').classList.contains('active')) {
|
2022-04-14 09:09:01 +02:00
|
|
|
let fontPath = fontCaveat.getPath(document.getElementById('input-text-signature').value, 0, 0, 42);
|
|
|
|
let fabricPath = new fabric.Path(fontPath.toPathData());
|
2021-10-12 01:02:40 +02:00
|
|
|
fabricPath.top = 0;
|
|
|
|
fabricPath.left = 0;
|
2021-09-25 15:01:22 +02:00
|
|
|
fabricPath.height = fabricPath.getScaledHeight();
|
2022-04-14 09:09:01 +02:00
|
|
|
let textCanvas = document.createElement('canvas');
|
2021-09-25 15:01:22 +02:00
|
|
|
textCanvas.width = fabricPath.getScaledWidth();
|
|
|
|
textCanvas.height = fabricPath.getScaledHeight();
|
2022-04-14 09:09:01 +02:00
|
|
|
let textCanvasFabric = new fabric.Canvas(textCanvas);
|
|
|
|
textCanvasFabric.add(fabricPath).renderAll();
|
|
|
|
svgItem.svg = svgToDataUrl(textCanvasFabric.toSVG());
|
2021-09-25 15:01:22 +02:00
|
|
|
}
|
|
|
|
if(document.getElementById('nav-import-tab').classList.contains('active')) {
|
2021-10-03 09:34:05 +02:00
|
|
|
svgItem.svg = document.getElementById('img-upload').src;
|
2021-09-25 15:01:22 +02:00
|
|
|
}
|
2021-10-03 09:34:05 +02:00
|
|
|
svgCollections.push(svgItem);
|
2021-09-25 15:01:22 +02:00
|
|
|
displaysSVG();
|
2021-09-25 16:48:41 +02:00
|
|
|
localStorage.setItem('svgCollections', JSON.stringify(svgCollections));
|
2021-10-03 10:12:55 +02:00
|
|
|
|
2022-04-14 09:09:01 +02:00
|
|
|
let svg_list_id = "svg_list";
|
2021-10-03 10:12:55 +02:00
|
|
|
if(svgItem.type) {
|
|
|
|
svg_list_id = svg_list_id + "_" + svgItem.type;
|
|
|
|
}
|
|
|
|
|
|
|
|
document.querySelector('#'+svg_list_id+' label:last-child').click();
|
2021-09-20 10:05:27 +02:00
|
|
|
});
|
2021-10-12 01:02:40 +02:00
|
|
|
|
|
|
|
|
2021-11-11 00:34:15 +01:00
|
|
|
document.getElementById('signature-pad-reset').addEventListener('click', function(event) {
|
|
|
|
signaturePad.clear();
|
|
|
|
event.preventDefault();
|
|
|
|
})
|
|
|
|
|
2021-09-25 15:36:34 +02:00
|
|
|
document.querySelectorAll('#modalAddSvg .nav-link').forEach(function(item) { item.addEventListener('shown.bs.tab', function (event) {
|
2022-04-14 09:09:01 +02:00
|
|
|
let firstInput = document.querySelector(event.target.dataset.bsTarget).querySelector('input');
|
2021-09-25 15:36:34 +02:00
|
|
|
if(firstInput) {
|
|
|
|
firstInput.focus();
|
|
|
|
}
|
|
|
|
})});
|
|
|
|
|
|
|
|
document.getElementById('modalAddSvg').addEventListener('shown.bs.modal', function (event) {
|
2021-10-03 20:51:41 +02:00
|
|
|
document.querySelector('#modalAddSvg #nav-tab button:first-child').focus();
|
2022-04-14 09:09:01 +02:00
|
|
|
let tab = document.querySelector('#modalAddSvg .tab-pane.active');
|
2021-10-03 20:51:41 +02:00
|
|
|
if(tab.querySelector('input')) {
|
|
|
|
tab.querySelector('input').focus();
|
|
|
|
}
|
2022-04-14 09:09:01 +02:00
|
|
|
let input_selected = document.querySelector('input[name="svg_2_add"]:checked');
|
2021-11-11 00:08:56 +01:00
|
|
|
if(input_selected) {
|
|
|
|
input_selected.checked = false;
|
|
|
|
input_selected.dispatchEvent(new Event("change"));
|
|
|
|
}
|
2021-09-25 15:36:34 +02:00
|
|
|
})
|
|
|
|
|
2021-09-25 15:01:22 +02:00
|
|
|
document.getElementById('modalAddSvg').addEventListener('hidden.bs.modal', function (event) {
|
|
|
|
signaturePad.clear();
|
2021-10-02 00:48:34 +02:00
|
|
|
document.getElementById('btn_modal_ajouter').setAttribute('disabled', 'disabled');
|
2021-10-03 09:34:05 +02:00
|
|
|
document.getElementById('input-svg-type').value = null;
|
2021-09-25 15:01:22 +02:00
|
|
|
document.getElementById('input-text-signature').value = null;
|
|
|
|
document.getElementById('input-image-upload').value = null;
|
2021-11-01 00:48:56 +01:00
|
|
|
document.getElementById('img-upload').src = "";
|
2021-09-25 15:01:22 +02:00
|
|
|
document.getElementById('img-upload').classList.add("d-none");
|
2021-09-25 15:36:34 +02:00
|
|
|
bootstrap.Tab.getOrCreateInstance(document.querySelector('#modalAddSvg #nav-tab button:first-child')).show();
|
|
|
|
})
|
2021-10-12 01:02:40 +02:00
|
|
|
|
2021-10-07 09:30:47 +02:00
|
|
|
document.getElementById('input-text-signature').addEventListener('keydown', function(event) {
|
2021-10-02 00:48:34 +02:00
|
|
|
document.getElementById('btn_modal_ajouter').removeAttribute('disabled');
|
2021-09-25 15:36:34 +02:00
|
|
|
if(event.key == 'Enter') {
|
|
|
|
document.getElementById('btn_modal_ajouter').click()
|
|
|
|
}
|
2021-09-25 15:01:22 +02:00
|
|
|
})
|
|
|
|
|
2021-09-20 00:10:22 +02:00
|
|
|
document.getElementById('input-image-upload').addEventListener('change', function(event) {
|
2022-04-14 09:09:01 +02:00
|
|
|
let data = new FormData();
|
2021-10-30 10:37:43 +02:00
|
|
|
data.append('file', document.getElementById('input-image-upload').files[0]);
|
|
|
|
uploadSVG(data);
|
|
|
|
event.preventDefault();
|
|
|
|
});
|
|
|
|
|
2022-03-31 13:29:38 +02:00
|
|
|
if(document.getElementById('save')) {
|
|
|
|
document.getElementById('save').addEventListener('click', function(event) {
|
2022-04-14 09:09:01 +02:00
|
|
|
let dataTransfer = new DataTransfer();
|
2022-03-31 13:29:38 +02:00
|
|
|
canvasEditions.forEach(function(canvasEdition, index) {
|
|
|
|
dataTransfer.items.add(new File([canvasEdition.toSVG()], index+'.svg', {
|
|
|
|
type: 'image/svg+xml'
|
|
|
|
}));
|
|
|
|
})
|
|
|
|
document.getElementById('input_svg').files = dataTransfer.files;
|
|
|
|
});
|
|
|
|
}
|
2021-10-12 01:02:40 +02:00
|
|
|
|
2022-03-31 18:38:38 +02:00
|
|
|
if(document.getElementById('save_share')) {
|
|
|
|
document.getElementById('save_share').addEventListener('click', function(event) {
|
2022-04-14 09:09:01 +02:00
|
|
|
let dataTransfer = new DataTransfer();
|
2022-04-02 00:58:35 +02:00
|
|
|
if(!document.getElementById('save').hasAttribute('disabled')) {
|
|
|
|
canvasEditions.forEach(function(canvasEdition, index) {
|
|
|
|
dataTransfer.items.add(new File([canvasEdition.toSVG()], index+'.svg', {
|
|
|
|
type: 'image/svg+xml'
|
|
|
|
}));
|
|
|
|
})
|
|
|
|
}
|
2022-03-31 18:38:38 +02:00
|
|
|
document.getElementById('input_svg_share').files = dataTransfer.files;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-10-07 01:38:44 +02:00
|
|
|
document.getElementById('save_mobile').addEventListener('click', function(event) {
|
|
|
|
document.getElementById('save').click();
|
|
|
|
});
|
|
|
|
|
2021-11-01 00:48:56 +01:00
|
|
|
document.getElementById('btn-svg-pdf-delete').addEventListener('click', function(event) {
|
|
|
|
deleteActiveObject();
|
|
|
|
});
|
|
|
|
|
2021-11-14 01:56:07 +01:00
|
|
|
document.getElementById('btn_svg_selected_close').addEventListener('click', function(event) {
|
2022-04-14 09:09:01 +02:00
|
|
|
let input_selected = document.querySelector('input[name="svg_2_add"]:checked');
|
2021-11-14 01:56:07 +01:00
|
|
|
|
|
|
|
stateAddLock(false);
|
|
|
|
input_selected.checked = false;
|
|
|
|
input_selected.dispatchEvent(new Event("change"));
|
|
|
|
this.blur();
|
|
|
|
});
|
|
|
|
|
2021-11-08 23:01:16 +01:00
|
|
|
document.addEventListener('click', function(event) {
|
|
|
|
if(event.target.nodeName == "DIV") {
|
2022-04-14 09:09:01 +02:00
|
|
|
let input_selected = document.querySelector('input[name="svg_2_add"]:checked');
|
2021-11-08 23:01:16 +01:00
|
|
|
if(!input_selected) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
stateAddLock(false);
|
|
|
|
input_selected.checked = false;
|
|
|
|
input_selected.dispatchEvent(new Event("change"));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-09-20 23:52:43 +02:00
|
|
|
document.addEventListener('keydown', function(event) {
|
2021-11-09 01:02:39 +01:00
|
|
|
if(event.key == 'Escape' && (event.target.tagName == "BODY" || event.target.name == "svg_2_add")) {
|
2022-04-14 09:09:01 +02:00
|
|
|
let input_selected = document.querySelector('input[name="svg_2_add"]:checked');
|
2021-11-09 01:02:39 +01:00
|
|
|
if(!input_selected) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
input_selected.checked = false;
|
|
|
|
stateAddLock(false);
|
|
|
|
input_selected.dispatchEvent(new Event("change"));
|
|
|
|
input_selected.blur();
|
|
|
|
return;
|
|
|
|
}
|
2021-09-20 23:52:43 +02:00
|
|
|
if(event.target.tagName != "BODY") {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if(event.key == 'Delete') {
|
2021-11-01 00:48:56 +01:00
|
|
|
deleteActiveObject();
|
2021-09-20 23:52:43 +02:00
|
|
|
return;
|
|
|
|
}
|
2021-10-12 01:02:40 +02:00
|
|
|
|
2021-09-21 00:49:15 +02:00
|
|
|
if(event.ctrlKey && event.key == 'c') {
|
2021-09-21 09:03:10 +02:00
|
|
|
if(!activeCanvas || !activeCanvas.getActiveObject()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
copiedObject = fabric.util.object.clone(activeCanvas.getActiveObject());
|
2021-10-12 01:02:40 +02:00
|
|
|
|
2021-09-21 00:49:15 +02:00
|
|
|
return;
|
|
|
|
}
|
2021-10-12 01:02:40 +02:00
|
|
|
|
2021-09-21 00:49:15 +02:00
|
|
|
if(event.ctrlKey && event.key == 'v') {
|
|
|
|
copiedObject = fabric.util.object.clone(copiedObject);
|
|
|
|
copiedObject.left = activeCanvasPointer.x;
|
|
|
|
copiedObject.top = activeCanvasPointer.y;
|
|
|
|
activeCanvas.add(copiedObject).renderAll();
|
|
|
|
return;
|
|
|
|
}
|
2021-10-10 21:49:34 +02:00
|
|
|
|
|
|
|
if(event.ctrlKey && (event.key == 'à' || event.key == '0')) {
|
|
|
|
autoZoom();
|
|
|
|
event.preventDefault() && event.stopPropagation();
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if(event.ctrlKey && (event.key == '=' || event.key == '+')) {
|
|
|
|
zoomChange(1);
|
|
|
|
event.preventDefault() && event.stopPropagation();
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if(event.ctrlKey && event.key == '-') {
|
|
|
|
zoomChange(-1);
|
|
|
|
event.preventDefault() && event.stopPropagation();
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2021-09-20 23:52:43 +02:00
|
|
|
});
|
2021-10-12 01:02:40 +02:00
|
|
|
|
2021-10-08 09:56:20 +02:00
|
|
|
window.addEventListener('resize', function(event) {
|
2021-10-10 21:49:34 +02:00
|
|
|
event.preventDefault() && event.stopPropagation();
|
2021-10-10 00:56:46 +02:00
|
|
|
if(windowWidth == window.innerWidth) {
|
|
|
|
return;
|
|
|
|
}
|
2021-10-11 01:17:06 +02:00
|
|
|
responsiveDisplay();
|
2021-10-10 00:56:46 +02:00
|
|
|
windowWidth = window.innerWidth;
|
2021-10-10 21:49:34 +02:00
|
|
|
autoZoom();
|
2021-10-08 09:56:20 +02:00
|
|
|
});
|
2021-10-10 21:49:34 +02:00
|
|
|
document.addEventListener('wheel', function(event) {
|
|
|
|
if(!event.ctrlKey) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
event.preventDefault() && event.stopPropagation();
|
2021-10-12 01:02:40 +02:00
|
|
|
|
2021-10-10 21:49:34 +02:00
|
|
|
if(event.deltaY > 0) {
|
|
|
|
zoomChange(-1)
|
|
|
|
} else {
|
|
|
|
zoomChange(1)
|
|
|
|
}
|
|
|
|
}, { passive: false });
|
2021-10-12 01:02:40 +02:00
|
|
|
|
2021-10-10 22:31:33 +02:00
|
|
|
document.getElementById('btn-zoom-decrease').addEventListener('click', function() {
|
|
|
|
zoomChange(-1)
|
|
|
|
});
|
|
|
|
document.getElementById('btn-zoom-increase').addEventListener('click', function() {
|
|
|
|
zoomChange(1)
|
|
|
|
});
|
2022-04-01 16:54:50 +02:00
|
|
|
|
|
|
|
if(hash) {
|
|
|
|
updateNbLayers();
|
|
|
|
setInterval(function() {
|
|
|
|
updateNbLayers();
|
|
|
|
}, 10000);
|
|
|
|
}
|
2022-01-05 02:25:39 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
var createSignaturePad = function() {
|
|
|
|
signaturePad = new SignaturePad(document.getElementById('signature-pad'), {
|
|
|
|
penColor: 'rgb(0, 0, 0)',
|
2022-04-14 00:50:17 +02:00
|
|
|
minWidth: 1,
|
2022-01-05 02:25:39 +01:00
|
|
|
maxWidth: 2,
|
|
|
|
onEnd: function() {
|
|
|
|
const file = new File([dataURLtoBlob(signaturePad.toDataURL())], "draw.png", {
|
|
|
|
type: 'image/png'
|
|
|
|
});
|
2022-04-14 09:09:01 +02:00
|
|
|
let data = new FormData();
|
2022-01-05 02:25:39 +01:00
|
|
|
data.append('file', file);
|
|
|
|
uploadSVG(data);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2022-03-26 11:02:47 +01:00
|
|
|
async function getPDFBlobFromCache(cacheUrl) {
|
|
|
|
const cache = await caches.open('pdf');
|
|
|
|
let responsePdf = await cache.match(cacheUrl);
|
|
|
|
|
|
|
|
if(!responsePdf) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
let pdfBlob = await responsePdf.blob();
|
|
|
|
|
|
|
|
return pdfBlob;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function uploadFromUrl(url) {
|
2022-03-26 23:10:06 +01:00
|
|
|
history.replaceState({}, '', '/signature');
|
2022-04-14 09:09:01 +02:00
|
|
|
let response = await fetch(url);
|
2022-03-26 11:02:47 +01:00
|
|
|
if(response.status != 200) {
|
|
|
|
return;
|
|
|
|
}
|
2022-04-14 09:09:01 +02:00
|
|
|
let pdfBlob = await response.blob();
|
2022-03-26 11:02:47 +01:00
|
|
|
|
|
|
|
if(pdfBlob.type != 'application/pdf' && pdfBlob.type != 'application/octet-stream') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
let dataTransfer = new DataTransfer();
|
|
|
|
let filename = url.replace(/^.*\//, '');
|
|
|
|
dataTransfer.items.add(new File([pdfBlob], filename, {
|
|
|
|
type: 'application/pdf'
|
|
|
|
}));
|
|
|
|
document.getElementById('input_pdf_upload').files = dataTransfer.files;
|
|
|
|
document.getElementById('input_pdf_upload').dispatchEvent(new Event("change"));
|
|
|
|
}
|
2022-01-05 02:25:39 +01:00
|
|
|
|
2022-04-01 14:52:30 +02:00
|
|
|
var modalSharing = function() {
|
|
|
|
if(window.location.hash == '#informations') {
|
|
|
|
let modalInformationsEl = document.getElementById('modal-share-informations');
|
|
|
|
let modalInformations = bootstrap.Modal.getOrCreateInstance(modalInformationsEl);
|
|
|
|
modalInformations.show();
|
|
|
|
modalInformationsEl.addEventListener('hidden.bs.modal', function (event) {
|
2022-04-01 15:03:50 +02:00
|
|
|
if(window.location.hash) {
|
|
|
|
history.pushState({}, '', window.location.href.replace(/#.*$/, ''));
|
|
|
|
}
|
2022-04-01 14:52:30 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
if(window.location.hash == '#signed') {
|
|
|
|
let modalSignedEl = document.getElementById('modal-signed');
|
|
|
|
let modalSigned = bootstrap.Modal.getOrCreateInstance(modalSignedEl);
|
|
|
|
modalSigned.show();
|
|
|
|
modalSignedEl.addEventListener('hidden.bs.modal', function (event) {
|
2022-04-01 15:03:50 +02:00
|
|
|
if(window.location.hash) {
|
|
|
|
history.pushState({}, '', window.location.href.replace(/#.*$/, ''));
|
|
|
|
}
|
2022-04-01 14:52:30 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-27 00:41:44 +02:00
|
|
|
var runCron = function() {
|
|
|
|
const xhr = new XMLHttpRequest();
|
|
|
|
xhr.open('GET', '/cron');
|
|
|
|
xhr.send();
|
|
|
|
}
|
|
|
|
|
2022-03-26 11:02:47 +01:00
|
|
|
var pageUpload = async function() {
|
2022-04-14 09:09:48 +02:00
|
|
|
document.querySelector('body').classList.remove('bg-light');
|
2022-03-26 11:02:47 +01:00
|
|
|
document.getElementById('input_pdf_upload').value = '';
|
|
|
|
document.getElementById('page-upload').classList.remove('d-none');
|
|
|
|
document.getElementById('page-signature').classList.add('d-none');
|
|
|
|
document.getElementById('input_pdf_upload').focus();
|
|
|
|
const cache = await caches.open('pdf');
|
|
|
|
document.getElementById('input_pdf_upload').addEventListener('change', async function(event) {
|
2022-04-01 00:00:54 +02:00
|
|
|
if(document.getElementById('input_pdf_upload').files[0].size > maxSize) {
|
2022-03-26 11:02:47 +01:00
|
|
|
|
2022-03-30 02:05:02 +02:00
|
|
|
alert("Le PDF ne doit pas dépasser " + Math.round(maxSize/1024/1024) + " Mo");s
|
2022-03-26 11:02:47 +01:00
|
|
|
document.getElementById('input_pdf_upload').value = "";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
let filename = document.getElementById('input_pdf_upload').files[0].name;
|
|
|
|
let response = new Response(document.getElementById('input_pdf_upload').files[0], { "status" : 200, "statusText" : "OK" });
|
|
|
|
let urlPdf = '/pdf/'+filename;
|
|
|
|
await cache.put(urlPdf, response);
|
2022-03-26 23:10:06 +01:00
|
|
|
history.pushState({}, '', '/signature#'+filename);
|
2022-03-26 11:02:47 +01:00
|
|
|
pageSignature(urlPdf)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-04-01 16:54:50 +02:00
|
|
|
var updateNbLayers = function() {
|
2022-04-01 15:43:47 +02:00
|
|
|
const xhr = new XMLHttpRequest();
|
2022-04-01 16:54:50 +02:00
|
|
|
xhr.open('GET', '/signature/'+hash+'/nblayers', true);
|
2022-04-01 15:43:47 +02:00
|
|
|
xhr.onload = function() {
|
|
|
|
if (xhr.status == 200) {
|
2022-04-15 00:47:36 +02:00
|
|
|
let newNblayers = xhr.response;
|
|
|
|
if(nblayers !== null && nblayers != newNblayers) {
|
|
|
|
reloadPDF('/signature/'+hash+'/pdf');
|
|
|
|
}
|
|
|
|
nblayers = newNblayers;
|
2022-04-01 16:54:50 +02:00
|
|
|
document.querySelectorAll('.nblayers').forEach(function(item) {
|
|
|
|
item.innerHTML = nblayers;
|
|
|
|
});
|
|
|
|
document.querySelector('#nblayers_text').classList.remove('d-none');
|
|
|
|
if(!nblayers) {
|
|
|
|
document.querySelector('#nblayers_text').classList.add('d-none');
|
|
|
|
}
|
2022-04-01 15:43:47 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
xhr.send();
|
|
|
|
};
|
|
|
|
|
2022-03-26 11:02:47 +01:00
|
|
|
var pageSignature = async function(url) {
|
2022-04-14 09:09:48 +02:00
|
|
|
document.querySelector('body').classList.add('bg-light');
|
2022-04-01 14:52:30 +02:00
|
|
|
modalSharing();
|
2022-03-26 11:02:47 +01:00
|
|
|
document.getElementById('page-upload').classList.add('d-none');
|
|
|
|
document.getElementById('page-signature').classList.remove('d-none');
|
2022-01-05 02:25:39 +01:00
|
|
|
fabric.Textbox.prototype._wordJoiners = /[]/;
|
|
|
|
menu = document.getElementById('sidebarTools');
|
|
|
|
menuOffcanvas = new bootstrap.Offcanvas(menu);
|
|
|
|
forceAddLock = !is_mobile();
|
|
|
|
addLock = forceAddLock;
|
|
|
|
|
|
|
|
if(localStorage.getItem('svgCollections')) {
|
|
|
|
svgCollections = JSON.parse(localStorage.getItem('svgCollections'));
|
|
|
|
}
|
|
|
|
|
|
|
|
opentype.load('/vendor/fonts/Caveat-Regular.ttf', function(err, font) {
|
|
|
|
fontCaveat = font;
|
|
|
|
});
|
2021-10-10 22:31:33 +02:00
|
|
|
|
2022-03-31 13:25:29 +02:00
|
|
|
let pdfBlob = null;
|
2022-04-01 00:52:44 +02:00
|
|
|
let filename = url.replace('/pdf/', '');
|
2022-03-31 13:25:29 +02:00
|
|
|
|
2022-03-31 11:53:55 +02:00
|
|
|
if(hash) {
|
2022-04-14 09:09:01 +02:00
|
|
|
let response = await fetch(url);
|
2022-03-31 11:53:55 +02:00
|
|
|
if(response.status != 200) {
|
|
|
|
return;
|
|
|
|
}
|
2022-03-31 13:25:29 +02:00
|
|
|
pdfBlob = await response.blob();
|
2022-04-01 00:52:44 +02:00
|
|
|
if(response.headers.get('Content-Disposition').match(/attachment; filename="/)) {
|
2022-04-02 01:54:21 +02:00
|
|
|
filename = response.headers.get('Content-Disposition').replace(/^[^"]*"/, "").replace(/"[^"]*$/, "").replace(/_signe-[0-9]+\x.pdf/, '.pdf');
|
2022-04-01 00:52:44 +02:00
|
|
|
}
|
2022-03-31 11:53:55 +02:00
|
|
|
} else {
|
2022-03-31 13:25:29 +02:00
|
|
|
pdfBlob = await getPDFBlobFromCache(url);
|
2022-03-31 11:53:55 +02:00
|
|
|
}
|
2022-04-01 00:52:44 +02:00
|
|
|
|
|
|
|
document.title = filename + ' - ' + document.title;
|
|
|
|
|
2022-03-26 11:02:47 +01:00
|
|
|
if(!pdfBlob) {
|
|
|
|
document.location = '/signature';
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-01-05 02:25:39 +01:00
|
|
|
createSignaturePad();
|
|
|
|
responsiveDisplay();
|
|
|
|
displaysSVG();
|
|
|
|
stateAddLock();
|
|
|
|
createEventsListener();
|
2022-03-26 11:02:47 +01:00
|
|
|
loadPDF(pdfBlob, filename);
|
|
|
|
};
|
2021-10-12 01:02:40 +02:00
|
|
|
|
2022-03-26 11:02:47 +01:00
|
|
|
(function () {
|
2022-04-27 00:41:44 +02:00
|
|
|
if(sharingMode) {
|
|
|
|
setTimeout(function() { runCron() }, 2000);
|
|
|
|
}
|
2022-03-31 11:53:55 +02:00
|
|
|
if(hash) {
|
|
|
|
pageSignature('/signature/'+hash+'/pdf');
|
2022-04-01 14:52:30 +02:00
|
|
|
window.addEventListener('hashchange', function() {
|
|
|
|
window.location.reload();
|
|
|
|
})
|
2022-03-31 11:53:55 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-03-26 11:02:47 +01:00
|
|
|
if(window.location.hash && window.location.hash.match(/^\#http/)) {
|
|
|
|
let hashUrl = window.location.hash.replace(/^\#/, '');
|
|
|
|
pageUpload();
|
|
|
|
uploadFromUrl(hashUrl);
|
|
|
|
} else if(window.location.hash) {
|
|
|
|
pageSignature('/pdf/'+window.location.hash.replace(/^\#/, ''));
|
|
|
|
} else {
|
|
|
|
pageUpload();
|
|
|
|
}
|
|
|
|
window.addEventListener('hashchange', function() {
|
|
|
|
window.location.reload();
|
|
|
|
})
|
2021-11-12 01:54:20 +01:00
|
|
|
})();
|