From e4b17e77f814651122edf03d330142c239a71f8b Mon Sep 17 00:00:00 2001 From: Zsolt Viczian Date: Thu, 3 Feb 2022 19:18:09 +0100 Subject: [PATCH] publish handleLinkNavigation to app props --- src/components/App.tsx | 8 ++++++++ src/packages/excalidraw/CHANGELOG.md | 4 ++++ src/packages/excalidraw/README_NEXT.md | 1 + src/packages/excalidraw/index.tsx | 2 ++ src/types.ts | 1 + 5 files changed, 16 insertions(+) diff --git a/src/components/App.tsx b/src/components/App.tsx index 979ad74cd..e24fadced 100644 --- a/src/components/App.tsx +++ b/src/components/App.tsx @@ -2394,6 +2394,14 @@ class App extends React.Component { if (lastPointerDownHittingLinkIcon && LastPointerUpHittingLinkIcon) { const url = this.hitLinkElement?.link; if (url) { + if (this.props.handleLinkNavigation) { + try { + this.props.handleLinkNavigation(url); + return; + } catch (error: any) { + console.error(error); + } + } const target = isLocalLink(url) ? "_self" : "_blank"; const newWindow = window.open(undefined, target); // https://mathiasbynens.github.io/rel-noopener/ diff --git a/src/packages/excalidraw/CHANGELOG.md b/src/packages/excalidraw/CHANGELOG.md index a231cf294..c53431d66 100644 --- a/src/packages/excalidraw/CHANGELOG.md +++ b/src/packages/excalidraw/CHANGELOG.md @@ -17,6 +17,10 @@ Please add the latest change on the top under the correct section. - [`exportToBlob`](https://github.com/excalidraw/excalidraw/blob/master/src/packages/excalidraw/README.md#exportToBlob) now automatically sets `appState.exportBackground` to `true` if exporting to `image/jpeg` MIME type (to ensure that alpha channel is not compressed to black color). +## Excalidraw App Props + +- `handleLinkNavigation (url: string) => void`. If provided Excalidraw will call this function to open element hyperlinks. + ### Features - Support updating library using [`updateScene`](https://github.com/excalidraw/excalidraw/blob/master/src/packages/excalidraw/README.md#updateScene) API [#4546](https://github.com/excalidraw/excalidraw/pull/4546). diff --git a/src/packages/excalidraw/README_NEXT.md b/src/packages/excalidraw/README_NEXT.md index a5f0cb9e4..2218efdbb 100644 --- a/src/packages/excalidraw/README_NEXT.md +++ b/src/packages/excalidraw/README_NEXT.md @@ -405,6 +405,7 @@ For a complete list of variables, check [theme.scss](https://github.com/excalidr | [`onLibraryChange`](#onLibraryChange) |
(items: LibraryItems) => void | Promise<any> 
| | The callback if supplied is triggered when the library is updated and receives the library items. | | [`autoFocus`](#autoFocus) | boolean | false | Implies whether to focus the Excalidraw component on page load | | [`generateIdForFile`](#generateIdForFile) | `(file: File) => string | Promise` | Allows you to override `id` generation for files added on canvas | +| [`handleLinkNavigation`](#handleLinkNavigation) | `(url: string) => void` | If provided Excalidraw will call this function to open element hyperlinks | ### Dimensions of Excalidraw diff --git a/src/packages/excalidraw/index.tsx b/src/packages/excalidraw/index.tsx index 60cc4ec20..08227d445 100644 --- a/src/packages/excalidraw/index.tsx +++ b/src/packages/excalidraw/index.tsx @@ -35,6 +35,7 @@ const Excalidraw = (props: ExcalidrawProps) => { onLibraryChange, autoFocus = false, generateIdForFile, + handleLinkNavigation, } = props; const canvasActions = props.UIOptions?.canvasActions; @@ -96,6 +97,7 @@ const Excalidraw = (props: ExcalidrawProps) => { onLibraryChange={onLibraryChange} autoFocus={autoFocus} generateIdForFile={generateIdForFile} + handleLinkNavigation={handleLinkNavigation} /> ); diff --git a/src/types.ts b/src/types.ts index f395336f0..9ba539cf4 100644 --- a/src/types.ts +++ b/src/types.ts @@ -247,6 +247,7 @@ export interface ExcalidrawProps { onLibraryChange?: (libraryItems: LibraryItems) => void | Promise; autoFocus?: boolean; generateIdForFile?: (file: File) => string | Promise; + handleLinkNavigation?: (url: string) => void; } export type SceneData = {