- TextStylePanel (Heading/TextBlock): line-height/letter-spacing presets,
text-transform (none/uppercase/lowercase/capitalize), italic + underline
toggles (fontStyle/textDecoration), and a custom NumericUnitInput
font-size alongside the existing preset row. All write to component
`style`; no toHtml changes needed (style already flows through
cssPropsToString for both components).
- ButtonStylePanel/ButtonLink: "Open in new tab" checkbox writes the
existing `target` prop ('_self'/'_blank' -- toHtml already emitted
rel="noopener noreferrer" for _blank). New Hover State section
(hoverBg/hoverColor via ColorPickerField) renders a scoped
`<style>.btn_<hash>:hover{...}</style>` block before the `<a>` in
toHtml, scoped per-node via scopeId (same pattern as Navbar/Menu) so
two buttons on one page don't collide; both values sanitized through
cssValue against <style>-element breakout. Editor canvas gets a live
hover preview via onMouseEnter/onMouseLeave local state (mirrors Menu's
approach), since there's no way to preview a CSS :hover rule directly
on an inline-styled React element.
- Heading/TextBlock/ButtonLink: added margin(per-side)/padding(per-side,
Text only)/border/box-shadow/opacity style defaults + AnimationControl/
VisibilityControl-backed animation/animationDelay/hideOnDesktop/
hideOnTablet/hideOnMobile props, each panel gaining collapsible
Spacing / Border & Effects / Animation & Visibility sections. Button
keeps its existing padding-shorthand preset row rather than adding a
redundant per-side padding control; only margin got the new
per-side SpacingControl.
- Tests: extended all three *.toHtml.test.ts files -- typography style
emission (line-height/letter-spacing/text-transform/font-style/
text-decoration/custom font-size), button target+rel, scoped hover
style emission + two adversarial style-breakout cases (</style><script>
and rule-injection via `;}selector{`), and craft.props assertions for
every new prop on all three components. 674 tests green, tsc + vite
build clean.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
133 lines
4.1 KiB
TypeScript
133 lines
4.1 KiB
TypeScript
import React, { CSSProperties, useCallback, useRef, useEffect } from 'react';
|
|
import { useNode, UserComponent } from '@craftjs/core';
|
|
import { cssPropsToString } from '../../utils/style-helpers';
|
|
import { escapeHtml } from '../../utils/escape';
|
|
|
|
type HeadingLevel = 'h1' | 'h2' | 'h3' | 'h4' | 'h5' | 'h6';
|
|
|
|
// `level` is settable via the AI `update_props` path and from deserialized
|
|
// saved state -- neither type-checked at runtime -- and is interpolated
|
|
// directly into the tag position (`React.createElement(level, ...)` /
|
|
// `<${tag}` in `toHtml`). A malicious value like `h2><img src=x
|
|
// onerror=alert(1)` (or a non-h1-6 string) must never reach that position
|
|
// unchecked. Anything not in this allowlist clamps to `'h2'`.
|
|
const ALLOWED_HEADING_LEVELS = ['h1', 'h2', 'h3', 'h4', 'h5', 'h6'] as const;
|
|
const sanitizeHeadingLevel = (level: unknown): HeadingLevel =>
|
|
(ALLOWED_HEADING_LEVELS as readonly unknown[]).includes(level) ? (level as HeadingLevel) : 'h2';
|
|
|
|
interface HeadingProps {
|
|
text?: string;
|
|
level?: HeadingLevel;
|
|
style?: CSSProperties;
|
|
cssId?: string;
|
|
cssClass?: string;
|
|
hideOnDesktop?: boolean;
|
|
hideOnTablet?: boolean;
|
|
hideOnMobile?: boolean;
|
|
animation?: string;
|
|
animationDelay?: string;
|
|
}
|
|
|
|
export const Heading: UserComponent<HeadingProps> = ({
|
|
text = 'Heading',
|
|
level = 'h2',
|
|
style = {},
|
|
}) => {
|
|
const {
|
|
connectors: { connect, drag },
|
|
selected,
|
|
actions: { setProp },
|
|
} = useNode((node) => ({
|
|
selected: node.events.selected,
|
|
}));
|
|
|
|
const safeLevel = sanitizeHeadingLevel(level);
|
|
const elRef = useRef<HTMLElement | null>(null);
|
|
const editedTextRef = useRef<string | null>(null);
|
|
|
|
const commitText = useCallback(() => {
|
|
if (elRef.current) {
|
|
const newText = elRef.current.innerText;
|
|
editedTextRef.current = newText;
|
|
setProp((p: HeadingProps) => { p.text = newText; });
|
|
}
|
|
}, [setProp]);
|
|
|
|
// Commit on blur
|
|
const handleBlur = useCallback(() => { commitText(); }, [commitText]);
|
|
|
|
// Also commit on deselect via effect
|
|
useEffect(() => {
|
|
if (!selected && editedTextRef.current !== null) {
|
|
setProp((p: HeadingProps) => { p.text = editedTextRef.current!; });
|
|
editedTextRef.current = null;
|
|
}
|
|
}, [selected, setProp]);
|
|
|
|
// Set DOM text on mount and when text prop changes externally (not during editing)
|
|
useEffect(() => {
|
|
if (elRef.current && !selected && editedTextRef.current === null) {
|
|
elRef.current.innerText = text || '';
|
|
}
|
|
}, [text, selected]);
|
|
|
|
return React.createElement(safeLevel, {
|
|
ref: (ref: HTMLElement | null): void => {
|
|
elRef.current = ref;
|
|
if (ref) connect(drag(ref));
|
|
},
|
|
contentEditable: selected,
|
|
suppressContentEditableWarning: true,
|
|
onBlur: handleBlur,
|
|
onInput: () => {
|
|
// Track that we have unsaved edits
|
|
if (elRef.current) {
|
|
editedTextRef.current = elRef.current.innerText;
|
|
}
|
|
},
|
|
style: { outline: 'none', cursor: selected ? 'text' : 'pointer', minHeight: '1em', ...style },
|
|
});
|
|
};
|
|
|
|
Heading.craft = {
|
|
displayName: 'Heading',
|
|
props: {
|
|
text: 'Your Heading',
|
|
level: 'h2' as HeadingLevel,
|
|
style: {
|
|
fontSize: '36px',
|
|
fontWeight: '700',
|
|
fontFamily: 'Inter, sans-serif',
|
|
color: '#1f2937',
|
|
marginBottom: '16px',
|
|
lineHeight: '',
|
|
letterSpacing: '',
|
|
textTransform: '' as CSSProperties['textTransform'],
|
|
fontStyle: '' as CSSProperties['fontStyle'],
|
|
textDecoration: '',
|
|
marginTop: '', marginRight: '', marginLeft: '',
|
|
paddingTop: '', paddingRight: '', paddingBottom: '', paddingLeft: '',
|
|
border: 'none',
|
|
boxShadow: 'none',
|
|
opacity: '1',
|
|
},
|
|
animation: '',
|
|
animationDelay: '0',
|
|
hideOnDesktop: false,
|
|
hideOnTablet: false,
|
|
hideOnMobile: false,
|
|
},
|
|
rules: {
|
|
canDrag: () => true,
|
|
canMoveIn: () => false,
|
|
canMoveOut: () => true,
|
|
},
|
|
};
|
|
|
|
(Heading as any).toHtml = (props: HeadingProps, _childrenHtml: string) => {
|
|
const tag = sanitizeHeadingLevel(props.level);
|
|
const safeText = escapeHtml(props.text || '');
|
|
const styleStr = cssPropsToString(props.style);
|
|
return { html: `<${tag}${styleStr ? ` style="${styleStr}"` : ''}>${safeText}</${tag}>` };
|
|
};
|