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

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