优化这段代码 let currentDropTarget: any; let transferredImage: HTMLElement | null; function dragTable(e: DragEvent) { if (!e || !e.dataTransfer) return; currentDropTarget = e.target; const field: string | number | undefined = (e.target as HTMLElement)?.id; const column = columnAll.value.find((c) => c.field == field)!; const showedElement = generateDragImage(column?.name); e.dataTransfer?.setDragImage(showedElement || document.body, 0, 0); e.dataTransfer.setData("text/plain", field); } function generateDragImage(name: string) { transferredImage = document.createElement("div"); document.body.appendChild(transferredImage); transferredImage.className = "table-header-transferred-image"; transferredImage.innerText = name; return transferredImage; } const changeBorder = throttle((target) => { if ( (target.draggable || target.classList.contains("s2-index-header")) && currentDropTarget && currentDropTarget != target ) { changeClassName(false); currentDropTarget = target; changeClassName(true); } }, 100); function allowDrop(e: MouseEvent) { e.preventDefault(); const target = e.target as HTMLElement; if (target) changeBorder( target.draggable || target.classList.contains("s2-index-header") ? target : target.parentNode ); } function dropTable(e: DragEvent) { e.preventDefault(); if (!e) return; const dragField = e.dataTransfer?.getData("text/plain") || ""; const dropField = (e.target as HTMLElement)?.id; if (dragField && dropField && dragField != dropField) { changeColumnOrder(dragField, dropField); } } function onDragend() { changeClassName(false); currentDropTarget = undefined; transferredImage && document.body.removeChild(transferredImage); } function changeClassName(isAdded: boolean) { if (isAdded) currentDropTarget.classList.add("s2-self-defined-dom-border"); else currentDropTarget.classList.remove("s2-self-defined-dom-border"); } function changeColumnOrder(dragField: string, dropField: string) { const dragIndex = columnAll.value.findIndex( (item) => item.field == dragField ); const dragColumn = columnAll.value.splice(dragIndex, 1)[0]; const dropIndex = dropField == "-1" ? -1 : columnAll.value.findIndex((item) => item.field == dropField); columnAll.value.splice(dropIndex + 1, 0, dragColumn); dataManager.updateDataConfig({ meta: columnAll.value }); tableManager?.render(dataManager.getFormattedData()); onColumnShowChange(); }
时间: 2024-01-26 10:01:57 浏览: 159
这段代码可以进行如下优化:
1. 使用类型定义来提高代码可读性和类型安全性;
2. 使用解构赋值来简化代码;
3. 函数名可以更具描述性;
4. 使用可选链操作符来简化代码。
优化后的代码如下:
```typescript
type Column = {
field: string | number | undefined;
name: string;
};
let currentDropTarget: HTMLElement | undefined;
let transferredImage: HTMLElement | | undefined;
function handleDragStart(e: DragEvent): void {
const { dataTransfer, target } = e;
if (!dataTransfer) return;
const field = (target as HTMLElement)?.id;
const column = columnAll.value.find((c) => c.field === field)!;
const showedElement = generateDragImage(column.name);
dataTransfer.setDragImage(showedElement || document.body, 0, 0);
dataTransfer.setData("text/plain", field);
currentDropTarget = target as HTMLElement;
}
function generateDragImage(name: string): HTMLElement {
transferredImage = document.createElement("div");
document.body.appendChild(transferredImage);
transferredImage.className = "table-header-transferred-image";
transferredImage.innerText = name;
return transferredImage;
}
const changeBorder = throttle((target: HTMLElement): void => {
if (
(target.draggable || target.classList.contains("s2-index-header")) &&
currentDropTarget &&
currentDropTarget !== target
) {
changeClassName(false);
currentDropTarget = target;
changeClassName(true);
}
}, 100);
function handleDragOver(e: MouseEvent): void {
e.preventDefault();
const target = e.target as HTMLElement;
if (!target) return;
const node = target.draggable
? target
: target.closest(".s2-index-header");
if (node) changeBorder(node);
}
function handleDrop(e: DragEvent): void {
e.preventDefault();
const dragField = e.dataTransfer?.getData("text/plain") || "";
const dropField = (e.target as HTMLElement)?.id || "";
if (dragField && dropField && dragField !== dropField) {
changeColumnOrder(dragField, dropField);
}
}
function handleDragEnd(): void {
changeClassName(false);
currentDropTarget = undefined;
transferredImage && document.body.removeChild(transferredImage);
}
function changeClassName(isAdded: boolean): void {
if (!currentDropTarget) return;
const className = "s2-self-defined-dom-border";
isAdded ? currentDropTarget.classList.add(className) : currentDropTarget.classList.remove(className);
}
function changeColumnOrder(dragField: string, dropField: string): void {
const dragIndex = columnAll.value.findIndex((item) => item.field === dragField);
const dragColumn = columnAll.value.splice(dragIndex, 1)[0];
const dropIndex = dropField === "-1" ? -1 : columnAll.value.findIndex((item) => item.field === dropField);
columnAll.value.splice(dropIndex + 1, 0, dragColumn);
dataManager.updateDataConfig({ meta: columnAll.value });
tableManager?.render(dataManager.getFormattedData());
onColumnShowChange();
}
```
阅读全文