diff --git a/components/header.js b/components/header.js index e0c4058..088a30f 100644 --- a/components/header.js +++ b/components/header.js @@ -310,13 +310,30 @@ export class Header extends HTMLElement { input.value = '/' input.style.minWidth = '300px' dialog.bodyEl.appendChild(input) + let errorEl const bGroup = document.createElement( 'm-forms-button-group' ) bGroup.addPrimary(this.text.createPage, () => { const newPath = this.encodePath(input.value) + const v = localStorage.getItem(newPath) + if (v !== null || newPath === this.path) { + if (!errorEl) { + errorEl = document.createElement('p') + errorEl.style.color = 'red' + const errText = this.text.alreadyExists + errorEl.innerText = errText + dialog.bodyEl.appendChild(errorEl) + return + } + return + } + localStorage.setItem(newPath, '') location.hash = newPath dialog.close() + this.dispatchEvent(new CustomEvent( + 'create-page', {bubbles: true} + )) }) bGroup.addCancel(this.text.cancel, () => { dialog.close() diff --git a/components/layout.js b/components/layout.js index 3fde587..d95805e 100644 --- a/components/layout.js +++ b/components/layout.js @@ -28,6 +28,9 @@ export class Layout extends HTMLElement { this.header.addEventListener('click-edit', () => { this.editing = !this.editing }) + this.header.addEventListener('create-page', () => { + this.editing = true + }) this.shadowRoot.appendChild(this.header) this.load() addEventListener('hashchange', () => { diff --git a/sw.js b/sw.js index 04b0de2..df2d74a 100644 --- a/sw.js +++ b/sw.js @@ -11,7 +11,7 @@ async function initCache() { '/components/dialog.js', '/forms/button-group.js', '/menu/dropdown.js', - ]) //3 + ]) //4 } self.addEventListener("install", event => {