diff --git a/components/header.js b/components/header.js index f054364..088a30f 100644 --- a/components/header.js +++ b/components/header.js @@ -309,14 +309,31 @@ export class Header extends HTMLElement { const input = document.createElement('input') input.value = '/' input.style.minWidth = '300px' - dialog.bodyEl.appendChi + 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', () => {