Fixes bug with unfinished filename extension (multiple dots problem)

This commit is contained in:
Dimitrios Kaitantzidis 2023-10-15 19:03:10 +03:00
parent d0ce7db9ee
commit 49f1f4e7c7
2 changed files with 22 additions and 3 deletions

View file

@ -17,6 +17,7 @@ class PdfContainer {
this.exportPdf = this.exportPdf.bind(this); this.exportPdf = this.exportPdf.bind(this);
this.updateFilename = this.updateFilename.bind(this); this.updateFilename = this.updateFilename.bind(this);
this.setDownloadAttribute = this.setDownloadAttribute.bind(this); this.setDownloadAttribute = this.setDownloadAttribute.bind(this);
this.preventIllegalChars = this.preventIllegalChars.bind(this);
this.pdfAdapters = pdfAdapters; this.pdfAdapters = pdfAdapters;
@ -36,6 +37,7 @@ class PdfContainer {
const downloadBtn = document.getElementById('export-button'); const downloadBtn = document.getElementById('export-button');
filenameInput.onkeyup = this.updateFilename; filenameInput.onkeyup = this.updateFilename;
filenameInput.onkeydown = this.preventIllegalChars;
filenameInput.disabled = true; filenameInput.disabled = true;
filenameInput.innerText = ""; filenameInput.innerText = "";
downloadBtn.disabled = true; downloadBtn.disabled = true;
@ -231,7 +233,14 @@ class PdfContainer {
let inputArr = filenameInput.value.split('.'); let inputArr = filenameInput.value.split('.');
if (inputArr !== null && inputArr !== undefined && inputArr.length > 0) { if (inputArr !== null && inputArr !== undefined && inputArr.length > 0) {
filenameInput.value = inputArr[0];
inputArr = inputArr.filter(n => n); // remove all empty strings, nulls or undefined
if (inputArr.length > 1) {
inputArr.pop(); // remove right part after last dot
}
filenameInput.value = inputArr.join();
this.filename = filenameInput.value; this.filename = filenameInput.value;
} }
@ -275,7 +284,17 @@ class PdfContainer {
downloadBtn.disabled = false; downloadBtn.disabled = false;
this.filename = filenameInput.value; this.filename = filenameInput.value;
filenameParagraph.innerText = this.filename + ".pdf"; }
preventIllegalChars(e) {
// const filenameInput = document.getElementById('filename-input');
//
// filenameInput.value = filenameInput.value.replace('.pdf', '');
//
// // prevent .
// if (filenameInput.value.includes('.')) {
// filenameInput.value.replace('.','');
// }
} }
} }