Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

state.ts 62KB

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