suppport disabling context menu in custom elements

This commit is contained in:
ad1992 2022-04-22 00:56:17 +05:30
parent 2e8c4d25f2
commit db9c9eb3d2
7 changed files with 24 additions and 7 deletions

View File

@ -129,6 +129,7 @@ import {
isBindingElement, isBindingElement,
isBindingElementType, isBindingElementType,
isBoundToContainer, isBoundToContainer,
isCustomElement,
isImageElement, isImageElement,
isInitializedImageElement, isInitializedImageElement,
isLinearElement, isLinearElement,
@ -3158,6 +3159,7 @@ class App extends React.Component<AppProps, AppState> {
} }
if ( if (
event.button !== POINTER_BUTTON.SECONDARY &&
this.state.activeTool.type === "selection" && this.state.activeTool.type === "selection" &&
this.props.onElementClick && this.props.onElementClick &&
hitElement hitElement
@ -5416,7 +5418,6 @@ class App extends React.Component<AppProps, AppState> {
event: React.PointerEvent<HTMLCanvasElement>, event: React.PointerEvent<HTMLCanvasElement>,
) => { ) => {
event.preventDefault(); event.preventDefault();
if ( if (
(event.nativeEvent.pointerType === "touch" || (event.nativeEvent.pointerType === "touch" ||
(event.nativeEvent.pointerType === "pen" && (event.nativeEvent.pointerType === "pen" &&
@ -5433,6 +5434,18 @@ class App extends React.Component<AppProps, AppState> {
includeLockedElements: true, includeLockedElements: true,
}); });
let disableContextMenu = false;
if (element && isCustomElement(element)) {
const config = getCustomElementConfig(
this.props.customElementsConfig,
element.customType,
);
disableContextMenu = !!config?.disableContextMenu;
}
if (disableContextMenu) {
this.contextMenuOpen = true;
return false;
}
const type = element ? "element" : "canvas"; const type = element ? "element" : "canvas";
const container = this.excalidrawContainerRef.current!; const container = this.excalidrawContainerRef.current!;

View File

@ -161,6 +161,7 @@ export const DEFAULT_CUSTOM_ELEMENT_CONFIG: Required<CustomElementConfig> = {
height: 40, height: 40,
stackedOnTop: false, stackedOnTop: false,
onCreate: () => {}, onCreate: () => {},
disableContextMenu: false,
}; };
export const MQ_MAX_WIDTH_PORTRAIT = 730; export const MQ_MAX_WIDTH_PORTRAIT = 730;
export const MQ_MAX_WIDTH_LANDSCAPE = 1000; export const MQ_MAX_WIDTH_LANDSCAPE = 1000;

View File

@ -211,6 +211,7 @@ export default function App() {
</svg>`)}`, </svg>`)}`,
width: 60, width: 60,
height: 60, height: 60,
disableContextMenu: true,
}, },
{ {
type: "custom", type: "custom",
@ -223,6 +224,7 @@ export default function App() {
transformHandles: false, transformHandles: false,
stackedOnTop: true, stackedOnTop: true,
onCreate, onCreate,
disableContextMenu: true,
}, },
{ {
type: "custom", type: "custom",

View File

@ -3,14 +3,14 @@ import {
NonDeletedExcalidrawElement, NonDeletedExcalidrawElement,
} from "../element/types"; } from "../element/types";
import { getElementAbsoluteCoords, getElementBounds } from "../element"; import { getElementAbsoluteCoords, getElementBounds } from "../element";
import { AppState, ExcalidrawProps } from "../types"; import { AppProps, AppState } from "../types";
import { isBoundToContainer } from "../element/typeChecks"; import { isBoundToContainer } from "../element/typeChecks";
import { getCustomElementConfig } from "../utils"; import { getCustomElementConfig } from "../utils";
export const getElementsWithinSelection = ( export const getElementsWithinSelection = (
elements: readonly NonDeletedExcalidrawElement[], elements: readonly NonDeletedExcalidrawElement[],
selection: NonDeletedExcalidrawElement, selection: NonDeletedExcalidrawElement,
customElementConfig: ExcalidrawProps["customElementsConfig"], customElementConfig: AppProps["customElementsConfig"],
) => { ) => {
const [selectionX1, selectionY1, selectionX2, selectionY2] = const [selectionX1, selectionY1, selectionX2, selectionY2] =
getElementAbsoluteCoords(selection); getElementAbsoluteCoords(selection);

View File

@ -1,5 +1,5 @@
import { ExcalidrawTextElement } from "../element/types"; import { ExcalidrawTextElement } from "../element/types";
import { AppClassProperties, AppState, ExcalidrawProps } from "../types"; import { AppClassProperties, AppProps, AppState } from "../types";
export type RenderConfig = { export type RenderConfig = {
// AppState values // AppState values
@ -27,7 +27,7 @@ export type RenderConfig = {
/** when exporting the behavior is slightly different (e.g. we can't use /** when exporting the behavior is slightly different (e.g. we can't use
CSS filters), and we disable render optimizations for best output */ CSS filters), and we disable render optimizations for best output */
isExporting: boolean; isExporting: boolean;
customElementsConfig?: ExcalidrawProps["customElementsConfig"]; customElementsConfig?: AppProps["customElementsConfig"];
}; };
export type SceneScroll = { export type SceneScroll = {

View File

@ -233,6 +233,7 @@ export type CustomElementConfig = {
height?: number; height?: number;
stackedOnTop: boolean; stackedOnTop: boolean;
onCreate?: (element: ExcalidrawElement) => void; onCreate?: (element: ExcalidrawElement) => void;
disableContextMenu: boolean;
}; };
export type ExcalidrawInitialDataState = Merge< export type ExcalidrawInitialDataState = Merge<
ImportedDataState, ImportedDataState,

View File

@ -11,7 +11,7 @@ import {
WINDOWS_EMOJI_FALLBACK_FONT, WINDOWS_EMOJI_FALLBACK_FONT,
} from "./constants"; } from "./constants";
import { FontFamilyValues, FontString } from "./element/types"; import { FontFamilyValues, FontString } from "./element/types";
import { AppState, DataURL, ExcalidrawProps, Zoom } from "./types"; import { AppProps, AppState, DataURL, Zoom } from "./types";
import { unstable_batchedUpdates } from "react-dom"; import { unstable_batchedUpdates } from "react-dom";
import { isDarwin } from "./keys"; import { isDarwin } from "./keys";
@ -627,7 +627,7 @@ export const getFrame = () => {
}; };
export const getCustomElementConfig = ( export const getCustomElementConfig = (
customElementConfig: ExcalidrawProps["customElementsConfig"], customElementConfig: AppProps["customElementsConfig"],
customType: string, customType: string,
) => { ) => {
if (!customElementConfig) { if (!customElementConfig) {