DrawioViewerScript.tsx 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import type { IGraphViewerGlobal } from '@growi/remark-drawio';
  2. import Head from 'next/head';
  3. import { type JSX, useCallback } from 'react';
  4. import { generateViewerMinJsUrl } from './use-viewer-min-js-url';
  5. declare global {
  6. // eslint-disable-next-line vars-on-top, no-var
  7. var GraphViewer: IGraphViewerGlobal;
  8. }
  9. type Props = {
  10. drawioUri: string;
  11. };
  12. export const DrawioViewerScript = ({ drawioUri }: Props): JSX.Element => {
  13. const loadedHandler = useCallback(() => {
  14. // disable useResizeSensor and checkVisibleState
  15. // for preventing resize event by viewer-static.min.js
  16. GraphViewer.useResizeSensor = false;
  17. GraphViewer.prototype.checkVisibleState = false;
  18. // Set responsive option.
  19. // refs: https://github.com/jgraph/drawio/blob/v13.9.1/src/main/webapp/js/diagramly/GraphViewer.js#L89-L95
  20. // GraphViewer.prototype.responsive = true;
  21. // Set z-index ($zindex-dropdown + 200) for lightbox.
  22. // 'lightbox' is like a modal dialog that appears when click on a drawio diagram.
  23. // z-index refs: https://github.com/twbs/bootstrap/blob/v4.6.2/scss/_variables.scss#L681
  24. GraphViewer.prototype.lightboxZIndex = 1200;
  25. GraphViewer.prototype.toolbarZIndex = 1200;
  26. GraphViewer.processElements();
  27. }, []);
  28. // Return empty element if drawioUri is not provided to avoid Invalid URL error
  29. if (!drawioUri) {
  30. return <></>;
  31. }
  32. const viewerMinJsSrc = generateViewerMinJsUrl(drawioUri);
  33. return (
  34. <Head>
  35. <script
  36. type="text/javascript"
  37. async
  38. src={viewerMinJsSrc}
  39. onLoad={loadedHandler}
  40. />
  41. </Head>
  42. );
  43. };