Update page title to indicate editing flows
This commit is contained in:
+27
-3
@@ -15,9 +15,12 @@ import PageEditor from './PageEditor.vue'
|
||||
import SiteEditor from './SiteEditor.vue'
|
||||
|
||||
let host = null
|
||||
let app = null
|
||||
let savedTitle = null
|
||||
|
||||
export function openEditor(path, { mode = 'page' } = {}) {
|
||||
closeEditor()
|
||||
savedTitle = document.title
|
||||
host = document.createElement('div')
|
||||
host.className = 'editor-host'
|
||||
// Docked inside #content: below the banner, next to the article only.
|
||||
@@ -26,10 +29,11 @@ export function openEditor(path, { mode = 'page' } = {}) {
|
||||
// Which kind of editor is open; pagerite.js uses this to decide
|
||||
// whether a pen click closes the panel or swaps in the other editor.
|
||||
document.body.dataset.editorMode = mode
|
||||
createApp(mode === 'site' ? SiteEditor : PageEditor, {
|
||||
app = createApp(mode === 'site' ? SiteEditor : PageEditor, {
|
||||
pagePath: path,
|
||||
onClose: closeEditor,
|
||||
}).mount(host)
|
||||
})
|
||||
app.mount(host)
|
||||
// The slide-in (editor-slide-in in pagerite.css) is a one-shot open
|
||||
// effect; once finished, drop it so that later stylesheet swaps (theme
|
||||
// change re-creating @keyframes) cannot restart it.
|
||||
@@ -48,6 +52,26 @@ export function closeEditor() {
|
||||
// Slide the panel out in sync with the page shifting back.
|
||||
host.firstElementChild?.classList.add('closing')
|
||||
const old = host
|
||||
const oldApp = app
|
||||
const restoreTitle = savedTitle
|
||||
host = null
|
||||
setTimeout(() => old.remove(), 250)
|
||||
app = null
|
||||
savedTitle = null
|
||||
setTimeout(() => {
|
||||
oldApp?.unmount()
|
||||
old.remove()
|
||||
if (host) return // a new editor has opened; its title is authoritative
|
||||
// Restore the server-rendered title for the current URL. Re-fetching makes
|
||||
// sure a brand change in the site editor or an in-place navigation leaves
|
||||
// the correct public title behind.
|
||||
fetch(location.pathname)
|
||||
.then((r) => r.text())
|
||||
.then((html) => {
|
||||
const doc = new DOMParser().parseFromString(html, 'text/html')
|
||||
if (doc.title) document.title = doc.title
|
||||
})
|
||||
.catch(() => {
|
||||
if (restoreTitle != null) document.title = restoreTitle
|
||||
})
|
||||
}, 250)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user