Repository navigation
feat: wire editor to REST load, autosave, reload persistence #289
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
ViewFromTheBox
merged 2 commits into
add/phase-one
from
enhancement/275-wire-editor-rest-load-save-persistence
Apr 14, 2026
Merged
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
141 changes: 141 additions & 0 deletions
141
resources/js/visual-editor/editor/components/EditorBoot.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,141 @@ | ||
| import { useCallback, useEffect, useMemo, useRef, useState } from 'react'; | ||
| import { EditorShell } from './EditorShell'; | ||
| import { | ||
| fetchPost, | ||
| useAutosave, | ||
| type AutosaveState, | ||
| type PostRestClientOptions, | ||
| } from '../rest'; | ||
| import { createEditorStore, type Block, type EditorStore } from '../store'; | ||
|
|
||
| export interface EditorBootProps { | ||
| postId: string; | ||
| postType: string; | ||
| apiBase: string; | ||
| } | ||
|
|
||
| type LoadState = | ||
| | { status: 'loading' } | ||
| | { status: 'error'; error: Error } | ||
| | { status: 'ready'; store: EditorStore }; | ||
|
|
||
| export function EditorBoot({ postId, apiBase }: EditorBootProps) { | ||
| const clientOptions = useMemo<PostRestClientOptions>( | ||
| () => ({ apiBase }), | ||
| [apiBase] | ||
| ); | ||
|
|
||
| const [loadState, setLoadState] = useState<LoadState>({ status: 'loading' }); | ||
| const [loadAttempt, setLoadAttempt] = useState(0); | ||
| const abortRef = useRef<AbortController | null>(null); | ||
|
|
||
| useEffect(() => { | ||
| const controller = new AbortController(); | ||
| abortRef.current = controller; | ||
|
|
||
| setLoadState({ status: 'loading' }); | ||
|
|
||
| fetchPost(postId, clientOptions, { signal: controller.signal }) | ||
| .then((payload) => { | ||
| if (controller.signal.aborted) { | ||
| return; | ||
| } | ||
|
|
||
| const store = createEditorStore(payload.blocks as Block[]); | ||
| setLoadState({ status: 'ready', store }); | ||
| }) | ||
| .catch((error: unknown) => { | ||
| if (controller.signal.aborted) { | ||
| return; | ||
| } | ||
|
|
||
| const wrapped = | ||
| error instanceof Error ? error : new Error('Failed to load post.'); | ||
| setLoadState({ status: 'error', error: wrapped }); | ||
| }); | ||
|
|
||
| return () => { | ||
| controller.abort(); | ||
| abortRef.current = null; | ||
| }; | ||
| }, [postId, clientOptions, loadAttempt]); | ||
|
|
||
| const retry = useCallback(() => { | ||
| setLoadAttempt((attempt) => attempt + 1); | ||
| }, []); | ||
|
|
||
| if (loadState.status === 'loading') { | ||
| return ( | ||
| <div | ||
| className="ve-editor-boot ve-editor-boot--loading" | ||
| data-ve-editor-boot="loading" | ||
| role="status" | ||
| aria-live="polite" | ||
| > | ||
| Loading editor… | ||
| </div> | ||
| ); | ||
| } | ||
|
|
||
| if (loadState.status === 'error') { | ||
| return ( | ||
| <div | ||
| className="ve-editor-boot ve-editor-boot--error" | ||
| data-ve-editor-boot="error" | ||
| role="alert" | ||
| > | ||
| <p className="ve-editor-boot__message"> | ||
| Failed to load the post: {loadState.error.message} | ||
| </p> | ||
| <button | ||
| type="button" | ||
| className="ve-editor-boot__retry" | ||
| onClick={retry} | ||
| > | ||
| Retry | ||
| </button> | ||
| </div> | ||
| ); | ||
| } | ||
|
|
||
| return ( | ||
| <EditorBootReady | ||
| store={loadState.store} | ||
| postId={postId} | ||
| clientOptions={clientOptions} | ||
| /> | ||
| ); | ||
| } | ||
|
|
||
| interface EditorBootReadyProps { | ||
| store: EditorStore; | ||
| postId: string; | ||
| clientOptions: PostRestClientOptions; | ||
| } | ||
|
|
||
| function EditorBootReady({ store, postId, clientOptions }: EditorBootReadyProps) { | ||
| const autosave = useAutosave({ store, postId, clientOptions }); | ||
|
|
||
| return ( | ||
| <> | ||
| <AutosaveAnnouncer state={autosave} /> | ||
| <EditorShell store={store} saveStatus={autosave} /> | ||
| </> | ||
| ); | ||
| } | ||
|
|
||
| function AutosaveAnnouncer({ state }: { state: AutosaveState }) { | ||
| if (state.status !== 'error' || !state.lastError) { | ||
| return null; | ||
| } | ||
|
|
||
| return ( | ||
| <div | ||
| className="ve-editor-boot__toast" | ||
| data-ve-editor-boot="save-error" | ||
| role="alert" | ||
| > | ||
| Failed to save: {state.lastError.message} | ||
| </div> | ||
| ); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 108 additions & 0 deletions
108
resources/js/visual-editor/editor/components/__tests__/EditorBoot.test.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| import { render, screen, waitFor } from '@testing-library/react'; | ||
| import userEvent from '@testing-library/user-event'; | ||
| import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; | ||
| import { EditorBoot } from '../EditorBoot'; | ||
| import { | ||
| clearRegistry, | ||
| registerBlock, | ||
| type BlockEditProps, | ||
| } from '../../registry'; | ||
|
|
||
| function Paragraph({ attributes, clientId }: BlockEditProps) { | ||
| return <p data-client-id={clientId}>{String(attributes.content ?? '')}</p>; | ||
| } | ||
|
|
||
| const originalFetch = globalThis.fetch; | ||
|
|
||
| beforeEach(() => { | ||
| clearRegistry(); | ||
| registerBlock({ name: 'core/paragraph', edit: Paragraph }); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| clearRegistry(); | ||
| globalThis.fetch = originalFetch; | ||
| vi.restoreAllMocks(); | ||
| }); | ||
|
|
||
| function jsonResponse(body: unknown, status = 200): Response { | ||
| return new Response(JSON.stringify(body), { | ||
| status, | ||
| headers: { 'Content-Type': 'application/json' }, | ||
| }); | ||
| } | ||
|
|
||
| describe('EditorBoot', () => { | ||
| it('fetches the post on mount and renders the editor', async () => { | ||
| const payload = { | ||
| id: 1, | ||
| title: 'Test', | ||
| blocks: [ | ||
| { | ||
| clientId: 'loaded-1', | ||
| name: 'core/paragraph', | ||
| attributes: { content: 'Loaded from REST' }, | ||
| innerBlocks: [], | ||
| }, | ||
| ], | ||
| updated_at: '2026-04-14T12:00:00+00:00', | ||
| }; | ||
|
|
||
| globalThis.fetch = vi | ||
| .fn() | ||
| .mockResolvedValue(jsonResponse(payload)) as unknown as typeof fetch; | ||
|
|
||
| render( | ||
| <EditorBoot postId="1" postType="post" apiBase="/visual-editor/api" /> | ||
| ); | ||
|
|
||
| expect(screen.getByText('Loading editor…')).toBeInTheDocument(); | ||
|
|
||
| await waitFor(() => { | ||
| expect(screen.getByText('Loaded from REST')).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| expect(document.querySelector('[data-ve-editor-shell]')).not.toBeNull(); | ||
| expect(globalThis.fetch).toHaveBeenCalledWith( | ||
| '/visual-editor/api/posts/1', | ||
| expect.objectContaining({ method: 'GET' }) | ||
| ); | ||
| }); | ||
|
|
||
| it('renders the error state with a retry button on load failure', async () => { | ||
| const fetchMock = vi | ||
| .fn() | ||
| .mockResolvedValueOnce( | ||
| new Response(JSON.stringify({ message: 'Nope' }), { status: 500 }) | ||
| ) | ||
| .mockResolvedValueOnce( | ||
| jsonResponse({ | ||
| id: 1, | ||
| title: 'Test', | ||
| blocks: [], | ||
| updated_at: '2026-04-14T12:00:00+00:00', | ||
| }) | ||
| ); | ||
|
|
||
| globalThis.fetch = fetchMock as unknown as typeof fetch; | ||
|
|
||
| render( | ||
| <EditorBoot postId="1" postType="post" apiBase="/visual-editor/api" /> | ||
| ); | ||
|
|
||
| await waitFor(() => { | ||
| expect(screen.getByRole('alert')).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| expect(screen.getByText(/Failed to load the post/)).toBeInTheDocument(); | ||
|
|
||
| const user = userEvent.setup(); | ||
| await user.click(screen.getByRole('button', { name: /retry/i })); | ||
|
|
||
| await waitFor(() => { | ||
| expect(document.querySelector('[data-ve-editor-shell]')).not.toBeNull(); | ||
| }); | ||
|
|
||
| expect(fetchMock).toHaveBeenCalledTimes(2); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.