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

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