UI Output:
Html :
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div id="overview"></div>
<script src="/jsProject/save-text-as-file.js"></script>
</body>
</html>
Js Script:
class SaveWrapper{
constructor(){
//Html wrapper
this.html = document.getElementById('overview');
this.parent = document.createElement('div');
this.parent.style.cssText = 'background: #cfcfcf; padding: 20px; width: 350px; margin: 0 auto;';
this.html.appendChild(this.parent);
this.wrapper = document.createElement('div');
this.parent.appendChild(this.wrapper);
// Ui wrapper
this.contectArea = document.createElement('div');
this.contectArea.style.cssText = 'border: 1px solid #fff; padding: 20px; border-radius: 3px; margin-bottom: 25px';
// Ui Textarea
this.contectAreaInput = document.createElement('textarea');
this.contectAreaInput.style.cssText = 'width: calc(100% - 38px); height:150px; padding: 20px; border:none; outline: none; resize: none';
this.contectArea.appendChild(this.contectAreaInput);
this.wrapper.appendChild(this.contectArea);
this.contectAreaInput.addEventListener("input", ()=>{
this.carCount.innerText = this.contectAreaInput.value.length;
});
// textarea word count
this.carCount = document.createElement('span');
this.contectArea.appendChild(this.carCount);
// UI elements file name and select save options
this.clientWeapper = document.createElement('div');
this.clientWeapper.style.cssText = 'display: flex; margin-top:10px; margin-bottom:30px; align-items:center; justify-content:center';
// File name
this.nameWeapper = document.createElement('div');
this.nameWeapper.style.cssText = 'width: calc(100% / 2 - 8px); margin-right: 40px;';
this.nameLabel = document.createElement('label');
this.nameLabel.innerText = 'File Name';
this.nameWeapper.appendChild(this.nameLabel);
// File name input
this.nameInput = document.createElement('input');
this.nameInput.style.cssText = 'display: block; width: 100%; padding: 10px';
this.nameInput.type = 'text';
this.nameInput.placeholder = 'Enter file name';
this.nameWeapper.appendChild(this.nameInput);
this.clientWeapper.appendChild(this.nameWeapper);
// File save options
this.saveWrapper = document.createElement('div');
this.saveWrapper.style.cssText = 'width: calc(100% / 2 - 12px';
this.saveLable = document.createElement('label');
this.saveLable.innerText = 'Save as';
this.saveWrapper.appendChild(this.saveLable);
this.saveInputValues = [
{id:'text/plain', name:'TEXT File (.txt)'},
{id:'text/javascript', name:'JS File (.js)'},
{id:'text/html', name:'HTML File (.html)'},
{id:'application/xml', name:'XML File (.xml)'},
{id:'application/json', name:'JSON File (.json)'},
{id:'application/msword', name:'DOC File (.doc)'}
];
this.saveInput = document.createElement('select');
// dynamic option selection data
for(let i = 0; i < this.saveInputValues.length; i++){
this.saveInputOption = document.createElement('option');
this.saveInputOptionTxt = document.createTextNode(this.saveInputValues[i].name);
this.saveInputOption.appendChild(this.saveInputOptionTxt);
this.idNames = this.saveInputValues[i].name;
this.ids = this.idNames.split(' ');
this.ids.pop();
this.ikd = this.ids.join('-');
this.saveInputOption.setAttribute('id', this.ikd);
this.saveInputOption.setAttribute('value', this.saveInputValues[i].id);
this.saveInput.style.cssText = 'display: block; width: 100%; padding: 10px';
this.saveInput.setAttribute('title', 'File selection');
this.saveInput.appendChild(this.saveInputOption);
};
this.saveInput.addEventListener('change', () => {
this.selectValue = this.saveInput.options[this.saveInput.selectedIndex].text;
this.selectText = this.selectValue.split(' ');
this.selectText.pop(' ');
this.text = this.selectText.join(' ');
this.saveBtn.innerText = `Save as ${this.text}`;
});
this.saveWrapper.appendChild(this.saveInput);
this.clientWeapper.appendChild(this.saveWrapper);
this.parent.appendChild(this.clientWeapper);
// Save wrapper
this.saveBtn = document.createElement("button");
this.saveBtn.setAttribute("type", "submit");
this.saveBtn.setAttribute("id", "saveBtn");
this.saveBtn.innerText = `Save`;
this.saveBtn.style.cssText = "display: block; width: 100%; padding: 10px; font-size: 20px; cursor: pointer; background-color: green;";
this.parent.appendChild(this.saveBtn);
// File name required
this.errorText= document.createElement('label');
this.errorText.innerText = ' **Please enter a name';
this.errorText.style.color = ' red';
// Save Btn clicked
this.saveBtn.addEventListener("click", () => {
if(!this.nameInput.value) {
this.nameLabel.append(this.errorText);
} else {
this.errorText.remove();
this.blob = new Blob([this.contectAreaInput.value], {type: this.saveInput.value});
this.fileUrl = URL.createObjectURL(this.blob);
this.link = document.createElement('a');
this.link.href = this.fileUrl;
this.link.download = this.nameInput.value;
this.link.click( );
this.nameInput.value = null;
};
});
}
}
const wrapper = new SaveWrapper();