Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion packages/editor-website/src/pages/en/index.astro
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@ const t = useTranslations('en');

<Section>
<div class="clippy-index__preview-bar">
<a class="nl-button nl-button--secondary" href="/preview">
<button type="button" id="clippy-reset" class="nl-button nl-button--subtle">
<span class="nl-button__label">Restore example content</span>
</button>
<a class="nl-button nl-button--secondary" id="clippy-preview-link" href="/preview">
<span class="nl-button__label">Preview</span>
</a>
</div>
Expand Down Expand Up @@ -100,10 +103,17 @@ const t = useTranslations('en');
</main>
</Document>

<script>
import { setupEditorPersistence } from '../../utils/editorPersistence';

setupEditorPersistence();
</script>

<style>
.clippy-index__preview-bar {
display: flex;
justify-content: flex-end;
gap: var(--basis-space-inline-sm);
padding-block-end: var(--basis-space-block-sm);
}
</style>
12 changes: 11 additions & 1 deletion packages/editor-website/src/pages/index.astro
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@ const t = useTranslations('nl');

<Section>
<div class="clippy-index__preview-bar">
<a class="nl-button nl-button--secondary" href="/preview">
<button type="button" id="clippy-reset" class="nl-button nl-button--subtle">
<span class="nl-button__label">Herstel voorbeeldinhoud</span>
</button>
<a class="nl-button nl-button--secondary" id="clippy-preview-link" href="/preview">
<span class="nl-button__label">Voorbeeld</span>
</a>
</div>
Expand Down Expand Up @@ -101,10 +104,17 @@ const t = useTranslations('nl');
</main>
</Document>

<script>
import { setupEditorPersistence } from '../utils/editorPersistence';

setupEditorPersistence();
</script>

<style>
.clippy-index__preview-bar {
display: flex;
justify-content: flex-end;
gap: var(--basis-space-inline-sm);
padding-block-end: var(--basis-space-block-sm);
}
</style>
59 changes: 58 additions & 1 deletion packages/editor-website/src/pages/preview.astro
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
---

import iconChevronLeft from '@tabler/icons/outline/chevron-left.svg?raw';
import { EDITOR_STORAGE_KEY } from '../utils/editorPersistence';
---

<html lang="nl" dir="ltr" class="ma-theme clippy-theme utrecht-root">
Expand All @@ -22,6 +23,12 @@
</script>
</head>
<body class="utrecht-body">
<header class="preview-header">
<a class="preview-header__back nl-button nl-button--subtle" href="/">
<span class="preview-header__icon" aria-hidden="true"><Fragment set:html={iconChevronLeft} /></span>
<span class="nl-button__label">Terug naar editor</span>
</a>
</header>
<clippy-context id="clippy-preview" readonly>
<div slot="value">
<h1>NL Design System Editor — voorbeeld</h1>
Expand Down Expand Up @@ -111,6 +118,32 @@
<p lang="ar" dir="rtl">هذه الفقرة باللغة العربية وتتدفق من اليمين إلى اليسار.</p>
</div>

<!--
Runs synchronously during parsing, before the deferred editor module
upgrades <clippy-context> and reads its "value" slot. This swaps in the
content edited on the home page (persisted to localStorage) so the
preview reflects the user's own document.
-->
<script is:inline define:vars={{ storageKey: EDITOR_STORAGE_KEY }}>

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add this script to the editorPersistence util as well? So all demo code is in the same file?

(function () {
try {
var saved = localStorage.getItem(storageKey);
if (!saved) return;
var slot = document.querySelector('#clippy-preview > div[slot="value"]');
if (slot) slot.innerHTML = saved;
} catch (error) {
/* localStorage may be unavailable (e.g. privacy mode); keep default content. */
}
})();

// When the page is restored from the back/forward (bfcache), scripts do
// not re-run and <clippy-context> keeps its previously validated content.
// Force a fresh load so the latest edits (and their validations) show.
window.addEventListener('pageshow', function (event) {
if (event.persisted) window.location.reload();
});
</script>

<div class="preview-layout">
<main class="preview-content">
<div class="preview-content__gutter-wrapper">
Expand All @@ -133,6 +166,30 @@
</clippy-context>

<style>
.preview-header {
display: flex;
align-items: center;
max-width: 1200px;
margin-inline: auto;
padding-inline: var(--basis-space-inline-xl, 1.5rem);
padding-block: var(--basis-space-block-md, 1rem);
}

.preview-header__back {
display: inline-flex;
align-items: center;
gap: var(--basis-space-inline-sm, 0.5rem);
}

.preview-header__icon {
display: inline-flex;
}

.preview-header__icon :global(svg) {
inline-size: 1.25em;
block-size: 1.25em;
}

.preview-layout {
display: grid;
grid-template-columns: 1fr 320px;
Expand Down
93 changes: 93 additions & 0 deletions packages/editor-website/src/utils/editorPersistence.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/**
* Shared helpers for persisting the demo editor's content in `localStorage` so
* that edits survive a page refresh and can be handed off to the preview page.
*
* Used by the home pages (`index.astro`, `en/index.astro`) to wire up saving,
* restoring and resetting, and by `preview.astro` to seed its read-only content.
*/

/** localStorage key shared by the editor pages and the preview page. */
export const EDITOR_STORAGE_KEY = 'clippy-editor-content';

/** Minimal surface of the TipTap editor instance exposed on `<clippy-editor>`. */
interface EditorLike {
getHTML(): string;
commands: { setContent(content: string, options?: { emitUpdate?: boolean }): boolean };
on(event: string, callback: () => void): void;
}

type EditorHost = HTMLElement & { editor?: EditorLike };

/**
* Wait for `<clippy-editor>` to upgrade and create its TipTap editor instance.
* Polls across animation frames because the instance is assigned asynchronously
* in the component's `firstUpdated()`.
*/
async function getEditor(el: EditorHost): Promise<EditorLike | undefined> {
await customElements.whenDefined('clippy-editor');
for (let i = 0; i < 300 && !el.editor; i += 1) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there not a ready() or equivalent available from within the editor?

await new Promise((resolve) => requestAnimationFrame(resolve));
}
return el.editor;
}

interface PersistenceOptions {
/** Id of the `<clippy-editor>` element. */
editorId?: string;
/** Id of the "reset to example content" button. */
resetButtonId?: string;
/** Id of the preview link/button. */
previewLinkId?: string;
}

/**
* Wire an editor page for persistence:
* - restores previously saved content on load,
* - saves to localStorage on every change,
* - resets to the original example content on demand,
* - flushes the latest content before navigating to the preview.
*/
export function setupEditorPersistence({
editorId = 'clippy-editor-localhost',
previewLinkId = 'clippy-preview-link',
resetButtonId = 'clippy-reset',
}: PersistenceOptions = {}): void {
const editorEl = document.getElementById(editorId) as EditorHost | null;
if (!editorEl) return;

const resetButton = document.getElementById(resetButtonId);
const previewLink = document.getElementById(previewLinkId);
const valueSlot = editorEl.querySelector('div[slot="value"]');

// Snapshot the initial slot markup so "reset" can restore the example content.
// Reset feeds this raw markup back through setContent(), which reproduces the
// exact same parse path the editor used on first render — so the reset result
// is byte-identical to the initial content.
const exampleHtml = valueSlot?.innerHTML ?? '';

getEditor(editorEl).then((editor) => {
if (!editor) return;

// Restore previously edited content without emitting an update (which would
// immediately re-save identical content).
const saved = localStorage.getItem(EDITOR_STORAGE_KEY);
if (saved) {
editor.commands.setContent(saved, { emitUpdate: false });
}

// Persist on every content change.
editor.on('update', () => {
localStorage.setItem(EDITOR_STORAGE_KEY, editor.getHTML());
});

resetButton?.addEventListener('click', () => {
editor.commands.setContent(exampleHtml);
localStorage.setItem(EDITOR_STORAGE_KEY, editor.getHTML());
});

// Guarantee the latest content is stored before navigating to the preview.
previewLink?.addEventListener('click', () => {
localStorage.setItem(EDITOR_STORAGE_KEY, editor.getHTML());
});
});
}