Skip to content

Commit

Permalink
Allow to drop elements into the page over the layout (#271)
Browse files Browse the repository at this point in the history
* Allow to drop elements into the page over the layout

* auto format code

* Fix bad dragging

* Add one more fix

* Fix typo

---------

Co-authored-by: cibernox <[email protected]>
  • Loading branch information
cibernox and cibernox authored Oct 10, 2024
1 parent 424db32 commit 1fb031d
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 6 deletions.
6 changes: 5 additions & 1 deletion assets/svelte/components/PageAstNode.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -177,11 +177,15 @@
{#each children as child, childIndex}
<svelte:self node={child} nodeId="{nodeId}.{childIndex}" />
{/each}
<!-- Using the component definition's example is actually visually confusing. Disabled for now -->
{#if isDragTarget && $draggedComponentDefinition}
<div class="dragged-element-placeholder">Preview</div>
{/if}
<!-- {#if isDragTarget && $draggedComponentDefinition}
<div class="dragged-element-placeholder">{@html $draggedComponentDefinition.example}</div>
{:else if previewDropInside}
<div class="dragged-element-placeholder">Preview</div>
{/if}
{/if} -->
{/if}
</svelte:element>
{/if}
Expand Down
18 changes: 16 additions & 2 deletions assets/svelte/components/PagePreview.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,25 @@
let isDraggingOver = false
async function handleDragDrop(e: DragEvent) {
let { target } = e
let {
target,
dataTransfer: { layoutZone },
} = e
$currentComponentCategory = null
if (!$draggedComponentDefinition) return
let draggedObj = $draggedComponentDefinition
if (target.id !== "fake-browser-content" && elementCanBeDroppedInTarget(draggedObj)) {
if (layoutZone) {
$live.pushEvent(
"render_component_in_page",
{ component_id: draggedObj.id, page_id: $page.id },
({ ast }: { ast: AstNode[] }) => {
// If the element was dropped before the main content, it appends it at the top of the page
// otherwise it appends it at the bottom of the page
const newAst = layoutZone === "preamble" ? [...ast, ...$page.ast] : [...$page.ast, ...ast]
$live.pushEvent("update_page_ast", { id: $page.id, ast: newAst })
},
)
} else if (target.id !== "fake-browser-content" && elementCanBeDroppedInTarget(draggedObj)) {
if (!(target instanceof HTMLElement) || !$slotTargetElement || $slotTargetElement.attrs.selfClose) {
resetDragDrop()
return
Expand Down
18 changes: 16 additions & 2 deletions assets/svelte/components/PageWrapper.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
let wrapper: HTMLElement
let styleWrapper: HTMLElement
let contentWrapper: HTMLElement
let twConfig = $tailwindConfig
let configPromise = import(twConfig)
onMount(async () => {
Expand Down Expand Up @@ -39,14 +40,27 @@
event.preventDefault()
}
}
// Annotates the drop event here to know wether it was fired before the main content
// or after it.
function handleDragDrop(e: DragEvent) {
const target = e.target as Node
if (!contentWrapper.contains(target)) {
if (target.compareDocumentPosition(contentWrapper) & Node.DOCUMENT_POSITION_PRECEDING) {
e.dataTransfer.layoutZone = "epilogue"
} else if (target.compareDocumentPosition(contentWrapper) & Node.DOCUMENT_POSITION_FOLLOWING) {
e.dataTransfer.layoutZone = "preamble"
}
}
}
</script>

<span bind:this={styleWrapper}></span>
<div bind:this={wrapper} on:click={preventLinkNavigation}>
<div bind:this={wrapper} on:click={preventLinkNavigation} on:drop={handleDragDrop}>
{#each $page.layout.ast as layoutAstNode}
<LayoutAstNode node={layoutAstNode}>
<!-- This seemingly useless wrapper is here just so we are sure that the layout and the page don't share the same parent, which screws the position calculations -->
<div class="contents">
<div class="contents" bind:this={contentWrapper}>
{#each $page.ast as astNode, index}
<PageAstNode node={astNode} nodeId={String(index)} />
{/each}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,9 @@
}
if (node === targetElement) {
endIndex = i
if (startIndex < 0) {
startIndex = i
}
break
}
startIndex = -1
Expand Down
Loading

0 comments on commit 1fb031d

Please sign in to comment.