This commit is contained in:
Juergen Kunz
2025-06-29 14:00:55 +00:00
parent 5066681b3a
commit 49ad998b2c
9 changed files with 949 additions and 21 deletions

View File

@ -11,6 +11,8 @@ export class WysiwygDragDropHandler {
private initialBlockY: number = 0;
private draggedBlockElement: HTMLElement | null = null;
private draggedBlockHeight: number = 0;
private draggedBlockContentHeight: number = 0;
private draggedBlockMarginTop: number = 0;
private lastUpdateTime: number = 0;
private updateThrottle: number = 80; // milliseconds
@ -48,11 +50,33 @@ export class WysiwygDragDropHandler {
this.initialMouseY = e.clientY;
this.draggedBlockElement = this.component.editorContentRef.querySelector(`[data-block-id="${block.id}"]`);
if (this.draggedBlockElement) {
// Get the wrapper rect for measurements
const rect = this.draggedBlockElement.getBoundingClientRect();
this.draggedBlockHeight = rect.height;
this.initialBlockY = rect.top;
// Get the inner block element for proper measurements
const innerBlock = this.draggedBlockElement.querySelector('.block');
if (innerBlock) {
const innerRect = innerBlock.getBoundingClientRect();
const computedStyle = window.getComputedStyle(innerBlock);
this.draggedBlockMarginTop = parseInt(computedStyle.marginTop) || 0;
this.draggedBlockContentHeight = innerRect.height;
}
// The drop indicator should match the wrapper height exactly
// The wrapper already includes all the space the block occupies
this.draggedBlockHeight = rect.height;
console.log('Drag measurements:', {
wrapperHeight: rect.height,
marginTop: this.draggedBlockMarginTop,
dropIndicatorHeight: this.draggedBlockHeight,
contentHeight: this.draggedBlockContentHeight,
blockId: block.id
});
// Create drop indicator
this.createDropIndicator();
@ -98,6 +122,8 @@ export class WysiwygDragDropHandler {
this.dragOverPosition = null;
this.draggedBlockElement = null;
this.draggedBlockHeight = 0;
this.draggedBlockContentHeight = 0;
this.draggedBlockMarginTop = 0;
this.initialBlockY = 0;
// Update component state
@ -284,34 +310,93 @@ export class WysiwygDragDropHandler {
if (!this.dropIndicator || !this.draggedBlockElement) return;
this.dropIndicator.style.display = 'block';
this.dropIndicator.style.height = `${this.draggedBlockHeight}px`;
const containerRect = this.component.editorContentRef.getBoundingClientRect();
// Calculate where the block will actually land
let topPosition = 0;
if (targetIndex === 0) {
// Before first block
topPosition = 0;
} else {
// After a specific block
const prevIndex = targetIndex - 1;
let blockCount = 0;
// Build array of visual block positions (excluding dragged block)
const visualBlocks: { index: number, top: number, bottom: number }[] = [];
for (let i = 0; i < blocks.length; i++) {
if (i === draggedIndex) continue; // Skip the dragged block
// Find the visual position of the block that will be before our dropped block
for (let i = 0; i < blocks.length; i++) {
if (i === draggedIndex) continue; // Skip the dragged block
if (blockCount === prevIndex) {
const rect = blocks[i].getBoundingClientRect();
topPosition = rect.bottom - containerRect.top + 16; // 16px gap
break;
const block = blocks[i];
const rect = block.getBoundingClientRect();
let top = rect.top - containerRect.top;
let bottom = rect.bottom - containerRect.top;
// Account for any transforms
const transform = window.getComputedStyle(block).transform;
if (transform && transform !== 'none') {
const matrix = new DOMMatrix(transform);
const yOffset = matrix.m42;
top += yOffset;
bottom += yOffset;
}
visualBlocks.push({ index: i, top, bottom });
}
// Sort by visual position
visualBlocks.sort((a, b) => a.top - b.top);
// Adjust targetIndex to account for excluded dragged block
let adjustedTargetIndex = targetIndex;
if (targetIndex > draggedIndex) {
adjustedTargetIndex--; // Reduce by 1 since dragged block is not in visualBlocks
}
// Calculate drop position
// Get the margin that will be applied based on the dragged block type
let blockMargin = 16; // default margin
if (this.draggedBlockElement) {
const draggedBlock = this.component.blocks.find(b => b.id === this.draggedBlockId);
if (draggedBlock) {
const blockType = draggedBlock.type;
if (blockType === 'heading-1' || blockType === 'heading-2' || blockType === 'heading-3') {
blockMargin = 24;
} else if (blockType === 'code' || blockType === 'quote') {
blockMargin = 20;
}
blockCount++;
}
}
this.dropIndicator.style.top = `${topPosition}px`;
if (adjustedTargetIndex === 0) {
// Insert at the very top - no margin needed for first block
topPosition = 0;
} else if (adjustedTargetIndex >= visualBlocks.length) {
// Insert at the end
const lastBlock = visualBlocks[visualBlocks.length - 1];
if (lastBlock) {
topPosition = lastBlock.bottom;
// Add margin that will be applied to the dropped block
topPosition += blockMargin;
}
} else {
// Insert between blocks
const blockBefore = visualBlocks[adjustedTargetIndex - 1];
if (blockBefore) {
topPosition = blockBefore.bottom;
// Add margin that will be applied to the dropped block
topPosition += blockMargin;
}
}
// Set the indicator height to match the dragged block
this.dropIndicator.style.height = `${this.draggedBlockHeight}px`;
// Set position
this.dropIndicator.style.top = `${Math.max(0, topPosition)}px`;
console.log('Drop indicator update:', {
targetIndex,
adjustedTargetIndex,
draggedIndex,
topPosition,
height: this.draggedBlockHeight,
blockMargin,
visualBlocks: visualBlocks.map(b => ({ index: b.index, top: b.top, bottom: b.bottom }))
});
}
/**