tldraw/state/state.ts

264 lines
7 KiB
TypeScript
Raw Normal View History

2021-05-09 12:03:39 +00:00
import { createSelectorHook, createState } from "@state-designer/react"
2021-05-12 22:08:53 +00:00
import { clamp, getCommonBounds, screenToWorld } from "utils/utils"
2021-05-09 21:22:25 +00:00
import * as vec from "utils/vec"
2021-05-13 06:44:52 +00:00
import { Bounds, Data, PointerInfo, Shape, ShapeType } from "types"
2021-05-10 12:16:57 +00:00
import { defaultDocument } from "./data"
2021-05-12 22:08:53 +00:00
import Shapes from "lib/shapes"
2021-05-13 08:34:56 +00:00
import history from "state/history"
2021-05-10 12:16:57 +00:00
import * as Sessions from "./sessions"
2021-05-09 12:03:39 +00:00
2021-05-09 21:22:25 +00:00
const initialData: Data = {
2021-05-13 06:44:52 +00:00
isReadOnly: false,
2021-05-09 13:04:42 +00:00
camera: {
point: [0, 0],
zoom: 1,
},
2021-05-10 12:16:57 +00:00
brush: undefined,
pointedId: null,
2021-05-12 11:27:33 +00:00
selectedIds: new Set([]),
2021-05-09 21:22:25 +00:00
currentPageId: "page0",
2021-05-10 12:16:57 +00:00
document: defaultDocument,
2021-05-09 13:04:42 +00:00
}
const state = createState({
data: initialData,
on: {
ZOOMED_CAMERA: {
do: "zoomCamera",
},
PANNED_CAMERA: {
do: "panCamera",
},
},
2021-05-10 12:16:57 +00:00
initial: "selecting",
states: {
selecting: {
2021-05-13 08:34:56 +00:00
on: {
UNDO: { do: "undo" },
REDO: { do: "redo" },
},
2021-05-13 06:44:52 +00:00
initial: "notPointing",
states: {
notPointing: {
on: {
POINTED_CANVAS: { to: "brushSelecting" },
POINTED_BOUNDS: { to: "pointingBounds" },
POINTED_SHAPE: [
"setPointedId",
{
if: "isPressingShiftKey",
then: {
if: "isPointedShapeSelected",
do: "pullPointedIdFromSelectedIds",
else: {
do: "pushPointedIdToSelectedIds",
to: "pointingBounds",
},
},
else: [
{
unless: "isPointedShapeSelected",
do: ["clearSelectedIds", "pushPointedIdToSelectedIds"],
},
{
to: "pointingBounds",
},
],
},
],
},
},
pointingBounds: {
on: {
STOPPED_POINTING: [
{
unless: ["isPointingBounds", "isPressingShiftKey"],
2021-05-13 06:44:52 +00:00
do: ["clearSelectedIds", "pushPointedIdToSelectedIds"],
},
{ to: "notPointing" },
],
MOVED_POINTER: {
unless: "isReadOnly",
if: "distanceImpliesDrag",
to: "draggingSelection",
},
},
2021-05-13 06:44:52 +00:00
},
draggingSelection: {
onEnter: "startTranslateSession",
on: {
MOVED_POINTER: "updateTranslateSession",
PANNED_CAMERA: "updateTranslateSession",
STOPPED_POINTING: { do: "completeSession", to: "selecting" },
CANCELLED: { do: "cancelSession", to: "selecting" },
},
},
brushSelecting: {
onEnter: [
{ unless: "isPressingShiftKey", do: "clearSelectedIds" },
"startBrushSession",
],
on: {
MOVED_POINTER: "updateBrushSession",
PANNED_CAMERA: "updateBrushSession",
STOPPED_POINTING: { do: "completeSession", to: "selecting" },
CANCELLED: { do: "cancelSession", to: "selecting" },
},
},
2021-05-10 12:16:57 +00:00
},
},
},
conditions: {
isPointingBounds(data, payload: PointerInfo) {
return payload.target === "bounds"
},
2021-05-13 06:44:52 +00:00
isReadOnly(data) {
return data.isReadOnly
},
distanceImpliesDrag(data, payload: PointerInfo) {
return vec.dist2(payload.origin, payload.point) > 16
},
isPointedShapeSelected(data) {
2021-05-12 11:27:33 +00:00
return data.selectedIds.has(data.pointedId)
},
isPressingShiftKey(data, payload: { shiftKey: boolean }) {
return payload.shiftKey
},
},
2021-05-09 13:04:42 +00:00
actions: {
2021-05-13 08:34:56 +00:00
// History
enableHistory() {
history.enable()
},
disableHistory() {
history.disable()
},
undo(data) {
history.undo(data)
},
redo(data) {
history.redo(data)
},
// Sessions
2021-05-10 12:16:57 +00:00
cancelSession(data) {
session.cancel(data)
session = undefined
},
completeSession(data) {
session.complete(data)
session = undefined
},
2021-05-13 08:34:56 +00:00
2021-05-13 06:44:52 +00:00
// Brushing
startBrushSession(data, payload: PointerInfo) {
2021-05-10 20:44:17 +00:00
session = new Sessions.BrushSession(
data,
screenToWorld(payload.point, data)
)
2021-05-10 12:16:57 +00:00
},
updateBrushSession(data, payload: PointerInfo) {
2021-05-10 20:44:17 +00:00
session.update(data, screenToWorld(payload.point, data))
2021-05-10 12:16:57 +00:00
},
2021-05-13 06:44:52 +00:00
// Dragging / Translating
startTranslateSession(data, payload: PointerInfo) {
2021-05-13 06:44:52 +00:00
session = new Sessions.TranslateSession(
data,
screenToWorld(payload.point, data)
)
},
updateTranslateSession(data, payload: PointerInfo) {
2021-05-13 06:44:52 +00:00
session.update(data, screenToWorld(payload.point, data))
},
// Selection
setPointedId(data, payload: PointerInfo) {
data.pointedId = payload.target
},
clearPointedId(data) {
data.pointedId = undefined
},
clearSelectedIds(data) {
2021-05-12 11:27:33 +00:00
data.selectedIds.clear()
},
pullPointedIdFromSelectedIds(data) {
const { selectedIds, pointedId } = data
2021-05-12 11:27:33 +00:00
selectedIds.delete(pointedId)
},
pushPointedIdToSelectedIds(data) {
2021-05-12 11:27:33 +00:00
data.selectedIds.add(data.pointedId)
},
// Camera
2021-05-09 13:04:42 +00:00
zoomCamera(data, payload: { delta: number; point: number[] }) {
const { camera } = data
const p0 = screenToWorld(payload.point, data)
camera.zoom = clamp(
camera.zoom - (payload.delta / 100) * camera.zoom,
0.5,
3
)
const p1 = screenToWorld(payload.point, data)
camera.point = vec.add(camera.point, vec.sub(p1, p0))
document.documentElement.style.setProperty(
"--camera-zoom",
camera.zoom.toString()
)
2021-05-09 13:04:42 +00:00
},
panCamera(data, payload: { delta: number[]; point: number[] }) {
const { camera } = data
data.camera.point = vec.sub(
camera.point,
vec.div(payload.delta, camera.zoom)
)
},
},
values: {
selectedIds(data) {
return new Set(data.selectedIds)
},
2021-05-12 22:08:53 +00:00
selectedBounds(data) {
const {
selectedIds,
currentPageId,
document: { pages },
} = data
return getCommonBounds(
...Array.from(selectedIds.values())
.map((id) => {
const shape = pages[currentPageId].shapes[id]
switch (shape.type) {
case ShapeType.Dot: {
return Shapes[shape.type].getBounds(shape)
}
case ShapeType.Circle: {
return Shapes[shape.type].getBounds(shape)
}
case ShapeType.Line: {
return Shapes[shape.type].getBounds(shape)
}
case ShapeType.Polyline: {
return Shapes[shape.type].getBounds(shape)
}
case ShapeType.Rectangle: {
return Shapes[shape.type].getBounds(shape)
}
default: {
return null
}
}
})
.filter(Boolean)
)
},
},
2021-05-09 13:04:42 +00:00
})
2021-05-09 12:03:39 +00:00
2021-05-10 12:16:57 +00:00
let session: Sessions.BaseSession
2021-05-09 12:03:39 +00:00
export default state
export const useSelector = createSelectorHook(state)