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

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