Skip to content

Commit

Permalink
chore: fix playground
Browse files Browse the repository at this point in the history
  • Loading branch information
ota-meshi committed Oct 25, 2023
1 parent 091fbc0 commit ccd79eb
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 38 deletions.
36 changes: 24 additions & 12 deletions docs/.vitepress/theme/components/playground-block.vue
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,9 @@ export default {
name: "PlaygroundBlock",
components: { PgEditor, RulesSettings, SnsBar },
data() {
const serializedString =
(typeof window !== "undefined" && window.location.hash.slice(1)) ||
""
const state = deserializeState(serializedString)
return {
code: state.code || DEFAULT_CODE,
rules: state.rules || Object.assign({}, DEFAULT_RULES_CONFIG),
code: DEFAULT_CODE,
rules: Object.assign({}, DEFAULT_RULES_CONFIG),
messages: [],
}
},
Expand All @@ -103,19 +99,34 @@ export default {
},
watch: {
serializedString(serializedString) {
if (typeof window !== "undefined") {
if (
typeof window !== "undefined" &&
serializedString !== window.location.hash.slice(1) &&
!this._initializing
) {
window.location.replace(`#${serializedString}`)
}
},
},
mounted() {
if (typeof window !== "undefined") {
window.addEventListener("hashchange", this.onUrlHashChange)
window.addEventListener("hashchange", this.processUrlHashChange)
}
const serializedString =
(typeof window !== "undefined" && window.location.hash.slice(1)) ||
""
if (serializedString) {
this._initializing = true
this.rules = {}
this.$nextTick().then(() => {
this._initializing = false
this.processUrlHashChange()
})
}
},
beforeDestroey() {
beforeUnmount() {
if (typeof window !== "undefined") {
window.removeEventListener("hashchange", this.onUrlHashChange)
window.removeEventListener("hashchange", this.processUrlHashChange)
}
},
methods: {
Expand All @@ -125,7 +136,7 @@ export default {
getRule(ruleId) {
return getRule(ruleId)
},
onUrlHashChange() {
processUrlHashChange() {
const serializedString =
(typeof window !== "undefined" &&
window.location.hash.slice(1)) ||
Expand All @@ -135,8 +146,9 @@ export default {
this.code = state.code || DEFAULT_CODE
this.rules =
state.rules || Object.assign({}, DEFAULT_RULES_CONFIG)
this.script = state.script
return true
}
return false
},
},
}
Expand Down
50 changes: 25 additions & 25 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
"devDependencies": {
"@changesets/cli": "^2.26.2",
"@ota-meshi/eslint-plugin": "^0.15.1",
"@ota-meshi/site-kit-eslint-editor-vue": "^0.1.0",
"@ota-meshi/site-kit-eslint-editor-vue": "^0.1.2",
"@svitejs/changesets-changelog-github-compact": "^1.1.0",
"@types/chai": "^4.2.22",
"@types/eslint": "^8.44.0",
Expand Down

0 comments on commit ccd79eb

Please sign in to comment.