Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

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