2021-10-08 05:51:04 -04:00
|
|
|
import { createContext, useContext, useMemo } from 'react'
|
2021-04-19 08:38:03 -04:00
|
|
|
import PropTypes from 'prop-types'
|
2021-10-08 06:09:41 -04:00
|
|
|
import useScopeValue from '../hooks/use-scope-value'
|
2021-04-19 08:38:03 -04:00
|
|
|
|
|
|
|
export const CompileContext = createContext()
|
|
|
|
|
|
|
|
CompileContext.Provider.propTypes = {
|
|
|
|
value: PropTypes.shape({
|
2021-10-08 05:51:04 -04:00
|
|
|
clsiServerId: PropTypes.string,
|
2021-04-19 08:38:03 -04:00
|
|
|
logEntries: PropTypes.object,
|
2021-10-08 05:51:04 -04:00
|
|
|
logEntryAnnotations: PropTypes.object,
|
|
|
|
pdfDownloadUrl: PropTypes.string,
|
|
|
|
pdfUrl: PropTypes.string,
|
|
|
|
setClsiServerId: PropTypes.func.isRequired,
|
|
|
|
setLogEntries: PropTypes.func.isRequired,
|
|
|
|
setLogEntryAnnotations: PropTypes.func.isRequired,
|
|
|
|
setPdfDownloadUrl: PropTypes.func.isRequired,
|
|
|
|
setPdfUrl: PropTypes.func.isRequired,
|
|
|
|
setUncompiled: PropTypes.func.isRequired,
|
2021-04-27 03:52:58 -04:00
|
|
|
uncompiled: PropTypes.bool,
|
|
|
|
}),
|
2021-04-19 08:38:03 -04:00
|
|
|
}
|
|
|
|
|
2021-06-16 05:32:38 -04:00
|
|
|
export function CompileProvider({ children }) {
|
2021-10-08 05:51:04 -04:00
|
|
|
// the log entries parsed from the compile output log
|
|
|
|
const [logEntries, setLogEntries] = useScopeValue('pdf.logEntries')
|
|
|
|
|
|
|
|
// annotations for display in the editor, built from the log entries
|
|
|
|
const [logEntryAnnotations, setLogEntryAnnotations] = useScopeValue(
|
|
|
|
'pdf.logEntryAnnotations'
|
|
|
|
)
|
|
|
|
|
|
|
|
// the URL for downloading the PDF
|
|
|
|
const [pdfDownloadUrl, setPdfDownloadUrl] = useScopeValue('pdf.downloadUrl')
|
|
|
|
|
|
|
|
// the URL for loading the PDF in the preview pane
|
|
|
|
const [pdfUrl, setPdfUrl] = useScopeValue('pdf.url')
|
|
|
|
|
|
|
|
// the project is considered to be "uncompiled" if a doc has changed since the last compile started
|
|
|
|
const [uncompiled, setUncompiled] = useScopeValue('pdf.uncompiled')
|
|
|
|
|
|
|
|
// the id of the CLSI server which ran the compile
|
|
|
|
const [clsiServerId, setClsiServerId] = useScopeValue('pdf.clsiServerId')
|
|
|
|
|
|
|
|
const value = useMemo(
|
|
|
|
() => ({
|
|
|
|
clsiServerId,
|
|
|
|
logEntries,
|
|
|
|
logEntryAnnotations,
|
|
|
|
pdfDownloadUrl,
|
|
|
|
pdfUrl,
|
|
|
|
setClsiServerId,
|
|
|
|
setLogEntries,
|
|
|
|
setLogEntryAnnotations,
|
|
|
|
setPdfDownloadUrl,
|
|
|
|
setPdfUrl,
|
|
|
|
setUncompiled,
|
|
|
|
uncompiled,
|
|
|
|
}),
|
|
|
|
[
|
|
|
|
clsiServerId,
|
|
|
|
logEntries,
|
|
|
|
logEntryAnnotations,
|
|
|
|
pdfDownloadUrl,
|
|
|
|
pdfUrl,
|
|
|
|
setClsiServerId,
|
|
|
|
setLogEntries,
|
|
|
|
setLogEntryAnnotations,
|
|
|
|
setPdfDownloadUrl,
|
|
|
|
setPdfUrl,
|
|
|
|
setUncompiled,
|
|
|
|
uncompiled,
|
|
|
|
]
|
|
|
|
)
|
2021-04-19 08:38:03 -04:00
|
|
|
|
|
|
|
return (
|
2021-06-16 05:32:38 -04:00
|
|
|
<CompileContext.Provider value={value}>{children}</CompileContext.Provider>
|
2021-04-19 08:38:03 -04:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
CompileProvider.propTypes = {
|
|
|
|
children: PropTypes.any,
|
|
|
|
}
|
|
|
|
|
|
|
|
export function useCompileContext(propTypes) {
|
|
|
|
const data = useContext(CompileContext)
|
|
|
|
PropTypes.checkPropTypes(propTypes, data, 'data', 'CompileContext.Provider')
|
|
|
|
return data
|
|
|
|
}
|