-
Notifications
You must be signed in to change notification settings - Fork 129
/
editor.js
128 lines (95 loc) · 2.89 KB
/
editor.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/**
* editor/editor.js: the main code that runs what is referred to as the "editor"
* in the documentation
*/
/***
* Helper functions
***/
/* Return the HTML string for the page */
function getHTML(data) {
// Generate an HTML page from the contents of each <textarea>
var pageData =
`
<!DOCTYPE html>
<head>
<style>
${data["css"]}
</style>
<script type="text/javascript">
${data["js"]}
</scr` +
// This has to be broken up because otherwise it is recognized as the main
// document's end script tag
`ipt>
</head>
<body>
${data["html"]}
</body>
`;
return pageData;
}
/* Return a link to view the page */
function getViewLink(pageData) {
return `http://jstrieb.github.io/urlpages/#${window.btoa(pageData)}`;
}
/***
* Button press functions
***/
/* Set the TinyUrl form hidden 'url' field to the view URL */
function setViewUrl() {
var data = {
"css" : document.getElementById("css").value,
"js" : document.getElementById("javascript").value,
"html" : document.getElementById("html").value
};
var html = encodeURIComponent(getHTML(data));
// Update the URL for the "Short Link" button
document.getElementById("url").value = getViewLink(html);
}
/* Set the TinyUrl form hidden 'url' field to the code URL */
function setCodeUrl() {
document.getElementById("url").value = window.location.href;
}
/* Show a prompt with the HTML page data so the user can copy the code */
function showCopyCodePrompt() {
var data = {
"css" : document.getElementById("css").value,
"js" : document.getElementById("javascript").value,
"html" : document.getElementById("html").value
};
var html = getHTML(data);
window.prompt("Copy to clipboard: ", html)
}
/***
* Main procedure functions
***/
/* Run once when the page is loaded */
function initialize() {
// Get page data from the URL and load it into the boxes
if (window.location.hash) {
var b64 = window.location.hash.slice(1);
var json = window.atob(b64);
var data = JSON.parse(json);
document.getElementById("css").value = data["css"];
document.getElementById("javascript").value = data["js"];
document.getElementById("html").value = data["html"];
}
update();
}
/* 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 = encodeURIComponent(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 download link
document.getElementById("downloadLink").href = `data:text/html,${html}`
// Update the <iframe> to display the generated page
window.frames[0].location.replace(`data:text/html,${html}`);
}