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

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 (88) hide show
  1. package/README.md +30 -30
  2. package/lib/browser/contributions/notebook-actions-contribution.d.ts +1 -0
  3. package/lib/browser/contributions/notebook-actions-contribution.d.ts.map +1 -1
  4. package/lib/browser/contributions/notebook-actions-contribution.js +31 -5
  5. package/lib/browser/contributions/notebook-actions-contribution.js.map +1 -1
  6. package/lib/browser/contributions/notebook-cell-actions-contribution.d.ts +2 -0
  7. package/lib/browser/contributions/notebook-cell-actions-contribution.d.ts.map +1 -1
  8. package/lib/browser/contributions/notebook-cell-actions-contribution.js +41 -7
  9. package/lib/browser/contributions/notebook-cell-actions-contribution.js.map +1 -1
  10. package/lib/browser/contributions/notebook-status-bar-contribution.d.ts +14 -0
  11. package/lib/browser/contributions/notebook-status-bar-contribution.d.ts.map +1 -0
  12. package/lib/browser/contributions/notebook-status-bar-contribution.js +75 -0
  13. package/lib/browser/contributions/notebook-status-bar-contribution.js.map +1 -0
  14. package/lib/browser/notebook-editor-widget.d.ts.map +1 -1
  15. package/lib/browser/notebook-editor-widget.js +3 -5
  16. package/lib/browser/notebook-editor-widget.js.map +1 -1
  17. package/lib/browser/notebook-frontend-module.d.ts.map +1 -1
  18. package/lib/browser/notebook-frontend-module.js +3 -0
  19. package/lib/browser/notebook-frontend-module.js.map +1 -1
  20. package/lib/browser/notebook-open-handler.d.ts +3 -2
  21. package/lib/browser/notebook-open-handler.d.ts.map +1 -1
  22. package/lib/browser/notebook-open-handler.js +12 -5
  23. package/lib/browser/notebook-open-handler.js.map +1 -1
  24. package/lib/browser/view/notebook-cell-editor.d.ts +1 -0
  25. package/lib/browser/view/notebook-cell-editor.d.ts.map +1 -1
  26. package/lib/browser/view/notebook-cell-editor.js +30 -16
  27. package/lib/browser/view/notebook-cell-editor.js.map +1 -1
  28. package/lib/browser/view/notebook-cell-list-view.d.ts +6 -4
  29. package/lib/browser/view/notebook-cell-list-view.d.ts.map +1 -1
  30. package/lib/browser/view/notebook-cell-list-view.js +20 -13
  31. package/lib/browser/view/notebook-cell-list-view.js.map +1 -1
  32. package/lib/browser/view-model/notebook-cell-model.d.ts +3 -0
  33. package/lib/browser/view-model/notebook-cell-model.d.ts.map +1 -1
  34. package/lib/browser/view-model/notebook-cell-model.js +5 -0
  35. package/lib/browser/view-model/notebook-cell-model.js.map +1 -1
  36. package/package.json +8 -8
  37. package/src/browser/contributions/cell-operations.ts +44 -44
  38. package/src/browser/contributions/notebook-actions-contribution.ts +379 -350
  39. package/src/browser/contributions/notebook-cell-actions-contribution.ts +525 -487
  40. package/src/browser/contributions/notebook-color-contribution.ts +268 -268
  41. package/src/browser/contributions/notebook-context-keys.ts +113 -113
  42. package/src/browser/contributions/notebook-label-provider-contribution.ts +85 -85
  43. package/src/browser/contributions/notebook-outline-contribution.ts +114 -114
  44. package/src/browser/contributions/notebook-output-action-contribution.ts +82 -82
  45. package/src/browser/contributions/notebook-preferences.ts +92 -92
  46. package/src/browser/contributions/notebook-status-bar-contribution.ts +77 -0
  47. package/src/browser/contributions/notebook-undo-redo-handler.ts +41 -41
  48. package/src/browser/index.ts +27 -27
  49. package/src/browser/notebook-cell-resource-resolver.ts +130 -130
  50. package/src/browser/notebook-editor-widget-factory.ts +82 -82
  51. package/src/browser/notebook-editor-widget.tsx +330 -331
  52. package/src/browser/notebook-frontend-module.ts +119 -115
  53. package/src/browser/notebook-open-handler.ts +120 -114
  54. package/src/browser/notebook-output-utils.ts +119 -119
  55. package/src/browser/notebook-renderer-registry.ts +85 -85
  56. package/src/browser/notebook-type-registry.ts +54 -54
  57. package/src/browser/notebook-types.ts +186 -186
  58. package/src/browser/renderers/cell-output-webview.ts +33 -33
  59. package/src/browser/service/notebook-clipboard-service.ts +43 -43
  60. package/src/browser/service/notebook-context-manager.ts +162 -162
  61. package/src/browser/service/notebook-editor-widget-service.ts +101 -101
  62. package/src/browser/service/notebook-execution-service.ts +139 -139
  63. package/src/browser/service/notebook-execution-state-service.ts +311 -311
  64. package/src/browser/service/notebook-kernel-history-service.ts +124 -124
  65. package/src/browser/service/notebook-kernel-quick-pick-service.ts +479 -479
  66. package/src/browser/service/notebook-kernel-service.ts +357 -357
  67. package/src/browser/service/notebook-model-resolver-service.ts +160 -160
  68. package/src/browser/service/notebook-monaco-text-model-service.ts +48 -48
  69. package/src/browser/service/notebook-options.ts +155 -155
  70. package/src/browser/service/notebook-renderer-messaging-service.ts +121 -121
  71. package/src/browser/service/notebook-service.ts +215 -215
  72. package/src/browser/style/index.css +483 -471
  73. package/src/browser/view/notebook-cell-editor.tsx +263 -247
  74. package/src/browser/view/notebook-cell-list-view.tsx +279 -262
  75. package/src/browser/view/notebook-cell-toolbar-factory.tsx +102 -102
  76. package/src/browser/view/notebook-cell-toolbar.tsx +74 -74
  77. package/src/browser/view/notebook-code-cell-view.tsx +350 -350
  78. package/src/browser/view/notebook-find-widget.tsx +335 -335
  79. package/src/browser/view/notebook-main-toolbar.tsx +235 -235
  80. package/src/browser/view/notebook-markdown-cell-view.tsx +208 -208
  81. package/src/browser/view/notebook-viewport-service.ts +61 -61
  82. package/src/browser/view-model/notebook-cell-model.ts +473 -466
  83. package/src/browser/view-model/notebook-cell-output-model.ts +100 -100
  84. package/src/browser/view-model/notebook-model.ts +550 -550
  85. package/src/common/index.ts +18 -18
  86. package/src/common/notebook-common.ts +337 -337
  87. package/src/common/notebook-protocol.ts +35 -35
  88. 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
+ }