mirror of
https://github.com/codex-team/editor.js
synced 2024-06-26 09:20:07 +02:00
84 lines
2.1 KiB
TypeScript
84 lines
2.1 KiB
TypeScript
import SelectionUtils from '../selection';
|
|||
|
|||
import Module from '../__module';
|
|||
/**
|
|||
*
|
|||
*/
|
|||
export default class DragNDrop extends Module {
|
|||
/**
|
|||
* If drag has been started at editor, we save it
|
|||
*
|
|||
* @type {boolean}
|
|||
* @private
|
|||
*/
|
|||
private isStartedAtEditor = false;
|
|||
|
|||
/**
|
|||
Revert "Release: 2.19 (#1341)" (#1363)
This reverts commit 78775703c967dee84d75689135333b5d89db392b.
|
* Bind events
|
||
*/
|
|||
public prepare(): void {
|
|||
Revert "Release: 2.19 (#1341)" (#1363)
This reverts commit 78775703c967dee84d75689135333b5d89db392b.
|
this.bindEvents();
|
||
}
|
|||
|
|||
/**
|
|||
* Add drag events listeners to editor zone
|
|||
Revert "Release: 2.19 (#1341)" (#1363)
This reverts commit 78775703c967dee84d75689135333b5d89db392b.
|
*
|
||
* @private
|
|||
*/
|
|||
Revert "Release: 2.19 (#1341)" (#1363)
This reverts commit 78775703c967dee84d75689135333b5d89db392b.
|
private bindEvents(): void {
|
||
this.Editor.Listeners.on(this.Editor.UI.nodes.holder, 'drop', this.processDrop, true);
|
|||
|
|||
Revert "Release: 2.19 (#1341)" (#1363)
This reverts commit 78775703c967dee84d75689135333b5d89db392b.
|
this.Editor.Listeners.on(this.Editor.UI.nodes.holder, 'dragstart', (dragEvent: DragEvent) => {
|
||
if (SelectionUtils.isAtEditor && !SelectionUtils.isCollapsed) {
|
|||
this.isStartedAtEditor = true;
|
|||
}
|
|||
|
|||
Revert "Release: 2.19 (#1341)" (#1363)
This reverts commit 78775703c967dee84d75689135333b5d89db392b.
|
this.Editor.InlineToolbar.close();
|
||
});
|
|||
|
|||
Revert "Release: 2.19 (#1341)" (#1363)
This reverts commit 78775703c967dee84d75689135333b5d89db392b.
|
/* Prevent default browser behavior to allow drop on non-contenteditable elements */
|
||
this.Editor.Listeners.on(this.Editor.UI.nodes.holder, 'dragover', (e) => e.preventDefault(), true);
|
|||
}
|
|||
|
|||
/**
|
|||
* Handle drop event
|
|||
*
|
|||
* @param {DragEvent} dropEvent - drop event
|
|||
*/
|
|||
Revert "Release: 2.19 (#1341)" (#1363)
This reverts commit 78775703c967dee84d75689135333b5d89db392b.
|
private processDrop = async (dropEvent: DragEvent): Promise<void> => {
|
||
const {
|
|||
BlockManager,
|
|||
|
Caret,
|
||
Paste,
|
|||
} = this.Editor;
|
|||
|
|||
dropEvent.preventDefault();
|
|||
|
|||
BlockManager.blocks.forEach((block) => {
|
|||
block.dropTarget = false;
|
|||
});
|
|||
|
|||
if (SelectionUtils.isAtEditor && !SelectionUtils.isCollapsed && this.isStartedAtEditor) {
|
|||
document.execCommand('delete');
|
|||
}
|
|||
|
|||
this.isStartedAtEditor = false;
|
|||
|
|||
/**
|
|||
* Try to set current block by drop target.
|
|||
* If drop target (error will be thrown) is not part of the Block, set last Block as current.
|
|||
*/
|
|||
try {
|
|||
|
const targetBlock = BlockManager.setCurrentBlockByChildNode(dropEvent.target as Node);
|
||
|
|||
this.Editor.Caret.setToBlock(targetBlock, Caret.positions.END);
|
|||
} catch (e) {
|
|||
|
const targetBlock = BlockManager.setCurrentBlockByChildNode(BlockManager.lastBlock.holder);
|
||
|
|||
this.Editor.Caret.setToBlock(targetBlock, Caret.positions.END);
|
|||
}
|
|||
|
|||
Revert "Release: 2.19 (#1341)" (#1363)
This reverts commit 78775703c967dee84d75689135333b5d89db392b.
|
Paste.processDataTransfer(dropEvent.dataTransfer, true);
|
||
}
|
|||
}
|