-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add AdvancedEditors with an iframe [FC-0076] (#1568)
Creates the AdvancedEditor to support editors like Drag and Drop, openresponse, poll, survey, and other advanced editors. - AdvancedEditor created to call studio_view of the block - Update LibraryBlock to support any view (and use studio_view in AdvancedEditor) Intercept xblock-event message to close the Advanced editor on cancel or save
- Loading branch information
Showing
7 changed files
with
178 additions
and
20 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import { getConfig } from '@edx/frontend-platform'; | ||
|
||
import { | ||
render, | ||
initializeMocks, | ||
waitFor, | ||
} from '../testUtils'; | ||
import AdvancedEditor from './AdvancedEditor'; | ||
|
||
jest.mock('./containers/EditorContainer', () => ({ | ||
EditorModalWrapper: jest.fn(() => (<div>Advanced Editor Iframe</div>)), | ||
})); | ||
const onCloseMock = jest.fn(); | ||
|
||
describe('AdvancedEditor', () => { | ||
beforeEach(() => { | ||
initializeMocks(); | ||
}); | ||
|
||
it('should call onClose when receiving "cancel-clicked" message', () => { | ||
render(<AdvancedEditor usageKey="test" onClose={onCloseMock} />); | ||
|
||
const messageEvent = new MessageEvent('message', { | ||
data: { | ||
type: 'xblock-event', | ||
eventName: 'cancel', | ||
}, | ||
origin: getConfig().STUDIO_BASE_URL, | ||
}); | ||
|
||
window.dispatchEvent(messageEvent); | ||
|
||
expect(onCloseMock).toHaveBeenCalled(); | ||
}); | ||
|
||
it('should call onClose when receiving "save-clicked" message', () => { | ||
render(<AdvancedEditor usageKey="test" onClose={onCloseMock} />); | ||
|
||
const messageEvent = new MessageEvent('message', { | ||
data: { | ||
type: 'xblock-event', | ||
eventName: 'save', | ||
data: { | ||
state: 'end', | ||
}, | ||
}, | ||
origin: getConfig().STUDIO_BASE_URL, | ||
}); | ||
|
||
window.dispatchEvent(messageEvent); | ||
|
||
expect(onCloseMock).toHaveBeenCalled(); | ||
}); | ||
|
||
it('should call showToast when receiving "error" message', async () => { | ||
const { mockShowToast } = initializeMocks(); | ||
|
||
render(<AdvancedEditor usageKey="test" onClose={onCloseMock} />); | ||
|
||
const messageEvent = new MessageEvent('message', { | ||
data: { | ||
type: 'xblock-event', | ||
eventName: 'error', | ||
}, | ||
origin: getConfig().STUDIO_BASE_URL, | ||
}); | ||
|
||
window.dispatchEvent(messageEvent); | ||
|
||
await waitFor(() => { | ||
expect(mockShowToast).toHaveBeenCalled(); | ||
}); | ||
}); | ||
|
||
it('should not call onClose if the message is from an invalid origin', () => { | ||
render(<AdvancedEditor usageKey="test" onClose={onCloseMock} />); | ||
|
||
const messageEvent = new MessageEvent('message', { | ||
data: { | ||
type: 'xblock-event', | ||
eventName: 'cancel', | ||
}, | ||
origin: 'https://invalid-origin.com', | ||
}); | ||
|
||
window.dispatchEvent(messageEvent); | ||
|
||
expect(onCloseMock).not.toHaveBeenCalled(); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import React, { useEffect } from 'react'; | ||
import { getConfig } from '@edx/frontend-platform'; | ||
import { useIntl } from '@edx/frontend-platform/i18n'; | ||
|
||
import { LibraryBlock } from '../library-authoring/LibraryBlock'; | ||
import { EditorModalWrapper } from './containers/EditorContainer'; | ||
import { ToastContext } from '../generic/toast-context'; | ||
import messages from './messages'; | ||
|
||
interface AdvancedEditorProps { | ||
usageKey: string, | ||
onClose: Function | null, | ||
} | ||
|
||
const AdvancedEditor = ({ usageKey, onClose }: AdvancedEditorProps) => { | ||
const intl = useIntl(); | ||
const { showToast } = React.useContext(ToastContext); | ||
|
||
useEffect(() => { | ||
const handleIframeMessage = (event) => { | ||
if (event.origin !== getConfig().STUDIO_BASE_URL) { | ||
return; | ||
} | ||
|
||
if (event.data.type === 'xblock-event') { | ||
const { eventName, data } = event.data; | ||
|
||
if (onClose && (eventName === 'cancel' | ||
|| (eventName === 'save' && data.state === 'end')) | ||
) { | ||
onClose(); | ||
} else if (eventName === 'error') { | ||
showToast(intl.formatMessage(messages.advancedEditorGenericError)); | ||
} | ||
} | ||
}; | ||
|
||
window.addEventListener('message', handleIframeMessage); | ||
|
||
return () => { | ||
window.removeEventListener('message', handleIframeMessage); | ||
}; | ||
}, []); | ||
|
||
return ( | ||
<EditorModalWrapper onClose={onClose as () => void}> | ||
<LibraryBlock | ||
usageKey={usageKey} | ||
view="studio_view" | ||
/> | ||
</EditorModalWrapper> | ||
); | ||
}; | ||
|
||
export default AdvancedEditor; |
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