the drop block at start added

This commit is contained in:
robonetphy 2023-08-28 18:52:54 +05:30
parent 9f81269ad0
commit 4f95a4e145

View file

@ -1,7 +1,7 @@
import SelectionUtils from '../selection'; import SelectionUtils from '../selection';
import Block, { BlockDropZonePosition } from '../block'; import Block, { BlockDropZonePosition } from '../block';
import * as _ from '../utils'; import * as _ from '../utils';
import Dom from '../dom'; import $ from '../dom';
import Module from '../__module'; import Module from '../__module';
/** /**
* *
@ -113,24 +113,30 @@ export default class DragNDrop extends Module {
* Try to set current block by drop target. * Try to set current block by drop target.
* If drop target is not part of the Block, set last Block as current. * If drop target is not part of the Block, set last Block as current.
*/ */
let targetBlock = BlockManager.getBlockByChildNode(dropEvent.target as Node); const firstLevelBlock = (dropEvent.target as HTMLElement).closest(`.${Block.CSS.wrapper}`);
let targetBlock = BlockManager.blocks.find((block) => block.holder === firstLevelBlock);
let shouldMoveToFirst = false;
if (targetBlock) { if (targetBlock) {
if (targetBlock.dropZonePosition === BlockDropZonePosition.Top) { if (targetBlock.dropZonePosition === BlockDropZonePosition.Top) {
const currentIndex = BlockManager.getBlockIndex(targetBlock); const currentIndex = BlockManager.getBlockIndex(targetBlock);
let targetIndex;
if (currentIndex > 0) { if (currentIndex > 0) {
targetBlock = BlockManager.getBlockByIndex(currentIndex - 1); targetIndex = currentIndex - 1;
Caret.setToBlock(targetBlock, Caret.positions.END);
} }
else { else {
// Paste the block at the end of first block.
targetIndex = 0;
// then swap the first block with second block.
shouldMoveToFirst = true;
} }
} else { targetBlock = BlockManager.getBlockByIndex(targetIndex);
Caret.setToBlock(targetBlock, Caret.positions.END);
} }
Caret.setToBlock(targetBlock, Caret.positions.END);
} else { } else {
const lastBlock = BlockManager.getBlockByChildNode(BlockManager.lastBlock.holder); const firstLevelBlock = (BlockManager.lastBlock.holder as HTMLElement).closest(`.${Block.CSS.wrapper}`);
let lastBlock = BlockManager.blocks.find((block) => block.holder === firstLevelBlock);
Caret.setToBlock(lastBlock, Caret.positions.END); Caret.setToBlock(lastBlock, Caret.positions.END);
} }
@ -141,6 +147,11 @@ export default class DragNDrop extends Module {
BlockSelection.clearSelection(); BlockSelection.clearSelection();
await Paste.processDataTransfer(dropEvent.dataTransfer, true); await Paste.processDataTransfer(dropEvent.dataTransfer, true);
// swapping of the first block with second block.
if (shouldMoveToFirst) {
BlockManager.move(1, 0);
}
} }
/** /**
@ -157,7 +168,9 @@ export default class DragNDrop extends Module {
this.removeDragImage(); this.removeDragImage();
const selectedBlocks = BlockSelection.selectedBlocks; const selectedBlocks = BlockSelection.selectedBlocks;
const targetBlock = BlockManager.getBlockByChildNode(dropEvent.target as Node);
const firstLevelBlock = (dropEvent.target as HTMLElement).closest(`.${Block.CSS.wrapper}`);
const targetBlock = BlockManager.blocks.find((block) => block.holder === firstLevelBlock);
if (!targetBlock) { if (!targetBlock) {
// This means that we are trying to drop a block without references. // This means that we are trying to drop a block without references.
@ -232,7 +245,7 @@ export default class DragNDrop extends Module {
/** /**
* Create a drag image with all blocks content. * Create a drag image with all blocks content.
*/ */
const dragImage: HTMLElement = Dom.make('div'); const dragImage: HTMLElement = $.make('div');
dragImage.id = `drag-image-${_.generateId()}`; dragImage.id = `drag-image-${_.generateId()}`;
dragImage.style.position = 'absolute'; dragImage.style.position = 'absolute';