-
Notifications
You must be signed in to change notification settings - Fork 80
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add component preview to library authoring [FC-0062] #1242
Merged
bradenmacdonald
merged 16 commits into
openedx:master
from
open-craft:rpenido/fal-3801-component-sidebar-preview-tab
Sep 14, 2024
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
2a03de9
feat: add component preview to library authoring
rpenido a9023ba
fix: change view shape
rpenido 70cd2ef
fix: types and cleanup
rpenido 1c509b6
fix: eslint
rpenido ab56de4
feat: add expand button and modal
rpenido 68de1a1
fix: minor style adjust
rpenido 31c6ccb
fix: adjust style and remove hardcoded url
rpenido ffa566f
fix: add istanbul ignore in the code that depends on the frame
rpenido 30f60cf
fix: eslint
rpenido 990815c
feat: simpler xblock embedding
bradenmacdonald 395e6f6
fix: design issues and removing overlay from modal overview
rpenido 0e9b907
refactor: code cleanup
rpenido a70323d
Merge branch 'master' into rpenido/fal-3801-component-sidebar-preview…
rpenido 0ed8e02
fix: remove unused code and add localization
rpenido a5bd650
docs: add comments from review
rpenido d2edd55
fix: remove footer close button
rpenido File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
|
@@ -9,3 +9,7 @@ | |
.mw-300px { | ||
max-width: 300px; | ||
} | ||
|
||
.right-0 { | ||
right: 0; | ||
} |
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,93 @@ | ||
import { useEffect, useRef, useState } from 'react'; | ||
import { useIntl } from '@edx/frontend-platform/i18n'; | ||
import { getConfig } from '@edx/frontend-platform'; | ||
|
||
import messages from './messages'; | ||
|
||
interface LibraryBlockProps { | ||
onBlockNotification?: (event: { eventType: string; [key: string]: any }) => void; | ||
usageKey: string; | ||
} | ||
/** | ||
* React component that displays an XBlock in a sandboxed IFrame. | ||
* | ||
* The IFrame is resized responsively so that it fits the content height. | ||
* | ||
* We use an IFrame so that the XBlock code, including user-authored HTML, | ||
* cannot access things like the user's cookies, nor can it make GET/POST | ||
* requests as the user. However, it is allowed to call any XBlock handlers. | ||
*/ | ||
const LibraryBlock = ({ onBlockNotification, usageKey }: LibraryBlockProps) => { | ||
const iframeRef = useRef<HTMLIFrameElement>(null); | ||
const [iFrameHeight, setIFrameHeight] = useState(600); | ||
const lmsBaseUrl = getConfig().LMS_BASE_URL; | ||
|
||
const intl = useIntl(); | ||
|
||
/** | ||
* Handle any messages we receive from the XBlock Runtime code in the IFrame. | ||
* See wrap.ts to see the code that sends these messages. | ||
*/ | ||
/* istanbul ignore next */ | ||
const receivedWindowMessage = async (event) => { | ||
if (!iframeRef.current || event.source !== iframeRef.current.contentWindow) { | ||
return; // This is some other random message. | ||
} | ||
|
||
const { method, replyKey, ...args } = event.data; | ||
|
||
if (method === 'update_frame_height') { | ||
setIFrameHeight(args.height); | ||
} else if (method?.indexOf('xblock:') === 0) { | ||
// This is a notification from the XBlock's frontend via 'runtime.notify(event, args)' | ||
if (onBlockNotification) { | ||
onBlockNotification({ | ||
eventType: method.substr(7), // Remove the 'xblock:' prefix that we added in wrap.ts | ||
...args, | ||
}); | ||
} | ||
} | ||
}; | ||
|
||
/** | ||
* Prepare to receive messages from the IFrame. | ||
*/ | ||
useEffect(() => { | ||
// Messages are the only way that the code in the IFrame can communicate | ||
// with the surrounding UI. | ||
window.addEventListener('message', receivedWindowMessage); | ||
|
||
return () => { | ||
window.removeEventListener('message', receivedWindowMessage); | ||
}; | ||
}, []); | ||
|
||
return ( | ||
<div style={{ | ||
height: `${iFrameHeight}px`, | ||
boxSizing: 'content-box', | ||
position: 'relative', | ||
overflow: 'hidden', | ||
minHeight: '200px', | ||
}} | ||
> | ||
<iframe | ||
ref={iframeRef} | ||
title={intl.formatMessage(messages.iframeTitle)} | ||
src={`${lmsBaseUrl}/xblocks/v2/${usageKey}/embed/student_view/`} | ||
data-testid="block-preview" | ||
style={{ | ||
width: '100%', | ||
height: '100%', | ||
minHeight: '200px', | ||
border: '0 none', | ||
}} | ||
// allowing 'autoplay' is required to allow the video XBlock to control the YouTube iframe it has. | ||
allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" | ||
|
||
/> | ||
</div> | ||
); | ||
}; | ||
|
||
export default LibraryBlock; |
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,2 @@ | ||
/* eslint-disable-next-line import/prefer-default-export */ | ||
export { default as LibraryBlock } from './LibraryBlock'; |
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,11 @@ | ||
import { defineMessages } from '@edx/frontend-platform/i18n'; | ||
|
||
const messages = defineMessages({ | ||
iframeTitle: { | ||
id: 'course-authoring.library-authoring.library-block.iframe-title', | ||
defaultMessage: 'Preview', | ||
description: 'The title for the LibraryBlock iframe', | ||
}, | ||
}); | ||
|
||
export default messages; |
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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.component-preview-modal { | ||
min-width: map-get($grid-breakpoints, "md"); | ||
} |
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,64 @@ | ||
import React from 'react'; | ||
import { useIntl } from '@edx/frontend-platform/i18n'; | ||
import { Button, StandardModal, useToggle } from '@openedx/paragon'; | ||
import { OpenInFull } from '@openedx/paragon/icons'; | ||
|
||
import { LibraryBlock } from '../LibraryBlock'; | ||
import messages from './messages'; | ||
|
||
// This is a simple overlay to prevent interaction with the preview | ||
const PreviewOverlay = () => ( | ||
<div className="position-absolute w-100 h-100 zindex-9" /> | ||
); | ||
|
||
interface ModalComponentPreviewProps { | ||
isOpen: boolean; | ||
close: () => void; | ||
usageKey: string; | ||
} | ||
|
||
const ModalComponentPreview = ({ isOpen, close, usageKey }: ModalComponentPreviewProps) => { | ||
const intl = useIntl(); | ||
|
||
return ( | ||
<StandardModal | ||
title={intl.formatMessage(messages.previewModalTitle)} | ||
isOpen={isOpen} | ||
onClose={close} | ||
className="component-preview-modal" | ||
> | ||
<LibraryBlock usageKey={usageKey} /> | ||
</StandardModal> | ||
); | ||
}; | ||
|
||
interface ComponentPreviewProps { | ||
usageKey: string; | ||
} | ||
|
||
const ComponentPreview = ({ usageKey }: ComponentPreviewProps) => { | ||
const intl = useIntl(); | ||
|
||
const [isModalOpen, openModal, closeModal] = useToggle(); | ||
|
||
return ( | ||
<> | ||
<div className="position-relative m-2"> | ||
<PreviewOverlay /> | ||
<Button | ||
size="sm" | ||
variant="light" | ||
iconBefore={OpenInFull} | ||
onClick={openModal} | ||
className="position-absolute right-0 zindex-10 m-1" | ||
> | ||
{intl.formatMessage(messages.previewExpandButtonTitle)} | ||
</Button> | ||
<LibraryBlock usageKey={usageKey} /> | ||
</div> | ||
<ModalComponentPreview isOpen={isModalOpen} close={closeModal} usageKey={usageKey} /> | ||
</> | ||
); | ||
}; | ||
|
||
export default ComponentPreview; |
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,3 +1,4 @@ | ||
@import "library-authoring/component-info/ComponentPreview"; | ||
@import "library-authoring/components/ComponentCard"; | ||
@import "library-authoring/library-info/LibraryPublishStatus"; | ||
@import "library-authoring/LibraryAuthoringPage"; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the future, it would be awesome to use something like react-reverse-portal to "re-parent" the
<LibraryBlock>
so that it moves from the sidebar to the modal instead of being copied into the modal. Then for example, you could start playing a video in the sidebar and "Expand" it and it would still be playing in the modal. Please mention that to the UX team as a possible future improvement after this merges (and post MVP).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure! I will mention that!