Skip to content

Commit fd95ae8

Browse files
authored
lexical: jupyter cell (#359)
* bump: icons * lexical: code * fix: lexical code * bump: yjs * lexical: sparkle * bump: version
1 parent 2d5db31 commit fd95ae8

File tree

22 files changed

+168
-55
lines changed

22 files changed

+168
-55
lines changed

attic/slate/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
"kill": "./../dev/sh/kill.sh || true"
2424
},
2525
"dependencies": {
26-
"@datalayer/icons-react": "^0.3.4",
26+
"@datalayer/icons-react": "^1.0.0",
2727
"@datalayer/jupyter-react": "^0.18.12",
2828
"@datalayer/primer-addons": "^0.3.2",
2929
"@emotion/css": "^11.1.3",

examples/cra/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"eject": "react-scripts eject"
1313
},
1414
"dependencies": {
15-
"@datalayer/icons-react": "^0.3.4",
15+
"@datalayer/icons-react": "^1.0.0",
1616
"@datalayer/jupyter-react": "^0.18.12",
1717
"@datalayer/primer-addons": "^0.3.2",
1818
"jupyterlab-plotly": "^5.17.0",

examples/next-js/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"lint": "next lint"
1111
},
1212
"dependencies": {
13-
"@datalayer/icons-react": "^0.3.4",
13+
"@datalayer/icons-react": "^1.0.0",
1414
"@datalayer/jupyter-react": "^0.18.12",
1515
"@datalayer/primer-addons": "^0.3.2",
1616
"autoprefixer": "^10.4.14",

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@
203203
"webpack-cli": "4.10.0",
204204
"webpack-dev-server": "4.9.3",
205205
"y-websocket": "1.4.5",
206-
"yjs": "13.5.47"
206+
"yjs": "13.6.24"
207207
},
208208
"overrides": {
209209
"@jupyter-widgets/base": "6.0.6",
@@ -314,6 +314,6 @@
314314
"webpack-cli": "4.10.0",
315315
"webpack-dev-server": "4.9.3",
316316
"y-websocket": "1.4.5",
317-
"yjs": "13.5.47"
317+
"yjs": "13.6.24"
318318
}
319319
}

packages/docusaurus-plugin/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
"directory": "packages/docusaurus-plugin"
2323
},
2424
"dependencies": {
25-
"@datalayer/icons-react": "^0.3.4",
25+
"@datalayer/icons-react": "^1.0.0",
2626
"@datalayer/jupyter-react": "^0.17.0",
2727
"@datalayer/primer-addons": "^0.3.2",
2828
"@docusaurus/core": "^2.4.0",

packages/lexical/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@datalayer/jupyter-lexical",
3-
"version": "0.1.2",
3+
"version": "0.2.0",
44
"description": "Jupyter UI for Lexical",
55
"license": "MIT",
66
"main": "lib/index.js",
@@ -60,7 +60,7 @@
6060
},
6161
"dependencies": {
6262
"@datalayer/icons-react": "^0.3.7",
63-
"@datalayer/jupyter-react": "^0.19.9",
63+
"@datalayer/jupyter-react": "^0.20.0",
6464
"@datalayer/primer-addons": "^0.3.2",
6565
"@jupyterlab/application": "^4.0.0",
6666
"@jupyterlab/coreutils": "^6.0.0",

packages/lexical/src/components/JupyterCellComponent.tsx

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,24 @@
44
* MIT License
55
*/
66

7+
import { IOutput } from '@jupyterlab/nbformat';
78
import { Cell } from '@datalayer/jupyter-react';
89

9-
export const JupyterCellComponent = (props: any) => {
10+
type IJupyterCellComponentProps = {
11+
code: string;
12+
outputs: IOutput[];
13+
loading: string;
14+
autoStart: boolean;
15+
}
16+
17+
export const JupyterCellComponent = (props: IJupyterCellComponentProps) => {
18+
const { autoStart, code, outputs } = props;
1019
return (
1120
<Cell
1221
// startDefaultKernel
13-
source="print('Hello Jupyter React')"
14-
autoStart
22+
source={code}
23+
autoStart={autoStart}
24+
outputs={outputs}
1525
/>
1626
)
1727
}

packages/lexical/src/convert/NbformatToLexical.tsx

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,31 @@
66

77
import { LexicalEditor, INSERT_PARAGRAPH_COMMAND } from "lexical";
88
import { INotebookContent, IOutput } from "@jupyterlab/nbformat";
9-
import { INSERT_JUPYTER_COMMAND } from "../plugins/JupyterPlugin";
9+
// import { INSERT_JUPYTER_CELL_OUTPUT_COMMAND } from "../plugins/JupyterCellOutputPlugin";
10+
import { INSERT_JUPYTER_CELL_COMMAND } from "../plugins/JupyterCellPlugin";
1011
import { $convertFromMarkdownString, TRANSFORMERS } from "./markdown";
1112

1213
export const nbformatToLexical = (notebook: INotebookContent, editor: LexicalEditor) => {
1314
editor.update(() => {
1415
notebook.cells.map(cell => {
15-
let source = '';
16+
let code = '';
1617
if (typeof cell.source === 'object') {
17-
source = (cell.source as string[]).join('\n');
18+
code = (cell.source as string[]).join('\n');
1819
}
1920
if (typeof cell.source === 'string') {
20-
source = cell.source as string;
21+
code = cell.source as string;
2122
}
2223
if (cell.cell_type === 'markdown') {
23-
$convertFromMarkdownString(source, TRANSFORMERS);
24+
$convertFromMarkdownString(code, TRANSFORMERS);
2425
}
2526
if (cell.cell_type === 'code') {
2627
const outputs = cell.outputs as IOutput[];
27-
editor.dispatchCommand(INSERT_JUPYTER_COMMAND, { code: source, outputs: outputs, loading: "initial" })
28+
editor.dispatchCommand(INSERT_JUPYTER_CELL_COMMAND, {
29+
code,
30+
outputs,
31+
loading: "Loading...",
32+
autoStart: false
33+
});
2834
}
2935
editor.dispatchCommand(INSERT_PARAGRAPH_COMMAND, undefined);
3036
});

packages/lexical/src/editor/Editor.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import { AutoLinkNode, LinkNode } from "@lexical/link";
2828
import { CodeNode } from '@lexical/code';
2929
import { INotebookContent } from "@jupyterlab/nbformat";
3030
import {
31-
JupyterPlugin, EquationNode, HorizontalRulePlugin,
31+
JupyterCellOutputPlugin, EquationNode, HorizontalRulePlugin,
3232
ListMaxIndentLevelPlugin, AutoLinkPlugin, ComponentPickerMenuPlugin,
3333
EquationsPlugin, ImagesPlugin, YouTubePlugin, ImageNode, YouTubeNode,
3434
JupyterCodeHighlightNode, JupyterCodeNode, JupyterOutputNode, JupyterCellNode,
@@ -132,7 +132,7 @@ export function Editor(props: Props) {
132132
<ListMaxIndentLevelPlugin maxDepth={7} />
133133
<MarkdownPlugin />
134134
<JupyterCellPlugin />
135-
<JupyterPlugin />
135+
<JupyterCellOutputPlugin />
136136
<ComponentPickerMenuPlugin/>
137137
<EquationsPlugin/>
138138
<ImagesPlugin/>

packages/lexical/src/nodes/JupyterCellNode.tsx

Lines changed: 67 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,37 @@
66

77
import { ReactNode } from "react";
88
import { DecoratorNode, EditorConfig, LexicalEditor, LexicalNode, NodeKey, SerializedLexicalNode } from "lexical";
9+
import { IOutput } from "@jupyterlab/nbformat";
10+
import { JupyterCellProps } from "./../plugins/JupyterCellPlugin";
911
import JupyterCellNodeComponent from "./JupyterCellNodeComponent";
1012

13+
const TYPE = "jupyter-cell";
14+
1115
export class JupyterCellNode extends DecoratorNode<ReactNode> {
16+
private __code: string;
17+
private __outputs: IOutput[];
18+
private __loading: string;
19+
private __autoStart: boolean;
1220
private __data: any;
1321

1422
/** @override */
1523
static getType() {
16-
return "custom";
24+
return TYPE;
1725
}
1826

1927
/** @override */
2028
static clone(node: JupyterCellNode) {
21-
console.log(`clone: node.__data: ${node.__data} node.__key: ${node.__key} node: ${JSON.stringify(node, null, 2)}`);
22-
return new JupyterCellNode(node.__data, node.__key);
29+
console.debug(`clone: node: ${JSON.stringify(node, null, 2)}`);
30+
return new JupyterCellNode(node.__code, node.__outputs, node.__loading, node.__autoStart, node.__data, node.__key);
2331
}
2432

2533
/** @override */
26-
constructor(data = "[]", key?: NodeKey) {
34+
constructor(code: string, outputs: IOutput[], loading: string, autoStart: boolean, data = "[]", key?: NodeKey) {
2735
super(key);
36+
this.__code = code;
37+
this.__outputs = outputs;
38+
this.__loading = loading;
39+
this.__autoStart = autoStart;
2840
this.__data = data;
2941
}
3042

@@ -47,38 +59,82 @@ export class JupyterCellNode extends DecoratorNode<ReactNode> {
4759

4860
/** @override */
4961
decorate(editor: LexicalEditor) {
50-
console.log(`decorate -> this.getKey(): ${this.getKey()} this.__data: ${this.__data}`);
51-
return <JupyterCellNodeComponent nodeKey={this.getKey()} data={this.__data} />;
62+
console.log(`decorate -> key: ${this.getKey()} outputs: ${this.__outputs} data: ${this.__data}`);
63+
return <JupyterCellNodeComponent
64+
nodeKey={this.getKey()}
65+
code={this.__code}
66+
outputs={this.__outputs}
67+
loading={this.__loading}
68+
autoStart={this.__autoStart}
69+
data={this.__data}
70+
/>
71+
}
72+
73+
setCode(code: string) {
74+
const self = this.getWritable();
75+
self.__code = code;
76+
}
77+
get code() {
78+
return this.__code;
79+
}
80+
81+
setOutputs(outputs: IOutput[]) {
82+
const self = this.getWritable();
83+
self.__outputs = outputs;
84+
}
85+
get outputs() {
86+
return this.__outputs;
87+
}
88+
89+
setLoading(loading: string) {
90+
const self = this.getWritable();
91+
self.__loading = loading;
92+
}
93+
get loading() {
94+
return this.__loading;
95+
}
96+
97+
setAutostart(autoStart: boolean) {
98+
const self = this.getWritable();
99+
self.__autoStart = autoStart;
100+
}
101+
get autoStart() {
102+
return this.__autoStart;
52103
}
53104

54105
/** @override */
55106
setData(data: any) {
56107
const self = this.getWritable();
57108
self.__data = data;
58109
}
59-
60110
get data() {
61111
return this.__data;
62112
}
63113

64114
/** @override */
65115
static importJSON(serializedNode: SerializedLexicalNode) {
66-
return new JupyterCellNode((serializedNode as unknown as JupyterCellNode).data);
116+
const n = serializedNode as unknown as JupyterCellNode;
117+
return new JupyterCellNode(n.code, n.outputs, n.loading, n.autoStart, n.data);
67118
}
68119

69120
/** @override */
70121
exportJSON() {
71122
return {
123+
code: this.__code,
124+
outputs: this.__outputs,
125+
loading: this.__loading,
126+
autoStart: this.__autoStart,
72127
data: this.__data,
73-
type: 'custom',
128+
type: TYPE,
74129
version: 1,
75130
};
76131
}
77132

78133
}
79134

80-
export function $createJupyterCellNode() {
81-
return new JupyterCellNode();
135+
export function $createJupyterCellNode(props: JupyterCellProps) {
136+
const { code, outputs, loading, autoStart } = props;
137+
return new JupyterCellNode(code, outputs, loading, autoStart);
82138
}
83139

84140
export function $isJupyterCellNode(node: LexicalNode) {

0 commit comments

Comments
 (0)