In: Computer Science
Swift -
Implement a simple text editor program that includes a text input field (with several lines), a text label, and two buttons, called "Open" and "Save".
When the button "Save" is touched, the app will save the contents of the text input field to a data file (sample.txt).
When the button "Open" is touched, the app will read the content from the data file, (sample.txt)) and feed the content into the text label.
Thank you.
<html>
<body>
<table>
<tr><td>Text Input: </td></tr>
<tr>
<td colspan="3">
<textarea id="inputTextToSave" cols="80"
rows="25"></textarea>
</td>
</tr>
<tr>
<td>Text Label:
<textarea id="inputTextToSave"></textarea>
</td>
</tr>
<tr>
<td>Filename to Save As:</td>
<td><input
id="inputFileNameToSaveAs"></input></td>
<td><button
onclick="saveTextAsFile()">Save</button></td>
</tr>
<tr>
<td>Select a File to Load:</td>
<td><input type="file"
id="fileToLoad"></td>
<td><button
onclick="loadFileAsText()">Open</button><td>
</tr>
</table>
<script type="text/javascript">
function saveTextAsFile()
{
var textToSave =
document.getElementById("inputTextToSave").value;
var textToSaveAsBlob = new Blob([textToSave],
{type:"text/plain"});
var textToSaveAsURL =
window.URL.createObjectURL(textToSaveAsBlob);
var fileNameToSaveAs =
document.getElementById("inputFileNameToSaveAs").value;
var downloadLink = document.createElement("a");
downloadLink.download = fileNameToSaveAs;
downloadLink.innerHTML = "Download File";
downloadLink.href = textToSaveAsURL;
downloadLink.onclick = destroyClickedElement;
downloadLink.style.display = "none";
document.body.appendChild(downloadLink);
downloadLink.click();
}
function destroyClickedElement(event)
{
document.body.removeChild(event.target);
}
function loadFileAsText()
{
var fileToLoad =
document.getElementById("fileToLoad").files[0];
var fileReader = new FileReader();
fileReader.onload = function(fileLoadedEvent)
{
var textFromFileLoaded = fileLoadedEvent.target.result;
document.getElementById("inputTextToSave").value =
textFromFileLoaded;
};
fileReader.readAsText(fileToLoad, "UTF-8");
}
</script>
</body>
</html>