Skip to content

Commit

Permalink
feat: support setting/resetting cursor from host (#5215)
Browse files Browse the repository at this point in the history
* Support setting/resetting cursor type from host

* add docs

* minor
  • Loading branch information
ad1992 committed May 20, 2022
1 parent 92f30f7 commit 07ebd7c
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 7 deletions.
11 changes: 10 additions & 1 deletion src/components/App.tsx
Expand Up @@ -383,6 +383,8 @@ class App extends React.Component<AppProps, AppState> {
setToastMessage: this.setToastMessage,
id: this.id,
setActiveTool: this.setActiveTool,
setCursor: this.setCursor,
resetCursor: this.resetCursor,
} as const;
if (typeof excalidrawRef === "function") {
excalidrawRef(api);
Expand Down Expand Up @@ -1921,6 +1923,13 @@ class App extends React.Component<AppProps, AppState> {
}
};

private setCursor = (cursor: string) => {
setCursor(this.canvas, cursor);
};

private resetCursor = () => {
resetCursor(this.canvas);
};
/**
* returns whether user is making a gesture with >= 2 fingers (points)
* on o touch screen (not on a trackpad). Currently only relates to Darwin
Expand Down Expand Up @@ -3013,7 +3022,7 @@ class App extends React.Component<AppProps, AppState> {
pointerDownState,
);
} else if (this.state.activeTool.type === "custom") {
setCursor(this.canvas, CURSOR_TYPE.CROSSHAIR);
setCursor(this.canvas, CURSOR_TYPE.AUTO);
} else if (this.state.activeTool.type !== "eraser") {
this.createGenericElementOnPointerDown(
this.state.activeTool.type,
Expand Down
2 changes: 2 additions & 0 deletions src/packages/excalidraw/CHANGELOG.md
Expand Up @@ -17,6 +17,8 @@ Please add the latest change on the top under the correct section.

#### Features

- Export API to [set](https://github.com/excalidraw/excalidraw/blob/master/src/packages/excalidraw/README.md#setCursor) and [reset](https://github.com/excalidraw/excalidraw/blob/master/src/packages/excalidraw/README.md#resetCursor) mouse cursor on the canvas [#5215](https://github.com/excalidraw/excalidraw/pull/5215).

- Export [`sceneCoordsToViewportCoords`](https://github.com/excalidraw/excalidraw/blob/master/src/packages/excalidraw/README.md#onPointerDown) and [`viewportCoordsToSceneCoords`](https://github.com/excalidraw/excalidraw/blob/master/src/packages/excalidraw/README.md#onPointerDown) utilities [#5187](https://github.com/excalidraw/excalidraw/pull/5187).

- Added [`useHandleLibrary`](https://github.com/excalidraw/excalidraw/blob/master/src/packages/excalidraw/README.md#useHandleLibrary) hook to automatically handle importing of libraries when `#addLibrary` URL hash key is present, and potentially for initializing library as well [#5115](https://github.com/excalidraw/excalidraw/pull/5115).
Expand Down
14 changes: 14 additions & 0 deletions src/packages/excalidraw/README_NEXT.md
Expand Up @@ -495,6 +495,8 @@ You can pass a `ref` when you want to access some excalidraw APIs. We expose the
| [id](#id) | string | Unique ID for the excalidraw component. |
| [getFiles](#getFiles) | <code>() => <a href="https://github.com/excalidraw/excalidraw/blob/master/src/types.ts#L64">files</a> </code> | This API can be used to get the files present in the scene. It may contain files that aren't referenced by any element, so if you're persisting the files to a storage, you should compare them against stored elements. |
| [setActiveTool](#setActiveTool) | <code>(tool: { type: typeof <a href="https://github.com/excalidraw/excalidraw/blob/master/src/shapes.tsx#L4">SHAPES</a>[number]["value"] &#124; "eraser" } &#124; { type: "custom"; customType: string }) => void</code> | This API can be used to set the active tool |
| [setCursor](#setCursor) | <code>(cursor: string) => void </code> | This API can be used to set customise the mouse cursor on the canvas |
| [resetCursor](#resetCursor) | <code>() => void </code> | This API can be used to reset to default mouse cursor on the canvas |

#### `readyPromise`

Expand Down Expand Up @@ -707,6 +709,18 @@ This API has the below signature. It sets the `tool` passed in param as the acti
(tool: { type: typeof <a href="https://github.com/excalidraw/excalidraw/blob/master/src/shapes.tsx#L4">SHAPES</a>[number]["value"] &#124; "eraser" } &#124; { type: "custom"; customType: string }) => void
</pre>

#### `setCursor`

This API can be used to customise the mouse cursor on the canvas and has the below signature. It sets the mouse cursor to the cursor passed in param.

<pre>
(cursor: string) => void
</pre>

#### `resetCursor`

This API can be used to reset to default mouse cursor.

#### `detectScroll`

Indicates whether Excalidraw should listen for `scroll` event on the nearest scrollable container in the DOM tree and recompute the coordinates (e.g. to correctly handle the cursor) when the component's position changes. You can disable this when you either know this doesn't affect your app or you want to take care of it yourself (calling the [`refresh()`](#ref) method).
Expand Down
38 changes: 33 additions & 5 deletions src/packages/excalidraw/example/App.js
Expand Up @@ -31,8 +31,19 @@ const {
} = window.ExcalidrawLib;

const COMMENT_SVG = (
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512">
<path d="M256 32C114.6 32 .0272 125.1 .0272 240c0 47.63 19.91 91.25 52.91 126.2c-14.88 39.5-45.87 72.88-46.37 73.25c-6.625 7-8.375 17.25-4.625 26C5.818 474.2 14.38 480 24 480c61.5 0 109.1-25.75 139.1-46.25C191.1 442.8 223.3 448 256 448c141.4 0 255.1-93.13 255.1-208S397.4 32 256 32zM256.1 400c-26.75 0-53.12-4.125-78.38-12.12l-22.75-7.125l-19.5 13.75c-14.25 10.12-33.88 21.38-57.5 29c7.375-12.12 14.37-25.75 19.88-40.25l10.62-28l-20.62-21.87C69.82 314.1 48.07 282.2 48.07 240c0-88.25 93.25-160 208-160s208 71.75 208 160S370.8 400 256.1 400z" />
<svg
xmlns="http://www.w3.org/2000/svg"
width="24"
height="24"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
stroke-width="2"
stroke-linecap="round"
stroke-linejoin="round"
class="feather feather-message-circle"
>
<path d="M21 11.5a8.38 8.38 0 0 1-.9 3.8 8.5 8.5 0 0 1-7.6 4.7 8.38 8.38 0 0 1-3.8-.9L3 21l1.9-5.7a8.38 8.38 0 0 1-.9-3.8 8.5 8.5 0 0 1 4.7-7.6 8.38 8.38 0 0 1 3.8-.9h.5a8.48 8.48 0 0 1 8 8v.5z"></path>
</svg>
);
const COMMENT_ICON_DIMENSION = 32;
Expand Down Expand Up @@ -119,12 +130,29 @@ export default function App() {
{" "}
<button
className="custom-element"
onClick={() =>
onClick={() => {
excalidrawAPI.setActiveTool({
type: "custom",
customType: "comment",
})
}
});
const url = `data:${MIME_TYPES.svg},${encodeURIComponent(
`<svg
xmlns="http://www.w3.org/2000/svg"
width="24"
height="24"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
stroke-width="2"
stroke-linecap="round"
stroke-linejoin="round"
class="feather feather-message-circle"
>
<path d="M21 11.5a8.38 8.38 0 0 1-.9 3.8 8.5 8.5 0 0 1-7.6 4.7 8.38 8.38 0 0 1-3.8-.9L3 21l1.9-5.7a8.38 8.38 0 0 1-.9-3.8 8.5 8.5 0 0 1 4.7-7.6 8.38 8.38 0 0 1 3.8-.9h.5a8.48 8.48 0 0 1 8 8v.5z"></path>
</svg>`,
)}`;
excalidrawAPI.setCursor(`url(${url}), auto`);
}}
>
{COMMENT_SVG}
</button>
Expand Down
2 changes: 2 additions & 0 deletions src/types.ts
Expand Up @@ -466,6 +466,8 @@ export type ExcalidrawImperativeAPI = {
ready: true;
id: string;
setActiveTool: InstanceType<typeof App>["setActiveTool"];
setCursor: InstanceType<typeof App>["setCursor"];
resetCursor: InstanceType<typeof App>["resetCursor"];
};

export type DeviceType = {
Expand Down
3 changes: 2 additions & 1 deletion src/utils.ts
Expand Up @@ -301,7 +301,8 @@ export const setCursorForShape = (
setEraserCursor(canvas, appState.theme);
// do nothing if image tool is selected which suggests there's
// a image-preview set as the cursor
} else if (appState.activeTool.type !== "image") {
// Ignore custom type as well and let host decide
} else if (!["image", "custom"].includes(appState.activeTool.type)) {
canvas.style.cursor = CURSOR_TYPE.CROSSHAIR;
}
};
Expand Down

2 comments on commit 07ebd7c

@vercel
Copy link

@vercel vercel bot commented on 07ebd7c May 20, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

excalidraw-package-example – ./src/packages/excalidraw

excalidraw-package-example-excalidraw.vercel.app
excalidraw-package-example-git-master-excalidraw.vercel.app
excalidraw-package-example.vercel.app

@vercel
Copy link

@vercel vercel bot commented on 07ebd7c May 20, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.