2026-10-07 11:41:56 -07:00
|
|
|
import React, { CSSProperties } from 'react';
|
2026-04-05 18:31:16 -07:00
|
|
|
import { useNode, UserComponent } from '@craftjs/core';
|
|
|
|
|
import { cssPropsToString } from '../../utils/style-helpers';
|
2026-07-12 18:03:44 -07:00
|
|
|
import { escapeHtml } from '../../utils/escape';
|
2026-10-07 11:41:56 -07:00
|
|
|
import { resolveInlineHtml } from '../../utils/inline-html';
|
|
|
|
|
import { useInlineEditable } from './useInlineEditable';
|
2026-04-05 18:31:16 -07:00
|
|
|
|
|
|
|
|
interface TextBlockProps {
|
|
|
|
|
text?: string;
|
2026-10-07 11:41:56 -07:00
|
|
|
/** Sanitised inline rich text (#46); '' = plain `text` only. */
|
|
|
|
|
html?: string;
|
2026-04-05 18:31:16 -07:00
|
|
|
style?: CSSProperties;
|
|
|
|
|
cssId?: string;
|
|
|
|
|
cssClass?: string;
|
|
|
|
|
hideOnDesktop?: boolean;
|
|
|
|
|
hideOnTablet?: boolean;
|
|
|
|
|
hideOnMobile?: boolean;
|
|
|
|
|
animation?: string;
|
|
|
|
|
animationDelay?: string;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const TextBlock: UserComponent<TextBlockProps> = ({
|
|
|
|
|
text = 'Start typing here...',
|
2026-10-07 11:41:56 -07:00
|
|
|
html = '',
|
2026-04-05 18:31:16 -07:00
|
|
|
style = {},
|
|
|
|
|
}) => {
|
|
|
|
|
const {
|
|
|
|
|
connectors: { connect, drag },
|
|
|
|
|
selected,
|
|
|
|
|
actions: { setProp },
|
|
|
|
|
} = useNode((node) => ({
|
|
|
|
|
selected: node.events.selected,
|
|
|
|
|
}));
|
|
|
|
|
|
2026-10-07 11:41:56 -07:00
|
|
|
const { elRef, onBlur, onInput, onKeyDown } = useInlineEditable(text, html, selected, setProp);
|
2026-04-05 18:31:16 -07:00
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<p
|
|
|
|
|
ref={(ref: HTMLParagraphElement | null) => {
|
|
|
|
|
elRef.current = ref;
|
|
|
|
|
if (ref) connect(drag(ref));
|
|
|
|
|
}}
|
|
|
|
|
contentEditable={selected}
|
|
|
|
|
suppressContentEditableWarning
|
2026-10-07 11:41:56 -07:00
|
|
|
onBlur={onBlur}
|
|
|
|
|
onInput={onInput}
|
|
|
|
|
onKeyDown={onKeyDown}
|
2026-04-05 18:31:16 -07:00
|
|
|
style={{
|
|
|
|
|
outline: 'none',
|
|
|
|
|
cursor: selected ? 'text' : 'pointer',
|
|
|
|
|
minHeight: '1em',
|
|
|
|
|
...style,
|
|
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/* ---------- Craft config ---------- */
|
|
|
|
|
|
|
|
|
|
TextBlock.craft = {
|
|
|
|
|
displayName: 'Text',
|
|
|
|
|
props: {
|
|
|
|
|
text: 'Start typing here...',
|
2026-10-07 11:41:56 -07:00
|
|
|
html: '',
|
2026-04-05 18:31:16 -07:00
|
|
|
style: {
|
|
|
|
|
fontSize: '16px',
|
|
|
|
|
lineHeight: '1.6',
|
|
|
|
|
color: '#3f3f46',
|
2026-07-14 06:41:53 -07:00
|
|
|
letterSpacing: '',
|
|
|
|
|
textTransform: '' as CSSProperties['textTransform'],
|
|
|
|
|
fontStyle: '' as CSSProperties['fontStyle'],
|
|
|
|
|
textDecoration: '',
|
|
|
|
|
marginTop: '', marginRight: '', marginBottom: '', marginLeft: '',
|
|
|
|
|
paddingTop: '', paddingRight: '', paddingBottom: '', paddingLeft: '',
|
|
|
|
|
border: 'none',
|
|
|
|
|
boxShadow: 'none',
|
|
|
|
|
opacity: '1',
|
2026-04-05 18:31:16 -07:00
|
|
|
},
|
2026-07-14 06:41:53 -07:00
|
|
|
animation: '',
|
|
|
|
|
animationDelay: '0',
|
|
|
|
|
hideOnDesktop: false,
|
|
|
|
|
hideOnTablet: false,
|
|
|
|
|
hideOnMobile: false,
|
2026-04-05 18:31:16 -07:00
|
|
|
},
|
|
|
|
|
rules: {
|
|
|
|
|
canDrag: () => true,
|
|
|
|
|
canMoveIn: () => false,
|
|
|
|
|
canMoveOut: () => true,
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/* ---------- HTML export ---------- */
|
|
|
|
|
|
|
|
|
|
(TextBlock as any).toHtml = (props: TextBlockProps, _childrenHtml: string) => {
|
|
|
|
|
const styleStr = cssPropsToString(props.style);
|
2026-10-07 11:41:56 -07:00
|
|
|
const escapedText = resolveInlineHtml(props.text, props.html) ?? escapeHtml(props.text || '');
|
2026-04-05 18:31:16 -07:00
|
|
|
return { html: `<p${styleStr ? ` style="${styleStr}"` : ''}>${escapedText}</p>` };
|
|
|
|
|
};
|