tldraw/state/hacks.ts

131 lines
3.4 KiB
TypeScript
Raw Normal View History

import { DrawShape, PointerInfo } from 'types'
2021-06-29 17:58:07 +00:00
import { deepClone, setToArray } from 'utils'
2021-06-29 12:00:59 +00:00
import tld from 'utils/tld'
import { freeze } from 'immer'
import session from './session'
2021-07-02 12:04:45 +00:00
import coopState from 'state/coop/coop-state'
import state from './state'
import vec from 'utils/vec'
import * as Session from './sessions'
/**
* While a user is drawing with the draw tool, we want to update the shape without
* going through the trouble of updating the entire state machine. Speciifcally, we
* do not want to push the change through immer. Instead, we'll push the change
* directly to the state using `forceData`.
* @param info
*/
2021-07-04 18:45:07 +00:00
export function fastTranslate(info: PointerInfo): void {
const data = { ...state.data }
session.update<Session.TranslateSession>(
data,
tld.screenToWorld(info.point, data),
info.shiftKey,
info.altKey
)
state.forceData(freeze(data))
}
export function fastTransform(info: PointerInfo): void {
const data = { ...state.data }
session.update<Session.TransformSession | Session.TransformSingleSession>(
data,
tld.screenToWorld(info.point, data),
info.shiftKey
)
state.forceData(freeze(data))
}
2021-06-21 21:35:28 +00:00
export function fastDrawUpdate(info: PointerInfo): void {
const data = { ...state.data }
2021-07-04 18:45:07 +00:00
coopState.send('MOVED_CURSOR', {
2021-07-02 12:04:45 +00:00
pageId: data.currentPageId,
point: info.point,
})
2021-06-30 20:31:29 +00:00
session.update<Session.DrawSession>(
data,
2021-06-29 12:00:59 +00:00
tld.screenToWorld(info.point, data),
info.pressure,
info.shiftKey
)
2021-06-29 12:00:59 +00:00
const selectedId = setToArray(tld.getSelectedIds(data))[0]
2021-06-29 18:30:57 +00:00
const { shapes } = data.document.pages[data.currentPageId]
2021-06-29 18:30:57 +00:00
const shape = shapes[selectedId] as DrawShape
shapes[selectedId] = deepClone(shape)
state.forceData(freeze(data))
}
2021-06-21 21:35:28 +00:00
export function fastPanUpdate(delta: number[]): void {
const data = { ...state.data }
2021-06-29 12:00:59 +00:00
const camera = tld.getCurrentCamera(data)
camera.point = vec.sub(camera.point, vec.div(delta, camera.zoom))
2021-06-29 18:08:25 +00:00
data.pageStates[data.currentPageId].camera = deepClone(camera)
state.forceData(freeze(data))
}
2021-06-21 21:35:28 +00:00
export function fastZoomUpdate(point: number[], delta: number): void {
const data = { ...state.data }
2021-06-29 12:00:59 +00:00
const camera = tld.getCurrentCamera(data)
const next = camera.zoom - (delta / 100) * camera.zoom
2021-06-29 12:00:59 +00:00
const p0 = tld.screenToWorld(point, data)
camera.zoom = tld.getCameraZoom(next)
const p1 = tld.screenToWorld(point, data)
camera.point = vec.add(camera.point, vec.sub(p1, p0))
2021-06-29 18:08:25 +00:00
data.pageStates[data.currentPageId].camera = deepClone(camera)
state.forceData(freeze(data))
}
export function fastPinchCamera(
point: number[],
delta: number[],
2021-06-21 21:35:28 +00:00
distanceDelta: number
): void {
const data = { ...state.data }
2021-06-29 12:00:59 +00:00
const camera = tld.getCurrentCamera(data)
camera.point = vec.sub(camera.point, vec.div(delta, camera.zoom))
2021-06-18 15:19:10 +00:00
const next = camera.zoom - (distanceDelta / 350) * camera.zoom
2021-06-29 12:00:59 +00:00
const p0 = tld.screenToWorld(point, data)
camera.zoom = tld.getCameraZoom(next)
const p1 = tld.screenToWorld(point, data)
camera.point = vec.add(camera.point, vec.sub(p1, p0))
2021-06-15 12:20:22 +00:00
const pageState = data.pageStates[data.currentPageId]
2021-06-29 17:58:07 +00:00
pageState.camera = deepClone(camera)
2021-06-15 12:20:22 +00:00
data.pageStates[data.currentPageId] = { ...pageState }
state.forceData(freeze(data))
}
2021-06-21 21:35:28 +00:00
export function fastBrushSelect(point: number[]): void {
const data = { ...state.data }
2021-06-13 13:55:37 +00:00
2021-06-29 12:00:59 +00:00
session.update<Session.BrushSession>(data, tld.screenToWorld(point, data))
2021-06-29 17:58:07 +00:00
data.brush = deepClone(data.brush)
state.forceData(freeze(data))
}