Skip to content

Commit 6a82f78

Browse files
authored
Fixed storing of the notebook-outlineview state data (#13648)
* fixed storing of the notebook-outlineview state data Signed-off-by: Jonah Iden <[email protected]> * fixed is function and URI handling Signed-off-by: Jonah Iden <[email protected]> * improved is method for NotebookCellOutlineNode Signed-off-by: Jonah Iden <[email protected]> --------- Signed-off-by: Jonah Iden <[email protected]>
1 parent 11d6cd0 commit 6a82f78

File tree

2 files changed

+41
-17
lines changed

2 files changed

+41
-17
lines changed

packages/notebook/src/browser/contributions/notebook-label-provider-contribution.ts

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,13 @@
1616

1717
import { codicon, LabelProvider, LabelProviderContribution } from '@theia/core/lib/browser';
1818
import { inject, injectable } from '@theia/core/shared/inversify';
19-
import { CellKind } from '../../common';
19+
import { CellKind, CellUri } from '../../common';
2020
import { NotebookService } from '../service/notebook-service';
2121
import { NotebookCellOutlineNode } from './notebook-outline-contribution';
2222
import type Token = require('markdown-it/lib/token');
2323
import markdownit = require('@theia/core/shared/markdown-it');
24+
import { NotebookCellModel } from '../view-model/notebook-cell-model';
25+
import { URI } from '@theia/core';
2426

2527
@injectable()
2628
export class NotebookLabelProviderContribution implements LabelProviderContribution {
@@ -41,23 +43,43 @@ export class NotebookLabelProviderContribution implements LabelProviderContribut
4143
}
4244

4345
getIcon(element: NotebookCellOutlineNode): string {
44-
return element.notebookCell.cellKind === CellKind.Markup ? codicon('markdown') : codicon('code');
46+
const cell = this.findCellByUri(element.uri);
47+
if (cell) {
48+
return cell.cellKind === CellKind.Markup ? codicon('markdown') : codicon('code');
49+
}
50+
return '';
4551
}
4652

4753
getName(element: NotebookCellOutlineNode): string {
48-
return element.notebookCell.cellKind === CellKind.Code ?
49-
element.notebookCell.text.split('\n')[0] :
50-
this.extractPlaintext(this.markdownIt.parse(element.notebookCell.text.split('\n')[0], {}));
54+
const cell = this.findCellByUri(element.uri);
55+
if (cell) {
56+
return cell.cellKind === CellKind.Code ?
57+
cell.text.split('\n')[0] :
58+
this.extractPlaintext(this.markdownIt.parse(cell.text.split('\n')[0], {}));
59+
}
60+
return '';
5161
}
5262

5363
getLongName(element: NotebookCellOutlineNode): string {
54-
return element.notebookCell.cellKind === CellKind.Code ?
55-
element.notebookCell.text.split('\n')[0] :
56-
this.extractPlaintext(this.markdownIt.parse(element.notebookCell.text.split('\n')[0], {}));
64+
const cell = this.findCellByUri(element.uri);
65+
if (cell) {
66+
return cell.cellKind === CellKind.Code ?
67+
cell.text.split('\n')[0] :
68+
this.extractPlaintext(this.markdownIt.parse(cell.text.split('\n')[0], {}));
69+
}
70+
return '';
5771
}
5872

5973
extractPlaintext(parsedMarkdown: Token[]): string {
6074
return parsedMarkdown.map(token => token.children ? this.extractPlaintext(token.children) : token.content).join('');
6175
}
6276

77+
findCellByUri(uri: URI): NotebookCellModel | undefined {
78+
const parsed = CellUri.parse(uri);
79+
if (parsed) {
80+
return this.notebookService.getNotebookEditorModel(parsed.notebook)?.cells.find(cell => cell.handle === parsed?.handle);
81+
}
82+
return undefined;
83+
}
84+
6385
}

packages/notebook/src/browser/contributions/notebook-outline-contribution.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,20 @@ import { OutlineViewService } from '@theia/outline-view/lib/browser/outline-view
2121
import { NotebookModel } from '../view-model/notebook-model';
2222
import { OutlineSymbolInformationNode } from '@theia/outline-view/lib/browser/outline-view-widget';
2323
import { NotebookEditorWidget } from '../notebook-editor-widget';
24-
import { NotebookCellModel } from '../view-model/notebook-cell-model';
25-
import { DisposableCollection, URI } from '@theia/core';
24+
import { DisposableCollection, isObject, URI } from '@theia/core';
2625
import { CellKind, CellUri } from '../../common';
2726
import { NotebookService } from '../service/notebook-service';
2827
export interface NotebookCellOutlineNode extends OutlineSymbolInformationNode {
29-
notebookCell: NotebookCellModel;
3028
uri: URI;
3129
}
3230

3331
export namespace NotebookCellOutlineNode {
3432
export function is(element: object): element is NotebookCellOutlineNode {
35-
return TreeNode.is(element) && OutlineSymbolInformationNode.is(element) && 'notebookCell' in element;
33+
return TreeNode.is(element)
34+
&& OutlineSymbolInformationNode.is(element)
35+
&& isObject<NotebookCellOutlineNode>(element)
36+
&& element.uri instanceof URI
37+
&& element.uri.scheme === CellUri.cellUriScheme;
3638
}
3739
}
3840

@@ -94,17 +96,17 @@ export class NotebookOutlineContribution implements FrontendApplicationContribut
9496
children: [],
9597
selected: model.selectedCell === cell,
9698
expanded: false,
97-
notebookCell: cell,
98-
uri: model.uri,
99+
uri: cell.uri,
99100
} as NotebookCellOutlineNode));
100101
}
101102

102103
selectCell(node: object): void {
103104
if (NotebookCellOutlineNode.is(node)) {
104-
const parsed = CellUri.parse(node.notebookCell.uri);
105+
const parsed = CellUri.parse(node.uri);
105106
const model = parsed && this.notebookService.getNotebookEditorModel(parsed.notebook);
106-
if (model) {
107-
model.setSelectedCell(node.notebookCell);
107+
const cell = model?.cells.find(c => c.handle === parsed?.handle);
108+
if (model && cell) {
109+
model.setSelectedCell(cell);
108110
}
109111
}
110112
}

0 commit comments

Comments
 (0)