您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

state.ts 54KB

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