forked from tomberek/tomberek.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
markdeep.html
235 lines (215 loc) · 9.1 KB
/
markdeep.html
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
<!DOCTYPE html>
<html>
<head>
<title>Real-time Markdeep Editor</title>
<!--
Mostly adopted from: http://htmledit.squarefree.com/
Also used
<a href="https://casual-effects.com/markdeep/">Markdeep</a>-
<a href="https://www.tildefriends.net/~cory/sharedwiki#test">Co-op Markdeep</a>-
<a href="http://asciiflow.com/">ASIIFlow</a>
-->
<style>
textarea {
font-family: monospace;
height: 260px;
}
img, canvas {
width: 600px;
height: 600px;
}
</style>
<script>
window.markdeepOptions = {mode: 'script'};</script>
<script src="https://tomberek.info/js/markdeep.min.js"></script>
<script>
function debounce(func, wait, immediate) {
var timeout;
return function() {
var context = this, args = arguments;
var later = function() {
timeout = null;
func.apply(context, args);
};
var callNow = immediate && !timeout;
clearTimeout(timeout);
timeout = setTimeout(later, wait);
if (callNow) func.apply(context, args);
};
};
function getCaret(el) {
if (el.selectionStart) {
return el.selectionStart;
} else if (document.selection) {
el.focus();
var r = document.selection.createRange();
if (r == null) {
return 0;
}
var re = el.createTextRange(),
rc = re.duplicate();
re.moveToBookmark(r.getBookmark());
rc.setEndPoint('EndToStart', re);
return rc.text.length;
}
return 0;
}
function setCaretPosition(elem, caretPos) {
if (elem != null) {
if (elem.createTextRange) {
var range = elem.createTextRange();
range.move('character', caretPos);
range.select();
}
else {
if (elem.selectionStart) {
elem.focus();
elem.setSelectionRange(caretPos, caretPos);
}
else elem.focus();
}
}
}
function init(){
window.editbox.f.png.addEventListener("click",function(){updateImage();});
window.editbox.f.ta.addEventListener("keydown", function(e) {
if(e.keyCode == 8) {
if (e.shiftKey){return true;}
var text = this.value;
var caret = getCaret(this);
if (!caret || !text.substring(caret).split('\n')[0].endsWith('*')) return true;
var output = text.substring(0, caret-1);
this.value = output + ' ' + text.substring(caret);
setCaretPosition(this, caret-1);
//e.stopImmediatePropagation();
e.preventDefault();
return false;
}
return true;
});
window.editbox.f.ta.addEventListener("keypress", function(e) {
var key = String.fromCharCode(e.which || e.charCode || e.keyCode);
if (key == " " && e.shiftKey){return true;}
if (/[\s\S]/.test(key)) {
var text = this.value;
var caret = getCaret(this);
if (!caret || !text.substring(caret).split('\n')[0].endsWith('*')) return true;
var output = text.substring(0, caret);
this.value = output + key + text.substring(caret + 1);
setCaretPosition(this, caret + 1);
e.preventDefault();
return false;
}
return true;
});
window.editbox.f.ta.addEventListener("keyup", debounce(function(e) {
var fr = window.document.getElementsByTagName('frameset')[0]
var d_el = window.document.createElement('frame');
fr.appendChild(d_el);
var d = d_el.contentDocument;
var bullet = '<script>var arr=document.currentScript.parentElement.querySelectorAll(\'.bullet\'); for (var i=0;i<arr.length;i++){ arr[i].innerHTML=arr[i].innerHTML.replace(/^(\\s*)(\\>\;|\\<\;|[\!,\@,\#,\%]*)(\\[x\\]|\\[ \\]|\\(x\\)|\\( \\)|-) /gm,function(match,p1,p2,p3){ switch(p3){ case "[x]": return p1+ "+ <label>" + p2 + "<\/label><input type=\'checkbox\' class=\'box\' checked><\/input>"; case "[ ]": return p1+ "+ <label>" + p2 + "<\/label><input type=\'checkbox\' class=\'box\'><\/input>"; case "(x)": return p1+ "+ <label>" + p2 + "<\/label><input type=\'checkbox\' class=\'circle\' checked><\/input>"; case "( )": return p1+ "+ <label>" + p2 + "<\/label><input type=\'checkbox\' class=\'circle\'><\/input>"; case "-": return p1 + "+ -<label>" + p2 + "<\/label>"; default: return match; } }) ; } <\/script> <style> .bullet > ul {position:relative;} .bullet li {position:static;} .bullet label { font-family: Menlo; background:#fff; position: absolute; left:0em; } .bullet > ul {padding-left:20px;} .bullet input[type=checkbox] { width: 1em; height: 1em; margin-top: 0em; margin-bottom: 0em; border: #666 thin; border-style:solid; -webkit-appearance: none; ;} .bullet input[type=checkbox].circle {border-radius: 100px;} .bullet input[type=checkbox]:checked {background: #70a0ff;} .bullet li {list-style-type:none;} <\/style> ';
d.open();d.write( (window.editbox.document.f.ta.value || '- Try lists\n- In a div with class="bullet"\n') + bullet + '<script>window.history.pushState({},"","' + window.editbox.window.location.search + '");window.markdeepOptions = {mode: "' + window.editbox.f.mode.value + '",tocStyle:"'+window.editbox.f.tocStyle.value+'",detectMath:'+window.editbox.f.detectMath.value+',showURLs:'+window.editbox.f.showURLs.value+'};window.alreadyProcessedMarkdeep=false;<\/script><!-- Markdeep: --><style class="fallback">body{visibility:hidden;white-space:pre;font-family:monospace}</style><script src="https://tomberek.info/js/markdeep.min.js"><\/script><script>window.alreadyProcessedMarkdeep&&(document.body.style.visibility="visible")<\/script>');d.close();
var func = function(){
var g = fr;
if(d.readyState == "complete" ){
var scrollY=0;
while (g.children[2] && g.children[2].contentDocument.readyState =="complete") {
scrollY=g.children[1].contentWindow.pageYOffset;
g.children[2].contentWindow.scrollTo(0,scrollY);
g.removeChild(g.children[1]);
}
d_el.setAttribute('id',"dynamicframe");
// updateImage();
} else {
setTimeout(func,50);
}
}
func();
},50,true));
window.editbox.f.ta.dispatchEvent(new Event("keyup"));
}
</script>
<script>
function getMatchedCSSRules(element) {
var i, len, matching = [], sheets = element.ownerDocument.styleSheets;
function loopRules(rules) {
var i, len, rule;
for (i = 0, len = rules.length; i < len; i++) {
rule = rules[i];
if (rule instanceof CSSMediaRule) {
if (window.matchMedia(rule.conditionText).matches) {
loopRules(rule.cssRules);
}
} else if (rule instanceof CSSStyleRule) {
if (element.matches(rule.selectorText)) {
matching.push(rule);
}
}
}
};
for (i = 0, len = sheets.length; i < len; i++) {
loopRules(sheets[i].cssRules);
}
return matching;
}
var domURL = self.URL || self.webkitURL || self;
function updateImage(){
var ss = window.document.getElementById("dynamicframe").contentDocument.querySelectorAll("svg")
for(var i=0;i<ss.length;i++){
var s = ss[i];
var sh=s.getAttribute('height');
var sw=s.getAttribute('width');
s.setAttribute('viewBox',"0 0 " + sw + " " + sh);
s.removeAttribute('height')
s.removeAttribute('width')
var newElement = document.createElementNS("http://www.w3.org/2000/svg", 'style');
var n = document.createTextNode('path , polygon { display: block; '+
'text-align: center; stroke-linecap: round; stroke-width: 2px;'+
'break-inside: avoid; stroke: rgb(0, 0, 0); fill: rgb(0, 0, 0); }'+
'text {'+
'font-family: stroke:none; Menlo,Consolas, monospace; font-size: 13.1px;}');
newElement.appendChild(n);
if (!s) {
return;
}
s.prepend(newElement);
var svgText = s.outerHTML;
var d = window.document.getElementById("dynamicframe").contentDocument;
var myCanvas = d.createElement("canvas");
d.body.appendChild(myCanvas);
myCanvas.setAttribute('height',s.getAttribute('height'));
myCanvas.setAttribute('width',s.getAttribute('width'));
myCanvas.setAttribute('hidden',true);
var ctxt = myCanvas.getContext("2d");
ctxt.clearRect(0, 0, myCanvas.width, myCanvas.height);
drawInlineSVG(s,myCanvas,ctxt, svgText, function(el,myCanvas) {
var png = myCanvas.toDataURL("image/png");
var myPng = d.createElement('a');
myPng.setAttribute('href',png);
myPng.setAttribute('download','markdeep.png');
el.parentElement.appendChild(myPng);
myPng.appendChild(el);
domURL.revokeObjectURL(png);
});
}
}
function drawInlineSVG(el,myCanvas, ctx, rawSVG, callback) {
var svg = new Blob([rawSVG], {type:"image/svg+xml;charset=utf-8"}),
domURL = self.URL || self.webkitURL || self,
url = domURL.createObjectURL(svg),
img = new Image;
img.onload = function () {
ctx.drawImage(this, 0, 0);
domURL.revokeObjectURL(url);
callback(el,myCanvas);
};
img.src = url;
}
</script>
</head>
<frameset resizable="yes" cols="45%,*" onload="init();">
<!-- about:blank confuses opera, so use javascript: URLs instead -->
<frame name="editbox" src="markdeep-editor.html">
<frame id="dynamicframe" src="markdeep-viewer.html">
</frameset>
</html>