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

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