setDeviceType

This commit is contained in:
Zsolt Viczian 2022-03-20 18:14:47 +01:00
parent 7e7864ca3d
commit 53ba9dffd9
3 changed files with 23 additions and 8 deletions

View File

@ -14,7 +14,7 @@ import {
hasText, hasText,
} from "../scene"; } from "../scene";
import { SHAPES } from "../shapes"; import { SHAPES } from "../shapes";
import { AppState, Zoom } from "../types"; import { AppState, DeviceType, Zoom } from "../types";
import { capitalizeString, isTransparent, setCursorForShape } from "../utils"; import { capitalizeString, isTransparent, setCursorForShape } from "../utils";
import Stack from "./Stack"; import Stack from "./Stack";
import { ToolButton } from "./ToolButton"; import { ToolButton } from "./ToolButton";
@ -187,6 +187,7 @@ export const ShapesSwitcher = ({
onImageAction, onImageAction,
appState, appState,
penDetected, penDetected,
setDeviceType,
}: { }: {
canvas: HTMLCanvasElement | null; canvas: HTMLCanvasElement | null;
elementType: AppState["elementType"]; elementType: AppState["elementType"];
@ -194,6 +195,7 @@ export const ShapesSwitcher = ({
onImageAction: (data: { pointerType: PointerType | null }) => void; onImageAction: (data: { pointerType: PointerType | null }) => void;
appState: AppState; appState: AppState;
penDetected: boolean; penDetected: boolean;
setDeviceType: (obj: Partial<DeviceType>) => void;
}) => { }) => {
return ( return (
<> <>
@ -219,6 +221,7 @@ export const ShapesSwitcher = ({
onChange={({ pointerType }) => { onChange={({ pointerType }) => {
if (!penDetected && pointerType === "pen") { if (!penDetected && pointerType === "pen") {
setAppState({ penMode: true }); setAppState({ penMode: true });
setDeviceType({ penDetected: true });
} }
setAppState({ setAppState({
elementType: value, elementType: value,

View File

@ -497,6 +497,7 @@ class App extends React.Component<AppProps, AppState> {
appState={this.state} appState={this.state}
files={this.files} files={this.files}
setAppState={this.setAppState} setAppState={this.setAppState}
setDeviceType={this.setDeviceType}
actionManager={this.actionManager} actionManager={this.actionManager}
elements={this.scene.getElements()} elements={this.scene.getElements()}
onCollabButtonClick={onCollabButtonClick} onCollabButtonClick={onCollabButtonClick}
@ -902,7 +903,7 @@ class App extends React.Component<AppProps, AppState> {
// --------------------------------------------------------------------- // ---------------------------------------------------------------------
const { width, height } = const { width, height } =
this.excalidrawContainerRef.current!.getBoundingClientRect(); this.excalidrawContainerRef.current!.getBoundingClientRect();
this.deviceType = updateObject(this.deviceType, { this.setDeviceType({
isMobile: isMobile:
width < MQ_MAX_WIDTH_PORTRAIT || width < MQ_MAX_WIDTH_PORTRAIT ||
(height < MQ_MAX_HEIGHT_LANDSCAPE && (height < MQ_MAX_HEIGHT_LANDSCAPE &&
@ -918,9 +919,7 @@ class App extends React.Component<AppProps, AppState> {
`(max-width: ${MQ_MAX_WIDTH_PORTRAIT}px), (max-height: ${MQ_MAX_HEIGHT_LANDSCAPE}px) and (max-width: ${MQ_MAX_WIDTH_LANDSCAPE}px)`, `(max-width: ${MQ_MAX_WIDTH_PORTRAIT}px), (max-height: ${MQ_MAX_HEIGHT_LANDSCAPE}px) and (max-width: ${MQ_MAX_WIDTH_LANDSCAPE}px)`,
); );
const handler = () => { const handler = () => {
this.deviceType = updateObject(this.deviceType, { this.setDeviceType({ isMobile: mediaQuery.matches });
isMobile: mediaQuery.matches,
});
}; };
mediaQuery.addListener(handler); mediaQuery.addListener(handler);
this.detachIsMobileMqHandler = () => mediaQuery.removeListener(handler); this.detachIsMobileMqHandler = () => mediaQuery.removeListener(handler);
@ -1562,6 +1561,10 @@ class App extends React.Component<AppProps, AppState> {
this.setState(obj); this.setState(obj);
}; };
setDeviceType = (obj: Partial<DeviceType>): void => {
this.deviceType = updateObject(this.deviceType, obj);
};
removePointer = (event: React.PointerEvent<HTMLElement> | PointerEvent) => { removePointer = (event: React.PointerEvent<HTMLElement> | PointerEvent) => {
if (touchTimeout) { if (touchTimeout) {
this.resetContextMenuTimer(); this.resetContextMenuTimer();
@ -2866,7 +2869,7 @@ class App extends React.Component<AppProps, AppState> {
//fires only once, if pen is detected, penMode is enabled //fires only once, if pen is detected, penMode is enabled
//the user can disable this by toggling the penMode button //the user can disable this by toggling the penMode button
if (!this.deviceType.penDetected && event.pointerType === "pen") { if (!this.deviceType.penDetected && event.pointerType === "pen") {
this.deviceType = updateObject(this.deviceType, { penDetected: true }); this.setDeviceType({ penDetected: true });
this.setState({ penMode: true }); this.setState({ penMode: true });
} }
@ -2874,7 +2877,7 @@ class App extends React.Component<AppProps, AppState> {
!this.deviceType.isTouchScreen && !this.deviceType.isTouchScreen &&
["pen", "touch"].includes(event.pointerType) ["pen", "touch"].includes(event.pointerType)
) { ) {
this.deviceType = updateObject(this.deviceType, { isTouchScreen: true }); this.setDeviceType({ isTouchScreen: true });
} }
if (isPanning) { if (isPanning) {

View File

@ -8,7 +8,13 @@ import { NonDeletedExcalidrawElement } from "../element/types";
import { Language, t } from "../i18n"; import { Language, t } from "../i18n";
import { calculateScrollCenter, getSelectedElements } from "../scene"; import { calculateScrollCenter, getSelectedElements } from "../scene";
import { ExportType } from "../scene/types"; import { ExportType } from "../scene/types";
import { AppProps, AppState, ExcalidrawProps, BinaryFiles } from "../types"; import {
AppProps,
AppState,
ExcalidrawProps,
BinaryFiles,
DeviceType,
} from "../types";
import { muteFSAbortError } from "../utils"; import { muteFSAbortError } from "../utils";
import { SelectedShapeActions, ShapesSwitcher, ZoomActions } from "./Actions"; import { SelectedShapeActions, ShapesSwitcher, ZoomActions } from "./Actions";
import { BackgroundPickerAndDarkModeToggle } from "./BackgroundPickerAndDarkModeToggle"; import { BackgroundPickerAndDarkModeToggle } from "./BackgroundPickerAndDarkModeToggle";
@ -44,6 +50,7 @@ interface LayerUIProps {
files: BinaryFiles; files: BinaryFiles;
canvas: HTMLCanvasElement | null; canvas: HTMLCanvasElement | null;
setAppState: React.Component<any, AppState>["setState"]; setAppState: React.Component<any, AppState>["setState"];
setDeviceType: (obj: Partial<DeviceType>) => void;
elements: readonly NonDeletedExcalidrawElement[]; elements: readonly NonDeletedExcalidrawElement[];
onCollabButtonClick?: () => void; onCollabButtonClick?: () => void;
onLockToggle: () => void; onLockToggle: () => void;
@ -74,6 +81,7 @@ const LayerUI = ({
appState, appState,
files, files,
setAppState, setAppState,
setDeviceType,
canvas, canvas,
elements, elements,
onCollabButtonClick, onCollabButtonClick,
@ -353,6 +361,7 @@ const LayerUI = ({
}); });
}} }}
penDetected={deviceType.penDetected} penDetected={deviceType.penDetected}
setDeviceType={setDeviceType}
/> />
</Stack.Row> </Stack.Row>
</Island> </Island>