Skip to content

Commit

Permalink
[pr] props.activeTool
Browse files Browse the repository at this point in the history
  • Loading branch information
dwelle committed Oct 10, 2023
1 parent dd2f365 commit 66a1102
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 12 deletions.
18 changes: 17 additions & 1 deletion dev-docs/docs/@excalidraw/excalidraw/api/props/props.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ All `props` are *optional*.
| [`generateIdForFile`](#generateidforfile) | `function` | _ | Allows you to override `id` generation for files added on canvas |
| [`validateEmbeddable`](#validateEmbeddable) | string[] | `boolean | RegExp | RegExp[] | ((link: string) => boolean | undefined)` | \_ | use for custom src url validation |
| [`renderEmbeddable`](/docs/@excalidraw/excalidraw/api/props/render-props#renderEmbeddable) | `function` | \_ | Render function that can override the built-in `<iframe>` |
| [`activeTool`](#activeTool) | `object` | - | Set the active editor tool (forced) |

### Storing custom data on Excalidraw elements

Expand Down Expand Up @@ -236,4 +237,19 @@ validateEmbeddable?: boolean | string[] | RegExp | RegExp[] | ((link: string) =>

This is an optional property. By default we support a handful of well-known sites. You may allow additional sites or disallow the default ones by supplying a custom validator. If you pass `true`, all URLs will be allowed. You can also supply a list of hostnames, RegExp (or list of RegExp objects), or a function. If the function returns `undefined`, the built-in validator will be used.

Supplying a list of hostnames (with or without `www.`) is the preferred way to allow a specific list of domains.
Supplying a list of hostnames (with or without `www.`) is the preferred way to allow a specific list of domains.

### activeTool

```ts
activeTool?:
| {
type: ToolType;
}
| {
type: "custom";
customType: string;
};
```

Tool to be force-set as active. As long as the prop is set, the editor active tool will be set to this. Useful only in specific circumstances such as when UI is disabled and the editor should be limited to just one tool (such as a laser pointer).
36 changes: 25 additions & 11 deletions src/components/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1982,6 +1982,13 @@ class App extends React.Component<AppProps, AppState> {
this.refreshDeviceState(this.excalidrawContainerRef.current);
}

if (
this.props.activeTool &&
this.props.activeTool.type !== this.state.activeTool.type
) {
this.setActiveTool(this.props.activeTool);
}

if (
prevState.scrollX !== this.state.scrollX ||
prevState.scrollY !== this.state.scrollY
Expand Down Expand Up @@ -3243,11 +3250,7 @@ class App extends React.Component<AppProps, AppState> {
| { type: "custom"; customType: string; locked?: boolean },
) => {
const nextActiveTool = updateActiveTool(this.state, tool);
if (nextActiveTool.type === "hand") {
setCursor(this.interactiveCanvas, CURSOR_TYPE.GRAB);
} else if (!isHoldingSpace) {
setCursorForShape(this.interactiveCanvas, this.state);
}

if (isToolIcon(document.activeElement)) {
this.focusContainer();
}
Expand All @@ -3264,8 +3267,10 @@ class App extends React.Component<AppProps, AppState> {
originSnapOffset: null,
activeEmbeddable: null,
} as const;
let nextState: AppState;

if (nextActiveTool.type !== "selection") {
return {
nextState = {
...prevState,
activeTool: nextActiveTool,
selectedElementIds: makeNextSelectedElementIds({}, prevState),
Expand All @@ -3274,12 +3279,21 @@ class App extends React.Component<AppProps, AppState> {
multiElement: null,
...commonResets,
};
} else {
nextState = {
...prevState,
activeTool: nextActiveTool,
...commonResets,
};
}
return {
...prevState,
activeTool: nextActiveTool,
...commonResets,
};

if (nextActiveTool.type === "hand") {
setCursor(this.interactiveCanvas, CURSOR_TYPE.GRAB);
} else if (!isHoldingSpace) {
setCursorForShape(this.interactiveCanvas, nextState);
}

return nextState;
});
};

Expand Down
2 changes: 2 additions & 0 deletions src/packages/excalidraw/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ const ExcalidrawBase = (props: ExcalidrawProps) => {
renderEmbeddable,
ui,
interactive,
activeTool,
} = props;

const canvasActions = props.UIOptions?.canvasActions;
Expand Down Expand Up @@ -127,6 +128,7 @@ const ExcalidrawBase = (props: ExcalidrawProps) => {
onHomeButtonClick={onHomeButtonClick}
ui={ui}
interactive={interactive}
activeTool={activeTool}
>
{children}
</App>
Expand Down
8 changes: 8 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,14 @@ export interface ExcalidrawProps {
) => JSX.Element | null;
interactive?: boolean;
ui?: boolean;
activeTool?:
| {
type: ToolType;
}
| {
type: "custom";
customType: string;
};
}

export type SceneData = {
Expand Down

0 comments on commit 66a1102

Please sign in to comment.