@theia/notebook 1.41.0
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.
- package/README.md +30 -0
- package/lib/browser/contributions/notebook-actions-contribution.d.ts +27 -0
- package/lib/browser/contributions/notebook-actions-contribution.d.ts.map +1 -0
- package/lib/browser/contributions/notebook-actions-contribution.js +172 -0
- package/lib/browser/contributions/notebook-actions-contribution.js.map +1 -0
- package/lib/browser/contributions/notebook-cell-actions-contribution.d.ts +30 -0
- package/lib/browser/contributions/notebook-cell-actions-contribution.d.ts.map +1 -0
- package/lib/browser/contributions/notebook-cell-actions-contribution.js +200 -0
- package/lib/browser/contributions/notebook-cell-actions-contribution.js.map +1 -0
- package/lib/browser/contributions/notebook-color-contribution.d.ts +6 -0
- package/lib/browser/contributions/notebook-color-contribution.d.ts.map +1 -0
- package/lib/browser/contributions/notebook-color-contribution.js +252 -0
- package/lib/browser/contributions/notebook-color-contribution.js.map +1 -0
- package/lib/browser/contributions/notebook-context-keys.d.ts +38 -0
- package/lib/browser/contributions/notebook-context-keys.d.ts.map +1 -0
- package/lib/browser/contributions/notebook-context-keys.js +95 -0
- package/lib/browser/contributions/notebook-context-keys.js.map +1 -0
- package/lib/browser/index.d.ts +11 -0
- package/lib/browser/index.d.ts.map +1 -0
- package/lib/browser/index.js +38 -0
- package/lib/browser/index.js.map +1 -0
- package/lib/browser/notebook-cell-resource-resolver.d.ts +20 -0
- package/lib/browser/notebook-cell-resource-resolver.d.ts.map +1 -0
- package/lib/browser/notebook-cell-resource-resolver.js +75 -0
- package/lib/browser/notebook-cell-resource-resolver.js.map +1 -0
- package/lib/browser/notebook-editor-widget-factory.d.ts +17 -0
- package/lib/browser/notebook-editor-widget-factory.d.ts.map +1 -0
- package/lib/browser/notebook-editor-widget-factory.js +78 -0
- package/lib/browser/notebook-editor-widget-factory.js.map +1 -0
- package/lib/browser/notebook-editor-widget.d.ts +49 -0
- package/lib/browser/notebook-editor-widget.d.ts.map +1 -0
- package/lib/browser/notebook-editor-widget.js +150 -0
- package/lib/browser/notebook-editor-widget.js.map +1 -0
- package/lib/browser/notebook-frontend-module.d.ts +5 -0
- package/lib/browser/notebook-frontend-module.d.ts.map +1 -0
- package/lib/browser/notebook-frontend-module.js +81 -0
- package/lib/browser/notebook-frontend-module.js.map +1 -0
- package/lib/browser/notebook-open-handler.d.ts +18 -0
- package/lib/browser/notebook-open-handler.d.ts.map +1 -0
- package/lib/browser/notebook-open-handler.js +94 -0
- package/lib/browser/notebook-open-handler.js.map +1 -0
- package/lib/browser/notebook-renderer-registry.d.ts +17 -0
- package/lib/browser/notebook-renderer-registry.d.ts.map +1 -0
- package/lib/browser/notebook-renderer-registry.js +63 -0
- package/lib/browser/notebook-renderer-registry.js.map +1 -0
- package/lib/browser/notebook-type-registry.d.ts +7 -0
- package/lib/browser/notebook-type-registry.d.ts.map +1 -0
- package/lib/browser/notebook-type-registry.js +42 -0
- package/lib/browser/notebook-type-registry.js.map +1 -0
- package/lib/browser/renderers/cell-output-webview.d.ts +12 -0
- package/lib/browser/renderers/cell-output-webview.d.ts.map +1 -0
- package/lib/browser/renderers/cell-output-webview.js +20 -0
- package/lib/browser/renderers/cell-output-webview.js.map +1 -0
- package/lib/browser/service/notebook-cell-context-manager.d.ts +16 -0
- package/lib/browser/service/notebook-cell-context-manager.d.ts.map +1 -0
- package/lib/browser/service/notebook-cell-context-manager.js +83 -0
- package/lib/browser/service/notebook-cell-context-manager.js.map +1 -0
- package/lib/browser/service/notebook-editor-service.d.ts +22 -0
- package/lib/browser/service/notebook-editor-service.d.ts.map +1 -0
- package/lib/browser/service/notebook-editor-service.js +95 -0
- package/lib/browser/service/notebook-editor-service.js.map +1 -0
- package/lib/browser/service/notebook-execution-service.d.ts +25 -0
- package/lib/browser/service/notebook-execution-service.d.ts.map +1 -0
- package/lib/browser/service/notebook-execution-service.js +140 -0
- package/lib/browser/service/notebook-execution-service.js.map +1 -0
- package/lib/browser/service/notebook-execution-state-service.d.ts +94 -0
- package/lib/browser/service/notebook-execution-state-service.d.ts.map +1 -0
- package/lib/browser/service/notebook-execution-state-service.js +250 -0
- package/lib/browser/service/notebook-execution-state-service.js.map +1 -0
- package/lib/browser/service/notebook-kernel-history-service.d.ts +23 -0
- package/lib/browser/service/notebook-kernel-history-service.d.ts.map +1 -0
- package/lib/browser/service/notebook-kernel-history-service.js +109 -0
- package/lib/browser/service/notebook-kernel-history-service.js.map +1 -0
- package/lib/browser/service/notebook-kernel-quick-pick-service.d.ts +65 -0
- package/lib/browser/service/notebook-kernel-quick-pick-service.d.ts.map +1 -0
- package/lib/browser/service/notebook-kernel-quick-pick-service.js +430 -0
- package/lib/browser/service/notebook-kernel-quick-pick-service.js.map +1 -0
- package/lib/browser/service/notebook-kernel-service.d.ts +112 -0
- package/lib/browser/service/notebook-kernel-service.d.ts.map +1 -0
- package/lib/browser/service/notebook-kernel-service.js +274 -0
- package/lib/browser/service/notebook-kernel-service.js.map +1 -0
- package/lib/browser/service/notebook-model-resolver-service.d.ts +26 -0
- package/lib/browser/service/notebook-model-resolver-service.d.ts.map +1 -0
- package/lib/browser/service/notebook-model-resolver-service.js +139 -0
- package/lib/browser/service/notebook-model-resolver-service.js.map +1 -0
- package/lib/browser/service/notebook-renderer-messaging-service.d.ts +30 -0
- package/lib/browser/service/notebook-renderer-messaging-service.d.ts.map +1 -0
- package/lib/browser/service/notebook-renderer-messaging-service.js +94 -0
- package/lib/browser/service/notebook-renderer-messaging-service.js.map +1 -0
- package/lib/browser/service/notebook-service.d.ts +64 -0
- package/lib/browser/service/notebook-service.d.ts.map +1 -0
- package/lib/browser/service/notebook-service.js +162 -0
- package/lib/browser/service/notebook-service.js.map +1 -0
- package/lib/browser/view/notebook-cell-editor.d.ts +26 -0
- package/lib/browser/view/notebook-cell-editor.d.ts.map +1 -0
- package/lib/browser/view/notebook-cell-editor.js +79 -0
- package/lib/browser/view/notebook-cell-editor.js.map +1 -0
- package/lib/browser/view/notebook-cell-list-view.d.ts +43 -0
- package/lib/browser/view/notebook-cell-list-view.d.ts.map +1 -0
- package/lib/browser/view/notebook-cell-list-view.js +111 -0
- package/lib/browser/view/notebook-cell-list-view.js.map +1 -0
- package/lib/browser/view/notebook-cell-toolbar-factory.d.ts +25 -0
- package/lib/browser/view/notebook-cell-toolbar-factory.d.ts.map +1 -0
- package/lib/browser/view/notebook-cell-toolbar-factory.js +93 -0
- package/lib/browser/view/notebook-cell-toolbar-factory.js.map +1 -0
- package/lib/browser/view/notebook-cell-toolbar.d.ts +25 -0
- package/lib/browser/view/notebook-cell-toolbar.d.ts.map +1 -0
- package/lib/browser/view/notebook-cell-toolbar.js +50 -0
- package/lib/browser/view/notebook-cell-toolbar.js.map +1 -0
- package/lib/browser/view/notebook-code-cell-view.d.ts +47 -0
- package/lib/browser/view/notebook-code-cell-view.d.ts.map +1 -0
- package/lib/browser/view/notebook-code-cell-view.js +166 -0
- package/lib/browser/view/notebook-code-cell-view.js.map +1 -0
- package/lib/browser/view/notebook-main-toolbar.d.ts +31 -0
- package/lib/browser/view/notebook-main-toolbar.d.ts.map +1 -0
- package/lib/browser/view/notebook-main-toolbar.js +118 -0
- package/lib/browser/view/notebook-main-toolbar.js.map +1 -0
- package/lib/browser/view/notebook-markdown-cell-view.d.ts +12 -0
- package/lib/browser/view/notebook-markdown-cell-view.d.ts.map +1 -0
- package/lib/browser/view/notebook-markdown-cell-view.js +70 -0
- package/lib/browser/view/notebook-markdown-cell-view.js.map +1 -0
- package/lib/browser/view-model/notebook-cell-model.d.ts +74 -0
- package/lib/browser/view-model/notebook-cell-model.d.ts.map +1 -0
- package/lib/browser/view-model/notebook-cell-model.js +226 -0
- package/lib/browser/view-model/notebook-cell-model.js.map +1 -0
- package/lib/browser/view-model/notebook-cell-output-model.d.ts +19 -0
- package/lib/browser/view-model/notebook-cell-output-model.d.ts.map +1 -0
- package/lib/browser/view-model/notebook-cell-output-model.js +61 -0
- package/lib/browser/view-model/notebook-cell-output-model.js.map +1 -0
- package/lib/browser/view-model/notebook-model.d.ts +59 -0
- package/lib/browser/view-model/notebook-model.d.ts.map +1 -0
- package/lib/browser/view-model/notebook-model.js +315 -0
- package/lib/browser/view-model/notebook-model.js.map +1 -0
- package/lib/common/index.d.ts +3 -0
- package/lib/common/index.d.ts.map +1 -0
- package/lib/common/index.js +30 -0
- package/lib/common/index.js.map +1 -0
- package/lib/common/notebook-common.d.ts +344 -0
- package/lib/common/notebook-common.d.ts.map +1 -0
- package/lib/common/notebook-common.js +112 -0
- package/lib/common/notebook-common.js.map +1 -0
- package/lib/common/notebook-protocol.d.ts +21 -0
- package/lib/common/notebook-protocol.d.ts.map +1 -0
- package/lib/common/notebook-protocol.js +18 -0
- package/lib/common/notebook-protocol.js.map +1 -0
- package/lib/common/notebook-range.d.ts +14 -0
- package/lib/common/notebook-range.d.ts.map +1 -0
- package/lib/common/notebook-range.js +18 -0
- package/lib/common/notebook-range.js.map +1 -0
- package/package.json +52 -0
- package/src/browser/contributions/notebook-actions-contribution.ts +171 -0
- package/src/browser/contributions/notebook-cell-actions-contribution.ts +204 -0
- package/src/browser/contributions/notebook-color-contribution.ts +268 -0
- package/src/browser/contributions/notebook-context-keys.ts +99 -0
- package/src/browser/index.ts +26 -0
- package/src/browser/notebook-cell-resource-resolver.ts +79 -0
- package/src/browser/notebook-editor-widget-factory.ts +70 -0
- package/src/browser/notebook-editor-widget.tsx +157 -0
- package/src/browser/notebook-frontend-module.ts +95 -0
- package/src/browser/notebook-open-handler.ts +87 -0
- package/src/browser/notebook-renderer-registry.ts +62 -0
- package/src/browser/notebook-type-registry.ts +30 -0
- package/src/browser/renderers/cell-output-webview.ts +32 -0
- package/src/browser/service/notebook-cell-context-manager.ts +70 -0
- package/src/browser/service/notebook-editor-service.ts +86 -0
- package/src/browser/service/notebook-execution-service.ts +139 -0
- package/src/browser/service/notebook-execution-state-service.ts +322 -0
- package/src/browser/service/notebook-kernel-history-service.ts +109 -0
- package/src/browser/service/notebook-kernel-quick-pick-service.ts +494 -0
- package/src/browser/service/notebook-kernel-service.ts +357 -0
- package/src/browser/service/notebook-model-resolver-service.ts +141 -0
- package/src/browser/service/notebook-renderer-messaging-service.ts +111 -0
- package/src/browser/service/notebook-service.ts +178 -0
- package/src/browser/style/index.css +236 -0
- package/src/browser/view/notebook-cell-editor.tsx +97 -0
- package/src/browser/view/notebook-cell-list-view.tsx +171 -0
- package/src/browser/view/notebook-cell-toolbar-factory.tsx +91 -0
- package/src/browser/view/notebook-cell-toolbar.tsx +70 -0
- package/src/browser/view/notebook-code-cell-view.tsx +190 -0
- package/src/browser/view/notebook-main-toolbar.tsx +115 -0
- package/src/browser/view/notebook-markdown-cell-view.tsx +73 -0
- package/src/browser/view-model/notebook-cell-model.ts +271 -0
- package/src/browser/view-model/notebook-cell-output-model.ts +69 -0
- package/src/browser/view-model/notebook-model.ts +372 -0
- package/src/common/index.ts +18 -0
- package/src/common/notebook-common.ts +462 -0
- package/src/common/notebook-protocol.ts +35 -0
- package/src/common/notebook-range.ts +30 -0
|
@@ -0,0 +1,372 @@
|
|
|
1
|
+
// *****************************************************************************
|
|
2
|
+
// Copyright (C) 20023 Typefox and others.
|
|
3
|
+
//
|
|
4
|
+
// This program and the accompanying materials are made available under the
|
|
5
|
+
// terms of the Eclipse Public License v. 2.0 which is available at
|
|
6
|
+
// http://www.eclipse.org/legal/epl-2.0.
|
|
7
|
+
//
|
|
8
|
+
// This Source Code may also be made available under the following Secondary
|
|
9
|
+
// Licenses when the conditions for such availability set forth in the Eclipse
|
|
10
|
+
// Public License v. 2.0 are satisfied: GNU General Public License, version 2
|
|
11
|
+
// with the GNU Classpath Exception which is available at
|
|
12
|
+
// https://www.gnu.org/software/classpath/license.html.
|
|
13
|
+
//
|
|
14
|
+
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
|
|
15
|
+
// *****************************************************************************
|
|
16
|
+
|
|
17
|
+
import { Disposable, Emitter, URI } from '@theia/core';
|
|
18
|
+
import { Saveable, SaveOptions } from '@theia/core/lib/browser';
|
|
19
|
+
import {
|
|
20
|
+
CellData,
|
|
21
|
+
CellEditOperation, CellEditType, CellUri, NotebookCellInternalMetadata,
|
|
22
|
+
NotebookCellsChangeType, NotebookCellTextModelSplice, NotebookData,
|
|
23
|
+
NotebookDocumentMetadata, NotebookModelWillAddRemoveEvent,
|
|
24
|
+
NotebookTextModelChangedEvent, NullablePartialNotebookCellInternalMetadata
|
|
25
|
+
} from '../../common';
|
|
26
|
+
import { NotebookSerializer } from '../service/notebook-service';
|
|
27
|
+
import { FileService } from '@theia/filesystem/lib/browser/file-service';
|
|
28
|
+
import { NotebookCellModel, NotebookCellModelFactory, NotebookCellModelProps } from './notebook-cell-model';
|
|
29
|
+
import { MonacoTextModelService } from '@theia/monaco/lib/browser/monaco-text-model-service';
|
|
30
|
+
import { inject, injectable, interfaces } from '@theia/core/shared/inversify';
|
|
31
|
+
import { NotebookKernel } from '../service/notebook-kernel-service';
|
|
32
|
+
import { UndoRedoService } from '@theia/editor/lib/browser/undo-redo-service';
|
|
33
|
+
|
|
34
|
+
export const NotebookModelFactory = Symbol('NotebookModelFactory');
|
|
35
|
+
|
|
36
|
+
export function createNotebookModelContainer(parent: interfaces.Container, props: NotebookModelProps): interfaces.Container {
|
|
37
|
+
const child = parent.createChild();
|
|
38
|
+
|
|
39
|
+
child.bind(NotebookModelProps).toConstantValue(props);
|
|
40
|
+
child.bind(NotebookModel).toSelf();
|
|
41
|
+
|
|
42
|
+
return child;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const NotebookModelProps = Symbol('NotebookModelProps');
|
|
46
|
+
export interface NotebookModelProps {
|
|
47
|
+
data: NotebookData,
|
|
48
|
+
uri: URI,
|
|
49
|
+
viewType: string,
|
|
50
|
+
serializer: NotebookSerializer,
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
@injectable()
|
|
54
|
+
export class NotebookModel implements Saveable, Disposable {
|
|
55
|
+
|
|
56
|
+
private readonly onDirtyChangedEmitter = new Emitter<void>();
|
|
57
|
+
readonly onDirtyChanged = this.onDirtyChangedEmitter.event;
|
|
58
|
+
|
|
59
|
+
private readonly onDidSaveNotebookEmitter = new Emitter<void>();
|
|
60
|
+
readonly onDidSaveNotebook = this.onDidSaveNotebookEmitter.event;
|
|
61
|
+
|
|
62
|
+
private readonly onDidAddOrRemoveCellEmitter = new Emitter<NotebookModelWillAddRemoveEvent>();
|
|
63
|
+
readonly onDidAddOrRemoveCell = this.onDidAddOrRemoveCellEmitter.event;
|
|
64
|
+
|
|
65
|
+
private readonly onDidChangeContentEmitter = new Emitter<NotebookTextModelChangedEvent>();
|
|
66
|
+
readonly onDidChangeContent = this.onDidChangeContentEmitter.event;
|
|
67
|
+
|
|
68
|
+
@inject(FileService)
|
|
69
|
+
private readonly fileService: FileService;
|
|
70
|
+
|
|
71
|
+
@inject(UndoRedoService)
|
|
72
|
+
private readonly undoRedoService: UndoRedoService;
|
|
73
|
+
|
|
74
|
+
readonly autoSave: 'off' | 'afterDelay' | 'onFocusChange' | 'onWindowChange';
|
|
75
|
+
|
|
76
|
+
nextHandle: number = 0;
|
|
77
|
+
|
|
78
|
+
kernel?: NotebookKernel;
|
|
79
|
+
|
|
80
|
+
dirty: boolean;
|
|
81
|
+
selectedCell?: NotebookCellModel;
|
|
82
|
+
private dirtyCells: NotebookCellModel[] = [];
|
|
83
|
+
|
|
84
|
+
cells: NotebookCellModel[];
|
|
85
|
+
|
|
86
|
+
get uri(): URI {
|
|
87
|
+
return this.props.uri;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
get viewType(): string {
|
|
91
|
+
return this.props.viewType;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
metadata: NotebookDocumentMetadata = {};
|
|
95
|
+
|
|
96
|
+
constructor(@inject(NotebookModelProps) private props: NotebookModelProps,
|
|
97
|
+
@inject(MonacoTextModelService) modelService: MonacoTextModelService,
|
|
98
|
+
@inject(NotebookCellModelFactory) private cellModelFactory: (props: NotebookCellModelProps) => NotebookCellModel) {
|
|
99
|
+
this.dirty = false;
|
|
100
|
+
|
|
101
|
+
this.cells = props.data.cells.map((cell, index) => cellModelFactory({
|
|
102
|
+
uri: CellUri.generate(props.uri, index),
|
|
103
|
+
handle: index,
|
|
104
|
+
source: cell.source,
|
|
105
|
+
language: cell.language,
|
|
106
|
+
cellKind: cell.cellKind,
|
|
107
|
+
outputs: cell.outputs,
|
|
108
|
+
metadata: cell.metadata,
|
|
109
|
+
internalMetadata: cell.internalMetadata,
|
|
110
|
+
collapseState: cell.collapseState
|
|
111
|
+
}));
|
|
112
|
+
|
|
113
|
+
this.addCellOutputListeners(this.cells);
|
|
114
|
+
|
|
115
|
+
this.metadata = this.metadata;
|
|
116
|
+
|
|
117
|
+
modelService.onDidCreate(editorModel => {
|
|
118
|
+
const modelUri = new URI(editorModel.uri);
|
|
119
|
+
if (modelUri.scheme === CellUri.scheme) {
|
|
120
|
+
const cellUri = CellUri.parse(modelUri);
|
|
121
|
+
if (cellUri && cellUri.notebook.isEqual(this.uri)) {
|
|
122
|
+
const cell = this.cells.find(c => c.handle === cellUri.handle);
|
|
123
|
+
if (cell) {
|
|
124
|
+
cell.textModel = editorModel;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
});
|
|
129
|
+
this.nextHandle = this.cells.length;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
dispose(): void {
|
|
133
|
+
this.onDirtyChangedEmitter.dispose();
|
|
134
|
+
this.onDidSaveNotebookEmitter.dispose();
|
|
135
|
+
this.onDidAddOrRemoveCellEmitter.dispose();
|
|
136
|
+
this.onDidChangeContentEmitter.dispose();
|
|
137
|
+
this.cells.forEach(cell => cell.dispose());
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
async save(options: SaveOptions): Promise<void> {
|
|
141
|
+
this.dirtyCells = [];
|
|
142
|
+
this.dirty = false;
|
|
143
|
+
this.onDirtyChangedEmitter.fire();
|
|
144
|
+
|
|
145
|
+
const serializedNotebook = await this.props.serializer.notebookToData({
|
|
146
|
+
cells: this.cells.map(cell => cell.getData()),
|
|
147
|
+
metadata: this.metadata
|
|
148
|
+
});
|
|
149
|
+
this.fileService.writeFile(this.uri, serializedNotebook);
|
|
150
|
+
|
|
151
|
+
this.onDidSaveNotebookEmitter.fire();
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
createSnapshot(): Saveable.Snapshot {
|
|
155
|
+
const model = this;
|
|
156
|
+
return {
|
|
157
|
+
read(): string {
|
|
158
|
+
return JSON.stringify({
|
|
159
|
+
cells: model.cells.map(cell => cell.getData()),
|
|
160
|
+
metadata: model.metadata
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
};
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
async revert(options?: Saveable.RevertOptions): Promise<void> {
|
|
167
|
+
this.dirty = false;
|
|
168
|
+
this.onDirtyChangedEmitter.fire();
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
isDirty(): boolean {
|
|
172
|
+
return this.dirty;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
cellDirtyChanged(cell: NotebookCellModel, dirtyState: boolean): void {
|
|
176
|
+
if (dirtyState) {
|
|
177
|
+
this.dirtyCells.push(cell);
|
|
178
|
+
} else {
|
|
179
|
+
this.dirtyCells.splice(this.dirtyCells.indexOf(cell), 1);
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
const oldDirtyState = this.dirty;
|
|
183
|
+
this.dirty = this.dirtyCells.length > 0;
|
|
184
|
+
if (this.dirty !== oldDirtyState) {
|
|
185
|
+
this.onDirtyChangedEmitter.fire();
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
undo(): void {
|
|
190
|
+
// TODO we probably need to check if a monaco editor is focused and if so, not undo
|
|
191
|
+
this.undoRedoService.undo(this.uri);
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
redo(): void {
|
|
195
|
+
// TODO see undo
|
|
196
|
+
this.undoRedoService.redo(this.uri);
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
setSelectedCell(cell: NotebookCellModel): void {
|
|
200
|
+
this.selectedCell = cell;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
private addCellOutputListeners(cells: NotebookCellModel[]): void {
|
|
204
|
+
for (const cell of cells) {
|
|
205
|
+
cell.onDidChangeOutputs(() => {
|
|
206
|
+
this.dirty = true;
|
|
207
|
+
this.onDirtyChangedEmitter.fire();
|
|
208
|
+
});
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
applyEdits(rawEdits: CellEditOperation[], computeUndoRedo: boolean): void {
|
|
213
|
+
const editsWithDetails = rawEdits.map((edit, index) => {
|
|
214
|
+
let cellIndex: number = -1;
|
|
215
|
+
if ('index' in edit) {
|
|
216
|
+
cellIndex = edit.index;
|
|
217
|
+
} else if ('handle' in edit) {
|
|
218
|
+
cellIndex = this.getCellIndexByHandle(edit.handle);
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
return {
|
|
222
|
+
edit,
|
|
223
|
+
cellIndex,
|
|
224
|
+
end: edit.editType === CellEditType.Replace ? edit.index + edit.count : cellIndex,
|
|
225
|
+
originalIndex: index
|
|
226
|
+
};
|
|
227
|
+
}).filter(edit => !!edit);
|
|
228
|
+
|
|
229
|
+
for (const { edit, cellIndex } of editsWithDetails) {
|
|
230
|
+
const cell = this.cells[cellIndex];
|
|
231
|
+
if (cell) {
|
|
232
|
+
this.cellDirtyChanged(cell, true);
|
|
233
|
+
}
|
|
234
|
+
switch (edit.editType) {
|
|
235
|
+
case CellEditType.Replace:
|
|
236
|
+
this.replaceCells(edit.index, edit.count, edit.cells, computeUndoRedo);
|
|
237
|
+
break;
|
|
238
|
+
case CellEditType.Output: {
|
|
239
|
+
if (edit.append) {
|
|
240
|
+
cell.spliceNotebookCellOutputs({ deleteCount: 0, newOutputs: edit.outputs, start: cell.outputs.length });
|
|
241
|
+
} else {
|
|
242
|
+
// could definitely be more efficient. See vscode __spliceNotebookCellOutputs2
|
|
243
|
+
// For now, just replace the whole existing output with the new output
|
|
244
|
+
cell.spliceNotebookCellOutputs({ start: 0, deleteCount: cell.outputs.length, newOutputs: edit.outputs });
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
break;
|
|
248
|
+
}
|
|
249
|
+
case CellEditType.OutputItems:
|
|
250
|
+
break;
|
|
251
|
+
case CellEditType.Metadata:
|
|
252
|
+
this.updateNotebookMetadata(edit.metadata, computeUndoRedo);
|
|
253
|
+
break;
|
|
254
|
+
case CellEditType.PartialInternalMetadata:
|
|
255
|
+
this.changeCellInternalMetadataPartial(this.cells[cellIndex], edit.internalMetadata);
|
|
256
|
+
break;
|
|
257
|
+
case CellEditType.CellLanguage:
|
|
258
|
+
this.changeCellLanguage(this.cells[cellIndex], edit.language, computeUndoRedo);
|
|
259
|
+
break;
|
|
260
|
+
case CellEditType.DocumentMetadata:
|
|
261
|
+
break;
|
|
262
|
+
case CellEditType.Move:
|
|
263
|
+
this.moveCellToIndex(cellIndex, edit.length, edit.newIdx, computeUndoRedo);
|
|
264
|
+
break;
|
|
265
|
+
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
private replaceCells(start: number, deleteCount: number, newCells: CellData[], computeUndoRedo: boolean): void {
|
|
271
|
+
const cells = newCells.map(cell => {
|
|
272
|
+
const handle = this.nextHandle++;
|
|
273
|
+
return this.cellModelFactory({
|
|
274
|
+
uri: CellUri.generate(this.uri, handle),
|
|
275
|
+
handle: handle,
|
|
276
|
+
source: cell.source,
|
|
277
|
+
language: cell.language,
|
|
278
|
+
cellKind: cell.cellKind,
|
|
279
|
+
outputs: cell.outputs,
|
|
280
|
+
metadata: cell.metadata,
|
|
281
|
+
internalMetadata: cell.internalMetadata,
|
|
282
|
+
collapseState: cell.collapseState
|
|
283
|
+
});
|
|
284
|
+
});
|
|
285
|
+
this.addCellOutputListeners(cells);
|
|
286
|
+
|
|
287
|
+
const changes: NotebookCellTextModelSplice<NotebookCellModel>[] = [[start, deleteCount, cells]];
|
|
288
|
+
|
|
289
|
+
const deletedCells = this.cells.splice(start, deleteCount, ...cells);
|
|
290
|
+
|
|
291
|
+
for (const cell of deletedCells) {
|
|
292
|
+
cell.dispose();
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
if (computeUndoRedo) {
|
|
296
|
+
this.undoRedoService.pushElement(this.uri,
|
|
297
|
+
async () => this.replaceCells(start, newCells.length, deletedCells.map(cell => cell.getData()), false),
|
|
298
|
+
async () => this.replaceCells(start, deleteCount, newCells, false));
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
this.onDidAddOrRemoveCellEmitter.fire({ rawEvent: { kind: NotebookCellsChangeType.ModelChange, changes } });
|
|
302
|
+
this.onDidChangeContentEmitter.fire({ rawEvents: [{ kind: NotebookCellsChangeType.ModelChange, changes }] });
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
private changeCellInternalMetadataPartial(cell: NotebookCellModel, internalMetadata: NullablePartialNotebookCellInternalMetadata): void {
|
|
306
|
+
const newInternalMetadata: NotebookCellInternalMetadata = {
|
|
307
|
+
...cell.internalMetadata
|
|
308
|
+
};
|
|
309
|
+
let k: keyof NotebookCellInternalMetadata;
|
|
310
|
+
// eslint-disable-next-line guard-for-in
|
|
311
|
+
for (k in internalMetadata) {
|
|
312
|
+
newInternalMetadata[k] = (internalMetadata[k] ?? undefined) as never;
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
cell.internalMetadata = newInternalMetadata;
|
|
316
|
+
this.onDidChangeContentEmitter.fire({
|
|
317
|
+
rawEvents: [
|
|
318
|
+
{ kind: NotebookCellsChangeType.ChangeCellInternalMetadata, index: this.cells.indexOf(cell), internalMetadata: newInternalMetadata }
|
|
319
|
+
]
|
|
320
|
+
});
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
private updateNotebookMetadata(metadata: NotebookDocumentMetadata, computeUndoRedo: boolean): void {
|
|
324
|
+
const oldMetadata = this.metadata;
|
|
325
|
+
if (computeUndoRedo) {
|
|
326
|
+
this.undoRedoService.pushElement(this.uri,
|
|
327
|
+
async () => this.updateNotebookMetadata(oldMetadata, false),
|
|
328
|
+
async () => this.updateNotebookMetadata(metadata, false)
|
|
329
|
+
);
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
this.metadata = metadata;
|
|
333
|
+
this.onDidChangeContentEmitter.fire({
|
|
334
|
+
rawEvents: [{ kind: NotebookCellsChangeType.ChangeDocumentMetadata, metadata: this.metadata }],
|
|
335
|
+
synchronous: true,
|
|
336
|
+
});
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
private changeCellLanguage(cell: NotebookCellModel, languageId: string, computeUndoRedo: boolean): void {
|
|
340
|
+
if (cell.language === languageId) {
|
|
341
|
+
return;
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
cell.language = languageId;
|
|
345
|
+
|
|
346
|
+
this.onDidChangeContentEmitter.fire({
|
|
347
|
+
rawEvents: [{ kind: NotebookCellsChangeType.ChangeCellLanguage, index: this.cells.indexOf(cell), language: languageId }],
|
|
348
|
+
synchronous: true,
|
|
349
|
+
});
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
private moveCellToIndex(fromIndex: number, length: number, toIndex: number, computeUndoRedo: boolean): boolean {
|
|
353
|
+
if (computeUndoRedo) {
|
|
354
|
+
this.undoRedoService.pushElement(this.uri,
|
|
355
|
+
async () => { this.moveCellToIndex(toIndex, length, fromIndex, false); },
|
|
356
|
+
async () => { this.moveCellToIndex(fromIndex, length, toIndex, false); }
|
|
357
|
+
);
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
const cells = this.cells.splice(fromIndex, length);
|
|
361
|
+
this.cells.splice(toIndex, 0, ...cells);
|
|
362
|
+
this.onDidChangeContentEmitter.fire({
|
|
363
|
+
rawEvents: [{ kind: NotebookCellsChangeType.Move, index: fromIndex, length, newIdx: toIndex, cells }],
|
|
364
|
+
});
|
|
365
|
+
|
|
366
|
+
return true;
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
private getCellIndexByHandle(handle: number): number {
|
|
370
|
+
return this.cells.findIndex(c => c.handle === handle);
|
|
371
|
+
}
|
|
372
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
// *****************************************************************************
|
|
2
|
+
// Copyright (C) 2023 TypeFox and others.
|
|
3
|
+
//
|
|
4
|
+
// This program and the accompanying materials are made available under the
|
|
5
|
+
// terms of the Eclipse Public License v. 2.0 which is available at
|
|
6
|
+
// http://www.eclipse.org/legal/epl-2.0.
|
|
7
|
+
//
|
|
8
|
+
// This Source Code may also be made available under the following Secondary
|
|
9
|
+
// Licenses when the conditions for such availability set forth in the Eclipse
|
|
10
|
+
// Public License v. 2.0 are satisfied: GNU General Public License, version 2
|
|
11
|
+
// with the GNU Classpath Exception which is available at
|
|
12
|
+
// https://www.gnu.org/software/classpath/license.html.
|
|
13
|
+
//
|
|
14
|
+
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
|
|
15
|
+
// *****************************************************************************
|
|
16
|
+
|
|
17
|
+
export * from './notebook-common';
|
|
18
|
+
export * from './notebook-range';
|