2022-06-11 03:08:49 +00:00
|
|
|
import { useDefinedContext } from "../../../utils/definedContext";
|
|
|
|
import { createStateContext } from "../../../utils/stateContext";
|
|
|
|
|
2022-07-18 04:12:11 +00:00
|
|
|
|
2022-06-11 03:08:49 +00:00
|
|
|
/**
|
|
|
|
* The mode the editing context is currently in.
|
|
|
|
*/
|
|
|
|
export enum EditingMode {
|
|
|
|
/**
|
|
|
|
* The page is being (pre)viewed.
|
|
|
|
*/
|
|
|
|
VIEW = "view",
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The page is being edited.
|
|
|
|
*/
|
|
|
|
EDIT = "edit",
|
|
|
|
}
|
|
|
|
|
2022-07-18 04:12:11 +00:00
|
|
|
|
2022-06-11 03:08:49 +00:00
|
|
|
/**
|
|
|
|
* The context of a previewable `form`.
|
|
|
|
*/
|
|
|
|
export const EditingContext = createStateContext<EditingMode>()
|
|
|
|
|
|
|
|
|
2022-07-18 04:12:11 +00:00
|
|
|
/**
|
|
|
|
* Parameters of {@link EditingModeBranch}.
|
|
|
|
*
|
|
|
|
* @todo Perhaps this should be changed to callbacks, so that elements are _not_ rendered unless they are required.
|
|
|
|
*/
|
2022-06-11 03:08:49 +00:00
|
|
|
export type EditingModeBranchProps = {
|
|
|
|
[Mode in EditingMode]: JSX.Element
|
|
|
|
}
|
|
|
|
|
2022-07-18 04:12:11 +00:00
|
|
|
|
2022-06-11 03:08:49 +00:00
|
|
|
/**
|
|
|
|
* Component branching between its props based on the {@link EditingMode} of its innermost surrounding context.
|
|
|
|
*/
|
|
|
|
export const EditingModeBranch = (props: EditingModeBranchProps) => {
|
|
|
|
const [mode] = useDefinedContext(EditingContext)
|
|
|
|
|
|
|
|
return props[mode]
|
|
|
|
}
|