forked from LoneFry/QuickEditor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQuickEditor.htm
282 lines (259 loc) · 14.7 KB
/
QuickEditor.htm
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<!--
/******************************************************************************
* QuickEditor
* A useful tool for making quick and dirty tests of HTML, CSS, and JavaScript
*
* License: CC BY-NC-SA
* Creative Commons Attribution-NonCommercial-ShareAlike
* http://creativecommons.org/licenses/by-nc-sa/3.0/
*
* 2005?: initial IE version
* 2007-4-30: cross browser version
* 2007-5-21: improved range finding for multiline tab
* 2011-5-25: altered backfeed to grab <html>.outer instead of <body>.inner
* 2012-2-19: altered style for readability: monospace font, tab-size, color
* 2012-2-20: Add cross browser buffer saving, remove related alerts
*
* If you find an error, or improvement, let me know: dev at lonefry dot com
*
* Quirks mode to support styling of textarea and iframe for height and width
* The Code validates, but since I'm in quirks mode, I don't badge that
*****************************************************************************/
-->
<head>
<title>Quick Editor</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="description" content="A useful tool for making quick and dirty tests of HTML, CSS, and JavaScript">
<meta name="keywords" content="Development, HTML, XHTML, DHTML, CSS, javascript, quick, edit">
<style type="text/css">
body {margin:0;padding:0;border:0;}
table#cheeseyTableLayout{height:100%;width:100%;}
div#quickEditor {height:100%}
div.QE_left #one{margin:0;padding:0;border:0;width:49.9%;height:100%;float:left ;}
div.QE_left #two{margin:0;padding:0;border:0;width:50% ;height:100%;float:right;}
div.QE_top #one{margin:0;padding:0;border:0;width:100% ;height:50% ; }
div.QE_top #two{margin:0;padding:0;border:0;width:100% ;height:50% ; }
div.QE_right #one{margin:0;padding:0;border:0;width:49.9%;height:100%;float:right;}
div.QE_right #two{margin:0;padding:0;border:0;width:50% ;height:100%;float:left ;}
#QE_body{margin:0;padding:0;border:0;width:100%;height:100%;background-color:#000;color:#ffc;
font:10pt monospace;-moz-tab-size:4;-o-tab-size:4;-webkit-tab-size:4;tab-size:4;}
#QE_demo{margin:0;padding:0;border:0;width:100%;height:100%;background-color:#fff;}
</style>
<!--[if IE]>
<script type="text/javascript">var bIE=true;</script>
<![endif]-->
<script type="text/javascript"><!--
if (!bIE) var bIE=false;
bBackFeed=false;
bUpdate=true;
function QE_update() {
//to hack around an IE quirk, I am making the gross assumption
// that no one would have a  in their text
document.getElementById('QE_demo').contentWindow.document.designMode=bBackFeed?'on':'off';
QE_demo.document.open();
QE_demo.document.write(document.getElementById('QE_body').value.replace(/\034/g,''));
QE_demo.document.close();
if (bBackFeed) {
if (!bIE) {
document.getElementById('QE_demo').contentWindow.addEventListener("keyup", QE_backFeed, true);
} else {
document.getElementById('QE_demo').contentWindow.document.onkeyup=QE_backFeed;
}
}
}
function QE_backFeed() {
if (!bBackFeed) return;
//document.getElementById('QE_body').value=document.getElementById('QE_demo').contentWindow.document.body.innerHTML;
document.getElementById('QE_body').value=document.getElementById('QE_demo').contentWindow.document.body.parentNode.outerHTML;
}
function QE_savefile() {
var d = document.getElementById('QE_demo').contentWindow.document;
d.open( 'text/html', 'replace' );
QE_demo.document.write(document.getElementById('QE_body').value.replace(/\034/g,''));
d.close();
if (bIE) {
d.execCommand('SaveAs', null, 'edit_'+round(new Date()/1000)+'.htm');
} else {
QE_demo.location='data:application/octet-stream,'+encodeURIComponent(document.getElementById('QE_body').value.replace(/\034/g,''));
}
}
function IE_setSelectionRange(oTextarea, iStart, iEnd){
var oRng = oTextarea.createTextRange();
oRng.collapse(true);
oRng.moveStart("character", iStart);
oRng.moveEnd("character", iEnd - iStart);
oRng.select();
}
function IE_hackPara(oTextarea) {
//to hack around an IE quirk, I am making the gross assumption
// that no one would have a  in their text
if (!bIE)return;
if (oTextarea.value.charCodeAt(oTextarea.value.length-1)==13
|| oTextarea.value.charCodeAt(oTextarea.value.length-1)==10) {
oTextarea.value=oTextarea.value.replace(/\034/g,'')+String.fromCharCode(28);
IE_setSelectionRange(oTextarea,oTextarea.value.length-2,oTextarea.value.length-2);
}
}
function IE_unHackPara(oTextarea) {
//to hack around an IE quirk, I am making the gross assumption
// that no one would have a  in their text
if (!bIE)return;
oTextarea.value=oTextarea.value.replace(/\034/g,'');
}
function QE_catchKey(oTextarea,event) {
event=event||window.event;
var sTab='\t';
if (bIE) {
//the below method almost worked, except that IE likes to
//ignore trailing linebreaks, that's why I quietly add the
// FIRST thing in this function...or on keyup
var oRng = document.selection.createRange();
var oRng2 = oRng.duplicate();
oRng2.moveToElementText(oTextarea);
oRng2.setEndPoint('StartToEnd', oRng);
iEnd = oTextarea.value.length-oRng2.text.length;
oRng2.setEndPoint('StartToStart', oRng);
iStart = oTextarea.value.length-oRng2.text.length;
//handle tabs IE
if (9 == event.keyCode) {
oTextarea.focus();
var sNewText=sTab;
var aText=oTextarea.value.substring(iStart,iEnd).split('\n');
if (aText.length > 1) { //multi-line selection!
iStart = oTextarea.value.lastIndexOf('\n',iStart+1);
if (iStart == -1) iStart=0;
if (oTextarea.value.charCodeAt(iEnd)==13)iEnd-=1;
var sBefore=oTextarea.value.substring(0, iStart);
var sOldText=oTextarea.value.substring(iStart,iEnd);
var sAfter=oTextarea.value.substring(iEnd);
aText=sOldText.split('\n');
if (event.shiftKey) {
sNewText=sOldText.replace(new RegExp('\n'+sTab,'g'),'\n').replace(new RegExp('^'+sTab,''),'');
} else {
sNewText=(iStart==0?sTab:'')+aText.join('\n'+sTab);
}
oTextarea.value = sBefore + sNewText + sAfter;
IE_setSelectionRange(oTextarea, iStart-(sBefore.split('\n').length-1)
,iStart+sNewText.length-((sBefore+sNewText).split('\n').length-1));
} else {
oRng.text=sTab;
oRng.select();
}
return false;
} else
//handle returns IE
if (13 == event.keyCode) {
if (iStart==0) return true;
if (oTextarea.value.substr(iStart-1,0).charCodeAt(0) == 13) return true;
var iPrevLine = oTextarea.value.substring(0, iStart).lastIndexOf('\n');
if (iPrevLine == -1) iPrevLine=0;
var aBits=/^\n?([\t ]*)[^\t ]?/.exec(oTextarea.value.substring(iPrevLine, iStart));
if (!aBits || aBits[1].length==0)return true;
var sBefore=oTextarea.value.substring(0, iStart);
oTextarea.value = sBefore
+'\n'+aBits[1]
+oTextarea.value.substring(iEnd);
IE_setSelectionRange(oTextarea, (sBefore+aBits[1]).length+2-sBefore.split('\r').length
,(sBefore+aBits[1]).length+2-sBefore.split('\r').length);
return false;
}
} else {
var iStart = oTextarea.selectionStart;
var iEnd = oTextarea.selectionEnd;
//handle tabs FF
if (9 == event.which) {
oTextarea.focus();
var sNewText=sTab;
var aText=oTextarea.value.substring(iStart,iEnd).split('\n');
if (aText.length > 1) { //multi-line selection!
iStart = oTextarea.value.lastIndexOf('\n',iStart+1);
if (iStart == -1) iStart=0;
if (oTextarea.value.charCodeAt(iEnd)==10)iEnd-=1;
var sBefore=oTextarea.value.substring(0, iStart);
var sOldText=oTextarea.value.substring(iStart,iEnd);
var sAfter=oTextarea.value.substring(iEnd);
aText=sOldText.split('\n');
if (event.shiftKey) {
sNewText=sOldText.replace(new RegExp('\n'+sTab,'g'),'\n').replace(new RegExp('^'+sTab,''),'');
} else {
sNewText=(iStart==0?sTab:'')+aText.join('\n'+sTab);
}
oTextarea.value = sBefore + sNewText + sAfter;
oTextarea.setSelectionRange(iStart,iStart+sNewText.length);
} else {
oTextarea.value = oTextarea.value.substring(0, iStart)
+sTab
+oTextarea.value.substring(iEnd);
oTextarea.setSelectionRange(iStart+sTab.length,iStart+sTab.length);
}
return false;
} else
//handle returns FF
if (13 == event.which) {
if (iStart==0) return true;
var iPrevLine = oTextarea.value.substring(0, iStart).lastIndexOf('\n');
if (iPrevLine == -1) iPrevLine=0;
var aBits=/^\n?([\t ]*)[^\t ]?/.exec(oTextarea.value.substring(iPrevLine, iStart));
if (!aBits || aBits[1].length==0)return true;
oTextarea.value = oTextarea.value.substring(0, iStart)
+'\n'+aBits[1]
+oTextarea.value.substring(iEnd);
oTextarea.setSelectionRange(iStart+aBits[1].length+1,iStart+aBits[1].length+1);
return false;
}
}
}
//--></script>
</head>
<body>
<table id="cheeseyTableLayout" width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td height="1">
<input type="button" onclick="document.getElementById('quickEditor').className='QE_left';" value="Text on Left">
<input type="button" onclick="document.getElementById('quickEditor').className='QE_top';" value="Text on Top">
<input type="button" onclick="document.getElementById('quickEditor').className='QE_right';" value="Text on Right">
<input type="checkbox" id="obBackFeed" onclick="bBackFeed=this.checked;if(bUpdate){QE_update();}" onchange="bBackFeed=this.checked;if(bUpdate){QE_update();}"><label for="obBackFeed">ContentEditable?</label>
<input type="checkbox" id="obUpdate" onclick="bUpdate =this.checked;if(bUpdate){QE_update();}" onchange="bUpdate =this.checked;if(bUpdate){QE_update();}" checked="checked"><label for="obUpdate">Auto Update?</label>
<input type="button" onclick="QE_update();" value="Update">
<input type="button" onclick="QE_savefile();" value="Save File">
</td>
</tr>
<tr>
<td height="100%">
<div id="quickEditor" class="QE_top">
<div id="one"><textarea cols="80" rows="2" id="QE_body" onkeydown="return QE_catchKey(this,event);"
onkeyup="IE_hackPara(this);if(bUpdate){QE_update();}" onchange="IE_hackPara(this);if(bUpdate){QE_update();}"
onblur="IE_unHackPara(this);"><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Title</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<style type="text/css">
a img { border: 0; }
.u { text-decoration: underline; }
</style>
<script type="text/javascript">
</script>
</head>
<body>
<p><b>HTML</b> <i>typed in the black box</i> <span class="u">renders in the white box</span>.</p>
<p>
<a href="http://validator.w3.org/check?uri=referer"><img
src="http://www.w3.org/Icons/valid-html401"
alt="Valid HTML 4.01 Strict" height="31" width="88"></a>
</p>
</body>
</html></textarea></div>
<div id="two"><iframe id="QE_demo" name="QE_demo" frameborder="0"></iframe></div>
</div>
</td>
</tr>
</table>
<script type="text/javascript">
QE_update();
document.getElementById('QE_body').focus();
</script>
</body>
</html>