You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

state.ts 57KB

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