-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Only initialize notebook cell editor when in viewport (#13476)
* only initialize notebbok cell editor when in viewport Signed-off-by: Jonah Iden <jonah.iden@typefox.io> * using actual line height for editor height estimation Signed-off-by: Jonah Iden <jonah.iden@typefox.io> * fix editors sometimes not correctly filling Signed-off-by: Jonah Iden <jonah.iden@typefox.io> * rebase build fix and removed console-logs Signed-off-by: Jonah Iden <jonah.iden@typefox.io> --------- Signed-off-by: Jonah Iden <jonah.iden@typefox.io>
- Loading branch information
1 parent
3e16157
commit 79a3244
Showing
6 changed files
with
148 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
packages/notebook/src/browser/view/notebook-viewport-service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// ***************************************************************************** | ||
// Copyright (C) 2024 TypeFox and others. | ||
// | ||
// This program and the accompanying materials are made available under the | ||
// terms of the Eclipse Public License v. 2.0 which is available at | ||
// http://www.eclipse.org/legal/epl-2.0. | ||
// | ||
// This Source Code may also be made available under the following Secondary | ||
// Licenses when the conditions for such availability set forth in the Eclipse | ||
// Public License v. 2.0 are satisfied: GNU General Public License, version 2 | ||
// with the GNU Classpath Exception which is available at | ||
// https://www.gnu.org/software/classpath/license.html. | ||
// | ||
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 | ||
// ***************************************************************************** | ||
|
||
import { Disposable } from '@theia/core'; | ||
import { injectable } from '@theia/core/shared/inversify'; | ||
import { Emitter } from '@theia/core/shared/vscode-languageserver-protocol'; | ||
|
||
/** | ||
* this service is for managing the viewport and scroll state of a notebook editor. | ||
* its used both for restoring scroll state after reopening an editor and for cell to check if they are in the viewport. | ||
*/ | ||
@injectable() | ||
export class NotebookViewportService implements Disposable { | ||
|
||
protected onDidChangeViewportEmitter = new Emitter<void>(); | ||
readonly onDidChangeViewport = this.onDidChangeViewportEmitter.event; | ||
|
||
protected _viewportElement: HTMLDivElement | undefined; | ||
|
||
protected resizeObserver: ResizeObserver; | ||
|
||
set viewportElement(element: HTMLDivElement | undefined) { | ||
this._viewportElement = element; | ||
if (element) { | ||
this.onDidChangeViewportEmitter.fire(); | ||
this.resizeObserver?.disconnect(); | ||
this.resizeObserver = new ResizeObserver(() => this.onDidChangeViewportEmitter.fire()); | ||
this.resizeObserver.observe(element); | ||
} | ||
} | ||
|
||
isElementInViewport(element: HTMLElement): boolean { | ||
if (this._viewportElement) { | ||
const rect = element.getBoundingClientRect(); | ||
const viewRect = this._viewportElement.getBoundingClientRect(); | ||
return rect.top < viewRect.top ? rect.bottom > viewRect.top : rect.top < viewRect.bottom; | ||
} | ||
return false; | ||
} | ||
|
||
onScroll(e: HTMLDivElement): void { | ||
this.onDidChangeViewportEmitter.fire(); | ||
} | ||
|
||
dispose(): void { | ||
this.resizeObserver.disconnect(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters