feat: introduce CardPhysics class to encapsulate card drag and physics state from LayoutCard.

This commit is contained in:
2025-11-27 20:37:20 +01:00
parent 9bf9550b03
commit e164a88a97
5 changed files with 110 additions and 217 deletions
+7 -29
View File
@@ -1,5 +1,7 @@
import { constrainDimensions, getInitialPosition, CONTAINER_PADDING } from './utils/layoutUtils';
import { fetchLayoutRecords, upsertLayoutRecords } from './db';
import { CardPhysics } from './CardPhysics';
export interface LayoutCardState {
id: string;
@@ -26,18 +28,16 @@ export class LayoutCard implements LayoutCardState {
private _centerX: number = 0;
private _centerY: number = 0;
public store: LayoutStore;
public dragStartX: number = 0;
public dragStartY: number = 0;
public isDirty: boolean = false;
public physics: CardPhysics;
public intendedX: number = 0;
public intendedY: number = 0;
private dragOffset: { x: number, y: number } | null = null;
public dragPointerId: number | null = null;
public isDirty: boolean = false;
constructor(id: string, store: LayoutStore, initialData: Partial<LayoutCardState> = {}, ref: HTMLElement | null = null) {
this.id = id;
this.store = store;
Object.assign(this, initialData);
this.physics = new CardPhysics(this);
this.intendedX = this.x;
this.intendedY = this.y;
this.ref = ref;
@@ -50,12 +50,7 @@ export class LayoutCard implements LayoutCardState {
this.applyTransform();
}
beginDrag(offset: { x: number, y: number }, pointerId: number) {
this.dragOffset = offset;
this.dragStartX = this.x;
this.dragStartY = this.y;
this.dragPointerId = pointerId;
}
getConstrainedPosition(x: number, y: number): { x: number, y: number } {
const rad = (this.rotation * Math.PI) / 180;
@@ -77,24 +72,7 @@ export class LayoutCard implements LayoutCardState {
return { x: newX, y: newY };
}
continueDrag(delta: { x: number, y: number }) {
if (!this.dragOffset) return;
const targetX = this.x + delta.x;
const targetY = this.y + delta.y;
const { x: newX, y: newY } = this.getConstrainedPosition(targetX, targetY);
this.update({
x: newX,
y: newY
}, { markDirty: true });
}
finishDrag() {
this.dragOffset = null;
this.dragPointerId = null;
}
update(changes: Partial<LayoutCardState>, options: { markDirty?: boolean, isConstraintUpdate?: boolean } = {}) {
Object.assign(this, changes);
@@ -283,7 +261,7 @@ export class LayoutCard implements LayoutCardState {
}
get isDragging(): boolean {
return !!this.dragOffset;
return this.physics.isDragging;
}
}