@myst-theme/jupyter 0.0.1 → 0.0.3

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 (79) hide show
  1. package/dist/cjs/ClientOnly.d.ts +5 -0
  2. package/dist/cjs/ClientOnly.d.ts.map +1 -0
  3. package/dist/cjs/ClientOnly.js +12 -0
  4. package/dist/cjs/convertImages.d.ts +3 -0
  5. package/dist/cjs/convertImages.d.ts.map +1 -0
  6. package/dist/cjs/convertImages.js +70 -0
  7. package/dist/cjs/error.d.ts +1 -1
  8. package/dist/cjs/error.d.ts.map +1 -1
  9. package/dist/cjs/error.js +2 -2
  10. package/dist/cjs/hooks.d.ts +3 -34
  11. package/dist/cjs/hooks.d.ts.map +1 -1
  12. package/dist/cjs/hooks.js +52 -15
  13. package/dist/cjs/jupyter.d.ts +1 -1
  14. package/dist/cjs/jupyter.d.ts.map +1 -1
  15. package/dist/cjs/jupyter.js +7 -4
  16. package/dist/cjs/output.d.ts +2 -3
  17. package/dist/cjs/output.d.ts.map +1 -1
  18. package/dist/cjs/output.js +11 -26
  19. package/dist/cjs/safe.d.ts +1 -1
  20. package/dist/cjs/safe.d.ts.map +1 -1
  21. package/dist/cjs/safe.js +5 -5
  22. package/dist/cjs/selectors.d.ts +2 -2
  23. package/dist/cjs/selectors.d.ts.map +1 -1
  24. package/dist/cjs/stream.d.ts +1 -1
  25. package/dist/cjs/stream.d.ts.map +1 -1
  26. package/dist/cjs/stream.js +2 -2
  27. package/dist/esm/ClientOnly.d.ts +5 -0
  28. package/dist/esm/ClientOnly.d.ts.map +1 -0
  29. package/dist/esm/ClientOnly.js +9 -0
  30. package/dist/esm/convertImages.d.ts +3 -0
  31. package/dist/esm/convertImages.d.ts.map +1 -0
  32. package/dist/esm/convertImages.js +66 -0
  33. package/dist/esm/error.d.ts +1 -1
  34. package/dist/esm/error.d.ts.map +1 -1
  35. package/dist/esm/error.js +1 -1
  36. package/dist/esm/hooks.d.ts +3 -34
  37. package/dist/esm/hooks.d.ts.map +1 -1
  38. package/dist/esm/hooks.js +52 -15
  39. package/dist/esm/jupyter.d.ts +1 -1
  40. package/dist/esm/jupyter.d.ts.map +1 -1
  41. package/dist/esm/jupyter.js +7 -4
  42. package/dist/esm/output.d.ts +2 -3
  43. package/dist/esm/output.d.ts.map +1 -1
  44. package/dist/esm/output.js +5 -19
  45. package/dist/esm/safe.d.ts +1 -1
  46. package/dist/esm/safe.d.ts.map +1 -1
  47. package/dist/esm/safe.js +1 -1
  48. package/dist/esm/selectors.d.ts +2 -2
  49. package/dist/esm/selectors.d.ts.map +1 -1
  50. package/dist/esm/stream.d.ts +1 -1
  51. package/dist/esm/stream.d.ts.map +1 -1
  52. package/dist/esm/stream.js +1 -1
  53. package/dist/types/ClientOnly.d.ts +5 -0
  54. package/dist/types/ClientOnly.d.ts.map +1 -0
  55. package/dist/types/convertImages.d.ts +3 -0
  56. package/dist/types/convertImages.d.ts.map +1 -0
  57. package/dist/types/error.d.ts +1 -1
  58. package/dist/types/error.d.ts.map +1 -1
  59. package/dist/types/hooks.d.ts +3 -34
  60. package/dist/types/hooks.d.ts.map +1 -1
  61. package/dist/types/jupyter.d.ts +1 -1
  62. package/dist/types/jupyter.d.ts.map +1 -1
  63. package/dist/types/output.d.ts +2 -3
  64. package/dist/types/output.d.ts.map +1 -1
  65. package/dist/types/safe.d.ts +1 -1
  66. package/dist/types/safe.d.ts.map +1 -1
  67. package/dist/types/selectors.d.ts +2 -2
  68. package/dist/types/selectors.d.ts.map +1 -1
  69. package/dist/types/stream.d.ts +1 -1
  70. package/dist/types/stream.d.ts.map +1 -1
  71. package/package.json +9 -11
  72. package/dist/cjs/outputBlock.d.ts +0 -10
  73. package/dist/cjs/outputBlock.d.ts.map +0 -1
  74. package/dist/cjs/outputBlock.js +0 -13
  75. package/dist/esm/outputBlock.d.ts +0 -10
  76. package/dist/esm/outputBlock.d.ts.map +0 -1
  77. package/dist/esm/outputBlock.js +0 -6
  78. package/dist/types/outputBlock.d.ts +0 -10
  79. package/dist/types/outputBlock.d.ts.map +0 -1
@@ -0,0 +1,5 @@
1
+ import type { ReactNode } from 'react';
2
+ export default function ClientOnly({ children }: {
3
+ children: ReactNode;
4
+ }): JSX.Element | null;
5
+ //# sourceMappingURL=ClientOnly.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ClientOnly.d.ts","sourceRoot":"","sources":["../../src/ClientOnly.tsx"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,OAAO,CAAC;AAEvC,MAAM,CAAC,OAAO,UAAU,UAAU,CAAC,EAAE,QAAQ,EAAE,EAAE;IAAE,QAAQ,EAAE,SAAS,CAAA;CAAE,sBAMvE"}
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const jsx_runtime_1 = require("react/jsx-runtime");
4
+ const react_1 = require("react");
5
+ function ClientOnly({ children }) {
6
+ const [mounted, setMounted] = (0, react_1.useState)(false);
7
+ (0, react_1.useEffect)(() => {
8
+ setMounted(true);
9
+ }, []);
10
+ return mounted ? (0, jsx_runtime_1.jsx)(jsx_runtime_1.Fragment, { children: children }) : null;
11
+ }
12
+ exports.default = ClientOnly;
@@ -0,0 +1,3 @@
1
+ import type { MinifiedOutput } from 'nbtx';
2
+ export declare function fetchAndEncodeOutputImages(outputs: MinifiedOutput[]): Promise<MinifiedOutput[]>;
3
+ //# sourceMappingURL=convertImages.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"convertImages.d.ts","sourceRoot":"","sources":["../../src/convertImages.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAsB,cAAc,EAAE,MAAM,MAAM,CAAC;AA6B/D,wBAAsB,0BAA0B,CAC9C,OAAO,EAAE,cAAc,EAAE,GACxB,OAAO,CAAC,cAAc,EAAE,CAAC,CAmC3B"}
@@ -0,0 +1,70 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.fetchAndEncodeOutputImages = void 0;
13
+ function requestImageAsBase64String(src) {
14
+ return __awaiter(this, void 0, void 0, function* () {
15
+ const img = new Image();
16
+ img.crossOrigin = 'Anonymous';
17
+ const base64String = new Promise((resolve, reject) => {
18
+ img.onload = function ol() {
19
+ const canvas = document.createElement('canvas');
20
+ const ctx = canvas.getContext('2d');
21
+ if (!ctx) {
22
+ // eslint-disable-next-line no-console
23
+ console.error('Could not get canvas context');
24
+ return reject();
25
+ }
26
+ canvas.height = img.naturalHeight;
27
+ canvas.width = img.naturalWidth;
28
+ ctx.drawImage(img, 0, 0);
29
+ const dataURL = canvas.toDataURL('image/png');
30
+ const [, base64] = dataURL.split(';base64,');
31
+ resolve(base64);
32
+ };
33
+ // trigger the load
34
+ img.src = src;
35
+ });
36
+ return base64String;
37
+ });
38
+ }
39
+ function fetchAndEncodeOutputImages(outputs) {
40
+ return __awaiter(this, void 0, void 0, function* () {
41
+ return Promise.all(outputs.map((output) => __awaiter(this, void 0, void 0, function* () {
42
+ if (!('data' in output))
43
+ return output;
44
+ const imageMimetypes = Object.keys(output.data).filter((mimetype) => mimetype !== 'image/svg' && mimetype.startsWith('image/'));
45
+ if (imageMimetypes.length === 0)
46
+ return output;
47
+ // this is an async fetch, so we need to await the result
48
+ const images = yield Promise.all(imageMimetypes.map((mimetype) => __awaiter(this, void 0, void 0, function* () {
49
+ /*
50
+ image/* types can be either raw svg, base64 encoded or a URL.
51
+ base64 encoded images can include data:image/*;base64, or be naked
52
+ svgs can also be base64 encoded, or plain '<svg ...></svg>'.
53
+ URLs can be relative or absolute
54
+ */
55
+ const data = output.data[mimetype];
56
+ if (data.path) {
57
+ const base64 = yield requestImageAsBase64String(data.path);
58
+ return Object.assign(Object.assign({}, data), { content: base64 });
59
+ }
60
+ return data;
61
+ })));
62
+ imageMimetypes.forEach((mimetype, i) => {
63
+ // eslint-disable-next-line no-param-reassign
64
+ output.data[mimetype] = images[i];
65
+ });
66
+ return output;
67
+ })));
68
+ });
69
+ }
70
+ exports.fetchAndEncodeOutputImages = fetchAndEncodeOutputImages;
@@ -1,5 +1,5 @@
1
1
  /// <reference types="react" />
2
- import type { MinifiedErrorOutput } from '@curvenote/nbtx';
2
+ import type { MinifiedErrorOutput } from 'nbtx';
3
3
  export default function Error({ output }: {
4
4
  output: MinifiedErrorOutput;
5
5
  }): JSX.Element;
@@ -1 +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"}
1
+ {"version":3,"file":"error.d.ts","sourceRoot":"","sources":["../../src/error.tsx"],"names":[],"mappings":";AAEA,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,MAAM,CAAC;AAGhD,MAAM,CAAC,OAAO,UAAU,KAAK,CAAC,EAAE,MAAM,EAAE,EAAE;IAAE,MAAM,EAAE,mBAAmB,CAAA;CAAE,eAcxE"}
package/dist/cjs/error.js CHANGED
@@ -5,10 +5,10 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  const jsx_runtime_1 = require("react/jsx-runtime");
7
7
  const ansi_to_react_1 = __importDefault(require("ansi-to-react"));
8
- const blocks_1 = require("@curvenote/blocks");
8
+ const nbtx_1 = require("nbtx");
9
9
  const components_1 = require("./components");
10
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) => {
11
+ return ((0, jsx_runtime_1.jsx)(components_1.MaybeLongContent, { content: (0, nbtx_1.ensureString)(output.traceback), path: output.path, render: (content) => {
12
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
13
  } }));
14
14
  }
@@ -1,4 +1,4 @@
1
- import type { MinifiedOutput } from '@curvenote/nbtx/dist/minify/types';
1
+ import type { MinifiedOutput } from 'nbtx';
2
2
  /**
3
3
  * Truncation vs Summarization
4
4
  *
@@ -25,39 +25,8 @@ export declare function useLongContent(content?: string, url?: string): {
25
25
  error?: Error;
26
26
  };
27
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;
28
+ data: MinifiedOutput[] | undefined;
29
+ error: any | undefined;
61
30
  };
62
31
  export default function useWindowSize(): {
63
32
  width: number;
@@ -1 +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"}
1
+ {"version":3,"file":"hooks.d.ts","sourceRoot":"","sources":["../../src/hooks.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAIV,cAAc,EAEf,MAAM,MAAM,CAAC;AAId;;;;;;;;;;;;;;;;GAgBG;AAEH,UAAU,WAAW;IACnB,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,OAAO,EAAE,MAAM,CAAC;CACjB;AA0BD,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,GAAG;IACtE,IAAI,EAAE,cAAc,EAAE,GAAG,SAAS,CAAC;IACnC,KAAK,EAAE,GAAG,GAAG,SAAS,CAAC;CACxB,CAqCA;AAUD,MAAM,CAAC,OAAO,UAAU,aAAa;;;EAapC"}
package/dist/cjs/hooks.js CHANGED
@@ -1,17 +1,47 @@
1
1
  "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
2
11
  var __importDefault = (this && this.__importDefault) || function (mod) {
3
12
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
13
  };
5
14
  Object.defineProperty(exports, "__esModule", { value: true });
6
15
  exports.useFetchAnyTruncatedContent = exports.useLongContent = void 0;
7
16
  const immutable_1 = __importDefault(require("swr/immutable"));
8
- const utils_1 = require("@curvenote/nbtx/dist/minify/utils");
17
+ const nbtx_1 = require("nbtx");
9
18
  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
- });
19
+ function fetcher(url) {
20
+ return __awaiter(this, void 0, void 0, function* () {
21
+ const resp = yield fetch(url);
22
+ if (resp.status === 200) {
23
+ const content = yield resp.text();
24
+ if (url.endsWith('.json')) {
25
+ // This is for backward compatibility, previously we saved as:
26
+ // { content: string, content_type: string }
27
+ // This is now directly saved as the content (e.g. an image, json, html, text, etc.)
28
+ try {
29
+ const data = JSON.parse(content);
30
+ const keys = Object.keys(data);
31
+ if (keys.length === 2 && keys.includes('content') && keys.includes('content_type')) {
32
+ return data;
33
+ }
34
+ // pass
35
+ }
36
+ catch (error) {
37
+ // pass
38
+ }
39
+ }
40
+ return { content };
41
+ }
42
+ throw new Error(`Content returned with status ${resp.status}.`);
43
+ });
44
+ }
15
45
  function useLongContent(content, url) {
16
46
  if (typeof document === 'undefined') {
17
47
  // This is ONLY called on the server
@@ -40,22 +70,29 @@ function shallowCloneOutputs(outputs) {
40
70
  function useFetchAnyTruncatedContent(outputs) {
41
71
  const itemsWithPaths = [];
42
72
  const updated = shallowCloneOutputs(outputs);
43
- (0, utils_1.walkPaths)(updated, (path, obj) => {
73
+ (0, nbtx_1.walkOutputs)(updated, (obj) => {
44
74
  // images have paths, but we don't need to fetch them
45
75
  if ('content_type' in obj && obj.content_type.startsWith('image/'))
46
76
  return;
47
- obj.path = path;
48
- itemsWithPaths.push(obj);
77
+ if (obj.path) {
78
+ itemsWithPaths.push(obj);
79
+ }
49
80
  });
50
81
  const { data, error } = (0, immutable_1.default)(itemsWithPaths.map(({ path }) => path), arrayFetcher);
51
82
  data === null || data === void 0 ? void 0 : data.forEach(({ content }, idx) => {
52
83
  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
84
+ if ('text' in obj) {
85
+ // stream
86
+ obj.text = content;
87
+ }
88
+ else if ('traceback' in obj) {
89
+ // error
90
+ obj.traceback = content;
91
+ }
92
+ else {
93
+ // mimeoutput
94
+ obj.content = content;
95
+ }
59
96
  obj.path = undefined;
60
97
  });
61
98
  return {
@@ -1,5 +1,5 @@
1
1
  /// <reference types="react" />
2
- import type { MinifiedOutput } from '@curvenote/nbtx';
2
+ import type { MinifiedOutput } from 'nbtx';
3
3
  export declare const NativeJupyterOutputs: ({ id, outputs, }: {
4
4
  id: string;
5
5
  outputs: MinifiedOutput[];
@@ -1 +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"}
1
+ {"version":3,"file":"jupyter.d.ts","sourceRoot":"","sources":["../../src/jupyter.tsx"],"names":[],"mappings":";AAKA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,MAAM,CAAC;AAS3C,eAAO,MAAM,oBAAoB;QAI3B,MAAM;aACD,cAAc,EAAE;iBAoE1B,CAAC"}
@@ -30,25 +30,28 @@ const hooks_1 = __importStar(require("./hooks"));
30
30
  const nanoid_1 = require("nanoid");
31
31
  const react_redux_1 = require("react-redux");
32
32
  const connect_1 = require("@curvenote/connect");
33
- const nbtx_1 = require("@curvenote/nbtx");
33
+ const nbtx_1 = require("nbtx");
34
34
  const outline_1 = require("@heroicons/react/24/outline");
35
+ const convertImages_1 = require("./convertImages");
35
36
  const selectors_1 = require("./selectors");
36
37
  const PERCENT_OF_WINDOW = 0.9;
37
38
  const NativeJupyterOutputs = ({ id, outputs, }) => {
39
+ var _a;
38
40
  const windowSize = (0, hooks_1.default)();
39
41
  const { data, error } = (0, hooks_1.useFetchAnyTruncatedContent)(outputs);
40
42
  const [loading, setLoading] = (0, react_1.useState)(true);
41
43
  const [frameHeight, setFrameHeight] = (0, react_1.useState)(0);
42
44
  const [clamped, setClamped] = (0, react_1.useState)(false);
43
45
  const uid = (0, react_1.useMemo)(nanoid_1.nanoid, []);
44
- const height = (0, react_redux_1.useSelector)((state) => (0, selectors_1.selectIFrameHeight)(state, uid));
46
+ const { height } = (_a = (0, react_redux_1.useSelector)((state) => (0, selectors_1.selectIFrameHeight)(state, uid))) !== null && _a !== void 0 ? _a : {};
45
47
  const rendererReady = (0, react_redux_1.useSelector)((state) => (0, selectors_1.selectIFrameReady)(state, uid));
46
48
  const iframeRef = (0, react_1.useRef)(null);
47
49
  (0, react_1.useEffect)(() => {
48
50
  if (iframeRef.current == null || !rendererReady || !data)
49
51
  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
+ (0, convertImages_1.fetchAndEncodeOutputImages)(data).then((out) => {
53
+ const compactOutputs = (0, nbtx_1.convertToIOutputs)(out, {});
54
+ connect_1.host.commsDispatch(iframeRef.current, connect_1.actions.connectHostSendContent(uid, compactOutputs));
52
55
  });
53
56
  }, [id, iframeRef.current, rendererReady]);
54
57
  (0, react_1.useEffect)(() => {
@@ -1,7 +1,6 @@
1
1
  /// <reference types="react" />
2
- import type { GenericNode } from 'mystjs';
3
- import type { MinifiedOutput } from '@curvenote/nbtx/dist/minify/types';
2
+ import type { GenericNode } from 'myst-common';
3
+ import type { MinifiedOutput } from 'nbtx';
4
4
  export declare function allOutputsAreSafe(outputs: MinifiedOutput[], directOutputTypes: Set<string>, directMimeTypes: Set<string>): boolean;
5
- export declare function anyErrors(outputs: MinifiedOutput[]): boolean;
6
5
  export declare function Output(node: GenericNode): JSX.Element;
7
6
  //# sourceMappingURL=output.d.ts.map
@@ -1 +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"}
1
+ {"version":3,"file":"output.d.ts","sourceRoot":"","sources":["../../src/output.tsx"],"names":[],"mappings":";AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAE/C,OAAO,KAAK,EAAsB,cAAc,EAAE,MAAM,MAAM,CAAC;AAgB/D,wBAAgB,iBAAiB,CAC/B,OAAO,EAAE,cAAc,EAAE,EACzB,iBAAiB,EAAE,GAAG,CAAC,MAAM,CAAC,EAC9B,eAAe,EAAE,GAAG,CAAC,MAAM,CAAC,WAY7B;AAED,wBAAgB,MAAM,CAAC,IAAI,EAAE,WAAW,eA6BvC"}
@@ -3,20 +3,20 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.Output = exports.anyErrors = exports.allOutputsAreSafe = void 0;
6
+ exports.Output = exports.allOutputsAreSafe = void 0;
7
7
  const jsx_runtime_1 = require("react/jsx-runtime");
8
- const blocks_1 = require("@curvenote/blocks");
8
+ const nbtx_1 = require("nbtx");
9
9
  const classnames_1 = __importDefault(require("classnames"));
10
10
  const safe_1 = require("./safe");
11
11
  const jupyter_1 = require("./jupyter");
12
- const outputBlock_1 = require("./outputBlock");
12
+ const ClientOnly_1 = __importDefault(require("./ClientOnly"));
13
13
  const DIRECT_OUTPUT_TYPES = new Set(['stream', 'error']);
14
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,
15
+ nbtx_1.KnownCellOutputMimeTypes.TextPlain,
16
+ nbtx_1.KnownCellOutputMimeTypes.ImagePng,
17
+ nbtx_1.KnownCellOutputMimeTypes.ImageGif,
18
+ nbtx_1.KnownCellOutputMimeTypes.ImageJpeg,
19
+ nbtx_1.KnownCellOutputMimeTypes.ImageBmp,
20
20
  ]);
21
21
  function allOutputsAreSafe(outputs, directOutputTypes, directMimeTypes) {
22
22
  return outputs.reduce((flag, output) => {
@@ -31,36 +31,21 @@ function allOutputsAreSafe(outputs, directOutputTypes, directMimeTypes) {
31
31
  }, true);
32
32
  }
33
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
34
  function Output(node) {
48
35
  const outputs = node.data;
49
36
  const allSafe = allOutputsAreSafe(outputs, DIRECT_OUTPUT_TYPES, DIRECT_MIME_TYPES);
50
- const hasError = anyErrors(outputs);
51
37
  let component;
52
38
  if (allSafe) {
53
39
  component = (0, jsx_runtime_1.jsx)(safe_1.SafeOutputs, { keyStub: node.key, outputs: outputs });
54
40
  }
55
41
  else {
56
42
  // 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 });
43
+ component = ((0, jsx_runtime_1.jsx)(ClientOnly_1.default, { children: (0, jsx_runtime_1.jsx)(jupyter_1.NativeJupyterOutputs, { id: node.key, outputs: outputs }) }));
59
44
  }
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', {
45
+ return ((0, jsx_runtime_1.jsx)("figure", Object.assign({ id: node.identifier || undefined, className: (0, classnames_1.default)('max-w-full overflow-auto m-0 group not-prose relative', {
61
46
  'text-left': !node.align || node.align === 'left',
62
47
  'text-center': node.align === 'center',
63
48
  '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));
49
+ }) }, { children: component }), node.key));
65
50
  }
66
51
  exports.Output = Output;
@@ -1,5 +1,5 @@
1
1
  /// <reference types="react" />
2
- import type { MinifiedOutput } from '@curvenote/nbtx/dist/minify/types';
2
+ import type { MinifiedOutput } from 'nbtx';
3
3
  export declare function SafeOutputs({ keyStub, outputs }: {
4
4
  keyStub: string;
5
5
  outputs: MinifiedOutput[];
@@ -1 +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"}
1
+ {"version":3,"file":"safe.d.ts","sourceRoot":"","sources":["../../src/safe.tsx"],"names":[],"mappings":";AACA,OAAO,KAAK,EAA2C,cAAc,EAAE,MAAM,MAAM,CAAC;AAwEpF,wBAAgB,WAAW,CAAC,EAAE,OAAO,EAAE,OAAO,EAAE,EAAE;IAAE,OAAO,EAAE,MAAM,CAAC;IAAC,OAAO,EAAE,cAAc,EAAE,CAAA;CAAE,eAM/F"}
package/dist/cjs/safe.js CHANGED
@@ -5,7 +5,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.SafeOutputs = void 0;
7
7
  const jsx_runtime_1 = require("react/jsx-runtime");
8
- const jupyter_1 = require("@curvenote/blocks/dist/blocks/types/jupyter");
8
+ const nbtx_1 = require("nbtx");
9
9
  const stream_1 = __importDefault(require("./stream"));
10
10
  const error_1 = __importDefault(require("./error"));
11
11
  const ansi_to_react_1 = __importDefault(require("ansi-to-react"));
@@ -25,10 +25,10 @@ const ansi_to_react_1 = __importDefault(require("ansi-to-react"));
25
25
  - "text/plain"
26
26
  */
27
27
  const RENDER_PRIORITY = [
28
- jupyter_1.KnownCellOutputMimeTypes.ImagePng,
29
- jupyter_1.KnownCellOutputMimeTypes.ImageJpeg,
30
- jupyter_1.KnownCellOutputMimeTypes.ImageGif,
31
- jupyter_1.KnownCellOutputMimeTypes.ImageBmp,
28
+ nbtx_1.KnownCellOutputMimeTypes.ImagePng,
29
+ nbtx_1.KnownCellOutputMimeTypes.ImageJpeg,
30
+ nbtx_1.KnownCellOutputMimeTypes.ImageGif,
31
+ nbtx_1.KnownCellOutputMimeTypes.ImageBmp,
32
32
  ];
33
33
  function findSafeMimeOutputs(output) {
34
34
  const data = output.data;
@@ -2,8 +2,8 @@ import type { HostState } from '@curvenote/connect';
2
2
  export interface State {
3
3
  app: HostState;
4
4
  }
5
- export declare const selectIFrameHeight: (state: State, id: string) => number | null;
6
- export declare const selectIFrameReady: (state: State, id: string) => boolean;
5
+ export declare const selectIFrameHeight: (state: State, id: string) => Pick<import("@curvenote/connect").HostStateItem, "height" | "width"> | undefined;
6
+ export declare const selectIFrameReady: (state: State, id: string) => Boolean;
7
7
  export declare const selectIFrameSendFailed: (state: State, id: string) => false | {
8
8
  message: string | undefined;
9
9
  };
@@ -1 +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"}
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,qFACX,CAAC;AAEjD,eAAO,MAAM,iBAAiB,UAAW,KAAK,MAAM,MAAM,YACT,CAAC;AAElD,eAAO,MAAM,sBAAsB,UAAW,KAAK,MAAM,MAAM;;CACb,CAAC"}
@@ -1,5 +1,5 @@
1
1
  /// <reference types="react" />
2
- import type { MinifiedStreamOutput } from '@curvenote/nbtx';
2
+ import type { MinifiedStreamOutput } from 'nbtx';
3
3
  export default function Stream({ output }: {
4
4
  output: MinifiedStreamOutput;
5
5
  }): JSX.Element;
@@ -1 +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"}
1
+ {"version":3,"file":"stream.d.ts","sourceRoot":"","sources":["../../src/stream.tsx"],"names":[],"mappings":";AAEA,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,MAAM,CAAC;AAGjD,MAAM,CAAC,OAAO,UAAU,MAAM,CAAC,EAAE,MAAM,EAAE,EAAE;IAAE,MAAM,EAAE,oBAAoB,CAAA;CAAE,eAY1E"}
@@ -5,9 +5,9 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  const jsx_runtime_1 = require("react/jsx-runtime");
7
7
  const ansi_to_react_1 = __importDefault(require("ansi-to-react"));
8
- const blocks_1 = require("@curvenote/blocks");
8
+ const nbtx_1 = require("nbtx");
9
9
  const components_1 = require("./components");
10
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 : '' }) }))) }));
11
+ return ((0, jsx_runtime_1.jsx)(components_1.MaybeLongContent, { content: (0, nbtx_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
12
  }
13
13
  exports.default = Stream;
@@ -0,0 +1,5 @@
1
+ import type { ReactNode } from 'react';
2
+ export default function ClientOnly({ children }: {
3
+ children: ReactNode;
4
+ }): JSX.Element | null;
5
+ //# sourceMappingURL=ClientOnly.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ClientOnly.d.ts","sourceRoot":"","sources":["../../src/ClientOnly.tsx"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,OAAO,CAAC;AAEvC,MAAM,CAAC,OAAO,UAAU,UAAU,CAAC,EAAE,QAAQ,EAAE,EAAE;IAAE,QAAQ,EAAE,SAAS,CAAA;CAAE,sBAMvE"}
@@ -0,0 +1,9 @@
1
+ import { Fragment as _Fragment, jsx as _jsx } from "react/jsx-runtime";
2
+ import { useEffect, useState } from 'react';
3
+ export default function ClientOnly({ children }) {
4
+ const [mounted, setMounted] = useState(false);
5
+ useEffect(() => {
6
+ setMounted(true);
7
+ }, []);
8
+ return mounted ? _jsx(_Fragment, { children: children }) : null;
9
+ }
@@ -0,0 +1,3 @@
1
+ import type { MinifiedOutput } from 'nbtx';
2
+ export declare function fetchAndEncodeOutputImages(outputs: MinifiedOutput[]): Promise<MinifiedOutput[]>;
3
+ //# sourceMappingURL=convertImages.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"convertImages.d.ts","sourceRoot":"","sources":["../../src/convertImages.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAsB,cAAc,EAAE,MAAM,MAAM,CAAC;AA6B/D,wBAAsB,0BAA0B,CAC9C,OAAO,EAAE,cAAc,EAAE,GACxB,OAAO,CAAC,cAAc,EAAE,CAAC,CAmC3B"}
@@ -0,0 +1,66 @@
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
10
+ function requestImageAsBase64String(src) {
11
+ return __awaiter(this, void 0, void 0, function* () {
12
+ const img = new Image();
13
+ img.crossOrigin = 'Anonymous';
14
+ const base64String = new Promise((resolve, reject) => {
15
+ img.onload = function ol() {
16
+ const canvas = document.createElement('canvas');
17
+ const ctx = canvas.getContext('2d');
18
+ if (!ctx) {
19
+ // eslint-disable-next-line no-console
20
+ console.error('Could not get canvas context');
21
+ return reject();
22
+ }
23
+ canvas.height = img.naturalHeight;
24
+ canvas.width = img.naturalWidth;
25
+ ctx.drawImage(img, 0, 0);
26
+ const dataURL = canvas.toDataURL('image/png');
27
+ const [, base64] = dataURL.split(';base64,');
28
+ resolve(base64);
29
+ };
30
+ // trigger the load
31
+ img.src = src;
32
+ });
33
+ return base64String;
34
+ });
35
+ }
36
+ export function fetchAndEncodeOutputImages(outputs) {
37
+ return __awaiter(this, void 0, void 0, function* () {
38
+ return Promise.all(outputs.map((output) => __awaiter(this, void 0, void 0, function* () {
39
+ if (!('data' in output))
40
+ return output;
41
+ const imageMimetypes = Object.keys(output.data).filter((mimetype) => mimetype !== 'image/svg' && mimetype.startsWith('image/'));
42
+ if (imageMimetypes.length === 0)
43
+ return output;
44
+ // this is an async fetch, so we need to await the result
45
+ const images = yield Promise.all(imageMimetypes.map((mimetype) => __awaiter(this, void 0, void 0, function* () {
46
+ /*
47
+ image/* types can be either raw svg, base64 encoded or a URL.
48
+ base64 encoded images can include data:image/*;base64, or be naked
49
+ svgs can also be base64 encoded, or plain '<svg ...></svg>'.
50
+ URLs can be relative or absolute
51
+ */
52
+ const data = output.data[mimetype];
53
+ if (data.path) {
54
+ const base64 = yield requestImageAsBase64String(data.path);
55
+ return Object.assign(Object.assign({}, data), { content: base64 });
56
+ }
57
+ return data;
58
+ })));
59
+ imageMimetypes.forEach((mimetype, i) => {
60
+ // eslint-disable-next-line no-param-reassign
61
+ output.data[mimetype] = images[i];
62
+ });
63
+ return output;
64
+ })));
65
+ });
66
+ }
@@ -1,5 +1,5 @@
1
1
  /// <reference types="react" />
2
- import type { MinifiedErrorOutput } from '@curvenote/nbtx';
2
+ import type { MinifiedErrorOutput } from 'nbtx';
3
3
  export default function Error({ output }: {
4
4
  output: MinifiedErrorOutput;
5
5
  }): JSX.Element;
@@ -1 +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"}
1
+ {"version":3,"file":"error.d.ts","sourceRoot":"","sources":["../../src/error.tsx"],"names":[],"mappings":";AAEA,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,MAAM,CAAC;AAGhD,MAAM,CAAC,OAAO,UAAU,KAAK,CAAC,EAAE,MAAM,EAAE,EAAE;IAAE,MAAM,EAAE,mBAAmB,CAAA;CAAE,eAcxE"}
package/dist/esm/error.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import { jsx as _jsx } from "react/jsx-runtime";
2
2
  import Ansi from 'ansi-to-react';
3
- import { ensureString } from '@curvenote/blocks';
3
+ import { ensureString } from 'nbtx';
4
4
  import { MaybeLongContent } from './components';
5
5
  export default function Error({ output }) {
6
6
  return (_jsx(MaybeLongContent, { content: ensureString(output.traceback), path: output.path, render: (content) => {