Skip to content

Commit

Permalink
Ability to supply a baseUrl (#4)
Browse files Browse the repository at this point in the history
* Ability to supply a baseUrl

* Update handleEvent.ts

---------

Co-authored-by: Marc Veens <veens.marc@gmail.com>
  • Loading branch information
sabinayakc and marcveens authored Mar 14, 2024
1 parent de84d84 commit 230e9cf
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 3 deletions.
3 changes: 2 additions & 1 deletion src/DrawIoEmbed.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import React from 'react';
export const DrawIoEmbed = forwardRef<DrawIoEmbedRef, DrawIoEmbedProps>(
(props, ref) => {
const {
baseUrl,
urlParameters,
configuration,
xml,
Expand All @@ -31,7 +32,7 @@ export const DrawIoEmbed = forwardRef<DrawIoEmbedRef, DrawIoEmbedProps>(

const iframeRef = useRef<HTMLIFrameElement>(null);
const action = useActions(iframeRef);
const iframeUrl = getEmbedUrl(urlParameters, !!configuration);
const iframeUrl = getEmbedUrl(baseUrl, urlParameters, !!configuration);
const [isInitialized, setIsInitialized] = useState(false);

const messageHandler = (evt: MessageEvent) => {
Expand Down
4 changes: 4 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { useActions } from './hooks/useActions';

export type DrawIoEmbedProps = {
/**
* Base URL for draw.io embed URL. Defaults to https://embed.diagrams.net
*/
baseUrl?: string;
/**
* Parameters documented at https://www.drawio.com/doc/faq/embed-mode
*/
Expand Down
3 changes: 2 additions & 1 deletion src/utils/getEmbedUrl.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { UrlParameters } from '../types';

export const getEmbedUrl = (
baseUrl?: string,
urlParameters?: UrlParameters,
addConfiguration?: boolean
) => {
const url = new URL('/', 'https://embed.diagrams.net');
const url = new URL('/', baseUrl ?? 'https://embed.diagrams.net');
const urlSearchParams = new URLSearchParams();

urlSearchParams.append('proto', 'json');
Expand Down
2 changes: 1 addition & 1 deletion src/utils/handleEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ type EventHandler = {
};

export function handleEvent(event: MessageEvent, handlers: EventHandler) {
if (!event.origin.includes('embed.diagrams.net')) {
if (!event.origin.includes('embed.diagrams.net') && !event.origin.includes(baseUrl)) {

Check failure on line 8 in src/utils/handleEvent.ts

View workflow job for this annotation

GitHub Actions / build (16.x)

Cannot find name 'baseUrl'.

Check failure on line 8 in src/utils/handleEvent.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

Cannot find name 'baseUrl'.
return;
}

Expand Down

0 comments on commit 230e9cf

Please sign in to comment.