Simple CMS Template
This template is a minimal content management system for building a site. Read the tutorial “Building a CMS Frontend” to learn how it works.
Source
Section titled “Source”<!doctype html><html> <head> <meta charset="utf-8"> <link rel="stylesheet" href="/ui.css"> <link rel="icon" type="image/png" sizes="32x32" href="/thumb.png"> </head> <body> <nav> <div class="view-mode active"> <button class="new">New</button> <hr> <button class="edit">Edit</button> <button class="remove">Delete</button> </div> <div class="edit-mode"> <button class="save">Save</button> <button class="cancel">Cancel</button> </div> </nav> <main></main> <textarea id="editor"></textarea> <script type="module" src="/ui.js"></script> </body></html>{ "title": "My Website", "description": "A simple editable website.", "fallback": "/index.html"}// Routes are extensionless virtual paths (/, /about, …) with page content stored// under /pages/<route>.html. A route has no real file, so the manifest `fallback`// serves this shell there and we render the stored page into it. (/index.html is// the shell itself; storing pages at real .html paths would win the navigation// and serve them raw, outside the site chrome.)var route = location.pathname.replace(/\/$/, '') || '/'var pathname = route === '/' || route === '/index.html' ? '/pages/index.html' : `/pages${route}.html`
const $ = (sel, parent = document) => parent.querySelector(sel)const nav = $('nav')const main = $('main')const editor = $('#editor')
async function readPage () { return nomad.fs.readFile(pathname).catch(e => '')}
async function onNew (e) { var page = prompt('Enter the name of your new page') if (!page) return page = page.replace(/\.html$/, '').replace(/^\//, '') await nomad.fs.writeFile(`/pages/${page}.html`, `<h1>${page}</h1>`) location.pathname = `/${page}`}
async function onSave (e) { await nomad.fs.writeFile(pathname, editor.value) location.reload()}
async function onDelete (e) { if (!confirm('Are you sure?')) return await nomad.fs.unlink(pathname) location.reload()}
async function enterEditMode () { $('.view-mode', nav).classList.remove('active') $('.edit-mode', nav).classList.add('active') main.classList.remove('active') editor.classList.add('active') editor.value = await readPage()}
async function enterViewMode () { $('.view-mode', nav).classList.add('active') $('.edit-mode', nav).classList.remove('active') main.classList.add('active') editor.classList.remove('active') main.innerHTML = await readPage()}
async function setup () { // register event listeners $('button.new', nav).addEventListener('click', onNew) $('button.edit', nav).addEventListener('click', e => enterEditMode()) $('button.remove', nav).addEventListener('click', onDelete) $('button.save', nav).addEventListener('click', onSave) $('button.cancel', nav).addEventListener('click', e => enterViewMode())
// start in view mode enterViewMode()}setup()body { font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
display: grid; grid-template-columns: 100px 1fr; grid-gap: 30px; height: 100%; margin: 0;}
body > nav { background: #f3f3f8; height: 100%; padding: 10px;}
body > nav .view-mode,body > nav .edit-mode,main,#editor { display: none;}
body > nav .view-mode.active,body > nav .edit-mode.active,main.active,#editor.active { display: block;}
body > nav button { display: block; width: 100%; margin-bottom: 5px;}
body > nav hr { border: 0; border-top: 1px solid #ddd;}
#editor { height: 100vh;}MIT License
Copyright (c) 2020 Blue Link Labs
Permission is hereby granted, free of charge, to any person obtaining a copyof this software and associated documentation files (the "Software"), to dealin the Software without restriction, including without limitation the rightsto use, copy, modify, merge, publish, distribute, sublicense, and/or sellcopies of the Software, and to permit persons to whom the Software isfurnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in allcopies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS ORIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THEAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHERLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THESOFTWARE.