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