Skip to content

Commit 5702da7

Browse files
authored
Merge pull request #38 from DataFlowAnalysis/fix/no-scroll-on-label-edit
Prevent page scrolling when editing a label caused by the focus event
2 parents 136be1c + 7c6fb79 commit 5702da7

File tree

2 files changed

+45
-2
lines changed

2 files changed

+45
-2
lines changed

src/common/labelEditNoScroll.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { ContainerModule } from "inversify";
2+
import {
3+
EditLabelAction,
4+
EditLabelActionHandler,
5+
EditLabelUI,
6+
SModelRootImpl,
7+
TYPES,
8+
configureActionHandler,
9+
} from "sprotty";
10+
11+
// For our use-case the sprotty container is at (0, 0) and fills the whole screen.
12+
// Scrolling is disabled using CSS which disallows scrolling from the user.
13+
// However the page might still be scrolled due to focus events.
14+
// This is the case for the default sprotty EditLabelUI.
15+
// When editing a label at a position where the edit control
16+
// of the UI would be outside the viewport (at the right or bottom)
17+
// the page would scroll to the right/bottom due to the focus event.
18+
// To circumvent this we inherit from the default EditLabelUI and change it to
19+
// scroll the page back to the page origin at (0, 0) if it has been moved due to the
20+
// focus event.
21+
22+
class NoScrollEditLabelUI extends EditLabelUI {
23+
protected override onBeforeShow(
24+
containerElement: HTMLElement,
25+
root: Readonly<SModelRootImpl>,
26+
...contextElementIds: string[]
27+
): void {
28+
super.onBeforeShow(containerElement, root, ...contextElementIds);
29+
30+
// Scroll page to 0,0 if not already there
31+
if (window.scrollX !== 0 || window.scrollY !== 0) {
32+
window.scrollTo(0, 0);
33+
}
34+
}
35+
}
36+
37+
export const noScrollLabelEditUiModule = new ContainerModule((bind, _unbind, isBound) => {
38+
const context = { bind, isBound };
39+
configureActionHandler(context, EditLabelAction.KIND, EditLabelActionHandler);
40+
bind(NoScrollEditLabelUI).toSelf().inSingletonScope();
41+
bind(TYPES.IUIExtension).toService(NoScrollEditLabelUI);
42+
});

src/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import {
1515
exportModule,
1616
hoverModule,
1717
labelEditModule,
18-
labelEditUiModule,
1918
modelSourceModule,
2019
moveModule,
2120
routingModule,
@@ -28,6 +27,7 @@ import {
2827
import { elkLayoutModule } from "sprotty-elk";
2928
import { dfdAutoLayoutModule } from "./features/autoLayout/di.config";
3029
import { dfdCommonModule } from "./common/di.config";
30+
import { noScrollLabelEditUiModule } from "./common/labelEditNoScroll";
3131
import { dfdLabelModule } from "./features/labels/di.config";
3232
import { toolPaletteModule } from "./features/toolPalette/di.config";
3333
import { serializeModule } from "./features/serialize/di.config";
@@ -68,7 +68,6 @@ container.load(
6868
zorderModule,
6969
undoRedoModule,
7070
labelEditModule,
71-
labelEditUiModule,
7271
edgeEditModule,
7372
exportModule,
7473
edgeLayoutModule,
@@ -78,6 +77,8 @@ container.load(
7877

7978
// Custom modules
8079
dfdCommonModule,
80+
noScrollLabelEditUiModule,
81+
8182
dfdAutoLayoutModule,
8283
dfdElementsModule,
8384
serializeModule,

0 commit comments

Comments
 (0)