feat(site-builder): reusable global sections with per-page overrides
Make a top-level section global (context menu), place instances from the
Section library's new Global tab, edit the master once in its own zone
("Editing global section" banner, Done - update N instances) and every
instance on every page/header/footer is rebuilt from it. Instances are real
inlined subtrees (custom.global on the root, custom.gkey on every node), so
export and published output are unchanged static HTML.
- utils/global-sections.ts: pure model (createMaster, instantiate,
syncInstances, collectOverridesFromInstance, applyOverrides, detach,
findInstanceRoot, master zone round trip). Unchanged input returns the
identical string; sync keeps the instance root id and the instance's own
inner ids per gkey.
- PageContext: __global__:<id> zone (editGlobal / finishGlobal); leaving it
commits the master (rev + 1 only when it changed), syncs every stored
slot and applies the sync to the returned-to canvas as ONE undo step.
- GlobalSectionsContext: makeGlobal / insertInstance / detach / setOverride
/ resetOverride / remove (refused while used), one undo step each.
- Inline text edits inside an instance record an override (text + html)
via useGlobalOverrideCommit; GuidedStyles shows a Global section panel
(Edit master, Detach, content override fields) instead of style panels.
- RenderNode: Global badge, dashed outline, lock icon on inner nodes.
- buildSavePayload: a master zone on the canvas is never saved into a page.
Refs #57
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
48061d37ae
commit
dd8b99be06
15 files changed
+1898
-29
No files matched your search
@@ -10,6 +10,7 @@ import { useMobileChrome } from '../state/MobileChromeContext';
|
||||
import { useSitesmithModal } from '../state/SitesmithContext';
|
||||
import { insertBlock } from '../panels/left/blocks-registry';
|
||||
import { openSectionLibrary } from '../panels/sections/SectionLibraryModal';
|
||||
import { useGlobalSections } from '../state/GlobalSectionsContext';
|
||||
|
||||
interface CanvasProps {
|
||||
/** Item 10: when false, applies `.guides-off` to hide the dashed drop-target guides. */
|
||||
@@ -156,9 +157,16 @@ export const EmptyCanvasHint: React.FC = () => {
|
||||
export const Canvas: React.FC<CanvasProps> = ({ showGuides }) => {
|
||||
const { device } = useDevice();
|
||||
const width = DEVICE_WIDTHS[device];
|
||||
const { isEditingHeader, isEditingFooter, headerPage, footerPage } = usePages();
|
||||
const { isEditingHeader, isEditingFooter, isEditingGlobal, globalEditingId, headerPage, footerPage, pages } = usePages();
|
||||
const globals = useGlobalSections();
|
||||
|
||||
const isEditingRegularPage = !isEditingHeader && !isEditingFooter;
|
||||
const isEditingRegularPage = !isEditingHeader && !isEditingFooter && !isEditingGlobal;
|
||||
// #57: instances the master feeds (stored pages; the canvas holds the master).
|
||||
const instanceCount = useMemo(
|
||||
() => (globalEditingId ? globals.usage()[globalEditingId] ?? 0 : 0),
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
[globalEditingId, pages, headerPage, footerPage],
|
||||
);
|
||||
|
||||
// Fast-follow item 4: while `MobileSelectionToolbar` is up (fixed, ~53px
|
||||
// tall, just above the tab bar), it covers whatever content was at the
|
||||
@@ -238,6 +246,24 @@ export const Canvas: React.FC<CanvasProps> = ({ showGuides }) => {
|
||||
</div>
|
||||
)}
|
||||
|
||||
{isEditingGlobal && globalEditingId && (
|
||||
<div className="global-master-banner" data-testid="global-master-banner">
|
||||
<i className="fa fa-globe" aria-hidden />
|
||||
<span className="global-master-banner-text">
|
||||
Editing global section: <strong>{globals.library[globalEditingId]?.name ?? 'Global section'}</strong>
|
||||
{' '}-- changes apply everywhere it is used
|
||||
</span>
|
||||
<button
|
||||
type="button"
|
||||
className="global-master-banner-done"
|
||||
data-testid="global-master-done"
|
||||
onClick={() => globals.finishEditingMaster()}
|
||||
>
|
||||
Done - update {instanceCount} {instanceCount === 1 ? 'instance' : 'instances'}
|
||||
</button>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{isEditingRegularPage && (
|
||||
<ZonePreview craftState={headerPage.craftState} zone="header" />
|
||||
)}
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
import React, { useCallback, useEffect, useMemo, useRef } from 'react';
|
||||
import React, { useCallback, useContext, useEffect, useMemo, useRef } from 'react';
|
||||
import { createPortal } from 'react-dom';
|
||||
import { NodeElement, useEditor, useNode } from '@craftjs/core';
|
||||
import { useDevice } from '../state/DeviceContext';
|
||||
import { hasAnyOverride, hasActiveOverride, resolveStyleForDevice, type ResponsiveProps } from '../utils/responsive-style';
|
||||
import type { DeviceMode } from '../types';
|
||||
import { GlobalInstanceContext } from '../state/GlobalInstanceContext';
|
||||
|
||||
interface RenderNodeProps {
|
||||
render: React.ReactElement;
|
||||
@@ -44,6 +45,12 @@ interface RenderNodeProps {
|
||||
* - `data-hide-desktop|tablet|mobile` / `data-craft-selected` (#37): mirror
|
||||
* the node's hide flags and selection for the device-emulation CSS.
|
||||
*
|
||||
* - `data-wsb-global` / `data-wsb-global-inner` (#57): global-section
|
||||
* instance root (dashed accent outline) / a node inside one (locked
|
||||
* styling; content edits become overrides). Every node is wrapped in a
|
||||
* `GlobalInstanceContext` provider (always, so the element type never
|
||||
* changes) carrying the nearest instance root id.
|
||||
*
|
||||
* Per-breakpoint styles (#45/#37): when the device is not desktop and the
|
||||
* node carries `styleTablet`/`styleMobile`, this is the ONE place the
|
||||
* effective style is resolved (`resolveStyleForDevice`), so no component
|
||||
@@ -89,7 +96,8 @@ const ResolvedRender: React.FC<{ device: DeviceMode }> = ({ device }) => {
|
||||
export const RenderNode: React.FC<RenderNodeProps> = ({ render }) => {
|
||||
const { actions } = useEditor();
|
||||
const { device } = useDevice();
|
||||
const { id, isSelected, dom, name, parent, isCanvas, isHovered, isComponent, tabletOverride, mobileOverride, hideDesktop, hideTablet, hideMobile } = useNode((node) => {
|
||||
const parentInstance = useContext(GlobalInstanceContext);
|
||||
const { id, isSelected, dom, name, parent, isCanvas, isHovered, isComponent, tabletOverride, mobileOverride, hideDesktop, hideTablet, hideMobile, isInstanceRoot } = useNode((node) => {
|
||||
// Keep the collector cheap (booleans only); ResolvedRender collects the
|
||||
// node's type/props/children itself, as Craft's DefaultRender does.
|
||||
const props = node.data.props as ResponsiveProps | undefined;
|
||||
@@ -107,8 +115,11 @@ export const RenderNode: React.FC<RenderNodeProps> = ({ render }) => {
|
||||
hideDesktop: !!node.data.props?.hideOnDesktop,
|
||||
hideTablet: !!node.data.props?.hideOnTablet,
|
||||
hideMobile: !!node.data.props?.hideOnMobile,
|
||||
isInstanceRoot: !!(node.data.custom?.global as { libraryId?: string } | undefined)?.libraryId,
|
||||
};
|
||||
});
|
||||
const instanceRoot = isInstanceRoot ? id : parentInstance;
|
||||
const isInstanceInner = !isInstanceRoot && !!parentInstance;
|
||||
|
||||
// Overrides that apply on the current device (mobile inherits tablet's).
|
||||
const activeOverride = device === 'tablet' ? tabletOverride : device === 'mobile' ? mobileOverride : false;
|
||||
@@ -180,18 +191,38 @@ export const RenderNode: React.FC<RenderNodeProps> = ({ render }) => {
|
||||
}
|
||||
}, [dom, activeOverride]);
|
||||
|
||||
useEffect(() => {
|
||||
if (!dom) return;
|
||||
if (isInstanceRoot) dom.setAttribute('data-wsb-global', '');
|
||||
else dom.removeAttribute('data-wsb-global');
|
||||
if (isInstanceInner) dom.setAttribute('data-wsb-global-inner', '');
|
||||
else dom.removeAttribute('data-wsb-global-inner');
|
||||
}, [dom, isInstanceRoot, isInstanceInner]);
|
||||
|
||||
// String-typed (plain DOM) nodes go through Craft's own SimpleElement
|
||||
// connector wrapper, which is not exported -- leave those untouched. Every
|
||||
// resolver component is a React component (see ResolvedRender above).
|
||||
const output: React.ReactElement = isComponent ? <ResolvedRender device={device} /> : render;
|
||||
const output: React.ReactElement = (
|
||||
<GlobalInstanceContext.Provider value={instanceRoot}>
|
||||
{isComponent ? <ResolvedRender device={device} /> : render}
|
||||
</GlobalInstanceContext.Provider>
|
||||
);
|
||||
|
||||
if (!active) return <>{output}</>;
|
||||
if (!active) return output;
|
||||
|
||||
return (
|
||||
<>
|
||||
{output}
|
||||
{createPortal(
|
||||
<div ref={badgeRef} className="component-indicator" style={{ position: 'fixed' }}>
|
||||
{isInstanceRoot && (
|
||||
<span className="component-indicator-global" data-testid="global-badge" title="Global section: edit the master to change it everywhere">
|
||||
<i className="fa fa-globe" aria-hidden /> Global
|
||||
</span>
|
||||
)}
|
||||
{isInstanceInner && (
|
||||
<i className="fa fa-lock component-indicator-lock" aria-label="Part of a global section" title="Part of a global section: text and images are per-page overrides; design comes from the master" />
|
||||
)}
|
||||
<span>{name}</span>
|
||||
{parent && (
|
||||
<button
|
||||
|
||||
Reference in new issue
Block a user