-
Notifications
You must be signed in to change notification settings - Fork 213
feat: pdf authoring #2916
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
Merged
Merged
feat: pdf authoring #2916
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
9a1ed3c
feat: pdf authoring
Kelketek da0d8fd
feat: address notes, pdf library support
Kelketek 1ade77f
fix: return handling from library uploads
Kelketek f4c9c7e
fix: use path instead of URL for PDF embed
Kelketek 2589a13
fix: static paths
Kelketek 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
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,32 @@ | ||
| /* Shared react-query hooks for editors. */ | ||
| import { useSelector } from 'react-redux'; | ||
| import { EditorState, selectors } from '@src/editors/data/redux'; | ||
| import { useEditorContext } from '@src/editors/EditorContext'; | ||
| import { getAuthenticatedHttpClient } from '@edx/frontend-platform/auth'; | ||
| import { useMutation } from '@tanstack/react-query'; | ||
| import * as urls from '@src/editors/data/services/cms/urls'; | ||
|
|
||
| export const useAssetUpload = ({ blockId, isLibrary }: { blockId: string, isLibrary: boolean }) => { | ||
| const studioEndpointUrl = useSelector((state: EditorState) => selectors.app.studioEndpointUrl(state))!; | ||
| const { learningContextId } = useEditorContext(); | ||
| const client = getAuthenticatedHttpClient(); | ||
| return useMutation({ | ||
| mutationFn: async (file: File) => { | ||
| const data = new FormData(); | ||
| if (isLibrary) { | ||
| data.append('content', file); | ||
| return client.put( | ||
| urls.libraryAssets({ | ||
| studioEndpointUrl, learningContextId, blockId, assetName: file.name, | ||
| }), | ||
| data, | ||
| ); | ||
| } | ||
| data.append('file', file); | ||
| return client.post( | ||
| urls.courseAssets({ studioEndpointUrl, learningContextId }), | ||
| data, | ||
| ); | ||
| }, | ||
| }); | ||
| }; |
This file contains hidden or 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,60 @@ | ||
| import { useQuery } from '@tanstack/react-query'; | ||
| import { useSelector } from 'react-redux'; | ||
| import { selectors } from '@src/editors/data/redux'; | ||
| import { camelizeKeys } from '@src/editors/utils'; | ||
| import { getAuthenticatedHttpClient } from '@edx/frontend-platform/auth'; | ||
| import type { Axios, AxiosResponse } from 'axios'; | ||
| import * as urls from '@src/editors/data/services/cms/urls'; | ||
|
|
||
| interface UseBlockDataParams<T> { | ||
| blockId: string, | ||
| uniqueId: string, | ||
| handlerName: string, | ||
| defaultData: T, | ||
| } | ||
|
|
||
| interface DeriveHandlerUrlParams { | ||
| studioEndpointUrl: string, | ||
| blockId: string, | ||
| handlerName: string, | ||
| isLibrary: boolean, | ||
| client: Axios, | ||
| } | ||
|
|
||
| export const immediate = <T>(val: T) => new Promise((resolve) => { resolve(val); }); | ||
|
|
||
| const deriveHandlerUrl = async ({ | ||
| studioEndpointUrl, blockId, handlerName, isLibrary, client, | ||
| }: DeriveHandlerUrlParams) => { | ||
| if (isLibrary) { | ||
| return client.get(urls.boundHandlerUrl({ studioEndpointUrl, blockId, handlerName })).then( | ||
| (response: AxiosResponse<{ handler_url: string }>) => response.data.handler_url, | ||
| ); | ||
| } | ||
| return urls.handlerUrl({ blockId, studioEndpointUrl, handlerName }); | ||
| }; | ||
|
|
||
| // Unique ID required due to intractable race conditions. See ./contexts.tsx file. | ||
| export const useBlockHandlerData = <T>({ | ||
| blockId, uniqueId, handlerName, defaultData, | ||
| }: UseBlockDataParams<T>) => { | ||
| const studioEndpointUrl = useSelector(selectors.app.studioEndpointUrl)!; | ||
| const isLibrary = useSelector(selectors.app.isLibrary); | ||
| const client = getAuthenticatedHttpClient(); | ||
| return useQuery<T>({ | ||
| queryKey: ['blockHandlerData', blockId, uniqueId, handlerName], | ||
| staleTime: Infinity, | ||
| queryFn: async ({ signal }) => { | ||
| if (!blockId) { | ||
| // No blockId is set yet, so there's nothing to fetch. | ||
| return immediate(defaultData); | ||
| } | ||
| return client.get( | ||
| await deriveHandlerUrl({ | ||
| blockId, studioEndpointUrl, handlerName, isLibrary, client, | ||
| }), | ||
| { cancelSource: signal }, | ||
| ).then((res: AxiosResponse<unknown>) => camelizeKeys(res.data) as T); | ||
| }, | ||
| }); | ||
| }; | ||
79 changes: 79 additions & 0 deletions
79
src/editors/containers/PdfEditor/components/PdfEditingModal.tsx
This file contains hidden or 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,79 @@ | ||
| import { EditorComponent } from '@src/editors/EditorComponent'; | ||
| import { useFormikContext } from 'formik'; | ||
| import React, { | ||
| PropsWithChildren, useContext, useEffect, useRef, | ||
| } from 'react'; | ||
| import EditorContainer from '@src/editors/containers/EditorContainer'; | ||
| import { PdfBlockContext, PdfState } from '@src/editors/containers/PdfEditor/contexts'; | ||
| import { isEqual } from 'lodash'; | ||
| import DownloadOptions from '@src/editors/containers/PdfEditor/components/sections/DownloadOptions'; | ||
| import { UploadWidget } from '@src/editors/sharedComponents/UploadWidget'; | ||
| import { Spinner } from '@openedx/paragon'; | ||
| import { FormattedMessage, useIntl } from '@edx/frontend-platform/i18n'; | ||
| import messages from './messages'; | ||
|
|
||
| const EditorWrapper: React.FC<PropsWithChildren> = ({ children }) => { | ||
| const intl = useIntl(); | ||
| const { isPending, fetchError } = useContext(PdfBlockContext); | ||
| if (fetchError) { | ||
| return ( | ||
| <div className="text-center p-6"> | ||
| <FormattedMessage {...messages.blockFailed} /> | ||
| </div> | ||
| ); | ||
| } | ||
| if (isPending) { | ||
| return ( | ||
| <div className="text-center p-6"> | ||
| <Spinner | ||
| animation="border" | ||
| className="m-3" | ||
| screenReaderText={intl.formatMessage(messages.blockLoading)} | ||
| /> | ||
| </div> | ||
| ); | ||
| } | ||
| return <>{children}</>; /* eslint-disable-line react/jsx-no-useless-fragment */ | ||
| }; | ||
|
|
||
| const PdfEditingModal: React.FC<EditorComponent> = (props) => { | ||
| const intl = useIntl(); | ||
| const { fields, blockId, isLibrary } = useContext(PdfBlockContext); | ||
| const originalState = useRef({ ...fields }); | ||
| const { values, setValues } = useFormikContext<PdfState>(); | ||
|
|
||
| useEffect(() => { | ||
| // Form is initialized before we get these values, so we have to set them | ||
| // when they arrive. | ||
| void setValues(fields); // eslint-disable-line no-void | ||
| }, [fields]); | ||
|
|
||
| const isDirty = () => isEqual(originalState, values); | ||
|
|
||
| const getContent = () => { | ||
| const settings = { ...values }; | ||
| // disableAllDownload is not a setting we control, but a backend flag. Have to remove it or the | ||
| // backend will reject. | ||
| return Object.fromEntries(Object.entries(settings).filter(([key]) => key !== 'disableAllDownload')); | ||
| }; | ||
|
|
||
| return ( | ||
| <EditorContainer {...props} isDirty={isDirty} getContent={getContent}> | ||
| <EditorWrapper> | ||
| <div className="mt-2"> | ||
| <UploadWidget | ||
| supportedFileFormats="application/pdf" | ||
| urlFieldName="url" | ||
| label={intl.formatMessage(messages.urlFieldLabel)} | ||
| blockId={blockId} | ||
| isLibrary={isLibrary} | ||
| id="pdf-url" | ||
| /> | ||
| </div> | ||
| <DownloadOptions /> | ||
| </EditorWrapper> | ||
| </EditorContainer> | ||
| ); | ||
| }; | ||
|
|
||
| export default PdfEditingModal; |
16 changes: 16 additions & 0 deletions
16
src/editors/containers/PdfEditor/components/PdfEditorContainer.tsx
This file contains hidden or 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,16 @@ | ||
| import React, { useContext } from 'react'; | ||
| import { PdfBlockContext } from '@src/editors/containers/PdfEditor/contexts'; | ||
| import { Formik } from 'formik'; | ||
| import { EditorComponent } from '@src/editors/EditorComponent'; | ||
| import PdfEditingModal from '@src/editors/containers/PdfEditor/components/PdfEditingModal'; | ||
|
|
||
| const PdfEditorContainer: React.FC<EditorComponent> = (props) => { | ||
| const { fields } = useContext(PdfBlockContext); | ||
| return ( | ||
| <Formik initialValues={fields} onSubmit={() => undefined}> | ||
| <PdfEditingModal {...props} /> | ||
| </Formik> | ||
| ); | ||
| }; | ||
|
|
||
| export default PdfEditorContainer; |
This file contains hidden or 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,19 @@ | ||
| import { defineMessages } from '@edx/frontend-platform/i18n'; | ||
|
|
||
| export default defineMessages({ | ||
| blockFailed: { | ||
| id: 'authoring.pdfEditor.blockFailed', | ||
| defaultMessage: 'PDF block failed to load', | ||
| description: 'Error message for PDF block failing to load', | ||
| }, | ||
| blockLoading: { | ||
| id: 'authoring.pdfEditor.blockLoading', | ||
| defaultMessage: 'Loading PDF Editor', | ||
| description: 'Message shown to screen readers when the PDF block is loading.', | ||
| }, | ||
| urlFieldLabel: { | ||
| id: 'authoring.pdfEditor.urlFieldLabel', | ||
| defaultMessage: 'File', | ||
| description: 'Label for the PDF URL field', | ||
| }, | ||
| }); |
45 changes: 45 additions & 0 deletions
45
src/editors/containers/PdfEditor/components/sections/DownloadOptions.tsx
This file contains hidden or 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,45 @@ | ||
| import React from 'react'; | ||
| import { useFormikContext } from 'formik'; | ||
| import { PdfState } from '@src/editors/containers/PdfEditor/contexts'; | ||
| import { optional, useUrlValidator } from '@src/editors/utils/validators'; | ||
| import { useIntl } from '@edx/frontend-platform/i18n'; | ||
| import CheckboxField from '@src/editors/sharedComponents/CheckboxField'; | ||
| import TextField from '@src/editors/sharedComponents/TextField'; | ||
| import messages from './messages'; | ||
|
|
||
| const DownloadOptions: React.FC = () => { | ||
| const intl = useIntl(); | ||
| const { values } = useFormikContext<PdfState>(); | ||
| const urlValidator = optional(useUrlValidator()); | ||
| if (values.disableAllDownload) { | ||
| // Download configuration is disabled at the instance-level, so don't even show these options. | ||
| return <></>; // eslint-disable-line react/jsx-no-useless-fragment | ||
| } | ||
| return ( | ||
| <> | ||
| <div className="mt-5 mb-4"> | ||
| <CheckboxField | ||
| label={intl.formatMessage(messages.allowDownloadLabel)} | ||
| id="pdf-allow-download" | ||
| hint={intl.formatMessage(messages.allowDownloadHint)} | ||
| fieldConfig="allowDownload" | ||
| /> | ||
| </div> | ||
| <TextField | ||
| label={intl.formatMessage(messages.sourceUrlLabel)} | ||
| name="sourceUrl" | ||
| id="pdf-source-url" | ||
| hint={intl.formatMessage(messages.sourceUrlHint)} | ||
| fieldConfig={{ validate: urlValidator }} | ||
| /> | ||
| <TextField | ||
| label={intl.formatMessage(messages.sourceDocumentButtonTextLabel)} | ||
| id="pdf-source-text" | ||
| placeholder={intl.formatMessage(messages.sourceDocumentButtonTextPlaceholder)} | ||
| name="sourceText" | ||
| /> | ||
| </> | ||
| ); | ||
| }; | ||
|
|
||
| export default DownloadOptions; |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.