@tmagic/stage 1.1.0-beta.2 → 1.1.0-beta.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/util.ts CHANGED
@@ -17,8 +17,8 @@
17
17
  */
18
18
  import { removeClassName } from '@tmagic/utils';
19
19
 
20
- import { Mode, SELECTED_CLASS } from './const';
21
- import type { Offset } from './types';
20
+ import { GHOST_EL_ID_PREFIX, Mode, SELECTED_CLASS, ZIndex } from './const';
21
+ import type { Offset, SortEventData } from './types';
22
22
 
23
23
  const getParents = (el: Element, relative: Element) => {
24
24
  let cur: Element | null = el.parentElement;
@@ -50,6 +50,21 @@ export const getOffset = (el: HTMLElement): Offset => {
50
50
  };
51
51
  };
52
52
 
53
+ // 将蒙层占位节点覆盖在原节点上方
54
+ export const getTargetElStyle = (el: HTMLElement) => {
55
+ const offset = getOffset(el);
56
+ const { transform } = getComputedStyle(el);
57
+ return `
58
+ position: absolute;
59
+ transform: ${transform};
60
+ left: ${offset.left}px;
61
+ top: ${offset.top}px;
62
+ width: ${el.clientWidth}px;
63
+ height: ${el.clientHeight}px;
64
+ z-index: ${ZIndex.DRAG_EL};
65
+ `;
66
+ };
67
+
53
68
  export const getAbsolutePosition = (el: HTMLElement, { top, left }: Offset) => {
54
69
  const { offsetParent } = el;
55
70
 
@@ -144,3 +159,73 @@ export const calcValueByFontsize = (doc: Document, value: number) => {
144
159
 
145
160
  return value;
146
161
  };
162
+
163
+ /**
164
+ * 下移组件位置
165
+ * @param {number} deltaTop 偏移量
166
+ * @param {Object} detail 当前选中的组件配置
167
+ */
168
+ export const down = (deltaTop: number, target: HTMLElement | SVGElement): SortEventData | void => {
169
+ let swapIndex = 0;
170
+ let addUpH = target.clientHeight;
171
+ const brothers = Array.from(target.parentNode?.children || []).filter(
172
+ (node) => !node.id.startsWith(GHOST_EL_ID_PREFIX),
173
+ );
174
+ const index = brothers.indexOf(target);
175
+ // 往下移动
176
+ const downEls = brothers.slice(index + 1) as HTMLElement[];
177
+
178
+ for (let i = 0; i < downEls.length; i++) {
179
+ const ele = downEls[i];
180
+ // 是 fixed 不做处理
181
+ if (ele.style?.position === 'fixed') {
182
+ continue;
183
+ }
184
+ addUpH += ele.clientHeight / 2;
185
+ if (deltaTop <= addUpH) {
186
+ break;
187
+ }
188
+ addUpH += ele.clientHeight / 2;
189
+ swapIndex = i;
190
+ }
191
+ return {
192
+ src: target.id,
193
+ dist: downEls.length && swapIndex > -1 ? downEls[swapIndex].id : target.id,
194
+ };
195
+ };
196
+
197
+ /**
198
+ * 上移组件位置
199
+ * @param {Array} brothers 处于同一容器下的所有子组件配置
200
+ * @param {number} index 当前组件所处的位置
201
+ * @param {number} deltaTop 偏移量
202
+ * @param {Object} detail 当前选中的组件配置
203
+ */
204
+ export const up = (deltaTop: number, target: HTMLElement | SVGElement): SortEventData | void => {
205
+ const brothers = Array.from(target.parentNode?.children || []).filter(
206
+ (node) => !node.id.startsWith(GHOST_EL_ID_PREFIX),
207
+ );
208
+ const index = brothers.indexOf(target);
209
+ // 往上移动
210
+ const upEls = brothers.slice(0, index) as HTMLElement[];
211
+
212
+ let addUpH = target.clientHeight;
213
+ let swapIndex = upEls.length - 1;
214
+
215
+ for (let i = upEls.length - 1; i >= 0; i--) {
216
+ const ele = upEls[i];
217
+ if (!ele) continue;
218
+ // 是 fixed 不做处理
219
+ if (ele.style.position === 'fixed') continue;
220
+
221
+ addUpH += ele.clientHeight / 2;
222
+ if (-deltaTop <= addUpH) break;
223
+ addUpH += ele.clientHeight / 2;
224
+
225
+ swapIndex = i;
226
+ }
227
+ return {
228
+ src: target.id,
229
+ dist: upEls.length && swapIndex > -1 ? upEls[swapIndex].id : target.id,
230
+ };
231
+ };