Skip to content

Commit 05dda78

Browse files
committed
Show a hint about code being runnable when the user hasn't used that feature yet
Closes marijnh#92
1 parent 8ffee02 commit 05dda78

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

html/css/ejs.css

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ pre {
4848
padding: 5px 0 5px 15px;
4949
line-height: 1.35;
5050
margin: 1rem 0;
51+
position: relative;
5152
}
5253

5354
pre[data-language=javascript] {
@@ -314,6 +315,29 @@ blockquote footer:before {
314315
font-size: 60%;
315316
}
316317

318+
.sandboxhint {
319+
position: absolute;
320+
top: 5px;
321+
right: 0px;
322+
font-family: tahoma, arial, sans-serif;
323+
font-size: 70%;
324+
padding: 4px 8px;
325+
background: rgb(220, 220, 220);
326+
color: white;
327+
border-radius: 5px;
328+
}
329+
330+
.sandboxhint:before {
331+
position: absolute;
332+
width: 0; height: 0;
333+
border-top: 6px solid transparent;
334+
border-bottom: 6px solid transparent;
335+
border-right: 12px solid rgb(220, 220, 220);
336+
top: 6px;
337+
left: -11px;
338+
content: '';
339+
}
340+
317341

318342
div.image img {
319343
max-width: 640px;

html/js/ejs.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,20 @@ window.addEventListener("load", function() {
1010
// If there's no ecmascript 5 support, don't try to initialize
1111
if (!Object.create || !window.JSON) return;
1212

13+
var sandboxHint = null;
14+
if (window.chapNum && window.chapNum < 20 && window.localStorage && !localStorage.getItem("usedSandbox")) {
15+
var pres = document.getElementsByTagName("pre");
16+
for (var i = 0; i < pres.length; i++) {
17+
var pre = pres[i];
18+
if (!/^(text\/)?(javascript|html)$/.test(pre.getAttribute("data-language")) ||
19+
chapNum == 1 && !/console\.log/.test(pre.textContent)) continue;
20+
sandboxHint = elt("div", {"class": "sandboxhint"},
21+
"edit & run code by clicking it");
22+
pre.appendChild(sandboxHint);
23+
break;
24+
}
25+
}
26+
1327
document.body.addEventListener("click", function(e) {
1428
for (var n = e.target; n; n = n.parentNode) {
1529
if (n.className == "c_ident") return;
@@ -69,6 +83,12 @@ window.addEventListener("load", function() {
6983
var article = document.getElementsByTagName("article")[0];
7084

7185
function activateCode(node, e, lang) {
86+
if (sandboxHint) {
87+
sandboxHint.parentNode.removeChild(sandboxHint);
88+
sandboxHint = null;
89+
localStorage.setItem("usedSandbox", "true");
90+
}
91+
7292
var code = node.textContent;
7393
var wrap = node.parentNode.insertBefore(elt("div", {"class": "editor-wrap"}), node);
7494
var editor = CodeMirror(function(div) {wrap.insertBefore(div, wrap.firstChild)}, {

0 commit comments

Comments
 (0)