Skip to content

Commit f4c0e66

Browse files
committed
Refactor 'media upload' integration in campaign visual editor.
1 parent 3aba2b0 commit f4c0e66

File tree

4 files changed

+32
-33
lines changed

4 files changed

+32
-33
lines changed

frontend/email-builder/src/App/InspectorDrawer/ConfigurationPanel/input-panels/ImageSidebarPanel.tsx

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React, { useState } from 'react';
2-
2+
import CloudUploadIcon from '@mui/icons-material/CloudUpload';
33
import {
44
VerticalAlignBottomOutlined,
55
VerticalAlignCenterOutlined,
@@ -42,6 +42,13 @@ export default function ImageSidebarPanel({ data, setData }: ImageSidebarPanelPr
4242
updateData({ ...data, props: { ...data.props, url } });
4343
}}
4444
/>
45+
<a href="#" class="select-media"
46+
style={{ display: 'inline-flex', alignItems: 'center', gap: '0.5rem', marginTop: '5px' }}
47+
onClick={(e) => {
48+
// @ts-ignore
49+
window.parent.postMessage('visualeditor.select-media', '*');
50+
e.preventDefault();
51+
}}><CloudUploadIcon style={{fontSize: '1rem'}} /> Select media</a>
4552

4653
<TextInput
4754
label="Alt text"

frontend/email-builder/src/App/InspectorDrawer/index.tsx

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import React from 'react';
22

3-
import { Box, Drawer, Tab, Tabs } from '@mui/material';
3+
import {
4+
Box, Drawer, Tab, Tabs,
5+
} from '@mui/material';
46

57
import { setSidebarTab, useInspectorDrawerOpen, useSelectedSidebarTab } from '../../documents/editor/EditorContext';
68

@@ -26,6 +28,7 @@ export default function InspectorDrawer() {
2628
<Drawer
2729
variant="persistent"
2830
anchor="right"
31+
className="sidebar"
2932
open={inspectorDrawerOpen}
3033
sx={{
3134
width: inspectorDrawerOpen ? INSPECTOR_DRAWER_WIDTH : 0,
@@ -34,10 +37,13 @@ export default function InspectorDrawer() {
3437
PaperProps={{ style: { position: 'absolute', zIndex: 0 } }}
3538
ModalProps={{
3639
container: document.querySelector('.email-builder-container'),
37-
style: { position: 'absolute', zIndex: 0 }
40+
style: { position: 'absolute', zIndex: 0 },
3841
}}
3942
>
40-
<Box sx={{ width: INSPECTOR_DRAWER_WIDTH, height: 49, borderBottom: 1, borderColor: 'divider' }}>
43+
<Box sx={{
44+
width: INSPECTOR_DRAWER_WIDTH, height: 49, borderBottom: 1, borderColor: 'divider',
45+
}}
46+
>
4147
<Box px={2}>
4248
<Tabs value={selectedSidebarTab} onChange={(_, v) => setSidebarTab(v)}>
4349
<Tab value="styles" label="Styles" />

frontend/email-builder/src/documents/blocks/helpers/EditorChildrenIds/AddBlockMenu/buttons.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export const BUTTONS: TButtonProps[] = [
2727
block: () => ({
2828
type: 'Heading',
2929
data: {
30-
props: { text: 'Hello friend' },
30+
props: { text: 'Heading' },
3131
style: {
3232
padding: { top: 16, bottom: 16, left: 24, right: 24 },
3333
},

frontend/src/components/VisualEditor.vue

Lines changed: 14 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ export default {
7070
});
7171
},
7272
73+
// Inject media URL into the image URL input field in the visual edior sidebar.
7374
onMediaSelect(media) {
7475
const iframe = this.$refs.visualEditor;
7576
const input = iframe.contentDocument.querySelector('.image-url input');
@@ -87,34 +88,14 @@ export default {
8788
8889
// Observe DOM changes in the iframe to inject media selector
8990
// into the image URL input fields.
90-
setupInjectMediaObserver(iframe) {
91-
const observer = new MutationObserver((mutations) => {
92-
mutations.forEach((mutation) => {
93-
mutation.addedNodes.forEach((node) => {
94-
node.querySelectorAll('.image-url').forEach((img) => {
95-
// Create anchor tag
96-
const anchor = document.createElement('a');
97-
anchor.href = '#';
98-
anchor.className = 'open-media-selector';
99-
anchor.textContent = 'Select Image';
100-
anchor.style.marginTop = '5px';
101-
anchor.addEventListener('click', (e) => {
102-
e.preventDefault();
103-
this.isMediaVisible = true;
104-
});
105-
if (img.parentNode) {
106-
img.parentNode.insertBefore(anchor, img.nextSibling);
107-
}
108-
});
109-
});
110-
});
111-
});
91+
onSidebarMount(msg) {
92+
if (!msg.data) {
93+
return;
94+
}
11295
113-
// Start observing.
114-
observer.observe(iframe.contentDocument.querySelector('#visual-editor-container'), {
115-
childList: true,
116-
subtree: true,
117-
});
96+
if (msg.data === 'visualeditor.select-media') {
97+
this.isMediaVisible = true;
98+
}
11899
},
119100
},
120101
@@ -142,12 +123,17 @@ export default {
142123
iframe.onload = () => {
143124
this.loadScript().then(() => {
144125
this.initEditor();
145-
this.setupInjectMediaObserver(iframe);
146126
}).catch((error) => {
147127
/* eslint-disable-next-line no-console */
148128
console.error('Failed to load email-builer script:', error);
149129
});
150130
};
131+
132+
window.addEventListener('message', this.onSidebarMount, false);
133+
},
134+
135+
unmounted() {
136+
window.removeEventListener('message', this.onSidebarMount, false);
151137
},
152138
153139
watch: {

0 commit comments

Comments
 (0)