[Docs] Use replaceState API when demo code changes (#710)

In the current demo, every time the code in the left panel is changed,
the new code is stored in the URL hash, which pushes a new history
entry. This effectively "breaks" the back button. This change calls
`history.replaceState` instead, so a single click of the back button
will return the browser to whatever page was open before navigating to
the demo.
master
Brandon Mills 2017-02-15 00:02:17 -05:00 committed by Christopher Chedeau
parent 74fb4a43b1
commit eda26398e5
1 changed files with 15 additions and 1 deletions

View File

@ -131,6 +131,20 @@ function getOptions() {
return options;
}
function replaceHash(hash) {
if (
typeof URL === "function" &&
typeof history === "object" &&
typeof history.replaceState === "function"
) {
var url = new URL(location);
url.hash = hash;
history.replaceState(null, null, url);
} else {
location.hash = hash;
}
}
function format() {
var options = getOptions();
[docEditor, outputEditor].forEach(function(editor) {
@ -146,7 +160,7 @@ function format() {
Object.assign({content: inputEditor.getValue(), options: options})
)
);
location.hash = value;
replaceHash(value);
var res;
try {
res = prettier.format(inputEditor.getValue(), options);