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 45KB

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