Skip to content

Commit c423ede

Browse files
Changes reposition target in real time
1 parent 55f1122 commit c423ede

File tree

2 files changed

+20
-16
lines changed

2 files changed

+20
-16
lines changed

‎packages/atri-app-core/src/api/canvasApi.ts‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,11 @@ subscribeCanvasMachine("upWhileDrag", (context) => {
8282
});
8383

8484
subscribeCanvasMachine("reposition", (context) => {
85-
const canvasZone = (context.mousePosition!.target as HTMLElement).closest(
85+
console.log("Reposition atri-app-core: ", context.repositionTarget);
86+
const canvasZone = (context.repositionTarget!.target as HTMLElement).closest(
8687
"[data-atri-canvas-id]"
8788
);
88-
const parentEl = (context.mousePosition!.target as HTMLElement).closest(
89+
const parentEl = (context.repositionTarget!.target as HTMLElement).closest(
8990
"[data-atri-parent]"
9091
);
9192
if (canvasZone) {
@@ -102,11 +103,11 @@ subscribeCanvasMachine("reposition", (context) => {
102103
id === CANVAS_ZONE_ROOT_ID
103104
? getComponentIndexInsideCanvasZone(
104105
canvasZone.getAttribute("data-atri-canvas-id")!,
105-
context.mousePosition!
106+
context.repositionTarget!
106107
)
107108
: getComponentIndexInsideParentComponent(
108109
id,
109-
context.mousePosition!
110+
context.repositionTarget!
110111
),
111112
canvasZoneId: canvasZone.getAttribute("data-atri-canvas-id"),
112113
},

‎packages/atri-app-core/src/canvasMachine.ts‎

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,11 @@ type CanvasMachineContext = {
128128
hovered: string | null;
129129
selected: string | null;
130130
lastDropped: string | null; // string until COMPONENT_RENDERED received, otherwise null
131+
repositionTarget: {
132+
pageX: number;
133+
pageY: number;
134+
target: MouseEvent["target"];
135+
} | null;
131136
};
132137

133138
// actions
@@ -179,16 +184,7 @@ function changeComponentLoc(
179184
context: CanvasMachineContext,
180185
event: MOUSE_MOVE_EVENT
181186
) {
182-
const { target } = event.event;
183-
if (target !== null && "closest" in target) {
184-
const parentComp = (target as HTMLElement).closest("[data-atri-parent]");
185-
const canvasZone = (target as HTMLElement).closest("[data-atri-canvas-id]");
186-
if (parentComp !== null) {
187-
const parentCompId = parentComp.getAttribute("data-atri-comp-id");
188-
const canvasZoneId = canvasZone?.getAttribute("data-atri-canvas-id");
189-
context.mousePosition = event.event;
190-
}
191-
}
187+
context.repositionTarget = event.event;
192188
}
193189

194190
function setRepositionDataToNull(context: CanvasMachineContext) {
@@ -407,6 +403,7 @@ export function createCanvasMachine(id: string) {
407403
hovered: null,
408404
selected: null,
409405
lastDropped: null,
406+
repositionTarget: null,
410407
},
411408
states: {
412409
[initial]: {
@@ -509,7 +506,8 @@ export function createCanvasMachine(id: string) {
509506
},
510507
states: {
511508
[repositionIdle]: {
512-
entry: () => {
509+
entry: (context) => {
510+
context.repositionTarget = null;
513511
console.log("Entered Reposition Idle State");
514512
},
515513
exit: () => {
@@ -519,7 +517,7 @@ export function createCanvasMachine(id: string) {
519517
[MOUSE_MOVE]: {
520518
target: repositionActive,
521519
cond: isNotInTheSameParent,
522-
actions: ["emitRepositionStarted"],
520+
actions: ["emitRepositionStarted", "changeComponentLoc"],
523521
},
524522
},
525523
},
@@ -536,6 +534,11 @@ export function createCanvasMachine(id: string) {
536534
cond: isInTheSameParent,
537535
actions: ["emitRepositionEnded"],
538536
},
537+
[MOUSE_MOVE]: {
538+
target: repositionActive,
539+
cond: isNotInTheSameParent,
540+
actions: ["emitRepositionStarted", "changeComponentLoc"],
541+
},
539542
},
540543
},
541544
},

0 commit comments

Comments
 (0)