panel-layer.ts 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /**
  2. * Copyright (c) 2025 Bytedance Ltd. and/or its affiliates
  3. * SPDX-License-Identifier: MIT
  4. */
  5. import ReactDOM from 'react-dom';
  6. import { createElement } from 'react';
  7. import { injectable, inject } from 'inversify';
  8. import { domUtils, Disposable } from '@flowgram.ai/utils';
  9. import { Layer, PluginContext } from '@flowgram.ai/core';
  10. import { PanelLayer as PanelLayerComp } from '../components/panel-layer';
  11. import { PanelManagerConfig } from './panel-config';
  12. @injectable()
  13. export class PanelLayer extends Layer {
  14. @inject(PanelManagerConfig) private readonly panelConfig: PanelManagerConfig;
  15. @inject(PluginContext) private readonly pluginContext: PluginContext;
  16. readonly panelRoot = domUtils.createDivWithClass('gedit-flow-panel-layer');
  17. layout: JSX.Element | null = null;
  18. onReady(): void {
  19. this.panelConfig.getPopupContainer(this.pluginContext).appendChild(this.panelRoot);
  20. this.toDispose.push(
  21. Disposable.create(() => {
  22. // Remove from PopupContainer
  23. this.panelRoot.remove();
  24. })
  25. );
  26. const commonStyle = {
  27. pointerEvents: 'none',
  28. width: '100%',
  29. height: '100%',
  30. position: 'absolute',
  31. left: 0,
  32. top: 0,
  33. zIndex: 100,
  34. };
  35. domUtils.setStyle(this.panelRoot, commonStyle);
  36. }
  37. render(): JSX.Element {
  38. if (!this.layout) {
  39. const { children, ...layoutProps } = this.panelConfig.layerProps;
  40. this.layout = createElement(PanelLayerComp, layoutProps, children);
  41. }
  42. return ReactDOM.createPortal(this.layout, this.panelRoot);
  43. }
  44. }