@theia/notebook 1.53.0-next.55 → 1.53.0-next.64

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 (78) hide show
  1. package/README.md +30 -30
  2. package/lib/browser/index.d.ts +1 -0
  3. package/lib/browser/index.d.ts.map +1 -1
  4. package/lib/browser/index.js +1 -0
  5. package/lib/browser/index.js.map +1 -1
  6. package/lib/browser/notebook-frontend-module.d.ts.map +1 -1
  7. package/lib/browser/notebook-frontend-module.js +2 -0
  8. package/lib/browser/notebook-frontend-module.js.map +1 -1
  9. package/lib/browser/service/notebook-cell-editor-service.d.ts +16 -0
  10. package/lib/browser/service/notebook-cell-editor-service.d.ts.map +1 -0
  11. package/lib/browser/service/notebook-cell-editor-service.js +53 -0
  12. package/lib/browser/service/notebook-cell-editor-service.js.map +1 -0
  13. package/lib/browser/view/notebook-cell-editor.d.ts +2 -0
  14. package/lib/browser/view/notebook-cell-editor.d.ts.map +1 -1
  15. package/lib/browser/view/notebook-cell-editor.js +12 -1
  16. package/lib/browser/view/notebook-cell-editor.js.map +1 -1
  17. package/lib/browser/view/notebook-code-cell-view.d.ts +2 -0
  18. package/lib/browser/view/notebook-code-cell-view.d.ts.map +1 -1
  19. package/lib/browser/view/notebook-code-cell-view.js +6 -1
  20. package/lib/browser/view/notebook-code-cell-view.js.map +1 -1
  21. package/lib/browser/view/notebook-markdown-cell-view.d.ts +2 -0
  22. package/lib/browser/view/notebook-markdown-cell-view.d.ts.map +1 -1
  23. package/lib/browser/view/notebook-markdown-cell-view.js +8 -3
  24. package/lib/browser/view/notebook-markdown-cell-view.js.map +1 -1
  25. package/package.json +7 -7
  26. package/src/browser/contributions/cell-operations.ts +44 -44
  27. package/src/browser/contributions/notebook-actions-contribution.ts +379 -379
  28. package/src/browser/contributions/notebook-cell-actions-contribution.ts +525 -525
  29. package/src/browser/contributions/notebook-color-contribution.ts +268 -268
  30. package/src/browser/contributions/notebook-context-keys.ts +113 -113
  31. package/src/browser/contributions/notebook-label-provider-contribution.ts +85 -85
  32. package/src/browser/contributions/notebook-outline-contribution.ts +114 -114
  33. package/src/browser/contributions/notebook-output-action-contribution.ts +82 -82
  34. package/src/browser/contributions/notebook-preferences.ts +92 -92
  35. package/src/browser/contributions/notebook-status-bar-contribution.ts +77 -77
  36. package/src/browser/contributions/notebook-undo-redo-handler.ts +41 -41
  37. package/src/browser/index.ts +28 -27
  38. package/src/browser/notebook-cell-resource-resolver.ts +130 -130
  39. package/src/browser/notebook-editor-widget-factory.ts +82 -82
  40. package/src/browser/notebook-editor-widget.tsx +330 -330
  41. package/src/browser/notebook-frontend-module.ts +121 -119
  42. package/src/browser/notebook-open-handler.ts +120 -120
  43. package/src/browser/notebook-output-utils.ts +119 -119
  44. package/src/browser/notebook-renderer-registry.ts +85 -85
  45. package/src/browser/notebook-type-registry.ts +54 -54
  46. package/src/browser/notebook-types.ts +186 -186
  47. package/src/browser/renderers/cell-output-webview.ts +33 -33
  48. package/src/browser/service/notebook-cell-editor-service.ts +56 -0
  49. package/src/browser/service/notebook-clipboard-service.ts +43 -43
  50. package/src/browser/service/notebook-context-manager.ts +162 -162
  51. package/src/browser/service/notebook-editor-widget-service.ts +101 -101
  52. package/src/browser/service/notebook-execution-service.ts +139 -139
  53. package/src/browser/service/notebook-execution-state-service.ts +311 -311
  54. package/src/browser/service/notebook-kernel-history-service.ts +124 -124
  55. package/src/browser/service/notebook-kernel-quick-pick-service.ts +479 -479
  56. package/src/browser/service/notebook-kernel-service.ts +357 -357
  57. package/src/browser/service/notebook-model-resolver-service.ts +160 -160
  58. package/src/browser/service/notebook-monaco-text-model-service.ts +48 -48
  59. package/src/browser/service/notebook-options.ts +155 -155
  60. package/src/browser/service/notebook-renderer-messaging-service.ts +121 -121
  61. package/src/browser/service/notebook-service.ts +215 -215
  62. package/src/browser/style/index.css +484 -483
  63. package/src/browser/view/notebook-cell-editor.tsx +276 -263
  64. package/src/browser/view/notebook-cell-list-view.tsx +279 -279
  65. package/src/browser/view/notebook-cell-toolbar-factory.tsx +102 -102
  66. package/src/browser/view/notebook-cell-toolbar.tsx +74 -74
  67. package/src/browser/view/notebook-code-cell-view.tsx +355 -350
  68. package/src/browser/view/notebook-find-widget.tsx +335 -335
  69. package/src/browser/view/notebook-main-toolbar.tsx +235 -235
  70. package/src/browser/view/notebook-markdown-cell-view.tsx +215 -208
  71. package/src/browser/view/notebook-viewport-service.ts +61 -61
  72. package/src/browser/view-model/notebook-cell-model.ts +473 -473
  73. package/src/browser/view-model/notebook-cell-output-model.ts +100 -100
  74. package/src/browser/view-model/notebook-model.ts +550 -550
  75. package/src/common/index.ts +18 -18
  76. package/src/common/notebook-common.ts +337 -337
  77. package/src/common/notebook-protocol.ts +35 -35
  78. package/src/common/notebook-range.ts +30 -30
@@ -1,155 +1,155 @@
1
-
2
- // *****************************************************************************
3
- // Copyright (C) 2024 TypeFox and others.
4
- //
5
- // This program and the accompanying materials are made available under the
6
- // terms of the Eclipse Public License v. 2.0 which is available at
7
- // http://www.eclipse.org/legal/epl-2.0.
8
- //
9
- // This Source Code may also be made available under the following Secondary
10
- // Licenses when the conditions for such availability set forth in the Eclipse
11
- // Public License v. 2.0 are satisfied: GNU General Public License, version 2
12
- // with the GNU Classpath Exception which is available at
13
- // https://www.gnu.org/software/classpath/license.html.
14
- //
15
- // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
16
- // *****************************************************************************
17
-
18
- import { inject, injectable, postConstruct } from '@theia/core/shared/inversify';
19
- import { PreferenceService } from '@theia/core/lib/browser';
20
- import { Emitter } from '@theia/core';
21
- import { NotebookPreferences, notebookPreferenceSchema } from '../contributions/notebook-preferences';
22
- import { EditorPreferences } from '@theia/editor/lib/browser';
23
- import { BareFontInfo } from '@theia/monaco-editor-core/esm/vs/editor/common/config/fontInfo';
24
- import { PixelRatio } from '@theia/monaco-editor-core/esm/vs/base/browser/browser';
25
-
26
- const notebookOutputOptionsRelevantPreferences = [
27
- 'editor.fontSize',
28
- 'editor.fontFamily',
29
- NotebookPreferences.NOTEBOOK_LINE_NUMBERS,
30
- NotebookPreferences.OUTPUT_LINE_HEIGHT,
31
- NotebookPreferences.OUTPUT_FONT_SIZE,
32
- NotebookPreferences.OUTPUT_FONT_FAMILY,
33
- NotebookPreferences.OUTPUT_SCROLLING,
34
- NotebookPreferences.OUTPUT_WORD_WRAP,
35
- NotebookPreferences.OUTPUT_LINE_LIMIT
36
- ];
37
-
38
- export interface NotebookOutputOptions {
39
- // readonly outputNodePadding: number;
40
- readonly outputNodeLeftPadding: number;
41
- // readonly previewNodePadding: number;
42
- // readonly markdownLeftMargin: number;
43
- // readonly leftMargin: number;
44
- // readonly rightMargin: number;
45
- // readonly runGutter: number;
46
- // readonly dragAndDropEnabled: boolean;
47
- readonly fontSize: number;
48
- readonly outputFontSize?: number;
49
- readonly fontFamily: string;
50
- readonly outputFontFamily?: string;
51
- // readonly markupFontSize: number;
52
- // readonly markdownLineHeight: number;
53
- readonly outputLineHeight: number;
54
- readonly outputScrolling: boolean;
55
- readonly outputWordWrap: boolean;
56
- readonly outputLineLimit: number;
57
- // readonly outputLinkifyFilePaths: boolean;
58
- // readonly minimalError: boolean;
59
-
60
- }
61
-
62
- @injectable()
63
- export class NotebookOptionsService {
64
-
65
- @inject(PreferenceService)
66
- protected readonly preferenceService: PreferenceService;
67
-
68
- @inject(EditorPreferences)
69
- protected readonly editorPreferences: EditorPreferences;
70
-
71
- protected outputOptionsChangedEmitter = new Emitter<NotebookOutputOptions>();
72
- onDidChangeOutputOptions = this.outputOptionsChangedEmitter.event;
73
-
74
- protected fontInfo?: BareFontInfo;
75
- get editorFontInfo(): BareFontInfo {
76
- return this.getOrCreateMonacoFontInfo();
77
- }
78
-
79
- @postConstruct()
80
- protected init(): void {
81
- this.preferenceService.onPreferencesChanged(async preferenceChanges => {
82
- if (notebookOutputOptionsRelevantPreferences.some(p => p in preferenceChanges)) {
83
- this.outputOptionsChangedEmitter.fire(this.computeOutputOptions());
84
- }
85
- });
86
- }
87
-
88
- computeOutputOptions(): NotebookOutputOptions {
89
- const outputLineHeight = this.getNotebookPreferenceWithDefault<number>(NotebookPreferences.OUTPUT_LINE_HEIGHT);
90
-
91
- const fontSize = this.preferenceService.get<number>('editor.fontSize')!;
92
- const outputFontSize = this.getNotebookPreferenceWithDefault<number>(NotebookPreferences.OUTPUT_FONT_SIZE);
93
-
94
- return {
95
- fontSize,
96
- outputFontSize: outputFontSize,
97
- fontFamily: this.preferenceService.get<string>('editor.fontFamily')!,
98
- outputNodeLeftPadding: 8,
99
- outputFontFamily: this.getNotebookPreferenceWithDefault<string>(NotebookPreferences.OUTPUT_FONT_FAMILY),
100
- outputLineHeight: this.computeOutputLineHeight(outputLineHeight, outputFontSize ?? fontSize),
101
- outputScrolling: this.getNotebookPreferenceWithDefault<boolean>(NotebookPreferences.OUTPUT_SCROLLING)!,
102
- outputWordWrap: this.getNotebookPreferenceWithDefault<boolean>(NotebookPreferences.OUTPUT_WORD_WRAP)!,
103
- outputLineLimit: this.getNotebookPreferenceWithDefault<number>(NotebookPreferences.OUTPUT_LINE_LIMIT)!
104
- };
105
- }
106
-
107
- protected getNotebookPreferenceWithDefault<T>(key: string): T {
108
- return this.preferenceService.get<T>(key, notebookPreferenceSchema.properties?.[key]?.default as T);
109
- }
110
-
111
- protected computeOutputLineHeight(lineHeight: number, outputFontSize: number): number {
112
- const minimumLineHeight = 9;
113
-
114
- if (lineHeight === 0) {
115
- // use editor line height
116
- lineHeight = this.editorFontInfo.lineHeight;
117
- } else if (lineHeight < minimumLineHeight) {
118
- // Values too small to be line heights in pixels are in ems.
119
- let fontSize = outputFontSize;
120
- if (fontSize === 0) {
121
- fontSize = this.preferenceService.get<number>('editor.fontSize')!;
122
- }
123
-
124
- lineHeight = lineHeight * fontSize;
125
- }
126
-
127
- // Enforce integer, minimum constraints
128
- lineHeight = Math.round(lineHeight);
129
- if (lineHeight < minimumLineHeight) {
130
- lineHeight = minimumLineHeight;
131
- }
132
-
133
- return lineHeight;
134
- }
135
-
136
- protected getOrCreateMonacoFontInfo(): BareFontInfo {
137
- if (!this.fontInfo) {
138
- this.fontInfo = this.createFontInfo();
139
- this.editorPreferences.onPreferenceChanged(e => this.fontInfo = this.createFontInfo());
140
- }
141
- return this.fontInfo;
142
- }
143
-
144
- protected createFontInfo(): BareFontInfo {
145
- return BareFontInfo.createFromRawSettings({
146
- fontFamily: this.editorPreferences['editor.fontFamily'],
147
- fontWeight: String(this.editorPreferences['editor.fontWeight']),
148
- fontSize: this.editorPreferences['editor.fontSize'],
149
- fontLigatures: this.editorPreferences['editor.fontLigatures'],
150
- lineHeight: this.editorPreferences['editor.lineHeight'],
151
- letterSpacing: this.editorPreferences['editor.letterSpacing'],
152
- }, PixelRatio.value);
153
- }
154
-
155
- }
1
+
2
+ // *****************************************************************************
3
+ // Copyright (C) 2024 TypeFox and others.
4
+ //
5
+ // This program and the accompanying materials are made available under the
6
+ // terms of the Eclipse Public License v. 2.0 which is available at
7
+ // http://www.eclipse.org/legal/epl-2.0.
8
+ //
9
+ // This Source Code may also be made available under the following Secondary
10
+ // Licenses when the conditions for such availability set forth in the Eclipse
11
+ // Public License v. 2.0 are satisfied: GNU General Public License, version 2
12
+ // with the GNU Classpath Exception which is available at
13
+ // https://www.gnu.org/software/classpath/license.html.
14
+ //
15
+ // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
16
+ // *****************************************************************************
17
+
18
+ import { inject, injectable, postConstruct } from '@theia/core/shared/inversify';
19
+ import { PreferenceService } from '@theia/core/lib/browser';
20
+ import { Emitter } from '@theia/core';
21
+ import { NotebookPreferences, notebookPreferenceSchema } from '../contributions/notebook-preferences';
22
+ import { EditorPreferences } from '@theia/editor/lib/browser';
23
+ import { BareFontInfo } from '@theia/monaco-editor-core/esm/vs/editor/common/config/fontInfo';
24
+ import { PixelRatio } from '@theia/monaco-editor-core/esm/vs/base/browser/browser';
25
+
26
+ const notebookOutputOptionsRelevantPreferences = [
27
+ 'editor.fontSize',
28
+ 'editor.fontFamily',
29
+ NotebookPreferences.NOTEBOOK_LINE_NUMBERS,
30
+ NotebookPreferences.OUTPUT_LINE_HEIGHT,
31
+ NotebookPreferences.OUTPUT_FONT_SIZE,
32
+ NotebookPreferences.OUTPUT_FONT_FAMILY,
33
+ NotebookPreferences.OUTPUT_SCROLLING,
34
+ NotebookPreferences.OUTPUT_WORD_WRAP,
35
+ NotebookPreferences.OUTPUT_LINE_LIMIT
36
+ ];
37
+
38
+ export interface NotebookOutputOptions {
39
+ // readonly outputNodePadding: number;
40
+ readonly outputNodeLeftPadding: number;
41
+ // readonly previewNodePadding: number;
42
+ // readonly markdownLeftMargin: number;
43
+ // readonly leftMargin: number;
44
+ // readonly rightMargin: number;
45
+ // readonly runGutter: number;
46
+ // readonly dragAndDropEnabled: boolean;
47
+ readonly fontSize: number;
48
+ readonly outputFontSize?: number;
49
+ readonly fontFamily: string;
50
+ readonly outputFontFamily?: string;
51
+ // readonly markupFontSize: number;
52
+ // readonly markdownLineHeight: number;
53
+ readonly outputLineHeight: number;
54
+ readonly outputScrolling: boolean;
55
+ readonly outputWordWrap: boolean;
56
+ readonly outputLineLimit: number;
57
+ // readonly outputLinkifyFilePaths: boolean;
58
+ // readonly minimalError: boolean;
59
+
60
+ }
61
+
62
+ @injectable()
63
+ export class NotebookOptionsService {
64
+
65
+ @inject(PreferenceService)
66
+ protected readonly preferenceService: PreferenceService;
67
+
68
+ @inject(EditorPreferences)
69
+ protected readonly editorPreferences: EditorPreferences;
70
+
71
+ protected outputOptionsChangedEmitter = new Emitter<NotebookOutputOptions>();
72
+ onDidChangeOutputOptions = this.outputOptionsChangedEmitter.event;
73
+
74
+ protected fontInfo?: BareFontInfo;
75
+ get editorFontInfo(): BareFontInfo {
76
+ return this.getOrCreateMonacoFontInfo();
77
+ }
78
+
79
+ @postConstruct()
80
+ protected init(): void {
81
+ this.preferenceService.onPreferencesChanged(async preferenceChanges => {
82
+ if (notebookOutputOptionsRelevantPreferences.some(p => p in preferenceChanges)) {
83
+ this.outputOptionsChangedEmitter.fire(this.computeOutputOptions());
84
+ }
85
+ });
86
+ }
87
+
88
+ computeOutputOptions(): NotebookOutputOptions {
89
+ const outputLineHeight = this.getNotebookPreferenceWithDefault<number>(NotebookPreferences.OUTPUT_LINE_HEIGHT);
90
+
91
+ const fontSize = this.preferenceService.get<number>('editor.fontSize')!;
92
+ const outputFontSize = this.getNotebookPreferenceWithDefault<number>(NotebookPreferences.OUTPUT_FONT_SIZE);
93
+
94
+ return {
95
+ fontSize,
96
+ outputFontSize: outputFontSize,
97
+ fontFamily: this.preferenceService.get<string>('editor.fontFamily')!,
98
+ outputNodeLeftPadding: 8,
99
+ outputFontFamily: this.getNotebookPreferenceWithDefault<string>(NotebookPreferences.OUTPUT_FONT_FAMILY),
100
+ outputLineHeight: this.computeOutputLineHeight(outputLineHeight, outputFontSize ?? fontSize),
101
+ outputScrolling: this.getNotebookPreferenceWithDefault<boolean>(NotebookPreferences.OUTPUT_SCROLLING)!,
102
+ outputWordWrap: this.getNotebookPreferenceWithDefault<boolean>(NotebookPreferences.OUTPUT_WORD_WRAP)!,
103
+ outputLineLimit: this.getNotebookPreferenceWithDefault<number>(NotebookPreferences.OUTPUT_LINE_LIMIT)!
104
+ };
105
+ }
106
+
107
+ protected getNotebookPreferenceWithDefault<T>(key: string): T {
108
+ return this.preferenceService.get<T>(key, notebookPreferenceSchema.properties?.[key]?.default as T);
109
+ }
110
+
111
+ protected computeOutputLineHeight(lineHeight: number, outputFontSize: number): number {
112
+ const minimumLineHeight = 9;
113
+
114
+ if (lineHeight === 0) {
115
+ // use editor line height
116
+ lineHeight = this.editorFontInfo.lineHeight;
117
+ } else if (lineHeight < minimumLineHeight) {
118
+ // Values too small to be line heights in pixels are in ems.
119
+ let fontSize = outputFontSize;
120
+ if (fontSize === 0) {
121
+ fontSize = this.preferenceService.get<number>('editor.fontSize')!;
122
+ }
123
+
124
+ lineHeight = lineHeight * fontSize;
125
+ }
126
+
127
+ // Enforce integer, minimum constraints
128
+ lineHeight = Math.round(lineHeight);
129
+ if (lineHeight < minimumLineHeight) {
130
+ lineHeight = minimumLineHeight;
131
+ }
132
+
133
+ return lineHeight;
134
+ }
135
+
136
+ protected getOrCreateMonacoFontInfo(): BareFontInfo {
137
+ if (!this.fontInfo) {
138
+ this.fontInfo = this.createFontInfo();
139
+ this.editorPreferences.onPreferenceChanged(e => this.fontInfo = this.createFontInfo());
140
+ }
141
+ return this.fontInfo;
142
+ }
143
+
144
+ protected createFontInfo(): BareFontInfo {
145
+ return BareFontInfo.createFromRawSettings({
146
+ fontFamily: this.editorPreferences['editor.fontFamily'],
147
+ fontWeight: String(this.editorPreferences['editor.fontWeight']),
148
+ fontSize: this.editorPreferences['editor.fontSize'],
149
+ fontLigatures: this.editorPreferences['editor.fontLigatures'],
150
+ lineHeight: this.editorPreferences['editor.lineHeight'],
151
+ letterSpacing: this.editorPreferences['editor.letterSpacing'],
152
+ }, PixelRatio.value);
153
+ }
154
+
155
+ }
@@ -1,121 +1,121 @@
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
- * Copyright (c) Microsoft Corporation. All rights reserved.
18
- * Licensed under the MIT License. See License.txt in the project root for license information.
19
- *--------------------------------------------------------------------------------------------*/
20
-
21
- import { Emitter } from '@theia/core';
22
- import { inject, injectable } from '@theia/core/shared/inversify';
23
- import { Disposable } from '@theia/core/shared/vscode-languageserver-protocol';
24
- import { NotebookEditorWidgetService } from './notebook-editor-widget-service';
25
-
26
- interface RendererMessage {
27
- editorId: string;
28
- rendererId: string;
29
- message: unknown
30
- };
31
-
32
- export interface RendererMessaging extends Disposable {
33
- /**
34
- * Method called when a message is received. Should return a boolean
35
- * indicating whether a renderer received it.
36
- */
37
- receiveMessage?: (rendererId: string, message: unknown) => Promise<boolean>;
38
-
39
- /**
40
- * Sends a message to an extension from a renderer.
41
- */
42
- postMessage(rendererId: string, message: unknown): void;
43
- }
44
-
45
- @injectable()
46
- export class NotebookRendererMessagingService implements Disposable {
47
-
48
- protected readonly postMessageEmitter = new Emitter<RendererMessage>();
49
- readonly onPostMessage = this.postMessageEmitter.event;
50
-
51
- protected readonly willActivateRendererEmitter = new Emitter<string>();
52
- readonly onWillActivateRenderer = this.willActivateRendererEmitter.event;
53
-
54
- @inject(NotebookEditorWidgetService)
55
- protected readonly editorWidgetService: NotebookEditorWidgetService;
56
-
57
- protected readonly activations = new Map<string /* rendererId */, undefined | RendererMessage[]>();
58
- protected readonly scopedMessaging = new Map<string /* editorId */, RendererMessaging>();
59
-
60
- receiveMessage(editorId: string | undefined, rendererId: string, message: unknown): Promise<boolean> {
61
- if (editorId === undefined) {
62
- const sends = [...this.scopedMessaging.values()].map(e => e.receiveMessage?.(rendererId, message));
63
- return Promise.all(sends).then(values => values.some(value => !!value));
64
- }
65
-
66
- return this.scopedMessaging.get(editorId)?.receiveMessage?.(rendererId, message) ?? Promise.resolve(false);
67
- }
68
-
69
- prepare(rendererId: string): void {
70
- if (this.activations.has(rendererId)) {
71
- return;
72
- }
73
-
74
- const queue: RendererMessage[] = [];
75
- this.activations.set(rendererId, queue);
76
-
77
- Promise.all(this.willActivateRendererEmitter.fire(rendererId)).then(() => {
78
- for (const message of queue) {
79
- this.postMessageEmitter.fire(message);
80
- }
81
- this.activations.set(rendererId, undefined);
82
- });
83
- }
84
-
85
- public getScoped(editorId: string): RendererMessaging {
86
- const existing = this.scopedMessaging.get(editorId);
87
- if (existing) {
88
- return existing;
89
- }
90
-
91
- const messaging: RendererMessaging = {
92
- postMessage: (rendererId, message) => this.postMessage(editorId, rendererId, message),
93
- receiveMessage: async (rendererId, message) => {
94
- this.editorWidgetService.getNotebookEditor(editorId)?.postRendererMessage(rendererId, message);
95
- return true;
96
- },
97
- dispose: () => this.scopedMessaging.delete(editorId),
98
- };
99
-
100
- this.scopedMessaging.set(editorId, messaging);
101
- return messaging;
102
- }
103
-
104
- protected postMessage(editorId: string, rendererId: string, message: unknown): void {
105
- if (!this.activations.has(rendererId)) {
106
- this.prepare(rendererId);
107
- }
108
-
109
- const activation = this.activations.get(rendererId);
110
- const toSend = { rendererId, editorId, message };
111
- if (activation === undefined) {
112
- this.postMessageEmitter.fire(toSend);
113
- } else {
114
- activation.push(toSend);
115
- }
116
- }
117
-
118
- dispose(): void {
119
- this.postMessageEmitter.dispose();
120
- }
121
- }
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
+ * Copyright (c) Microsoft Corporation. All rights reserved.
18
+ * Licensed under the MIT License. See License.txt in the project root for license information.
19
+ *--------------------------------------------------------------------------------------------*/
20
+
21
+ import { Emitter } from '@theia/core';
22
+ import { inject, injectable } from '@theia/core/shared/inversify';
23
+ import { Disposable } from '@theia/core/shared/vscode-languageserver-protocol';
24
+ import { NotebookEditorWidgetService } from './notebook-editor-widget-service';
25
+
26
+ interface RendererMessage {
27
+ editorId: string;
28
+ rendererId: string;
29
+ message: unknown
30
+ };
31
+
32
+ export interface RendererMessaging extends Disposable {
33
+ /**
34
+ * Method called when a message is received. Should return a boolean
35
+ * indicating whether a renderer received it.
36
+ */
37
+ receiveMessage?: (rendererId: string, message: unknown) => Promise<boolean>;
38
+
39
+ /**
40
+ * Sends a message to an extension from a renderer.
41
+ */
42
+ postMessage(rendererId: string, message: unknown): void;
43
+ }
44
+
45
+ @injectable()
46
+ export class NotebookRendererMessagingService implements Disposable {
47
+
48
+ protected readonly postMessageEmitter = new Emitter<RendererMessage>();
49
+ readonly onPostMessage = this.postMessageEmitter.event;
50
+
51
+ protected readonly willActivateRendererEmitter = new Emitter<string>();
52
+ readonly onWillActivateRenderer = this.willActivateRendererEmitter.event;
53
+
54
+ @inject(NotebookEditorWidgetService)
55
+ protected readonly editorWidgetService: NotebookEditorWidgetService;
56
+
57
+ protected readonly activations = new Map<string /* rendererId */, undefined | RendererMessage[]>();
58
+ protected readonly scopedMessaging = new Map<string /* editorId */, RendererMessaging>();
59
+
60
+ receiveMessage(editorId: string | undefined, rendererId: string, message: unknown): Promise<boolean> {
61
+ if (editorId === undefined) {
62
+ const sends = [...this.scopedMessaging.values()].map(e => e.receiveMessage?.(rendererId, message));
63
+ return Promise.all(sends).then(values => values.some(value => !!value));
64
+ }
65
+
66
+ return this.scopedMessaging.get(editorId)?.receiveMessage?.(rendererId, message) ?? Promise.resolve(false);
67
+ }
68
+
69
+ prepare(rendererId: string): void {
70
+ if (this.activations.has(rendererId)) {
71
+ return;
72
+ }
73
+
74
+ const queue: RendererMessage[] = [];
75
+ this.activations.set(rendererId, queue);
76
+
77
+ Promise.all(this.willActivateRendererEmitter.fire(rendererId)).then(() => {
78
+ for (const message of queue) {
79
+ this.postMessageEmitter.fire(message);
80
+ }
81
+ this.activations.set(rendererId, undefined);
82
+ });
83
+ }
84
+
85
+ public getScoped(editorId: string): RendererMessaging {
86
+ const existing = this.scopedMessaging.get(editorId);
87
+ if (existing) {
88
+ return existing;
89
+ }
90
+
91
+ const messaging: RendererMessaging = {
92
+ postMessage: (rendererId, message) => this.postMessage(editorId, rendererId, message),
93
+ receiveMessage: async (rendererId, message) => {
94
+ this.editorWidgetService.getNotebookEditor(editorId)?.postRendererMessage(rendererId, message);
95
+ return true;
96
+ },
97
+ dispose: () => this.scopedMessaging.delete(editorId),
98
+ };
99
+
100
+ this.scopedMessaging.set(editorId, messaging);
101
+ return messaging;
102
+ }
103
+
104
+ protected postMessage(editorId: string, rendererId: string, message: unknown): void {
105
+ if (!this.activations.has(rendererId)) {
106
+ this.prepare(rendererId);
107
+ }
108
+
109
+ const activation = this.activations.get(rendererId);
110
+ const toSend = { rendererId, editorId, message };
111
+ if (activation === undefined) {
112
+ this.postMessageEmitter.fire(toSend);
113
+ } else {
114
+ activation.push(toSend);
115
+ }
116
+ }
117
+
118
+ dispose(): void {
119
+ this.postMessageEmitter.dispose();
120
+ }
121
+ }