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

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