You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

state.ts 48KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645
  1. import { createSelectorHook, createState } from '@state-designer/react'
  2. import { updateFromCode } from 'lib/code/generate'
  3. import { createShape, getShapeUtils } from 'lib/shape-utils'
  4. import * as vec from 'utils/vec'
  5. import inputs from './inputs'
  6. import { defaultDocument } from './data'
  7. import history from './history'
  8. import storage from './storage'
  9. import * as Sessions from './sessions'
  10. import commands from './commands'
  11. import {
  12. getChildren,
  13. getCommonBounds,
  14. getCurrent,
  15. getCurrentCamera,
  16. getPage,
  17. getSelectedBounds,
  18. getSelectedShapes,
  19. getShape,
  20. screenToWorld,
  21. setZoomCSS,
  22. translateBounds,
  23. getParentOffset,
  24. getParentRotation,
  25. rotateBounds,
  26. getBoundsCenter,
  27. getDocumentBranch,
  28. getCameraZoom,
  29. getSelectedIds,
  30. setSelectedIds,
  31. } from 'utils/utils'
  32. import {
  33. Data,
  34. PointerInfo,
  35. Shape,
  36. ShapeType,
  37. Corner,
  38. Edge,
  39. CodeControl,
  40. MoveType,
  41. ShapeStyles,
  42. DistributeType,
  43. AlignType,
  44. StretchType,
  45. DashStyle,
  46. SizeStyle,
  47. ColorStyle,
  48. Bounds,
  49. } from 'types'
  50. import session from './session'
  51. import { pointInBounds } from 'utils/bounds'
  52. const initialData: Data = {
  53. isReadOnly: false,
  54. settings: {
  55. fontSize: 13,
  56. isDarkMode: false,
  57. isCodeOpen: false,
  58. isStyleOpen: false,
  59. isToolLocked: false,
  60. isPenLocked: false,
  61. nudgeDistanceLarge: 10,
  62. nudgeDistanceSmall: 1,
  63. },
  64. currentStyle: {
  65. size: SizeStyle.Medium,
  66. color: ColorStyle.Black,
  67. dash: DashStyle.Solid,
  68. isFilled: false,
  69. },
  70. activeTool: 'select',
  71. brush: undefined,
  72. boundsRotation: 0,
  73. pointedId: null,
  74. hoveredId: null,
  75. currentPageId: 'page1',
  76. currentParentId: 'page1',
  77. currentCodeFileId: 'file0',
  78. codeControls: {},
  79. document: defaultDocument,
  80. pageStates: {
  81. page1: {
  82. selectedIds: new Set([]),
  83. camera: {
  84. point: [0, 0],
  85. zoom: 1,
  86. },
  87. },
  88. page2: {
  89. selectedIds: new Set([]),
  90. camera: {
  91. point: [0, 0],
  92. zoom: 1,
  93. },
  94. },
  95. },
  96. }
  97. const state = createState({
  98. data: initialData,
  99. on: {
  100. UNMOUNTED: [{ unless: 'isReadOnly', do: 'forceSave' }, { to: 'loading' }],
  101. },
  102. initial: 'loading',
  103. states: {
  104. loading: {
  105. on: {
  106. MOUNTED: [
  107. 'restoreSavedData',
  108. {
  109. to: 'ready',
  110. },
  111. ],
  112. },
  113. },
  114. ready: {
  115. onEnter: {
  116. wait: 0.01,
  117. if: 'hasSelection',
  118. do: 'zoomCameraToSelectionActual',
  119. else: ['zoomCameraToFit', 'zoomCameraToActual'],
  120. },
  121. on: {
  122. ZOOMED_CAMERA: {
  123. do: 'zoomCamera',
  124. },
  125. PANNED_CAMERA: {
  126. do: 'panCamera',
  127. },
  128. ZOOMED_TO_ACTUAL: {
  129. if: 'hasSelection',
  130. do: 'zoomCameraToSelectionActual',
  131. else: 'zoomCameraToActual',
  132. },
  133. ZOOMED_TO_SELECTION: {
  134. if: 'hasSelection',
  135. do: 'zoomCameraToSelection',
  136. },
  137. ZOOMED_TO_FIT: ['zoomCameraToFit', 'zoomCameraToActual'],
  138. ZOOMED_IN: 'zoomIn',
  139. ZOOMED_OUT: 'zoomOut',
  140. RESET_CAMERA: 'resetCamera',
  141. TOGGLED_SHAPE_LOCK: { if: 'hasSelection', do: 'lockSelection' },
  142. TOGGLED_SHAPE_HIDE: { if: 'hasSelection', do: 'hideSelection' },
  143. TOGGLED_SHAPE_ASPECT_LOCK: {
  144. if: 'hasSelection',
  145. do: 'aspectLockSelection',
  146. },
  147. SELECTED_SELECT_TOOL: { to: 'selecting' },
  148. SELECTED_DRAW_TOOL: { unless: 'isReadOnly', to: 'draw' },
  149. SELECTED_ARROW_TOOL: { unless: 'isReadOnly', to: 'arrow' },
  150. SELECTED_DOT_TOOL: { unless: 'isReadOnly', to: 'dot' },
  151. SELECTED_CIRCLE_TOOL: { unless: 'isReadOnly', to: 'circle' },
  152. SELECTED_ELLIPSE_TOOL: { unless: 'isReadOnly', to: 'ellipse' },
  153. SELECTED_RAY_TOOL: { unless: 'isReadOnly', to: 'ray' },
  154. SELECTED_LINE_TOOL: { unless: 'isReadOnly', to: 'line' },
  155. SELECTED_POLYLINE_TOOL: { unless: 'isReadOnly', to: 'polyline' },
  156. SELECTED_RECTANGLE_TOOL: { unless: 'isReadOnly', to: 'rectangle' },
  157. TOGGLED_CODE_PANEL_OPEN: 'toggleCodePanel',
  158. TOGGLED_STYLE_PANEL_OPEN: 'toggleStylePanel',
  159. POINTED_CANVAS: ['closeStylePanel', 'clearCurrentParentId'],
  160. CHANGED_STYLE: ['updateStyles', 'applyStylesToSelection'],
  161. SELECTED_ALL: { to: 'selecting', do: 'selectAll' },
  162. NUDGED: { do: 'nudgeSelection' },
  163. USED_PEN_DEVICE: 'enablePenLock',
  164. DISABLED_PEN_LOCK: 'disablePenLock',
  165. CLEARED_PAGE: {
  166. if: 'hasSelection',
  167. do: 'deleteSelection',
  168. else: ['selectAll', 'deleteSelection'],
  169. },
  170. CHANGED_PAGE: 'changePage',
  171. CREATED_PAGE: ['clearSelectedIds', 'createPage'],
  172. DELETED_PAGE: { unless: 'hasOnlyOnePage', do: 'deletePage' },
  173. },
  174. initial: 'selecting',
  175. states: {
  176. selecting: {
  177. onEnter: 'setActiveToolSelect',
  178. on: {
  179. SAVED: 'forceSave',
  180. UNDO: 'undo',
  181. REDO: 'redo',
  182. SAVED_CODE: 'saveCode',
  183. DELETED: 'deleteSelection',
  184. INCREASED_CODE_FONT_SIZE: 'increaseCodeFontSize',
  185. DECREASED_CODE_FONT_SIZE: 'decreaseCodeFontSize',
  186. CHANGED_CODE_CONTROL: 'updateControls',
  187. GENERATED_FROM_CODE: ['setCodeControls', 'setGeneratedShapes'],
  188. TOGGLED_TOOL_LOCK: 'toggleToolLock',
  189. MOVED_TO_PAGE: {
  190. if: 'hasSelection',
  191. do: ['moveSelectionToPage', 'zoomCameraToSelectionActual'],
  192. },
  193. MOVED: { if: 'hasSelection', do: 'moveSelection' },
  194. DUPLICATED: { if: 'hasSelection', do: 'duplicateSelection' },
  195. ROTATED_CCW: { if: 'hasSelection', do: 'rotateSelectionCcw' },
  196. ALIGNED: { if: 'hasMultipleSelection', do: 'alignSelection' },
  197. STRETCHED: { if: 'hasMultipleSelection', do: 'stretchSelection' },
  198. DISTRIBUTED: {
  199. if: 'hasMultipleSelection',
  200. do: 'distributeSelection',
  201. },
  202. GROUPED: { if: 'hasMultipleSelection', do: 'groupSelection' },
  203. UNGROUPED: {
  204. if: ['hasSelection', 'selectionIncludesGroups'],
  205. do: 'ungroupSelection',
  206. },
  207. },
  208. initial: 'notPointing',
  209. states: {
  210. notPointing: {
  211. on: {
  212. CANCELLED: 'clearSelectedIds',
  213. STARTED_PINCHING: { to: 'pinching' },
  214. POINTED_CANVAS: { to: 'brushSelecting' },
  215. POINTED_BOUNDS: [
  216. {
  217. if: 'isPressingMetaKey',
  218. to: 'brushSelecting',
  219. },
  220. { to: 'pointingBounds' },
  221. ],
  222. POINTED_BOUNDS_HANDLE: {
  223. if: 'isPointingRotationHandle',
  224. to: 'rotatingSelection',
  225. else: { to: 'transformingSelection' },
  226. },
  227. POINTED_HANDLE: { to: 'translatingHandles' },
  228. MOVED_OVER_SHAPE: {
  229. if: 'pointHitsShape',
  230. then: {
  231. unless: 'shapeIsHovered',
  232. do: 'setHoveredId',
  233. },
  234. else: {
  235. if: 'shapeIsHovered',
  236. do: 'clearHoveredId',
  237. },
  238. },
  239. UNHOVERED_SHAPE: 'clearHoveredId',
  240. DOUBLE_POINTED_SHAPE: [
  241. {
  242. unless: 'isPressingShiftKey',
  243. do: [
  244. 'setDrilledPointedId',
  245. 'clearSelectedIds',
  246. 'pushPointedIdToSelectedIds',
  247. ],
  248. to: 'pointingBounds',
  249. },
  250. ],
  251. POINTED_SHAPE: [
  252. {
  253. if: 'isPressingMetaKey',
  254. to: 'brushSelecting',
  255. },
  256. 'setPointedId',
  257. {
  258. if: 'pointInSelectionBounds',
  259. to: 'pointingBounds',
  260. },
  261. {
  262. unless: 'isPointedShapeSelected',
  263. then: {
  264. if: 'isPressingShiftKey',
  265. do: ['pushPointedIdToSelectedIds', 'clearPointedId'],
  266. else: ['clearSelectedIds', 'pushPointedIdToSelectedIds'],
  267. },
  268. },
  269. {
  270. to: 'pointingBounds',
  271. },
  272. ],
  273. RIGHT_POINTED: [
  274. {
  275. if: 'isPointingCanvas',
  276. do: 'clearSelectedIds',
  277. else: {
  278. if: 'isPointingShape',
  279. then: [
  280. 'setPointedId',
  281. {
  282. unless: 'isPointedShapeSelected',
  283. do: [
  284. 'clearSelectedIds',
  285. 'pushPointedIdToSelectedIds',
  286. ],
  287. },
  288. ],
  289. },
  290. },
  291. ],
  292. },
  293. },
  294. pointingBounds: {
  295. on: {
  296. STOPPED_POINTING: [
  297. {
  298. if: 'isPressingShiftKey',
  299. then: [
  300. {
  301. if: 'isPointedShapeSelected',
  302. do: 'pullPointedIdFromSelectedIds',
  303. },
  304. ],
  305. else: {
  306. unless: 'isPointingBounds',
  307. do: ['clearSelectedIds', 'pushPointedIdToSelectedIds'],
  308. },
  309. },
  310. { to: 'notPointing' },
  311. ],
  312. MOVED_POINTER: {
  313. unless: 'isReadOnly',
  314. if: 'distanceImpliesDrag',
  315. to: 'translatingSelection',
  316. },
  317. },
  318. },
  319. rotatingSelection: {
  320. onEnter: 'startRotateSession',
  321. onExit: 'clearBoundsRotation',
  322. on: {
  323. MOVED_POINTER: 'updateRotateSession',
  324. PANNED_CAMERA: 'updateRotateSession',
  325. PRESSED_SHIFT_KEY: 'keyUpdateRotateSession',
  326. RELEASED_SHIFT_KEY: 'keyUpdateRotateSession',
  327. STOPPED_POINTING: { do: 'completeSession', to: 'selecting' },
  328. CANCELLED: { do: 'cancelSession', to: 'selecting' },
  329. },
  330. },
  331. transformingSelection: {
  332. onEnter: 'startTransformSession',
  333. on: {
  334. MOVED_POINTER: 'updateTransformSession',
  335. PANNED_CAMERA: 'updateTransformSession',
  336. PRESSED_SHIFT_KEY: 'keyUpdateTransformSession',
  337. RELEASED_SHIFT_KEY: 'keyUpdateTransformSession',
  338. STOPPED_POINTING: { do: 'completeSession', to: 'selecting' },
  339. CANCELLED: { do: 'cancelSession', to: 'selecting' },
  340. },
  341. },
  342. translatingSelection: {
  343. onEnter: 'startTranslateSession',
  344. on: {
  345. MOVED_POINTER: 'updateTranslateSession',
  346. PANNED_CAMERA: 'updateTranslateSession',
  347. PRESSED_SHIFT_KEY: 'keyUpdateTranslateSession',
  348. RELEASED_SHIFT_KEY: 'keyUpdateTranslateSession',
  349. PRESSED_ALT_KEY: 'keyUpdateTranslateSession',
  350. RELEASED_ALT_KEY: 'keyUpdateTranslateSession',
  351. STOPPED_POINTING: { do: 'completeSession', to: 'selecting' },
  352. CANCELLED: { do: 'cancelSession', to: 'selecting' },
  353. },
  354. },
  355. translatingHandles: {
  356. onEnter: 'startHandleSession',
  357. on: {
  358. MOVED_POINTER: 'updateHandleSession',
  359. PANNED_CAMERA: 'updateHandleSession',
  360. PRESSED_SHIFT_KEY: 'keyUpdateHandleSession',
  361. RELEASED_SHIFT_KEY: 'keyUpdateHandleSession',
  362. STOPPED_POINTING: { do: 'completeSession', to: 'selecting' },
  363. CANCELLED: { do: 'cancelSession', to: 'selecting' },
  364. },
  365. },
  366. brushSelecting: {
  367. onEnter: [
  368. {
  369. unless: ['isPressingMetaKey', 'isPressingShiftKey'],
  370. do: 'clearSelectedIds',
  371. },
  372. 'clearBoundsRotation',
  373. 'startBrushSession',
  374. ],
  375. on: {
  376. STARTED_PINCHING: { do: 'completeSession', to: 'pinching' },
  377. // Currently using hacks.fastBrushSelect
  378. // MOVED_POINTER: 'updateBrushSession',
  379. PANNED_CAMERA: 'updateBrushSession',
  380. STOPPED_POINTING: { do: 'completeSession', to: 'selecting' },
  381. CANCELLED: { do: 'cancelSession', to: 'selecting' },
  382. },
  383. },
  384. },
  385. },
  386. pinching: {
  387. on: {
  388. // Pinching uses hacks.fastPinchCamera
  389. // PINCHED: { do: 'pinchCamera' },
  390. },
  391. initial: 'selectPinching',
  392. onExit: { secretlyDo: 'updateZoomCSS' },
  393. states: {
  394. selectPinching: {
  395. on: {
  396. STOPPED_PINCHING: { to: 'selecting' },
  397. },
  398. },
  399. toolPinching: {
  400. on: {
  401. STOPPED_PINCHING: { to: 'usingTool.previous' },
  402. },
  403. },
  404. },
  405. },
  406. usingTool: {
  407. initial: 'draw',
  408. onEnter: 'clearSelectedIds',
  409. on: {
  410. STARTED_PINCHING: {
  411. do: 'breakSession',
  412. to: 'pinching.toolPinching',
  413. },
  414. TOGGLED_TOOL_LOCK: 'toggleToolLock',
  415. },
  416. states: {
  417. draw: {
  418. onEnter: 'setActiveToolDraw',
  419. initial: 'creating',
  420. states: {
  421. creating: {
  422. on: {
  423. CANCELLED: { to: 'selecting' },
  424. POINTED_SHAPE: {
  425. get: 'newDraw',
  426. do: 'createShape',
  427. to: 'draw.editing',
  428. },
  429. POINTED_CANVAS: {
  430. get: 'newDraw',
  431. do: 'createShape',
  432. to: 'draw.editing',
  433. },
  434. UNDO: { do: 'undo' },
  435. REDO: { do: 'redo' },
  436. },
  437. },
  438. editing: {
  439. onEnter: 'startDrawSession',
  440. on: {
  441. CANCELLED: {
  442. do: 'breakSession',
  443. to: 'selecting',
  444. },
  445. STOPPED_POINTING: {
  446. do: 'completeSession',
  447. to: 'draw.creating',
  448. },
  449. PRESSED_SHIFT: 'keyUpdateDrawSession',
  450. RELEASED_SHIFT: 'keyUpdateDrawSession',
  451. // MOVED_POINTER: 'updateDrawSession',
  452. PANNED_CAMERA: 'updateDrawSession',
  453. },
  454. },
  455. },
  456. },
  457. dot: {
  458. onEnter: 'setActiveToolDot',
  459. initial: 'creating',
  460. states: {
  461. creating: {
  462. on: {
  463. CANCELLED: { to: 'selecting' },
  464. POINTED_SHAPE: {
  465. get: 'newDot',
  466. do: 'createShape',
  467. to: 'dot.editing',
  468. },
  469. POINTED_CANVAS: {
  470. get: 'newDot',
  471. do: 'createShape',
  472. to: 'dot.editing',
  473. },
  474. },
  475. },
  476. editing: {
  477. on: {
  478. STOPPED_POINTING: [
  479. 'completeSession',
  480. {
  481. if: 'isToolLocked',
  482. to: 'dot.creating',
  483. else: {
  484. to: 'selecting',
  485. },
  486. },
  487. ],
  488. CANCELLED: {
  489. do: 'breakSession',
  490. to: 'selecting',
  491. },
  492. },
  493. initial: 'inactive',
  494. states: {
  495. inactive: {
  496. on: {
  497. MOVED_POINTER: {
  498. if: 'distanceImpliesDrag',
  499. to: 'dot.editing.active',
  500. },
  501. },
  502. },
  503. active: {
  504. onEnter: 'startTranslateSession',
  505. on: {
  506. MOVED_POINTER: 'updateTranslateSession',
  507. PANNED_CAMERA: 'updateTranslateSession',
  508. },
  509. },
  510. },
  511. },
  512. },
  513. },
  514. arrow: {
  515. onEnter: 'setActiveToolArrow',
  516. initial: 'creating',
  517. states: {
  518. creating: {
  519. on: {
  520. CANCELLED: { to: 'selecting' },
  521. POINTED_SHAPE: {
  522. get: 'newArrow',
  523. do: 'createShape',
  524. to: 'arrow.editing',
  525. },
  526. POINTED_CANVAS: {
  527. get: 'newArrow',
  528. do: 'createShape',
  529. to: 'arrow.editing',
  530. },
  531. UNDO: { do: 'undo' },
  532. REDO: { do: 'redo' },
  533. },
  534. },
  535. editing: {
  536. onEnter: 'startArrowSession',
  537. on: {
  538. STOPPED_POINTING: [
  539. 'completeSession',
  540. {
  541. if: 'isToolLocked',
  542. to: 'arrow.creating',
  543. else: { to: 'selecting' },
  544. },
  545. ],
  546. CANCELLED: {
  547. do: 'breakSession',
  548. if: 'isToolLocked',
  549. to: 'arrow.creating',
  550. else: { to: 'selecting' },
  551. },
  552. PRESSED_SHIFT: 'keyUpdateArrowSession',
  553. RELEASED_SHIFT: 'keyUpdateArrowSession',
  554. MOVED_POINTER: 'updateArrowSession',
  555. PANNED_CAMERA: 'updateArrowSession',
  556. },
  557. },
  558. },
  559. },
  560. circle: {
  561. onEnter: 'setActiveToolCircle',
  562. initial: 'creating',
  563. states: {
  564. creating: {
  565. on: {
  566. CANCELLED: { to: 'selecting' },
  567. POINTED_SHAPE: {
  568. to: 'circle.editing',
  569. },
  570. POINTED_CANVAS: {
  571. to: 'circle.editing',
  572. },
  573. },
  574. },
  575. editing: {
  576. on: {
  577. STOPPED_POINTING: { to: 'selecting' },
  578. CANCELLED: { to: 'selecting' },
  579. MOVED_POINTER: {
  580. if: 'distanceImpliesDrag',
  581. then: {
  582. get: 'newCircle',
  583. do: 'createShape',
  584. to: 'drawingShape.bounds',
  585. },
  586. },
  587. },
  588. },
  589. },
  590. },
  591. ellipse: {
  592. onEnter: 'setActiveToolEllipse',
  593. initial: 'creating',
  594. states: {
  595. creating: {
  596. on: {
  597. CANCELLED: { to: 'selecting' },
  598. POINTED_CANVAS: {
  599. to: 'ellipse.editing',
  600. },
  601. },
  602. },
  603. editing: {
  604. on: {
  605. STOPPED_POINTING: { to: 'selecting' },
  606. CANCELLED: { to: 'selecting' },
  607. MOVED_POINTER: {
  608. if: 'distanceImpliesDrag',
  609. then: {
  610. get: 'newEllipse',
  611. do: 'createShape',
  612. to: 'drawingShape.bounds',
  613. },
  614. },
  615. },
  616. },
  617. },
  618. },
  619. rectangle: {
  620. onEnter: 'setActiveToolRectangle',
  621. initial: 'creating',
  622. states: {
  623. creating: {
  624. on: {
  625. CANCELLED: { to: 'selecting' },
  626. POINTED_SHAPE: {
  627. to: 'rectangle.editing',
  628. },
  629. POINTED_CANVAS: {
  630. to: 'rectangle.editing',
  631. },
  632. },
  633. },
  634. editing: {
  635. on: {
  636. STOPPED_POINTING: { to: 'selecting' },
  637. CANCELLED: { to: 'selecting' },
  638. MOVED_POINTER: {
  639. if: 'distanceImpliesDrag',
  640. then: {
  641. get: 'newRectangle',
  642. do: 'createShape',
  643. to: 'drawingShape.bounds',
  644. },
  645. },
  646. },
  647. },
  648. },
  649. },
  650. ray: {
  651. onEnter: 'setActiveToolRay',
  652. initial: 'creating',
  653. states: {
  654. creating: {
  655. on: {
  656. CANCELLED: { to: 'selecting' },
  657. POINTED_SHAPE: {
  658. get: 'newRay',
  659. do: 'createShape',
  660. to: 'ray.editing',
  661. },
  662. POINTED_CANVAS: {
  663. get: 'newRay',
  664. do: 'createShape',
  665. to: 'ray.editing',
  666. },
  667. },
  668. },
  669. editing: {
  670. on: {
  671. STOPPED_POINTING: { to: 'selecting' },
  672. CANCELLED: { to: 'selecting' },
  673. MOVED_POINTER: {
  674. if: 'distanceImpliesDrag',
  675. to: 'drawingShape.direction',
  676. },
  677. },
  678. },
  679. },
  680. },
  681. line: {
  682. onEnter: 'setActiveToolLine',
  683. initial: 'creating',
  684. states: {
  685. creating: {
  686. on: {
  687. CANCELLED: { to: 'selecting' },
  688. POINTED_SHAPE: {
  689. get: 'newLine',
  690. do: 'createShape',
  691. to: 'line.editing',
  692. },
  693. POINTED_CANVAS: {
  694. get: 'newLine',
  695. do: 'createShape',
  696. to: 'line.editing',
  697. },
  698. },
  699. },
  700. editing: {
  701. on: {
  702. STOPPED_POINTING: { to: 'selecting' },
  703. CANCELLED: { to: 'selecting' },
  704. MOVED_POINTER: {
  705. if: 'distanceImpliesDrag',
  706. to: 'drawingShape.direction',
  707. },
  708. },
  709. },
  710. },
  711. },
  712. polyline: {
  713. onEnter: 'setActiveToolPolyline',
  714. },
  715. },
  716. },
  717. drawingShape: {
  718. on: {
  719. STOPPED_POINTING: [
  720. 'completeSession',
  721. {
  722. if: 'isToolLocked',
  723. to: 'usingTool.previous',
  724. else: { to: 'selecting' },
  725. },
  726. ],
  727. CANCELLED: {
  728. do: 'breakSession',
  729. to: 'selecting',
  730. },
  731. },
  732. initial: 'drawingShapeBounds',
  733. states: {
  734. bounds: {
  735. onEnter: 'startDrawTransformSession',
  736. on: {
  737. MOVED_POINTER: 'updateTransformSession',
  738. PANNED_CAMERA: 'updateTransformSession',
  739. },
  740. },
  741. direction: {
  742. onEnter: 'startDirectionSession',
  743. on: {
  744. MOVED_POINTER: 'updateDirectionSession',
  745. PANNED_CAMERA: 'updateDirectionSession',
  746. },
  747. },
  748. },
  749. },
  750. },
  751. },
  752. },
  753. results: {
  754. newArrow() {
  755. return ShapeType.Arrow
  756. },
  757. newDraw() {
  758. return ShapeType.Draw
  759. },
  760. newDot() {
  761. return ShapeType.Dot
  762. },
  763. newRay() {
  764. return ShapeType.Ray
  765. },
  766. newLine() {
  767. return ShapeType.Line
  768. },
  769. newCircle() {
  770. return ShapeType.Circle
  771. },
  772. newEllipse() {
  773. return ShapeType.Ellipse
  774. },
  775. newRectangle() {
  776. return ShapeType.Rectangle
  777. },
  778. },
  779. conditions: {
  780. isPointingCanvas(data, payload: PointerInfo) {
  781. return payload.target === 'canvas'
  782. },
  783. isPointingBounds(data, payload: PointerInfo) {
  784. return getSelectedIds(data).size > 0 && payload.target === 'bounds'
  785. },
  786. isPointingShape(data, payload: PointerInfo) {
  787. return (
  788. payload.target &&
  789. payload.target !== 'canvas' &&
  790. payload.target !== 'bounds'
  791. )
  792. },
  793. isReadOnly(data) {
  794. return data.isReadOnly
  795. },
  796. distanceImpliesDrag(data, payload: PointerInfo) {
  797. return vec.dist2(payload.origin, payload.point) > 8
  798. },
  799. hasPointedTarget(data, payload: PointerInfo) {
  800. return payload.target !== undefined
  801. },
  802. isPointedShapeSelected(data) {
  803. return getSelectedIds(data).has(data.pointedId)
  804. },
  805. isPressingShiftKey(data, payload: PointerInfo) {
  806. return payload.shiftKey
  807. },
  808. isPressingMetaKey(data, payload: PointerInfo) {
  809. return payload.metaKey
  810. },
  811. shapeIsHovered(data, payload: { target: string }) {
  812. return data.hoveredId === payload.target
  813. },
  814. pointInSelectionBounds(data, payload: PointerInfo) {
  815. if (getSelectedIds(data).size === 0) return false
  816. return pointInBounds(
  817. screenToWorld(payload.point, data),
  818. getSelectionBounds(data)
  819. )
  820. },
  821. pointHitsShape(data, payload: PointerInfo) {
  822. const shape = getShape(data, payload.target)
  823. return getShapeUtils(shape).hitTest(
  824. shape,
  825. screenToWorld(payload.point, data)
  826. )
  827. },
  828. isPointingRotationHandle(
  829. data,
  830. payload: { target: Edge | Corner | 'rotate' }
  831. ) {
  832. return payload.target === 'rotate'
  833. },
  834. hasSelection(data) {
  835. return getSelectedIds(data).size > 0
  836. },
  837. hasMultipleSelection(data) {
  838. return getSelectedIds(data).size > 1
  839. },
  840. isToolLocked(data) {
  841. return data.settings.isToolLocked
  842. },
  843. isPenLocked(data) {
  844. return data.settings.isPenLocked
  845. },
  846. hasOnlyOnePage(data) {
  847. return Object.keys(data.document.pages).length === 1
  848. },
  849. selectionIncludesGroups(data) {
  850. return getSelectedShapes(data).some(
  851. (shape) => shape.type === ShapeType.Group
  852. )
  853. },
  854. },
  855. actions: {
  856. /* ---------------------- Pages --------------------- */
  857. changePage(data, payload: { id: string }) {
  858. commands.changePage(data, payload.id)
  859. },
  860. createPage(data) {
  861. commands.createPage(data)
  862. },
  863. deletePage(data, payload: { id: string }) {
  864. commands.deletePage(data, payload.id)
  865. },
  866. /* --------------------- Shapes --------------------- */
  867. createShape(data, payload, type: ShapeType) {
  868. const shape = createShape(type, {
  869. parentId: data.currentPageId,
  870. point: screenToWorld(payload.point, data),
  871. style: getCurrent(data.currentStyle),
  872. })
  873. const siblings = getChildren(data, shape.parentId)
  874. const childIndex = siblings.length
  875. ? siblings[siblings.length - 1].childIndex + 1
  876. : 1
  877. getShapeUtils(shape).setProperty(shape, 'childIndex', childIndex)
  878. getPage(data).shapes[shape.id] = shape
  879. setSelectedIds(data, [shape.id])
  880. },
  881. /* -------------------- Sessions -------------------- */
  882. // Shared
  883. breakSession(data) {
  884. session.current?.cancel(data)
  885. session.clear()
  886. history.disable()
  887. commands.deleteSelected(data)
  888. history.enable()
  889. },
  890. cancelSession(data) {
  891. session.current?.cancel(data)
  892. session.clear()
  893. },
  894. completeSession(data) {
  895. session.current?.complete(data)
  896. session.clear()
  897. },
  898. // Brushing
  899. startBrushSession(data, payload: PointerInfo) {
  900. session.current = new Sessions.BrushSession(
  901. data,
  902. screenToWorld(payload.point, data)
  903. )
  904. },
  905. updateBrushSession(data, payload: PointerInfo) {
  906. session.current.update(data, screenToWorld(payload.point, data))
  907. },
  908. // Rotating
  909. startRotateSession(data, payload: PointerInfo) {
  910. session.current = new Sessions.RotateSession(
  911. data,
  912. screenToWorld(payload.point, data)
  913. )
  914. },
  915. keyUpdateRotateSession(data, payload: PointerInfo) {
  916. session.current.update(
  917. data,
  918. screenToWorld(inputs.pointer.point, data),
  919. payload.shiftKey
  920. )
  921. },
  922. updateRotateSession(data, payload: PointerInfo) {
  923. session.current.update(
  924. data,
  925. screenToWorld(payload.point, data),
  926. payload.shiftKey
  927. )
  928. },
  929. // Dragging / Translating
  930. startTranslateSession(data) {
  931. session.current = new Sessions.TranslateSession(
  932. data,
  933. screenToWorld(inputs.pointer.origin, data)
  934. )
  935. },
  936. keyUpdateTranslateSession(
  937. data,
  938. payload: { shiftKey: boolean; altKey: boolean }
  939. ) {
  940. session.current.update(
  941. data,
  942. screenToWorld(inputs.pointer.point, data),
  943. payload.shiftKey,
  944. payload.altKey
  945. )
  946. },
  947. updateTranslateSession(data, payload: PointerInfo) {
  948. session.current.update(
  949. data,
  950. screenToWorld(payload.point, data),
  951. payload.shiftKey,
  952. payload.altKey
  953. )
  954. },
  955. // Dragging Handle
  956. startHandleSession(data, payload: PointerInfo) {
  957. const shapeId = Array.from(getSelectedIds(data).values())[0]
  958. const handleId = payload.target
  959. session.current = new Sessions.HandleSession(
  960. data,
  961. shapeId,
  962. handleId,
  963. screenToWorld(inputs.pointer.origin, data)
  964. )
  965. },
  966. keyUpdateHandleSession(
  967. data,
  968. payload: { shiftKey: boolean; altKey: boolean }
  969. ) {
  970. session.current.update(
  971. data,
  972. screenToWorld(inputs.pointer.point, data),
  973. payload.shiftKey,
  974. payload.altKey
  975. )
  976. },
  977. updateHandleSession(data, payload: PointerInfo) {
  978. session.current.update(
  979. data,
  980. screenToWorld(payload.point, data),
  981. payload.shiftKey,
  982. payload.altKey
  983. )
  984. },
  985. // Transforming
  986. startTransformSession(
  987. data,
  988. payload: PointerInfo & { target: Corner | Edge }
  989. ) {
  990. const point = screenToWorld(inputs.pointer.origin, data)
  991. session.current =
  992. getSelectedIds(data).size === 1
  993. ? new Sessions.TransformSingleSession(data, payload.target, point)
  994. : new Sessions.TransformSession(data, payload.target, point)
  995. },
  996. startDrawTransformSession(data, payload: PointerInfo) {
  997. session.current = new Sessions.TransformSingleSession(
  998. data,
  999. Corner.BottomRight,
  1000. screenToWorld(payload.point, data),
  1001. true
  1002. )
  1003. },
  1004. keyUpdateTransformSession(data, payload: PointerInfo) {
  1005. session.current.update(
  1006. data,
  1007. screenToWorld(inputs.pointer.point, data),
  1008. payload.shiftKey,
  1009. payload.altKey
  1010. )
  1011. },
  1012. updateTransformSession(data, payload: PointerInfo) {
  1013. session.current.update(
  1014. data,
  1015. screenToWorld(payload.point, data),
  1016. payload.shiftKey,
  1017. payload.altKey
  1018. )
  1019. },
  1020. // Direction
  1021. startDirectionSession(data, payload: PointerInfo) {
  1022. session.current = new Sessions.DirectionSession(
  1023. data,
  1024. screenToWorld(inputs.pointer.origin, data)
  1025. )
  1026. },
  1027. updateDirectionSession(data, payload: PointerInfo) {
  1028. session.current.update(data, screenToWorld(payload.point, data))
  1029. },
  1030. // Drawing
  1031. startDrawSession(data, payload: PointerInfo) {
  1032. const id = Array.from(getSelectedIds(data).values())[0]
  1033. session.current = new Sessions.DrawSession(
  1034. data,
  1035. id,
  1036. screenToWorld(inputs.pointer.origin, data),
  1037. payload.shiftKey
  1038. )
  1039. },
  1040. keyUpdateDrawSession(data, payload: PointerInfo) {
  1041. session.current.update(
  1042. data,
  1043. screenToWorld(inputs.pointer.point, data),
  1044. payload.pressure,
  1045. payload.shiftKey
  1046. )
  1047. },
  1048. updateDrawSession(data, payload: PointerInfo) {
  1049. session.current.update(
  1050. data,
  1051. screenToWorld(payload.point, data),
  1052. payload.pressure,
  1053. payload.shiftKey
  1054. )
  1055. },
  1056. // Arrow
  1057. startArrowSession(data, payload: PointerInfo) {
  1058. const id = Array.from(getSelectedIds(data).values())[0]
  1059. session.current = new Sessions.ArrowSession(
  1060. data,
  1061. id,
  1062. screenToWorld(inputs.pointer.origin, data),
  1063. payload.shiftKey
  1064. )
  1065. },
  1066. keyUpdateArrowSession(data, payload: PointerInfo) {
  1067. session.current.update(
  1068. data,
  1069. screenToWorld(inputs.pointer.point, data),
  1070. payload.shiftKey
  1071. )
  1072. },
  1073. updateArrowSession(data, payload: PointerInfo) {
  1074. session.current.update(
  1075. data,
  1076. screenToWorld(payload.point, data),
  1077. payload.shiftKey
  1078. )
  1079. },
  1080. // Nudges
  1081. nudgeSelection(data, payload: { delta: number[]; shiftKey: boolean }) {
  1082. commands.nudge(
  1083. data,
  1084. vec.mul(
  1085. payload.delta,
  1086. payload.shiftKey
  1087. ? data.settings.nudgeDistanceLarge
  1088. : data.settings.nudgeDistanceSmall
  1089. )
  1090. )
  1091. },
  1092. /* -------------------- Selection ------------------- */
  1093. selectAll(data) {
  1094. const selectedIds = getSelectedIds(data)
  1095. const page = getPage(data)
  1096. selectedIds.clear()
  1097. for (let id in page.shapes) {
  1098. if (page.shapes[id].parentId === data.currentPageId) {
  1099. selectedIds.add(id)
  1100. }
  1101. }
  1102. },
  1103. setHoveredId(data, payload: PointerInfo) {
  1104. data.hoveredId = payload.target
  1105. },
  1106. clearHoveredId(data) {
  1107. data.hoveredId = undefined
  1108. },
  1109. setPointedId(data, payload: PointerInfo) {
  1110. data.pointedId = getPointedId(data, payload.target)
  1111. data.currentParentId = getParentId(data, data.pointedId)
  1112. },
  1113. setDrilledPointedId(data, payload: PointerInfo) {
  1114. data.pointedId = getDrilledPointedId(data, payload.target)
  1115. data.currentParentId = getParentId(data, data.pointedId)
  1116. },
  1117. clearCurrentParentId(data) {
  1118. data.currentParentId = data.currentPageId
  1119. data.pointedId = undefined
  1120. },
  1121. clearPointedId(data) {
  1122. data.pointedId = undefined
  1123. },
  1124. clearSelectedIds(data) {
  1125. setSelectedIds(data, [])
  1126. },
  1127. pullPointedIdFromSelectedIds(data) {
  1128. const { pointedId } = data
  1129. const selectedIds = getSelectedIds(data)
  1130. selectedIds.delete(pointedId)
  1131. },
  1132. pushPointedIdToSelectedIds(data) {
  1133. getSelectedIds(data).add(data.pointedId)
  1134. },
  1135. moveSelection(data, payload: { type: MoveType }) {
  1136. commands.move(data, payload.type)
  1137. },
  1138. moveSelectionToPage(data, payload: { id: string }) {
  1139. commands.moveToPage(data, payload.id)
  1140. },
  1141. alignSelection(data, payload: { type: AlignType }) {
  1142. commands.align(data, payload.type)
  1143. },
  1144. stretchSelection(data, payload: { type: StretchType }) {
  1145. commands.stretch(data, payload.type)
  1146. },
  1147. distributeSelection(data, payload: { type: DistributeType }) {
  1148. commands.distribute(data, payload.type)
  1149. },
  1150. duplicateSelection(data) {
  1151. commands.duplicate(data)
  1152. },
  1153. lockSelection(data) {
  1154. commands.toggle(data, 'isLocked')
  1155. },
  1156. hideSelection(data) {
  1157. commands.toggle(data, 'isHidden')
  1158. },
  1159. aspectLockSelection(data) {
  1160. commands.toggle(data, 'isAspectRatioLocked')
  1161. },
  1162. deleteSelection(data) {
  1163. commands.deleteSelected(data)
  1164. },
  1165. rotateSelectionCcw(data) {
  1166. commands.rotateCcw(data)
  1167. },
  1168. groupSelection(data) {
  1169. commands.group(data)
  1170. },
  1171. ungroupSelection(data) {
  1172. commands.ungroup(data)
  1173. },
  1174. /* ---------------------- Tool ---------------------- */
  1175. setActiveTool(data, payload: { tool: ShapeType | 'select' }) {
  1176. data.activeTool = payload.tool
  1177. },
  1178. setActiveToolSelect(data) {
  1179. data.activeTool = 'select'
  1180. },
  1181. setActiveToolDraw(data) {
  1182. data.activeTool = ShapeType.Draw
  1183. },
  1184. setActiveToolRectangle(data) {
  1185. data.activeTool = ShapeType.Rectangle
  1186. },
  1187. setActiveToolEllipse(data) {
  1188. data.activeTool = ShapeType.Ellipse
  1189. },
  1190. setActiveToolArrow(data) {
  1191. data.activeTool = ShapeType.Arrow
  1192. },
  1193. setActiveToolDot(data) {
  1194. data.activeTool = ShapeType.Dot
  1195. },
  1196. setActiveToolPolyline(data) {
  1197. data.activeTool = ShapeType.Polyline
  1198. },
  1199. setActiveToolRay(data) {
  1200. data.activeTool = ShapeType.Ray
  1201. },
  1202. setActiveToolCircle(data) {
  1203. data.activeTool = ShapeType.Circle
  1204. },
  1205. setActiveToolLine(data) {
  1206. data.activeTool = ShapeType.Line
  1207. },
  1208. /* --------------------- Camera --------------------- */
  1209. zoomIn(data) {
  1210. const camera = getCurrentCamera(data)
  1211. const i = Math.round((camera.zoom * 100) / 25)
  1212. const center = [window.innerWidth / 2, window.innerHeight / 2]
  1213. const p0 = screenToWorld(center, data)
  1214. camera.zoom = getCameraZoom((i + 1) * 0.25)
  1215. const p1 = screenToWorld(center, data)
  1216. camera.point = vec.add(camera.point, vec.sub(p1, p0))
  1217. setZoomCSS(camera.zoom)
  1218. },
  1219. zoomOut(data) {
  1220. const camera = getCurrentCamera(data)
  1221. const i = Math.round((camera.zoom * 100) / 25)
  1222. const center = [window.innerWidth / 2, window.innerHeight / 2]
  1223. const p0 = screenToWorld(center, data)
  1224. camera.zoom = getCameraZoom((i - 1) * 0.25)
  1225. const p1 = screenToWorld(center, data)
  1226. camera.point = vec.add(camera.point, vec.sub(p1, p0))
  1227. setZoomCSS(camera.zoom)
  1228. },
  1229. zoomCameraToActual(data) {
  1230. const camera = getCurrentCamera(data)
  1231. const center = [window.innerWidth / 2, window.innerHeight / 2]
  1232. const p0 = screenToWorld(center, data)
  1233. camera.zoom = 1
  1234. const p1 = screenToWorld(center, data)
  1235. camera.point = vec.add(camera.point, vec.sub(p1, p0))
  1236. setZoomCSS(camera.zoom)
  1237. },
  1238. zoomCameraToSelectionActual(data) {
  1239. const camera = getCurrentCamera(data)
  1240. const bounds = getSelectedBounds(data)
  1241. const mx = (window.innerWidth - bounds.width) / 2
  1242. const my = (window.innerHeight - bounds.height) / 2
  1243. camera.zoom = 1
  1244. camera.point = vec.add([-bounds.minX, -bounds.minY], [mx, my])
  1245. setZoomCSS(camera.zoom)
  1246. },
  1247. zoomCameraToSelection(data) {
  1248. const camera = getCurrentCamera(data)
  1249. const bounds = getSelectedBounds(data)
  1250. const zoom = getCameraZoom(
  1251. bounds.width > bounds.height
  1252. ? (window.innerWidth - 128) / bounds.width
  1253. : (window.innerHeight - 128) / bounds.height
  1254. )
  1255. const mx = (window.innerWidth - bounds.width * zoom) / 2 / zoom
  1256. const my = (window.innerHeight - bounds.height * zoom) / 2 / zoom
  1257. camera.zoom = zoom
  1258. camera.point = vec.add([-bounds.minX, -bounds.minY], [mx, my])
  1259. setZoomCSS(camera.zoom)
  1260. },
  1261. zoomCameraToFit(data) {
  1262. const camera = getCurrentCamera(data)
  1263. const page = getPage(data)
  1264. const shapes = Object.values(page.shapes)
  1265. if (shapes.length === 0) {
  1266. return
  1267. }
  1268. const bounds = getCommonBounds(
  1269. ...Object.values(shapes).map((shape) =>
  1270. getShapeUtils(shape).getBounds(shape)
  1271. )
  1272. )
  1273. const zoom = getCameraZoom(
  1274. bounds.width > bounds.height
  1275. ? (window.innerWidth - 128) / bounds.width
  1276. : (window.innerHeight - 128) / bounds.height
  1277. )
  1278. const mx = (window.innerWidth - bounds.width * zoom) / 2 / zoom
  1279. const my = (window.innerHeight - bounds.height * zoom) / 2 / zoom
  1280. camera.zoom = zoom
  1281. camera.point = vec.add([-bounds.minX, -bounds.minY], [mx, my])
  1282. setZoomCSS(camera.zoom)
  1283. },
  1284. zoomCamera(data, payload: { delta: number; point: number[] }) {
  1285. const camera = getCurrentCamera(data)
  1286. const next = camera.zoom - (payload.delta / 100) * camera.zoom
  1287. const p0 = screenToWorld(payload.point, data)
  1288. camera.zoom = getCameraZoom(next)
  1289. const p1 = screenToWorld(payload.point, data)
  1290. camera.point = vec.add(camera.point, vec.sub(p1, p0))
  1291. setZoomCSS(camera.zoom)
  1292. },
  1293. panCamera(data, payload: { delta: number[] }) {
  1294. const camera = getCurrentCamera(data)
  1295. camera.point = vec.sub(camera.point, vec.div(payload.delta, camera.zoom))
  1296. },
  1297. updateZoomCSS(data) {
  1298. const camera = getCurrentCamera(data)
  1299. setZoomCSS(camera.zoom)
  1300. },
  1301. pinchCamera(
  1302. data,
  1303. payload: {
  1304. delta: number[]
  1305. distanceDelta: number
  1306. angleDelta: number
  1307. point: number[]
  1308. }
  1309. ) {
  1310. const camera = getCurrentCamera(data)
  1311. camera.point = vec.sub(camera.point, vec.div(payload.delta, camera.zoom))
  1312. const next = camera.zoom - (payload.distanceDelta / 300) * camera.zoom
  1313. const p0 = screenToWorld(payload.point, data)
  1314. camera.zoom = getCameraZoom(next)
  1315. const p1 = screenToWorld(payload.point, data)
  1316. camera.point = vec.add(camera.point, vec.sub(p1, p0))
  1317. setZoomCSS(camera.zoom)
  1318. },
  1319. resetCamera(data) {
  1320. const camera = getCurrentCamera(data)
  1321. camera.zoom = 1
  1322. camera.point = [window.innerWidth / 2, window.innerHeight / 2]
  1323. document.documentElement.style.setProperty('--camera-zoom', '1')
  1324. },
  1325. /* ---------------------- History ---------------------- */
  1326. // History
  1327. popHistory() {
  1328. history.pop()
  1329. },
  1330. enableHistory() {
  1331. history.enable()
  1332. },
  1333. disableHistory() {
  1334. history.disable()
  1335. },
  1336. undo(data) {
  1337. history.undo(data)
  1338. },
  1339. redo(data) {
  1340. history.redo(data)
  1341. },
  1342. /* --------------------- Styles --------------------- */
  1343. toggleStylePanel(data) {
  1344. data.settings.isStyleOpen = !data.settings.isStyleOpen
  1345. },
  1346. closeStylePanel(data) {
  1347. data.settings.isStyleOpen = false
  1348. },
  1349. updateStyles(data, payload: Partial<ShapeStyles>) {
  1350. Object.assign(data.currentStyle, payload)
  1351. },
  1352. applyStylesToSelection(data, payload: Partial<ShapeStyles>) {
  1353. commands.style(data, payload)
  1354. },
  1355. /* ---------------------- Code ---------------------- */
  1356. closeCodePanel(data) {
  1357. data.settings.isCodeOpen = false
  1358. },
  1359. openCodePanel(data) {
  1360. data.settings.isCodeOpen = true
  1361. },
  1362. toggleCodePanel(data) {
  1363. data.settings.isCodeOpen = !data.settings.isCodeOpen
  1364. },
  1365. setGeneratedShapes(
  1366. data,
  1367. payload: { shapes: Shape[]; controls: CodeControl[] }
  1368. ) {
  1369. commands.generate(data, data.currentPageId, payload.shapes)
  1370. },
  1371. setCodeControls(data, payload: { controls: CodeControl[] }) {
  1372. data.codeControls = Object.fromEntries(
  1373. payload.controls.map((control) => [control.id, control])
  1374. )
  1375. },
  1376. increaseCodeFontSize(data) {
  1377. data.settings.fontSize++
  1378. },
  1379. decreaseCodeFontSize(data) {
  1380. data.settings.fontSize--
  1381. },
  1382. updateControls(data, payload: { [key: string]: any }) {
  1383. for (let key in payload) {
  1384. data.codeControls[key].value = payload[key]
  1385. }
  1386. history.disable()
  1387. setSelectedIds(data, [])
  1388. try {
  1389. const { shapes } = updateFromCode(
  1390. data,
  1391. data.document.code[data.currentCodeFileId].code
  1392. )
  1393. commands.generate(data, data.currentPageId, shapes)
  1394. } catch (e) {
  1395. console.error(e)
  1396. }
  1397. history.enable()
  1398. },
  1399. /* -------------------- Settings -------------------- */
  1400. enablePenLock(data) {
  1401. data.settings.isPenLocked = true
  1402. },
  1403. disablePenLock(data) {
  1404. data.settings.isPenLocked = false
  1405. },
  1406. toggleToolLock(data) {
  1407. data.settings.isToolLocked = !data.settings.isToolLocked
  1408. },
  1409. /* ---------------------- Data ---------------------- */
  1410. forceSave(data) {
  1411. storage.save(data)
  1412. },
  1413. savePage(data) {
  1414. storage.savePage(data, data.currentPageId)
  1415. },
  1416. loadPage(data) {
  1417. storage.loadPage(data, data.currentPageId)
  1418. },
  1419. saveCode(data, payload: { code: string }) {
  1420. data.document.code[data.currentCodeFileId].code = payload.code
  1421. storage.save(data)
  1422. },
  1423. restoreSavedData(data) {
  1424. storage.load(data)
  1425. },
  1426. clearBoundsRotation(data) {
  1427. data.boundsRotation = 0
  1428. },
  1429. },
  1430. values: {
  1431. selectedIds(data) {
  1432. return new Set(getSelectedIds(data))
  1433. },
  1434. selectedBounds(data) {
  1435. return getSelectionBounds(data)
  1436. },
  1437. selectedStyle(data) {
  1438. const selectedIds = Array.from(getSelectedIds(data).values())
  1439. const { currentStyle } = data
  1440. if (selectedIds.length === 0) {
  1441. return currentStyle
  1442. }
  1443. const page = getPage(data)
  1444. const shapeStyles = selectedIds.map((id) => page.shapes[id].style)
  1445. const commonStyle: ShapeStyles = {} as ShapeStyles
  1446. const overrides = new Set<string>([])
  1447. for (const shapeStyle of shapeStyles) {
  1448. for (let key in currentStyle) {
  1449. if (overrides.has(key)) continue
  1450. if (commonStyle[key] === undefined) {
  1451. commonStyle[key] = shapeStyle[key]
  1452. } else {
  1453. if (commonStyle[key] === shapeStyle[key]) continue
  1454. commonStyle[key] = currentStyle[key]
  1455. overrides.add(key)
  1456. }
  1457. }
  1458. }
  1459. return commonStyle
  1460. },
  1461. },
  1462. })
  1463. export default state
  1464. export const useSelector = createSelectorHook(state)
  1465. function getParentId(data: Data, id: string) {
  1466. const shape = getPage(data).shapes[id]
  1467. return shape.parentId
  1468. }
  1469. function getPointedId(data: Data, id: string) {
  1470. const shape = getPage(data).shapes[id]
  1471. return shape.parentId === data.currentParentId ||
  1472. shape.parentId === data.currentPageId
  1473. ? id
  1474. : getPointedId(data, shape.parentId)
  1475. }
  1476. function getDrilledPointedId(data: Data, id: string) {
  1477. const shape = getPage(data).shapes[id]
  1478. return shape.parentId === data.currentPageId ||
  1479. shape.parentId === data.pointedId ||
  1480. shape.parentId === data.currentParentId
  1481. ? id
  1482. : getDrilledPointedId(data, shape.parentId)
  1483. }
  1484. function hasPointedIdInChildren(data: Data, id: string, pointedId: string) {
  1485. const shape = getPage(data).shapes[id]
  1486. if (shape.type !== ShapeType.Group) {
  1487. return false
  1488. }
  1489. if (shape.children.includes(pointedId)) {
  1490. return true
  1491. }
  1492. return shape.children.some((childId) =>
  1493. hasPointedIdInChildren(data, childId, pointedId)
  1494. )
  1495. }
  1496. function getSelectionBounds(data: Data) {
  1497. const selectedIds = getSelectedIds(data)
  1498. const page = getPage(data)
  1499. const shapes = Array.from(selectedIds.values())
  1500. .map((id) => page.shapes[id])
  1501. .filter(Boolean)
  1502. if (selectedIds.size === 0) return null
  1503. if (selectedIds.size === 1) {
  1504. if (!shapes[0]) {
  1505. console.error('Could not find that shape! Clearing selected IDs.')
  1506. setSelectedIds(data, [])
  1507. return null
  1508. }
  1509. const shape = shapes[0]
  1510. const shapeUtils = getShapeUtils(shape)
  1511. if (!shapeUtils.canTransform) return null
  1512. let bounds = shapeUtils.getBounds(shape)
  1513. let parentId = shape.parentId
  1514. while (parentId !== data.currentPageId) {
  1515. const parent = page.shapes[parentId]
  1516. bounds = rotateBounds(
  1517. bounds,
  1518. getBoundsCenter(getShapeUtils(parent).getBounds(parent)),
  1519. parent.rotation
  1520. )
  1521. bounds.rotation = parent.rotation
  1522. parentId = parent.parentId
  1523. }
  1524. return bounds
  1525. }
  1526. const uniqueSelectedShapeIds: string[] = Array.from(
  1527. new Set(
  1528. Array.from(selectedIds.values()).flatMap((id) =>
  1529. getDocumentBranch(data, id)
  1530. )
  1531. ).values()
  1532. )
  1533. const commonBounds = getCommonBounds(
  1534. ...uniqueSelectedShapeIds
  1535. .map((id) => page.shapes[id])
  1536. .filter((shape) => shape.type !== ShapeType.Group)
  1537. .map((shape) => getShapeUtils(shape).getRotatedBounds(shape))
  1538. )
  1539. return commonBounds
  1540. }