diff --git a/src/components/App.tsx b/src/components/App.tsx index 69963764..0f07de3f 100644 --- a/src/components/App.tsx +++ b/src/components/App.tsx @@ -2255,10 +2255,7 @@ class App extends React.Component { touchTimeout = window.setTimeout(() => { touchTimeout = 0; if (!invalidateContextMenu) { - this.openContextMenu({ - clientX: event.clientX, - clientY: event.clientY, - }); + this.handleCanvasContextMenu(event); } }, TOUCH_CTX_MENU_TIMEOUT); } @@ -3660,7 +3657,19 @@ class App extends React.Component { event: React.PointerEvent, ) => { event.preventDefault(); - this.openContextMenu(event); + + const { x, y } = viewportCoordsToSceneCoords(event, this.state); + const element = this.getElementAtPosition(x, y); + + const type = element ? "element" : "canvas"; + + if (element && !this.state.selectedElementIds[element.id]) { + this.setState({ selectedElementIds: { [element.id]: true } }, () => { + this._openContextMenu(event, type); + }); + } else { + this._openContextMenu(event, type); + } }; private maybeDragNewGenericElement = ( @@ -3750,18 +3759,17 @@ class App extends React.Component { return false; }; - private openContextMenu = ({ - clientX, - clientY, - }: { - clientX: number; - clientY: number; - }) => { - const { x, y } = viewportCoordsToSceneCoords( - { clientX, clientY }, - this.state, - ); - + /** @private use this.handleCanvasContextMenu */ + private _openContextMenu = ( + { + clientX, + clientY, + }: { + clientX: number; + clientY: number; + }, + type: "canvas" | "element", + ) => { const maybeGroupAction = actionGroup.contextItemPredicate!( this.actionManager.getElementsIncludingDeleted(), this.actionManager.getAppState(), @@ -3777,7 +3785,7 @@ class App extends React.Component { const _isMobile = isMobile(); const elements = this.scene.getElements(); - const element = this.getElementAtPosition(x, y); + const options: ContextMenuOption[] = []; if (probablySupportsClipboardBlob && elements.length > 0) { options.push(actionCopyAsPng); @@ -3786,7 +3794,7 @@ class App extends React.Component { if (probablySupportsClipboardWriteText && elements.length > 0) { options.push(actionCopyAsSvg); } - if (!element) { + if (type === "canvas") { const viewModeOptions = [ ...options, typeof this.props.gridModeEnabled === "undefined" && @@ -3850,10 +3858,6 @@ class App extends React.Component { return; } - if (!this.state.selectedElementIds[element.id]) { - this.setState({ selectedElementIds: { [element.id]: true } }); - } - if (this.state.viewModeEnabled) { ContextMenu.push({ options: [navigator.clipboard && actionCopy, ...options],