Untitled conversation
- Usercode
- Assistant
return class DndPlugin extends RectLifecycleInterface {
constructor() {
super();
this.currentRect = null;
this.action = NULL;
this.host = null;
this.startX = 0;
this.startY = 0;
this.startRect = null;
this.boundOnMouseMove = this.onMouseMove.bind(this);
this.boundOnMouseUp = this.onMouseUp.bind(this);
this.listeners = new WeakMap();
this.isAnimating = false;
this.MIN_WIDTH = 50;
this.MIN_HEIGHT = 50;
}
attach(host) {
if (host) {
this.host = host;
}
return null;
}
onRectCreate(rect, element) {
const callback = this.onMouseDown.bind(this, rect);
this.listeners.set(element, callback);
element.addEventListener('mousedown', callback);
}
onRectRemove(rect, element) {
const callback = this.listeners.get(element);
if (callback) {
element.removeEventListener('mousedown', callback);
this.listeners.delete(element);
}
}
onMouseDown(rect, e) {
const rectElement = e.target;
if (!rectElement) return;
const rectStyle = rectElement.style;
const hostStyle = this.host.style;
const width = parseFloat(rectStyle.getPropertyValue('--width'));
const height = parseFloat(rectStyle.getPropertyValue('--height'));
const borderSize = parseFloat(rectStyle.getPropertyValue('--border-size') || hostStyle.getPropertyValue('--border-size')) || 6;
const vertexSize = parseFloat(rectStyle.getPropertyValue('--vertex-size') || hostStyle.getPropertyValue('--vertex-size')) || 0;
const bbox = rectElement.getBoundingClientRect();
const cursorOffsetX = e.clientX - bbox.left;
const cursorOffsetY = e.clientY - bbox.top;
const xOffset = width - borderSize;
const yOffset = height - borderSize;
const halfVertexMinusBorder = (vertexSize - borderSize) / 2;
const halfVertexPlusBorder = (vertexSize + borderSize) / 2;
const minX = -halfVertexMinusBorder;
const maxX = halfVertexPlusBorder + xOffset;
const minY = minX;
const maxY = halfVertexPlusBorder + yOffset;
if (cursorOffsetX < minX || cursorOffsetX > maxX || cursorOffsetY < minY || cursorOffsetY > maxY) {
return;
}
const xSorted = [
0,
borderSize,
halfVertexPlusBorder,
-halfVertexMinusBorder + xOffset,
width - borderSize,
width
];
const ySorted = [
0,
borderSize,
halfVertexPlusBorder,
-halfVertexMinusBorder + yOffset,
height - borderSize,
height
];
let xi = 0;
let yi = 0;
for (; xi < 6 && cursorOffsetX >= xSorted[xi]; xi++) { }
for (; yi < 6 && cursorOffsetY >= ySorted[yi]; yi++) { }
this.action = ACTION_TABLE[yi][xi];
if (this.action === NULL) {
return;
}
this.startX = e.clientX;
this.startY = e.clientY;
this.currentRect = rect;
this.startRect = { x: parseInt(rectStyle.left, 10), y: parseInt(rectStyle.top, 10), width, height };
e.preventDefault();
document.addEventListener('mousemove', this.boundOnMouseMove);
document.addEventListener('mouseup', this.boundOnMouseUp);
}
onMouseMove(e) {
if (!this.currentRect || this.isAnimating) return;
this.isAnimating = true;
const dx = e.clientX - this.startX;
const dy = e.clientY - this.startY;
if (this.action === MOVE) {
this.moveRect(this.currentRect, this.startRect, dx, dy);
} else {
this.resizeRect(this.currentRect, this.startRect, dx, dy);
}
}
moveRect(rect, startRect, dx, dy) {
let {x, y} = startRect;
x += dx;
y += dy;
requestAnimationFrame(() => {
rect.update({ x, y });
this.isAnimating = false;
});
}
resizeRect(rect, startRect, dx, dy) {
const action = this.action;
const {x: startX, y: startY, width: startWidth, height: startHeight} = startRect;
const width = Math.max(startWidth + WIDTH_CHANGE_TABLE[action] * dx, this.MIN_WIDTH);
const height = Math.max(startHeight + HEIGHT_CHANGE_TABLE[action] * dy, this.MIN_HEIGHT);
const x = startX + X_CHANGE_TABLE[action] * (startWidth - width);
const y = startY + Y_CHANGE_TABLE[action] * (startHeight - height);
requestAnimationFrame(() => {
this.updateRect(rect, startRect, { x, y, width, height });
this.isAnimating = false;
});
}
updateRect(rect, startRect, newRect) {
const updates = {};
for (const key in newRect) {
if (newRect[key] !== startRect[key]) {
updates[key] = newRect[key];
}
}
if (Object.keys(updates).length > 0) {
rect.update(updates);
}
}
onMouseUp() {
document.removeEventListener('mousemove', this.boundOnMouseMove);
document.removeEventListener('mouseup', this.boundOnMouseUp);
this.currentRect = null;
this.action = NULL;
this.isAnimating = false;
}
}
})();
suggest better variable names especially startRect
Improving variable names enhances code readability and maintainability. Focusing on the startRect variable, we can choose a more descriptive name that clearly conveys its purpose. Additionally, I'll suggest improvements for other variable names to further enhance the code quality.
startRect → initialRectState
currentRect → activeRect
action → currentAction
host → containerElement
listeners → elementListenersMap
boundOnMouseMove → mouseMoveHandler
boundOnMouseUp → mouseUpHandler
startX → initialMouseX
startY → initialMouseY
isAnimating → animationInProgress
rect → rectangle
Here's the revised code incorporating the suggested variable name changes:
startRect → initialRectState
startRectinitialRectStateOther Variable Renames:
currentRect → activeRect
action → currentAction
host → containerElement
listeners → elementListenersMap
boundOnMouseMove → mouseMoveHandler
boundOnMouseUp → mouseUpHandler
startX → initialMouseX and startY → initialMouseY
isAnimating → animationInProgress
rect → rectangle
dx and dy are common, consider using full words like deltaX and deltaY for clarity.moveRect and resizeRect are appropriately named.By adopting these naming conventions and practices, the code becomes more intuitive and easier for others (or your future self) to understand and maintain.
Not yet included in a published release.
return class DndPlugin extends RectLifecycleInterface {
constructor() {
super();
this.activeRect = null;
this.currentAction = NULL;
this.containerElement = null;
this.initialMouseX = 0;
this.initialMouseY = 0;
this.initialRectState = null;
this.mouseMoveHandler = this.onMouseMove.bind(this);
this.mouseUpHandler = this.onMouseUp.bind(this);
this.elementListenersMap = new WeakMap();
this.animationInProgress = false;
this.MIN_WIDTH = 50;
this.MIN_HEIGHT = 50;
}
attach(container) {
if (container) {
this.containerElement = container;
}
return null;
}
onRectCreate(rectangle, element) {
const callback = this.onMouseDown.bind(this, rectangle);
this.elementListenersMap.set(element, callback);
element.addEventListener('mousedown', callback);
}
onRectRemove(rectangle, element) {
const callback = this.elementListenersMap.get(element);
if (callback) {
element.removeEventListener('mousedown', callback);
this.elementListenersMap.delete(element);
}
}
onMouseDown(rectangle, e) {
const rectElement = e.target;
if (!rectElement) return;
const rectStyle = rectElement.style;
const containerStyle = this.containerElement.style;
const width = parseFloat(rectStyle.getPropertyValue('--width'));
const height = parseFloat(rectStyle.getPropertyValue('--height'));
const borderSize = parseFloat(rectStyle.getPropertyValue('--border-size') || containerStyle.getPropertyValue('--border-size')) || 6;
const vertexSize = parseFloat(rectStyle.getPropertyValue('--vertex-size') || containerStyle.getPropertyValue('--vertex-size')) || 0;
const bbox = rectElement.getBoundingClientRect();
const cursorOffsetX = e.clientX - bbox.left;
const cursorOffsetY = e.clientY - bbox.top;
const xOffset = width - borderSize;
const yOffset = height - borderSize;
const halfVertexMinusBorder = (vertexSize - borderSize) / 2;
const halfVertexPlusBorder = (vertexSize + borderSize) / 2;
const minX = -halfVertexMinusBorder;
const maxX = halfVertexPlusBorder + xOffset;
const minY = minX;
const maxY = halfVertexPlusBorder + yOffset;
if (cursorOffsetX < minX || cursorOffsetX > maxX || cursorOffsetY < minY || cursorOffsetY > maxY) {
return;
}
const xSorted = [
0,
borderSize,
halfVertexPlusBorder,
-halfVertexMinusBorder + xOffset,
width - borderSize,
width
];
const ySorted = [
0,
borderSize,
halfVertexPlusBorder,
-halfVertexMinusBorder + yOffset,
height - borderSize,
height
];
let xi = 0;
let yi = 0;
for (; xi < 6 && cursorOffsetX >= xSorted[xi]; xi++) { }
for (; yi < 6 && cursorOffsetY >= ySorted[yi]; yi++) { }
this.currentAction = ACTION_TABLE[yi][xi];
if (this.currentAction === NULL) {
return;
}
this.initialMouseX = e.clientX;
this.initialMouseY = e.clientY;
this.activeRect = rectangle;
this.initialRectState = {
x: parseInt(rectStyle.left, 10),
y: parseInt(rectStyle.top, 10),
width,
height
};
e.preventDefault();
document.addEventListener('mousemove', this.mouseMoveHandler);
document.addEventListener('mouseup', this.mouseUpHandler);
}
onMouseMove(e) {
if (!this.activeRect || this.animationInProgress) return;
this.animationInProgress = true;
const deltaX = e.clientX - this.initialMouseX;
const deltaY = e.clientY - this.initialMouseY;
if (this.currentAction === MOVE) {
this.moveRect(this.activeRect, this.initialRectState, deltaX, deltaY);
} else {
this.resizeRect(this.activeRect, this.initialRectState, deltaX, deltaY);
}
}
moveRect(rectangle, initialRect, deltaX, deltaY) {
let {x, y} = initialRect;
x += deltaX;
y += deltaY;
requestAnimationFrame(() => {
rectangle.update({ x, y });
this.animationInProgress = false;
});
}
resizeRect(rectangle, initialRect, deltaX, deltaY) {
const action = this.currentAction;
const {x: startX, y: startY, width: startWidth, height: startHeight} = initialRect;
const width = Math.max(startWidth + WIDTH_CHANGE_TABLE[action] * deltaX, this.MIN_WIDTH);
const height = Math.max(startHeight + HEIGHT_CHANGE_TABLE[action] * deltaY, this.MIN_HEIGHT);
const x = startX + X_CHANGE_TABLE[action] * (startWidth - width);
const y = startY + Y_CHANGE_TABLE[action] * (startHeight - height);
requestAnimationFrame(() => {
this.updateRect(rectangle, initialRect, { x, y, width, height });
this.animationInProgress = false;
});
}
updateRect(rectangle, initialRect, newRect) {
const updates = {};
for (const key in newRect) {
if (newRect[key] !== initialRect[key]) {
updates[key] = newRect[key];
}
}
if (Object.keys(updates).length > 0) {
rectangle.update(updates);
}
}
onMouseUp() {
document.removeEventListener('mousemove', this.mouseMoveHandler);
document.removeEventListener('mouseup', this.mouseUpHandler);
this.activeRect = null;
this.currentAction = NULL;
this.animationInProgress = false;
}
}
})();