Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

imprv: Loading draw.io (diagrams.net) resources #7575

Merged
merged 3 commits into from
Apr 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions apps/app/src/components/PageEditor/DrawioModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
} from 'reactstrap';

import { getDiagramsNetLangCode } from '~/client/util/locale-utils';
import { useDrawioUri } from '~/stores/context';
import { useRendererConfig } from '~/stores/context';
import { useDrawioModal } from '~/stores/modal';
import { usePersonalSettings } from '~/stores/personal-settings';
import loggerFactory from '~/utils/logger';
Expand Down Expand Up @@ -38,7 +38,7 @@ const drawioConfig: DrawioConfig = {


export const DrawioModal = (): JSX.Element => {
const { data: drawioUri } = useDrawioUri();
const { data: rendererConfig } = useRendererConfig();
const { data: personalSettingsInfo } = usePersonalSettings({
// make immutable
revalidateIfStale: false,
Expand All @@ -50,13 +50,13 @@ export const DrawioModal = (): JSX.Element => {
const isOpened = drawioModalData?.isOpened ?? false;

const drawioUriWithParams = useMemo(() => {
if (drawioUri == null) {
if (rendererConfig == null) {
return undefined;
}

let url;
try {
url = new URL(drawioUri);
url = new URL(rendererConfig.drawioUri);
}
catch (err) {
logger.debug(err);
Expand All @@ -71,19 +71,19 @@ export const DrawioModal = (): JSX.Element => {
url.searchParams.append('configure', '1');

return url;
}, [drawioUri, personalSettingsInfo?.lang]);
}, [rendererConfig, personalSettingsInfo?.lang]);

const drawioCommunicationHelper = useMemo(() => {
if (drawioUri == null) {
if (rendererConfig == null) {
return undefined;
}

return new DrawioCommunicationHelper(
drawioUri,
rendererConfig.drawioUri,
drawioConfig,
{ onClose: closeDrawioModal, onSave: drawioModalData?.onSave },
);
}, [closeDrawioModal, drawioModalData?.onSave, drawioUri]);
}, [closeDrawioModal, drawioModalData?.onSave, rendererConfig]);

const receiveMessageHandler = useCallback((event: MessageEvent) => {
if (drawioModalData == null) {
Expand Down
22 changes: 14 additions & 8 deletions apps/app/src/components/Script/DrawioViewerScript.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import { useCallback } from 'react';

import type { IGraphViewerGlobal } from '@growi/remark-drawio';
import Script from 'next/script';
import Head from 'next/head';

import { useDrawioUri } from '~/stores/context';
import { useRendererConfig } from '~/stores/context';

declare global {
// eslint-disable-next-line vars-on-top, no-var
var GraphViewer: IGraphViewerGlobal;
}

export const DrawioViewerScript = (): JSX.Element => {
const { data: drawioUri } = useDrawioUri();
const { data: rendererConfig } = useRendererConfig();

const loadedHandler = useCallback(() => {
// disable useResizeSensor and checkVisibleState
Expand All @@ -32,11 +32,17 @@ export const DrawioViewerScript = (): JSX.Element => {
GraphViewer.processElements();
}, []);

if (rendererConfig == null) {
return <></>;
}

return (
<Script
type="text/javascript"
src={(new URL('/js/viewer.min.js', drawioUri)).toString()}
onLoad={loadedHandler}
/>
<Head>
<script
type="text/javascript" async
src={(new URL('/js/viewer.min.js', rendererConfig.drawioUri)).toString()}
onLoad={loadedHandler}
/>
</Head>
);
};
1 change: 1 addition & 0 deletions apps/app/src/interfaces/services/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export type RendererConfig = {
isIndentSizeForced: boolean,
highlightJsStyleBorder: boolean,

drawioUri: string,
plantumlUri: string | null,
blockdiagUri: string | null,
} & XssOptionConfig;
4 changes: 2 additions & 2 deletions apps/app/src/pages/[[...path]].page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import {
useIsForbidden, useIsSharedUser,
useIsEnabledStaleNotification, useIsIdenticalPath,
useIsSearchServiceConfigured, useIsSearchServiceReachable, useDisableLinkSharing,
useDrawioUri, useHackmdUri, useDefaultIndentSize, useIsIndentSizeForced,
useHackmdUri, useDefaultIndentSize, useIsIndentSizeForced,
useIsAclEnabled, useIsSearchPage, useIsEnabledAttachTitleHeader,
useCsrfToken, useIsSearchScopeChildrenAsDefault, useCurrentPathname,
useIsSlackConfigured, useRendererConfig,
Expand Down Expand Up @@ -211,7 +211,6 @@ const Page: NextPageWithLayout<Props> = (props: Props) => {
// useIsMailerSetup(props.isMailerSetup);
useIsAclEnabled(props.isAclEnabled);
// useHasSlackConfig(props.hasSlackConfig);
useDrawioUri(props.drawioUri);
useHackmdUri(props.hackmdUri);
// useNoCdn(props.noCdn);
useDefaultIndentSize(props.adminPreferredIndentSize);
Expand Down Expand Up @@ -577,6 +576,7 @@ function injectServerConfigurations(context: GetServerSidePropsContext, props: P
adminPreferredIndentSize: configManager.getConfig('markdown', 'markdown:adminPreferredIndentSize'),
isIndentSizeForced: configManager.getConfig('markdown', 'markdown:isIndentSizeForced'),

drawioUri: configManager.getConfig('crowi', 'app:drawioUri'),
plantumlUri: process.env.PLANTUML_URI ?? null,
blockdiagUri: process.env.BLOCKDIAG_URI ?? null,

Expand Down
9 changes: 2 additions & 7 deletions apps/app/src/pages/_private-legacy-pages.page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import type { CrowiRequest } from '~/interfaces/crowi-request';
import type { RendererConfig } from '~/interfaces/services/renderer';
import type { IUser, IUserHasId } from '~/interfaces/user';
import {
useCsrfToken, useCurrentUser, useDrawioUri, useIsSearchPage, useIsSearchScopeChildrenAsDefault,
useCsrfToken, useCurrentUser, useIsSearchPage, useIsSearchScopeChildrenAsDefault,
useIsSearchServiceConfigured, useIsSearchServiceReachable, useRendererConfig,
} from '~/stores/context';

Expand All @@ -29,8 +29,6 @@ type Props = CommonProps & {
isSearchServiceReachable: boolean,
isSearchScopeChildrenAsDefault: boolean,

drawioUri: string | null,

// Render config
rendererConfig: RendererConfig,

Expand All @@ -52,8 +50,6 @@ const PrivateLegacyPage: NextPage<Props> = (props: Props) => {
useIsSearchServiceReachable(props.isSearchServiceReachable);
useIsSearchScopeChildrenAsDefault(props.isSearchScopeChildrenAsDefault);

useDrawioUri(props.drawioUri);

// init sidebar config with UserUISettings and sidebarConfig
useInitSidebarConfig(props.sidebarConfig, props.userUISettings);

Expand Down Expand Up @@ -88,8 +84,6 @@ async function injectServerConfigurations(context: GetServerSidePropsContext, pr
props.isSearchServiceReachable = searchService.isReachable;
props.isSearchScopeChildrenAsDefault = configManager.getConfig('crowi', 'customize:isSearchScopeChildrenAsDefault');

props.drawioUri = configManager.getConfig('crowi', 'app:drawioUri');

props.sidebarConfig = {
isSidebarDrawerMode: configManager.getConfig('crowi', 'customize:isSidebarDrawerMode'),
isSidebarClosedAtDockMode: configManager.getConfig('crowi', 'customize:isSidebarClosedAtDockMode'),
Expand All @@ -101,6 +95,7 @@ async function injectServerConfigurations(context: GetServerSidePropsContext, pr
adminPreferredIndentSize: configManager.getConfig('markdown', 'markdown:adminPreferredIndentSize'),
isIndentSizeForced: configManager.getConfig('markdown', 'markdown:isIndentSizeForced'),

drawioUri: configManager.getConfig('crowi', 'app:drawioUri'),
plantumlUri: process.env.PLANTUML_URI ?? null,
blockdiagUri: process.env.BLOCKDIAG_URI ?? null,

Expand Down
9 changes: 2 additions & 7 deletions apps/app/src/pages/_search.page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import type { CrowiRequest } from '~/interfaces/crowi-request';
import type { RendererConfig } from '~/interfaces/services/renderer';
import type { IUser, IUserHasId } from '~/interfaces/user';
import {
useCsrfToken, useCurrentUser, useDrawioUri, useIsContainerFluid, useIsSearchPage, useIsSearchScopeChildrenAsDefault,
useCsrfToken, useCurrentUser, useIsContainerFluid, useIsSearchPage, useIsSearchScopeChildrenAsDefault,
useIsSearchServiceConfigured, useIsSearchServiceReachable, useRendererConfig, useShowPageLimitationL,
} from '~/stores/context';

Expand All @@ -32,8 +32,6 @@ type Props = CommonProps & {
isSearchServiceReachable: boolean,
isSearchScopeChildrenAsDefault: boolean,

drawioUri: string | null,

// Render config
rendererConfig: RendererConfig,

Expand All @@ -58,8 +56,6 @@ const SearchResultPage: NextPageWithLayout<Props> = (props: Props) => {
useIsSearchServiceReachable(props.isSearchServiceReachable);
useIsSearchScopeChildrenAsDefault(props.isSearchScopeChildrenAsDefault);

useDrawioUri(props.drawioUri);

// init sidebar config with UserUISettings and sidebarConfig
useInitSidebarConfig(props.sidebarConfig, props.userUISettings);

Expand Down Expand Up @@ -125,8 +121,6 @@ function injectServerConfigurations(context: GetServerSidePropsContext, props: P
props.isSearchScopeChildrenAsDefault = configManager.getConfig('crowi', 'customize:isSearchScopeChildrenAsDefault');
props.isContainerFluid = configManager.getConfig('crowi', 'customize:isContainerFluid');

props.drawioUri = configManager.getConfig('crowi', 'app:drawioUri');

props.sidebarConfig = {
isSidebarDrawerMode: configManager.getConfig('crowi', 'customize:isSidebarDrawerMode'),
isSidebarClosedAtDockMode: configManager.getConfig('crowi', 'customize:isSidebarClosedAtDockMode'),
Expand All @@ -138,6 +132,7 @@ function injectServerConfigurations(context: GetServerSidePropsContext, props: P
adminPreferredIndentSize: configManager.getConfig('markdown', 'markdown:adminPreferredIndentSize'),
isIndentSizeForced: configManager.getConfig('markdown', 'markdown:isIndentSizeForced'),

drawioUri: configManager.getConfig('crowi', 'app:drawioUri'),
plantumlUri: process.env.PLANTUML_URI ?? null,
blockdiagUri: process.env.BLOCKDIAG_URI ?? null,

Expand Down
1 change: 1 addition & 0 deletions apps/app/src/pages/me/[[...path]].page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ async function injectServerConfigurations(context: GetServerSidePropsContext, pr
adminPreferredIndentSize: configManager.getConfig('markdown', 'markdown:adminPreferredIndentSize'),
isIndentSizeForced: configManager.getConfig('markdown', 'markdown:isIndentSizeForced'),

drawioUri: configManager.getConfig('crowi', 'app:drawioUri'),
plantumlUri: process.env.PLANTUML_URI ?? null,
blockdiagUri: process.env.BLOCKDIAG_URI ?? null,

Expand Down
4 changes: 2 additions & 2 deletions apps/app/src/pages/share/[[...path]].page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import type { IShareLinkHasId } from '~/interfaces/share-link';
import type { PageDocument } from '~/server/models/page';
import {
useCurrentUser, useRendererConfig, useIsSearchPage, useCurrentPathname,
useShareLinkId, useIsSearchServiceConfigured, useIsSearchServiceReachable, useIsSearchScopeChildrenAsDefault, useDrawioUri, useIsContainerFluid,
useShareLinkId, useIsSearchServiceConfigured, useIsSearchServiceReachable, useIsSearchScopeChildrenAsDefault, useIsContainerFluid,
} from '~/stores/context';
import { useCurrentPageId, useIsNotFound } from '~/stores/page';
import loggerFactory from '~/utils/logger';
Expand Down Expand Up @@ -90,7 +90,6 @@ const SharedPage: NextPageWithLayout<Props> = (props: Props) => {
useIsSearchServiceConfigured(props.isSearchServiceConfigured);
useIsSearchServiceReachable(props.isSearchServiceReachable);
useIsSearchScopeChildrenAsDefault(props.isSearchScopeChildrenAsDefault);
useDrawioUri(props.drawioUri);
useIsContainerFluid(props.isContainerFluid);


Expand Down Expand Up @@ -156,6 +155,7 @@ function injectServerConfigurations(context: GetServerSidePropsContext, props: P
adminPreferredIndentSize: configManager.getConfig('markdown', 'markdown:adminPreferredIndentSize'),
isIndentSizeForced: configManager.getConfig('markdown', 'markdown:isIndentSizeForced'),

drawioUri: configManager.getConfig('crowi', 'app:drawioUri'),
plantumlUri: process.env.PLANTUML_URI ?? null,
blockdiagUri: process.env.BLOCKDIAG_URI ?? null,

Expand Down
20 changes: 1 addition & 19 deletions apps/app/src/pages/tags.page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { BasicLayout } from '../components/Layout/BasicLayout';
import {
useCurrentUser, useIsSearchPage,
useIsSearchServiceConfigured, useIsSearchServiceReachable,
useIsSearchScopeChildrenAsDefault, useRendererConfig,
useIsSearchScopeChildrenAsDefault,
} from '../stores/context';

import { NextPageWithLayout } from './_app.page';
Expand Down Expand Up @@ -64,8 +64,6 @@ const TagPage: NextPageWithLayout<CommonProps> = (props: Props) => {
// init sidebar config with UserUISettings and sidebarConfig
useInitSidebarConfig(props.sidebarConfig, props.userUISettings);

useRendererConfig(props.rendererConfig);

const title = generateCustomTitle(props, t('Tags'));

return (
Expand Down Expand Up @@ -139,22 +137,6 @@ function injectServerConfigurations(context: GetServerSidePropsContext, props: P
isSidebarClosedAtDockMode: configManager.getConfig('crowi', 'customize:isSidebarClosedAtDockMode'),
};

props.rendererConfig = {
isEnabledLinebreaks: configManager.getConfig('markdown', 'markdown:isEnabledLinebreaks'),
isEnabledLinebreaksInComments: configManager.getConfig('markdown', 'markdown:isEnabledLinebreaksInComments'),
adminPreferredIndentSize: configManager.getConfig('markdown', 'markdown:adminPreferredIndentSize'),
isIndentSizeForced: configManager.getConfig('markdown', 'markdown:isIndentSizeForced'),

plantumlUri: process.env.PLANTUML_URI ?? null,
blockdiagUri: process.env.BLOCKDIAG_URI ?? null,

// XSS Options
isEnabledXssPrevention: configManager.getConfig('markdown', 'markdown:rehypeSanitize:isEnabledPrevention'),
xssOption: configManager.getConfig('markdown', 'markdown:rehypeSanitize:option'),
attrWhiteList: JSON.parse(crowi.configManager.getConfig('markdown', 'markdown:rehypeSanitize:attributes')),
tagWhiteList: crowi.configManager.getConfig('markdown', 'markdown:rehypeSanitize:tagNames'),
highlightJsStyleBorder: crowi.configManager.getConfig('crowi', 'customize:highlightJsStyleBorder'),
};
}

/**
Expand Down
20 changes: 1 addition & 19 deletions apps/app/src/pages/trash.page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { BasicLayout } from '../components/Layout/BasicLayout';
import {
useCurrentUser, useCurrentPathname,
useIsSearchServiceConfigured, useIsSearchServiceReachable,
useIsSearchScopeChildrenAsDefault, useIsSearchPage, useShowPageLimitationXL, useIsGuestUser, useRendererConfig,
useIsSearchScopeChildrenAsDefault, useIsSearchPage, useShowPageLimitationXL, useIsGuestUser,
} from '../stores/context';

import type { NextPageWithLayout } from './_app.page';
Expand Down Expand Up @@ -55,8 +55,6 @@ const TrashPage: NextPageWithLayout<CommonProps> = (props: Props) => {

useShowPageLimitationXL(props.showPageLimitationXL);

useRendererConfig(props.rendererConfig);

const { data: isDrawerMode } = useDrawerMode();
const { data: isGuestUser } = useIsGuestUser();

Expand Down Expand Up @@ -128,22 +126,6 @@ function injectServerConfigurations(context: GetServerSidePropsContext, props: P
isSidebarClosedAtDockMode: configManager.getConfig('crowi', 'customize:isSidebarClosedAtDockMode'),
};

props.rendererConfig = {
isEnabledLinebreaks: configManager.getConfig('markdown', 'markdown:isEnabledLinebreaks'),
isEnabledLinebreaksInComments: configManager.getConfig('markdown', 'markdown:isEnabledLinebreaksInComments'),
adminPreferredIndentSize: configManager.getConfig('markdown', 'markdown:adminPreferredIndentSize'),
isIndentSizeForced: configManager.getConfig('markdown', 'markdown:isIndentSizeForced'),

plantumlUri: process.env.PLANTUML_URI ?? null,
blockdiagUri: process.env.BLOCKDIAG_URI ?? null,

// XSS Options
isEnabledXssPrevention: configManager.getConfig('markdown', 'markdown:rehypeSanitize:isEnabledPrevention'),
xssOption: configManager.getConfig('markdown', 'markdown:rehypeSanitize:option'),
attrWhiteList: JSON.parse(crowi.configManager.getConfig('markdown', 'markdown:rehypeSanitize:attributes')),
tagWhiteList: crowi.configManager.getConfig('markdown', 'markdown:rehypeSanitize:tagNames'),
highlightJsStyleBorder: crowi.configManager.getConfig('crowi', 'customize:highlightJsStyleBorder'),
};
}

/**
Expand Down
2 changes: 1 addition & 1 deletion apps/app/src/server/service/config-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ const ENV_VAR_NAME_TO_CONFIG_INFO = {
ns: 'crowi',
key: 'app:drawioUri',
type: ValueType.STRING,
default: null,
default: 'https://embed.diagrams.net/',
},
NCHAN_URI: {
ns: 'crowi',
Expand Down
4 changes: 0 additions & 4 deletions apps/app/src/stores/context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,6 @@ export const useRegistrationWhiteList = (initialData?: Nullable<string[]>): SWRR
return useContextSWR<Nullable<string[]>, Error>('registrationWhiteList', initialData);
};

export const useDrawioUri = (initialData?: Nullable<string>): SWRResponse<string, Error> => {
return useContextSWR('drawioUri', initialData ?? undefined, { fallbackData: 'https://embed.diagrams.net/' });
};

export const useHackmdUri = (initialData?: Nullable<string>): SWRResponse<Nullable<string>, Error> => {
return useContextSWR<Nullable<string>, Error>('hackmdUri', initialData);
};
Expand Down