Skip to content

Commit b9babe1

Browse files
committed
Add autofocus attribute for html snippets
1 parent d67ee55 commit b9babe1

3 files changed

Lines changed: 8 additions & 9 deletions

File tree

14_event.txt

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,7 @@ enabling the effect on `"keydown"`, and turning it off again on
248248
`"keyup"`.
249249

250250
[source,text/html]
251+
[focus="yes"]
251252
----
252253
<p>This page turns violet when you hold the V key.</p>
253254
<script>
@@ -262,13 +263,6 @@ enabling the effect on `"keydown"`, and turning it off again on
262263
</script>
263264
----
264265

265-
ifdef::html_target[]
266-
267-
To make the browser actually deliver key events to the example page,
268-
you have to click it first to focus it.
269-
270-
endif::html_target[]
271-
272266
Despite its name, `"keydown"` is not only fired when the key is
273267
physically pushed down. When a key is pressed and held, the event is
274268
fired again every time the key “repeats”. Sometimes, for example when
@@ -308,6 +302,7 @@ looking at the `shiftKey`, `ctrlKey`, `altKey`, and `metaKey`
308302
properties of key (and mouse) events.
309303

310304
[source,text/html]
305+
[focus="yes"]
311306
----
312307
<p>Press control-space to continue.</p>
313308
<script>
@@ -329,6 +324,7 @@ The `String.fromCharCode` function can be used to turn this code into
329324
an actual (single-character) string.
330325

331326
[source,text/html]
327+
[focus="yes"]
332328
----
333329
<p>Focus this page and type something.</p>
334330
<script>

asciidoc_html.conf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ options=sectionbody
3636
</div></div>
3737

3838
[source-highlight-block]
39-
<pre{id? id="{id}"} data-language="{language}" class="snippet cm-s-default"{sandbox? data-sandbox="{sandbox}"}>
39+
<pre{id? id="{id}"} data-language="{language}" class="snippet cm-s-default"{sandbox? data-sandbox="{sandbox}"}{focus? data-focus="true"}>
4040
|
4141
</pre>
4242

html/js/ejs.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,10 @@ window.addEventListener("load", function() {
124124
var val = data.editor.getValue();
125125
getSandbox(data.sandbox, data.isHTML, function(box) {
126126
if (data.isHTML)
127-
box.setHTML(withDoctype(val), data.output, Math.min);
127+
box.setHTML(withDoctype(val), data.output, function() {
128+
if (data.orig.getAttribute("data-focus"))
129+
box.win.focus();
130+
});
128131
else
129132
box.run(val, data.output);
130133
});

0 commit comments

Comments
 (0)