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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. import Command from "./command"
  2. import history from "../history"
  3. import { Data, MoveType, Shape } from "types"
  4. import { forceIntegerChildIndices, getChildren, getPage } from "utils/utils"
  5. import { getShapeUtils } from "lib/shape-utils"
  6. export default function moveCommand(data: Data, type: MoveType) {
  7. const { currentPageId } = data
  8. const page = getPage(data)
  9. const selectedIds = Array.from(data.selectedIds.values())
  10. const initialIndices = Object.fromEntries(
  11. selectedIds.map((id) => [id, page.shapes[id].childIndex])
  12. )
  13. history.execute(
  14. data,
  15. new Command({
  16. name: "move_shapes",
  17. category: "canvas",
  18. manualSelection: true,
  19. do(data) {
  20. const page = getPage(data, currentPageId)
  21. const shapes = selectedIds.map((id) => page.shapes[id])
  22. const shapesByParentId = shapes.reduce<Record<string, Shape[]>>(
  23. (acc, shape) => {
  24. if (acc[shape.parentId] === undefined) {
  25. acc[shape.parentId] = []
  26. }
  27. acc[shape.parentId].push(shape)
  28. return acc
  29. },
  30. {}
  31. )
  32. switch (type) {
  33. case MoveType.ToFront: {
  34. for (let id in shapesByParentId) {
  35. moveToFront(shapesByParentId[id], getChildren(data, id))
  36. }
  37. break
  38. }
  39. case MoveType.ToBack: {
  40. for (let id in shapesByParentId) {
  41. moveToBack(shapesByParentId[id], getChildren(data, id))
  42. }
  43. break
  44. }
  45. case MoveType.Forward: {
  46. for (let id in shapesByParentId) {
  47. const visited = new Set<string>()
  48. const siblings = getChildren(data, id)
  49. shapesByParentId[id]
  50. .sort((a, b) => b.childIndex - a.childIndex)
  51. .forEach((shape) => moveForward(shape, siblings, visited))
  52. }
  53. break
  54. }
  55. case MoveType.Backward: {
  56. for (let id in shapesByParentId) {
  57. const visited = new Set<string>()
  58. const siblings = getChildren(data, id)
  59. shapesByParentId[id]
  60. .sort((a, b) => a.childIndex - b.childIndex)
  61. .forEach((shape) => moveBackward(shape, siblings, visited))
  62. }
  63. break
  64. }
  65. }
  66. },
  67. undo(data) {
  68. const page = getPage(data)
  69. for (let id of selectedIds) {
  70. const shape = page.shapes[id]
  71. getShapeUtils(shape).setChildIndex(shape, initialIndices[id])
  72. }
  73. },
  74. })
  75. )
  76. }
  77. function moveToFront(shapes: Shape[], siblings: Shape[]) {
  78. shapes.sort((a, b) => a.childIndex - b.childIndex)
  79. const diff = siblings
  80. .filter((sib) => !shapes.includes(sib))
  81. .sort((a, b) => b.childIndex - a.childIndex)
  82. if (diff.length === 0) return
  83. const startIndex = Math.ceil(diff[0].childIndex) + 1
  84. shapes.forEach((shape, i) =>
  85. getShapeUtils(shape).setChildIndex(shape, startIndex + i)
  86. )
  87. }
  88. function moveToBack(shapes: Shape[], siblings: Shape[]) {
  89. shapes.sort((a, b) => b.childIndex - a.childIndex)
  90. const diff = siblings
  91. .filter((sib) => !shapes.includes(sib))
  92. .sort((a, b) => a.childIndex - b.childIndex)
  93. if (diff.length === 0) return
  94. const startIndex = diff[0]?.childIndex
  95. const step = startIndex / (shapes.length + 1)
  96. shapes.forEach((shape, i) =>
  97. getShapeUtils(shape).setChildIndex(shape, startIndex - (i + 1) * step)
  98. )
  99. }
  100. function moveForward(shape: Shape, siblings: Shape[], visited: Set<string>) {
  101. visited.add(shape.id)
  102. const index = siblings.indexOf(shape)
  103. const nextSibling = siblings[index + 1]
  104. if (nextSibling && !visited.has(nextSibling.id)) {
  105. const nextNextSibling = siblings[index + 2]
  106. let nextIndex = nextNextSibling
  107. ? (nextSibling.childIndex + nextNextSibling.childIndex) / 2
  108. : Math.ceil(nextSibling.childIndex + 1)
  109. if (nextIndex === nextSibling.childIndex) {
  110. forceIntegerChildIndices(siblings)
  111. nextIndex = nextNextSibling
  112. ? (nextSibling.childIndex + nextNextSibling.childIndex) / 2
  113. : Math.ceil(nextSibling.childIndex + 1)
  114. }
  115. getShapeUtils(shape).setChildIndex(shape, nextIndex)
  116. siblings.sort((a, b) => a.childIndex - b.childIndex)
  117. }
  118. }
  119. function moveBackward(shape: Shape, siblings: Shape[], visited: Set<string>) {
  120. visited.add(shape.id)
  121. const index = siblings.indexOf(shape)
  122. const nextSibling = siblings[index - 1]
  123. if (nextSibling && !visited.has(nextSibling.id)) {
  124. const nextNextSibling = siblings[index - 2]
  125. let nextIndex = nextNextSibling
  126. ? (nextSibling.childIndex + nextNextSibling.childIndex) / 2
  127. : nextSibling.childIndex / 2
  128. if (shape.childIndex === nextSibling.childIndex) {
  129. forceIntegerChildIndices(siblings)
  130. nextNextSibling
  131. ? (nextSibling.childIndex + nextNextSibling.childIndex) / 2
  132. : nextSibling.childIndex / 2
  133. }
  134. getShapeUtils(shape).setChildIndex(shape, nextIndex)
  135. siblings.sort((a, b) => a.childIndex - b.childIndex)
  136. }
  137. }