Skip to content

Commit f9ecd94

Browse files
authored
fix: notebook state (#458)
* fix: notebook state * fix: build
1 parent 3152247 commit f9ecd94

File tree

14 files changed

+360
-78
lines changed

14 files changed

+360
-78
lines changed

examples/lexical/src/AppNbformat.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,21 +93,21 @@ const Tabs = () => {
9393
<UnderlineNav.Item
9494
href=""
9595
aria-current={tab === 'editor' ? 'page' : undefined}
96-
// onClick={(e: any) => goToTab(e, 'editor', notebook?.model)}
96+
onClick={(e: any) => goToTab(e, 'editor', notebook?.model)}
9797
>
9898
Editor
9999
</UnderlineNav.Item>
100100
<UnderlineNav.Item
101101
href=""
102102
aria-current={tab === 'notebook' ? 'page' : undefined}
103-
// onClick={(e: any) => goToTab(e, 'notebook', notebook?.model)}
103+
onClick={(e: any) => goToTab(e, 'notebook', notebook?.model)}
104104
>
105105
Notebook
106106
</UnderlineNav.Item>
107107
<UnderlineNav.Item
108108
href=""
109109
aria-current={tab === 'nbformat' ? 'page' : undefined}
110-
// onClick={(e: any) => goToTab(e, 'nbformat', notebook?.model)}
110+
onClick={(e: any) => goToTab(e, 'nbformat', notebook?.model)}
111111
>
112112
NbFormat
113113
</UnderlineNav.Item>

packages/lexical/src/examples/AppNbformat.tsx

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
*/
66

77
import { useState, useMemo } from 'react';
8-
// import { $getRoot } from 'lexical';
8+
import { $getRoot } from 'lexical';
99
import styled from 'styled-components';
1010
import {
11-
// useNotebookStore,
11+
useNotebookStore,
1212
useJupyter,
1313
JupyterReactTheme,
1414
Notebook,
@@ -20,8 +20,8 @@ import { UnderlineNav, Button } from '@primer/react';
2020
import { ThreeBarsIcon } from '@primer/octicons-react';
2121
import { JSONTree } from 'react-json-tree';
2222
import { INotebookContent } from '@jupyterlab/nbformat';
23-
// import { INotebookModel } from '@jupyterlab/notebook';
24-
// import { lexicalToNbformat } from './..';
23+
import { INotebookModel } from '@jupyterlab/notebook';
24+
import { lexicalToNbformat } from './..';
2525
import { useLexical, LexicalProvider, Editor } from './..';
2626

2727
import INITIAL_LEXICAL_MODEL from './content/Example.lexical.json';
@@ -43,16 +43,15 @@ const Tabs = () => {
4343
const { serviceManager, defaultKernel } = useJupyter({
4444
startDefaultKernel: true,
4545
});
46-
// const notebookStore = useNotebookStore();
47-
const [tab, _] = useState<TabType>('editor');
46+
const notebookStore = useNotebookStore();
47+
const [tab, setTab] = useState<TabType>('editor');
4848
const [nbformat, setNbformat] = useState<INotebookContent>(
4949
INITIAL_NBFORMAT_MODEL,
5050
);
5151
const extensions = useMemo(
5252
() => [new CellSidebarExtension({ factory: CellSidebar })],
5353
[],
5454
);
55-
/*
5655
const notebook = notebookStore.selectNotebook(NOTEBOOK_UID);
5756
const goToTab = (
5857
e: any,
@@ -80,28 +79,27 @@ const Tabs = () => {
8079
}
8180
setTab(toTab);
8281
};
83-
*/
8482
return (
8583
<Box className="center">
8684
<UnderlineNav aria-label="Underline Navigation">
8785
<UnderlineNav.Item
8886
href=""
8987
aria-current={tab === 'editor' ? 'page' : undefined}
90-
// onClick={e => goToTab(e, 'editor', notebook?.model)}
88+
onClick={e => goToTab(e, 'editor', notebook?.model)}
9189
>
9290
Editor
9391
</UnderlineNav.Item>
9492
<UnderlineNav.Item
9593
href=""
9694
aria-current={tab === 'notebook' ? 'page' : undefined}
97-
// onClick={e => goToTab(e, 'notebook', notebook?.model)}
95+
onClick={e => goToTab(e, 'notebook', notebook?.model)}
9896
>
9997
Notebook
10098
</UnderlineNav.Item>
10199
<UnderlineNav.Item
102100
href=""
103101
aria-current={tab === 'nbformat' ? 'page' : undefined}
104-
// onClick={e => goToTab(e, 'nbformat', notebook?.model)}
102+
onClick={e => goToTab(e, 'nbformat', notebook?.model)}
105103
>
106104
NbFormat
107105
</UnderlineNav.Item>

packages/react/src/components/notebook/NotebookAdapter.ts

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ import {
1212
} from '@jupyterlab/notebook';
1313
import { Context } from '@jupyterlab/docregistry';
1414
import { NotebookModel } from '@jupyterlab/notebook';
15+
import { KernelMessage } from '@jupyterlab/services';
1516
import * as nbformat from '@jupyterlab/nbformat';
17+
import { Kernel } from '../../jupyter/kernel/Kernel';
1618
import { NotebookCommandIds } from './NotebookCommands';
1719
import * as Diff from 'diff';
1820

@@ -22,6 +24,7 @@ export class NotebookAdapter {
2224
private _notebook: Notebook;
2325
private _context: Context<NotebookModel>;
2426
private _defaultCellType: nbformat.CellType = 'code';
27+
private _kernelInfo: KernelMessage.IInfoReply | null = null;
2528

2629
constructor(
2730
commands: CommandRegistry,
@@ -62,6 +65,63 @@ export class NotebookAdapter {
6265
return this._context;
6366
}
6467

68+
/**
69+
* Get the session context from the notebook panel.
70+
*/
71+
get sessionContext() {
72+
return this._panel.sessionContext;
73+
}
74+
75+
/**
76+
* Get the kernel connection from the session context.
77+
*/
78+
get kernel() {
79+
return this._panel.sessionContext?.session?.kernel ?? null;
80+
}
81+
82+
/**
83+
* Get the cached kernel info (language_info, etc.).
84+
* Call fetchKernelInfo() first to populate this.
85+
*/
86+
get kernelInfo(): KernelMessage.IInfoReply | null {
87+
return this._kernelInfo;
88+
}
89+
90+
/**
91+
* Fetch and cache the kernel info.
92+
* Returns the cached info if already fetched.
93+
*/
94+
async fetchKernelInfo(): Promise<KernelMessage.IInfoReply | null> {
95+
if (this._kernelInfo) {
96+
return this._kernelInfo;
97+
}
98+
const kernel = this.kernel;
99+
if (kernel) {
100+
this._kernelInfo = await kernel.info;
101+
return this._kernelInfo;
102+
}
103+
return null;
104+
}
105+
106+
/**
107+
* Assign a new kernel to the notebook.
108+
*
109+
* @param kernel - The Kernel instance to assign to the notebook
110+
*
111+
* @remarks
112+
* This method changes the kernel associated with the notebook's session context.
113+
* It uses the kernel's id to request a kernel change through the session context.
114+
* The kernel info cache is cleared when a new kernel is assigned.
115+
*/
116+
assignKernel(kernel: Kernel): void {
117+
// Clear the cached kernel info since we're changing kernels
118+
this._kernelInfo = null;
119+
// Change the kernel using the session context
120+
this._context?.sessionContext.changeKernel({
121+
id: kernel.id,
122+
});
123+
}
124+
65125
/**
66126
* Set the default cell type for new cells.
67127
*/

packages/react/src/components/notebook/NotebookBase.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -562,7 +562,7 @@ export function NotebookBase(props: INotebookBaseProps): JSX.Element {
562562
console.log(`[NotebookBase] Adapter cell count:`, adapter.getCellCount());
563563
const currentNotebooks = notebookStore.getState().notebooks;
564564
const updatedNotebooks = new Map(currentNotebooks);
565-
updatedNotebooks.set(id, { adapter });
565+
updatedNotebooks.set(id, { adapter, portals: [] });
566566
notebookStore.getState().setNotebooks(updatedNotebooks);
567567
console.log(
568568
`[NotebookBase] Store now has ${updatedNotebooks.size} notebooks`

0 commit comments

Comments
 (0)