-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
210 lines (177 loc) · 6.51 KB
/
index.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
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
var types = require("./codepen-data");
var specials = require("./specials");
var languageHTML = /language-(\w+)/;
function cleanCodePenData(data) {
if(docObject.codepen) {
docObject.codepen.forEach(function(replacement){
if(data.html) {
data.html = data.html.split(replacement[0]).join(replacement[1]);
}
if(data.js) {
data.js = data.js.split(replacement[0]).join(replacement[1]);
}
});
}
}
function createCodePen(data) {
var JSONstring = JSON.stringify(data);
var form =
'<form action="https://codepen.io/pen/define" method="POST" target="_blank">' +
'<input type="hidden" name="data">' +
'</form>';
var div = document.createElement("div");
div.innerHTML = form;
div.firstChild.firstChild.value = JSONstring;
document.body.appendChild(div);
div.firstChild.submit();
setTimeout(function(){
document.body.removeChild(div);
},10);
}
var browserMatches = document.body.matches || document.body.msMatchesSelector;
function matches(selector) {
if(this.nodeType === 1) {
if(selector.indexOf(",") >= 0 ) {
return selector.split(",").some(function(selector){
return browserMatches.call(this, selector);
}, this);
} else {
return browserMatches.call(this, selector);
}
} else {
return false;
}
}
function findSelector(start, selector) {
while(start) {
if(matches.call(start, selector)) {
return start;
}
if(start.querySelector) {
var pre = start.querySelector(selector);
if(pre) {
return pre;
}
}
// needs to be previousSibling for zombie
start = start.previousSibling;
}
}
function findDemoWrapper(el) {
while(el && el.parentNode) {
if(matches.call(el.parentNode, '.demo_wrapper')) {
return el.parentNode;
}
el = el.parentNode;
}
}
function findPreForToolbarBtn(el) {
while(el) {
if (el.nodeName === "PRE") {
return el;
}
if (matches.call(el, '.toolbar')) {
el = findSelector(el, 'pre');
} else {
el = el.parentNode;
}
}
}
function getStylesFromIframe(iframe) {
var styles = iframe.contentDocument.documentElement.querySelectorAll("style");
var cssText = "";
styles.forEach(function(style){
cssText += style.innerHTML;
});
return cssText;
}
var isRegistered = false;
module.exports = function() {
var codepens = document.querySelectorAll('div.codepen');
//remove the old codepen links
codepens.forEach(function(codepen){
var wrapper = findSelector(codepen, "pre, .demo_wrapper");
var special = codepen.getAttribute("data-codepen");
//the CodePen iframe wrapper has ".codepen" class too
if (wrapper) {
wrapper.setAttribute('data-has-run', true);
if (special) {
wrapper.setAttribute('data-codepen', special);
}
codepen.parentNode.removeChild(codepen);
}
});
if (!isRegistered) {
//Register PrismJS "Run" custom button
Prism.plugins.toolbar.registerButton("run-code", function(env) {
isRegistered = true;
var demoWrapper = findDemoWrapper(env.element);
var pre = env.element.parentElement;
var hasRunBtn = demoWrapper ? demoWrapper.getAttribute("data-has-run") : pre.getAttribute("data-has-run");
//prevent other demos without codepen link to register Run button
if (hasRunBtn) {
var btn = document.createElement("button");
btn.innerHTML = "Run";
btn.setAttribute("data-run", "");
return btn;
}
});
document.body.addEventListener('click', function (ev) {
if (ev.target.getAttribute('data-run') != null) {
var btn = ev.target;
var demoWrapper = findDemoWrapper(btn);
if (!demoWrapper) {
var preElement = findPreForToolbarBtn(btn);
var codeElement = preElement.querySelector("code");
var language = codeElement.className.match(languageHTML)[1];
var text = codeElement.textContent;
var data = types[language](text);
if(data.js) {
data.js = data.js.trim();
}
if(data.html) {
data.html = data.html.trim();
}
var special = preElement.getAttribute("data-codepen");
if (special && specials[special]) {
specials[special](data);
}
if(data) {
cleanCodePenData(data);
if(window.CREATE_CODE_PEN) {
CREATE_CODE_PEN(data);
} else {
createCodePen(data);
}
} else {
console.warn("Unable to create a codepen for this demo");
}
}
if (demoWrapper && matches.call(demoWrapper, '.demo_wrapper')) {
var htmlCode = demoWrapper.querySelector('[data-for=html] code');
var htmlText = htmlCode ? htmlCode.textContent.trim() : '';
var jsCode = demoWrapper.querySelector('[data-for=js] code');
var jsText = jsCode ? jsCode.textContent.trim() : '';
var cssText = getStylesFromIframe(demoWrapper.querySelector('iframe'));
var special = demoWrapper.getAttribute("data-codepen");
if (special && specials[special]) {
specials[special](data);
}
var codePen = {
html: htmlText,
js: jsText,
js_module: true,
editors: '1011',
css: cssText.trim()
};
cleanCodePenData(codePen);
if (window.CREATE_CODE_PEN) {
CREATE_CODE_PEN(codePen);
} else {
createCodePen(codePen);
}
}
}
});
}
};