Skip to content

Commit

Permalink
refactor: refactor event globals to differentiate from lastPointerUp (
Browse files Browse the repository at this point in the history
  • Loading branch information
dwelle authored Oct 4, 2023
1 parent 8b83804 commit fa33aa0
Showing 1 changed file with 37 additions and 25 deletions.
62 changes: 37 additions & 25 deletions src/components/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -491,8 +491,9 @@ class App extends React.Component<AppProps, AppState> {
private iFrameRefs = new Map<ExcalidrawElement["id"], HTMLIFrameElement>();

hitLinkElement?: NonDeletedExcalidrawElement;
lastPointerDown: React.PointerEvent<HTMLElement> | null = null;
lastPointerUp: React.PointerEvent<HTMLElement> | PointerEvent | null = null;
lastPointerDownEvent: React.PointerEvent<HTMLElement> | null = null;
lastPointerUpEvent: React.PointerEvent<HTMLElement> | PointerEvent | null =
null;
lastViewportPosition = { x: 0, y: 0 };

constructor(props: AppProps) {
Expand Down Expand Up @@ -3736,10 +3737,10 @@ class App extends React.Component<AppProps, AppState> {
isTouchScreen: boolean,
) => {
const draggedDistance = distance2d(
this.lastPointerDown!.clientX,
this.lastPointerDown!.clientY,
this.lastPointerUp!.clientX,
this.lastPointerUp!.clientY,
this.lastPointerDownEvent!.clientX,
this.lastPointerDownEvent!.clientY,
this.lastPointerUpEvent!.clientX,
this.lastPointerUpEvent!.clientY,
);
if (
!this.hitLinkElement ||
Expand All @@ -3750,7 +3751,7 @@ class App extends React.Component<AppProps, AppState> {
return;
}
const lastPointerDownCoords = viewportCoordsToSceneCoords(
this.lastPointerDown!,
this.lastPointerDownEvent!,
this.state,
);
const lastPointerDownHittingLinkIcon = isPointHittingLink(
Expand All @@ -3760,7 +3761,7 @@ class App extends React.Component<AppProps, AppState> {
this.device.isMobile,
);
const lastPointerUpCoords = viewportCoordsToSceneCoords(
this.lastPointerUp!,
this.lastPointerUpEvent!,
this.state,
);
const lastPointerUpHittingLinkIcon = isPointHittingLink(
Expand Down Expand Up @@ -4465,7 +4466,8 @@ class App extends React.Component<AppProps, AppState> {
return;
}

this.lastPointerDown = event;
this.lastPointerDownEvent = event;

this.setState({
lastPointerDownWith: event.pointerType,
cursorButton: "down",
Expand Down Expand Up @@ -4605,14 +4607,14 @@ class App extends React.Component<AppProps, AppState> {
event: React.PointerEvent<HTMLCanvasElement>,
) => {
this.removePointer(event);
this.lastPointerUp = event;
this.lastPointerUpEvent = event;

const scenePointer = viewportCoordsToSceneCoords(
{ clientX: event.clientX, clientY: event.clientY },
this.state,
);
const clicklength =
event.timeStamp - (this.lastPointerDown?.timeStamp ?? 0);
event.timeStamp - (this.lastPointerDownEvent?.timeStamp ?? 0);
if (this.device.isMobile && clicklength < 300) {
const hitElement = this.getElementAtPosition(
scenePointer.x,
Expand Down Expand Up @@ -5366,7 +5368,9 @@ class App extends React.Component<AppProps, AppState> {
const [gridX, gridY] = getGridPoint(
sceneX,
sceneY,
this.lastPointerDown?.[KEYS.CTRL_OR_CMD] ? null : this.state.gridSize,
this.lastPointerDownEvent?.[KEYS.CTRL_OR_CMD]
? null
: this.state.gridSize,
);

const embedLink = getEmbedLink(link);
Expand Down Expand Up @@ -5416,7 +5420,9 @@ class App extends React.Component<AppProps, AppState> {
const [gridX, gridY] = getGridPoint(
sceneX,
sceneY,
this.lastPointerDown?.[KEYS.CTRL_OR_CMD] ? null : this.state.gridSize,
this.lastPointerDownEvent?.[KEYS.CTRL_OR_CMD]
? null
: this.state.gridSize,
);

const topLayerFrame = this.getTopLayerFrameAtSceneCoords({
Expand Down Expand Up @@ -5593,7 +5599,9 @@ class App extends React.Component<AppProps, AppState> {
const [gridX, gridY] = getGridPoint(
pointerDownState.origin.x,
pointerDownState.origin.y,
this.lastPointerDown?.[KEYS.CTRL_OR_CMD] ? null : this.state.gridSize,
this.lastPointerDownEvent?.[KEYS.CTRL_OR_CMD]
? null
: this.state.gridSize,
);

const topLayerFrame = this.getTopLayerFrameAtSceneCoords({
Expand Down Expand Up @@ -5651,7 +5659,9 @@ class App extends React.Component<AppProps, AppState> {
const [gridX, gridY] = getGridPoint(
pointerDownState.origin.x,
pointerDownState.origin.y,
this.lastPointerDown?.[KEYS.CTRL_OR_CMD] ? null : this.state.gridSize,
this.lastPointerDownEvent?.[KEYS.CTRL_OR_CMD]
? null
: this.state.gridSize,
);

const frame = newFrameElement({
Expand Down Expand Up @@ -6773,17 +6783,17 @@ class App extends React.Component<AppProps, AppState> {
}
if (isEraserActive(this.state)) {
const draggedDistance = distance2d(
this.lastPointerDown!.clientX,
this.lastPointerDown!.clientY,
this.lastPointerUp!.clientX,
this.lastPointerUp!.clientY,
this.lastPointerDownEvent!.clientX,
this.lastPointerDownEvent!.clientY,
this.lastPointerUpEvent!.clientX,
this.lastPointerUpEvent!.clientY,
);

if (draggedDistance === 0) {
const scenePointer = viewportCoordsToSceneCoords(
{
clientX: this.lastPointerUp!.clientX,
clientY: this.lastPointerUp!.clientY,
clientX: this.lastPointerUpEvent!.clientX,
clientY: this.lastPointerUpEvent!.clientY,
},
this.state,
);
Expand Down Expand Up @@ -7039,14 +7049,16 @@ class App extends React.Component<AppProps, AppState> {

if (
hitElement &&
this.lastPointerUp &&
this.lastPointerDown &&
this.lastPointerUp.timeStamp - this.lastPointerDown.timeStamp < 300 &&
this.lastPointerUpEvent &&
this.lastPointerDownEvent &&
this.lastPointerUpEvent.timeStamp -
this.lastPointerDownEvent.timeStamp <
300 &&
gesture.pointers.size <= 1 &&
isEmbeddableElement(hitElement) &&
this.isEmbeddableCenter(
hitElement,
this.lastPointerUp,
this.lastPointerUpEvent,
pointerDownState.origin.x,
pointerDownState.origin.y,
)
Expand Down

0 comments on commit fa33aa0

Please sign in to comment.