Compare commits

..

2 Commits

Author SHA1 Message Date
dwelle
cb2bf44997 fix setting stale cursor on tool change 2023-10-10 16:01:44 +02:00
dwelle
c199686c05 feat: support props.activeTool 2023-10-05 18:17:46 +02:00
5 changed files with 74 additions and 90 deletions

View File

@@ -30,6 +30,7 @@ All `props` are *optional*.
| [`generateIdForFile`](#generateidforfile) | `function` | _ | Allows you to override `id` generation for files added on canvas | | [`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 | | [`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>` | | [`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 ### Storing custom data on Excalidraw elements
@@ -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. 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).

View File

@@ -1211,7 +1211,6 @@ class App extends React.Component<AppProps, AppState> {
} }
app={this} app={this}
isCollaborating={this.props.isCollaborating} isCollaborating={this.props.isCollaborating}
uiDisabled={this.props.ui === false}
> >
{this.props.children} {this.props.children}
</LayerUI> </LayerUI>
@@ -1238,16 +1237,14 @@ class App extends React.Component<AppProps, AppState> {
closable={this.state.toast.closable} closable={this.state.toast.closable}
/> />
)} )}
{this.state.contextMenu && {this.state.contextMenu && (
this.props.interactive !== false && <ContextMenu
this.props.ui !== false && ( items={this.state.contextMenu.items}
<ContextMenu top={this.state.contextMenu.top}
items={this.state.contextMenu.items} left={this.state.contextMenu.left}
top={this.state.contextMenu.top} actionManager={this.actionManager}
left={this.state.contextMenu.left} />
actionManager={this.actionManager} )}
/>
)}
<StaticCanvas <StaticCanvas
canvas={this.canvas} canvas={this.canvas}
rc={this.rc} rc={this.rc}
@@ -1906,6 +1903,13 @@ class App extends React.Component<AppProps, AppState> {
this.refreshDeviceState(this.excalidrawContainerRef.current); this.refreshDeviceState(this.excalidrawContainerRef.current);
} }
if (
this.props.activeTool &&
this.props.activeTool.type !== this.state.activeTool.type
) {
this.setActiveTool(this.props.activeTool);
}
if ( if (
prevState.scrollX !== this.state.scrollX || prevState.scrollX !== this.state.scrollX ||
prevState.scrollY !== this.state.scrollY prevState.scrollY !== this.state.scrollY
@@ -2111,10 +2115,6 @@ class App extends React.Component<AppProps, AppState> {
event.preventDefault(); event.preventDefault();
} }
if (this.props.interactive === false) {
return;
}
if (!didTapTwice) { if (!didTapTwice) {
didTapTwice = true; didTapTwice = true;
clearTimeout(tappedTwiceTimer); clearTimeout(tappedTwiceTimer);
@@ -2149,10 +2149,6 @@ class App extends React.Component<AppProps, AppState> {
}; };
private onTouchEnd = (event: TouchEvent) => { private onTouchEnd = (event: TouchEvent) => {
if (this.props.interactive === false) {
return;
}
this.resetContextMenuTimer(); this.resetContextMenuTimer();
if (event.touches.length > 0) { if (event.touches.length > 0) {
this.setState({ this.setState({
@@ -2169,10 +2165,6 @@ class App extends React.Component<AppProps, AppState> {
public pasteFromClipboard = withBatchedUpdates( public pasteFromClipboard = withBatchedUpdates(
async (event: ClipboardEvent | null) => { async (event: ClipboardEvent | null) => {
if (this.props.interactive === false) {
return;
}
const isPlainPaste = !!(IS_PLAIN_PASTE && event); const isPlainPaste = !!(IS_PLAIN_PASTE && event);
// #686 // #686
@@ -3151,11 +3143,7 @@ class App extends React.Component<AppProps, AppState> {
| { type: "custom"; customType: string }, | { type: "custom"; customType: string },
) => { ) => {
const nextActiveTool = updateActiveTool(this.state, tool); 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)) { if (isToolIcon(document.activeElement)) {
this.focusContainer(); this.focusContainer();
} }
@@ -3172,8 +3160,10 @@ class App extends React.Component<AppProps, AppState> {
originSnapOffset: null, originSnapOffset: null,
activeEmbeddable: null, activeEmbeddable: null,
} as const; } as const;
let nextState: AppState;
if (nextActiveTool.type !== "selection") { if (nextActiveTool.type !== "selection") {
return { nextState = {
...prevState, ...prevState,
activeTool: nextActiveTool, activeTool: nextActiveTool,
selectedElementIds: makeNextSelectedElementIds({}, prevState), selectedElementIds: makeNextSelectedElementIds({}, prevState),
@@ -3182,12 +3172,21 @@ class App extends React.Component<AppProps, AppState> {
multiElement: null, multiElement: null,
...commonResets, ...commonResets,
}; };
} else {
nextState = {
...prevState,
activeTool: nextActiveTool,
...commonResets,
};
} }
return {
...prevState, if (nextActiveTool.type === "hand") {
activeTool: nextActiveTool, setCursor(this.interactiveCanvas, CURSOR_TYPE.GRAB);
...commonResets, } else if (!isHoldingSpace) {
}; setCursorForShape(this.interactiveCanvas, nextState);
}
return nextState;
}); });
}; };
@@ -3215,10 +3214,6 @@ class App extends React.Component<AppProps, AppState> {
private onGestureStart = withBatchedUpdates((event: GestureEvent) => { private onGestureStart = withBatchedUpdates((event: GestureEvent) => {
event.preventDefault(); event.preventDefault();
if (this.props.interactive === false) {
return false;
}
// we only want to deselect on touch screens because user may have selected // we only want to deselect on touch screens because user may have selected
// elements by mistake while zooming // elements by mistake while zooming
if (this.isTouchScreenMultiTouchGesture()) { if (this.isTouchScreenMultiTouchGesture()) {
@@ -3234,10 +3229,6 @@ class App extends React.Component<AppProps, AppState> {
private onGestureChange = withBatchedUpdates((event: GestureEvent) => { private onGestureChange = withBatchedUpdates((event: GestureEvent) => {
event.preventDefault(); event.preventDefault();
if (this.props.interactive === false) {
return false;
}
// onGestureChange only has zoom factor but not the center. // onGestureChange only has zoom factor but not the center.
// If we're on iPad or iPhone, then we recognize multi-touch and will // If we're on iPad or iPhone, then we recognize multi-touch and will
// zoom in at the right location in the touchmove handler // zoom in at the right location in the touchmove handler
@@ -3269,11 +3260,6 @@ class App extends React.Component<AppProps, AppState> {
// fires only on Safari // fires only on Safari
private onGestureEnd = withBatchedUpdates((event: GestureEvent) => { private onGestureEnd = withBatchedUpdates((event: GestureEvent) => {
event.preventDefault(); event.preventDefault();
if (this.props.interactive === false) {
return false;
}
// reselect elements only on touch screens (see onGestureStart) // reselect elements only on touch screens (see onGestureStart)
if (this.isTouchScreenMultiTouchGesture()) { if (this.isTouchScreenMultiTouchGesture()) {
this.setState({ this.setState({
@@ -3855,10 +3841,6 @@ class App extends React.Component<AppProps, AppState> {
private handleCanvasPointerMove = ( private handleCanvasPointerMove = (
event: React.PointerEvent<HTMLCanvasElement>, event: React.PointerEvent<HTMLCanvasElement>,
) => { ) => {
if (this.props.interactive === false) {
return false;
}
this.savePointer(event.clientX, event.clientY, this.state.cursorButton); this.savePointer(event.clientX, event.clientY, this.state.cursorButton);
if (gesture.pointers.has(event.pointerId)) { if (gesture.pointers.has(event.pointerId)) {
@@ -4511,10 +4493,8 @@ class App extends React.Component<AppProps, AppState> {
return; return;
} }
if (this.props.interactive !== false) { if (this.handleCanvasPanUsingWheelOrSpaceDrag(event)) {
if (this.handleCanvasPanUsingWheelOrSpaceDrag(event)) { return;
return;
}
} }
this.lastPointerDownEvent = event; this.lastPointerDownEvent = event;
@@ -4546,20 +4526,14 @@ class App extends React.Component<AppProps, AppState> {
selectedElementsAreBeingDragged: false, selectedElementsAreBeingDragged: false,
}); });
if ( if (this.handleDraggingScrollBar(event, pointerDownState)) {
this.props.interactive !== false &&
this.handleDraggingScrollBar(event, pointerDownState)
) {
return; return;
} }
this.clearSelectionIfNotUsingSelection(); this.clearSelectionIfNotUsingSelection();
this.updateBindingEnabledOnPointerMove(event); this.updateBindingEnabledOnPointerMove(event);
if ( if (this.handleSelectionOnPointerDown(event, pointerDownState)) {
this.props.interactive !== false &&
this.handleSelectionOnPointerDown(event, pointerDownState)
) {
return; return;
} }
@@ -4641,15 +4615,15 @@ class App extends React.Component<AppProps, AppState> {
const onPointerMove = const onPointerMove =
this.onPointerMoveFromPointerDownHandler(pointerDownState); this.onPointerMoveFromPointerDownHandler(pointerDownState);
const onPointerUp =
this.onPointerUpFromPointerDownHandler(pointerDownState);
const onKeyDown = this.onKeyDownFromPointerDownHandler(pointerDownState);
const onKeyUp = this.onKeyUpFromPointerDownHandler(pointerDownState);
lastPointerUp = onPointerUp;
if (!this.state.viewModeEnabled || this.state.activeTool.type === "laser") { if (!this.state.viewModeEnabled || this.state.activeTool.type === "laser") {
const onPointerUp =
this.onPointerUpFromPointerDownHandler(pointerDownState);
const onKeyDown = this.onKeyDownFromPointerDownHandler(pointerDownState);
const onKeyUp = this.onKeyUpFromPointerDownHandler(pointerDownState);
lastPointerUp = onPointerUp;
window.addEventListener(EVENT.POINTER_MOVE, onPointerMove); window.addEventListener(EVENT.POINTER_MOVE, onPointerMove);
window.addEventListener(EVENT.POINTER_UP, onPointerUp); window.addEventListener(EVENT.POINTER_UP, onPointerUp);
window.addEventListener(EVENT.KEYDOWN, onKeyDown); window.addEventListener(EVENT.KEYDOWN, onKeyDown);
@@ -7844,10 +7818,6 @@ class App extends React.Component<AppProps, AppState> {
) => { ) => {
event.preventDefault(); event.preventDefault();
if (this.props.interactive === false) {
return;
}
if ( if (
(("pointerType" in event.nativeEvent && (("pointerType" in event.nativeEvent &&
event.nativeEvent.pointerType === "touch") || event.nativeEvent.pointerType === "touch") ||
@@ -8259,7 +8229,7 @@ class App extends React.Component<AppProps, AppState> {
event: WheelEvent | React.WheelEvent<HTMLDivElement | HTMLCanvasElement>, event: WheelEvent | React.WheelEvent<HTMLDivElement | HTMLCanvasElement>,
) => { ) => {
event.preventDefault(); event.preventDefault();
if (isPanning || this.props.interactive === false) { if (isPanning) {
return; return;
} }

View File

@@ -79,7 +79,6 @@ interface LayerUIProps {
children?: React.ReactNode; children?: React.ReactNode;
app: AppClassProperties; app: AppClassProperties;
isCollaborating: boolean; isCollaborating: boolean;
uiDisabled: boolean;
} }
const DefaultMainMenu: React.FC<{ const DefaultMainMenu: React.FC<{
@@ -138,7 +137,6 @@ const LayerUI = ({
children, children,
app, app,
isCollaborating, isCollaborating,
uiDisabled,
}: LayerUIProps) => { }: LayerUIProps) => {
const device = useDevice(); const device = useDevice();
const tunnels = useInitializeTunnels(); const tunnels = useInitializeTunnels();
@@ -356,10 +354,6 @@ const LayerUI = ({
const isSidebarDocked = useAtomValue(isSidebarDockedAtom, jotaiScope); const isSidebarDocked = useAtomValue(isSidebarDockedAtom, jotaiScope);
if (uiDisabled) {
return null;
}
const layerUIJSX = ( const layerUIJSX = (
<> <>
{/* ------------------------- tunneled UI ---------------------------- */} {/* ------------------------- tunneled UI ---------------------------- */}

View File

@@ -44,8 +44,7 @@ const ExcalidrawBase = (props: ExcalidrawProps) => {
children, children,
validateEmbeddable, validateEmbeddable,
renderEmbeddable, renderEmbeddable,
ui, activeTool,
interactive,
} = props; } = props;
const canvasActions = props.UIOptions?.canvasActions; const canvasActions = props.UIOptions?.canvasActions;
@@ -102,7 +101,7 @@ const ExcalidrawBase = (props: ExcalidrawProps) => {
onPointerUpdate={onPointerUpdate} onPointerUpdate={onPointerUpdate}
renderTopRightUI={renderTopRightUI} renderTopRightUI={renderTopRightUI}
langCode={langCode} langCode={langCode}
viewModeEnabled={interactive === false ? true : viewModeEnabled} viewModeEnabled={viewModeEnabled}
zenModeEnabled={zenModeEnabled} zenModeEnabled={zenModeEnabled}
gridModeEnabled={gridModeEnabled} gridModeEnabled={gridModeEnabled}
libraryReturnUrl={libraryReturnUrl} libraryReturnUrl={libraryReturnUrl}
@@ -121,8 +120,7 @@ const ExcalidrawBase = (props: ExcalidrawProps) => {
onScrollChange={onScrollChange} onScrollChange={onScrollChange}
validateEmbeddable={validateEmbeddable} validateEmbeddable={validateEmbeddable}
renderEmbeddable={renderEmbeddable} renderEmbeddable={renderEmbeddable}
ui={ui} activeTool={activeTool}
interactive={interactive}
> >
{children} {children}
</App> </App>

View File

@@ -445,8 +445,14 @@ export interface ExcalidrawProps {
element: NonDeleted<ExcalidrawEmbeddableElement>, element: NonDeleted<ExcalidrawEmbeddableElement>,
appState: AppState, appState: AppState,
) => JSX.Element | null; ) => JSX.Element | null;
interactive?: boolean; activeTool?:
ui?: boolean; | {
type: ToolType;
}
| {
type: "custom";
customType: string;
};
} }
export type SceneData = { export type SceneData = {