mirror of
https://github.com/24eme/signaturepdf.git
synced 2023-08-25 09:33:08 +02:00
Refacto de l'interface pour qu'ajout d'élement de signature soit plus
générique, à l'aide d'une modal
This commit is contained in:
parent
b76395074a
commit
f7db55cb75
115
public/js/app.js
115
public/js/app.js
@ -14,31 +14,77 @@ loadingTask.promise.then(function(pdf) {
|
||||
var copiedObject = null;
|
||||
var activeCanvas = null;
|
||||
var activeCanvasPointer = null;
|
||||
var canvasEditions = [];
|
||||
var svgCollections = [];
|
||||
|
||||
opentype.load('/vendor/fonts/Caveat-Regular.ttf', function(err, font) {
|
||||
fontCaveat = font;
|
||||
});
|
||||
|
||||
|
||||
var displaysSVG = function() {
|
||||
document.getElementById('svg_list').innerHTML = "";
|
||||
svgCollections.forEach((svg, i) => {
|
||||
var inputRadio = document.createElement('input');
|
||||
inputRadio.type = "radio";
|
||||
inputRadio.classList.add("btn-check");
|
||||
inputRadio.id="radio_svg_"+i;
|
||||
inputRadio.name = "svg_2_add";
|
||||
inputRadio.autocomplete = "off";
|
||||
inputRadio.value = svg;
|
||||
var svgButton = document.createElement('label');
|
||||
svgButton.classList.add('btn');
|
||||
svgButton.classList.add('btn-lg');
|
||||
svgButton.classList.add('btn-outline-secondary');
|
||||
svgButton.htmlFor = "radio_svg_"+i;
|
||||
var svgImg = document.createElement('img');
|
||||
svgImg.src = svg;
|
||||
svgImg.style = "max-width: 180px;max-height: 70px;";
|
||||
svgButton.appendChild(svgImg);
|
||||
document.getElementById('svg_list').appendChild(inputRadio);
|
||||
document.getElementById('svg_list').appendChild(svgButton);
|
||||
});
|
||||
}
|
||||
|
||||
displaysSVG();
|
||||
|
||||
document.getElementById('btn_modal_ajouter').addEventListener('click', function() {
|
||||
if(document.getElementById('nav-draw-tab').classList.contains('active')) {
|
||||
svgCollections.push(signaturePad.toDataURL("image/svg+xml"));
|
||||
}
|
||||
if(document.getElementById('nav-type-tab').classList.contains('active')) {
|
||||
var fontPath = fontCaveat.getPath(document.getElementById('input-text-signature').value, 0, 0, 42);
|
||||
var fabricPath = new fabric.Path(fontPath.toPathData());
|
||||
fabricPath.top = 0;
|
||||
fabricPath.left = 0;
|
||||
fabricPath.height = fabricPath.getScaledHeight();
|
||||
var textCanvas = document.createElement('canvas');
|
||||
textCanvas.width = fabricPath.getScaledWidth();
|
||||
textCanvas.height = fabricPath.getScaledHeight();
|
||||
var textCanvas = new fabric.Canvas(textCanvas);
|
||||
textCanvas.add(fabricPath).renderAll();
|
||||
svgCollections.push("data:image/svg+xml;base64,"+btoa(textCanvas.toSVG()));
|
||||
}
|
||||
if(document.getElementById('nav-import-tab').classList.contains('active')) {
|
||||
svgCollections.push(document.getElementById('img-upload').src);
|
||||
}
|
||||
displaysSVG();
|
||||
});
|
||||
|
||||
var signaturePad = new SignaturePad(document.getElementById('signature-pad'), {
|
||||
backgroundColor: 'rgba(255, 255, 255, 0)',
|
||||
penColor: 'rgb(0, 0, 0)',
|
||||
minWidth: 0.75,
|
||||
maxWidth: 1.1,
|
||||
onEnd: function() {
|
||||
document.getElementById('radio_signature_pad').checked = true;
|
||||
}
|
||||
maxWidth: 1.1
|
||||
});
|
||||
|
||||
document.getElementById('input-text-signature').addEventListener('keypress', function(event) {
|
||||
document.getElementById('radio_signature_text').checked = true;
|
||||
});
|
||||
|
||||
document.getElementById('input-signature-text-classic').addEventListener('keypress', function(event) {
|
||||
document.getElementById('radio_signature_text_classic').checked = true;
|
||||
});
|
||||
|
||||
var svgImage = null;
|
||||
|
||||
document.getElementById('modalAddSvg').addEventListener('hidden.bs.modal', function (event) {
|
||||
signaturePad.clear();
|
||||
document.getElementById('input-text-signature').value = null;
|
||||
document.getElementById('input-image-upload').value = null;
|
||||
document.getElementById('img-upload').src = null;
|
||||
document.getElementById('img-upload').classList.add("d-none");
|
||||
})
|
||||
|
||||
document.getElementById('input-image-upload').addEventListener('change', function(event) {
|
||||
var data = new FormData();
|
||||
data.append('file', document.getElementById('input-image-upload').files[0]);
|
||||
@ -47,8 +93,7 @@ loadingTask.promise.then(function(pdf) {
|
||||
|
||||
xhr.open( 'POST', document.getElementById('form-image-upload').action, true );
|
||||
xhr.onreadystatechange = function () {
|
||||
svgImage = "data:image/svg+xml;base64,"+btoa(this.responseText);
|
||||
document.getElementById('radio_signature_image').checked = true;
|
||||
var svgImage = "data:image/svg+xml;base64,"+btoa(this.responseText);
|
||||
document.getElementById('img-upload').src = svgImage;
|
||||
document.getElementById('img-upload').classList.remove("d-none");
|
||||
};
|
||||
@ -57,12 +102,10 @@ loadingTask.promise.then(function(pdf) {
|
||||
event.preventDefault();
|
||||
});
|
||||
|
||||
var canvasEditions = [];
|
||||
|
||||
document.getElementById('save').addEventListener('click', function(event) {
|
||||
canvasEditions.forEach(function(canvasEdition, index) {
|
||||
document.getElementById('data-svg-'+index).value = canvasEdition.toSVG();
|
||||
})
|
||||
})
|
||||
});
|
||||
|
||||
document.addEventListener('keydown', function(event) {
|
||||
@ -125,19 +168,8 @@ loadingTask.promise.then(function(pdf) {
|
||||
canvasEdition.on('mouse:dblclick', function(event) {
|
||||
x = event.pointer.x
|
||||
y = event.pointer.y
|
||||
|
||||
var svg2add = null;
|
||||
|
||||
if(document.getElementById('radio_signature_pad').checked) {
|
||||
svg2add = signaturePad.toDataURL("image/svg+xml");
|
||||
}
|
||||
|
||||
if(document.getElementById('radio_signature_image').checked) {
|
||||
svg2add = svgImage;
|
||||
}
|
||||
|
||||
if(svg2add) {
|
||||
fabric.loadSVGFromURL(svg2add, function(objects, options) {
|
||||
if(document.querySelector('input[name="svg_2_add"]:checked')) {
|
||||
fabric.loadSVGFromURL(document.querySelector('input[name="svg_2_add"]:checked').value, function(objects, options) {
|
||||
var svg = fabric.util.groupSVGElements(objects, options);
|
||||
svg.scaleToHeight(100);
|
||||
svg.top = y - (svg.getScaledHeight() / 2);
|
||||
@ -145,32 +177,25 @@ loadingTask.promise.then(function(pdf) {
|
||||
canvasEdition.add(svg).renderAll();
|
||||
});
|
||||
}
|
||||
|
||||
if(document.getElementById('radio_signature_text').checked) {
|
||||
var fontPath = fontCaveat.getPath(document.getElementById('input-text-signature').value, 0, 0, 42);
|
||||
var fabricPath = new fabric.Path(fontPath.toPathData());
|
||||
fabricPath.top = y - (fabricPath.getScaledHeight() / 2);
|
||||
fabricPath.left = x - (fabricPath.getScaledWidth() / 2);
|
||||
canvasEdition.add(fabricPath).renderAll();
|
||||
}
|
||||
|
||||
if(document.getElementById('radio_signature_text_classic').checked) {
|
||||
/*if(document.getElementById('radio_signature_text_classic').checked) {
|
||||
var textSignature = new fabric.Text(document.getElementById('input-signature-text-classic').value, { fontSize: 16 });
|
||||
textSignature.top = y - (textSignature.getScaledHeight() / 2);
|
||||
textSignature.left = x - (textSignature.getScaledWidth() / 2);
|
||||
canvasEdition.add(textSignature).renderAll();
|
||||
}
|
||||
}*/
|
||||
});
|
||||
|
||||
canvasEditions.push(canvasEdition);
|
||||
|
||||
var renderContext = {
|
||||
canvasContext: context,
|
||||
viewport: viewport
|
||||
viewport: viewport,
|
||||
enhanceTextSelection: true
|
||||
};
|
||||
var renderTask = page.render(renderContext);
|
||||
renderTask.promise.then(function () {
|
||||
console.log('Page rendered');
|
||||
|
||||
});
|
||||
});
|
||||
}
|
||||
|
@ -22,26 +22,10 @@
|
||||
<div class="row">
|
||||
<div id="container-pages" class="col-lg-10 col-md-9 col-sm-8 col-xs-6 bg-light text-center"></div>
|
||||
<aside class="col-lg-2 col-md-3 col-sm-4 col-xs-6 mt-2 position-fixed end-0 bg-white">
|
||||
<div class="form-check form-switch float-end"><input class="form-check-input" type="radio" name="radio_signature" id="radio_signature_pad"></div>
|
||||
<h5><i class="bi bi-vector-pen"></i> À main levée</h5>
|
||||
<canvas id="signature-pad" class="border bg-light" width=235 height=125></canvas>
|
||||
<hr />
|
||||
<div class="form-check form-switch float-end"><input class="form-check-input" type="radio" name="radio_signature" id="radio_signature_text"></div>
|
||||
<h5><i class="bi bi-fonts"></i> Texte à la main</h5>
|
||||
<input id="input-text-signature" type="text" class="form-control" placeholder="Ma signature" style="font-family: Caveat; font-size: 24px;" />
|
||||
<hr />
|
||||
<div class="form-check form-switch float-end"><input class="form-check-input" type="radio" name="radio_signature" id="radio_signature_image"></div>
|
||||
<h5><i class="bi bi-image"></i> Image</h5>
|
||||
<div class="text-center">
|
||||
<img id="img-upload" class="d-none" style="max-height: 80px; max-width: 235px;" src="" />
|
||||
<div id="svg_list" class="d-grid gap-2"></div>
|
||||
<div class="d-grid gap-2 mt-3">
|
||||
<button type="button" class="btn btn-secondary" data-bs-toggle="modal" data-bs-target="#modalAddSvg"><i class="bi bi-plus-circle"></i> Ajouter un élément</button>
|
||||
</div>
|
||||
<form id="form-image-upload" action="/image2svg" method="POST" enctype="multipart/form-data">
|
||||
<input id="input-image-upload" class="form-control" name="image" type="file">
|
||||
</form>
|
||||
<hr />
|
||||
<div class="form-check form-switch float-end"><input class="form-check-input" type="radio" name="radio_signature" id="radio_signature_text_classic"></div>
|
||||
<h5><i class="bi bi-type"></i> Texte classique</h5>
|
||||
<input id="input-signature-text-classic" type="text" class="form-control" placeholder="" />
|
||||
<hr />
|
||||
<p><small class="text-muted"><i class="bi bi-hand-index"></i><i class="bi bi-hand-index"></i> <i class="bi bi-plus-circle-fill"></i> Double-cliquez sur le PDF pour ajouter l'élément sélectionné</small></p>
|
||||
<form id="form_pdf" action="/<?php echo $key ?>/save" method="post">
|
||||
@ -53,6 +37,43 @@
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="modal fade" id="modalAddSvg" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
|
||||
<div class="modal-dialog">
|
||||
<div class="modal-content">
|
||||
<div class="modal-body">
|
||||
<nav>
|
||||
<div class="nav nav-tabs" id="nav-tab" role="tablist">
|
||||
<button class="nav-link active" id="nav-draw-tab" data-bs-toggle="tab" data-bs-target="#nav-draw" type="button" role="tab" aria-controls="nav-draw" aria-selected="true"><i class="bi bi-vector-pen"></i> Dessiner</button>
|
||||
<button class="nav-link" id="nav-type-tab" data-bs-toggle="tab" data-bs-target="#nav-type" type="button" role="tab" aria-controls="nav-type" aria-selected="false"><i class="bi bi-fonts"></i> Saisir</button>
|
||||
<button class="nav-link" id="nav-import-tab" data-bs-toggle="tab" data-bs-target="#nav-import" type="button" role="tab" aria-controls="nav-import" aria-selected="false"><i class="bi bi-image"></i> Importer</button>
|
||||
</div>
|
||||
</nav>
|
||||
<div class="tab-content mt-3" id="nav-tabContent">
|
||||
<div class="tab-pane fade show active" id="nav-draw" role="tabpanel" aria-labelledby="nav-draw-tab">
|
||||
<canvas id="signature-pad" class="border bg-light" width="462" height="175"></canvas>
|
||||
</div>
|
||||
<div class="tab-pane fade" id="nav-type" role="tabpanel" aria-labelledby="nav-type-tab">
|
||||
<input id="input-text-signature" type="text" class="form-control form-control-lg" placeholder="Ma signature" style="font-family: Caveat; font-size: 48px;" />
|
||||
</div>
|
||||
<div class="tab-pane fade" id="nav-import" role="tabpanel" aria-labelledby="nav-import-tab">
|
||||
<div class="text-center">
|
||||
<img id="img-upload" class="d-none" style="max-width: 460px;" src="" />
|
||||
</div>
|
||||
<form id="form-image-upload" action="/image2svg" method="POST" enctype="multipart/form-data">
|
||||
<input id="input-image-upload" class="form-control" name="image" type="file">
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="modal-footer">
|
||||
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Annuler</button>
|
||||
<button id="btn_modal_ajouter" type="button" class="btn btn-primary" data-bs-dismiss="modal">Ajouter</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.1/dist/js/bootstrap.min.js" integrity="sha384-skAcpIdS7UcVUC05LJ9Dxay8AXcDYfBJqt1CJ85S/CFujBsIzCIv+l9liuYLaMQ/" crossorigin="anonymous"></script>
|
||||
<script src="/vendor/pdf.js?legacy"></script>
|
||||
<script src="/vendor/fabric.min.js?4.4.0"></script>
|
||||
<script src="/vendor/signature_pad.umd.min.js?3.0.0-beta.3"></script>
|
||||
|
Loading…
Reference in New Issue
Block a user