|
| 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 | +}); |
0 commit comments