Introducing a new way to review your PRs.

Read the announcement

frappe / builder

Craft beautiful websites effortlessly with an intuitive visual builder and publish them instantly

View on GitHub

20 documents · Updated Nov 23, 2026

docs / editing-state-and-history

Editing state and history

Editing state and history keep multi-step page work safe. The editor separates working page content from published content, tracks local changes for undo and redo, saves drafts in the background, and uses snapshots for durable recovery. These rules connect the canvas to the page store, the version service, and the live collaboration channel. For the wider editor frame, see Visual builder workspace.

State model

The editor uses shared state to keep the page, the active canvas, and the workspace controls aligned. This state describes what the user is editing now, what can be saved, and which actions must be blocked.

Workspace stores

The builder store owns workspace state such as the current mode, read-only status, open panels, active viewers, and AI build ownership. It separates AI availability from active AI canvas ownership and tracks whether the AI setup state is known, so the editor does not treat an unavailable or still-loading AI service as an active build. The canvas store owns the active canvas, selection, fragment editing, and version preview. The page store owns the active page, its draft, route data, page data, load state, save state, and page-level version actions. Their shared boundary is the active page shown through the active canvas. builderStore.ts canvasStore.ts pageStore.ts

The page store also guards page changes with a load token. It applies an asynchronous page result only while the selected page is still current, including delayed page actions such as marking a page as staging. The stores do not treat every change as a page save. Canvas edits change the working block tree first. The page store later serializes that tree for draft storage. This separation lets the editor support local undo without making each small step a server write. Page selection and route effects connect to Page navigation and routing, while page data and scripts connect to Client-side code and data blocks.

References: frontend/…/builderStore.ts• L24-87 frontend/…/canvasStore.ts• L13-65 frontend/…/pageStore.ts• L45-63 frontend/…/pageStore.ts• L65-154 frontend/…/pageStore.ts• L255-277

Editing modes

Page mode edits the current page draft. Fragment mode edits a separate copy of a reusable component or block template. The copy must pass through its save action before it changes the reusable owner, so unfinished fragment work does not spread to all page uses. canvasStore.ts PageBuilder.vue

Read-only state can come from site maintenance, a protected page, or version preview. It blocks writes and also disables local canvas history. AI build state adds a separate ownership rule: while the server streams a build, normal editor autosave must stand down. For the user-facing AI flow, see AI client experience. PageBuilder.vue builderStore.ts BuilderCanvas.vue

References: frontend/…/canvasStore.ts• L168-217 frontend/…/PageBuilder.vue• L152-172 frontend/…/PageBuilder.vue• L210-217 frontend/…/BuilderCanvas.vue• L293-305

Builder events

Builder events turn browser actions into changes to the active canvas or page session. One event layer handles keyboard shortcuts, clipboard actions, mode changes, block deletion, and page visibility changes. It checks the current editing and read-only state before it changes content.

Event routing

The builder event layer sends user actions to the active canvas and its controllers. Clipboard input can add blocks, update HTML, or apply styles. Delete and cut remove selected blocks only when the editor allows writes. Save shortcuts use the fragment save path when the user is editing a fragment. useBuilderEvents.ts useBuilderEvents.ts useBuilderEvents.ts

These actions change the same block tree watched by canvas history and draft autosave. The event layer therefore does not own persistence. It starts a change, while the canvas history records a local step and the page store later saves the resulting draft. For pointer and drop behavior, see Editor canvas interaction. Property changes use the same edit boundary; see Property and style editing.

References: frontend/…/useBuilderEvents.ts• L55-80 frontend/…/useBuilderEvents.ts• L110-165 frontend/…/useBuilderEvents.ts• L221-275

Change grouping

Continuous gestures must appear as one useful edit rather than many small edits. Drag, resize, text, color, and reorder flows can pause history while the gesture runs, then resume with one commit when the value is complete. This keeps undo aligned with the user’s action. useCanvasHistory.ts useDraggableBlock.ts useBlockReorder.ts

References: frontend/…/useCanvasHistory.ts• L150-181 frontend/…/useDraggableBlock.ts• L30-61 frontend/…/useBlockReorder.ts• L70-86

Canvas history

Canvas history provides fast local recovery while a user works. It records the block tree and the selected block set, so undo restores both page content and the editing focus that gives the next action its meaning.

Undo and redo

The history service watches the active block tree and selection. It groups nearby changes with a short delay, keeps separate undo and redo stacks, and clears redo steps after a new edit. The stacks are local to the canvas and have a fixed capacity, so they are not a substitute for saved versions. useCanvasHistory.ts useCanvasHistory.ts useCanvasHistory.ts

The command system exposes undo and redo to the editor shortcuts. The active canvas decides whether a step is available, which keeps commands valid when the user changes from a page canvas to a fragment canvas. Commands/index.ts

References: frontend/…/useCanvasHistory.ts• L6-24 frontend/…/useCanvasHistory.ts• L64-85 frontend/…/useCanvasHistory.ts• L109-143 frontend/…/index.ts• L289-312

History boundaries

Loading a new page creates a new history context. A version preview swaps the visible root without recording that swap, then restores the draft and keeps the draft’s undo and redo stacks. A read-only canvas disables both recording and navigation until editing is allowed again. useCanvasUtils.ts useCanvasUtils.ts useCanvasHistory.ts

These boundaries prevent inspection from becoming an edit. They also keep local recovery meaningful: after a preview ends, undo applies to the draft the user was editing rather than to the version that was only viewed.

References: frontend/…/useCanvasUtils.ts• L113-115 frontend/…/useCanvasUtils.ts• L161-177 frontend/…/useCanvasHistory.ts• L99-115 frontend/…/BuilderCanvas.vue• L299-305

Version history

Version history provides durable page checkpoints beyond the local undo window. A snapshot stores page fields at a point in time. Publish snapshots preserve content that went live, while manual snapshots give users named recovery points.

Snapshot lifecycle

The page service captures a publish snapshot before it moves the draft into the live page. A manual snapshot captures the current working content and can carry a user label. Both snapshot types include the page content and page data script, and each type has its own retention limit. builder_page.py builder_page.py

Snapshots use a shared record model that stores the source document, captured fields, label, and type. The model can rebuild a view of the source document without changing it. This allows the editor to show an old version before the user chooses to restore it. builder_snapshot.py builder_snapshot.py

References: builder/…/builder_page.py• L346-363 builder/…/builder_page.py• L375-394 builder/…/builder_snapshot.py• L28-63 builder/…/builder_snapshot.py• L66-88

Version preview

The version history panel lists the current draft, the published page when needed, and saved page snapshots. Selecting a version replaces the visible canvas in read-only mode. The current draft stays backed up so the user can leave preview without changing working content. VersionHistory.vue VersionHistory.vue canvasStore.ts

The preview path is different from restore. Preview is inspection only. Restore loads the selected snapshot into the page’s draft, reloads the editor, and leaves the live page unchanged until a later publish. VersionHistory.vue builder_page.py

References: frontend/…/VersionHistory.vue• L42-89 frontend/…/VersionHistory.vue• L91-125 frontend/…/canvasStore.ts• L67-101 frontend/…/VersionHistory.vue• L247-260 frontend/…/pageStore.ts• L291-318 builder/…/builder_page.py• L396-420

Component version pins

Page snapshots protect reusable content as well as page layout. A component version pin records the component state used by a page instance, including its block and data script. A restored page can therefore keep the component version that the snapshot saw, rather than silently taking a later shared edit. For the broader reuse model, see Block composition system and Assets and block templates. component_versions.py component_versions.py

If a pinned version is no longer available, resolution falls back to the live component. Restore can report a warning when a referenced component cannot render. This makes version recovery usable while still exposing missing dependencies. component_versions.py component_versions.py

References: builder/…/component_versions.py• L4-17 builder/…/component_versions.py• L52-67 builder/…/component_versions.py• L172-200 builder/…/component_versions.py• L225-240

Realtime coordination

Realtime coordination gives the editor awareness of other users on the same page and helps it refresh stale page data. It is a presence and freshness channel, not a shared character-by-character undo system.

Page presence

When the builder opens a page, it subscribes to that page’s document and marks it open. The server can then send the current viewers, which the builder store turns into user information for the toolbar. When the page closes or the route changes, the editor closes the old document and opens the new one. PageBuilder.vue realtimeHandler.ts realtimeHandler.ts

The realtime wrapper also exposes event listeners and message publishing through the shared socket. Subscription calls are throttled and tracked by document, which limits repeated page-open requests during route changes. The handler tracks each open document so an unsubscribe removes only that document’s subscription. In editor demo mode, it does not open a socket because the demo has no live collaborators. realtimeHandler.ts realtimeHandler.ts realtimeHandler.ts

References: frontend/…/PageBuilder.vue• L250-283 frontend/…/PageBuilder.vue• L306-310 frontend/…/realtimeHandler.ts• L5-14 frontend/…/realtimeHandler.ts• L16-45 frontend/…/realtimeHandler.ts• L47-79

Freshness checks

When the browser tab becomes visible again, the builder checks the page’s modified value against a fresh page read. If the value changed, it reloads the page into the canvas. This limits the time that an inactive editor can show stale content. useBuilderEvents.ts

This refresh path does not merge two local block trees. It relies on page loading to replace the canvas state, so save state and page ownership must be clear before a refresh or route switch.

References: frontend/…/useBuilderEvents.ts• L424-437 frontend/…/pageStore.ts• L65-108

Save-oriented state

Save-oriented state connects fast canvas edits to durable page data. The editor marks the canvas as dirty for local interaction, then uses a delayed page save to reduce server writes. The page store tracks in-flight saves and identifies the current save request so publish, staging changes, and restore can wait for a stable draft.

Draft autosave

In page mode, a deep change to the page canvas starts a short delayed save. The save reads the current block tree, removes runtime parent links, and sends the result as draft_blocks. It does not run while the editor is read-only, while AI owns the canvas, or while the page is still being loaded. PageBuilder.vue pageStore.ts

The saved draft is separate from the published blocks value. Publish first waits for pending draft saves, then promotes the draft and captures the content that goes live. pageStore.ts builder_page.py

References: frontend/…/PageBuilder.vue• L325-347 frontend/…/pageStore.ts• L361-410 frontend/…/pageStore.ts• L238-253 builder/…/builder_page.py• L346-363

Page data state

The page store keeps route variables and data returned by page data scripts next to the active draft. It reloads that data when the active page or route variables change, so data-driven blocks use the values for the current page route. Apps can also provide page data through the page-data hook, so a page can have editor data even when it has no page data script. pageStore.ts

Generated repeater data can also be saved to the page through the same store. The store updates the page script and refreshes page data after the save, so the canvas can render the new repeater content without a separate page reload. pageStore.ts

References: frontend/…/pageStore.ts• L413-455

Save synchronization

The page store marks a save as active and gives each request an identity. A newer request may start before an older one finishes, but only the latest request can clear the shared saving state. This prevents an older response from telling publish or restore that the draft is already safe. pageStore.ts

Publish and snapshot restore wait until saving ends. Page status changes also capture their target page before waiting, then apply a refetch only if that page is still active. This prevents a route change during the action from replacing the page currently being edited. Restore then writes the selected content into the draft and reloads the page. pageStore.ts pageStore.ts

The main editing sequence is therefore:

sequenceDiagram
    participant U as User
    participant C as Canvas
    participant H as Local history
    participant P as Page store
    participant S as Page service

    U->>C: Make an edit
    C->>H: Record a grouped local step
    C->>P: Mark draft as changed
    P->>S: Save draft after delay
    U->>P: Publish, stage, or restore
    P->>P: Wait for active save
    P->>S: Apply page action

References: frontend/…/pageStore.ts• L361-410 frontend/…/pageStore.ts• L238-253 frontend/…/pageStore.ts• L255-277 frontend/…/pageStore.ts• L301-318 frontend/…/pageStore.ts• L556-568

Change checklist

When these files or sections are changed, remember to consider these:

  • Keep page mode and fragment mode on separate save paths. A fragment edit must not autosave into the page draft.
  • Keep read-only checks aligned across event handlers, canvas history, autosave, version preview, and publish actions.
  • Keep the local history boundary separate from snapshot preview. A preview must not create an undo step or discard the draft stack.
  • Keep draft_blocks, published blocks, page data scripts, and snapshot restore fields compatible.
  • Keep save request identity and the saving state correct when more than one autosave can be in flight.
  • Keep page load tokens from allowing an old page response or delayed page action to replace a newer route selection.
  • Keep page status actions tied to the page that started them. A route change must not apply a refetch to the new active page.
  • Keep page route changes paired with realtime close, subscribe, and open calls.
  • Keep component version pins and restore warnings aligned when snapshot data or component resolution changes.
  • Keep AI configuration state separate from active AI canvas ownership. Configuration can enable controls, but ownership must still block autosave during a live build.
  • Keep AI canvas ownership from racing with editor autosave or page restore.
  • Keep route variable changes and generated repeater data in sync with page data refreshes.
  • Preserve page-data hook results when changing the rule for pages without a page data script.

Important failure modes

TriggerConsequenceGuard
A version preview writes through the normal canvas pathThe viewed version can replace the working draft or add false undo stepsUse silent root replacement and read-only history during preview
A new edit starts after an older save requestThe older response can clear saving state too soonClear the saving state only when the latest save identity completes
Publish runs before draft saving endsThe live page can miss the last canvas editWait for waitTillPageIsSaved() before publish
An older page request finishes after a newer page selectionThe canvas can show the wrong page and save edits to the wrong targetCompare the request token and selected page before applying the result
A page route changes while Mark as Staging is waiting for confirmation or save completionThe status action can refetch the wrong page into the active editorCapture the target page and load token, then update activePage only when the target is still active
Autosave runs during an AI canvas streamPartial generated content can overwrite the intended draftBlock page saves while AI owns the canvas
AI controls appear before configuration state is knownA user can start an unavailable AI flow or see a false disabled stateKeep unknown, configured, and active-build state separate
A fragment exits without its save actionA reusable component or template can lose its editRequire save or explicit discard confirmation
A restored snapshot references a missing component versionPart of the restored page may not renderResolve the pin, fall back when safe, and surface restore warnings
A route changes without closing the old realtime documentViewer state and page freshness can refer to the wrong pageClose the old document before opening the new one
Route variables change without a page data refreshData-driven blocks can show values for the previous routeStore the variable and reload page data for the active page
A page-data hook result is cleared because the page has no scriptData-driven blocks can lose app-provided data in the editorAllow page data loading for pages without a page data script

Key files

FileWhy to read it
frontend/…/builderStore.tsDefines workspace-wide read-only, viewer, mode, AI setup, and AI ownership state
frontend/…/canvasStore.tsDefines active canvas, fragment boundaries, and version preview state
frontend/…/pageStore.tsCoordinates page loading, stale-load protection, route data, draft saves, page status changes, publish, snapshot creation, restore, and app-provided page data
frontend/…/PageBuilder.vueConnects stores to page and fragment canvases, realtime presence, route changes, and autosave
frontend/…/useBuilderEvents.tsShows how browser actions enter the editing flow and how freshness checks reload a page
frontend/…/useCanvasHistory.tsDefines local undo, redo, grouping, and read-only history behavior
frontend/…/useCanvasUtils.tsDefines history setup, root replacement, and canvas dirty state
frontend/…/VersionHistory.vueShows the user-facing preview, manual save, and restore flow
frontend/…/realtimeHandler.tsWraps page presence, document subscriptions, demo-mode behavior, and socket messages
builder/…/builder_snapshot.pyDefines durable snapshot capture, lookup, restore, and retention behavior
builder/…/builder_page.pyDefines page publish, manual snapshot, and draft restore behavior
builder/…/component_versions.pyExplains how snapshots keep reusable component dependencies stable