DrawioModal.tsx 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. import React, {
  2. useCallback,
  3. useEffect,
  4. useMemo,
  5. } from 'react';
  6. import { useCodeMirrorEditorIsolated } from '@growi/editor';
  7. import { useDrawioModalForEditor } from '@growi/editor/src/stores/use-drawio';
  8. import {
  9. Modal,
  10. ModalBody,
  11. } from 'reactstrap';
  12. import { getDiagramsNetLangCode } from '~/client/util/locale-utils';
  13. import { replaceFocusedDrawioWithEditor, getMarkdownDrawioMxfile } from '~/components/PageEditor/markdown-drawio-util-for-editor';
  14. import { useRendererConfig } from '~/stores/context';
  15. import { useDrawioModal } from '~/stores/modal';
  16. import { usePersonalSettings } from '~/stores/personal-settings';
  17. import loggerFactory from '~/utils/logger';
  18. import { type DrawioConfig, DrawioCommunicationHelper } from './DrawioCommunicationHelper';
  19. const logger = loggerFactory('growi:components:DrawioModal');
  20. const headerColor = '#334455';
  21. const fontFamily = "-apple-system, BlinkMacSystemFont, 'Hiragino Kaku Gothic ProN', Meiryo, sans-serif";
  22. const drawioConfig: DrawioConfig = {
  23. css: `
  24. .geMenubarContainer { background-color: ${headerColor} !important; }
  25. .geMenubar { background-color: ${headerColor} !important; }
  26. .geEditor { font-family: ${fontFamily} !important; }
  27. html td.mxPopupMenuItem {
  28. font-family: ${fontFamily} !important;
  29. font-size: 8pt !important;
  30. }
  31. `,
  32. customFonts: ['Charter'],
  33. compressXml: true,
  34. };
  35. export const DrawioModal = (): JSX.Element => {
  36. const { data: rendererConfig } = useRendererConfig();
  37. const { data: personalSettingsInfo } = usePersonalSettings({
  38. // make immutable
  39. revalidateIfStale: false,
  40. revalidateOnFocus: false,
  41. revalidateOnReconnect: false,
  42. });
  43. const { data: drawioModalData, close: closeDrawioModal } = useDrawioModal();
  44. const { data: drawioModalDataInEditor, close: closeDrawioModalInEdior } = useDrawioModalForEditor();
  45. const isOpened = drawioModalData?.isOpened ?? false;
  46. const isOpendInEditor = drawioModalDataInEditor?.isOpened ?? false;
  47. const editorKey = drawioModalDataInEditor?.editorKey ?? null;
  48. const { data: codeMirrorEditor } = useCodeMirrorEditorIsolated(editorKey);
  49. const editor = codeMirrorEditor?.view;
  50. if ((isOpened || isOpendInEditor) && editorKey == null && drawioModalData?.onSave == null) {
  51. closeDrawioModalInEdior();
  52. }
  53. const drawioUriWithParams = useMemo(() => {
  54. if (rendererConfig == null) {
  55. return undefined;
  56. }
  57. let url;
  58. try {
  59. url = new URL(rendererConfig.drawioUri);
  60. }
  61. catch (err) {
  62. logger.debug(err);
  63. return undefined;
  64. }
  65. // refs: https://desk.draw.io/support/solutions/articles/16000042546-what-url-parameters-are-supported-
  66. url.searchParams.append('spin', '1');
  67. url.searchParams.append('embed', '1');
  68. url.searchParams.append('lang', getDiagramsNetLangCode(personalSettingsInfo?.lang || 'en'));
  69. url.searchParams.append('ui', 'atlas');
  70. url.searchParams.append('configure', '1');
  71. return url;
  72. }, [rendererConfig, personalSettingsInfo?.lang]);
  73. const drawioCommunicationHelper = useMemo(() => {
  74. if (rendererConfig == null) {
  75. return undefined;
  76. }
  77. const save = editor != null ? (drawioMxFile: string) => {
  78. replaceFocusedDrawioWithEditor(editor, drawioMxFile);
  79. } : drawioModalData?.onSave;
  80. return new DrawioCommunicationHelper(
  81. rendererConfig.drawioUri,
  82. drawioConfig,
  83. { onClose: closeDrawioModal, onSave: save },
  84. );
  85. }, [closeDrawioModal, drawioModalData?.onSave, editor, rendererConfig]);
  86. const receiveMessageHandler = useCallback((event: MessageEvent) => {
  87. if (drawioModalData == null) {
  88. return;
  89. }
  90. const drawioMxFile = editor != null ? getMarkdownDrawioMxfile(editor) : drawioModalData.drawioMxFile;
  91. drawioCommunicationHelper?.onReceiveMessage(event, drawioMxFile);
  92. }, [drawioCommunicationHelper, drawioModalData, editor]);
  93. useEffect(() => {
  94. if (isOpened) {
  95. window.addEventListener('message', receiveMessageHandler);
  96. }
  97. else {
  98. window.removeEventListener('message', receiveMessageHandler);
  99. }
  100. // clean up
  101. return function() {
  102. window.removeEventListener('message', receiveMessageHandler);
  103. };
  104. }, [isOpened, receiveMessageHandler]);
  105. return (
  106. <Modal
  107. isOpen={isOpened || isOpendInEditor}
  108. toggle={() => closeDrawioModal()}
  109. backdrop="static"
  110. className="drawio-modal grw-body-only-modal-expanded"
  111. size="xl"
  112. keyboard={false}
  113. >
  114. <ModalBody className="p-0">
  115. {/* Loading spinner */}
  116. <div className="w-100 h-100 position-absolute d-flex">
  117. <div className="mx-auto my-auto">
  118. <i className="fa fa-3x fa-spinner fa-pulse mx-auto text-muted"></i>
  119. </div>
  120. </div>
  121. {/* iframe */}
  122. { drawioUriWithParams != null && (
  123. <div className="w-100 h-100 position-absolute d-flex">
  124. { isOpened && (
  125. <iframe
  126. src={drawioUriWithParams.href}
  127. className="border-0 flex-grow-1"
  128. >
  129. </iframe>
  130. ) }
  131. </div>
  132. ) }
  133. </ModalBody>
  134. </Modal>
  135. );
  136. };