Skip to content

Commit 223c49c

Browse files
committed
fix: keep the playground's address clean until something changes
Opening /playground/ no longer writes the sample code into the URL hash. The hash is written after the first change, and Copy link writes it as before.
1 parent 9315227 commit 223c49c

1 file changed

Lines changed: 15 additions & 6 deletions

File tree

‎docs/javascripts/playground.js‎

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,9 @@ function setStatus(icon, text) {
294294
}
295295

296296
let run = 0;
297+
// The hash is written once the visitor changes something, so opening the page
298+
// leaves its address clean.
299+
let touched = false;
297300

298301
async function update() {
299302
const ticket = ++run;
@@ -302,7 +305,7 @@ async function update() {
302305
const style = styleName === "file" ? ui.config.value : styleName;
303306
ui.configWrap.hidden = styleName !== "file";
304307
renderCommands();
305-
writeHash();
308+
if (touched) writeHash();
306309

307310
let mod;
308311
if (!modules.has(major)) setStatus("", `Loading clang-format ${major}…`);
@@ -387,13 +390,18 @@ ui.version.value = initial.version || DEFAULT_VERSION;
387390
ui.style.value = initial.style || "LLVM";
388391
ui.lang.value = initial.lang || "main.cpp";
389392
if (initial.config !== undefined) ui.config.value = initial.config;
393+
touched = location.hash.length > 1;
390394
ui.input.value = initial.code !== undefined ? initial.code : SAMPLE;
391395

392-
ui.version.addEventListener("change", update);
393-
ui.style.addEventListener("change", update);
394-
ui.lang.addEventListener("change", update);
395-
ui.input.addEventListener("input", scheduleUpdate);
396-
ui.config.addEventListener("input", scheduleUpdate);
396+
const changed = (handler) => () => {
397+
touched = true;
398+
handler();
399+
};
400+
ui.version.addEventListener("change", changed(update));
401+
ui.style.addEventListener("change", changed(update));
402+
ui.lang.addEventListener("change", changed(update));
403+
ui.input.addEventListener("input", changed(scheduleUpdate));
404+
ui.config.addEventListener("input", changed(scheduleUpdate));
397405
ui.viewDiff.addEventListener("click", () => showView("diff"));
398406
ui.viewOut.addEventListener("click", () => showView("out"));
399407

@@ -407,6 +415,7 @@ ui.input.addEventListener("keydown", (event) => {
407415
if (event.key === "Tab" && !event.shiftKey && !escaped) {
408416
event.preventDefault();
409417
ui.input.setRangeText(" ", ui.input.selectionStart, ui.input.selectionEnd, "end");
418+
touched = true;
410419
scheduleUpdate();
411420
}
412421
escaped = false;

0 commit comments

Comments
 (0)