Skip to content

Commit 4c49282

Browse files
dwursteisenclaude
andcommitted
Fix Try it button to dispatch play event and load editor on demand
Remove data-example attribute from the Try it button since the code is already in the terminal body. In setupDocMode, remove the toolbar and buttons that were visible even when the tiny-editor was hidden. Instead, create a hidden container and listen for a "play" custom event on the tiny-editor element to trigger game creation. The API page dispatches this event when the Try it button is clicked. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 6b7cb87 commit 4c49282

File tree

3 files changed

+15
-39
lines changed

3 files changed

+15
-39
lines changed

tiny-doc/src/docs/templates/pages/api.peb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@
245245
var editor = wrapper.querySelector('tiny-editor');
246246
if (terminal && editor) {
247247
terminal.style.display = 'none';
248-
editor.style.display = '';
248+
editor.dispatchEvent(new Event('play'));
249249
}
250250
});
251251

tiny-doc/src/docs/templates/partials/fn-card.peb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
<span class="terminal__dot terminal__dot--yellow"></span>
2525
<span class="terminal__dot terminal__dot--green"></span>
2626
<span class="terminal__filename">example.lua</span>
27-
<a class="fn-card__try-btn" data-example="{{ fn.example }}">
27+
<a class="fn-card__try-btn">
2828
<span class="material-symbols-outlined" style="font-size: 1rem; vertical-align: middle;">play_arrow</span>
2929
Try it
3030
</a>

tiny-web-editor/src/jsMain/kotlin/Main.kt

Lines changed: 13 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -83,52 +83,28 @@ private fun setupDocMode(
8383
val spritePath = game.getAttribute("sprite")
8484
val levelPath = game.getAttribute("level")
8585

86-
val toolbar = document.createElement("div") {
87-
setAttribute("class", "tiny-toolbar")
88-
}
89-
game.before(toolbar)
90-
91-
val link = document.createElement("a") {
92-
setAttribute("id", "link-editor-$index")
93-
setAttribute("class", "tiny-play tiny-button")
94-
setAttribute("href", "#link-editor-$index")
95-
} as HTMLAnchorElement
96-
toolbar.appendChild(link)
97-
98-
val playLink = document.createElement("div").apply {
86+
val container = document.createElement("div").apply {
9987
setAttribute("class", "tiny-container")
88+
asDynamic().style.display = "none"
10089
}
101-
toolbar.after(playLink)
90+
game.after(container)
10291

10392
val codeToUse = decodedCode ?: "-- Update the code to update the game!\n$code"
10493

105-
var clicked = false
106-
link.innerHTML = """<i data-lucide="play"></i> Run and edit this example"""
107-
link.onclick = { _ ->
108-
if (!clicked) {
109-
createGame(playLink, index, codeToUse, spritePath, levelPath, rootPath)
110-
clicked = true
94+
var started = false
95+
game.addEventListener("play", {
96+
if (!started) {
97+
container.asDynamic().style.display = ""
98+
createGame(container, index, codeToUse, spritePath, levelPath, rootPath)
99+
started = true
111100
}
112-
true
113-
}
114-
115-
val editorLink = (
116-
document.createElement("a") {
117-
setAttribute("class", "tiny-button tiny-button-right")
118-
id = "share-$index"
119-
innerHTML = """<i data-lucide="external-link"></i> Editor"""
120-
} as HTMLAnchorElement
121-
).apply {
122-
val b64 = Base64.encode(code.encodeToByteArray())
123-
href = "editor.html?game=$b64"
124-
target = "_blank"
125-
}
126-
127-
toolbar.appendChild(editorLink)
101+
})
128102

129103
// There is a user code. Let's unfold the game.
130104
if (savedCode != null) {
131-
createGame(playLink, index, codeToUse, spritePath, levelPath, rootPath)
105+
container.asDynamic().style.display = ""
106+
createGame(container, index, codeToUse, spritePath, levelPath, rootPath)
107+
started = true
132108
}
133109
}
134110

0 commit comments

Comments
 (0)