import { actions, ContentRef } from "@nteract/core"; import { Cells, CodeCell, RawCell } from "@nteract/stateful-components"; import CodeMirrorEditor from "@nteract/stateful-components/lib/inputs/connected-editors/codemirror"; import { PassedEditorProps } from "@nteract/stateful-components/lib/inputs/editor"; import Prompt, { PassedPromptProps } from "@nteract/stateful-components/lib/inputs/prompt"; import * as React from "react"; import { connect } from "react-redux"; import { Dispatch } from "redux"; import { userContext } from "../../../UserContext"; import loadTransform from "../NotebookComponent/loadTransform"; import { AzureTheme } from "./AzureTheme"; import "./base.css"; import "./default.css"; import MarkdownCell from "./markdown-cell"; import "./NotebookReadOnlyRenderer.less"; import SandboxOutputs from "./outputs/SandboxOutputs"; export interface NotebookRendererProps { contentRef: ContentRef; hideInputs?: boolean; hidePrompts?: boolean; addTransform: (component: React.ComponentType & { MIMETYPE: string }) => void; } /** * This is the class that uses nteract to render a read-only notebook. */ class NotebookReadOnlyRenderer extends React.Component { componentDidMount() { if (!userContext.features.sandboxNotebookOutputs) { loadTransform(this.props as NotebookRendererProps); } } private renderPrompt(id: string, contentRef: string): JSX.Element { if (this.props.hidePrompts) { return <>; } return ( {(props: PassedPromptProps) => { if (props.status === "busy") { return {"[*]"}; } if (props.status === "queued") { return {"[…]"}; } if (typeof props.executionCount === "number") { return {`[${props.executionCount}]`}; } return {"[ ]"}; }} ); } render(): JSX.Element { return (
{{ code: ({ id, contentRef }: { id: string; contentRef: ContentRef }) => ( {{ prompt: (props: { id: string; contentRef: string }) => this.renderPrompt(props.id, props.contentRef), outputs: userContext.features.sandboxNotebookOutputs ? () => : undefined, editor: { codemirror: (props: PassedEditorProps) => this.props.hideInputs ? <> : , }, }} ), markdown: ({ id, contentRef }: { id: string; contentRef: ContentRef }) => ( {{ editor: {}, }} ), raw: ({ id, contentRef }: { id: string; contentRef: ContentRef }) => ( {{ editor: { codemirror: (props: PassedEditorProps) => this.props.hideInputs ? <> : , }, }} ), }}
); } } // eslint-disable-next-line @typescript-eslint/no-unused-vars const makeMapDispatchToProps = (initialDispatch: Dispatch, initialProps: NotebookRendererProps) => { const mapDispatchToProps = (dispatch: Dispatch) => { return { addTransform: (transform: React.ComponentType & { MIMETYPE: string }) => { return dispatch( actions.addTransform({ mediaType: transform.MIMETYPE, component: transform, }), ); }, }; }; return mapDispatchToProps; }; export default connect(undefined, makeMapDispatchToProps)(NotebookReadOnlyRenderer);