-
Notifications
You must be signed in to change notification settings - Fork 218
feat: PDF auto-conversion #3201
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
base: master
Are you sure you want to change the base?
Changes from 3 commits
ff9a08c
93041dc
a077643
b9c74cc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,6 +14,8 @@ import { UploadWidget } from '@src/editors/sharedComponents/UploadWidget'; | |
| import { Spinner } from '@openedx/paragon'; | ||
| import { FormattedMessage, useIntl } from '@edx/frontend-platform/i18n'; | ||
| import messages from './messages'; | ||
| import { FieldSaverArgs } from '@src/editors/sharedComponents/UploadWidget/UploadWidget'; | ||
| import { usePdfConversion } from '@src/editors/containers/PdfEditor/api'; | ||
|
|
||
| const EditorWrapper: React.FC<PropsWithChildren> = ({ children }) => { | ||
| const intl = useIntl(); | ||
|
|
@@ -44,6 +46,7 @@ const PdfEditingModal: React.FC<EditorComponent> = (props) => { | |
| const { fields, blockId, isLibrary } = useContext(PdfBlockContext); | ||
| const originalState = useRef({ ...fields }); | ||
| const { values, setValues } = useFormikContext<PdfState>(); | ||
| const mutation = usePdfConversion(blockId); | ||
|
|
||
| useEffect(() => { | ||
| // Form is initialized before we get these values, so we have to set them | ||
|
|
@@ -57,19 +60,46 @@ const PdfEditingModal: React.FC<EditorComponent> = (props) => { | |
| 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')); | ||
| const ignored = ['disableAllDownload', 'conversionAvailable']; | ||
| return Object.fromEntries(Object.entries(settings).filter(([key]) => !ignored.includes(key))); | ||
| }; | ||
|
|
||
| const supportedFormats = ['application/pdf']; | ||
| if (values.conversionAvailable) { | ||
| supportedFormats.push( | ||
| 'application/msword', | ||
| 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', | ||
| 'application/vnd.oasis.opendocument.presentation', | ||
| 'application/vnd.oasis.opendocument.spreadsheet', | ||
| 'application/vnd.oasis.opendocument.text', | ||
| 'application/vnd.ms-powerpoint', | ||
| 'application/vnd.openxmlformats-officedocument.presentationml.presentation', | ||
| 'application/rtf', | ||
| 'application/vnd.ms-excel', | ||
| 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', | ||
| ); | ||
| } | ||
|
|
||
| const saver = async (args: FieldSaverArgs<string>) => { | ||
| if (!values.conversionAvailable || args.sourceFile.type == 'application/pdf') { | ||
| // No conversion means no swapping of file values-- just set the field value and be done. | ||
| return args.control.setValue(args.value); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If someone uploads a .docx, lets it convert, then replaces it with a plain .pdf, this branch only sets the url field.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think keeping it is the lesser of the two evils here. If the user has the PDF autoconverted, and doesn't like the result, they might go into name-brand Office and print to PDF, and then replace the PDF, not knowing that it implicitly cleared the source URL. I'm going to lean toward the case that prevents data loss even if it might mean that the two documents get a bit out of sync. In most cases I would not expect the replacement document to be totally unrelated, just out of date. |
||
| } | ||
| const pdfUrl = await mutation.mutateAsync(args.value); | ||
| await setValues(prev => ({ ...prev, url: pdfUrl, sourceUrl: args.value })); | ||
| }; | ||
|
|
||
| return ( | ||
| <EditorContainer {...props} isDirty={isDirty} getContent={getContent}> | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Noticed one more thing - Save button is still live during conversion. So while Gotenberg is working, the author can hit Save, the old values.url gets serialised, and the converted PDF ends up orphaned in course or library assets with nothing pointing at it. The smallest fix I can see is an optional prop (say |
||
| <EditorWrapper> | ||
| <div className="mt-2"> | ||
| <UploadWidget | ||
| supportedFileFormats="application/pdf" | ||
| supportedFileFormats={supportedFormats} | ||
| urlFieldName="url" | ||
| label={intl.formatMessage(messages.urlFieldLabel)} | ||
| blockId={blockId} | ||
| isLibrary={isLibrary} | ||
| saveField={saver} | ||
| id="pdf-url" | ||
| /> | ||
| </div> | ||
|
|
||
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.
Nit: please add a docstring. For example, it's not obvious if this is converting from PDF or to PDF from the code here alone. And is this functionality that is always available? Are there specific errors we can expect?