-
Notifications
You must be signed in to change notification settings - Fork 1.3k
[DRAFT] feat: allow move child element out side parent #1371
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,7 @@ export function startDrag(domId: string): number | null { | |
} | ||
const htmlChildren = Array.from(parent.children).filter(isValidHtmlElement); | ||
const originalIndex = htmlChildren.indexOf(el); | ||
|
||
prepareElementForDragging(el); | ||
createStub(el); | ||
const pos = getAbsolutePosition(el); | ||
|
@@ -44,14 +45,57 @@ export function drag(domId: string, dx: number, dy: number, x: number, y: number | |
el.style.width = styles.width + 1; | ||
el.style.height = styles.height + 1; | ||
el.style.position = 'fixed'; | ||
el.style.zIndex = '9999'; | ||
|
||
const targetContainer = findTargetContainerAtPoint(x, y, el); | ||
|
||
if (targetContainer) { | ||
moveStub(el, x, y, targetContainer); | ||
} else { | ||
removeStub(); | ||
} | ||
} | ||
|
||
function findTargetContainerAtPoint( | ||
x: number, | ||
y: number, | ||
draggedElement: HTMLElement, | ||
): HTMLElement | null { | ||
draggedElement.style.display = 'none'; | ||
|
||
try { | ||
let element = document.elementFromPoint(x, y) as HTMLElement | null; | ||
while (element) { | ||
const styles = window.getComputedStyle(element); | ||
if ( | ||
(styles.display === 'flex' || | ||
styles.display === 'grid' || | ||
styles.display === 'block') && | ||
element !== draggedElement && | ||
!element.hasAttribute(EditorAttributes.DATA_ONLOOK_DRAGGING) && | ||
!draggedElement.contains(element) && | ||
!isStubElement(element) | ||
) { | ||
return element; | ||
} | ||
element = element.parentElement; | ||
} | ||
} finally { | ||
draggedElement.style.display = ''; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is distructive. The element loses its original display value. |
||
} | ||
|
||
return null; | ||
} | ||
|
||
moveStub(el, x, y); | ||
function isStubElement(element: HTMLElement): boolean { | ||
return element.id === EditorAttributes.ONLOOK_STUB_ID; | ||
} | ||
|
||
export function endDrag(domId: string): { | ||
newIndex: number; | ||
child: DomElement; | ||
parent: DomElement; | ||
oldParent?: DomElement; | ||
} | null { | ||
const el = elementFromDomId(domId); | ||
if (!el) { | ||
|
@@ -60,29 +104,43 @@ export function endDrag(domId: string): { | |
return null; | ||
} | ||
|
||
const parent = el.parentElement; | ||
if (!parent) { | ||
const originalParent = el.parentElement; | ||
const stub = document.getElementById(EditorAttributes.ONLOOK_STUB_ID); | ||
const stubParent = stub?.parentElement; | ||
|
||
if (!stubParent || !originalParent) { | ||
console.warn('End drag parent not found'); | ||
cleanUpElementAfterDragging(el); | ||
removeStub(); | ||
return null; | ||
} | ||
|
||
const stubIndex = getCurrentStubIndex(parent, el); | ||
const stubIndex = getCurrentStubIndex(stubParent, el); | ||
|
||
cleanUpElementAfterDragging(el); | ||
removeStub(); | ||
|
||
if (stubIndex === -1) { | ||
return null; | ||
} | ||
|
||
const elementIndex = Array.from(parent.children).indexOf(el); | ||
const elementIndex = Array.from(originalParent.children).indexOf(el); | ||
if (stubIndex === elementIndex) { | ||
return null; | ||
} | ||
|
||
if (stubParent !== originalParent) { | ||
return { | ||
newIndex: stubIndex, | ||
child: getDomElement(el, false), | ||
parent: getDomElement(stubParent, false), | ||
oldParent: getDomElement(originalParent, false), | ||
}; | ||
} | ||
|
||
return { | ||
newIndex: stubIndex, | ||
child: getDomElement(el, false), | ||
parent: getDomElement(parent, false), | ||
parent: getDomElement(stubParent, false), | ||
}; | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ import type { ProjectsManager } from '@/lib/projects'; | |
import { invokeMainChannel, sendAnalytics, sendToWebview } from '@/lib/utils'; | ||
import type { | ||
Action, | ||
CodeInsert, | ||
CodeInsertImage, | ||
CodeRemoveImage, | ||
EditTextAction, | ||
|
@@ -211,16 +212,51 @@ export class CodeManager { | |
continue; | ||
} | ||
|
||
const movedEl: CodeMove = { | ||
oid: target.oid, | ||
type: CodeActionType.MOVE, | ||
location, | ||
}; | ||
|
||
const request = await getOrCreateCodeDiffRequest(location.targetOid, oidToCodeChange); | ||
request.structureChanges.push(movedEl); | ||
if (!location.oldParentOid) { | ||
const movedEl: CodeMove = { | ||
oid: target.oid, | ||
type: CodeActionType.MOVE, | ||
location, | ||
}; | ||
|
||
const request = await getOrCreateCodeDiffRequest( | ||
location.targetOid, | ||
oidToCodeChange, | ||
); | ||
request.structureChanges.push(movedEl); | ||
} else { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Kitenite Im facing a problem here, please take a look There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you explicitly state the problem? I don't have enough context to understand. |
||
// TODO: Handle moving from one parent to another | ||
// 1. Remove from old parent | ||
const removeRequest = await getOrCreateCodeDiffRequest( | ||
location.oldParentOid, | ||
oidToCodeChange, | ||
); | ||
removeRequest.structureChanges.push({ | ||
oid: target.oid, | ||
type: CodeActionType.REMOVE, | ||
}); | ||
|
||
// 2. Add to new parent | ||
// This code still wrong since I can not get full element | ||
|
||
const addRequest = await getOrCreateCodeDiffRequest( | ||
location.targetOid, | ||
oidToCodeChange, | ||
); | ||
addRequest.structureChanges.push({ | ||
oid: target.oid, | ||
type: CodeActionType.INSERT, | ||
location, | ||
children: [], // This is wrong | ||
tagName: '', // This is wrong | ||
attributes: {}, // This is wrong | ||
textContent: '', | ||
pasteParams: null, | ||
}); | ||
} | ||
} | ||
|
||
console.log('oidToCodeChange', JSON.stringify(Array.from(oidToCodeChange.values()))); | ||
await this.getAndWriteCodeDiff(Array.from(oidToCodeChange.values())); | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should early return for certain conditions.