@myst-theme/jupyter 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (82) hide show
  1. package/README.md +6 -0
  2. package/dist/cjs/components.d.ts +10 -0
  3. package/dist/cjs/components.d.ts.map +1 -0
  4. package/dist/cjs/components.js +41 -0
  5. package/dist/cjs/error.d.ts +6 -0
  6. package/dist/cjs/error.d.ts.map +1 -0
  7. package/dist/cjs/error.js +15 -0
  8. package/dist/cjs/hooks.d.ts +67 -0
  9. package/dist/cjs/hooks.d.ts.map +1 -0
  10. package/dist/cjs/hooks.js +85 -0
  11. package/dist/cjs/index.d.ts +6 -0
  12. package/dist/cjs/index.d.ts.map +1 -0
  13. package/dist/cjs/index.js +7 -0
  14. package/dist/cjs/jupyter.d.ts +7 -0
  15. package/dist/cjs/jupyter.d.ts.map +1 -0
  16. package/dist/cjs/jupyter.js +75 -0
  17. package/dist/cjs/output.d.ts +7 -0
  18. package/dist/cjs/output.d.ts.map +1 -0
  19. package/dist/cjs/output.js +66 -0
  20. package/dist/cjs/outputBlock.d.ts +10 -0
  21. package/dist/cjs/outputBlock.d.ts.map +1 -0
  22. package/dist/cjs/outputBlock.js +13 -0
  23. package/dist/cjs/safe.d.ts +7 -0
  24. package/dist/cjs/safe.d.ts.map +1 -0
  25. package/dist/cjs/safe.js +77 -0
  26. package/dist/cjs/selectors.d.ts +10 -0
  27. package/dist/cjs/selectors.d.ts.map +1 -0
  28. package/dist/cjs/selectors.js +10 -0
  29. package/dist/cjs/stream.d.ts +6 -0
  30. package/dist/cjs/stream.d.ts.map +1 -0
  31. package/dist/cjs/stream.js +13 -0
  32. package/dist/esm/components.d.ts +10 -0
  33. package/dist/esm/components.d.ts.map +1 -0
  34. package/dist/esm/components.js +36 -0
  35. package/dist/esm/error.d.ts +6 -0
  36. package/dist/esm/error.d.ts.map +1 -0
  37. package/dist/esm/error.js +9 -0
  38. package/dist/esm/hooks.d.ts +67 -0
  39. package/dist/esm/hooks.d.ts.map +1 -0
  40. package/dist/esm/hooks.js +76 -0
  41. package/dist/esm/index.d.ts +6 -0
  42. package/dist/esm/index.d.ts.map +1 -0
  43. package/dist/esm/index.js +5 -0
  44. package/dist/esm/jupyter.d.ts +7 -0
  45. package/dist/esm/jupyter.d.ts.map +1 -0
  46. package/dist/esm/jupyter.js +48 -0
  47. package/dist/esm/output.d.ts +7 -0
  48. package/dist/esm/output.d.ts.map +1 -0
  49. package/dist/esm/output.js +57 -0
  50. package/dist/esm/outputBlock.d.ts +10 -0
  51. package/dist/esm/outputBlock.d.ts.map +1 -0
  52. package/dist/esm/outputBlock.js +6 -0
  53. package/dist/esm/safe.d.ts +7 -0
  54. package/dist/esm/safe.d.ts.map +1 -0
  55. package/dist/esm/safe.js +70 -0
  56. package/dist/esm/selectors.d.ts +10 -0
  57. package/dist/esm/selectors.d.ts.map +1 -0
  58. package/dist/esm/selectors.js +4 -0
  59. package/dist/esm/stream.d.ts +6 -0
  60. package/dist/esm/stream.d.ts.map +1 -0
  61. package/dist/esm/stream.js +7 -0
  62. package/dist/types/components.d.ts +10 -0
  63. package/dist/types/components.d.ts.map +1 -0
  64. package/dist/types/error.d.ts +6 -0
  65. package/dist/types/error.d.ts.map +1 -0
  66. package/dist/types/hooks.d.ts +67 -0
  67. package/dist/types/hooks.d.ts.map +1 -0
  68. package/dist/types/index.d.ts +6 -0
  69. package/dist/types/index.d.ts.map +1 -0
  70. package/dist/types/jupyter.d.ts +7 -0
  71. package/dist/types/jupyter.d.ts.map +1 -0
  72. package/dist/types/output.d.ts +7 -0
  73. package/dist/types/output.d.ts.map +1 -0
  74. package/dist/types/outputBlock.d.ts +10 -0
  75. package/dist/types/outputBlock.d.ts.map +1 -0
  76. package/dist/types/safe.d.ts +7 -0
  77. package/dist/types/safe.d.ts.map +1 -0
  78. package/dist/types/selectors.d.ts +10 -0
  79. package/dist/types/selectors.d.ts.map +1 -0
  80. package/dist/types/stream.d.ts +6 -0
  81. package/dist/types/stream.d.ts.map +1 -0
  82. package/package.json +55 -0
package/README.md ADDED
@@ -0,0 +1,6 @@
1
+ # @myst-theme/diagrams
2
+
3
+ [![myst-demo on npm](https://img.shields.io/npm/v/myst-demo.svg)](https://www.npmjs.com/package/myst-demo)
4
+ [![MIT License](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/curvenote/curvenote/blob/main/LICENSE)
5
+
6
+ A demo component for MyST
@@ -0,0 +1,10 @@
1
+ /// <reference types="react" />
2
+ export declare const MaybeLongContent: ({ content, path, render, }: {
3
+ content?: string | undefined;
4
+ path?: string | undefined;
5
+ render: (content: string) => JSX.Element;
6
+ }) => JSX.Element;
7
+ export declare const DangerousHTML: ({ content, ...rest }: {
8
+ content: string;
9
+ }) => JSX.Element;
10
+ //# sourceMappingURL=components.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"components.d.ts","sourceRoot":"","sources":["../../src/components.tsx"],"names":[],"mappings":";AAGA,eAAO,MAAM,gBAAgB;;;sBAOT,MAAM,KAAK,WAAW;iBAUzC,CAAC;AAEF,eAAO,MAAM,aAAa;aAAqC,MAAM;iBAWpE,CAAC"}
@@ -0,0 +1,41 @@
1
+ "use strict";
2
+ var __rest = (this && this.__rest) || function (s, e) {
3
+ var t = {};
4
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
5
+ t[p] = s[p];
6
+ if (s != null && typeof Object.getOwnPropertySymbols === "function")
7
+ for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
8
+ if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
9
+ t[p[i]] = s[p[i]];
10
+ }
11
+ return t;
12
+ };
13
+ Object.defineProperty(exports, "__esModule", { value: true });
14
+ exports.DangerousHTML = exports.MaybeLongContent = void 0;
15
+ const jsx_runtime_1 = require("react/jsx-runtime");
16
+ const react_1 = require("react");
17
+ const hooks_1 = require("./hooks");
18
+ const MaybeLongContent = ({ content, path, render, }) => {
19
+ const { error, data } = (0, hooks_1.useLongContent)(content, path);
20
+ if (error) {
21
+ return (0, jsx_runtime_1.jsxs)("div", Object.assign({ className: "text-red-500" }, { children: ["Error loading content: ", error.message] }));
22
+ }
23
+ if (!data) {
24
+ return (0, jsx_runtime_1.jsx)("div", { children: "Fetching long content...." });
25
+ }
26
+ return (0, jsx_runtime_1.jsx)("div", { children: render(data.content) });
27
+ };
28
+ exports.MaybeLongContent = MaybeLongContent;
29
+ const DangerousHTML = (_a) => {
30
+ var { content } = _a, rest = __rest(_a, ["content"]);
31
+ const ref = (0, react_1.useRef)(null);
32
+ (0, react_1.useEffect)(() => {
33
+ if (!content || !ref.current)
34
+ return;
35
+ const el = document.createRange().createContextualFragment(content);
36
+ ref.current.innerHTML = '';
37
+ ref.current.appendChild(el);
38
+ }, [content, ref]);
39
+ return (0, jsx_runtime_1.jsx)("div", Object.assign({}, rest, { ref: ref }));
40
+ };
41
+ exports.DangerousHTML = DangerousHTML;
@@ -0,0 +1,6 @@
1
+ /// <reference types="react" />
2
+ import type { MinifiedErrorOutput } from '@curvenote/nbtx';
3
+ export default function Error({ output }: {
4
+ output: MinifiedErrorOutput;
5
+ }): JSX.Element;
6
+ //# sourceMappingURL=error.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"error.d.ts","sourceRoot":"","sources":["../../src/error.tsx"],"names":[],"mappings":";AAEA,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,iBAAiB,CAAC;AAG3D,MAAM,CAAC,OAAO,UAAU,KAAK,CAAC,EAAE,MAAM,EAAE,EAAE;IAAE,MAAM,EAAE,mBAAmB,CAAA;CAAE,eAcxE"}
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const jsx_runtime_1 = require("react/jsx-runtime");
7
+ const ansi_to_react_1 = __importDefault(require("ansi-to-react"));
8
+ const blocks_1 = require("@curvenote/blocks");
9
+ const components_1 = require("./components");
10
+ function Error({ output }) {
11
+ return ((0, jsx_runtime_1.jsx)(components_1.MaybeLongContent, { content: (0, blocks_1.ensureString)(output.traceback), path: output.path, render: (content) => {
12
+ return ((0, jsx_runtime_1.jsx)("pre", Object.assign({ className: "text-sm font-thin font-system jupyter-error" }, { children: (0, jsx_runtime_1.jsx)(ansi_to_react_1.default, { children: content !== null && content !== void 0 ? content : '' }) })));
13
+ } }));
14
+ }
15
+ exports.default = Error;
@@ -0,0 +1,67 @@
1
+ import type { MinifiedOutput } from '@curvenote/nbtx/dist/minify/types';
2
+ /**
3
+ * Truncation vs Summarization
4
+ *
5
+ * In Curvespace, we're decided to change our data structure for outputs to align it as
6
+ * closely as possible with Jupyters nbformat.IOutput[] type, but in a way that still allows
7
+ * us to truncate output content and push that to storage.
8
+ *
9
+ * This will be used only in the CLI and Curvespace initially but should be ported back to
10
+ * the rest of the code base. This will mean
11
+ *
12
+ * - changing the DB schema
13
+ * - migration
14
+ * - changing API response
15
+ * - changing the frontend
16
+ * - changing the extension
17
+ *
18
+ */
19
+ interface LongContent {
20
+ content_type?: string;
21
+ content: string;
22
+ }
23
+ export declare function useLongContent(content?: string, url?: string): {
24
+ data?: LongContent;
25
+ error?: Error;
26
+ };
27
+ export declare function useFetchAnyTruncatedContent(outputs: MinifiedOutput[]): {
28
+ data: ({
29
+ data: {};
30
+ path?: string | undefined;
31
+ traceback: string & string[];
32
+ output_type: "error";
33
+ ename: string;
34
+ evalue: string;
35
+ } | {
36
+ data: {};
37
+ path?: string | undefined;
38
+ output_type: "stream";
39
+ name: import("@jupyterlab/nbformat").StreamType;
40
+ text: import("@jupyterlab/nbformat").MultilineString;
41
+ } | {
42
+ data: {};
43
+ output_type: import("@curvenote/nbtx/dist/minify/types").MimeOutputType;
44
+ execution_count?: import("@jupyterlab/nbformat").ExecutionCount | undefined;
45
+ metadata: import("@lumino/coreutils").PartialJSONObject;
46
+ } | {
47
+ [x: string]: import("@lumino/coreutils").PartialJSONValue | undefined;
48
+ path?: string | undefined;
49
+ traceback: string & string[];
50
+ output_type: "error";
51
+ ename: string;
52
+ evalue: string;
53
+ } | {
54
+ [x: string]: import("@lumino/coreutils").PartialJSONValue | undefined;
55
+ path?: string | undefined;
56
+ output_type: "stream";
57
+ name: import("@jupyterlab/nbformat").StreamType;
58
+ text: import("@jupyterlab/nbformat").MultilineString;
59
+ })[] | undefined;
60
+ error: any;
61
+ };
62
+ export default function useWindowSize(): {
63
+ width: number;
64
+ height: number;
65
+ };
66
+ export {};
67
+ //# sourceMappingURL=hooks.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"hooks.d.ts","sourceRoot":"","sources":["../../src/hooks.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAIV,cAAc,EAEf,MAAM,mCAAmC,CAAC;AAI3C;;;;;;;;;;;;;;;;GAgBG;AAEH,UAAU,WAAW;IACnB,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,OAAO,EAAE,MAAM,CAAC;CACjB;AAQD,wBAAgB,cAAc,CAC5B,OAAO,CAAC,EAAE,MAAM,EAChB,GAAG,CAAC,EAAE,MAAM,GACX;IAAE,IAAI,CAAC,EAAE,WAAW,CAAC;IAAC,KAAK,CAAC,EAAE,KAAK,CAAA;CAAE,CAQvC;AAuBD,wBAAgB,2BAA2B,CAAC,OAAO,EAAE,cAAc,EAAE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EA6BpE;AAUD,MAAM,CAAC,OAAO,UAAU,aAAa;;;EAapC"}
@@ -0,0 +1,85 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.useFetchAnyTruncatedContent = exports.useLongContent = void 0;
7
+ const immutable_1 = __importDefault(require("swr/immutable"));
8
+ const utils_1 = require("@curvenote/nbtx/dist/minify/utils");
9
+ const react_1 = require("react");
10
+ const fetcher = (...args) => fetch(...args).then((res) => {
11
+ if (res.status === 200)
12
+ return res.json();
13
+ throw new Error(`Content returned with status ${res.status}.`);
14
+ });
15
+ function useLongContent(content, url) {
16
+ if (typeof document === 'undefined') {
17
+ // This is ONLY called on the server
18
+ return url ? {} : { data: { content: content !== null && content !== void 0 ? content : '' } };
19
+ }
20
+ const { data, error } = (0, immutable_1.default)(url || null, fetcher);
21
+ if (!url)
22
+ return { data: { content: content !== null && content !== void 0 ? content : '' } };
23
+ return { data, error };
24
+ }
25
+ exports.useLongContent = useLongContent;
26
+ const arrayFetcher = (...urls) => {
27
+ return Promise.all(urls.map((url) => fetcher(url)));
28
+ };
29
+ function shallowCloneOutputs(outputs) {
30
+ return outputs.map((output) => {
31
+ if ('data' in output && output.data) {
32
+ const data = output.data;
33
+ return Object.assign(Object.assign({}, output), { data: Object.entries(data).reduce((acc, [mimetype, payload]) => {
34
+ return Object.assign(Object.assign({}, acc), { [mimetype]: Object.assign({}, payload) });
35
+ }, {}) });
36
+ }
37
+ return Object.assign({}, output);
38
+ });
39
+ }
40
+ function useFetchAnyTruncatedContent(outputs) {
41
+ const itemsWithPaths = [];
42
+ const updated = shallowCloneOutputs(outputs);
43
+ (0, utils_1.walkPaths)(updated, (path, obj) => {
44
+ // images have paths, but we don't need to fetch them
45
+ if ('content_type' in obj && obj.content_type.startsWith('image/'))
46
+ return;
47
+ obj.path = path;
48
+ itemsWithPaths.push(obj);
49
+ });
50
+ const { data, error } = (0, immutable_1.default)(itemsWithPaths.map(({ path }) => path), arrayFetcher);
51
+ data === null || data === void 0 ? void 0 : data.forEach(({ content }, idx) => {
52
+ const obj = itemsWithPaths[idx];
53
+ if ('text' in obj)
54
+ obj.text = content; // stream
55
+ if ('traceback' in obj)
56
+ obj.traceback = content; // error
57
+ if ('content' in obj)
58
+ obj.content = content; // mimeoutput
59
+ obj.path = undefined;
60
+ });
61
+ return {
62
+ data: itemsWithPaths.length === 0 || data ? updated : undefined,
63
+ error,
64
+ };
65
+ }
66
+ exports.useFetchAnyTruncatedContent = useFetchAnyTruncatedContent;
67
+ function getWindowSize() {
68
+ const { innerWidth: width, innerHeight: height } = window;
69
+ return {
70
+ width,
71
+ height,
72
+ };
73
+ }
74
+ function useWindowSize() {
75
+ const [windowSize, setWindowSize] = (0, react_1.useState)(getWindowSize());
76
+ (0, react_1.useLayoutEffect)(() => {
77
+ function handleResize() {
78
+ setWindowSize(getWindowSize());
79
+ }
80
+ window.addEventListener('resize', handleResize);
81
+ return () => window.removeEventListener('resize', handleResize);
82
+ }, []);
83
+ return windowSize;
84
+ }
85
+ exports.default = useWindowSize;
@@ -0,0 +1,6 @@
1
+ import { Output } from './output';
2
+ declare const OUTPUT_RENDERERS: {
3
+ output: typeof Output;
4
+ };
5
+ export default OUTPUT_RENDERERS;
6
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,UAAU,CAAC;AAElC,QAAA,MAAM,gBAAgB;;CAErB,CAAC;AAEF,eAAe,gBAAgB,CAAC"}
@@ -0,0 +1,7 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const output_1 = require("./output");
4
+ const OUTPUT_RENDERERS = {
5
+ output: output_1.Output,
6
+ };
7
+ exports.default = OUTPUT_RENDERERS;
@@ -0,0 +1,7 @@
1
+ /// <reference types="react" />
2
+ import type { MinifiedOutput } from '@curvenote/nbtx';
3
+ export declare const NativeJupyterOutputs: ({ id, outputs, }: {
4
+ id: string;
5
+ outputs: MinifiedOutput[];
6
+ }) => JSX.Element;
7
+ //# sourceMappingURL=jupyter.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"jupyter.d.ts","sourceRoot":"","sources":["../../src/jupyter.tsx"],"names":[],"mappings":";AAKA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAC;AAQtD,eAAO,MAAM,oBAAoB;QAI3B,MAAM;aACD,cAAc,EAAE;iBAmE1B,CAAC"}
@@ -0,0 +1,75 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.NativeJupyterOutputs = void 0;
27
+ const jsx_runtime_1 = require("react/jsx-runtime");
28
+ const react_1 = require("react");
29
+ const hooks_1 = __importStar(require("./hooks"));
30
+ const nanoid_1 = require("nanoid");
31
+ const react_redux_1 = require("react-redux");
32
+ const connect_1 = require("@curvenote/connect");
33
+ const nbtx_1 = require("@curvenote/nbtx");
34
+ const outline_1 = require("@heroicons/react/24/outline");
35
+ const selectors_1 = require("./selectors");
36
+ const PERCENT_OF_WINDOW = 0.9;
37
+ const NativeJupyterOutputs = ({ id, outputs, }) => {
38
+ const windowSize = (0, hooks_1.default)();
39
+ const { data, error } = (0, hooks_1.useFetchAnyTruncatedContent)(outputs);
40
+ const [loading, setLoading] = (0, react_1.useState)(true);
41
+ const [frameHeight, setFrameHeight] = (0, react_1.useState)(0);
42
+ const [clamped, setClamped] = (0, react_1.useState)(false);
43
+ const uid = (0, react_1.useMemo)(nanoid_1.nanoid, []);
44
+ const height = (0, react_redux_1.useSelector)((state) => (0, selectors_1.selectIFrameHeight)(state, uid));
45
+ const rendererReady = (0, react_redux_1.useSelector)((state) => (0, selectors_1.selectIFrameReady)(state, uid));
46
+ const iframeRef = (0, react_1.useRef)(null);
47
+ (0, react_1.useEffect)(() => {
48
+ if (iframeRef.current == null || !rendererReady || !data)
49
+ return;
50
+ (0, nbtx_1.fetchAndEncodeOutputImages)((0, nbtx_1.convertToIOutputs)(data)).then((out) => {
51
+ connect_1.host.commsDispatch(iframeRef.current, connect_1.actions.connectHostSendContent(uid, out));
52
+ });
53
+ }, [id, iframeRef.current, rendererReady]);
54
+ (0, react_1.useEffect)(() => {
55
+ if (height == null)
56
+ return;
57
+ if (height > PERCENT_OF_WINDOW * windowSize.height) {
58
+ setFrameHeight(PERCENT_OF_WINDOW * windowSize.height);
59
+ setClamped(true);
60
+ }
61
+ else {
62
+ setFrameHeight(height + 25);
63
+ setClamped(false);
64
+ }
65
+ setLoading(false);
66
+ }, [height]);
67
+ if (error) {
68
+ return (0, jsx_runtime_1.jsxs)("div", Object.assign({ className: "text-red-500" }, { children: ["Error rendering output: ", error.message] }));
69
+ }
70
+ return ((0, jsx_runtime_1.jsxs)("div", { children: [loading && (0, jsx_runtime_1.jsx)("div", Object.assign({ className: "p-2.5" }, { children: "Loading..." })), (0, jsx_runtime_1.jsx)("iframe", { ref: iframeRef, id: uid, name: uid, title: uid, src: "https://next.curvenote.run", width: '100%', height: frameHeight, sandbox: "allow-scripts" }), clamped && ((0, jsx_runtime_1.jsx)("div", Object.assign({ className: "cursor-pointer p-1 pb-2 hover:bg-gray-50 dark:hover:bg-gray-700 text-center text-gray-500 hover:text-gray-600 dark:text-gray-200 dark:hover:text-gray-50", title: "Expand", onClick: () => {
71
+ setFrameHeight(height !== null && height !== void 0 ? height : 0);
72
+ setClamped(false);
73
+ } }, { children: (0, jsx_runtime_1.jsx)(outline_1.ChevronDoubleDownIcon, { className: "w-5 h-5 inline" }) })))] }));
74
+ };
75
+ exports.NativeJupyterOutputs = NativeJupyterOutputs;
@@ -0,0 +1,7 @@
1
+ /// <reference types="react" />
2
+ import type { GenericNode } from 'mystjs';
3
+ import type { MinifiedOutput } from '@curvenote/nbtx/dist/minify/types';
4
+ export declare function allOutputsAreSafe(outputs: MinifiedOutput[], directOutputTypes: Set<string>, directMimeTypes: Set<string>): boolean;
5
+ export declare function anyErrors(outputs: MinifiedOutput[]): boolean;
6
+ export declare function Output(node: GenericNode): JSX.Element;
7
+ //# sourceMappingURL=output.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"output.d.ts","sourceRoot":"","sources":["../../src/output.tsx"],"names":[],"mappings":";AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,QAAQ,CAAC;AAE1C,OAAO,KAAK,EAAsB,cAAc,EAAE,MAAM,mCAAmC,CAAC;AAgB5F,wBAAgB,iBAAiB,CAC/B,OAAO,EAAE,cAAc,EAAE,EACzB,iBAAiB,EAAE,GAAG,CAAC,MAAM,CAAC,EAC9B,eAAe,EAAE,GAAG,CAAC,MAAM,CAAC,WAY7B;AAWD,wBAAgB,SAAS,CAAC,OAAO,EAAE,cAAc,EAAE,WAElD;AAED,wBAAgB,MAAM,CAAC,IAAI,EAAE,WAAW,eA8BvC"}
@@ -0,0 +1,66 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.Output = exports.anyErrors = exports.allOutputsAreSafe = void 0;
7
+ const jsx_runtime_1 = require("react/jsx-runtime");
8
+ const blocks_1 = require("@curvenote/blocks");
9
+ const classnames_1 = __importDefault(require("classnames"));
10
+ const safe_1 = require("./safe");
11
+ const jupyter_1 = require("./jupyter");
12
+ const outputBlock_1 = require("./outputBlock");
13
+ const DIRECT_OUTPUT_TYPES = new Set(['stream', 'error']);
14
+ const DIRECT_MIME_TYPES = new Set([
15
+ blocks_1.KnownCellOutputMimeTypes.TextPlain,
16
+ blocks_1.KnownCellOutputMimeTypes.ImagePng,
17
+ blocks_1.KnownCellOutputMimeTypes.ImageGif,
18
+ blocks_1.KnownCellOutputMimeTypes.ImageJpeg,
19
+ blocks_1.KnownCellOutputMimeTypes.ImageBmp,
20
+ ]);
21
+ function allOutputsAreSafe(outputs, directOutputTypes, directMimeTypes) {
22
+ return outputs.reduce((flag, output) => {
23
+ if (directOutputTypes.has(output.output_type))
24
+ return true;
25
+ const data = output.data;
26
+ const mimetypes = data ? Object.keys(data) : [];
27
+ const safe = 'data' in output &&
28
+ Boolean(output.data) &&
29
+ mimetypes.every((mimetype) => directMimeTypes.has(mimetype));
30
+ return flag && safe;
31
+ }, true);
32
+ }
33
+ exports.allOutputsAreSafe = allOutputsAreSafe;
34
+ function listMimetypes(outputs, directOutputTypes) {
35
+ return outputs.map((output) => {
36
+ if (directOutputTypes.has(output.output_type))
37
+ return [output.output_type];
38
+ const data = output.data;
39
+ const mimetypes = data ? Object.keys(data) : [];
40
+ return [output.output_type, mimetypes];
41
+ });
42
+ }
43
+ function anyErrors(outputs) {
44
+ return outputs.reduce((flag, output) => flag || output.output_type === 'error', false);
45
+ }
46
+ exports.anyErrors = anyErrors;
47
+ function Output(node) {
48
+ const outputs = node.data;
49
+ const allSafe = allOutputsAreSafe(outputs, DIRECT_OUTPUT_TYPES, DIRECT_MIME_TYPES);
50
+ const hasError = anyErrors(outputs);
51
+ let component;
52
+ if (allSafe) {
53
+ component = (0, jsx_runtime_1.jsx)(safe_1.SafeOutputs, { keyStub: node.key, outputs: outputs });
54
+ }
55
+ else {
56
+ // Hide the iframe if rendering on the server
57
+ component =
58
+ typeof document === 'undefined' ? null : (0, jsx_runtime_1.jsx)(jupyter_1.NativeJupyterOutputs, { id: node.key, outputs: outputs });
59
+ }
60
+ return ((0, jsx_runtime_1.jsx)("figure", Object.assign({ suppressHydrationWarning: !allSafe, id: node.identifier || undefined, className: (0, classnames_1.default)('max-w-full overflow-auto m-0', {
61
+ 'text-left': !node.align || node.align === 'left',
62
+ 'text-center': node.align === 'center',
63
+ 'text-right': node.align === 'right',
64
+ }) }, { children: (0, jsx_runtime_1.jsx)(outputBlock_1.OutputBlock, Object.assign({ allSafe: allSafe, hasError: hasError }, { children: component })) }), node.key));
65
+ }
66
+ exports.Output = Output;
@@ -0,0 +1,10 @@
1
+ /// <reference types="react" />
2
+ type Props = {
3
+ children?: React.ReactNode;
4
+ allSafe?: boolean;
5
+ hasError?: boolean;
6
+ className?: string;
7
+ };
8
+ export declare function OutputBlock(props: Props): JSX.Element;
9
+ export {};
10
+ //# sourceMappingURL=outputBlock.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"outputBlock.d.ts","sourceRoot":"","sources":["../../src/outputBlock.tsx"],"names":[],"mappings":";AAEA,KAAK,KAAK,GAAG;IACX,QAAQ,CAAC,EAAE,KAAK,CAAC,SAAS,CAAC;IAC3B,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB,CAAC;AAEF,wBAAgB,WAAW,CAAC,KAAK,EAAE,KAAK,eAWvC"}
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.OutputBlock = void 0;
7
+ const jsx_runtime_1 = require("react/jsx-runtime");
8
+ const classnames_1 = __importDefault(require("classnames"));
9
+ function OutputBlock(props) {
10
+ const { children, allSafe, className } = props;
11
+ return ((0, jsx_runtime_1.jsx)("div", Object.assign({ suppressHydrationWarning: !allSafe, className: (0, classnames_1.default)('relative group not-prose overflow-auto mb-4 pl-0.5', className) }, { children: children })));
12
+ }
13
+ exports.OutputBlock = OutputBlock;
@@ -0,0 +1,7 @@
1
+ /// <reference types="react" />
2
+ import type { MinifiedOutput } from '@curvenote/nbtx/dist/minify/types';
3
+ export declare function SafeOutputs({ keyStub, outputs }: {
4
+ keyStub: string;
5
+ outputs: MinifiedOutput[];
6
+ }): JSX.Element;
7
+ //# sourceMappingURL=safe.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"safe.d.ts","sourceRoot":"","sources":["../../src/safe.tsx"],"names":[],"mappings":";AACA,OAAO,KAAK,EAGV,cAAc,EACf,MAAM,mCAAmC,CAAC;AAwE3C,wBAAgB,WAAW,CAAC,EAAE,OAAO,EAAE,OAAO,EAAE,EAAE;IAAE,OAAO,EAAE,MAAM,CAAC;IAAC,OAAO,EAAE,cAAc,EAAE,CAAA;CAAE,eAM/F"}
@@ -0,0 +1,77 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.SafeOutputs = void 0;
7
+ const jsx_runtime_1 = require("react/jsx-runtime");
8
+ const jupyter_1 = require("@curvenote/blocks/dist/blocks/types/jupyter");
9
+ const stream_1 = __importDefault(require("./stream"));
10
+ const error_1 = __importDefault(require("./error"));
11
+ const ansi_to_react_1 = __importDefault(require("ansi-to-react"));
12
+ /**
13
+ * https://jupyterbook.org/content/code-outputs.html#render-priority
14
+ *
15
+ * nb_render_priority:
16
+ html:
17
+ - "application/vnd.jupyter.widget-view+json"
18
+ - "application/javascript"
19
+ - "text/html"
20
+ - "image/svg+xml"
21
+ - "image/png"
22
+ - "image/jpeg"
23
+ - "text/markdown"
24
+ - "text/latex"
25
+ - "text/plain"
26
+ */
27
+ const RENDER_PRIORITY = [
28
+ jupyter_1.KnownCellOutputMimeTypes.ImagePng,
29
+ jupyter_1.KnownCellOutputMimeTypes.ImageJpeg,
30
+ jupyter_1.KnownCellOutputMimeTypes.ImageGif,
31
+ jupyter_1.KnownCellOutputMimeTypes.ImageBmp,
32
+ ];
33
+ function findSafeMimeOutputs(output) {
34
+ const data = output.data;
35
+ const image = RENDER_PRIORITY.reduce((acc, mimetype) => {
36
+ if (acc)
37
+ return acc;
38
+ if (data && data[mimetype])
39
+ return data[mimetype];
40
+ return undefined;
41
+ }, undefined);
42
+ const text = data && data['text/plain'];
43
+ return { image, text };
44
+ }
45
+ function OutputImage({ image, text }) {
46
+ var _a;
47
+ return (0, jsx_runtime_1.jsx)("img", { src: image === null || image === void 0 ? void 0 : image.path, alt: (_a = text === null || text === void 0 ? void 0 : text.content) !== null && _a !== void 0 ? _a : 'Image produced in Jupyter' });
48
+ }
49
+ function SafeOutput({ output }) {
50
+ switch (output.output_type) {
51
+ case 'stream':
52
+ return (0, jsx_runtime_1.jsx)(stream_1.default, { output: output });
53
+ case 'error':
54
+ return (0, jsx_runtime_1.jsx)(error_1.default, { output: output });
55
+ case 'display_data':
56
+ case 'execute_result':
57
+ case 'update_display_data': {
58
+ const { image, text } = findSafeMimeOutputs(output);
59
+ if (!image && !text)
60
+ return null;
61
+ if (image)
62
+ return (0, jsx_runtime_1.jsx)(OutputImage, { image: image, text: text });
63
+ if (text)
64
+ return ((0, jsx_runtime_1.jsx)("div", { children: (0, jsx_runtime_1.jsx)(ansi_to_react_1.default, { children: text.content }) }));
65
+ return null;
66
+ }
67
+ default:
68
+ console.warn(`Unknown output_type ${output['output_type']}`);
69
+ return null;
70
+ }
71
+ }
72
+ function SafeOutputs({ keyStub, outputs }) {
73
+ // TODO better key - add keys during content creation?
74
+ const components = outputs.map((output, idx) => ((0, jsx_runtime_1.jsx)(SafeOutput, { output: output }, `${keyStub}-${idx}`)));
75
+ return (0, jsx_runtime_1.jsx)(jsx_runtime_1.Fragment, { children: components });
76
+ }
77
+ exports.SafeOutputs = SafeOutputs;
@@ -0,0 +1,10 @@
1
+ import type { HostState } from '@curvenote/connect';
2
+ export interface State {
3
+ app: HostState;
4
+ }
5
+ export declare const selectIFrameHeight: (state: State, id: string) => number | null;
6
+ export declare const selectIFrameReady: (state: State, id: string) => boolean;
7
+ export declare const selectIFrameSendFailed: (state: State, id: string) => false | {
8
+ message: string | undefined;
9
+ };
10
+ //# sourceMappingURL=selectors.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"selectors.d.ts","sourceRoot":"","sources":["../../src/selectors.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,oBAAoB,CAAC;AAGpD,MAAM,WAAW,KAAK;IACpB,GAAG,EAAE,SAAS,CAAC;CAChB;AAED,eAAO,MAAM,kBAAkB,UAAW,KAAK,MAAM,MAAM,kBACX,CAAC;AAEjD,eAAO,MAAM,iBAAiB,UAAW,KAAK,MAAM,MAAM,YACT,CAAC;AAElD,eAAO,MAAM,sBAAsB,UAAW,KAAK,MAAM,MAAM;;CACb,CAAC"}
@@ -0,0 +1,10 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.selectIFrameSendFailed = exports.selectIFrameReady = exports.selectIFrameHeight = void 0;
4
+ const connect_1 = require("@curvenote/connect");
5
+ const selectIFrameHeight = (state, id) => connect_1.host.selectors.selectIFrameSize(state.app, id);
6
+ exports.selectIFrameHeight = selectIFrameHeight;
7
+ const selectIFrameReady = (state, id) => connect_1.host.selectors.selectIFrameReady(state.app, id);
8
+ exports.selectIFrameReady = selectIFrameReady;
9
+ const selectIFrameSendFailed = (state, id) => connect_1.host.selectors.selectIFrameFailed(state.app, id);
10
+ exports.selectIFrameSendFailed = selectIFrameSendFailed;
@@ -0,0 +1,6 @@
1
+ /// <reference types="react" />
2
+ import type { MinifiedStreamOutput } from '@curvenote/nbtx';
3
+ export default function Stream({ output }: {
4
+ output: MinifiedStreamOutput;
5
+ }): JSX.Element;
6
+ //# sourceMappingURL=stream.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"stream.d.ts","sourceRoot":"","sources":["../../src/stream.tsx"],"names":[],"mappings":";AAEA,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,iBAAiB,CAAC;AAG5D,MAAM,CAAC,OAAO,UAAU,MAAM,CAAC,EAAE,MAAM,EAAE,EAAE;IAAE,MAAM,EAAE,oBAAoB,CAAA;CAAE,eAY1E"}
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const jsx_runtime_1 = require("react/jsx-runtime");
7
+ const ansi_to_react_1 = __importDefault(require("ansi-to-react"));
8
+ const blocks_1 = require("@curvenote/blocks");
9
+ const components_1 = require("./components");
10
+ function Stream({ output }) {
11
+ return ((0, jsx_runtime_1.jsx)(components_1.MaybeLongContent, { content: (0, blocks_1.ensureString)(output.text), path: output.path, render: (content) => ((0, jsx_runtime_1.jsx)("pre", Object.assign({ className: "text-sm font-thin font-system" }, { children: (0, jsx_runtime_1.jsx)(ansi_to_react_1.default, { children: content !== null && content !== void 0 ? content : '' }) }))) }));
12
+ }
13
+ exports.default = Stream;
@@ -0,0 +1,10 @@
1
+ /// <reference types="react" />
2
+ export declare const MaybeLongContent: ({ content, path, render, }: {
3
+ content?: string | undefined;
4
+ path?: string | undefined;
5
+ render: (content: string) => JSX.Element;
6
+ }) => JSX.Element;
7
+ export declare const DangerousHTML: ({ content, ...rest }: {
8
+ content: string;
9
+ }) => JSX.Element;
10
+ //# sourceMappingURL=components.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"components.d.ts","sourceRoot":"","sources":["../../src/components.tsx"],"names":[],"mappings":";AAGA,eAAO,MAAM,gBAAgB;;;sBAOT,MAAM,KAAK,WAAW;iBAUzC,CAAC;AAEF,eAAO,MAAM,aAAa;aAAqC,MAAM;iBAWpE,CAAC"}