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

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