tldraw/state/state.ts

1125 lines
33 KiB
TypeScript
Raw Normal View History

2021-05-28 16:25:43 +00:00
import { createSelectorHook, createState } from '@state-designer/react'
import * as vec from 'utils/vec'
import inputs from './inputs'
import { defaultDocument } from './data'
import { shades } from 'lib/colors'
import { createShape, getShapeUtils } from 'lib/shape-utils'
import history from 'state/history'
import * as Sessions from './sessions'
import commands from './commands'
import { updateFromCode } from 'lib/code/generate'
import {
clamp,
2021-05-23 13:46:04 +00:00
getChildren,
getCommonBounds,
2021-05-26 10:34:10 +00:00
getCurrent,
getPage,
getSelectedBounds,
getShape,
screenToWorld,
setZoomCSS,
2021-05-28 16:25:43 +00:00
} from 'utils/utils'
import {
Data,
PointerInfo,
Shape,
ShapeType,
Corner,
Edge,
2021-05-17 10:01:11 +00:00
CodeControl,
2021-05-23 17:09:23 +00:00
MoveType,
2021-05-26 10:34:10 +00:00
ShapeStyles,
2021-05-26 19:20:52 +00:00
DistributeType,
AlignType,
StretchType,
2021-05-28 16:25:43 +00:00
} from 'types'
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-14 22:56:41 +00:00
settings: {
fontSize: 13,
2021-05-17 21:27:18 +00:00
isDarkMode: false,
isCodeOpen: false,
2021-05-26 10:34:10 +00:00
isStyleOpen: false,
2021-05-28 20:30:27 +00:00
isToolLocked: false,
isPenLocked: false,
nudgeDistanceLarge: 10,
nudgeDistanceSmall: 1,
2021-05-26 10:34:10 +00:00
},
currentStyle: {
2021-05-26 21:47:46 +00:00
fill: shades.lightGray,
stroke: shades.darkGray,
2021-05-28 20:30:27 +00:00
strokeWidth: 2,
2021-05-14 22:56:41 +00:00
},
2021-05-09 13:04:42 +00:00
camera: {
point: [0, 0],
zoom: 1,
},
2021-05-10 12:16:57 +00:00
brush: undefined,
2021-05-18 08:32:20 +00:00
boundsRotation: 0,
2021-05-10 12:16:57 +00:00
pointedId: null,
2021-05-14 22:56:41 +00:00
hoveredId: null,
2021-05-12 11:27:33 +00:00
selectedIds: new Set([]),
2021-05-28 16:25:43 +00:00
currentPageId: 'page0',
currentCodeFileId: 'file0',
2021-05-17 10:01:11 +00:00
codeControls: {},
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: {
2021-05-28 16:25:43 +00:00
do: 'zoomCamera',
2021-05-09 13:04:42 +00:00
},
PANNED_CAMERA: {
2021-05-28 16:25:43 +00:00
do: 'panCamera',
},
TOGGLED_SHAPE_LOCK: { if: 'hasSelection', do: 'lockSelection' },
TOGGLED_SHAPE_HIDE: { if: 'hasSelection', do: 'hideSelection' },
TOGGLED_SHAPE_ASPECT_LOCK: {
if: 'hasSelection',
do: 'aspectLockSelection',
},
2021-05-28 16:25:43 +00:00
SELECTED_SELECT_TOOL: { to: 'selecting' },
SELECTED_DRAW_TOOL: { unless: 'isReadOnly', to: 'draw' },
SELECTED_DOT_TOOL: { unless: 'isReadOnly', to: 'dot' },
SELECTED_CIRCLE_TOOL: { unless: 'isReadOnly', to: 'circle' },
SELECTED_ELLIPSE_TOOL: { unless: 'isReadOnly', to: 'ellipse' },
SELECTED_RAY_TOOL: { unless: 'isReadOnly', to: 'ray' },
SELECTED_LINE_TOOL: { unless: 'isReadOnly', to: 'line' },
SELECTED_POLYLINE_TOOL: { unless: 'isReadOnly', to: 'polyline' },
SELECTED_RECTANGLE_TOOL: { unless: 'isReadOnly', to: 'rectangle' },
TOGGLED_CODE_PANEL_OPEN: 'toggleCodePanel',
TOGGLED_STYLE_PANEL_OPEN: 'toggleStylePanel',
CHANGED_STYLE: ['updateStyles', 'applyStylesToSelection'],
RESET_CAMERA: 'resetCamera',
ZOOMED_TO_FIT: 'zoomCameraToFit',
2021-05-25 09:09:51 +00:00
ZOOMED_TO_SELECTION: {
2021-05-28 16:25:43 +00:00
if: 'hasSelection',
do: 'zoomCameraToSelection',
2021-05-25 09:09:51 +00:00
},
ZOOMED_TO_ACTUAL: {
2021-05-28 16:25:43 +00:00
if: 'hasSelection',
do: 'zoomCameraToSelectionActual',
else: 'zoomCameraToActual',
},
2021-05-28 16:25:43 +00:00
SELECTED_ALL: { to: 'selecting', do: 'selectAll' },
2021-05-28 20:30:27 +00:00
NUDGED: { do: 'nudgeSelection' },
USED_PEN_DEVICE: 'enablePenLock',
DISABLED_PEN_LOCK: 'disablePenLock',
2021-05-09 13:04:42 +00:00
},
2021-05-28 16:25:43 +00:00
initial: 'loading',
2021-05-10 12:16:57 +00:00
states: {
loading: {
on: {
2021-05-28 16:25:43 +00:00
MOUNTED: [
'restoreSavedData',
{
if: 'hasSelection',
do: 'zoomCameraToSelectionActual',
else: ['zoomCameraToFit', 'zoomCameraToActual'],
},
{
to: 'ready',
},
],
},
},
2021-05-17 21:27:18 +00:00
ready: {
2021-05-13 08:34:56 +00:00
on: {
2021-05-17 21:27:18 +00:00
UNMOUNTED: [
2021-05-28 16:25:43 +00:00
{ unless: 'isReadOnly', do: 'forceSave' },
{ to: 'loading' },
2021-05-17 21:27:18 +00:00
],
2021-05-13 08:34:56 +00:00
},
2021-05-28 16:25:43 +00:00
initial: 'selecting',
2021-05-13 06:44:52 +00:00
states: {
2021-05-17 21:27:18 +00:00
selecting: {
2021-05-13 06:44:52 +00:00
on: {
2021-05-28 16:25:43 +00:00
SAVED: 'forceSave',
2021-05-28 20:30:27 +00:00
UNDO: 'undo',
REDO: 'redo',
2021-05-28 16:25:43 +00:00
SAVED_CODE: 'saveCode',
2021-05-28 20:30:27 +00:00
DELETED: 'deleteSelectedIds',
STARTED_PINCHING: { to: 'pinching' },
2021-05-28 16:25:43 +00:00
INCREASED_CODE_FONT_SIZE: 'increaseCodeFontSize',
DECREASED_CODE_FONT_SIZE: 'decreaseCodeFontSize',
CHANGED_CODE_CONTROL: 'updateControls',
2021-05-28 20:30:27 +00:00
GENERATED_FROM_CODE: ['setCodeControls', 'setGeneratedShapes'],
TOGGLED_TOOL_LOCK: 'toggleToolLock',
MOVED: { if: 'hasSelection', do: 'moveSelection' },
ALIGNED: { if: 'hasSelection', do: 'alignSelection' },
STRETCHED: { if: 'hasSelection', do: 'stretchSelection' },
DISTRIBUTED: { if: 'hasSelection', do: 'distributeSelection' },
DUPLICATED: { if: 'hasSelection', do: 'duplicateSelection' },
2021-05-17 21:27:18 +00:00
},
2021-05-28 16:25:43 +00:00
initial: 'notPointing',
2021-05-17 21:27:18 +00:00
states: {
notPointing: {
on: {
2021-05-29 12:40:41 +00:00
CANCELLED: 'clearSelectedIds',
2021-05-28 16:25:43 +00:00
POINTED_CANVAS: { to: 'brushSelecting' },
POINTED_BOUNDS: { to: 'pointingBounds' },
POINTED_BOUNDS_HANDLE: {
2021-05-28 16:25:43 +00:00
if: 'isPointingRotationHandle',
to: 'rotatingSelection',
else: { to: 'transformingSelection' },
},
2021-05-17 21:27:18 +00:00
MOVED_OVER_SHAPE: {
2021-05-28 16:25:43 +00:00
if: 'pointHitsShape',
2021-05-17 21:27:18 +00:00
then: {
2021-05-28 16:25:43 +00:00
unless: 'shapeIsHovered',
do: 'setHoveredId',
2021-05-13 06:44:52 +00:00
},
2021-05-28 16:25:43 +00:00
else: { if: 'shapeIsHovered', do: 'clearHoveredId' },
2021-05-13 06:44:52 +00:00
},
2021-05-28 16:25:43 +00:00
UNHOVERED_SHAPE: 'clearHoveredId',
2021-05-17 21:27:18 +00:00
POINTED_SHAPE: [
2021-05-13 06:44:52 +00:00
{
2021-05-28 16:25:43 +00:00
if: 'isPressingMetaKey',
to: 'brushSelecting',
2021-05-20 08:19:13 +00:00
},
2021-05-28 16:25:43 +00:00
'setPointedId',
2021-05-20 08:19:13 +00:00
{
2021-05-28 16:25:43 +00:00
unless: 'isPointedShapeSelected',
2021-05-20 09:25:14 +00:00
then: {
2021-05-28 16:25:43 +00:00
if: 'isPressingShiftKey',
do: ['pushPointedIdToSelectedIds', 'clearPointedId'],
else: ['clearSelectedIds', 'pushPointedIdToSelectedIds'],
2021-05-20 09:25:14 +00:00
},
2021-05-20 08:19:13 +00:00
},
{
2021-05-28 16:25:43 +00:00
to: 'pointingBounds',
2021-05-13 06:44:52 +00:00
},
],
},
},
2021-05-17 21:27:18 +00:00
pointingBounds: {
2021-05-15 17:11:08 +00:00
on: {
2021-05-17 21:27:18 +00:00
STOPPED_POINTING: [
{
2021-05-28 16:25:43 +00:00
if: 'isPressingShiftKey',
2021-05-20 08:19:13 +00:00
then: {
2021-05-28 16:25:43 +00:00
if: 'isPointedShapeSelected',
do: 'pullPointedIdFromSelectedIds',
2021-05-20 08:19:13 +00:00
},
else: {
2021-05-28 16:25:43 +00:00
unless: 'isPointingBounds',
do: ['clearSelectedIds', 'pushPointedIdToSelectedIds'],
2021-05-20 08:19:13 +00:00
},
2021-05-17 21:27:18 +00:00
},
2021-05-28 16:25:43 +00:00
{ to: 'notPointing' },
2021-05-17 21:27:18 +00:00
],
2021-05-15 17:11:08 +00:00
MOVED_POINTER: {
2021-05-28 16:25:43 +00:00
unless: 'isReadOnly',
if: 'distanceImpliesDrag',
to: 'draggingSelection',
2021-05-15 17:11:08 +00:00
},
},
},
2021-05-17 21:27:18 +00:00
rotatingSelection: {
2021-05-28 16:25:43 +00:00
onEnter: 'startRotateSession',
onExit: 'clearBoundsRotation',
2021-05-17 21:27:18 +00:00
on: {
2021-05-28 16:25:43 +00:00
MOVED_POINTER: 'updateRotateSession',
PANNED_CAMERA: 'updateRotateSession',
PRESSED_SHIFT_KEY: 'keyUpdateRotateSession',
RELEASED_SHIFT_KEY: 'keyUpdateRotateSession',
STOPPED_POINTING: { do: 'completeSession', to: 'selecting' },
CANCELLED: { do: 'cancelSession', to: 'selecting' },
2021-05-17 21:27:18 +00:00
},
},
transformingSelection: {
2021-05-28 16:25:43 +00:00
onEnter: 'startTransformSession',
2021-05-17 21:27:18 +00:00
on: {
2021-05-28 16:25:43 +00:00
MOVED_POINTER: 'updateTransformSession',
PANNED_CAMERA: 'updateTransformSession',
PRESSED_SHIFT_KEY: 'keyUpdateTransformSession',
RELEASED_SHIFT_KEY: 'keyUpdateTransformSession',
STOPPED_POINTING: { do: 'completeSession', to: 'selecting' },
CANCELLED: { do: 'cancelSession', to: 'selecting' },
2021-05-17 21:27:18 +00:00
},
},
draggingSelection: {
2021-05-28 16:25:43 +00:00
onEnter: 'startTranslateSession',
2021-05-15 17:11:08 +00:00
on: {
2021-05-28 16:25:43 +00:00
MOVED_POINTER: 'updateTranslateSession',
PANNED_CAMERA: 'updateTranslateSession',
PRESSED_SHIFT_KEY: 'keyUpdateTranslateSession',
RELEASED_SHIFT_KEY: 'keyUpdateTranslateSession',
PRESSED_ALT_KEY: 'keyUpdateTranslateSession',
RELEASED_ALT_KEY: 'keyUpdateTranslateSession',
STOPPED_POINTING: { do: 'completeSession', to: 'selecting' },
CANCELLED: { do: 'cancelSession', to: 'selecting' },
2021-05-15 17:11:08 +00:00
},
},
2021-05-17 21:27:18 +00:00
brushSelecting: {
onEnter: [
2021-05-20 09:25:14 +00:00
{
2021-05-28 16:25:43 +00:00
unless: ['isPressingMetaKey', 'isPressingShiftKey'],
do: 'clearSelectedIds',
2021-05-20 09:25:14 +00:00
},
2021-05-28 16:25:43 +00:00
'clearBoundsRotation',
'startBrushSession',
2021-05-17 21:27:18 +00:00
],
on: {
2021-05-28 22:33:28 +00:00
STARTED_PINCHING: { to: 'pinching' },
2021-05-28 16:25:43 +00:00
MOVED_POINTER: 'updateBrushSession',
PANNED_CAMERA: 'updateBrushSession',
STOPPED_POINTING: { do: 'completeSession', to: 'selecting' },
CANCELLED: { do: 'cancelSession', to: 'selecting' },
2021-05-17 21:27:18 +00:00
},
2021-05-15 15:20:21 +00:00
},
},
},
2021-05-28 13:08:51 +00:00
pinching: {
on: {
2021-05-28 16:25:43 +00:00
STOPPED_PINCHING: { to: 'selecting' },
PINCHED: { do: 'pinchCamera' },
2021-05-28 13:08:51 +00:00
},
},
2021-05-28 20:30:27 +00:00
usingTool: {
initial: 'draw',
2021-05-28 21:05:40 +00:00
onEnter: 'clearSelectedIds',
2021-05-27 17:59:40 +00:00
states: {
2021-05-28 20:30:27 +00:00
draw: {
initial: 'creating',
states: {
creating: {
on: {
CANCELLED: { to: 'selecting' },
2021-05-28 21:05:40 +00:00
POINTED_SHAPE: {
get: 'newDraw',
do: 'createShape',
to: 'draw.editing',
},
2021-05-28 20:30:27 +00:00
POINTED_CANVAS: {
get: 'newDraw',
do: 'createShape',
to: 'draw.editing',
},
UNDO: { do: 'undo' },
REDO: { do: 'redo' },
},
},
editing: {
onEnter: 'startDrawSession',
on: {
STOPPED_POINTING: {
do: 'completeSession',
to: 'draw.creating',
},
CANCELLED: {
do: ['cancelSession', 'deleteSelectedIds'],
to: 'selecting',
},
MOVED_POINTER: 'updateDrawSession',
PANNED_CAMERA: 'updateDrawSession',
},
2021-05-27 17:59:40 +00:00
},
},
},
2021-05-28 20:30:27 +00:00
dot: {
initial: 'creating',
states: {
creating: {
on: {
CANCELLED: { to: 'selecting' },
2021-05-28 21:05:40 +00:00
POINTED_SHAPE: {
get: 'newDot',
do: 'createShape',
to: 'dot.editing',
},
2021-05-28 20:30:27 +00:00
POINTED_CANVAS: {
get: 'newDot',
do: 'createShape',
to: 'dot.editing',
},
},
2021-05-27 22:07:27 +00:00
},
2021-05-28 20:30:27 +00:00
editing: {
on: {
STOPPED_POINTING: [
'completeSession',
{
if: 'isToolLocked',
to: 'dot.creating',
else: {
to: 'selecting',
},
},
],
CANCELLED: {
do: ['cancelSession', 'deleteSelectedIds'],
to: 'selecting',
},
},
initial: 'inactive',
states: {
inactive: {
on: {
MOVED_POINTER: {
if: 'distanceImpliesDrag',
to: 'dot.editing.active',
},
},
},
active: {
onEnter: 'startTranslateSession',
on: {
MOVED_POINTER: 'updateTranslateSession',
PANNED_CAMERA: 'updateTranslateSession',
},
},
},
2021-05-27 17:59:40 +00:00
},
},
},
2021-05-28 20:30:27 +00:00
circle: {
initial: 'creating',
states: {
creating: {
on: {
CANCELLED: { to: 'selecting' },
2021-05-28 21:05:40 +00:00
POINTED_SHAPE: {
to: 'circle.editing',
},
2021-05-28 20:30:27 +00:00
POINTED_CANVAS: {
to: 'circle.editing',
},
},
2021-05-17 21:27:18 +00:00
},
2021-05-28 20:30:27 +00:00
editing: {
on: {
STOPPED_POINTING: { to: 'selecting' },
CANCELLED: { to: 'selecting' },
MOVED_POINTER: {
if: 'distanceImpliesDrag',
then: {
get: 'newCircle',
do: 'createShape',
to: 'drawingShape.bounds',
},
},
},
2021-05-17 21:27:18 +00:00
},
},
2021-05-28 20:30:27 +00:00
},
ellipse: {
initial: 'creating',
2021-05-17 21:27:18 +00:00
states: {
2021-05-28 20:30:27 +00:00
creating: {
2021-05-17 21:27:18 +00:00
on: {
2021-05-28 20:30:27 +00:00
CANCELLED: { to: 'selecting' },
POINTED_CANVAS: {
to: 'ellipse.editing',
2021-05-17 21:27:18 +00:00
},
},
},
2021-05-28 20:30:27 +00:00
editing: {
2021-05-17 21:27:18 +00:00
on: {
2021-05-28 20:30:27 +00:00
STOPPED_POINTING: { to: 'selecting' },
CANCELLED: { to: 'selecting' },
MOVED_POINTER: {
if: 'distanceImpliesDrag',
then: {
get: 'newEllipse',
do: 'createShape',
to: 'drawingShape.bounds',
},
},
2021-05-17 21:27:18 +00:00
},
},
},
2021-05-15 15:20:21 +00:00
},
2021-05-28 20:30:27 +00:00
rectangle: {
initial: 'creating',
states: {
creating: {
on: {
CANCELLED: { to: 'selecting' },
2021-05-28 21:05:40 +00:00
POINTED_SHAPE: {
to: 'rectangle.editing',
},
2021-05-28 20:30:27 +00:00
POINTED_CANVAS: {
to: 'rectangle.editing',
},
},
2021-05-15 17:11:08 +00:00
},
2021-05-28 20:30:27 +00:00
editing: {
on: {
STOPPED_POINTING: { to: 'selecting' },
CANCELLED: { to: 'selecting' },
MOVED_POINTER: {
if: 'distanceImpliesDrag',
then: {
get: 'newRectangle',
do: 'createShape',
to: 'drawingShape.bounds',
},
},
2021-05-23 13:46:04 +00:00
},
2021-05-17 21:27:18 +00:00
},
2021-05-15 17:11:08 +00:00
},
},
2021-05-28 20:30:27 +00:00
ray: {
initial: 'creating',
states: {
creating: {
on: {
CANCELLED: { to: 'selecting' },
2021-05-28 21:05:40 +00:00
POINTED_SHAPE: {
get: 'newRay',
do: 'createShape',
to: 'ray.editing',
},
2021-05-28 20:30:27 +00:00
POINTED_CANVAS: {
get: 'newRay',
do: 'createShape',
to: 'ray.editing',
},
},
2021-05-17 21:27:18 +00:00
},
2021-05-28 20:30:27 +00:00
editing: {
on: {
STOPPED_POINTING: { to: 'selecting' },
CANCELLED: { to: 'selecting' },
MOVED_POINTER: {
if: 'distanceImpliesDrag',
to: 'drawingShape.direction',
},
2021-05-23 13:46:04 +00:00
},
2021-05-17 21:27:18 +00:00
},
},
2021-05-15 17:11:08 +00:00
},
2021-05-28 20:30:27 +00:00
line: {
initial: 'creating',
states: {
creating: {
on: {
CANCELLED: { to: 'selecting' },
2021-05-28 21:05:40 +00:00
POINTED_SHAPE: {
get: 'newLine',
do: 'createShape',
to: 'line.editing',
},
2021-05-28 20:30:27 +00:00
POINTED_CANVAS: {
get: 'newLine',
do: 'createShape',
to: 'line.editing',
},
},
2021-05-17 21:27:18 +00:00
},
2021-05-28 20:30:27 +00:00
editing: {
on: {
STOPPED_POINTING: { to: 'selecting' },
CANCELLED: { to: 'selecting' },
MOVED_POINTER: {
if: 'distanceImpliesDrag',
to: 'drawingShape.direction',
},
2021-05-23 13:46:04 +00:00
},
2021-05-17 21:27:18 +00:00
},
},
2021-05-15 17:11:08 +00:00
},
2021-05-28 20:30:27 +00:00
polyline: {},
2021-05-15 17:11:08 +00:00
},
2021-05-17 21:27:18 +00:00
},
2021-05-28 20:30:27 +00:00
drawingShape: {
on: {
STOPPED_POINTING: [
'completeSession',
{
if: 'isToolLocked',
to: 'usingTool.previous',
else: { to: 'selecting' },
2021-05-15 17:11:08 +00:00
},
2021-05-28 20:30:27 +00:00
],
CANCELLED: {
do: ['cancelSession', 'deleteSelectedIds'],
to: 'selecting',
2021-05-15 17:11:08 +00:00
},
2021-05-17 21:27:18 +00:00
},
2021-05-28 20:30:27 +00:00
initial: 'drawingShapeBounds',
2021-05-17 21:27:18 +00:00
states: {
2021-05-28 20:30:27 +00:00
bounds: {
onEnter: 'startDrawTransformSession',
2021-05-15 17:11:08 +00:00
on: {
2021-05-28 20:30:27 +00:00
MOVED_POINTER: 'updateTransformSession',
PANNED_CAMERA: 'updateTransformSession',
2021-05-17 21:27:18 +00:00
},
},
2021-05-28 20:30:27 +00:00
direction: {
onEnter: 'startDirectionSession',
2021-05-17 21:27:18 +00:00
on: {
2021-05-28 20:30:27 +00:00
MOVED_POINTER: 'updateDirectionSession',
PANNED_CAMERA: 'updateDirectionSession',
2021-05-15 17:11:08 +00:00
},
},
},
2021-05-15 15:20:21 +00:00
},
},
},
2021-05-10 12:16:57 +00:00
},
2021-05-23 13:46:04 +00:00
results: {
2021-05-27 17:59:40 +00:00
newDraw() {
return ShapeType.Draw
},
2021-05-26 10:34:10 +00:00
newDot() {
return ShapeType.Dot
2021-05-23 13:46:04 +00:00
},
2021-05-26 10:34:10 +00:00
newRay() {
return ShapeType.Ray
2021-05-23 13:46:04 +00:00
},
2021-05-26 10:34:10 +00:00
newLine() {
return ShapeType.Line
2021-05-23 13:46:04 +00:00
},
2021-05-26 10:34:10 +00:00
newCircle() {
return ShapeType.Circle
2021-05-23 13:46:04 +00:00
},
2021-05-26 10:34:10 +00:00
newEllipse() {
return ShapeType.Ellipse
2021-05-23 13:46:04 +00:00
},
2021-05-26 10:34:10 +00:00
newRectangle() {
return ShapeType.Rectangle
2021-05-23 13:46:04 +00:00
},
},
conditions: {
isPointingBounds(data, payload: PointerInfo) {
2021-05-28 16:25:43 +00:00
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) > 8
2021-05-13 06:44:52 +00:00
},
isPointedShapeSelected(data) {
2021-05-12 11:27:33 +00:00
return data.selectedIds.has(data.pointedId)
},
2021-05-20 09:25:14 +00:00
isPressingShiftKey(data, payload: PointerInfo) {
return payload.shiftKey
},
2021-05-20 09:25:14 +00:00
isPressingMetaKey(data, payload: PointerInfo) {
return payload.metaKey
},
2021-05-14 22:56:41 +00:00
shapeIsHovered(data, payload: { target: string }) {
return data.hoveredId === payload.target
},
pointHitsShape(data, payload: { target: string; point: number[] }) {
const shape = getShape(data, payload.target)
2021-05-14 22:56:41 +00:00
return getShapeUtils(shape).hitTest(
shape,
screenToWorld(payload.point, data)
)
},
isPointingRotationHandle(
data,
2021-05-28 16:25:43 +00:00
payload: { target: Edge | Corner | 'rotate' }
) {
2021-05-28 16:25:43 +00:00
return payload.target === 'rotate'
},
hasSelection(data) {
return data.selectedIds.size > 0
},
2021-05-28 20:30:27 +00:00
isToolLocked(data) {
return data.settings.isToolLocked
},
isPenLocked(data) {
return data.settings.isPenLocked
},
},
2021-05-09 13:04:42 +00:00
actions: {
2021-05-15 15:20:21 +00:00
/* --------------------- Shapes --------------------- */
2021-05-26 10:34:10 +00:00
createShape(data, payload, type: ShapeType) {
const shape = createShape(type, {
point: screenToWorld(payload.point, data),
style: getCurrent(data.currentStyle),
})
2021-05-23 13:46:04 +00:00
const siblings = getChildren(data, shape.parentId)
const childIndex = siblings.length
? siblings[siblings.length - 1].childIndex + 1
: 1
getShapeUtils(shape).setProperty(shape, 'childIndex', childIndex)
2021-05-17 21:27:18 +00:00
getPage(data).shapes[shape.id] = shape
2021-05-19 21:24:41 +00:00
data.selectedIds.clear()
2021-05-17 21:27:18 +00:00
data.selectedIds.add(shape.id)
},
2021-05-15 15:20:21 +00:00
/* -------------------- Sessions -------------------- */
// Shared
2021-05-10 12:16:57 +00:00
cancelSession(data) {
2021-05-15 17:11:08 +00:00
session?.cancel(data)
2021-05-10 12:16:57 +00:00
session = undefined
},
completeSession(data) {
2021-05-15 17:11:08 +00:00
session?.complete(data)
2021-05-10 12:16:57 +00:00
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-14 12:44:23 +00:00
2021-05-17 21:27:18 +00:00
// Rotating
startRotateSession(data, payload: PointerInfo) {
session = new Sessions.RotateSession(
data,
screenToWorld(payload.point, data)
)
},
keyUpdateRotateSession(data, payload: PointerInfo) {
session.update(
data,
screenToWorld(inputs.pointer.point, data),
payload.shiftKey
)
},
2021-05-17 21:27:18 +00:00
updateRotateSession(data, payload: PointerInfo) {
session.update(data, screenToWorld(payload.point, data), payload.shiftKey)
2021-05-17 21:27:18 +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(inputs.pointer.origin, data),
2021-05-20 08:19:13 +00:00
payload.altKey
2021-05-13 06:44:52 +00:00
)
},
2021-05-20 08:19:13 +00:00
keyUpdateTranslateSession(
data,
payload: { shiftKey: boolean; altKey: boolean }
) {
2021-05-19 21:24:41 +00:00
session.update(
data,
screenToWorld(inputs.pointer.point, data),
2021-05-20 08:19:13 +00:00
payload.shiftKey,
2021-05-19 21:24:41 +00:00
payload.altKey
)
},
updateTranslateSession(data, payload: PointerInfo) {
2021-05-20 08:19:13 +00:00
session.update(
data,
screenToWorld(payload.point, data),
payload.shiftKey,
payload.altKey
)
2021-05-13 06:44:52 +00:00
},
2021-05-14 12:44:23 +00:00
// Dragging / Translating
startTransformSession(
data,
payload: PointerInfo & { target: Corner | Edge }
2021-05-14 12:44:23 +00:00
) {
const point = screenToWorld(inputs.pointer.origin, data)
2021-05-19 09:35:00 +00:00
session =
data.selectedIds.size === 1
? new Sessions.TransformSingleSession(data, payload.target, point)
: new Sessions.TransformSession(data, payload.target, point)
2021-05-14 12:44:23 +00:00
},
2021-05-17 21:27:18 +00:00
startDrawTransformSession(data, payload: PointerInfo) {
session = new Sessions.TransformSingleSession(
2021-05-17 21:27:18 +00:00
data,
Corner.BottomRight,
2021-05-19 21:24:41 +00:00
screenToWorld(payload.point, data),
true
2021-05-17 21:27:18 +00:00
)
},
2021-05-21 07:42:56 +00:00
keyUpdateTransformSession(data, payload: PointerInfo) {
session.update(
data,
screenToWorld(inputs.pointer.point, data),
payload.shiftKey,
payload.altKey
)
},
2021-05-14 12:44:23 +00:00
updateTransformSession(data, payload: PointerInfo) {
2021-05-21 07:42:56 +00:00
session.update(
data,
screenToWorld(payload.point, data),
payload.shiftKey,
payload.altKey
)
2021-05-14 12:44:23 +00:00
},
2021-05-15 15:20:21 +00:00
// Direction
startDirectionSession(data, payload: PointerInfo) {
session = new Sessions.DirectionSession(
data,
screenToWorld(inputs.pointer.origin, data)
2021-05-15 15:20:21 +00:00
)
},
updateDirectionSession(data, payload: PointerInfo) {
session.update(data, screenToWorld(payload.point, data))
},
2021-05-27 17:59:40 +00:00
// Drawing
startDrawSession(data) {
const id = Array.from(data.selectedIds.values())[0]
session = new Sessions.DrawSession(
data,
id,
screenToWorld(inputs.pointer.origin, data)
)
},
updateDrawSession(data, payload: PointerInfo) {
session.update(data, screenToWorld(payload.point, data))
},
2021-05-28 20:30:27 +00:00
// Nudges
nudgeSelection(data, payload: { delta: number[]; shiftKey: boolean }) {
commands.nudge(
data,
vec.mul(
payload.delta,
payload.shiftKey
? data.settings.nudgeDistanceLarge
: data.settings.nudgeDistanceSmall
)
)
},
2021-05-15 15:20:21 +00:00
/* -------------------- Selection ------------------- */
2021-05-17 21:27:18 +00:00
selectAll(data) {
const { selectedIds } = data
const page = getPage(data)
2021-05-17 21:27:18 +00:00
selectedIds.clear()
for (let id in page.shapes) {
2021-05-17 21:27:18 +00:00
selectedIds.add(id)
}
},
2021-05-14 22:56:41 +00:00
setHoveredId(data, payload: PointerInfo) {
data.hoveredId = payload.target
},
clearHoveredId(data) {
data.hoveredId = undefined
},
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)
},
2021-05-26 19:20:52 +00:00
moveSelection(data, payload: { type: MoveType }) {
commands.move(data, payload.type)
2021-05-23 13:46:04 +00:00
},
2021-05-26 19:20:52 +00:00
alignSelection(data, payload: { type: AlignType }) {
commands.align(data, payload.type)
2021-05-23 13:46:04 +00:00
},
2021-05-26 19:20:52 +00:00
stretchSelection(data, payload: { type: StretchType }) {
commands.stretch(data, payload.type)
2021-05-23 13:46:04 +00:00
},
2021-05-26 19:20:52 +00:00
distributeSelection(data, payload: { type: DistributeType }) {
commands.distribute(data, payload.type)
2021-05-23 13:46:04 +00:00
},
2021-05-28 20:30:27 +00:00
duplicateSelection(data) {
commands.duplicate(data)
},
lockSelection(data) {
commands.toggle(data, 'isLocked')
},
hideSelection(data) {
commands.toggle(data, 'isHidden')
},
aspectLockSelection(data) {
commands.toggle(data, 'isAspectRatioLocked')
},
2021-05-23 13:46:04 +00:00
/* --------------------- Camera --------------------- */
2021-05-17 10:01:11 +00:00
resetCamera(data) {
data.camera.zoom = 1
data.camera.point = [window.innerWidth / 2, window.innerHeight / 2]
2021-05-28 16:25:43 +00:00
document.documentElement.style.setProperty('--camera-zoom', '1')
2021-05-17 10:01:11 +00:00
},
2021-05-26 10:34:10 +00:00
zoomCameraToActual(data) {
const { camera } = data
2021-05-26 10:34:10 +00:00
const center = [window.innerWidth / 2, window.innerHeight / 2]
2021-05-26 10:34:10 +00:00
const p0 = screenToWorld(center, data)
camera.zoom = 1
const p1 = screenToWorld(center, data)
camera.point = vec.add(camera.point, vec.sub(p1, p0))
setZoomCSS(camera.zoom)
},
zoomCameraToSelectionActual(data) {
const { camera } = data
const bounds = getSelectedBounds(data)
2021-05-26 10:34:10 +00:00
const mx = (window.innerWidth - bounds.width) / 2
const my = (window.innerHeight - bounds.height) / 2
2021-05-25 11:38:21 +00:00
camera.zoom = 1
2021-05-26 10:34:10 +00:00
camera.point = vec.add([-bounds.minX, -bounds.minY], [mx, my])
setZoomCSS(camera.zoom)
},
2021-05-26 10:34:10 +00:00
zoomCameraToSelection(data) {
const { camera } = data
2021-05-26 10:34:10 +00:00
const bounds = getSelectedBounds(data)
2021-05-26 10:34:10 +00:00
const zoom =
bounds.width > bounds.height
? (window.innerWidth - 128) / bounds.width
: (window.innerHeight - 128) / bounds.height
const mx = (window.innerWidth - bounds.width * zoom) / 2 / zoom
const my = (window.innerHeight - bounds.height * zoom) / 2 / zoom
camera.zoom = zoom
camera.point = vec.add([-bounds.minX, -bounds.minY], [mx, my])
setZoomCSS(camera.zoom)
},
zoomCameraToFit(data) {
const { camera } = data
2021-05-25 11:38:21 +00:00
const page = getPage(data)
const shapes = Object.values(page.shapes)
if (shapes.length === 0) {
return
}
const bounds = getCommonBounds(
...Object.values(shapes).map((shape) =>
getShapeUtils(shape).getBounds(shape)
)
)
const zoom =
2021-05-26 10:34:10 +00:00
bounds.width > bounds.height
2021-05-25 11:38:21 +00:00
? (window.innerWidth - 128) / bounds.width
: (window.innerHeight - 128) / bounds.height
2021-05-25 11:38:21 +00:00
const mx = (window.innerWidth - bounds.width * zoom) / 2 / zoom
const my = (window.innerHeight - bounds.height * zoom) / 2 / zoom
camera.zoom = zoom
2021-05-25 11:38:21 +00:00
camera.point = vec.add([-bounds.minX, -bounds.minY], [mx, my])
setZoomCSS(camera.zoom)
},
2021-05-09 13:04:42 +00:00
zoomCamera(data, payload: { delta: number; point: number[] }) {
const { camera } = data
2021-05-26 10:34:10 +00:00
const next = camera.zoom - (payload.delta / 100) * camera.zoom
2021-05-09 13:04:42 +00:00
const p0 = screenToWorld(payload.point, data)
2021-05-26 10:34:10 +00:00
camera.zoom = clamp(next, 0.1, 3)
2021-05-09 13:04:42 +00:00
const p1 = screenToWorld(payload.point, data)
camera.point = vec.add(camera.point, vec.sub(p1, p0))
setZoomCSS(camera.zoom)
2021-05-09 13:04:42 +00:00
},
2021-05-28 13:08:51 +00:00
panCamera(data, payload: { delta: number[] }) {
2021-05-09 13:04:42 +00:00
const { camera } = data
2021-05-28 13:08:51 +00:00
camera.point = vec.sub(camera.point, vec.div(payload.delta, camera.zoom))
},
pinchCamera(
data,
payload: {
delta: number[]
distanceDelta: number
angleDelta: number
point: number[]
}
) {
const { camera } = data
camera.point = vec.sub(camera.point, vec.div(payload.delta, camera.zoom))
const next = camera.zoom - (payload.distanceDelta / 300) * camera.zoom
const p0 = screenToWorld(payload.point, data)
camera.zoom = clamp(next, 0.1, 3)
const p1 = screenToWorld(payload.point, data)
camera.point = vec.add(camera.point, vec.sub(p1, p0))
setZoomCSS(camera.zoom)
2021-05-09 13:04:42 +00:00
},
2021-05-15 15:20:21 +00:00
deleteSelectedIds(data) {
2021-05-23 17:09:23 +00:00
commands.deleteSelected(data)
2021-05-15 15:20:21 +00:00
},
2021-05-17 21:27:18 +00:00
/* ---------------------- History ---------------------- */
2021-05-15 15:20:21 +00:00
// History
2021-05-19 21:24:41 +00:00
popHistory() {
history.pop()
},
2021-05-17 21:27:18 +00:00
forceSave(data) {
history.save(data)
},
2021-05-15 15:20:21 +00:00
enableHistory() {
history.enable()
},
disableHistory() {
history.disable()
},
undo(data) {
history.undo(data)
},
redo(data) {
history.redo(data)
},
2021-05-26 10:34:10 +00:00
/* --------------------- Styles --------------------- */
toggleStylePanel(data) {
data.settings.isStyleOpen = !data.settings.isStyleOpen
},
updateStyles(data, payload: Partial<ShapeStyles>) {
Object.assign(data.currentStyle, payload)
},
applyStylesToSelection(data, payload: Partial<ShapeStyles>) {
commands.style(data, payload)
},
2021-05-17 21:27:18 +00:00
/* ---------------------- Code ---------------------- */
2021-05-28 20:30:27 +00:00
2021-05-17 21:27:18 +00:00
closeCodePanel(data) {
data.settings.isCodeOpen = false
},
openCodePanel(data) {
data.settings.isCodeOpen = true
},
toggleCodePanel(data) {
data.settings.isCodeOpen = !data.settings.isCodeOpen
},
2021-05-17 10:01:11 +00:00
setGeneratedShapes(
data,
payload: { shapes: Shape[]; controls: CodeControl[] }
) {
commands.generate(data, data.currentPageId, payload.shapes)
},
setCodeControls(data, payload: { controls: CodeControl[] }) {
data.codeControls = Object.fromEntries(
payload.controls.map((control) => [control.id, control])
)
2021-05-15 15:20:21 +00:00
},
increaseCodeFontSize(data) {
data.settings.fontSize++
},
decreaseCodeFontSize(data) {
data.settings.fontSize--
},
2021-05-17 10:01:11 +00:00
updateControls(data, payload: { [key: string]: any }) {
for (let key in payload) {
data.codeControls[key].value = payload[key]
}
history.disable()
data.selectedIds.clear()
try {
const { shapes } = updateFromCode(
data.document.code[data.currentCodeFileId].code,
data.codeControls
)
commands.generate(data, data.currentPageId, shapes)
} catch (e) {
console.error(e)
}
history.enable()
},
2021-05-28 20:30:27 +00:00
/* -------------------- Settings -------------------- */
enablePenLock(data) {
data.settings.isPenLocked = true
},
disablePenLock(data) {
data.settings.isPenLocked = false
},
toggleToolLock(data) {
data.settings.isToolLocked = !data.settings.isToolLocked
},
/* ---------------------- Data ---------------------- */
2021-05-16 08:33:08 +00:00
saveCode(data, payload: { code: string }) {
data.document.code[data.currentCodeFileId].code = payload.code
history.save(data)
},
restoreSavedData(data) {
2021-05-27 22:07:27 +00:00
history.load(data)
},
2021-05-18 08:32:20 +00:00
clearBoundsRotation(data) {
data.boundsRotation = 0
},
2021-05-09 13:04:42 +00:00
},
values: {
selectedIds(data) {
return new Set(data.selectedIds)
},
2021-05-12 22:08:53 +00:00
selectedBounds(data) {
const { selectedIds } = data
const page = getPage(data)
2021-05-12 22:08:53 +00:00
2021-05-17 10:01:11 +00:00
const shapes = Array.from(selectedIds.values())
.map((id) => page.shapes[id])
2021-05-17 10:01:11 +00:00
.filter(Boolean)
2021-05-15 13:02:13 +00:00
2021-05-14 12:44:23 +00:00
if (selectedIds.size === 0) return null
2021-05-18 08:32:20 +00:00
if (selectedIds.size === 1) {
2021-05-20 08:19:13 +00:00
if (!shapes[0]) {
2021-05-28 16:25:43 +00:00
console.error('Could not find that shape! Clearing selected IDs.')
2021-05-20 08:19:13 +00:00
data.selectedIds.clear()
return null
}
2021-05-18 08:32:20 +00:00
const shapeUtils = getShapeUtils(shapes[0])
if (!shapeUtils.canTransform) return null
return shapeUtils.getBounds(shapes[0])
2021-05-15 13:02:13 +00:00
}
2021-05-12 22:08:53 +00:00
return getCommonBounds(
2021-05-18 08:32:20 +00:00
...shapes.map((shape) => getShapeUtils(shape).getRotatedBounds(shape))
2021-05-12 22:08:53 +00:00
)
},
},
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)