55 days ago · Tech · hide · Fediverse post

Saving future me from past me.If you’re like me and using Astro + Sveltia CMS for your website, you might want to add this script. Last week, I lost all my changes from a draft after spending over 20 minutes writing it, and it was incredibly frustrating. Sveltia does have a restore option that appears when you reopen the page, but I find it a little finicky. I’d much rather have a warning before I accidentally close or refresh the page. I honestly thought this was already built in. diff --git a/src/pages/admin/index.astro b/src/pages/admin/index.astro index 443089b8..e7f27483 100644 --- a/src/pages/admin/index.astro +++ b/src/pages/admin/index.astro @@ -19,5 +19,26 @@ init({ config: cmsConfig }); </script> + + <script> + let isDirty = false; + + function isInEditor() { + const hash = location.hash; + return hash.includes('/entries/') || hash.endsWith('/new'); + } + + for (const event of ['input', 'change']) { + document.addEventListener(event, () => { + if (isInEditor()) isDirty =…

Commenting on Bubbles has been removed. Why? If you want to talk about this post, write to the author, use the comments on their blog if it has them, or reply with a "RE:" post on your own blog. Suggestions? feedback@bubbles.town