@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,208 +1,208 @@
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
- import * as React from '@theia/core/shared/react';
18
- import { MarkdownRenderer } from '@theia/core/lib/browser/markdown-rendering/markdown-renderer';
19
- import { MarkdownStringImpl } from '@theia/core/lib/common/markdown-rendering/markdown-string';
20
- import { NotebookModel } from '../view-model/notebook-model';
21
- import { CellRenderer } from './notebook-cell-list-view';
22
- import { NotebookCellModel } from '../view-model/notebook-cell-model';
23
- import { CellEditor } from './notebook-cell-editor';
24
- import { inject, injectable } from '@theia/core/shared/inversify';
25
- import { MonacoEditorServices } from '@theia/monaco/lib/browser/monaco-editor';
26
- import { CommandRegistry, nls } from '@theia/core';
27
- import { NotebookContextManager } from '../service/notebook-context-manager';
28
- import { NotebookOptionsService } from '../service/notebook-options';
29
- import { NotebookCodeCellStatus } from './notebook-code-cell-view';
30
- import { NotebookEditorFindMatch, NotebookEditorFindMatchOptions } from './notebook-find-widget';
31
- import * as mark from 'advanced-mark.js';
32
-
33
- @injectable()
34
- export class NotebookMarkdownCellRenderer implements CellRenderer {
35
-
36
- @inject(MarkdownRenderer)
37
- private readonly markdownRenderer: MarkdownRenderer;
38
- @inject(MonacoEditorServices)
39
- protected readonly monacoServices: MonacoEditorServices;
40
-
41
- @inject(NotebookContextManager)
42
- protected readonly notebookContextManager: NotebookContextManager;
43
-
44
- @inject(CommandRegistry)
45
- protected readonly commandRegistry: CommandRegistry;
46
-
47
- @inject(NotebookOptionsService)
48
- protected readonly notebookOptionsService: NotebookOptionsService;
49
-
50
- render(notebookModel: NotebookModel, cell: NotebookCellModel): React.ReactNode {
51
- return <MarkdownCell
52
- markdownRenderer={this.markdownRenderer}
53
- commandRegistry={this.commandRegistry}
54
- monacoServices={this.monacoServices}
55
- notebookOptionsService={this.notebookOptionsService}
56
- cell={cell}
57
- notebookModel={notebookModel}
58
- notebookContextManager={this.notebookContextManager} />;
59
- }
60
-
61
- renderDragImage(cell: NotebookCellModel): HTMLElement {
62
- const dragImage = document.createElement('div');
63
- dragImage.style.width = this.notebookContextManager.context?.clientWidth + 'px';
64
- const markdownString = new MarkdownStringImpl(cell.source, { supportHtml: true, isTrusted: true });
65
- const markdownElement = this.markdownRenderer.render(markdownString).element;
66
- dragImage.appendChild(markdownElement);
67
- return dragImage;
68
- }
69
- }
70
-
71
- interface MarkdownCellProps {
72
- markdownRenderer: MarkdownRenderer;
73
- monacoServices: MonacoEditorServices;
74
-
75
- commandRegistry: CommandRegistry;
76
- cell: NotebookCellModel;
77
- notebookModel: NotebookModel;
78
- notebookContextManager: NotebookContextManager;
79
- notebookOptionsService: NotebookOptionsService;
80
- }
81
-
82
- function MarkdownCell({
83
- markdownRenderer, monacoServices, cell, notebookModel, notebookContextManager, notebookOptionsService, commandRegistry
84
- }: MarkdownCellProps): React.JSX.Element {
85
- const [editMode, setEditMode] = React.useState(cell.editing);
86
- let empty = false;
87
-
88
- React.useEffect(() => {
89
- const listener = cell.onDidRequestCellEditChange(cellEdit => setEditMode(cellEdit));
90
- return () => listener.dispose();
91
- }, [editMode]);
92
-
93
- React.useEffect(() => {
94
- if (!editMode) {
95
- const instance = new mark(markdownContent);
96
- cell.onMarkdownFind = options => {
97
- instance.unmark();
98
- if (empty) {
99
- return [];
100
- }
101
- return searchInMarkdown(instance, options);
102
- };
103
- return () => {
104
- cell.onMarkdownFind = undefined;
105
- instance.unmark();
106
- };
107
- }
108
- }, [editMode, cell.source]);
109
-
110
- let markdownContent: HTMLElement[] = React.useMemo(() => {
111
- const markdownString = new MarkdownStringImpl(cell.source, { supportHtml: true, isTrusted: true });
112
- const rendered = markdownRenderer.render(markdownString).element;
113
- const children: HTMLElement[] = [];
114
- rendered.childNodes.forEach(child => {
115
- if (child instanceof HTMLElement) {
116
- children.push(child);
117
- }
118
- });
119
- return children;
120
- }, [cell.source]);
121
-
122
- if (markdownContent.length === 0) {
123
- const italic = document.createElement('i');
124
- italic.className = 'theia-notebook-empty-markdown';
125
- italic.innerText = nls.localizeByDefault('Empty markdown cell, double-click or press enter to edit.');
126
- italic.style.pointerEvents = 'none';
127
- markdownContent = [italic];
128
- empty = true;
129
- }
130
-
131
- return editMode ?
132
- (<div className='theia-notebook-markdown-editor-container' key="code">
133
- <CellEditor notebookModel={notebookModel} cell={cell}
134
- monacoServices={monacoServices}
135
- notebookContextManager={notebookContextManager}
136
- fontInfo={notebookOptionsService.editorFontInfo} />
137
- <NotebookCodeCellStatus cell={cell} notebook={notebookModel}
138
- commandRegistry={commandRegistry}
139
- onClick={() => cell.requestFocusEditor()} />
140
- </div >) :
141
- (<div className='theia-notebook-markdown-content' key="markdown"
142
- onDoubleClick={() => cell.requestEdit()}
143
- ref={node => node?.replaceChildren(...markdownContent)}
144
- />);
145
- }
146
-
147
- function searchInMarkdown(instance: mark, options: NotebookEditorFindMatchOptions): NotebookEditorFindMatch[] {
148
- const matches: NotebookEditorFindMatch[] = [];
149
- const markOptions: mark.MarkOptions & mark.RegExpOptions = {
150
- className: 'theia-find-match',
151
- diacritics: false,
152
- caseSensitive: options.matchCase,
153
- acrossElements: true,
154
- separateWordSearch: false,
155
- each: node => {
156
- matches.push(new MarkdownEditorFindMatch(node));
157
- }
158
- };
159
- if (options.regex || options.wholeWord) {
160
- let search = options.search;
161
- if (options.wholeWord) {
162
- if (!options.regex) {
163
- search = escapeRegExp(search);
164
- }
165
- search = '\\b' + search + '\\b';
166
- }
167
- instance.markRegExp(new RegExp(search, options.matchCase ? '' : 'i'), markOptions);
168
- } else {
169
- instance.mark(options.search, markOptions);
170
- }
171
- return matches;
172
- }
173
-
174
- function escapeRegExp(value: string): string {
175
- return value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
176
- }
177
-
178
- class MarkdownEditorFindMatch implements NotebookEditorFindMatch {
179
-
180
- constructor(readonly node: Node) { }
181
-
182
- private _selected = false;
183
-
184
- get selected(): boolean {
185
- return this._selected;
186
- }
187
-
188
- set selected(selected: boolean) {
189
- this._selected = selected;
190
- const className = 'theia-find-match-selected';
191
- if (this.node instanceof HTMLElement) {
192
- if (selected) {
193
- this.node.classList.add(className);
194
- } else {
195
- this.node.classList.remove(className);
196
- }
197
- }
198
- }
199
-
200
- show(): void {
201
- if (this.node instanceof HTMLElement) {
202
- this.node.scrollIntoView({
203
- behavior: 'instant',
204
- block: 'center'
205
- });
206
- }
207
- }
208
- }
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
+ import * as React from '@theia/core/shared/react';
18
+ import { MarkdownRenderer } from '@theia/core/lib/browser/markdown-rendering/markdown-renderer';
19
+ import { MarkdownStringImpl } from '@theia/core/lib/common/markdown-rendering/markdown-string';
20
+ import { NotebookModel } from '../view-model/notebook-model';
21
+ import { CellRenderer } from './notebook-cell-list-view';
22
+ import { NotebookCellModel } from '../view-model/notebook-cell-model';
23
+ import { CellEditor } from './notebook-cell-editor';
24
+ import { inject, injectable } from '@theia/core/shared/inversify';
25
+ import { MonacoEditorServices } from '@theia/monaco/lib/browser/monaco-editor';
26
+ import { CommandRegistry, nls } from '@theia/core';
27
+ import { NotebookContextManager } from '../service/notebook-context-manager';
28
+ import { NotebookOptionsService } from '../service/notebook-options';
29
+ import { NotebookCodeCellStatus } from './notebook-code-cell-view';
30
+ import { NotebookEditorFindMatch, NotebookEditorFindMatchOptions } from './notebook-find-widget';
31
+ import * as mark from 'advanced-mark.js';
32
+
33
+ @injectable()
34
+ export class NotebookMarkdownCellRenderer implements CellRenderer {
35
+
36
+ @inject(MarkdownRenderer)
37
+ private readonly markdownRenderer: MarkdownRenderer;
38
+ @inject(MonacoEditorServices)
39
+ protected readonly monacoServices: MonacoEditorServices;
40
+
41
+ @inject(NotebookContextManager)
42
+ protected readonly notebookContextManager: NotebookContextManager;
43
+
44
+ @inject(CommandRegistry)
45
+ protected readonly commandRegistry: CommandRegistry;
46
+
47
+ @inject(NotebookOptionsService)
48
+ protected readonly notebookOptionsService: NotebookOptionsService;
49
+
50
+ render(notebookModel: NotebookModel, cell: NotebookCellModel): React.ReactNode {
51
+ return <MarkdownCell
52
+ markdownRenderer={this.markdownRenderer}
53
+ commandRegistry={this.commandRegistry}
54
+ monacoServices={this.monacoServices}
55
+ notebookOptionsService={this.notebookOptionsService}
56
+ cell={cell}
57
+ notebookModel={notebookModel}
58
+ notebookContextManager={this.notebookContextManager} />;
59
+ }
60
+
61
+ renderDragImage(cell: NotebookCellModel): HTMLElement {
62
+ const dragImage = document.createElement('div');
63
+ dragImage.style.width = this.notebookContextManager.context?.clientWidth + 'px';
64
+ const markdownString = new MarkdownStringImpl(cell.source, { supportHtml: true, isTrusted: true });
65
+ const markdownElement = this.markdownRenderer.render(markdownString).element;
66
+ dragImage.appendChild(markdownElement);
67
+ return dragImage;
68
+ }
69
+ }
70
+
71
+ interface MarkdownCellProps {
72
+ markdownRenderer: MarkdownRenderer;
73
+ monacoServices: MonacoEditorServices;
74
+
75
+ commandRegistry: CommandRegistry;
76
+ cell: NotebookCellModel;
77
+ notebookModel: NotebookModel;
78
+ notebookContextManager: NotebookContextManager;
79
+ notebookOptionsService: NotebookOptionsService;
80
+ }
81
+
82
+ function MarkdownCell({
83
+ markdownRenderer, monacoServices, cell, notebookModel, notebookContextManager, notebookOptionsService, commandRegistry
84
+ }: MarkdownCellProps): React.JSX.Element {
85
+ const [editMode, setEditMode] = React.useState(cell.editing);
86
+ let empty = false;
87
+
88
+ React.useEffect(() => {
89
+ const listener = cell.onDidRequestCellEditChange(cellEdit => setEditMode(cellEdit));
90
+ return () => listener.dispose();
91
+ }, [editMode]);
92
+
93
+ React.useEffect(() => {
94
+ if (!editMode) {
95
+ const instance = new mark(markdownContent);
96
+ cell.onMarkdownFind = options => {
97
+ instance.unmark();
98
+ if (empty) {
99
+ return [];
100
+ }
101
+ return searchInMarkdown(instance, options);
102
+ };
103
+ return () => {
104
+ cell.onMarkdownFind = undefined;
105
+ instance.unmark();
106
+ };
107
+ }
108
+ }, [editMode, cell.source]);
109
+
110
+ let markdownContent: HTMLElement[] = React.useMemo(() => {
111
+ const markdownString = new MarkdownStringImpl(cell.source, { supportHtml: true, isTrusted: true });
112
+ const rendered = markdownRenderer.render(markdownString).element;
113
+ const children: HTMLElement[] = [];
114
+ rendered.childNodes.forEach(child => {
115
+ if (child instanceof HTMLElement) {
116
+ children.push(child);
117
+ }
118
+ });
119
+ return children;
120
+ }, [cell.source]);
121
+
122
+ if (markdownContent.length === 0) {
123
+ const italic = document.createElement('i');
124
+ italic.className = 'theia-notebook-empty-markdown';
125
+ italic.innerText = nls.localizeByDefault('Empty markdown cell, double-click or press enter to edit.');
126
+ italic.style.pointerEvents = 'none';
127
+ markdownContent = [italic];
128
+ empty = true;
129
+ }
130
+
131
+ return editMode ?
132
+ (<div className='theia-notebook-markdown-editor-container' key="code">
133
+ <CellEditor notebookModel={notebookModel} cell={cell}
134
+ monacoServices={monacoServices}
135
+ notebookContextManager={notebookContextManager}
136
+ fontInfo={notebookOptionsService.editorFontInfo} />
137
+ <NotebookCodeCellStatus cell={cell} notebook={notebookModel}
138
+ commandRegistry={commandRegistry}
139
+ onClick={() => cell.requestFocusEditor()} />
140
+ </div >) :
141
+ (<div className='theia-notebook-markdown-content' key="markdown"
142
+ onDoubleClick={() => cell.requestEdit()}
143
+ ref={node => node?.replaceChildren(...markdownContent)}
144
+ />);
145
+ }
146
+
147
+ function searchInMarkdown(instance: mark, options: NotebookEditorFindMatchOptions): NotebookEditorFindMatch[] {
148
+ const matches: NotebookEditorFindMatch[] = [];
149
+ const markOptions: mark.MarkOptions & mark.RegExpOptions = {
150
+ className: 'theia-find-match',
151
+ diacritics: false,
152
+ caseSensitive: options.matchCase,
153
+ acrossElements: true,
154
+ separateWordSearch: false,
155
+ each: node => {
156
+ matches.push(new MarkdownEditorFindMatch(node));
157
+ }
158
+ };
159
+ if (options.regex || options.wholeWord) {
160
+ let search = options.search;
161
+ if (options.wholeWord) {
162
+ if (!options.regex) {
163
+ search = escapeRegExp(search);
164
+ }
165
+ search = '\\b' + search + '\\b';
166
+ }
167
+ instance.markRegExp(new RegExp(search, options.matchCase ? '' : 'i'), markOptions);
168
+ } else {
169
+ instance.mark(options.search, markOptions);
170
+ }
171
+ return matches;
172
+ }
173
+
174
+ function escapeRegExp(value: string): string {
175
+ return value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
176
+ }
177
+
178
+ class MarkdownEditorFindMatch implements NotebookEditorFindMatch {
179
+
180
+ constructor(readonly node: Node) { }
181
+
182
+ private _selected = false;
183
+
184
+ get selected(): boolean {
185
+ return this._selected;
186
+ }
187
+
188
+ set selected(selected: boolean) {
189
+ this._selected = selected;
190
+ const className = 'theia-find-match-selected';
191
+ if (this.node instanceof HTMLElement) {
192
+ if (selected) {
193
+ this.node.classList.add(className);
194
+ } else {
195
+ this.node.classList.remove(className);
196
+ }
197
+ }
198
+ }
199
+
200
+ show(): void {
201
+ if (this.node instanceof HTMLElement) {
202
+ this.node.scrollIntoView({
203
+ behavior: 'instant',
204
+ block: 'center'
205
+ });
206
+ }
207
+ }
208
+ }
@@ -1,61 +1,61 @@
1
- // *****************************************************************************
2
- // Copyright (C) 2024 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 } from '@theia/core';
18
- import { injectable } from '@theia/core/shared/inversify';
19
- import { Emitter } from '@theia/core/shared/vscode-languageserver-protocol';
20
-
21
- /**
22
- * this service is for managing the viewport and scroll state of a notebook editor.
23
- * its used both for restoring scroll state after reopening an editor and for cell to check if they are in the viewport.
24
- */
25
- @injectable()
26
- export class NotebookViewportService implements Disposable {
27
-
28
- protected onDidChangeViewportEmitter = new Emitter<void>();
29
- readonly onDidChangeViewport = this.onDidChangeViewportEmitter.event;
30
-
31
- protected _viewportElement: HTMLDivElement | undefined;
32
-
33
- protected resizeObserver: ResizeObserver;
34
-
35
- set viewportElement(element: HTMLDivElement | undefined) {
36
- this._viewportElement = element;
37
- if (element) {
38
- this.onDidChangeViewportEmitter.fire();
39
- this.resizeObserver?.disconnect();
40
- this.resizeObserver = new ResizeObserver(() => this.onDidChangeViewportEmitter.fire());
41
- this.resizeObserver.observe(element);
42
- }
43
- }
44
-
45
- isElementInViewport(element: HTMLElement): boolean {
46
- if (this._viewportElement) {
47
- const rect = element.getBoundingClientRect();
48
- const viewRect = this._viewportElement.getBoundingClientRect();
49
- return rect.top < viewRect.top ? rect.bottom > viewRect.top : rect.top < viewRect.bottom;
50
- }
51
- return false;
52
- }
53
-
54
- onScroll(e: HTMLDivElement): void {
55
- this.onDidChangeViewportEmitter.fire();
56
- }
57
-
58
- dispose(): void {
59
- this.resizeObserver.disconnect();
60
- }
61
- }
1
+ // *****************************************************************************
2
+ // Copyright (C) 2024 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 } from '@theia/core';
18
+ import { injectable } from '@theia/core/shared/inversify';
19
+ import { Emitter } from '@theia/core/shared/vscode-languageserver-protocol';
20
+
21
+ /**
22
+ * this service is for managing the viewport and scroll state of a notebook editor.
23
+ * its used both for restoring scroll state after reopening an editor and for cell to check if they are in the viewport.
24
+ */
25
+ @injectable()
26
+ export class NotebookViewportService implements Disposable {
27
+
28
+ protected onDidChangeViewportEmitter = new Emitter<void>();
29
+ readonly onDidChangeViewport = this.onDidChangeViewportEmitter.event;
30
+
31
+ protected _viewportElement: HTMLDivElement | undefined;
32
+
33
+ protected resizeObserver: ResizeObserver;
34
+
35
+ set viewportElement(element: HTMLDivElement | undefined) {
36
+ this._viewportElement = element;
37
+ if (element) {
38
+ this.onDidChangeViewportEmitter.fire();
39
+ this.resizeObserver?.disconnect();
40
+ this.resizeObserver = new ResizeObserver(() => this.onDidChangeViewportEmitter.fire());
41
+ this.resizeObserver.observe(element);
42
+ }
43
+ }
44
+
45
+ isElementInViewport(element: HTMLElement): boolean {
46
+ if (this._viewportElement) {
47
+ const rect = element.getBoundingClientRect();
48
+ const viewRect = this._viewportElement.getBoundingClientRect();
49
+ return rect.top < viewRect.top ? rect.bottom > viewRect.top : rect.top < viewRect.bottom;
50
+ }
51
+ return false;
52
+ }
53
+
54
+ onScroll(e: HTMLDivElement): void {
55
+ this.onDidChangeViewportEmitter.fire();
56
+ }
57
+
58
+ dispose(): void {
59
+ this.resizeObserver.disconnect();
60
+ }
61
+ }