2026-05-23 14:13:42 -07:00
|
|
|
import React, { CSSProperties, useMemo } from 'react';
|
2026-04-05 18:31:16 -07:00
|
|
|
import { useNode, UserComponent } from '@craftjs/core';
|
2026-05-23 14:13:42 -07:00
|
|
|
import DOMPurify from 'dompurify';
|
2026-04-05 18:31:16 -07:00
|
|
|
|
|
|
|
|
interface HtmlBlockProps {
|
|
|
|
|
code: string;
|
|
|
|
|
style?: CSSProperties;
|
2026-05-23 14:13:42 -07:00
|
|
|
aiName?: string;
|
|
|
|
|
node_id?: string;
|
2026-04-05 18:31:16 -07:00
|
|
|
}
|
|
|
|
|
|
2026-05-23 14:13:42 -07:00
|
|
|
const PURIFY_CONFIG = {
|
|
|
|
|
ALLOWED_TAGS: [
|
|
|
|
|
'a','p','br','hr','div','span','section','article',
|
|
|
|
|
'header','footer','main','aside','nav',
|
|
|
|
|
'ul','ol','li',
|
|
|
|
|
'h1','h2','h3','h4','h5','h6',
|
|
|
|
|
'em','strong','b','i','u','s',
|
|
|
|
|
'blockquote','code','pre',
|
|
|
|
|
'img','figure','figcaption',
|
|
|
|
|
'iframe',
|
|
|
|
|
],
|
|
|
|
|
ALLOWED_ATTR: [
|
|
|
|
|
'href','src','alt','title','target','rel',
|
|
|
|
|
'width','height','class',
|
|
|
|
|
'allowfullscreen','allow','frameborder',
|
|
|
|
|
],
|
|
|
|
|
ALLOWED_URI_REGEXP: /^(?:(?:https?|mailto|tel|data:image\/[a-z]+;base64,):|[^a-z]|[a-z+.-]+(?:[^a-z+.\-:]|$))/i,
|
|
|
|
|
FORBID_TAGS: ['script','style','object','embed','link','meta','form','input','button','select','textarea'],
|
|
|
|
|
FORBID_ATTR: [/^on/i],
|
|
|
|
|
};
|
2026-04-05 18:31:16 -07:00
|
|
|
|
2026-05-23 14:13:42 -07:00
|
|
|
export function purifyHtml(input: string): string {
|
|
|
|
|
return DOMPurify.sanitize(input || '', PURIFY_CONFIG as any) as unknown as string;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const HtmlBlock: UserComponent<HtmlBlockProps> = ({ code = '', style = {} }) => {
|
|
|
|
|
const { connectors: { connect, drag }, selected } = useNode((node) => ({ selected: node.events.selected }));
|
|
|
|
|
const clean = useMemo(() => purifyHtml(code), [code]);
|
|
|
|
|
const setRef = (ref: HTMLElement | null): void => { if (ref) connect(drag(ref)); };
|
|
|
|
|
return React.createElement('div', {
|
|
|
|
|
ref: setRef,
|
|
|
|
|
style: {
|
|
|
|
|
minHeight: '40px',
|
|
|
|
|
outline: selected ? '2px solid #3b82f6' : 'none',
|
|
|
|
|
...style,
|
|
|
|
|
},
|
|
|
|
|
dangerouslySetInnerHTML: { __html: clean },
|
|
|
|
|
});
|
2026-04-05 18:31:16 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/* ---------- Craft config ---------- */
|
|
|
|
|
|
|
|
|
|
HtmlBlock.craft = {
|
|
|
|
|
displayName: 'HTML',
|
|
|
|
|
props: {
|
|
|
|
|
code: '',
|
|
|
|
|
style: {},
|
|
|
|
|
},
|
|
|
|
|
rules: {
|
|
|
|
|
canDrag: () => true,
|
|
|
|
|
canMoveIn: () => false,
|
|
|
|
|
canMoveOut: () => true,
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/* ---------- HTML export ---------- */
|
|
|
|
|
|
|
|
|
|
(HtmlBlock as any).toHtml = (props: HtmlBlockProps, _childrenHtml: string) => {
|
2026-07-12 12:06:07 -07:00
|
|
|
// Run through the same DOMPurify config used for the live editor preview
|
|
|
|
|
// so exported pages can't carry <script>/on*= payloads either.
|
|
|
|
|
return { html: purifyHtml(props.code || '') };
|
2026-04-05 18:31:16 -07:00
|
|
|
};
|