Make one edit flow feel immediate
Save the edit form without leaving the page, keep everything you typed when a save is refused, and keep the plain form working without JavaScript.
Allow 15–25 minutes. This is an editorial estimate. Continue in the same my-site folder, signed in as the editor.
Read
Start at L19. Every save so far sends the form's values to the server and comes back on Admin. That works everywhere, and it stays. This lesson adds one attribute, w-inplace, to the edit form: in a browser that runs JavaScript, Save then sends the form in the background and you stay on the page.
You still write only HTML. The What script that every page already loads is JavaScript; it reads w-inplace and does the work. Without JavaScript, the browser ignores the attribute and sends the form the old way, so nothing breaks.
After a saved change the server sends back the page's new revision, and the script puts it into the hidden _revision field, so your next save is checked against it. The form shows “Saved.” in its own status line. A refused save, for example “This page changed since you opened it.”, appears in the form's own alert line, and everything you typed stays where it is.
Sometimes the browser cannot tell how a save ended: the connection dropped, or the server failed while answering. Then the form says “The save could not be confirmed. Reload before saving again.” The change may or may not have been stored, so reload before you save again. If the first save was stored, a second save from the old revision is refused rather than overwriting it; if it was not, that revision is still the current one.
While a save is on its way the form is marked busy, and pressing Save or Enter again does nothing: only one save request runs at a time from this form.
Change
1. Keep a copy. Stop the dev server with Ctrl+C. Copy the whole my-site folder to a new folder such as my-site-before-L20.
2. Make the edit form save in place. Replace site/admin/pages/edit/[id].html. Both forms gain w-inplace and two empty lines: role="alert" for refusals and role="status" for “Saved.”. The opening sentence is shorter: it no longer names the title or the revision, which can change while the page stays open.
<what>
title = "Edit page | Admin | My website"
description = "Edit a page's working copy."
fetch.page.url = "local:pages"
fetch.page.copy = "working"
fetch.page.filter = "id=#params.id#"
fetch.page.single = true
</what>
<h1>Edit page</h1>
<p>You are editing this page's working copy. Saving does not publish your changes.</p>
<p><a href="/admin/pages/preview/#page.id#">Preview your last save</a>: it draws this page as you last saved it with the public page's component. Anything typed below and not saved yet is not included.</p>
<p><a href="/admin/pages/delete/#page.id#">Delete this page</a>: the next page asks you to confirm.</p>
<if flash.error>
<div class="form-error" role="alert">
<p>#flash.error#</p>
<ul>
<if errors.title><li>Title: #errors.title#</li></if>
<if errors.summary><li>Summary: #errors.summary#</li></if>
<if errors.body><li>Body: #errors.body#</li></if>
</ul>
</div>
</if>
<!-- After a refused save, the form shows what was typed and the revision it was opened with. -->
<if old>
<form class="sign-in" method="post" action="/w-action/pages/#page.id#?w-redirect=/admin" w-inplace>
<input type="hidden" name="_revision" value="#old._revision#">
<p>Address: <strong>/pages/#page.slug#</strong> (fixed when the page was created)</p>
<div class="field">
<label for="title">Title</label>
<input id="title" name="title" type="text" maxlength="120" value="#old.title#" aria-describedby="title-error">
<span id="title-error" class="field-error">#errors.title#</span>
</div>
<div class="field">
<label for="summary">Summary</label>
<input id="summary" name="summary" type="text" maxlength="280" value="#old.summary#" aria-describedby="summary-error">
<span id="summary-error" class="field-error">#errors.summary#</span>
</div>
<div class="field">
<label for="body">Body</label>
<textarea id="body" name="body" rows="8" maxlength="20000" aria-describedby="body-error">#old.body#</textarea>
<span id="body-error" class="field-error">#errors.body#</span>
</div>
<p class="save-alert" role="alert"></p>
<p class="save-status" role="status"></p>
<button type="submit">Save changes</button>
</form>
<else/>
<form class="sign-in" method="post" action="/w-action/pages/#page.id#?w-redirect=/admin" w-inplace>
<input type="hidden" name="_revision" value="#page._revision#">
<p>Address: <strong>/pages/#page.slug#</strong> (fixed when the page was created)</p>
<div class="field">
<label for="title">Title</label>
<input id="title" name="title" type="text" maxlength="120" value="#page.title#" aria-describedby="title-error">
<span id="title-error" class="field-error">#errors.title#</span>
</div>
<div class="field">
<label for="summary">Summary</label>
<input id="summary" name="summary" type="text" maxlength="280" value="#page.summary#" aria-describedby="summary-error">
<span id="summary-error" class="field-error">#errors.summary#</span>
</div>
<div class="field">
<label for="body">Body</label>
<textarea id="body" name="body" rows="8" maxlength="20000" aria-describedby="body-error">#page.body#</textarea>
<span id="body-error" class="field-error">#errors.body#</span>
</div>
<p class="save-alert" role="alert"></p>
<p class="save-status" role="status"></p>
<button type="submit">Save changes</button>
</form>
</if>
3. Style the two lines. Replace static/styles.css. Only the last three rules are new: an empty line takes no space.
/* Framework styles are supplied automatically. These rules style our site. */
.site-body {
margin: 0;
color: #26372c;
background: #faf8f2;
font-family: system-ui, sans-serif;
line-height: 1.65;
}
.site-header, .site-main, .site-footer {
width: min(100% - 2rem, 48rem);
margin-inline: auto;
}
.site-header {
display: flex;
flex-wrap: wrap;
align-items: center;
justify-content: space-between;
gap: 1rem;
padding-block: 1.5rem;
border-bottom: 1px solid #c9cec4;
}
.site-name { font-size: 1.2rem; font-weight: 750; }
.site-nav { display: flex; flex-wrap: wrap; gap: 1rem; }
.site-body a { color: #315f40; text-underline-offset: 0.2em; }
.site-body a:focus-visible {
outline: 3px solid #825414;
outline-offset: 4px;
}
.site-main { padding-block: 2.5rem; }
.site-main h1 { font-size: clamp(2rem, 7vw, 3rem); line-height: 1.15; }
.site-main p { max-width: 65ch; }
.site-main figure { margin: 2rem 0; }
.site-image { display: block; max-width: 100%; height: auto; border-radius: 1rem; }
.site-main figcaption { margin-top: 0.6rem; color: #4b594e; }
.site-footer { border-top: 1px solid #c9cec4; padding-block: 1.5rem; }
.skip-link {
position: absolute;
top: -10rem;
left: 1rem;
padding: 0.6rem 1rem;
background: #fff;
z-index: 10;
}
.skip-link:focus { top: 1rem; }
.card-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(min(100%, 18rem), 1fr)); gap: 1.25rem; }
.content-card { padding: 1.5rem; border: 1px solid #c9cec4; border-radius: 1rem; background: #fffdf8; }
.content-card h2 { margin-top: 0; font-size: 1.4rem; }
.card-note { font-size: 0.9rem; color: #4b594e; }
.visually-hidden {
position: absolute;
width: 1px;
height: 1px;
padding: 0;
margin: -1px;
overflow: hidden;
clip-path: inset(50%);
white-space: nowrap;
border: 0;
}
.empty-state { padding: 1rem 1.25rem; border: 1px dashed #c9cec4; border-radius: 1rem; }
.table-wrap { overflow-x: auto; }
.inventory { border-collapse: collapse; width: 100%; min-width: 28rem; }
.inventory caption { text-align: left; padding-bottom: 0.6rem; color: #4b594e; }
.inventory th, .inventory td { text-align: left; padding: 0.6rem 0.8rem; border-bottom: 1px solid #c9cec4; vertical-align: top; }
.inventory thead th { border-bottom-width: 2px; }
.listing-controls { display: flex; flex-wrap: wrap; align-items: end; gap: 0.75rem 1rem; margin-bottom: 1rem; }
.listing-controls .field { display: grid; gap: 0.25rem; }
.listing-controls input, .listing-controls select, .listing-controls button { box-sizing: border-box; min-height: 2.75rem; font: inherit; padding: 0.45rem 0.7rem; border: 1px solid #8a9586; border-radius: 0.5rem; background: #fffdf8; color: inherit; }
.listing-controls input { min-width: min(100%, 16rem); }
.listing-controls button { background: #315f40; border-color: #315f40; color: #fff; cursor: pointer; }
.listing-controls :focus-visible { outline: 3px solid #825414; outline-offset: 2px; }
.result-count { color: #4b594e; }
.pager { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 1rem; margin-top: 1.5rem; }
.pager [aria-current] { font-weight: 650; }
/* L13: the account corner and the sign-in form */
.site-account { display: flex; flex-wrap: wrap; align-items: center; gap: 0.75rem; }
.site-account form { margin: 0; }
.site-account button, .sign-in button { box-sizing: border-box; min-height: 2.75rem; font: inherit; padding: 0.45rem 0.9rem; border: 1px solid #315f40; border-radius: 0.5rem; background: #315f40; color: #fff; cursor: pointer; }
.sign-in { display: grid; gap: 1rem; max-width: 24rem; }
.sign-in .field { display: grid; gap: 0.25rem; }
.sign-in input { box-sizing: border-box; min-height: 2.75rem; font: inherit; padding: 0.45rem 0.7rem; border: 1px solid #8a9586; border-radius: 0.5rem; background: #fffdf8; color: inherit; }
.form-error { padding: 0.75rem 1rem; border-left: 4px solid #a33a2e; background: #fbeeec; }
/* L17: the note above a preview */
.preview-note { padding: 0.75rem 1rem; border-left: 4px solid #315f40; background: #edf3ec; }
/* L18: Publish and Unpublish side by side; Unpublish is the quieter button */
.publish-controls { display: flex; flex-wrap: wrap; gap: 0.75rem; margin-block: 1.5rem; }
.publish-controls .unpublish button { background: #fffdf8; color: #315f40; }
/* L19: the message after a change, and the delete button */
.form-success { padding: 0.75rem 1rem; border-left: 4px solid #315f40; background: #edf3ec; }
.danger button { background: #a33a2e; border-color: #a33a2e; }
/* L20: the save messages inside the edit form; an empty one takes no space */
.save-alert, .save-status { padding: 0.75rem 1rem; border-left: 4px solid #315f40; background: #edf3ec; }
.save-alert { border-left-color: #a33a2e; background: #fbeeec; }
.save-alert:empty, .save-status:empty { display: none; }
4. Start the server again with run-what dev. Nothing is published on this start.
Check
- Signed in, open a draft from All pages, change its summary and choose Save changes. You stay on the edit page and the form says “Saved.”. Change it again and save: “Saved.” again, with no conflict.
- Open the same page in a second tab and save a change there. Back in the first tab, change the title and save: the form says “This page changed since you opened it.”, and your new title is still in the field. Reload to get the latest version.
- Clear the title and save: the form says “title: This field is required.” and keeps the other fields.
- Turn JavaScript off in your browser (or use a browser without it) and save: the page goes to Admin, as in lesson 16.
Exercise
Save a page in place, then open its preview in another tab. Explain in two sentences why the preview shows your change and the public page does not.
If something goes wrong
- Save still goes to the Admin page. The form is missing
w-inplace, JavaScript is off, or the page was opened before you changed the file; reload it. - “The save could not be confirmed. Reload before saving again.” The connection or the server failed while answering. Reload the edit page, check what it shows, and save again if needed.
- “This page changed since you opened it.” Someone saved after you opened the page, maybe you in another tab. Copy what you typed, reload, and make your change again.
- Start again from a known state. Download the L20 checkpoint into a new, empty folder; never unpack it over your project. It has no database and no account: run
run-what admin add editorin the new folder, thenrun-what dev. The first start imports the eight pages and publishes them once; create a draft with the form from lesson 15 before editing.
Next: back to the course.