diff --git a/packages/core/src/components/entry-editor/Editor.tsx b/packages/core/src/components/entry-editor/Editor.tsx index 92c439aa..b11db2c1 100644 --- a/packages/core/src/components/entry-editor/Editor.tsx +++ b/packages/core/src/components/entry-editor/Editor.tsx @@ -21,7 +21,7 @@ import { import { loadMedia } from '@staticcms/core/actions/mediaLibrary'; import { loadScroll, toggleScroll } from '@staticcms/core/actions/scroll'; import useEntryCallback from '@staticcms/core/lib/hooks/useEntryCallback'; -import { selectFields } from '@staticcms/core/lib/util/collection.util'; +import { getFileFromSlug, selectFields } from '@staticcms/core/lib/util/collection.util'; import { useWindowEvent } from '@staticcms/core/lib/util/window.util'; import { selectConfig } from '@staticcms/core/reducers/selectors/config'; import { selectEntry } from '@staticcms/core/reducers/selectors/entries'; @@ -211,7 +211,24 @@ const Editor: FC> = ({ }; }, [collection, createBackup, entryDraft.entry, hasChanged]); + const hasLivePreview = useMemo(() => { + let livePreview = typeof collection.editor?.live_preview === 'string'; + + if ('files' in collection) { + if (entryDraft.entry) { + const file = getFileFromSlug(collection, entryDraft.entry.slug); + + if (file?.editor) { + livePreview = livePreview || typeof file.editor.live_preview === 'string'; + } + } + } + + return livePreview; + }, [collection, entryDraft.entry]); + useEntryCallback({ + hasLivePreview, collection, slug, callback: () => { diff --git a/packages/core/src/components/entry-editor/editor-preview-pane/EditorPreviewPane.tsx b/packages/core/src/components/entry-editor/editor-preview-pane/EditorPreviewPane.tsx index e6ad7ffa..a3939831 100644 --- a/packages/core/src/components/entry-editor/editor-preview-pane/EditorPreviewPane.tsx +++ b/packages/core/src/components/entry-editor/editor-preview-pane/EditorPreviewPane.tsx @@ -8,6 +8,7 @@ import { EDITOR_SIZE_COMPACT } from '@staticcms/core/constants/views'; import { getPreviewStyles, getPreviewTemplate } from '@staticcms/core/lib/registry'; import classNames from '@staticcms/core/lib/util/classNames.util'; import { selectTemplateName } from '@staticcms/core/lib/util/collection.util'; +import LivePreviewLoadedEvent from '@staticcms/core/lib/util/events/LivePreviewLoadedEvent'; import { useWindowEvent } from '@staticcms/core/lib/util/window.util'; import { selectConfig } from '@staticcms/core/reducers/selectors/config'; import { selectTheme } from '@staticcms/core/reducers/selectors/globalUI'; @@ -195,6 +196,10 @@ const EditorPreviewPane = (props: TranslatedProps) => { useWindowEvent('data:update', passEventToIframe); + const handleLivePreviewIframeLoaded = useCallback(() => { + window.dispatchEvent(new LivePreviewLoadedEvent()); + }, []); + return useMemo(() => { if (!element) { return null; @@ -222,9 +227,11 @@ const EditorPreviewPane = (props: TranslatedProps) => { {livePreviewUrlTemplate ? (