mirror of
https://github.com/Tencent/tmagic-editor.git
synced 2026-12-04 01:45:08 +08:00
feat(stage): 流式布局拖拽显示插入位置指示线
按拖拽预览中心计算即将插入的边,并在锚点上画出指示线。 横排和换行布局支持左右插入,不再只按纵向半高判断。 fix #402 Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
parent
a60a052906
commit
776e548755
@ -34,10 +34,24 @@ import MoveableHelper from 'moveable-helper';
|
||||
|
||||
import { calcValueByFontsize, getIdFromEl, setIdToEl } from '@tmagic/core';
|
||||
|
||||
import { DRAG_EL_ID_PREFIX, GHOST_EL_ID_PREFIX, Mode, ZIndex } from './const';
|
||||
import { DRAG_EL_ID_PREFIX, GHOST_EL_ID_PREFIX, Mode, SORT_INDICATOR_EL_ID_PREFIX, ZIndex } from './const';
|
||||
import TargetShadow from './TargetShadow';
|
||||
import type { DragResizeHelperConfig, Rect, TargetElement } from './types';
|
||||
import { getAbsolutePosition, getBorderWidth, getMarginValue, getOffset } from './util';
|
||||
import {
|
||||
getAbsolutePosition,
|
||||
getBorderWidth,
|
||||
getFlowAxis,
|
||||
getMarginValue,
|
||||
getOffset,
|
||||
resolveSortInsertion,
|
||||
} from './util';
|
||||
|
||||
/** 插入线厚度。线心压在边上,和常见的 drop indicator 一样 */
|
||||
const SORT_INDICATOR_SIZE = 2;
|
||||
/** 线起点的圆点,用来分辨横线和竖线 */
|
||||
const SORT_INDICATOR_TERMINAL = 8;
|
||||
/** 实心红,和蓝色选中框分开 */
|
||||
const SORT_INDICATOR_COLOR = '#ff4d4f';
|
||||
|
||||
/**
|
||||
* 拖拽/改变大小等操作发生时,moveable会抛出各种状态事件,DragResizeHelper负责响应这些事件,对目标节点target和拖拽节点targetShadow进行修改;
|
||||
@ -56,6 +70,8 @@ export default class DragResizeHelper {
|
||||
private moveableHelper: MoveableHelper;
|
||||
/** 流式布局下,目标节点的镜像节点 */
|
||||
private ghostEl: HTMLElement | undefined;
|
||||
/** 流式布局拖动时,标出即将插入位置的指示线 */
|
||||
private sortIndicatorEl: HTMLElement | undefined;
|
||||
/** 用于记录节点被改变前的位置 */
|
||||
private frameSnapShot = {
|
||||
left: 0,
|
||||
@ -155,7 +171,14 @@ export default class DragResizeHelper {
|
||||
public onDrag(e: OnDrag): void {
|
||||
// 流式布局
|
||||
if (this.ghostEl) {
|
||||
this.ghostEl.style.top = `${this.frameSnapShot.top + e.beforeTranslate[1]}px`;
|
||||
const axis = getFlowAxis(this.target);
|
||||
if (axis !== 'vertical') {
|
||||
this.ghostEl.style.left = `${this.frameSnapShot.left + e.beforeTranslate[0]}px`;
|
||||
}
|
||||
if (axis !== 'horizontal') {
|
||||
this.ghostEl.style.top = `${this.frameSnapShot.top + e.beforeTranslate[1]}px`;
|
||||
}
|
||||
this.updateSortIndicator();
|
||||
return;
|
||||
}
|
||||
|
||||
@ -194,6 +217,7 @@ export default class DragResizeHelper {
|
||||
public destroyGhostEl(): void {
|
||||
this.ghostEl?.remove();
|
||||
this.ghostEl = undefined;
|
||||
this.destroySortIndicator();
|
||||
}
|
||||
|
||||
public clear(): void {
|
||||
@ -379,6 +403,83 @@ export default class DragResizeHelper {
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 拖动过程中按预览中心画插入线。原节点保持不动。
|
||||
* 横线贴在上沿或下沿,表示换行插入;竖线贴在左沿或右沿,表示同一行插入。
|
||||
*/
|
||||
private updateSortIndicator(): void {
|
||||
const { target, ghostEl } = this;
|
||||
if (!target || !ghostEl) return;
|
||||
|
||||
const { anchor, edge } = resolveSortInsertion(ghostEl, target);
|
||||
if (!anchor || !edge) {
|
||||
this.destroySortIndicator();
|
||||
return;
|
||||
}
|
||||
|
||||
this.renderSortIndicator(anchor, edge);
|
||||
}
|
||||
|
||||
private renderSortIndicator(anchor: HTMLElement, edge: 'top' | 'bottom' | 'left' | 'right'): void {
|
||||
const indicator = this.sortIndicatorEl ?? anchor.ownerDocument.createElement('div');
|
||||
this.sortIndicatorEl = indicator;
|
||||
setIdToEl()(indicator, `${SORT_INDICATOR_EL_ID_PREFIX}${getIdFromEl()(anchor) || ''}`);
|
||||
indicator.dataset.sortEdge = edge;
|
||||
|
||||
const { top, left } = getAbsolutePosition(anchor, getOffset(anchor));
|
||||
const beside = edge === 'left' || edge === 'right';
|
||||
const half = SORT_INDICATOR_SIZE / 2;
|
||||
let lineLeft = left;
|
||||
let lineTop = top - half;
|
||||
if (edge === 'bottom') lineTop = top + anchor.clientHeight - half;
|
||||
if (edge === 'left') lineLeft = left - half;
|
||||
if (edge === 'right') lineLeft = left + anchor.clientWidth - half;
|
||||
// 线心压在边外侧。贴着画布左沿或上沿时会画出容器,收到可见区域内
|
||||
if (lineLeft < 0) lineLeft = 0;
|
||||
if (lineTop < 0) lineTop = 0;
|
||||
const lineWidth = beside ? SORT_INDICATOR_SIZE : anchor.clientWidth;
|
||||
const lineHeight = beside ? anchor.clientHeight : SORT_INDICATOR_SIZE;
|
||||
indicator.style.cssText = `
|
||||
position: absolute;
|
||||
z-index: ${ZIndex.SORT_INDICATOR};
|
||||
left: ${lineLeft}px;
|
||||
top: ${lineTop}px;
|
||||
width: ${lineWidth}px;
|
||||
height: ${lineHeight}px;
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
border-radius: ${half}px;
|
||||
background: ${SORT_INDICATOR_COLOR};
|
||||
pointer-events: none;
|
||||
box-sizing: border-box;
|
||||
`;
|
||||
|
||||
const terminal = (indicator.firstElementChild as HTMLElement | null) ?? anchor.ownerDocument.createElement('div');
|
||||
const overhang = (SORT_INDICATOR_SIZE - SORT_INDICATOR_TERMINAL) / 2;
|
||||
let terminalLeft = overhang;
|
||||
let terminalTop = overhang;
|
||||
if (lineLeft + terminalLeft < 0) terminalLeft = -lineLeft;
|
||||
if (lineTop + terminalTop < 0) terminalTop = -lineTop;
|
||||
terminal.style.cssText = `
|
||||
position: absolute;
|
||||
left: ${terminalLeft}px;
|
||||
top: ${terminalTop}px;
|
||||
width: ${SORT_INDICATOR_TERMINAL}px;
|
||||
height: ${SORT_INDICATOR_TERMINAL}px;
|
||||
border-radius: 50%;
|
||||
background: ${SORT_INDICATOR_COLOR};
|
||||
pointer-events: none;
|
||||
`;
|
||||
if (!terminal.parentElement) indicator.append(terminal);
|
||||
|
||||
anchor.parentNode?.append(indicator);
|
||||
}
|
||||
|
||||
private destroySortIndicator(): void {
|
||||
this.sortIndicatorEl?.remove();
|
||||
this.sortIndicatorEl = undefined;
|
||||
}
|
||||
|
||||
/**
|
||||
* 流式布局把目标节点复制一份进行拖拽,在拖拽结束前不影响页面原布局样式
|
||||
*/
|
||||
|
||||
@ -18,8 +18,6 @@
|
||||
|
||||
import Moveable, { MoveableOptions } from 'moveable';
|
||||
|
||||
import { getIdFromEl } from '@tmagic/core';
|
||||
|
||||
import { Mode, StageDragStatus } from './const';
|
||||
import type DragResizeHelper from './DragResizeHelper';
|
||||
import MoveableOptionsManager from './MoveableOptionsManager';
|
||||
@ -30,7 +28,7 @@ import type {
|
||||
MarkContainerEnd,
|
||||
StageDragResizeConfig,
|
||||
} from './types';
|
||||
import { down, getMode, up } from './util';
|
||||
import { getMode, resolveSortInsertion } from './util';
|
||||
|
||||
/**
|
||||
* 管理单选操作,响应选中操作,初始化moveableOption参数并初始化moveable,处理moveable回调事件对组件进行更新
|
||||
@ -319,24 +317,14 @@ export default class StageDragResize extends MoveableOptionsManager {
|
||||
}
|
||||
|
||||
private sort(): void {
|
||||
if (!this.target || !this.dragResizeHelper.getGhostEl()) throw new Error('未知错误');
|
||||
const { top } = this.dragResizeHelper.getGhostEl()!.getBoundingClientRect();
|
||||
const { top: oriTop } = this.target.getBoundingClientRect();
|
||||
const deltaTop = top - oriTop;
|
||||
if (Math.abs(deltaTop) >= this.target.clientHeight / 2) {
|
||||
if (deltaTop > 0) {
|
||||
this.emit('sort', down(deltaTop, this.target));
|
||||
} else {
|
||||
this.emit('sort', up(deltaTop, this.target));
|
||||
}
|
||||
} else {
|
||||
const id = getIdFromEl()(this.target);
|
||||
id &&
|
||||
this.emit('sort', {
|
||||
src: id,
|
||||
dist: id,
|
||||
});
|
||||
}
|
||||
const ghostEl = this.dragResizeHelper.getGhostEl();
|
||||
if (!this.target || !ghostEl) throw new Error('未知错误');
|
||||
const { src, dist } = resolveSortInsertion(ghostEl, this.target);
|
||||
src &&
|
||||
this.emit('sort', {
|
||||
src,
|
||||
dist,
|
||||
});
|
||||
}
|
||||
|
||||
private update(isResize = false, parentEl: HTMLElement | null = null): void {
|
||||
|
||||
@ -19,6 +19,9 @@
|
||||
/** 流式布局下拖动时需要clone一个镜像节点,镜像节点的id前缀 */
|
||||
export const GHOST_EL_ID_PREFIX = 'ghost_el_';
|
||||
|
||||
/** 流式布局拖动时,标记插入位置的指示线节点 id 前缀 */
|
||||
export const SORT_INDICATOR_EL_ID_PREFIX = 'sort_indicator_el_';
|
||||
|
||||
/** 拖动的时候需要在蒙层中创建一个占位节点,该节点的id前缀 */
|
||||
export const DRAG_EL_ID_PREFIX = 'drag_el_';
|
||||
|
||||
@ -50,6 +53,8 @@ export enum ZIndex {
|
||||
/** 选中的节点 */
|
||||
SELECTED_EL = '666',
|
||||
GHOST_EL = '700',
|
||||
/** 流式布局插入位置指示线,需要盖住页面内的组件 */
|
||||
SORT_INDICATOR = '10000',
|
||||
DRAG_EL = '9',
|
||||
HIGHLIGHT_EL = '8',
|
||||
}
|
||||
|
||||
@ -17,9 +17,59 @@
|
||||
*/
|
||||
import { getIdFromEl, removeClassName } from '@tmagic/core';
|
||||
|
||||
import { GHOST_EL_ID_PREFIX, Mode, SELECTED_CLASS, ZIndex } from './const';
|
||||
import { GHOST_EL_ID_PREFIX, Mode, SELECTED_CLASS, SORT_INDICATOR_EL_ID_PREFIX, ZIndex } from './const';
|
||||
import type { Offset, SortEventData, TargetElement } from './types';
|
||||
|
||||
/** 流式排序时不参与兄弟节点计算的占位节点(拖拽镜像、插入指示线) */
|
||||
const isSortPlaceholder = (el: HTMLElement): boolean => {
|
||||
const id = getIdFromEl()(el);
|
||||
return !!id && (id.startsWith(GHOST_EL_ID_PREFIX) || id.startsWith(SORT_INDICATOR_EL_ID_PREFIX));
|
||||
};
|
||||
|
||||
const getFlowBrothers = (target: TargetElement): HTMLElement[] =>
|
||||
Array.from(target.parentNode?.children || []).filter(
|
||||
(child) => !isSortPlaceholder(child as HTMLElement),
|
||||
) as HTMLElement[];
|
||||
|
||||
export type SortEdge = 'top' | 'bottom' | 'left' | 'right';
|
||||
|
||||
export interface SortInsertion extends SortEventData {
|
||||
/** 插入线贴在锚点的哪一条边;没有可落点的节点时为 null */
|
||||
edge: SortEdge | null;
|
||||
/** 插入线所依附的兄弟节点 */
|
||||
anchor: HTMLElement | null;
|
||||
}
|
||||
|
||||
export type FlowAxis = 'vertical' | 'horizontal' | 'both';
|
||||
|
||||
/**
|
||||
* 流式拖动的比较方向。
|
||||
* inline / inline-block 以及会换行的 flex(含纵向换行)可以上下也可以左右;
|
||||
* 不换行的横向 flex 只看左右,其余只看上下。
|
||||
*/
|
||||
export const getFlowAxis = (target: Element | null | undefined): FlowAxis => {
|
||||
if (!target) return 'vertical';
|
||||
const parent = target.parentElement;
|
||||
if (parent) {
|
||||
const { display, flexDirection, flexWrap } = getComputedStyle(parent);
|
||||
if (display === 'flex' || display === 'inline-flex') {
|
||||
const wraps = flexWrap === 'wrap' || flexWrap === 'wrap-reverse';
|
||||
if (wraps) return 'both';
|
||||
const horizontal = flexDirection === 'row' || flexDirection === 'row-reverse';
|
||||
return horizontal ? 'horizontal' : 'vertical';
|
||||
}
|
||||
}
|
||||
const { display } = getComputedStyle(target);
|
||||
if (display === 'inline' || display === 'inline-block') return 'both';
|
||||
return 'vertical';
|
||||
};
|
||||
|
||||
/** 横向 flex,或组件自身是 inline / inline-block 时,流式拖动包含左右方向 */
|
||||
export const isHorizontalFlow = (target: Element | null | undefined): boolean => {
|
||||
const axis = getFlowAxis(target);
|
||||
return axis === 'horizontal' || axis === 'both';
|
||||
};
|
||||
|
||||
const getParents = (el: Element, relative: Element) => {
|
||||
let cur: Element | null = el.parentElement;
|
||||
const parents: Element[] = [];
|
||||
@ -240,10 +290,8 @@ export const addSelectedClassName = (el: Element, doc: Document) => {
|
||||
export const down = (deltaTop: number, target: TargetElement): SortEventData => {
|
||||
let swapIndex = 0;
|
||||
let addUpH = target.clientHeight;
|
||||
const brothers = Array.from(target.parentNode?.children || []).filter(
|
||||
(child) => !getIdFromEl()(child as HTMLElement)?.startsWith(GHOST_EL_ID_PREFIX),
|
||||
);
|
||||
const index = brothers.indexOf(target);
|
||||
const brothers = getFlowBrothers(target);
|
||||
const index = brothers.indexOf(target as HTMLElement);
|
||||
// 往下移动
|
||||
const downEls = brothers.slice(index + 1) as HTMLElement[];
|
||||
|
||||
@ -277,10 +325,8 @@ export const down = (deltaTop: number, target: TargetElement): SortEventData =>
|
||||
* @param {Object} detail 当前选中的组件配置
|
||||
*/
|
||||
export const up = (deltaTop: number, target: TargetElement): SortEventData => {
|
||||
const brothers = Array.from(target.parentNode?.children || []).filter(
|
||||
(child) => !getIdFromEl()(child as HTMLElement)?.startsWith(GHOST_EL_ID_PREFIX),
|
||||
);
|
||||
const index = brothers.indexOf(target);
|
||||
const brothers = getFlowBrothers(target);
|
||||
const index = brothers.indexOf(target as HTMLElement);
|
||||
// 往上移动
|
||||
const upEls = brothers.slice(0, index) as HTMLElement[];
|
||||
|
||||
@ -308,6 +354,272 @@ export const up = (deltaTop: number, target: TargetElement): SortEventData => {
|
||||
};
|
||||
};
|
||||
|
||||
const flowSortItems = (target: TargetElement): HTMLElement[] =>
|
||||
getFlowBrothers(target).filter((el) => el.style?.position !== 'fixed');
|
||||
|
||||
const axisDistance = (probe: number, start: number, end: number): number => {
|
||||
if (probe < start) return start - probe;
|
||||
if (probe > end) return probe - end;
|
||||
return 0;
|
||||
};
|
||||
|
||||
const distanceToRect = (rect: DOMRect, probeX: number, probeY: number): number =>
|
||||
axisDistance(probeX, rect.left, rect.right) + axisDistance(probeY, rect.top, rect.bottom);
|
||||
|
||||
interface FlowBand {
|
||||
items: HTMLElement[];
|
||||
start: number;
|
||||
end: number;
|
||||
}
|
||||
|
||||
/** 行按 top 聚成带,列按 left 聚成带。同一条带里起点差不超过半条带宽 */
|
||||
const groupBands = (items: HTMLElement[], column: boolean): FlowBand[] => {
|
||||
const bands: FlowBand[] = [];
|
||||
for (const el of items) {
|
||||
const rect = el.getBoundingClientRect();
|
||||
const start = column ? rect.left : rect.top;
|
||||
const end = column ? rect.right : rect.bottom;
|
||||
const size = Math.max(end - start, 1);
|
||||
const band = bands.find((candidate) => {
|
||||
const bandSize = Math.max(candidate.end - candidate.start, size) / 2;
|
||||
return Math.abs(candidate.start - start) <= bandSize;
|
||||
});
|
||||
if (band) {
|
||||
band.items.push(el);
|
||||
band.start = Math.min(band.start, start);
|
||||
band.end = Math.max(band.end, end);
|
||||
} else {
|
||||
bands.push({ items: [el], start, end });
|
||||
}
|
||||
}
|
||||
bands.sort((a, b) => a.start - b.start);
|
||||
return bands;
|
||||
};
|
||||
|
||||
const bandMid = (el: HTMLElement, column: boolean): number => {
|
||||
const box = el.getBoundingClientRect();
|
||||
return column ? box.top + (box.bottom - box.top) / 2 : box.left + (box.right - box.left) / 2;
|
||||
};
|
||||
|
||||
const closestOnBand = (items: HTMLElement[], probe: number, column: boolean): HTMLElement =>
|
||||
items.reduce((best, el) =>
|
||||
Math.abs(bandMid(el, column) - probe) < Math.abs(bandMid(best, column) - probe) ? el : best,
|
||||
);
|
||||
|
||||
/** 还在当前行或列里时只沿主轴插入,避免另一半被当成换行或换列 */
|
||||
const besideInBand = (anchor: HTMLElement, probe: number, column: boolean, reversed: boolean) => {
|
||||
const rect = anchor.getBoundingClientRect();
|
||||
const start = column ? rect.top : rect.left;
|
||||
const size = column ? rect.bottom - rect.top : rect.right - rect.left;
|
||||
const onEnd = probe > start + size / 2;
|
||||
let edge: SortEdge;
|
||||
if (column) edge = onEnd ? 'bottom' : 'top';
|
||||
else edge = onEnd ? 'right' : 'left';
|
||||
const insertAfter = onEnd ? !reversed : reversed;
|
||||
return { anchor, edge, distAnchor: anchor, insertAfter };
|
||||
};
|
||||
|
||||
/**
|
||||
* 换到相邻行或列时,落到目标节点的视觉位置。
|
||||
* 正向主轴插到目标前面;反向主轴且源还在目标前面时,sort 才会把源放到目标原来的下标。
|
||||
*/
|
||||
const crossInsertAfter = (
|
||||
items: HTMLElement[],
|
||||
target: TargetElement,
|
||||
anchor: HTMLElement,
|
||||
reversed: boolean,
|
||||
): boolean => {
|
||||
if (!reversed) return false;
|
||||
const srcIndex = items.indexOf(target as HTMLElement);
|
||||
const anchorIndex = items.indexOf(anchor);
|
||||
return srcIndex >= 0 && srcIndex < anchorIndex;
|
||||
};
|
||||
|
||||
const crossEdge = (column: boolean, after: boolean): SortEdge => {
|
||||
if (column) return after ? 'right' : 'left';
|
||||
return after ? 'bottom' : 'top';
|
||||
};
|
||||
|
||||
/**
|
||||
* 预览还压在当前行或列上时沿主轴画线。
|
||||
* 离开后画在交叉轴上:中间间隙落到相邻行或列,超出首尾则插到整段前面或后面。
|
||||
*/
|
||||
const hitWrapped = (
|
||||
items: HTMLElement[],
|
||||
probeX: number,
|
||||
probeY: number,
|
||||
column: boolean,
|
||||
reversed: boolean,
|
||||
target: TargetElement,
|
||||
) => {
|
||||
const bands = groupBands(items, column);
|
||||
if (!bands.length) return null;
|
||||
|
||||
const probeCross = column ? probeX : probeY;
|
||||
const probeMain = column ? probeY : probeX;
|
||||
|
||||
let bandIndex = bands.findIndex((band) => probeCross >= band.start && probeCross <= band.end);
|
||||
let outside: 'before' | 'after' | null = null;
|
||||
if (bandIndex < 0) {
|
||||
let bestDist = Number.POSITIVE_INFINITY;
|
||||
bands.forEach((band, index) => {
|
||||
const dist = probeCross < band.start ? band.start - probeCross : probeCross - band.end;
|
||||
if (dist < bestDist) {
|
||||
bestDist = dist;
|
||||
bandIndex = index;
|
||||
outside = probeCross < band.start ? 'before' : 'after';
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
const band = bands[bandIndex];
|
||||
const anchor = closestOnBand(band.items, probeMain, column);
|
||||
if (outside === 'after' && bandIndex < bands.length - 1) {
|
||||
const distAnchor = closestOnBand(bands[bandIndex + 1].items, probeMain, column);
|
||||
return {
|
||||
anchor,
|
||||
edge: crossEdge(column, true),
|
||||
distAnchor,
|
||||
insertAfter: crossInsertAfter(items, target, distAnchor, reversed),
|
||||
};
|
||||
}
|
||||
if (outside === 'before' && bandIndex > 0) {
|
||||
const distAnchor = closestOnBand(bands[bandIndex - 1].items, probeMain, column);
|
||||
return {
|
||||
anchor,
|
||||
edge: crossEdge(column, false),
|
||||
distAnchor,
|
||||
insertAfter: crossInsertAfter(items, target, distAnchor, reversed),
|
||||
};
|
||||
}
|
||||
if (outside === 'after') {
|
||||
const endItem = band.items[band.items.length - 1];
|
||||
return { anchor: endItem, edge: crossEdge(column, true), distAnchor: endItem, insertAfter: true };
|
||||
}
|
||||
if (outside === 'before') {
|
||||
const startItem = band.items[0];
|
||||
return { anchor: startItem, edge: crossEdge(column, false), distAnchor: startItem, insertAfter: false };
|
||||
}
|
||||
return besideInBand(anchor, probeMain, column, reversed);
|
||||
};
|
||||
|
||||
/**
|
||||
* 预览中心落在兄弟节点的哪一条边。
|
||||
* 不在任何节点内时贴到距离最近的节点。
|
||||
*/
|
||||
const hitSortAnchor = (
|
||||
items: HTMLElement[],
|
||||
probeX: number,
|
||||
probeY: number,
|
||||
axis: 'vertical' | 'horizontal',
|
||||
): { anchor: HTMLElement; edge: SortEdge } | null => {
|
||||
if (!items.length) return null;
|
||||
|
||||
let anchor = items.find((el) => distanceToRect(el.getBoundingClientRect(), probeX, probeY) === 0);
|
||||
if (!anchor) {
|
||||
anchor = items.reduce((best, el) => {
|
||||
const bestDist = distanceToRect(best.getBoundingClientRect(), probeX, probeY);
|
||||
const dist = distanceToRect(el.getBoundingClientRect(), probeX, probeY);
|
||||
return dist < bestDist ? el : best;
|
||||
});
|
||||
}
|
||||
|
||||
const rect = anchor.getBoundingClientRect();
|
||||
const vertical = axis === 'vertical';
|
||||
const start = vertical ? rect.top : rect.left;
|
||||
const size = vertical ? rect.bottom - rect.top : rect.right - rect.left;
|
||||
const probe = vertical ? probeY : probeX;
|
||||
const after = probe > start + size / 2;
|
||||
let edge: SortEdge;
|
||||
if (vertical) {
|
||||
edge = after ? 'bottom' : 'top';
|
||||
} else {
|
||||
edge = after ? 'right' : 'left';
|
||||
}
|
||||
return { anchor, edge };
|
||||
};
|
||||
|
||||
/**
|
||||
* 把「插到锚点前/后」换成 sort(src, dist) 能表达的目标 id。
|
||||
* 源在目标前面时,sort 会把源插到目标之后;源在目标后面时,sort 会把源插到目标之前。
|
||||
* 锚点就是被拖节点时,四条边都还是当前占位。
|
||||
*/
|
||||
const distForInsertion = (
|
||||
items: HTMLElement[],
|
||||
target: TargetElement,
|
||||
anchor: HTMLElement,
|
||||
insertAfter: boolean,
|
||||
): string => {
|
||||
const src = getIdFromEl()(target) || '';
|
||||
const srcIndex = items.indexOf(target as HTMLElement);
|
||||
const anchorIndex = items.indexOf(anchor);
|
||||
if (srcIndex < 0 || anchorIndex < 0 || srcIndex === anchorIndex) return src;
|
||||
|
||||
if (insertAfter) {
|
||||
if (srcIndex < anchorIndex) return getIdFromEl()(anchor) || src;
|
||||
const next = items[anchorIndex + 1];
|
||||
if (!next || next === target) return src;
|
||||
return getIdFromEl()(next) || src;
|
||||
}
|
||||
|
||||
if (srcIndex > anchorIndex) return getIdFromEl()(anchor) || src;
|
||||
const prev = items[anchorIndex - 1];
|
||||
if (!prev || prev === target) return src;
|
||||
return getIdFromEl()(prev) || src;
|
||||
};
|
||||
|
||||
const readFlexFlow = (container: Element | null | undefined): { column: boolean; reversed: boolean } => {
|
||||
if (!container) return { column: false, reversed: false };
|
||||
const { display, flexDirection } = getComputedStyle(container);
|
||||
if (display !== 'flex' && display !== 'inline-flex') return { column: false, reversed: false };
|
||||
if (flexDirection === 'column' || flexDirection === 'column-reverse') {
|
||||
return { column: true, reversed: flexDirection === 'column-reverse' };
|
||||
}
|
||||
return { column: false, reversed: flexDirection === 'row-reverse' };
|
||||
};
|
||||
|
||||
/**
|
||||
* 按拖拽预览的中心决定插入线和排序目标。
|
||||
* 会换行时,沿主轴的线表示仍插在这一行或这一列里;交叉轴上的线表示已经换到相邻行或列。
|
||||
* 预览中心还在被拖节点自己身上时不改变顺序。
|
||||
* 预览与组件在同一文档里,避免画布 iframe 和指针不在一个坐标系。
|
||||
*/
|
||||
export const resolveSortInsertion = (ghostEl: HTMLElement, target: TargetElement): SortInsertion => {
|
||||
const src = getIdFromEl()(target) || '';
|
||||
const items = flowSortItems(target);
|
||||
const axis = getFlowAxis(target);
|
||||
const { column, reversed } = readFlexFlow(target.parentElement);
|
||||
const ghostRect = ghostEl.getBoundingClientRect();
|
||||
const probeX = ghostRect.left + ghostRect.width / 2;
|
||||
const probeY = ghostRect.top + ghostRect.height / 2;
|
||||
if (axis === 'both') {
|
||||
const wrapped = hitWrapped(items, probeX, probeY, column, reversed, target);
|
||||
if (!wrapped) return { src, dist: src, edge: null, anchor: null };
|
||||
return {
|
||||
src,
|
||||
dist: distForInsertion(items, target, wrapped.distAnchor, wrapped.insertAfter),
|
||||
edge: wrapped.edge,
|
||||
anchor: wrapped.anchor,
|
||||
};
|
||||
}
|
||||
|
||||
const hit = hitSortAnchor(items, probeX, probeY, axis);
|
||||
if (!hit) return { src, dist: src, edge: null, anchor: null };
|
||||
|
||||
const vertical = axis === 'vertical';
|
||||
let forward: SortEdge;
|
||||
if (vertical) forward = column && reversed ? 'top' : 'bottom';
|
||||
else forward = reversed ? 'left' : 'right';
|
||||
const insertAfter = hit.edge === forward;
|
||||
|
||||
return {
|
||||
src,
|
||||
dist: distForInsertion(items, target, hit.anchor, insertAfter),
|
||||
edge: hit.edge,
|
||||
anchor: hit.anchor,
|
||||
};
|
||||
};
|
||||
|
||||
export const isMoveableButton = (target: Element) =>
|
||||
target.classList.contains('moveable-button') || target.parentElement?.classList.contains('moveable-button');
|
||||
|
||||
|
||||
@ -137,6 +137,152 @@ describe('DragResizeHelper', () => {
|
||||
helper.destroy();
|
||||
});
|
||||
|
||||
test('流式拖动时在悬停组件的上沿或下沿显示插入线', () => {
|
||||
const rect = (top: number, height: number): DOMRect => ({
|
||||
x: 0,
|
||||
y: top,
|
||||
width: 80,
|
||||
height,
|
||||
top,
|
||||
left: 0,
|
||||
right: 80,
|
||||
bottom: top + height,
|
||||
toJSON: () => ({}),
|
||||
});
|
||||
const helper = createHelper();
|
||||
helper.setMode(Mode.SORTABLE);
|
||||
const parent = globalThis.document.createElement('div');
|
||||
const a = createTarget('a');
|
||||
const b = createTarget('b');
|
||||
b.style.top = '100px';
|
||||
parent.append(a, b);
|
||||
globalThis.document.body.append(parent);
|
||||
Object.defineProperty(b, 'clientHeight', { value: 100, configurable: true });
|
||||
Object.defineProperty(b, 'clientWidth', { value: 80, configurable: true });
|
||||
a.getBoundingClientRect = () => rect(0, 100);
|
||||
b.getBoundingClientRect = () => rect(100, 100);
|
||||
|
||||
helper.updateShadowEl(a);
|
||||
helper.onDragStart({ target: helper.getShadowEl() } as any);
|
||||
const ghost = helper.getGhostEl()!;
|
||||
ghost.getBoundingClientRect = () => rect(110, 100);
|
||||
helper.onDrag({ beforeTranslate: [0, 110], target: helper.getShadowEl() } as any);
|
||||
|
||||
const indicator = globalThis.document.querySelector('[data-tmagic-id^="sort_indicator_el_"]') as HTMLElement;
|
||||
expect(indicator.dataset.sortEdge).toBe('bottom');
|
||||
expect(indicator.style.top).toBe('199px');
|
||||
expect(indicator.style.height).toBe('2px');
|
||||
expect(indicator.style.width).toBe('80px');
|
||||
expect(indicator.style.backgroundColor).toBe('#ff4d4f');
|
||||
const terminal = indicator.firstElementChild as HTMLElement;
|
||||
expect(terminal.style.width).toBe('8px');
|
||||
expect(terminal.style.borderRadius).toBe('50%');
|
||||
|
||||
ghost.getBoundingClientRect = () => rect(70, 100);
|
||||
helper.onDrag({ beforeTranslate: [0, 70], target: helper.getShadowEl() } as any);
|
||||
expect(indicator.dataset.sortEdge).toBe('top');
|
||||
expect(indicator.style.top).toBe('99px');
|
||||
|
||||
helper.destroyGhostEl();
|
||||
expect(globalThis.document.querySelector('[data-tmagic-id^="sort_indicator_el_"]')).toBeNull();
|
||||
helper.destroy();
|
||||
});
|
||||
|
||||
test('水平布局拖动时镜像跟着左右移动,并在侧边显示插入线', () => {
|
||||
const rect = (left: number, width: number): DOMRect => ({
|
||||
x: left,
|
||||
y: 0,
|
||||
width,
|
||||
height: 40,
|
||||
top: 0,
|
||||
left,
|
||||
right: left + width,
|
||||
bottom: 40,
|
||||
toJSON: () => ({}),
|
||||
});
|
||||
const helper = createHelper();
|
||||
helper.setMode(Mode.SORTABLE);
|
||||
const parent = globalThis.document.createElement('div');
|
||||
parent.style.display = 'flex';
|
||||
parent.style.flexDirection = 'row';
|
||||
const a = createTarget('row-a');
|
||||
const b = createTarget('row-b');
|
||||
a.style.left = '0px';
|
||||
a.style.top = '0px';
|
||||
b.style.left = '100px';
|
||||
b.style.top = '0px';
|
||||
parent.append(a, b);
|
||||
globalThis.document.body.append(parent);
|
||||
Object.defineProperty(b, 'clientWidth', { value: 100, configurable: true });
|
||||
Object.defineProperty(b, 'clientHeight', { value: 40, configurable: true });
|
||||
a.getBoundingClientRect = () => rect(0, 100);
|
||||
b.getBoundingClientRect = () => rect(100, 100);
|
||||
|
||||
helper.updateShadowEl(a);
|
||||
helper.onDragStart({ target: helper.getShadowEl() } as any);
|
||||
const ghost = helper.getGhostEl()!;
|
||||
ghost.getBoundingClientRect = () => rect(110, 100);
|
||||
helper.onDrag({ beforeTranslate: [80, 0], target: helper.getShadowEl() } as any);
|
||||
|
||||
expect(ghost.style.left).toBe('80px');
|
||||
const indicator = globalThis.document.querySelector('[data-tmagic-id^="sort_indicator_el_"]') as HTMLElement;
|
||||
expect(indicator.dataset.sortEdge).toBe('right');
|
||||
expect(indicator.style.width).toBe('2px');
|
||||
expect(indicator.style.height).toBe('40px');
|
||||
expect(indicator.style.left).toBe('199px');
|
||||
helper.destroy();
|
||||
});
|
||||
|
||||
test('插入线贴着画布上沿或左沿时整段留在可见区域内', () => {
|
||||
const box = (left: number, top: number, width: number, height: number): DOMRect => ({
|
||||
x: left,
|
||||
y: top,
|
||||
width,
|
||||
height,
|
||||
top,
|
||||
left,
|
||||
right: left + width,
|
||||
bottom: top + height,
|
||||
toJSON: () => ({}),
|
||||
});
|
||||
const helper = createHelper();
|
||||
helper.setMode(Mode.SORTABLE);
|
||||
const parent = globalThis.document.createElement('div');
|
||||
const a = createTarget('edge-a');
|
||||
a.style.left = '0px';
|
||||
a.style.top = '0px';
|
||||
parent.append(a);
|
||||
globalThis.document.body.append(parent);
|
||||
Object.defineProperty(a, 'clientWidth', { value: 80, configurable: true });
|
||||
Object.defineProperty(a, 'clientHeight', { value: 100, configurable: true });
|
||||
a.getBoundingClientRect = () => box(0, 0, 80, 100);
|
||||
|
||||
helper.updateShadowEl(a);
|
||||
helper.onDragStart({ target: helper.getShadowEl() } as any);
|
||||
const ghost = helper.getGhostEl()!;
|
||||
ghost.getBoundingClientRect = () => box(0, 0, 80, 40);
|
||||
helper.onDrag({ beforeTranslate: [0, 0], target: helper.getShadowEl() } as any);
|
||||
|
||||
const indicator = globalThis.document.querySelector('[data-tmagic-id^="sort_indicator_el_"]') as HTMLElement;
|
||||
const terminal = indicator.firstElementChild as HTMLElement;
|
||||
expect(indicator.dataset.sortEdge).toBe('top');
|
||||
expect(indicator.style.top).toBe('0px');
|
||||
expect(indicator.style.left).toBe('0px');
|
||||
expect(terminal.style.top).toBe('0px');
|
||||
expect(terminal.style.left).toBe('0px');
|
||||
|
||||
parent.style.display = 'flex';
|
||||
parent.style.flexDirection = 'row';
|
||||
ghost.getBoundingClientRect = () => box(0, 10, 40, 40);
|
||||
helper.onDrag({ beforeTranslate: [0, 0], target: helper.getShadowEl() } as any);
|
||||
expect(indicator.dataset.sortEdge).toBe('left');
|
||||
expect(indicator.style.left).toBe('0px');
|
||||
expect(indicator.style.top).toBe('0px');
|
||||
expect(terminal.style.left).toBe('0px');
|
||||
expect(terminal.style.top).toBe('0px');
|
||||
helper.destroy();
|
||||
});
|
||||
|
||||
test('onRotate / onScale 更新 transform', () => {
|
||||
const helper = createHelper();
|
||||
const target = createTarget();
|
||||
|
||||
@ -127,6 +127,33 @@ describe('StageDragResize', () => {
|
||||
dr.destroy();
|
||||
});
|
||||
|
||||
test('resize / rotate / scale 以及连续 drag 会进入拖动中', () => {
|
||||
const { dr } = createInstance();
|
||||
const target = createTarget();
|
||||
dr.select(target);
|
||||
const shadow = (dr as any).dragResizeHelper.getShadowEl();
|
||||
const timer = setTimeout(() => undefined, 1000);
|
||||
(dr as any).delayedMarkContainer = () => timer;
|
||||
|
||||
moveableHandlers.resizeStart?.forEach((fn) => fn({ target: shadow }));
|
||||
moveableHandlers.resize?.forEach((fn) =>
|
||||
fn({ target: shadow, width: 120, height: 90, drag: { beforeTranslate: [1, 2] } }),
|
||||
);
|
||||
moveableHandlers.rotateStart?.forEach((fn) => fn({ target: shadow }));
|
||||
moveableHandlers.rotate?.forEach((fn) => fn({ target: shadow }));
|
||||
moveableHandlers.scaleStart?.forEach((fn) => fn({ target: shadow }));
|
||||
moveableHandlers.scale?.forEach((fn) => fn({ target: shadow }));
|
||||
|
||||
const dragEvent = { target: shadow, beforeTranslate: [1, 2], inputEvent: new MouseEvent('mousemove') };
|
||||
moveableHandlers.dragStart?.forEach((fn) => fn({ target: shadow }));
|
||||
moveableHandlers.drag?.forEach((fn) => fn(dragEvent));
|
||||
moveableHandlers.drag?.forEach((fn) => fn(dragEvent));
|
||||
|
||||
expect(dr.getDragStatus()).toBe(StageDragStatus.ING);
|
||||
clearTimeout(timer);
|
||||
dr.destroy();
|
||||
});
|
||||
|
||||
test('resizeEnd 触发 update 事件', () => {
|
||||
const { dr } = createInstance();
|
||||
const target = createTarget();
|
||||
|
||||
@ -322,6 +322,625 @@ describe('down / up 排序', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('resolveSortInsertion', () => {
|
||||
const rect = (top: number, height: number): DOMRect => ({
|
||||
x: 0,
|
||||
y: top,
|
||||
width: 100,
|
||||
height,
|
||||
top,
|
||||
left: 0,
|
||||
right: 100,
|
||||
bottom: top + height,
|
||||
toJSON: () => ({}),
|
||||
});
|
||||
|
||||
const setupColumn = () => {
|
||||
const parent = globalThis.document.createElement('div');
|
||||
const els = ['a', 'b', 'c'].map((id, index) => {
|
||||
const el = globalThis.document.createElement('div');
|
||||
el.dataset.tmagicId = id;
|
||||
el.getBoundingClientRect = () => rect(index * 100, 100);
|
||||
parent.appendChild(el);
|
||||
return el;
|
||||
});
|
||||
const ghost = globalThis.document.createElement('div');
|
||||
ghost.dataset.tmagicId = 'ghost_el_a';
|
||||
parent.appendChild(ghost);
|
||||
return { parent, els, ghost };
|
||||
};
|
||||
|
||||
test('预览中心在下一节点上半部分时,线在其上沿且不改变顺序', () => {
|
||||
const { els, ghost } = setupColumn();
|
||||
ghost.getBoundingClientRect = () => rect(70, 100);
|
||||
const result = util.resolveSortInsertion(ghost, els[0]);
|
||||
expect(result.anchor).toBe(els[1]);
|
||||
expect(result.edge).toBe('top');
|
||||
expect(result).toMatchObject({ src: 'a', dist: 'a' });
|
||||
});
|
||||
|
||||
test('预览中心越过下一节点中线时,插到该节点后面', () => {
|
||||
const { els, ghost } = setupColumn();
|
||||
ghost.getBoundingClientRect = () => rect(110, 100);
|
||||
const result = util.resolveSortInsertion(ghost, els[0]);
|
||||
expect(result.anchor).toBe(els[1]);
|
||||
expect(result.edge).toBe('bottom');
|
||||
expect(result).toMatchObject({ src: 'a', dist: 'b' });
|
||||
});
|
||||
|
||||
test('向上拖到上一节点上半部分时,插到该节点前面', () => {
|
||||
const { els, ghost } = setupColumn();
|
||||
ghost.getBoundingClientRect = () => rect(70, 100);
|
||||
const result = util.resolveSortInsertion(ghost, els[2]);
|
||||
expect(result.anchor).toBe(els[1]);
|
||||
expect(result.edge).toBe('top');
|
||||
expect(result).toMatchObject({ src: 'c', dist: 'b' });
|
||||
});
|
||||
|
||||
test('向上拖但仍在上一节点下半部分时,保持原位', () => {
|
||||
const { els, ghost } = setupColumn();
|
||||
ghost.getBoundingClientRect = () => rect(110, 100);
|
||||
const result = util.resolveSortInsertion(ghost, els[2]);
|
||||
expect(result.anchor).toBe(els[1]);
|
||||
expect(result.edge).toBe('bottom');
|
||||
expect(result).toMatchObject({ src: 'c', dist: 'c' });
|
||||
});
|
||||
|
||||
test('预览低于所有节点时,线在最后一个节点下沿', () => {
|
||||
const { els, ghost } = setupColumn();
|
||||
ghost.getBoundingClientRect = () => rect(360, 80);
|
||||
const result = util.resolveSortInsertion(ghost, els[0]);
|
||||
expect(result.anchor).toBe(els[2]);
|
||||
expect(result.edge).toBe('bottom');
|
||||
expect(result).toMatchObject({ src: 'a', dist: 'c' });
|
||||
});
|
||||
|
||||
test('源在锚点下方且要插到锚点后面时,落到锚点的下一个节点', () => {
|
||||
const { els, ghost } = setupColumn();
|
||||
ghost.getBoundingClientRect = () => rect(20, 80);
|
||||
const result = util.resolveSortInsertion(ghost, els[2]);
|
||||
expect(result.anchor).toBe(els[0]);
|
||||
expect(result.edge).toBe('bottom');
|
||||
expect(result).toMatchObject({ src: 'c', dist: 'b' });
|
||||
});
|
||||
|
||||
test('源在锚点上方且要插到锚点前面时,落到锚点的上一个节点', () => {
|
||||
const { els, ghost } = setupColumn();
|
||||
ghost.getBoundingClientRect = () => rect(180, 80);
|
||||
const result = util.resolveSortInsertion(ghost, els[0]);
|
||||
expect(result.anchor).toBe(els[2]);
|
||||
expect(result.edge).toBe('top');
|
||||
expect(result).toMatchObject({ src: 'a', dist: 'b' });
|
||||
});
|
||||
|
||||
test('插入指示线和 fixed 节点不参与命中', () => {
|
||||
const { parent, els, ghost } = setupColumn();
|
||||
const indicator = globalThis.document.createElement('div');
|
||||
indicator.dataset.tmagicId = 'sort_indicator_el_b';
|
||||
indicator.getBoundingClientRect = () => rect(100, 100);
|
||||
parent.insertBefore(indicator, els[1]);
|
||||
els[1].style.position = 'fixed';
|
||||
|
||||
ghost.getBoundingClientRect = () => rect(110, 100);
|
||||
const result = util.resolveSortInsertion(ghost, els[0]);
|
||||
expect(result.anchor).toBe(els[2]);
|
||||
expect(result.edge).toBe('top');
|
||||
});
|
||||
|
||||
const horizontalRect = (left: number, width = 100): DOMRect => ({
|
||||
x: left,
|
||||
y: 0,
|
||||
width,
|
||||
height: 40,
|
||||
top: 0,
|
||||
left,
|
||||
right: left + width,
|
||||
bottom: 40,
|
||||
toJSON: () => ({}),
|
||||
});
|
||||
|
||||
const setupRow = (direction: 'row' | 'row-reverse' = 'row') => {
|
||||
const parent = globalThis.document.createElement('div');
|
||||
parent.style.display = 'flex';
|
||||
parent.style.flexDirection = direction;
|
||||
const els = ['a', 'b', 'c'].map((id, index) => {
|
||||
const el = globalThis.document.createElement('div');
|
||||
el.dataset.tmagicId = id;
|
||||
const left = direction === 'row' ? index * 100 : (2 - index) * 100;
|
||||
el.getBoundingClientRect = () => horizontalRect(left);
|
||||
parent.appendChild(el);
|
||||
return el;
|
||||
});
|
||||
const ghost = globalThis.document.createElement('div');
|
||||
ghost.dataset.tmagicId = 'ghost_el_a';
|
||||
parent.appendChild(ghost);
|
||||
globalThis.document.body.appendChild(parent);
|
||||
return { parent, els, ghost };
|
||||
};
|
||||
|
||||
test('水平布局预览中心在下一节点左半部分时,线在其左沿且不改变顺序', () => {
|
||||
const { els, ghost } = setupRow();
|
||||
ghost.getBoundingClientRect = () => horizontalRect(70, 100);
|
||||
const result = util.resolveSortInsertion(ghost, els[0]);
|
||||
expect(result.anchor).toBe(els[1]);
|
||||
expect(result.edge).toBe('left');
|
||||
expect(result).toMatchObject({ src: 'a', dist: 'a' });
|
||||
});
|
||||
|
||||
test('水平布局预览中心越过下一节点中线时,插到该节点右边', () => {
|
||||
const { els, ghost } = setupRow();
|
||||
ghost.getBoundingClientRect = () => horizontalRect(110, 100);
|
||||
const result = util.resolveSortInsertion(ghost, els[0]);
|
||||
expect(result.anchor).toBe(els[1]);
|
||||
expect(result.edge).toBe('right');
|
||||
expect(result).toMatchObject({ src: 'a', dist: 'b' });
|
||||
});
|
||||
|
||||
test('水平反向布局时,视觉右侧对应文档顺序的前面', () => {
|
||||
const { els, ghost } = setupRow('row-reverse');
|
||||
ghost.getBoundingClientRect = () => horizontalRect(110, 100);
|
||||
const result = util.resolveSortInsertion(ghost, els[2]);
|
||||
expect(result.anchor).toBe(els[1]);
|
||||
expect(result.edge).toBe('right');
|
||||
expect(result).toMatchObject({ src: 'c', dist: 'b' });
|
||||
});
|
||||
|
||||
test.each(['inline', 'inline-block'] as const)('%s 预览落在下一行组件上时,按这一行的左右插入', (display) => {
|
||||
const parent = globalThis.document.createElement('div');
|
||||
const place = (id: string, left: number, top: number) => {
|
||||
const el = globalThis.document.createElement('div');
|
||||
el.dataset.tmagicId = id;
|
||||
el.style.display = display;
|
||||
el.getBoundingClientRect = () => ({
|
||||
x: left,
|
||||
y: top,
|
||||
width: 100,
|
||||
height: 40,
|
||||
top,
|
||||
left,
|
||||
right: left + 100,
|
||||
bottom: top + 40,
|
||||
toJSON: () => ({}),
|
||||
});
|
||||
parent.appendChild(el);
|
||||
return el;
|
||||
};
|
||||
const a = place('a', 0, 0);
|
||||
place('b', 100, 0);
|
||||
const c = place('c', 0, 50);
|
||||
const ghost = globalThis.document.createElement('div');
|
||||
ghost.getBoundingClientRect = () => ({
|
||||
x: 10,
|
||||
y: 50,
|
||||
width: 80,
|
||||
height: 20,
|
||||
top: 50,
|
||||
left: 10,
|
||||
right: 90,
|
||||
bottom: 70,
|
||||
toJSON: () => ({}),
|
||||
});
|
||||
parent.appendChild(ghost);
|
||||
globalThis.document.body.appendChild(parent);
|
||||
|
||||
const result = util.resolveSortInsertion(ghost, a);
|
||||
expect(result.anchor).toBe(c);
|
||||
expect(result.edge).toBe('left');
|
||||
expect(result).toMatchObject({ src: 'a', dist: 'b' });
|
||||
});
|
||||
|
||||
test('同一行下半区仍按左右插入,不画下方横线', () => {
|
||||
const parent = globalThis.document.createElement('div');
|
||||
const els = ['a', 'b', 'c'].map((id, index) => {
|
||||
const el = globalThis.document.createElement('div');
|
||||
el.dataset.tmagicId = id;
|
||||
el.style.display = 'inline-block';
|
||||
el.getBoundingClientRect = () => horizontalRect(index * 100);
|
||||
parent.appendChild(el);
|
||||
return el;
|
||||
});
|
||||
const ghost = globalThis.document.createElement('div');
|
||||
ghost.dataset.tmagicId = 'ghost_el_a';
|
||||
ghost.getBoundingClientRect = () => ({
|
||||
x: 110,
|
||||
y: 20,
|
||||
width: 100,
|
||||
height: 20,
|
||||
top: 20,
|
||||
left: 110,
|
||||
right: 210,
|
||||
bottom: 40,
|
||||
toJSON: () => ({}),
|
||||
});
|
||||
parent.appendChild(ghost);
|
||||
globalThis.document.body.appendChild(parent);
|
||||
|
||||
const result = util.resolveSortInsertion(ghost, els[0]);
|
||||
expect(result.anchor).toBe(els[1]);
|
||||
expect(result.edge).toBe('right');
|
||||
expect(result).toMatchObject({ src: 'a', dist: 'b' });
|
||||
});
|
||||
|
||||
test('拖出当前行后横线表示换到下一行,而不是插到右边', () => {
|
||||
const parent = globalThis.document.createElement('div');
|
||||
const place = (id: string, left: number, top: number) => {
|
||||
const el = globalThis.document.createElement('div');
|
||||
el.dataset.tmagicId = id;
|
||||
el.style.display = 'inline-block';
|
||||
el.getBoundingClientRect = () => ({
|
||||
x: left,
|
||||
y: top,
|
||||
width: 100,
|
||||
height: 40,
|
||||
top,
|
||||
left,
|
||||
right: left + 100,
|
||||
bottom: top + 40,
|
||||
toJSON: () => ({}),
|
||||
});
|
||||
parent.appendChild(el);
|
||||
return el;
|
||||
};
|
||||
const a = place('a', 0, 0);
|
||||
const b = place('b', 100, 0);
|
||||
place('c', 200, 0);
|
||||
place('d', 0, 50);
|
||||
place('e', 100, 50);
|
||||
const ghost = globalThis.document.createElement('div');
|
||||
ghost.dataset.tmagicId = 'ghost_el_a';
|
||||
ghost.getBoundingClientRect = () => ({
|
||||
x: 100,
|
||||
y: 35,
|
||||
width: 100,
|
||||
height: 20,
|
||||
top: 35,
|
||||
left: 100,
|
||||
right: 200,
|
||||
bottom: 55,
|
||||
toJSON: () => ({}),
|
||||
});
|
||||
parent.appendChild(ghost);
|
||||
globalThis.document.body.appendChild(parent);
|
||||
|
||||
const result = util.resolveSortInsertion(ghost, a);
|
||||
expect(result.anchor).toBe(b);
|
||||
expect(result.edge).toBe('bottom');
|
||||
expect(result.dist).toBe('d');
|
||||
});
|
||||
|
||||
test.each(['inline', 'inline-block'] as const)('%s 布局按左右插入', (display) => {
|
||||
const parent = globalThis.document.createElement('div');
|
||||
const els = ['a', 'b', 'c'].map((id, index) => {
|
||||
const el = globalThis.document.createElement('div');
|
||||
el.dataset.tmagicId = id;
|
||||
el.style.display = display;
|
||||
el.getBoundingClientRect = () => horizontalRect(index * 100);
|
||||
parent.appendChild(el);
|
||||
return el;
|
||||
});
|
||||
const ghost = globalThis.document.createElement('div');
|
||||
ghost.dataset.tmagicId = 'ghost_el_a';
|
||||
ghost.getBoundingClientRect = () => horizontalRect(110, 100);
|
||||
parent.appendChild(ghost);
|
||||
globalThis.document.body.appendChild(parent);
|
||||
|
||||
const result = util.resolveSortInsertion(ghost, els[0]);
|
||||
expect(result.anchor).toBe(els[1]);
|
||||
expect(result.edge).toBe('right');
|
||||
expect(result).toMatchObject({ src: 'a', dist: 'b' });
|
||||
});
|
||||
|
||||
test('纵向 flex 里的 inline-block 仍按上下插入', () => {
|
||||
const parent = globalThis.document.createElement('div');
|
||||
parent.style.display = 'flex';
|
||||
parent.style.flexDirection = 'column';
|
||||
const els = ['a', 'b'].map((id, index) => {
|
||||
const el = globalThis.document.createElement('div');
|
||||
el.dataset.tmagicId = id;
|
||||
el.style.display = 'inline-block';
|
||||
el.getBoundingClientRect = () => rect(index * 100, 100);
|
||||
parent.appendChild(el);
|
||||
return el;
|
||||
});
|
||||
const ghost = globalThis.document.createElement('div');
|
||||
ghost.getBoundingClientRect = () => rect(110, 100);
|
||||
parent.appendChild(ghost);
|
||||
globalThis.document.body.appendChild(parent);
|
||||
|
||||
const result = util.resolveSortInsertion(ghost, els[0]);
|
||||
expect(result.edge).toBe('bottom');
|
||||
expect(result).toMatchObject({ src: 'a', dist: 'b' });
|
||||
});
|
||||
|
||||
test('没有兄弟节点时不给出插入线', () => {
|
||||
const target = globalThis.document.createElement('div');
|
||||
target.dataset.tmagicId = 'solo';
|
||||
const ghost = globalThis.document.createElement('div');
|
||||
ghost.getBoundingClientRect = () => rect(0, 40);
|
||||
const result = util.resolveSortInsertion(ghost, target);
|
||||
expect(result).toMatchObject({ src: 'solo', dist: 'solo', edge: null, anchor: null });
|
||||
});
|
||||
|
||||
test('预览中心还在自身下半部分时保持原位', () => {
|
||||
const { els, ghost } = setupColumn();
|
||||
ghost.getBoundingClientRect = () => rect(20, 100);
|
||||
const result = util.resolveSortInsertion(ghost, els[0]);
|
||||
expect(result.anchor).toBe(els[0]);
|
||||
expect(result.edge).toBe('bottom');
|
||||
expect(result).toMatchObject({ src: 'a', dist: 'a' });
|
||||
});
|
||||
|
||||
test('预览中心还在自身上半部分时不和前一个交换', () => {
|
||||
const { els, ghost } = setupColumn();
|
||||
ghost.getBoundingClientRect = () => rect(100, 40);
|
||||
const result = util.resolveSortInsertion(ghost, els[1]);
|
||||
expect(result.anchor).toBe(els[1]);
|
||||
expect(result.edge).toBe('top');
|
||||
expect(result).toMatchObject({ src: 'b', dist: 'b' });
|
||||
});
|
||||
|
||||
test('水平布局预览中心还在自身右半部分时保持原位', () => {
|
||||
const { els, ghost } = setupRow();
|
||||
ghost.getBoundingClientRect = () => horizontalRect(20, 100);
|
||||
const result = util.resolveSortInsertion(ghost, els[0]);
|
||||
expect(result.anchor).toBe(els[0]);
|
||||
expect(result.edge).toBe('right');
|
||||
expect(result).toMatchObject({ src: 'a', dist: 'a' });
|
||||
});
|
||||
|
||||
test('拖到最后一行下方时,横线在行尾下沿并插到末尾', () => {
|
||||
const parent = globalThis.document.createElement('div');
|
||||
const els = ['a', 'b', 'c'].map((id, index) => {
|
||||
const el = globalThis.document.createElement('div');
|
||||
el.dataset.tmagicId = id;
|
||||
el.style.display = 'inline-block';
|
||||
el.getBoundingClientRect = () => horizontalRect(index * 100);
|
||||
parent.appendChild(el);
|
||||
return el;
|
||||
});
|
||||
const ghost = globalThis.document.createElement('div');
|
||||
ghost.dataset.tmagicId = 'ghost_el_a';
|
||||
ghost.getBoundingClientRect = () => ({
|
||||
x: 100,
|
||||
y: 50,
|
||||
width: 40,
|
||||
height: 20,
|
||||
top: 50,
|
||||
left: 100,
|
||||
right: 140,
|
||||
bottom: 70,
|
||||
toJSON: () => ({}),
|
||||
});
|
||||
parent.appendChild(ghost);
|
||||
globalThis.document.body.appendChild(parent);
|
||||
|
||||
const result = util.resolveSortInsertion(ghost, els[0]);
|
||||
expect(result.anchor).toBe(els[2]);
|
||||
expect(result.edge).toBe('bottom');
|
||||
expect(result).toMatchObject({ src: 'a', dist: 'c' });
|
||||
});
|
||||
|
||||
test('拖到第一行上方时,横线在行首上沿并插到开头', () => {
|
||||
const parent = globalThis.document.createElement('div');
|
||||
const els = ['a', 'b', 'c'].map((id, index) => {
|
||||
const el = globalThis.document.createElement('div');
|
||||
el.dataset.tmagicId = id;
|
||||
el.style.display = 'inline-block';
|
||||
el.getBoundingClientRect = () => horizontalRect(index * 100);
|
||||
parent.appendChild(el);
|
||||
return el;
|
||||
});
|
||||
const ghost = globalThis.document.createElement('div');
|
||||
ghost.dataset.tmagicId = 'ghost_el_c';
|
||||
ghost.getBoundingClientRect = () => ({
|
||||
x: 100,
|
||||
y: -30,
|
||||
width: 40,
|
||||
height: 20,
|
||||
top: -30,
|
||||
left: 100,
|
||||
right: 140,
|
||||
bottom: -10,
|
||||
toJSON: () => ({}),
|
||||
});
|
||||
parent.appendChild(ghost);
|
||||
globalThis.document.body.appendChild(parent);
|
||||
|
||||
const result = util.resolveSortInsertion(ghost, els[2]);
|
||||
expect(result.anchor).toBe(els[0]);
|
||||
expect(result.edge).toBe('top');
|
||||
expect(result).toMatchObject({ src: 'c', dist: 'a' });
|
||||
});
|
||||
|
||||
test('横向反向换行时,向下换行落到下一行同一视觉格', () => {
|
||||
const parent = globalThis.document.createElement('div');
|
||||
parent.style.display = 'flex';
|
||||
parent.style.flexDirection = 'row-reverse';
|
||||
parent.style.flexWrap = 'wrap';
|
||||
const place = (id: string, left: number, top: number) => {
|
||||
const el = globalThis.document.createElement('div');
|
||||
el.dataset.tmagicId = id;
|
||||
el.getBoundingClientRect = () => ({
|
||||
x: left,
|
||||
y: top,
|
||||
width: 100,
|
||||
height: 40,
|
||||
top,
|
||||
left,
|
||||
right: left + 100,
|
||||
bottom: top + 40,
|
||||
toJSON: () => ({}),
|
||||
});
|
||||
parent.appendChild(el);
|
||||
return el;
|
||||
};
|
||||
const a = place('a', 100, 0);
|
||||
place('b', 0, 0);
|
||||
place('c', 100, 50);
|
||||
place('d', 0, 50);
|
||||
const ghost = globalThis.document.createElement('div');
|
||||
ghost.dataset.tmagicId = 'ghost_el_a';
|
||||
ghost.getBoundingClientRect = () => ({
|
||||
x: 130,
|
||||
y: 35,
|
||||
width: 40,
|
||||
height: 20,
|
||||
top: 35,
|
||||
left: 130,
|
||||
right: 170,
|
||||
bottom: 55,
|
||||
toJSON: () => ({}),
|
||||
});
|
||||
parent.appendChild(ghost);
|
||||
globalThis.document.body.appendChild(parent);
|
||||
|
||||
const result = util.resolveSortInsertion(ghost, a);
|
||||
expect(result.anchor).toBe(a);
|
||||
expect(result.edge).toBe('bottom');
|
||||
expect(result.dist).toBe('c');
|
||||
});
|
||||
|
||||
test('纵向反向布局时,视觉下半部分对应文档顺序的前面', () => {
|
||||
const parent = globalThis.document.createElement('div');
|
||||
parent.style.display = 'flex';
|
||||
parent.style.flexDirection = 'column-reverse';
|
||||
const els = ['a', 'b', 'c'].map((id, index) => {
|
||||
const el = globalThis.document.createElement('div');
|
||||
el.dataset.tmagicId = id;
|
||||
el.getBoundingClientRect = () => rect((2 - index) * 100, 100);
|
||||
parent.appendChild(el);
|
||||
return el;
|
||||
});
|
||||
const ghost = globalThis.document.createElement('div');
|
||||
ghost.dataset.tmagicId = 'ghost_el_c';
|
||||
ghost.getBoundingClientRect = () => rect(110, 100);
|
||||
parent.appendChild(ghost);
|
||||
globalThis.document.body.appendChild(parent);
|
||||
|
||||
const down = util.resolveSortInsertion(ghost, els[2]);
|
||||
expect(down.anchor).toBe(els[1]);
|
||||
expect(down.edge).toBe('bottom');
|
||||
expect(down).toMatchObject({ src: 'c', dist: 'b' });
|
||||
|
||||
ghost.getBoundingClientRect = () => rect(100, 40);
|
||||
const up = util.resolveSortInsertion(ghost, els[0]);
|
||||
expect(up.anchor).toBe(els[1]);
|
||||
expect(up.edge).toBe('top');
|
||||
expect(up).toMatchObject({ src: 'a', dist: 'b' });
|
||||
});
|
||||
|
||||
test('纵向换行时在同一列按上下插入', () => {
|
||||
const parent = globalThis.document.createElement('div');
|
||||
parent.style.display = 'flex';
|
||||
parent.style.flexDirection = 'column';
|
||||
parent.style.flexWrap = 'wrap';
|
||||
const place = (id: string, left: number, top: number) => {
|
||||
const el = globalThis.document.createElement('div');
|
||||
el.dataset.tmagicId = id;
|
||||
el.getBoundingClientRect = () => ({
|
||||
x: left,
|
||||
y: top,
|
||||
width: 100,
|
||||
height: 40,
|
||||
top,
|
||||
left,
|
||||
right: left + 100,
|
||||
bottom: top + 40,
|
||||
toJSON: () => ({}),
|
||||
});
|
||||
parent.appendChild(el);
|
||||
return el;
|
||||
};
|
||||
const a = place('a', 0, 0);
|
||||
const b = place('b', 0, 40);
|
||||
place('c', 100, 0);
|
||||
const e = place('e', 100, 40);
|
||||
const ghost = globalThis.document.createElement('div');
|
||||
ghost.dataset.tmagicId = 'ghost_el_a';
|
||||
ghost.getBoundingClientRect = () => ({
|
||||
x: 20,
|
||||
y: 60,
|
||||
width: 40,
|
||||
height: 20,
|
||||
top: 60,
|
||||
left: 20,
|
||||
right: 60,
|
||||
bottom: 80,
|
||||
toJSON: () => ({}),
|
||||
});
|
||||
parent.appendChild(ghost);
|
||||
globalThis.document.body.appendChild(parent);
|
||||
|
||||
const inside = util.resolveSortInsertion(ghost, a);
|
||||
expect(inside.anchor).toBe(b);
|
||||
expect(inside.edge).toBe('bottom');
|
||||
expect(inside).toMatchObject({ src: 'a', dist: 'b' });
|
||||
|
||||
ghost.getBoundingClientRect = () => ({
|
||||
x: 120,
|
||||
y: 60,
|
||||
width: 40,
|
||||
height: 20,
|
||||
top: 60,
|
||||
left: 120,
|
||||
right: 160,
|
||||
bottom: 80,
|
||||
toJSON: () => ({}),
|
||||
});
|
||||
const nextColumn = util.resolveSortInsertion(ghost, a);
|
||||
expect(nextColumn.anchor).toBe(e);
|
||||
expect(nextColumn.edge).toBe('bottom');
|
||||
expect(nextColumn).toMatchObject({ src: 'a', dist: 'e' });
|
||||
});
|
||||
|
||||
test('拖出最后一列时,竖线在列尾右侧并插到末尾', () => {
|
||||
const parent = globalThis.document.createElement('div');
|
||||
parent.style.display = 'flex';
|
||||
parent.style.flexDirection = 'column';
|
||||
parent.style.flexWrap = 'wrap';
|
||||
const place = (id: string, left: number, top: number) => {
|
||||
const el = globalThis.document.createElement('div');
|
||||
el.dataset.tmagicId = id;
|
||||
el.getBoundingClientRect = () => ({
|
||||
x: left,
|
||||
y: top,
|
||||
width: 100,
|
||||
height: 40,
|
||||
top,
|
||||
left,
|
||||
right: left + 100,
|
||||
bottom: top + 40,
|
||||
toJSON: () => ({}),
|
||||
});
|
||||
parent.appendChild(el);
|
||||
return el;
|
||||
};
|
||||
const a = place('a', 0, 0);
|
||||
place('b', 0, 40);
|
||||
const d = place('d', 100, 40);
|
||||
const ghost = globalThis.document.createElement('div');
|
||||
ghost.dataset.tmagicId = 'ghost_el_a';
|
||||
ghost.getBoundingClientRect = () => ({
|
||||
x: 220,
|
||||
y: 10,
|
||||
width: 40,
|
||||
height: 20,
|
||||
top: 10,
|
||||
left: 220,
|
||||
right: 260,
|
||||
bottom: 30,
|
||||
toJSON: () => ({}),
|
||||
});
|
||||
parent.appendChild(ghost);
|
||||
globalThis.document.body.appendChild(parent);
|
||||
|
||||
const result = util.resolveSortInsertion(ghost, a);
|
||||
expect(result.anchor).toBe(d);
|
||||
expect(result.edge).toBe('right');
|
||||
expect(result).toMatchObject({ src: 'a', dist: 'd' });
|
||||
});
|
||||
});
|
||||
|
||||
describe('scrollElementIntoView', () => {
|
||||
const makeDomRect = (partial: Partial<DOMRect>): DOMRect => ({
|
||||
x: partial.x ?? partial.left ?? 0,
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user