|
|
@@ -1,6 +1,12 @@
|
|
|
import {
|
|
|
- type ReactNode, type JSX,
|
|
|
- memo, useCallback, useEffect, useMemo, useRef, useState,
|
|
|
+ type JSX,
|
|
|
+ type ReactNode,
|
|
|
+ memo,
|
|
|
+ useCallback,
|
|
|
+ useEffect,
|
|
|
+ useMemo,
|
|
|
+ useRef,
|
|
|
+ useState,
|
|
|
} from 'react';
|
|
|
|
|
|
import { debounce } from 'throttle-debounce';
|
|
|
@@ -9,35 +15,36 @@ import type { IGraphViewerGlobal } from '..';
|
|
|
import { generateMxgraphData } from '../utils/embed';
|
|
|
import { isGraphViewerGlobal } from '../utils/global';
|
|
|
|
|
|
-
|
|
|
import styles from './DrawioViewer.module.scss';
|
|
|
|
|
|
-
|
|
|
declare global {
|
|
|
// eslint-disable-next-line vars-on-top, no-var
|
|
|
var GraphViewer: IGraphViewerGlobal;
|
|
|
}
|
|
|
|
|
|
-
|
|
|
export type DrawioViewerProps = {
|
|
|
- diagramIndex: number,
|
|
|
- bol: number,
|
|
|
- eol: number,
|
|
|
- children?: ReactNode,
|
|
|
- onRenderingStart?: () => void,
|
|
|
- onRenderingUpdated?: (mxfile: string | null) => void,
|
|
|
-}
|
|
|
+ diagramIndex: number;
|
|
|
+ bol: number;
|
|
|
+ eol: number;
|
|
|
+ children?: ReactNode;
|
|
|
+ onRenderingStart?: () => void;
|
|
|
+ onRenderingUpdated?: (mxfile: string | null) => void;
|
|
|
+};
|
|
|
|
|
|
export type DrawioEditByViewerProps = {
|
|
|
- bol: number,
|
|
|
- eol: number,
|
|
|
- drawioMxFile: string,
|
|
|
-}
|
|
|
+ bol: number;
|
|
|
+ eol: number;
|
|
|
+ drawioMxFile: string;
|
|
|
+};
|
|
|
|
|
|
export const DrawioViewer = memo((props: DrawioViewerProps): JSX.Element => {
|
|
|
const {
|
|
|
- diagramIndex, bol, eol, children,
|
|
|
- onRenderingStart, onRenderingUpdated,
|
|
|
+ diagramIndex,
|
|
|
+ bol,
|
|
|
+ eol,
|
|
|
+ children,
|
|
|
+ onRenderingStart,
|
|
|
+ onRenderingUpdated,
|
|
|
} = props;
|
|
|
|
|
|
const drawioContainerRef = useRef<HTMLDivElement>(null);
|
|
|
@@ -56,7 +63,9 @@ export const DrawioViewer = memo((props: DrawioViewerProps): JSX.Element => {
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
- const mxgraphs = drawioContainerRef.current.getElementsByClassName('mxgraph') as HTMLCollectionOf<HTMLElement>;
|
|
|
+ const mxgraphs = drawioContainerRef.current.getElementsByClassName(
|
|
|
+ 'mxgraph',
|
|
|
+ ) as HTMLCollectionOf<HTMLElement>;
|
|
|
if (mxgraphs.length > 0) {
|
|
|
// This component should have only one '.mxgraph' element
|
|
|
const div = mxgraphs[0];
|
|
|
@@ -73,15 +82,17 @@ export const DrawioViewer = memo((props: DrawioViewerProps): JSX.Element => {
|
|
|
GraphViewer.prototype.lightboxZIndex = 1055; // set $zindex-modal
|
|
|
GraphViewer.prototype.toolbarZIndex = 1055; // set $zindex-modal
|
|
|
GraphViewer.createViewerForElement(div);
|
|
|
- }
|
|
|
- catch (err) {
|
|
|
+ } catch (err) {
|
|
|
setError(err);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}, []);
|
|
|
|
|
|
- const renderDrawioWithDebounce = useMemo(() => debounce(200, renderDrawio), [renderDrawio]);
|
|
|
+ const renderDrawioWithDebounce = useMemo(
|
|
|
+ () => debounce(200, renderDrawio),
|
|
|
+ [renderDrawio],
|
|
|
+ );
|
|
|
|
|
|
const mxgraphHtml = useMemo(() => {
|
|
|
setError(undefined);
|
|
|
@@ -90,17 +101,17 @@ export const DrawioViewer = memo((props: DrawioViewerProps): JSX.Element => {
|
|
|
return '';
|
|
|
}
|
|
|
|
|
|
- const code = children instanceof Array
|
|
|
- ? children
|
|
|
- .filter(elem => (typeof elem === 'string')) // omit non-string elements (e.g. br element generated by line-breaks option)
|
|
|
- .join('')
|
|
|
- : children.toString();
|
|
|
+ const code =
|
|
|
+ children instanceof Array
|
|
|
+ ? children
|
|
|
+ .filter((elem) => typeof elem === 'string') // omit non-string elements (e.g. br element generated by line-breaks option)
|
|
|
+ .join('')
|
|
|
+ : children.toString();
|
|
|
|
|
|
let mxgraphData;
|
|
|
try {
|
|
|
mxgraphData = generateMxgraphData(code);
|
|
|
- }
|
|
|
- catch (err) {
|
|
|
+ } catch (err) {
|
|
|
setError(err);
|
|
|
}
|
|
|
|
|
|
@@ -125,8 +136,8 @@ export const DrawioViewer = memo((props: DrawioViewerProps): JSX.Element => {
|
|
|
const container = drawioContainerRef.current;
|
|
|
if (container == null) return;
|
|
|
|
|
|
- const observerCallback = (mutationRecords:MutationRecord[]) => {
|
|
|
- mutationRecords.forEach((record:MutationRecord) => {
|
|
|
+ const observerCallback = (mutationRecords: MutationRecord[]) => {
|
|
|
+ mutationRecords.forEach((record: MutationRecord) => {
|
|
|
const target = record.target as HTMLElement;
|
|
|
|
|
|
const mxgraphData = target.dataset.mxgraph;
|
|
|
@@ -174,18 +185,18 @@ export const DrawioViewer = memo((props: DrawioViewerProps): JSX.Element => {
|
|
|
data-end-line-number-of-markdown={eol}
|
|
|
>
|
|
|
{/* show error */}
|
|
|
- { error != null && (
|
|
|
+ {error != null && (
|
|
|
<span className="text-muted">
|
|
|
<span className="material-symbols-outlined me-1">error</span>
|
|
|
{error.name && <strong>{error.name}: </strong>}
|
|
|
{error.message}
|
|
|
</span>
|
|
|
- ) }
|
|
|
+ )}
|
|
|
|
|
|
- { error == null && (
|
|
|
+ {error == null && (
|
|
|
// eslint-disable-next-line react/no-danger
|
|
|
<div dangerouslySetInnerHTML={{ __html: mxgraphHtml }} />
|
|
|
- ) }
|
|
|
+ )}
|
|
|
</div>
|
|
|
);
|
|
|
});
|