File size: 7,314 Bytes
f56a29b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import type { PPTElement } from '@/lib/types/slides';

export function useOrderElement() {
  /**
   * Get the z-order range of grouped elements
   * @param elementList All elements on the page
   * @param combineElementList Grouped elements list
   */
  const getCombineElementLevelRange = (
    elementList: PPTElement[],
    combineElementList: PPTElement[],
  ) => {
    return {
      minLevel: elementList.findIndex((_element) => _element.id === combineElementList[0].id),
      maxLevel: elementList.findIndex(
        (_element) => _element.id === combineElementList[combineElementList.length - 1].id,
      ),
    };
  };

  /**
   * Move up one layer
   * @param elementList All elements on the page
   * @param element The element being operated on
   */
  const moveUpElement = (elementList: PPTElement[], element: PPTElement) => {
    const copyOfElementList: PPTElement[] = JSON.parse(JSON.stringify(elementList));

    // If the element is a group member, all group members must be moved together
    if (element.groupId) {
      // Get all group members and their z-order range
      const combineElementList = copyOfElementList.filter(
        (_element) => _element.groupId === element.groupId,
      );
      const { minLevel, maxLevel } = getCombineElementLevelRange(elementList, combineElementList);

      // Already at the top level, cannot move further
      if (maxLevel >= elementList.length - 1) return;

      const nextElement = copyOfElementList[maxLevel + 1];
      const movedElementList = copyOfElementList.splice(minLevel, combineElementList.length);

      if (nextElement.groupId) {
        const nextCombineElementList = copyOfElementList.filter(
          (_element) => _element.groupId === nextElement.groupId,
        );
        copyOfElementList.splice(minLevel + nextCombineElementList.length, 0, ...movedElementList);
      } else copyOfElementList.splice(minLevel + 1, 0, ...movedElementList);
    }

    // If the element is not a group member
    else {
      // Get the element's z-level in the list
      const level = elementList.findIndex((item) => item.id === element.id);

      // Already at the top level, cannot move further
      if (level === elementList.length - 1) return;

      // Get the element above, remove this element from the list (cache removed element).
      // If the above element is in a group, insert above that group.
      // If the above element is not in any group, insert above that element.
      const nextElement = copyOfElementList[level + 1];
      const [movedElement] = copyOfElementList.splice(level, 1);
      if (nextElement.groupId) {
        const combineElementList = copyOfElementList.filter(
          (_element) => _element.groupId === nextElement.groupId,
        );
        copyOfElementList.splice(level + combineElementList.length, 0, movedElement);
      } else copyOfElementList.splice(level + 1, 0, movedElement);
    }

    return copyOfElementList;
  };

  /**
   * Move down one layer, same approach as move up
   * @param elementList All elements on the page
   * @param element The element being operated on
   */
  const moveDownElement = (elementList: PPTElement[], element: PPTElement) => {
    const copyOfElementList: PPTElement[] = JSON.parse(JSON.stringify(elementList));

    if (element.groupId) {
      const combineElementList = copyOfElementList.filter(
        (_element) => _element.groupId === element.groupId,
      );
      const { minLevel } = getCombineElementLevelRange(elementList, combineElementList);
      if (minLevel === 0) return;

      const prevElement = copyOfElementList[minLevel - 1];
      const movedElementList = copyOfElementList.splice(minLevel, combineElementList.length);

      if (prevElement.groupId) {
        const prevCombineElementList = copyOfElementList.filter(
          (_element) => _element.groupId === prevElement.groupId,
        );
        copyOfElementList.splice(minLevel - prevCombineElementList.length, 0, ...movedElementList);
      } else copyOfElementList.splice(minLevel - 1, 0, ...movedElementList);
    } else {
      const level = elementList.findIndex((item) => item.id === element.id);
      if (level === 0) return;

      const prevElement = copyOfElementList[level - 1];
      const movedElement = copyOfElementList.splice(level, 1)[0];

      if (prevElement.groupId) {
        const combineElementList = copyOfElementList.filter(
          (_element) => _element.groupId === prevElement.groupId,
        );
        copyOfElementList.splice(level - combineElementList.length, 0, movedElement);
      } else copyOfElementList.splice(level - 1, 0, movedElement);
    }

    return copyOfElementList;
  };

  /**
   * Bring to front
   * @param elementList All elements on the page
   * @param element The element being operated on
   */
  const moveTopElement = (elementList: PPTElement[], element: PPTElement) => {
    const copyOfElementList: PPTElement[] = JSON.parse(JSON.stringify(elementList));

    // If the element is a group member, all group members must be moved together
    if (element.groupId) {
      // Get all group members and their z-order range
      const combineElementList = copyOfElementList.filter(
        (_element) => _element.groupId === element.groupId,
      );
      const { minLevel, maxLevel } = getCombineElementLevelRange(elementList, combineElementList);

      // Already at the top level, cannot move further
      if (maxLevel === elementList.length - 1) return null;

      // Remove the group from the list, then append removed elements to the top
      const movedElementList = copyOfElementList.splice(minLevel, combineElementList.length);
      copyOfElementList.push(...movedElementList);
    }

    // If the element is not a group member
    else {
      // Get the element's z-level in the list
      const level = elementList.findIndex((item) => item.id === element.id);

      // Already at the top level, cannot move further
      if (level === elementList.length - 1) return null;

      // Remove the element from the list, then append it to the top
      copyOfElementList.splice(level, 1);
      copyOfElementList.push(element);
    }

    return copyOfElementList;
  };

  /**
   * Send to back, same approach as bring to front
   * @param elementList All elements on the page
   * @param element The element being operated on
   */
  const moveBottomElement = (elementList: PPTElement[], element: PPTElement) => {
    const copyOfElementList: PPTElement[] = JSON.parse(JSON.stringify(elementList));

    if (element.groupId) {
      const combineElementList = copyOfElementList.filter(
        (_element) => _element.groupId === element.groupId,
      );
      const { minLevel } = getCombineElementLevelRange(elementList, combineElementList);
      if (minLevel === 0) return;

      const movedElementList = copyOfElementList.splice(minLevel, combineElementList.length);
      copyOfElementList.unshift(...movedElementList);
    } else {
      const level = elementList.findIndex((item) => item.id === element.id);
      if (level === 0) return;

      copyOfElementList.splice(level, 1);
      copyOfElementList.unshift(element);
    }

    return copyOfElementList;
  };

  return {
    moveUpElement,
    moveDownElement,
    moveTopElement,
    moveBottomElement,
  };
}