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

move.command.spec.ts 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. import { TLDrawState } from '~state'
  2. import { mockDocument } from '~test'
  3. import { Utils } from '@tldraw/core'
  4. import type { Data } from '~types'
  5. const doc = Utils.deepClone(mockDocument)
  6. doc.pages.page1.shapes['a'] = {
  7. ...doc.pages.page1.shapes['rect1'],
  8. id: 'a',
  9. childIndex: 1,
  10. }
  11. doc.pages.page1.shapes['b'] = {
  12. ...doc.pages.page1.shapes['rect1'],
  13. id: 'b',
  14. childIndex: 2,
  15. }
  16. doc.pages.page1.shapes['c'] = {
  17. ...doc.pages.page1.shapes['rect1'],
  18. id: 'c',
  19. childIndex: 3,
  20. }
  21. doc.pages.page1.shapes['d'] = {
  22. ...doc.pages.page1.shapes['rect1'],
  23. id: 'd',
  24. childIndex: 4,
  25. }
  26. doc.pageStates.page1.selectedIds = ['a']
  27. delete doc.pages.page1.shapes['rect1']
  28. delete doc.pages.page1.shapes['rect2']
  29. delete doc.pages.page1.shapes['rect3']
  30. function getSortedShapeIds(data: Data) {
  31. return Object.values(data.page.shapes)
  32. .sort((a, b) => a.childIndex - b.childIndex)
  33. .map((shape) => shape.id)
  34. .join('')
  35. }
  36. describe('Move command', () => {
  37. const tlstate = new TLDrawState()
  38. it('does, undoes and redoes command', () => {
  39. tlstate.loadDocument(doc)
  40. tlstate.setSelectedIds(['b'])
  41. tlstate.moveToBack()
  42. expect(getSortedShapeIds(tlstate.getState())).toBe('bacd')
  43. tlstate.undo()
  44. expect(getSortedShapeIds(tlstate.getState())).toBe('abcd')
  45. tlstate.redo()
  46. expect(getSortedShapeIds(tlstate.getState())).toBe('bacd')
  47. })
  48. describe('to back', () => {
  49. it('moves a shape to back', () => {
  50. tlstate.loadDocument(doc)
  51. tlstate.setSelectedIds(['b'])
  52. tlstate.moveToBack()
  53. expect(getSortedShapeIds(tlstate.getState())).toBe('bacd')
  54. })
  55. it('moves two adjacent siblings to back', () => {
  56. tlstate.loadDocument(doc)
  57. tlstate.setSelectedIds(['b', 'c'])
  58. tlstate.moveToBack()
  59. expect(getSortedShapeIds(tlstate.getState())).toBe('bcad')
  60. })
  61. it('moves two non-adjacent siblings to back', () => {
  62. tlstate.loadDocument(doc)
  63. tlstate.setSelectedIds(['b', 'd'])
  64. tlstate.moveToBack()
  65. expect(getSortedShapeIds(tlstate.getState())).toBe('bdac')
  66. })
  67. })
  68. describe('backward', () => {
  69. it('moves a shape backward', () => {
  70. tlstate.loadDocument(doc)
  71. tlstate.setSelectedIds(['c'])
  72. tlstate.moveBackward()
  73. expect(getSortedShapeIds(tlstate.getState())).toBe('acbd')
  74. })
  75. it('moves a shape at first index backward', () => {
  76. tlstate.loadDocument(doc)
  77. tlstate.setSelectedIds(['a'])
  78. tlstate.moveBackward()
  79. expect(getSortedShapeIds(tlstate.getState())).toBe('abcd')
  80. })
  81. it('moves two adjacent siblings backward', () => {
  82. tlstate.loadDocument(doc)
  83. tlstate.setSelectedIds(['c', 'd'])
  84. tlstate.moveBackward()
  85. expect(getSortedShapeIds(tlstate.getState())).toBe('acdb')
  86. })
  87. it('moves two non-adjacent siblings backward', () => {
  88. tlstate.loadDocument(doc)
  89. tlstate.setSelectedIds(['b', 'd'])
  90. tlstate.moveBackward()
  91. expect(getSortedShapeIds(tlstate.getState())).toBe('badc')
  92. })
  93. it('moves two adjacent siblings backward at zero index', () => {
  94. tlstate.loadDocument(doc)
  95. tlstate.setSelectedIds(['a', 'b'])
  96. tlstate.moveBackward()
  97. expect(getSortedShapeIds(tlstate.getState())).toBe('abcd')
  98. })
  99. })
  100. describe('forward', () => {
  101. it('moves a shape forward', () => {
  102. tlstate.loadDocument(doc)
  103. tlstate.setSelectedIds(['c'])
  104. tlstate.moveForward()
  105. expect(getSortedShapeIds(tlstate.getState())).toBe('abdc')
  106. })
  107. it('moves a shape forward at the top index', () => {
  108. tlstate.loadDocument(doc)
  109. tlstate.setSelectedIds(['b'])
  110. tlstate.moveForward()
  111. tlstate.moveForward()
  112. tlstate.moveForward()
  113. expect(getSortedShapeIds(tlstate.getState())).toBe('acdb')
  114. })
  115. it('moves two adjacent siblings forward', () => {
  116. tlstate.loadDocument(doc)
  117. tlstate.setSelectedIds(['a', 'b'])
  118. tlstate.moveForward()
  119. expect(getSortedShapeIds(tlstate.getState())).toBe('cabd')
  120. })
  121. it('moves two non-adjacent siblings forward', () => {
  122. tlstate.loadDocument(doc)
  123. tlstate.setSelectedIds(['a', 'c'])
  124. tlstate.moveForward()
  125. expect(getSortedShapeIds(tlstate.getState())).toBe('badc')
  126. })
  127. it('moves two adjacent siblings forward at top index', () => {
  128. tlstate.loadDocument(doc)
  129. tlstate.setSelectedIds(['c', 'd'])
  130. tlstate.moveForward()
  131. expect(getSortedShapeIds(tlstate.getState())).toBe('abcd')
  132. })
  133. })
  134. describe('to front', () => {
  135. it('moves a shape to front', () => {
  136. tlstate.loadDocument(doc)
  137. tlstate.setSelectedIds(['b'])
  138. tlstate.moveToFront()
  139. expect(getSortedShapeIds(tlstate.getState())).toBe('acdb')
  140. })
  141. it('moves two adjacent siblings to front', () => {
  142. tlstate.loadDocument(doc)
  143. tlstate.setSelectedIds(['a', 'b'])
  144. tlstate.moveToFront()
  145. expect(getSortedShapeIds(tlstate.getState())).toBe('cdab')
  146. })
  147. it('moves two non-adjacent siblings to front', () => {
  148. tlstate.loadDocument(doc)
  149. tlstate.setSelectedIds(['a', 'c'])
  150. tlstate.moveToFront()
  151. expect(getSortedShapeIds(tlstate.getState())).toBe('bdac')
  152. })
  153. it('moves siblings already at front to front', () => {
  154. tlstate.loadDocument(doc)
  155. tlstate.setSelectedIds(['c', 'd'])
  156. tlstate.moveToFront()
  157. expect(getSortedShapeIds(tlstate.getState())).toBe('abcd')
  158. })
  159. })
  160. })