{
+ form?.setFieldValue?.(subjectProperty, value)
+ setObjectData?.((previous) => ({
+ ...previous,
+ [subjectProperty]: value
+ }))
+ }}
+ />
+ ) : null}
+
{
form?.setFieldValue?.('content', value)
setObjectData?.((prev) => ({
@@ -274,7 +301,7 @@ const TemplateEditor = ({
}))
}}
isEditing={isEditing}
- autoCompleteObject={objectData?.testObject}
+ autoCompleteObject={currentTemplate?.testObject}
errorLines={errorLines}
/>
@@ -318,13 +345,13 @@ const TemplateEditor = ({
type={'codeBlock'}
name='testObject'
language='json'
- objectData={objectData}
+ objectData={currentTemplate}
isEditing={true}
/>
)}
{testObjectViewMode == 'Tree' && (
{}
const TemplatePreview = ({
objectData,
documentTemplate,
+ content = null,
+ template,
+ templateType = 'documentTemplate',
+ capabilities = {},
loading,
isEditing,
onTestObjectOpen = noop,
@@ -31,6 +35,10 @@ const TemplatePreview = ({
showTestObject = false,
showPreviewSwitch = true
}) => {
+ const currentTemplate = template || documentTemplate
+ const htmlOnly = capabilities.htmlOnly === true
+ const showWidthControl = capabilities.widthControl === true
+ const defaultPreviewWidth = capabilities.defaultWidth || 600
const iframeRef = useRef(null)
const scrollElementRef = useRef(null)
const savedScrollRef = useRef({ top: 0, left: 0 })
@@ -48,6 +56,7 @@ const TemplatePreview = ({
const [downloadProgress, setDownloadProgress] = useState(null)
const [previewScale, setPreviewScale] = useState(1)
const [previewType, setPreviewType] = useState('HTML')
+ const [previewWidth, setPreviewWidth] = useState(defaultPreviewWidth)
const [panMode, setPanMode] = useState(false)
const [isPanning, setIsPanning] = useState(false)
const panStartRef = useRef({ x: 0, y: 0, scrollLeft: 0, scrollTop: 0 })
@@ -121,8 +130,12 @@ const TemplatePreview = ({
parseFloat(containerStyle.paddingBottom)
}
- const contentWidth = Math.ceil(visual.width + padX)
- const contentHeight = Math.ceil(visual.height + padY)
+ const contentWidth = showWidthControl
+ ? previewWidth
+ : Math.ceil(visual.width + padX)
+ const contentHeight = Math.ceil(
+ Math.max(visual.height + padY, doc.documentElement.scrollHeight)
+ )
if (!doc.head?.querySelector('style[data-preview-outer-scroll]')) {
const styleEl = doc.createElement('style')
@@ -140,7 +153,7 @@ const TemplatePreview = ({
}
return { width, height }
})
- }, [])
+ }, [previewWidth, showWidthControl])
const restoreScrollPosition = useCallback(() => {
const scrollEl = scrollElementRef.current
@@ -200,8 +213,11 @@ const TemplatePreview = ({
}
setPreviewContentHTML('')
- setIframeSize({ width: '100%', height: 'auto' })
- }, [])
+ setIframeSize({
+ width: showWidthControl ? `${previewWidth}px` : '100%',
+ height: 'auto'
+ })
+ }, [previewWidth, showWidthControl])
const stopRenderProgressListener = useCallback(() => {
if (typeof renderUnsubscribeRef.current === 'function') {
@@ -234,7 +250,7 @@ const TemplatePreview = ({
const reloadPreviewPDF = useCallback(
(content, testObject = {}) => {
- const templateId = documentTemplate?._id
+ const templateId = currentTemplate?._id
if (!templateId) {
return
}
@@ -346,7 +362,7 @@ const TemplatePreview = ({
},
[
applyPdfResult,
- documentTemplate?._id,
+ currentTemplate?._id,
fetchTemplateDownload,
stopRenderProgressListener,
subscribeToObjectEvent
@@ -355,39 +371,61 @@ const TemplatePreview = ({
const reloadPreview = useCallback(
(content, testObject = {}, scale = 1) => {
- const templateId = documentTemplate?._id
+ const templateId = currentTemplate?._id
if (!templateId) {
return
}
const requestId = ++previewRequestIdRef.current
setReloadLoading(true)
- fetchTemplatePreview(templateId, content, testObject, scale, (result) => {
- if (requestId !== previewRequestIdRef.current) {
- return
- }
- setReloadLoading(false)
- if (result?.error) {
- onPreviewMessage(result.error, true)
- } else {
- updatePreviewContentHTML(result.html)
- onPreviewMessage('No issues found.', false)
- }
- })
+ fetchTemplatePreview(
+ templateId,
+ content,
+ testObject,
+ scale,
+ (result) => {
+ if (requestId !== previewRequestIdRef.current) {
+ return
+ }
+ setReloadLoading(false)
+ if (result?.error) {
+ onPreviewMessage(result.error, true)
+ } else {
+ updatePreviewContentHTML(result.html)
+ onPreviewMessage('No issues found.', false)
+ }
+ },
+ templateType,
+ showWidthControl ? { width: previewWidth } : {}
+ )
},
- [fetchTemplatePreview, onPreviewMessage, documentTemplate?._id]
+ [
+ currentTemplate?._id,
+ fetchTemplatePreview,
+ onPreviewMessage,
+ previewWidth,
+ showWidthControl,
+ templateType
+ ]
)
- const templateContent = documentTemplate?.content
- const templateId = documentTemplate?._id
+ const templateContent = currentTemplate?.content
+ const templateId = currentTemplate?._id
useEffect(() => {
+ if (content != null) {
+ updatePreviewContentHTML(content)
+ return
+ }
+
if (!templateId || !templateContent || previewType != 'HTML') {
return
}
const timeoutId = window.setTimeout(() => {
- reloadPreview(templateContent, objectData, previewScale)
+ if (content == null) {
+ reloadPreview(templateContent, objectData, previewScale)
+ }
}, 300)
return () => {
@@ -398,8 +436,10 @@ const TemplatePreview = ({
templateContent,
templateId,
previewScale,
+ previewWidth,
previewType,
- reloadPreview
+ reloadPreview,
+ content
])
useEffect(() => {
@@ -534,7 +574,20 @@ const TemplatePreview = ({
}}
disabled={loading || reloadLoading}
/>
- {showPreviewSwitch == true ? (
+ {showWidthControl ? (
+ setPreviewWidth(value || defaultPreviewWidth)}
+ style={{ width: 130 }}
+ aria-label='Email preview width'
+ />
+ ) : null}
+ {showPreviewSwitch == true && !htmlOnly ? (