Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

state.ts 45KB

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