-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #38 from DataFlowAnalysis/fix/no-scroll-on-label-edit
Prevent page scrolling when editing a label caused by the focus event
- Loading branch information
Showing
2 changed files
with
45 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { ContainerModule } from "inversify"; | ||
import { | ||
EditLabelAction, | ||
EditLabelActionHandler, | ||
EditLabelUI, | ||
SModelRootImpl, | ||
TYPES, | ||
configureActionHandler, | ||
} from "sprotty"; | ||
|
||
// For our use-case the sprotty container is at (0, 0) and fills the whole screen. | ||
// Scrolling is disabled using CSS which disallows scrolling from the user. | ||
// However the page might still be scrolled due to focus events. | ||
// This is the case for the default sprotty EditLabelUI. | ||
// When editing a label at a position where the edit control | ||
// of the UI would be outside the viewport (at the right or bottom) | ||
// the page would scroll to the right/bottom due to the focus event. | ||
// To circumvent this we inherit from the default EditLabelUI and change it to | ||
// scroll the page back to the page origin at (0, 0) if it has been moved due to the | ||
// focus event. | ||
|
||
class NoScrollEditLabelUI extends EditLabelUI { | ||
protected override onBeforeShow( | ||
containerElement: HTMLElement, | ||
root: Readonly<SModelRootImpl>, | ||
...contextElementIds: string[] | ||
): void { | ||
super.onBeforeShow(containerElement, root, ...contextElementIds); | ||
|
||
// Scroll page to 0,0 if not already there | ||
if (window.scrollX !== 0 || window.scrollY !== 0) { | ||
window.scrollTo(0, 0); | ||
} | ||
} | ||
} | ||
|
||
export const noScrollLabelEditUiModule = new ContainerModule((bind, _unbind, isBound) => { | ||
const context = { bind, isBound }; | ||
configureActionHandler(context, EditLabelAction.KIND, EditLabelActionHandler); | ||
bind(NoScrollEditLabelUI).toSelf().inSingletonScope(); | ||
bind(TYPES.IUIExtension).toService(NoScrollEditLabelUI); | ||
}); |
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