Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

state.ts 49KB

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