Add external link support

Button bar added to bottom right with styling for buttons. Index added that
writes decoded HTML to the DOM to render a page from a link. Button added to
base64 encode a page and create and open a link to view it in a new tab
This commit is contained in:
Jacob Strieb 2019-06-30 00:57:20 -07:00
parent 84af590ed7
commit 60a3aa13d4
3 changed files with 60 additions and 12 deletions

3
.gitignore vendored
View File

@ -1,3 +1,6 @@
# Vim temporary swap files
*.swp
*.swo
# Backed up copies of old files
*.bak

View File

@ -32,6 +32,20 @@ iframe {
textarea, iframe {
border: 0.5px solid;
}
a, button {
background: black;
color: limegreen;
text-decoration: none;
padding: 2px 5px;
border: none;
}
#buttons {
position: absolute;
bottom: 0;
right: 0;
}
</style>
<script type="text/javascript">
@ -52,17 +66,8 @@ function initialize() {
}
/* Run each time a key is pressed on a text box */
function update() {
var data = {
"css" : document.getElementById("css").value,
"js" : document.getElementById("javascript").value,
"html" : document.getElementById("html").value
};
// Save page data to the URL
window.location.hash = "#" + window.btoa(JSON.stringify(data));
/* Return the HTML string for the page */
function getHTML(data) {
// Generate an HTML page from the contents of each <textarea>
var pageData =
`
@ -82,8 +87,34 @@ ${data["html"]}
</body>
`;
return pageData;
}
/* Return a link to view the page */
function getViewLink(pageData) {
return `../#${window.btoa(encodeURIComponent(pageData))}`
}
/* Run each time a key is pressed on a text box */
function update() {
var data = {
"css" : document.getElementById("css").value,
"js" : document.getElementById("javascript").value,
"html" : document.getElementById("html").value
};
var html = getHTML(data);
// Save encoded page data to the URL
window.location.hash = "#" + window.btoa(JSON.stringify(data));
// Update the URL for the "Get Link" button
document.getElementById("getLinkLink").href = getViewLink(html);
// Update the <iframe> to display the generated page
window.frames[0].location.replace(`data:text/html,${encodeURIComponent(pageData)}`);
window.frames[0].location.replace(`data:text/html,${encodeURIComponent(html)}`);
}
</script>
</head>
@ -94,6 +125,10 @@ ${data["html"]}
<textarea id="html" placeholder="HTML" rows="9"></textarea><textarea id="css" placeholder="CSS" rows="9"></textarea><textarea id="javascript" placeholder="JavaScript" rows="9"></textarea>
</div>
<div id="buttons">
<button><a id="getLinkLink" href="" target="_blank">Get Link</a></button>
</div>
<iframe></iframe>
</body>
</html>

View File

@ -0,0 +1,10 @@
<noscript>JavaScript is required to convert the URL into a usable web page.</noscript>
<script type="text/javascript">
if (window.location.hash) {
var hash = window.location.hash.slice(1);
var data = atob(hash);
document.write(decodeURIComponent(data));
} else {
window.location.replace("./editor");
}
</script>