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>
78 lines
2.6 KiB
TypeScript
78 lines
2.6 KiB
TypeScript
import React, { useCallback, useEffect, useRef } from 'react';
|
|
import {
|
|
sanitizeInlineHtml,
|
|
inlineHtmlToText,
|
|
isPlainInlineHtml,
|
|
textToInlineHtml,
|
|
resolveInlineHtml,
|
|
markExternalLinks,
|
|
} from '../../utils/inline-html';
|
|
import { useGlobalOverrideCommit } from '../../state/GlobalInstanceContext';
|
|
|
|
interface InlineTextProps {
|
|
text?: string;
|
|
html?: string;
|
|
}
|
|
|
|
const SHORTCUTS: Record<string, string> = { b: 'bold', i: 'italic', u: 'underline' };
|
|
|
|
/**
|
|
* contentEditable wiring shared by Heading and TextBlock (#46).
|
|
*
|
|
* - The DOM is written imperatively (innerHTML) only while the node is NOT
|
|
* selected and has no pending edit, so the caret is never reset mid-typing.
|
|
* - An edit is committed ONCE -- on blur, or on deselect if no blur came
|
|
* first -- as a single `setProp` (= one undo step) that stores BOTH the
|
|
* sanitised `html` and its plain `text` mirror. Unmarked content stores
|
|
* `html: ''` so legacy nodes keep their legacy shape.
|
|
* - mod+b/i/u is handled locally: the global shortcut dispatcher skips
|
|
* contentEditable targets.
|
|
*/
|
|
export function useInlineEditable(
|
|
text: string,
|
|
html: string,
|
|
selected: boolean,
|
|
setProp: (cb: (p: InlineTextProps) => void) => void,
|
|
) {
|
|
const elRef = useRef<HTMLElement | null>(null);
|
|
const pendingRef = useRef(false);
|
|
const commitProps = useGlobalOverrideCommit(setProp); // #57: inside a global instance -> override
|
|
|
|
const commit = useCallback(() => {
|
|
if (!pendingRef.current) return;
|
|
pendingRef.current = false;
|
|
const el = elRef.current;
|
|
if (!el) return;
|
|
markExternalLinks(el);
|
|
const clean = sanitizeInlineHtml(el.innerHTML);
|
|
const nextText = inlineHtmlToText(clean);
|
|
const nextHtml = isPlainInlineHtml(clean) ? '' : clean;
|
|
commitProps((p) => { p.html = nextHtml; p.text = nextText; });
|
|
}, [commitProps]);
|
|
|
|
// Deselect without a blur (e.g. Escape, programmatic selection change).
|
|
useEffect(() => {
|
|
if (!selected) commit();
|
|
}, [selected, commit]);
|
|
|
|
useEffect(() => {
|
|
if (elRef.current && !selected && !pendingRef.current) {
|
|
elRef.current.innerHTML = resolveInlineHtml(text, html) ?? textToInlineHtml(text || '');
|
|
}
|
|
}, [text, html, selected]);
|
|
|
|
const onInput = useCallback(() => { pendingRef.current = true; }, []);
|
|
|
|
const onKeyDown = useCallback((e: React.KeyboardEvent) => {
|
|
if (!(e.metaKey || e.ctrlKey) || e.shiftKey || e.altKey) return;
|
|
const cmd = SHORTCUTS[e.key.toLowerCase()];
|
|
if (!cmd) return;
|
|
if (typeof document.execCommand !== 'function') return;
|
|
e.preventDefault();
|
|
document.execCommand(cmd);
|
|
pendingRef.current = true;
|
|
}, []);
|
|
|
|
return { elRef, onBlur: commit, onInput, onKeyDown };
|
|
}
|