Skip to content

Commit 604a9f8

Browse files
author
Irene Ros
committed
Added cleanup hidden script support.
1 parent 5a3574f commit 604a9f8

File tree

3 files changed

+58
-1
lines changed

3 files changed

+58
-1
lines changed

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,23 @@ the editor that it's running for:
9292
console.log(someVar);
9393
</textarea>
9494

95+
# Cleanup scripts: #
96+
97+
Sometimes you just have to clean up the mess you made, or have something execute
98+
after your code that the reader doesn't need to see. No problem! Add a cleanup script
99+
as follows:
100+
101+
<script type="codemirror/cleanup" data-selector="#code4">
102+
someVar = 20;
103+
104+
// This will output 20!
105+
console.log(someVar);
106+
</script>
107+
108+
<textarea id="code4" name="code" class="code" mode="javascript" style="display: none;" runnable="true">
109+
var someVar = 10;
110+
</textarea>
111+
95112
# Globals! #
96113

97114
Sometimes you just need to access a global of some kind. The code in the codemirror editors is executed

deck.codemirror.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@
5353
var $container = $[deck]('getContainer'),
5454
opts = $[deck]('getOptions'),
5555
codeblocks = $(slide).find(opts.selectors.codemirroritem),
56-
hiddenScripts = [];
56+
hiddenScripts = [],
57+
cleanupScripts = [];
5758

5859
// Seek out and cache all hidden scripts
5960
$("script[type=codemirror]").each(function() {
@@ -62,6 +63,14 @@
6263
src: this.innerHTML
6364
});
6465
});
66+
67+
// Seek out and cache all cleanup scripts
68+
$("script[type=\"codemirror/cleanup\"]").each(function() {
69+
cleanupScripts.push({
70+
selector: $(this).data("selector"),
71+
src: this.innerHTML
72+
});
73+
});
6574

6675
// go through all code blocks
6776
$.each(codeblocks, function(i, codeblock) {
@@ -170,11 +179,19 @@
170179

171180
var combinedSource = "";
172181

182+
// Prepend all setup scripts
173183
$.each(hiddenScripts, function() {
174184
if ($(codeblock).is(this.selector)) {
175185
combinedSource += this.src + "\n";
176186
}
177187
});
188+
189+
// Append all cleanup scripts
190+
$.each(cleanupScripts, function() {
191+
if ($(codeblock).is(this.selector)) {
192+
combinedSource = combinedSource + this.src + "\n";
193+
}
194+
});
178195

179196
combinedSource += editor.getValue();
180197

introduction/index.html

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,29 @@ <h3> Then your code </h3>
147147
<div><textarea id="code4" name="code" class="code" mode="javascript" style="display: none;" runnable="true">// output my log
148148
console.log(someVar);</textarea></div>
149149
</section>
150+
151+
<section class="slide" id="cleanup-script-tags">
152+
<h2> Need to run some cleanup for your codes? </h2>
153+
<h3> Embed a secret hidden cleanup script tag like so in your slide: </h3>
154+
155+
<script type="codemirror/cleanup" data-selector="#code4">
156+
someVar = 12;
157+
console.log(someVar);
158+
</script>
159+
160+
<code>
161+
&lt;script type=&quot;codemirror/cleanup&quot; data-selector=&quot;#code3&quot;&gt;<br/>
162+
someVar = 12;
163+
console.log(someVar);<br/>
164+
&lt;/script&gt;<br/>
165+
</code>
166+
167+
<h3> Then your code </h3>
168+
<div><textarea id="code4" name="code" class="code" mode="javascript" style="display: none;" runnable="true">
169+
// set some initial value
170+
var someVar = 15;
171+
</textarea></div>
172+
</section>
150173

151174
<section class="slide" id="globals">
152175
<h2> Sometimes you need some globals </h2>

0 commit comments

Comments
 (0)