-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: listen to xblock interaction events
- Loading branch information
1 parent
bc8d59b
commit b34f1f5
Showing
14 changed files
with
606 additions
and
159 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,57 +1,185 @@ | ||
import { useRef, useEffect, FC } from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import { | ||
useRef, FC, useEffect, useState, useMemo, useCallback, | ||
} from 'react'; | ||
import { useIntl } from '@edx/frontend-platform/i18n'; | ||
import { getConfig } from '@edx/frontend-platform'; | ||
import { useToggle } from '@openedx/paragon'; | ||
import { useDispatch } from 'react-redux'; | ||
import { useNavigate } from 'react-router-dom'; | ||
|
||
import { IFRAME_FEATURE_POLICY } from '../constants'; | ||
import DeleteModal from '../../generic/delete-modal/DeleteModal'; | ||
import ConfigureModal from '../../generic/configure-modal/ConfigureModal'; | ||
import { copyToClipboard } from '../../generic/data/thunks'; | ||
import { COURSE_BLOCK_NAMES, IFRAME_FEATURE_POLICY } from '../../constants'; | ||
import { messageTypes, COMPONENT_TYPES } from '../constants'; | ||
import { fetchCourseUnitQuery } from '../data/thunk'; | ||
import { useIframe } from '../context/hooks'; | ||
import { useIFrameBehavior } from './hooks'; | ||
import messages from './messages'; | ||
|
||
/** | ||
* This offset is necessary to fully display the dropdown actions of the XBlock | ||
* in case the XBlock does not have content inside. | ||
*/ | ||
const IFRAME_BOTTOM_OFFSET = 220; | ||
import { | ||
XBlockContainerIframeProps, | ||
XBlockDataTypes, | ||
MessagePayloadTypes, | ||
} from './types'; | ||
|
||
interface XBlockContainerIframeProps { | ||
blockId: string; | ||
} | ||
|
||
const XBlockContainerIframe: FC<XBlockContainerIframeProps> = ({ blockId }) => { | ||
const XBlockContainerIframe: FC<XBlockContainerIframeProps> = ({ | ||
courseId, blockId, unitXBlockActions, courseVerticalChildren, handleConfigureSubmit, | ||
}) => { | ||
const intl = useIntl(); | ||
const iframeRef = useRef<HTMLIFrameElement>(null); | ||
const { setIframeRef } = useIframe(); | ||
const dispatch = useDispatch(); | ||
const navigate = useNavigate(); | ||
|
||
const [isDeleteModalOpen, openDeleteModal, closeDeleteModal] = useToggle(false); | ||
const [isConfigureModalOpen, openConfigureModal, closeConfigureModal] = useToggle(false); | ||
const { setIframeRef, sendMessageToIframe } = useIframe(); | ||
const [currentXBlockId, setCurrentXBlockId] = useState<string | null>(null); | ||
const [currentXBlockData, setCurrentXBlockData] = useState<any>({}); | ||
const [courseXBlockIframeOffset, setCourseXBlockIframeOffset] = useState<number>(0); | ||
|
||
const iframeUrl = useMemo(() => `${getConfig().STUDIO_BASE_URL}/container_embed/${blockId}`, [blockId]); | ||
|
||
useEffect(() => { | ||
setIframeRef(iframeRef); | ||
}, [setIframeRef]); | ||
|
||
useEffect(() => { | ||
if (currentXBlockId) { | ||
const foundXBlockInfo = courseVerticalChildren?.find(xblock => xblock.blockId === currentXBlockId); | ||
if (foundXBlockInfo) { | ||
const { name, userPartitionInfo, blockType } = foundXBlockInfo; | ||
|
||
setCurrentXBlockData({ | ||
category: COURSE_BLOCK_NAMES.component.id, | ||
displayName: name, | ||
userPartitionInfo, | ||
showCorrectness: 'always', | ||
blockType, | ||
id: currentXBlockId, | ||
}); | ||
} | ||
} | ||
}, [isConfigureModalOpen, currentXBlockId, courseVerticalChildren]); | ||
|
||
const handleRefreshIframe = useCallback(() => { | ||
// TODO: this artificial delay is a temporary solution | ||
// to ensure the iframe content is properly refreshed. | ||
setTimeout(() => { | ||
sendMessageToIframe(messageTypes.refreshXBlock, null); | ||
}, 1000); | ||
}, [sendMessageToIframe]); | ||
|
||
const handleDuplicateXBlock = useCallback( | ||
(xblockData: XBlockDataTypes) => { | ||
const duplicateAndNavigate = (blockType: string) => { | ||
unitXBlockActions.handleDuplicate(xblockData.id); | ||
if ([COMPONENT_TYPES.html, COMPONENT_TYPES.problem, COMPONENT_TYPES.video].includes(blockType)) { | ||
navigate(`/course/${courseId}/editor/${blockType}/${xblockData.id}`); | ||
} | ||
handleRefreshIframe(); | ||
}; | ||
|
||
duplicateAndNavigate(xblockData.blockType); | ||
}, | ||
[unitXBlockActions, courseId, navigate, handleRefreshIframe], | ||
); | ||
|
||
const iframeUrl = `${getConfig().STUDIO_BASE_URL}/container_embed/${blockId}`; | ||
const handleRefetchXBlocks = useCallback(() => { | ||
// TODO: this artificial delay is a temporary solution | ||
// to ensure the iframe content is properly refreshed. | ||
setTimeout(() => { | ||
dispatch(fetchCourseUnitQuery(blockId)); | ||
}, 1000); | ||
}, [dispatch, blockId]); | ||
|
||
useEffect(() => { | ||
const messageHandlers: Record<string, (payload: MessagePayloadTypes) => void> = { | ||
[messageTypes.deleteXBlock]: openDeleteModal, | ||
[messageTypes.manageXBlockAccess]: () => openConfigureModal(), | ||
[messageTypes.copyXBlock]: () => dispatch(copyToClipboard(currentXBlockId)), | ||
[messageTypes.duplicateXBlock]: () => handleDuplicateXBlock(currentXBlockData), | ||
[messageTypes.refreshPositions]: handleRefetchXBlocks, | ||
[messageTypes.newXBlockEditor]: ({ url }) => navigate(`/course/${courseId}/editor${url}`), | ||
[messageTypes.currentXBlockId]: ({ id }) => setCurrentXBlockId(id), | ||
[messageTypes.toggleCourseXBlockDropdown]: ({ | ||
courseXBlockDropdownHeight, | ||
}) => setCourseXBlockIframeOffset(courseXBlockDropdownHeight), | ||
}; | ||
|
||
const handleMessage = (event: MessageEvent) => { | ||
const { type, payload } = event.data || {}; | ||
|
||
// console.log('event.data =============>', event.data); | ||
|
||
if (type && messageHandlers[type]) { | ||
messageHandlers[type](payload); | ||
} | ||
}; | ||
|
||
window.addEventListener('message', handleMessage); | ||
|
||
return () => { | ||
window.removeEventListener('message', handleMessage); | ||
}; | ||
}, [dispatch, blockId, courseVerticalChildren, currentXBlockId, currentXBlockData]); | ||
|
||
const { iframeHeight } = useIFrameBehavior({ | ||
id: blockId, | ||
iframeUrl, | ||
}); | ||
|
||
useEffect(() => { | ||
setIframeRef(iframeRef); | ||
}, [setIframeRef]); | ||
const handleDeleteItemSubmit = () => { | ||
if (currentXBlockId) { | ||
unitXBlockActions.handleDelete(currentXBlockId); | ||
closeDeleteModal(); | ||
handleRefreshIframe(); | ||
} | ||
}; | ||
|
||
const onConfigureSubmit = (...args: any[]) => { | ||
if (currentXBlockId) { | ||
handleConfigureSubmit(currentXBlockId, ...args, closeConfigureModal); | ||
handleRefreshIframe(); | ||
} | ||
}; | ||
|
||
return ( | ||
<iframe | ||
ref={iframeRef} | ||
title={intl.formatMessage(messages.xblockIframeTitle)} | ||
src={iframeUrl} | ||
frameBorder="0" | ||
allow={IFRAME_FEATURE_POLICY} | ||
allowFullScreen | ||
loading="lazy" | ||
style={{ width: '100%', height: iframeHeight + IFRAME_BOTTOM_OFFSET }} | ||
scrolling="no" | ||
referrerPolicy="origin" | ||
/> | ||
<> | ||
<DeleteModal | ||
category="component" | ||
isOpen={isDeleteModalOpen} | ||
close={closeDeleteModal} | ||
onDeleteSubmit={handleDeleteItemSubmit} | ||
/> | ||
{currentXBlockId && ( | ||
<ConfigureModal | ||
isXBlockComponent | ||
isOpen={isConfigureModalOpen} | ||
onClose={closeConfigureModal} | ||
onConfigureSubmit={onConfigureSubmit} | ||
currentItemData={currentXBlockData} | ||
isSelfPaced={false} | ||
/> | ||
)} | ||
<iframe | ||
ref={iframeRef} | ||
title={intl.formatMessage(messages.xblockIframeTitle)} | ||
src={iframeUrl} | ||
frameBorder="0" | ||
allow={IFRAME_FEATURE_POLICY} | ||
allowFullScreen | ||
loading="lazy" | ||
style={{ | ||
width: '100%', | ||
height: iframeHeight + courseXBlockIframeOffset, | ||
}} | ||
scrolling="no" | ||
referrerPolicy="origin" | ||
aria-label={intl.formatMessage(messages.xblockIframeLabel, { xblockCount: courseVerticalChildren.length })} | ||
/> | ||
</> | ||
); | ||
}; | ||
|
||
XBlockContainerIframe.propTypes = { | ||
blockId: PropTypes.string.isRequired, | ||
}; | ||
|
||
export default XBlockContainerIframe; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.