diff --git a/packages/editor-sample/src/App/InspectorDrawer/ConfigurationPanel/input-panels/ImageSidebarPanel.tsx b/packages/editor-sample/src/App/InspectorDrawer/ConfigurationPanel/input-panels/ImageSidebarPanel.tsx index 97f0822..c63e495 100644 --- a/packages/editor-sample/src/App/InspectorDrawer/ConfigurationPanel/input-panels/ImageSidebarPanel.tsx +++ b/packages/editor-sample/src/App/InspectorDrawer/ConfigurationPanel/input-panels/ImageSidebarPanel.tsx @@ -5,7 +5,7 @@ import { VerticalAlignCenterOutlined, VerticalAlignTopOutlined, } from '@mui/icons-material'; -import { Stack, ToggleButton } from '@mui/material'; +import { Box, Button, Stack, ToggleButton } from '@mui/material'; import { ImageProps, ImagePropsSchema } from '@usewaypoint/block-image'; import BaseSidebarPanel from './helpers/BaseSidebarPanel'; @@ -13,6 +13,7 @@ import RadioGroupInput from './helpers/inputs/RadioGroupInput'; import TextDimensionInput from './helpers/inputs/TextDimensionInput'; import TextInput from './helpers/inputs/TextInput'; import MultiStylePropertyPanel from './helpers/style-inputs/MultiStylePropertyPanel'; +import { addImage, useImages } from '../../../../documents/editor/EditorContext'; type ImageSidebarPanelProps = { data: ImageProps; @@ -20,6 +21,7 @@ type ImageSidebarPanelProps = { }; export default function ImageSidebarPanel({ data, setData }: ImageSidebarPanelProps) { const [, setErrors] = useState(null); + const images = useImages(); const updateData = (d: unknown) => { const res = ImagePropsSchema.safeParse(d); @@ -31,8 +33,39 @@ export default function ImageSidebarPanel({ data, setData }: ImageSidebarPanelPr } }; + const handleFileUpload = (event: React.ChangeEvent) => { + const file = event.target.files?.[0]; + if (!file) { + return; + } + + const reader = new FileReader(); + reader.onload = () => { + addImage({ base64: reader.result as string, file }); + }; + reader.readAsDataURL(file); + }; + + const handleImageClick = (image: { base64: string; file: File }) => () => { + updateData({ ...data, props: { ...data.props, url: image.file.name } }); + }; + return ( + + + + {images.map((image) => ( + + Uploaded + {image.file.name} + + ))} + + { + const images = useImages(); + const iframeRef = useRef(null); + + const content = useMemo(() => { + let copy = html; + images.forEach((image) => { + copy = copy.replaceAll(image.file.name, image.base64); + }) + return copy + }, [images]); + + useEffect(() => { + if (iframeRef.current && content) { + const newChild = new DOMParser().parseFromString(content, 'text/html'); + iframeRef.current?.contentDocument?.replaceChild( + newChild.documentElement, + iframeRef.current.contentDocument.documentElement + ); + } + }, [iframeRef.current, content]); + + return ( +