Tldraw/packages/editor/src/lib/editor/types/event-types.ts

193 wiersze
4.7 KiB
TypeScript
Czysty Zwykły widok Historia

import { TLHandle, TLShape, VecModel } from '@tldraw/tlschema'
import { VecLike } from '../../primitives/Vec'
2023-04-25 11:01:25 +00:00
import { TLSelectionHandle } from './selection-types'
/** @public */
export type UiEventType = 'pointer' | 'click' | 'keyboard' | 'wheel' | 'pinch' | 'zoom'
/** @public */
export type TLPointerEventTarget =
| { target: 'canvas'; shape?: undefined }
| { target: 'selection'; handle?: TLSelectionHandle; shape?: undefined }
| { target: 'shape'; shape: TLShape }
| { target: 'handle'; shape: TLShape; handle: TLHandle }
/** @public */
export type TLPointerEventName =
| 'pointer_down'
| 'pointer_move'
| 'pointer_up'
| 'right_click'
| 'middle_click'
/** @public */
export type TLCLickEventName = 'double_click' | 'triple_click' | 'quadruple_click'
/** @public */
export type TLPinchEventName = 'pinch_start' | 'pinch' | 'pinch_end'
/** @public */
export type TLKeyboardEventName = 'key_down' | 'key_up' | 'key_repeat'
/** @public */
export type TLEventName =
| TLPointerEventName
| TLCLickEventName
| TLPinchEventName
| TLKeyboardEventName
| 'wheel'
| 'cancel'
| 'complete'
| 'interrupt'
[fix] Batch tick events (#3181) This PR fixes an issue where events happening on tick were not batched. ![Kapture 2024-03-17 at 22 49 52](https://github.com/tldraw/tldraw/assets/23072548/2bcfa335-a38f-46c4-a3f3-434cac61b6ce) We were listening to the `tick` event directly from the state node, rather than passing the event into the state chart at the top. This meant that it was bypassing the regular state chart rules, which was what got me looking at this; but then I noticed that we also weren't batching the changes, either. This causes computed stuff to re-compute after each atom is updated within the `onTick` handler, which can be a LOT. Before: <img width="1557" alt="image" src="https://github.com/tldraw/tldraw/assets/23072548/ba8791f2-faec-463d-945a-9f5920826aab"> After: <img width="1204" alt="image" src="https://github.com/tldraw/tldraw/assets/23072548/a00f8e4a-caca-406a-89a2-8cff0e01b642"> It's not game breaking but it's important enough to hotfix at least in the dot com. ### Change Type <!-- ❗ Please select a 'Scope' label ❗️ --> - [x] `sdk` — Changes the tldraw SDK - [ ] `dotcom` — Changes the tldraw.com web app - [ ] `docs` — Changes to the documentation, examples, or templates. - [ ] `vs code` — Changes to the vscode plugin - [ ] `internal` — Does not affect user-facing stuff <!-- ❗ Please select a 'Type' label ❗️ --> - [x] `bugfix` — Bug fix - [ ] `feature` — New feature - [ ] `improvement` — Improving existing features - [ ] `chore` — Updating dependencies, other boring stuff - [ ] `galaxy brain` — Architectural changes - [ ] `tests` — Changes to any test code - [ ] `tools` — Changes to infrastructure, CI, internal scripts, debugging tools, etc. - [ ] `dunno` — I don't know ### Test Plan 1. Select many shapes. 2. Resize them. ### Release Notes - Fix a performance issue effecting resizing multiple shapes.
2024-03-18 14:33:36 +00:00
| 'tick'
2023-04-25 11:01:25 +00:00
/** @public */
export interface TLBaseEventInfo {
type: UiEventType
shiftKey: boolean
altKey: boolean
ctrlKey: boolean
}
/** @public */
export type TLPointerEventInfo = TLBaseEventInfo & {
type: 'pointer'
name: TLPointerEventName
[Fix] Camera coordinate issues (#2719) This PR fixes some bugs related to our coordinate systems. These bugs would appear when the editor was not full screen. ![Kapture 2024-02-04 at 11 53 37](https://github.com/tldraw/tldraw/assets/23072548/9c2199f3-b34d-4fe1-a3e5-d0c65fe5a11e) In short, we were being inconsistent with whether the `currentScreenPoint` was relative to the top left corner of the component or the top left corner of the page that contained the component. Here's the actual system: <img width="898" alt="image" src="https://github.com/tldraw/tldraw/assets/23072548/c49b3686-aeb6-4164-a55d-8639d40290a1"> The `viewportPageBounds` describes the bounds of the component within the browser's space. Its `x` and `y` describe the delta in browser space between the top left corner of the **page** and the component. This is not effected by scrolling. The use's `screenPoint` describes the user's cursor's location relative to the `viewportPageBounds`. Its `x` and `y` describe the delta in browser space between the top left corner of the **component** and the cursor. While this is a bug fix, I'm marking it as major as apps may be depending on the previous (broken) behavior. ### Change Type - [x] `major` — Breaking change ### Test Plan 1. Zoom in, out, and pinch on an editor that isn't full screen. 2. Zoom in, out, and pinch on an editor that is full screen. 3. Drag and scroll on an editor that isn't full screen. 4. Drag and scroll on an editor that is full screen. - [x] Unit Tests ### Release Notes - Fixed bugs with `getViewportScreenCenter` that could effect zooming and pinching on editors that aren't full screen
2024-02-04 12:03:49 +00:00
// The pointer position in client space, i.e. clientX / clientY
2023-04-25 11:01:25 +00:00
point: VecLike
pointerId: number
button: number
isPen: boolean
} & TLPointerEventTarget
/** @public */
export type TLClickEventInfo = TLBaseEventInfo & {
type: 'click'
name: TLCLickEventName
point: VecLike
pointerId: number
button: number
phase: 'down' | 'up' | 'settle'
} & TLPointerEventTarget
/** @public */
export type TLKeyboardEventInfo = TLBaseEventInfo & {
type: 'keyboard'
name: TLKeyboardEventName
key: string
code: string
}
/** @public */
export type TLPinchEventInfo = TLBaseEventInfo & {
type: 'pinch'
name: TLPinchEventName
point: VecModel
delta: VecModel
2023-04-25 11:01:25 +00:00
}
/** @public */
export type TLWheelEventInfo = TLBaseEventInfo & {
type: 'wheel'
name: 'wheel'
delta: VecModel
point: VecModel
2023-04-25 11:01:25 +00:00
}
/** @public */
export type TLCancelEventInfo = { type: 'misc'; name: 'cancel' }
/** @public */
export type TLCompleteEventInfo = { type: 'misc'; name: 'complete' }
/** @public */
export type TLInterruptEventInfo = { type: 'misc'; name: 'interrupt' }
[fix] Batch tick events (#3181) This PR fixes an issue where events happening on tick were not batched. ![Kapture 2024-03-17 at 22 49 52](https://github.com/tldraw/tldraw/assets/23072548/2bcfa335-a38f-46c4-a3f3-434cac61b6ce) We were listening to the `tick` event directly from the state node, rather than passing the event into the state chart at the top. This meant that it was bypassing the regular state chart rules, which was what got me looking at this; but then I noticed that we also weren't batching the changes, either. This causes computed stuff to re-compute after each atom is updated within the `onTick` handler, which can be a LOT. Before: <img width="1557" alt="image" src="https://github.com/tldraw/tldraw/assets/23072548/ba8791f2-faec-463d-945a-9f5920826aab"> After: <img width="1204" alt="image" src="https://github.com/tldraw/tldraw/assets/23072548/a00f8e4a-caca-406a-89a2-8cff0e01b642"> It's not game breaking but it's important enough to hotfix at least in the dot com. ### Change Type <!-- ❗ Please select a 'Scope' label ❗️ --> - [x] `sdk` — Changes the tldraw SDK - [ ] `dotcom` — Changes the tldraw.com web app - [ ] `docs` — Changes to the documentation, examples, or templates. - [ ] `vs code` — Changes to the vscode plugin - [ ] `internal` — Does not affect user-facing stuff <!-- ❗ Please select a 'Type' label ❗️ --> - [x] `bugfix` — Bug fix - [ ] `feature` — New feature - [ ] `improvement` — Improving existing features - [ ] `chore` — Updating dependencies, other boring stuff - [ ] `galaxy brain` — Architectural changes - [ ] `tests` — Changes to any test code - [ ] `tools` — Changes to infrastructure, CI, internal scripts, debugging tools, etc. - [ ] `dunno` — I don't know ### Test Plan 1. Select many shapes. 2. Resize them. ### Release Notes - Fix a performance issue effecting resizing multiple shapes.
2024-03-18 14:33:36 +00:00
/** @public */
export type TLTickEventInfo = { type: 'misc'; name: 'tick'; elapsed: number }
2023-04-25 11:01:25 +00:00
/** @public */
export type TLEventInfo =
| TLPointerEventInfo
| TLClickEventInfo
| TLKeyboardEventInfo
| TLPinchEventInfo
| TLWheelEventInfo
| TLCancelEventInfo
| TLCompleteEventInfo
| TLInterruptEventInfo
[fix] Batch tick events (#3181) This PR fixes an issue where events happening on tick were not batched. ![Kapture 2024-03-17 at 22 49 52](https://github.com/tldraw/tldraw/assets/23072548/2bcfa335-a38f-46c4-a3f3-434cac61b6ce) We were listening to the `tick` event directly from the state node, rather than passing the event into the state chart at the top. This meant that it was bypassing the regular state chart rules, which was what got me looking at this; but then I noticed that we also weren't batching the changes, either. This causes computed stuff to re-compute after each atom is updated within the `onTick` handler, which can be a LOT. Before: <img width="1557" alt="image" src="https://github.com/tldraw/tldraw/assets/23072548/ba8791f2-faec-463d-945a-9f5920826aab"> After: <img width="1204" alt="image" src="https://github.com/tldraw/tldraw/assets/23072548/a00f8e4a-caca-406a-89a2-8cff0e01b642"> It's not game breaking but it's important enough to hotfix at least in the dot com. ### Change Type <!-- ❗ Please select a 'Scope' label ❗️ --> - [x] `sdk` — Changes the tldraw SDK - [ ] `dotcom` — Changes the tldraw.com web app - [ ] `docs` — Changes to the documentation, examples, or templates. - [ ] `vs code` — Changes to the vscode plugin - [ ] `internal` — Does not affect user-facing stuff <!-- ❗ Please select a 'Type' label ❗️ --> - [x] `bugfix` — Bug fix - [ ] `feature` — New feature - [ ] `improvement` — Improving existing features - [ ] `chore` — Updating dependencies, other boring stuff - [ ] `galaxy brain` — Architectural changes - [ ] `tests` — Changes to any test code - [ ] `tools` — Changes to infrastructure, CI, internal scripts, debugging tools, etc. - [ ] `dunno` — I don't know ### Test Plan 1. Select many shapes. 2. Resize them. ### Release Notes - Fix a performance issue effecting resizing multiple shapes.
2024-03-18 14:33:36 +00:00
| TLTickEventInfo
2023-04-25 11:01:25 +00:00
/** @public */
export type TLPointerEvent = (info: TLPointerEventInfo) => void
/** @public */
export type TLClickEvent = (info: TLClickEventInfo) => void
/** @public */
export type TLKeyboardEvent = (info: TLKeyboardEventInfo) => void
/** @public */
export type TLPinchEvent = (info: TLPinchEventInfo) => void
/** @public */
export type TLWheelEvent = (info: TLWheelEventInfo) => void
/** @public */
export type TLCancelEvent = (info: TLCancelEventInfo) => void
/** @public */
export type TLCompleteEvent = (info: TLCompleteEventInfo) => void
/** @public */
export type TLInterruptEvent = (info: TLInterruptEventInfo) => void
[fix] Batch tick events (#3181) This PR fixes an issue where events happening on tick were not batched. ![Kapture 2024-03-17 at 22 49 52](https://github.com/tldraw/tldraw/assets/23072548/2bcfa335-a38f-46c4-a3f3-434cac61b6ce) We were listening to the `tick` event directly from the state node, rather than passing the event into the state chart at the top. This meant that it was bypassing the regular state chart rules, which was what got me looking at this; but then I noticed that we also weren't batching the changes, either. This causes computed stuff to re-compute after each atom is updated within the `onTick` handler, which can be a LOT. Before: <img width="1557" alt="image" src="https://github.com/tldraw/tldraw/assets/23072548/ba8791f2-faec-463d-945a-9f5920826aab"> After: <img width="1204" alt="image" src="https://github.com/tldraw/tldraw/assets/23072548/a00f8e4a-caca-406a-89a2-8cff0e01b642"> It's not game breaking but it's important enough to hotfix at least in the dot com. ### Change Type <!-- ❗ Please select a 'Scope' label ❗️ --> - [x] `sdk` — Changes the tldraw SDK - [ ] `dotcom` — Changes the tldraw.com web app - [ ] `docs` — Changes to the documentation, examples, or templates. - [ ] `vs code` — Changes to the vscode plugin - [ ] `internal` — Does not affect user-facing stuff <!-- ❗ Please select a 'Type' label ❗️ --> - [x] `bugfix` — Bug fix - [ ] `feature` — New feature - [ ] `improvement` — Improving existing features - [ ] `chore` — Updating dependencies, other boring stuff - [ ] `galaxy brain` — Architectural changes - [ ] `tests` — Changes to any test code - [ ] `tools` — Changes to infrastructure, CI, internal scripts, debugging tools, etc. - [ ] `dunno` — I don't know ### Test Plan 1. Select many shapes. 2. Resize them. ### Release Notes - Fix a performance issue effecting resizing multiple shapes.
2024-03-18 14:33:36 +00:00
/** @public */
export type TLTickEvent = (info: TLTickEventInfo) => void
2023-04-25 11:01:25 +00:00
/** @public */
export type UiEvent =
| TLPointerEvent
| TLClickEvent
| TLKeyboardEvent
| TLPinchEvent
| TLCancelEvent
| TLCompleteEvent
/** @public */
export type TLEnterEventHandler = (info: any, from: string) => void
2023-04-25 11:01:25 +00:00
/** @public */
export type TLExitEventHandler = (info: any, to: string) => void
2023-04-25 11:01:25 +00:00
/** @public */
export interface TLEventHandlers {
onPointerDown: TLPointerEvent
onPointerMove: TLPointerEvent
onRightClick: TLPointerEvent
onDoubleClick: TLClickEvent
onTripleClick: TLClickEvent
onQuadrupleClick: TLClickEvent
onMiddleClick: TLPointerEvent
onPointerUp: TLPointerEvent
onKeyDown: TLKeyboardEvent
onKeyUp: TLKeyboardEvent
onKeyRepeat: TLKeyboardEvent
onWheel: TLWheelEvent
onCancel: TLCancelEvent
onComplete: TLCompleteEvent
onInterrupt: TLInterruptEvent
[fix] Batch tick events (#3181) This PR fixes an issue where events happening on tick were not batched. ![Kapture 2024-03-17 at 22 49 52](https://github.com/tldraw/tldraw/assets/23072548/2bcfa335-a38f-46c4-a3f3-434cac61b6ce) We were listening to the `tick` event directly from the state node, rather than passing the event into the state chart at the top. This meant that it was bypassing the regular state chart rules, which was what got me looking at this; but then I noticed that we also weren't batching the changes, either. This causes computed stuff to re-compute after each atom is updated within the `onTick` handler, which can be a LOT. Before: <img width="1557" alt="image" src="https://github.com/tldraw/tldraw/assets/23072548/ba8791f2-faec-463d-945a-9f5920826aab"> After: <img width="1204" alt="image" src="https://github.com/tldraw/tldraw/assets/23072548/a00f8e4a-caca-406a-89a2-8cff0e01b642"> It's not game breaking but it's important enough to hotfix at least in the dot com. ### Change Type <!-- ❗ Please select a 'Scope' label ❗️ --> - [x] `sdk` — Changes the tldraw SDK - [ ] `dotcom` — Changes the tldraw.com web app - [ ] `docs` — Changes to the documentation, examples, or templates. - [ ] `vs code` — Changes to the vscode plugin - [ ] `internal` — Does not affect user-facing stuff <!-- ❗ Please select a 'Type' label ❗️ --> - [x] `bugfix` — Bug fix - [ ] `feature` — New feature - [ ] `improvement` — Improving existing features - [ ] `chore` — Updating dependencies, other boring stuff - [ ] `galaxy brain` — Architectural changes - [ ] `tests` — Changes to any test code - [ ] `tools` — Changes to infrastructure, CI, internal scripts, debugging tools, etc. - [ ] `dunno` — I don't know ### Test Plan 1. Select many shapes. 2. Resize them. ### Release Notes - Fix a performance issue effecting resizing multiple shapes.
2024-03-18 14:33:36 +00:00
onTick: TLTickEvent
2023-04-25 11:01:25 +00:00
}
/** @public */
export const EVENT_NAME_MAP: Record<
Exclude<TLEventName, TLPinchEventName>,
keyof TLEventHandlers
> = {
wheel: 'onWheel',
pointer_down: 'onPointerDown',
pointer_move: 'onPointerMove',
pointer_up: 'onPointerUp',
right_click: 'onRightClick',
middle_click: 'onMiddleClick',
key_down: 'onKeyDown',
key_up: 'onKeyUp',
key_repeat: 'onKeyRepeat',
cancel: 'onCancel',
complete: 'onComplete',
interrupt: 'onInterrupt',
double_click: 'onDoubleClick',
triple_click: 'onTripleClick',
quadruple_click: 'onQuadrupleClick',
[fix] Batch tick events (#3181) This PR fixes an issue where events happening on tick were not batched. ![Kapture 2024-03-17 at 22 49 52](https://github.com/tldraw/tldraw/assets/23072548/2bcfa335-a38f-46c4-a3f3-434cac61b6ce) We were listening to the `tick` event directly from the state node, rather than passing the event into the state chart at the top. This meant that it was bypassing the regular state chart rules, which was what got me looking at this; but then I noticed that we also weren't batching the changes, either. This causes computed stuff to re-compute after each atom is updated within the `onTick` handler, which can be a LOT. Before: <img width="1557" alt="image" src="https://github.com/tldraw/tldraw/assets/23072548/ba8791f2-faec-463d-945a-9f5920826aab"> After: <img width="1204" alt="image" src="https://github.com/tldraw/tldraw/assets/23072548/a00f8e4a-caca-406a-89a2-8cff0e01b642"> It's not game breaking but it's important enough to hotfix at least in the dot com. ### Change Type <!-- ❗ Please select a 'Scope' label ❗️ --> - [x] `sdk` — Changes the tldraw SDK - [ ] `dotcom` — Changes the tldraw.com web app - [ ] `docs` — Changes to the documentation, examples, or templates. - [ ] `vs code` — Changes to the vscode plugin - [ ] `internal` — Does not affect user-facing stuff <!-- ❗ Please select a 'Type' label ❗️ --> - [x] `bugfix` — Bug fix - [ ] `feature` — New feature - [ ] `improvement` — Improving existing features - [ ] `chore` — Updating dependencies, other boring stuff - [ ] `galaxy brain` — Architectural changes - [ ] `tests` — Changes to any test code - [ ] `tools` — Changes to infrastructure, CI, internal scripts, debugging tools, etc. - [ ] `dunno` — I don't know ### Test Plan 1. Select many shapes. 2. Resize them. ### Release Notes - Fix a performance issue effecting resizing multiple shapes.
2024-03-18 14:33:36 +00:00
tick: 'onTick',
2023-04-25 11:01:25 +00:00
}