Преглед изворни кода

fix declaration for GraphViewer

Yuki Takei пре 3 година
родитељ
комит
d45cf680bd

+ 4 - 6
packages/app/src/components/Script/DrawioViewerScript.tsx

@@ -1,17 +1,15 @@
 import { useCallback } from 'react';
 
-import { IGraphViewer } from '@growi/remark-drawio-plugin';
+import type { IGraphViewer } from '@growi/remark-drawio-plugin';
 import Script from 'next/script';
 
-interface Window {
-  GraphViewer: IGraphViewer
+declare global {
+  // eslint-disable-next-line vars-on-top, no-var
+  var GraphViewer: IGraphViewer;
 }
-declare const window: Window;
 
 export const DrawioViewerScript = (): JSX.Element => {
   const loadedHandler = useCallback(() => {
-    const { GraphViewer } = window;
-
     // Set z-index ($zindex-dropdown + 200) for lightbox.
     // 'lightbox' is like a modal dialog that appears when click on a drawio diagram.
     // z-index refs: https://github.com/twbs/bootstrap/blob/v4.6.2/scss/_variables.scss#L681

+ 6 - 8
packages/remark-drawio-plugin/src/components/DrawioViewer.tsx

@@ -4,7 +4,7 @@ import React, {
 
 import { debounce } from 'throttle-debounce';
 
-import { IGraphViewer } from '..';
+import type { IGraphViewer } from '..';
 import { generateMxgraphData } from '../utils/embed';
 import { isGraphViewer } from '../utils/global';
 
@@ -12,12 +12,10 @@ import { isGraphViewer } from '../utils/global';
 import styles from './DrawioViewer.module.scss';
 
 
-interface Window {
-  // declare as an optional property
-  //  because this might be undefined if before load.
-  GraphViewer?: IGraphViewer,
+declare global {
+  // eslint-disable-next-line vars-on-top, no-var
+  var GraphViewer: IGraphViewer;
 }
-declare const window: Window;
 
 
 type Props = {
@@ -41,7 +39,7 @@ export const DrawioViewer = React.memo((props: Props): JSX.Element => {
       return;
     }
 
-    if (!isGraphViewer(window.GraphViewer)) {
+    if (!isGraphViewer(GraphViewer)) {
       // Do nothing if loading has not been terminated.
       // Alternatively, GraphViewer.processElements() will be called in Script.onLoad.
       // see DrawioViewerScript.tsx
@@ -57,7 +55,7 @@ export const DrawioViewer = React.memo((props: Props): JSX.Element => {
         div.innerHTML = '';
 
         try {
-          window.GraphViewer.createViewerForElement(div);
+          GraphViewer.createViewerForElement(div);
         }
         catch (err) {
           setError(err);