Skip to content

Commit

Permalink
Fix drag position (#410)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kitenite authored Sep 26, 2024
1 parent 790ce9e commit 01e717c
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 7 deletions.
1 change: 1 addition & 0 deletions app/common/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export enum EditorAttributes {
DATA_ONLOOK_ORIGINAL_INDEX = 'data-onlook-original-index',
DATA_ONLOOK_DRAGGING = 'data-onlook-dragging',
DATA_ONLOOK_DRAG_DIRECTION = 'data-onlook-drag-direction',
DATA_ONLOOK_DRAG_START_POSITION = 'data-onlook-drag-start-position',
DATA_ONLOOK_NEW_INDEX = 'data-onlook-new-index',
DATA_ONLOOK_UNIQUE_ID = 'data-onlook-unique-id',
DATA_ONLOOK_EDITING_TEXT = 'data-onlook-editing-text',
Expand Down
22 changes: 21 additions & 1 deletion app/electron/preload/webview/elements/move/drag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ export function startDrag(selector: string): number {
const originalIndex = htmlChildren.indexOf(el);
prepareElementForDragging(el, originalIndex);
createStub(el);
const pos = getAbsolutePosition(el);
el.setAttribute(EditorAttributes.DATA_ONLOOK_DRAG_START_POSITION, JSON.stringify(pos));
return originalIndex;
}

Expand All @@ -33,7 +35,14 @@ export function drag(dx: number, dy: number, x: number, y: number) {
el.style.width = styles.width;
el.style.height = styles.height;
el.style.position = 'fixed';
el.style.transform = `translate(${dx}px, ${dy}px)`;

const pos = JSON.parse(
el.getAttribute(EditorAttributes.DATA_ONLOOK_DRAG_START_POSITION) || '{}',
);
const left = pos.left + dx;
const top = pos.top + dy;
el.style.left = `${left}px`;
el.style.top = `${top}px`;
moveStub(el, x, y);
}

Expand Down Expand Up @@ -81,6 +90,8 @@ function prepareElementForDragging(el: HTMLElement, originalIndex: number) {
transform: el.style.transform,
width: el.style.width,
height: el.style.height,
left: el.style.left,
top: el.style.top,
};

el.setAttribute(EditorAttributes.DATA_ONLOOK_SAVED_STYLE, JSON.stringify(style));
Expand Down Expand Up @@ -121,6 +132,7 @@ function removeDragAttributes(el: HTMLElement) {
el.removeAttribute(EditorAttributes.DATA_ONLOOK_SAVED_STYLE);
el.removeAttribute(EditorAttributes.DATA_ONLOOK_DRAGGING);
el.removeAttribute(EditorAttributes.DATA_ONLOOK_DRAG_DIRECTION);
el.removeAttribute(EditorAttributes.DATA_ONLOOK_DRAG_START_POSITION);
}

function saveElementIndex(el: HTMLElement, newIndex: number) {
Expand All @@ -135,3 +147,11 @@ function saveElementIndex(el: HTMLElement, newIndex: number) {
el.removeAttribute(EditorAttributes.DATA_ONLOOK_NEW_INDEX);
}
}

function getAbsolutePosition(element: HTMLElement) {
const rect = element.getBoundingClientRect();
return {
left: rect.left + window.scrollX,
top: rect.top + window.scrollY,
};
}
12 changes: 6 additions & 6 deletions demos/remix/app/routes/_index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,30 +23,30 @@ export default function Index() {
<a
className="text-blue-700 underline visited:text-purple-900"
target="_blank"
href="https://remix.run/start/quickstart"
href="https://remix.run/docs"
rel="noreferrer"
>
5m Quick Start
Remix Docs
</a>
</li>
<li>
<a
className="text-blue-700 underline visited:text-purple-900"
target="_blank"
href="https://remix.run/start/tutorial"
href="https://remix.run/start/quickstart"
rel="noreferrer"
>
30m Tutorial
5m Quick Start
</a>
</li>
<li>
<a
className="text-blue-700 underline visited:text-purple-900"
target="_blank"
href="https://remix.run/docs"
href="https://remix.run/start/tutorial"
rel="noreferrer"
>
Remix Docs
30m Tutorial
</a>
</li>
</ul>
Expand Down

0 comments on commit 01e717c

Please sign in to comment.