* Refactored drag n drop functionality to a hook and component. * Add the snackbar to the article editor. * Now you can drag and drop images in the post body content. * Cleaning up a few things. * Now post cover images can be dragged and dropped. * Removed unused empty function. * Added @testing-library/preact-hooks so that we can test hooks. * Some renaming, small refactor. * Added tests * Added more tests. * Added more tests. * Added message about dropping only one image in post body. * Added message about dropping only one image for cover image. * put onDrop into it's own meethod. * More tests. * Added some API documentation. * Updated editor help with drag and drop image help. * drag and drop styling * . * revert * Fixed broken test. Co-authored-by: Nick Taylor <nick@dev.to>
79 lines
1.8 KiB
JavaScript
79 lines
1.8 KiB
JavaScript
import { addSnackbarItem } from '../../Snackbar';
|
|
import { processImageUpload } from '../actions';
|
|
|
|
/**
|
|
* Determines if at least one type of drag and drop datum type matches the data transfer type to match.
|
|
*
|
|
* @param {string[]} types An array of data transfer types.
|
|
* @param {string} dataTransferType The data transfer type to match.
|
|
*/
|
|
export function matchesDataTransferType(
|
|
types = [],
|
|
dataTransferType = 'Files',
|
|
) {
|
|
return types.some((type) => type === dataTransferType);
|
|
}
|
|
|
|
// TODO: Document functions
|
|
export function handleImageDrop(handleImageSuccess, handleImageFailure) {
|
|
return function (event) {
|
|
event.preventDefault();
|
|
|
|
if (!matchesDataTransferType(event.dataTransfer.types)) {
|
|
return;
|
|
}
|
|
|
|
event.currentTarget
|
|
.closest('.drop-area')
|
|
.classList.remove('drop-area--active');
|
|
|
|
const { files } = event.dataTransfer;
|
|
|
|
if (files.length > 1) {
|
|
addSnackbarItem({
|
|
message: 'Only one image can be dropped at a time.',
|
|
addCloseButton: true,
|
|
});
|
|
return;
|
|
}
|
|
|
|
processImageUpload(files, handleImageSuccess, handleImageFailure);
|
|
};
|
|
}
|
|
|
|
/**
|
|
* Dragover handler for the editor
|
|
*
|
|
* @param {DragEvent} event the drag event.
|
|
*/
|
|
export function onDragOver(event) {
|
|
event.preventDefault();
|
|
event.currentTarget
|
|
.closest('.drop-area')
|
|
.classList.add('drop-area--active');
|
|
}
|
|
|
|
/**
|
|
* DragExit handler for the editor
|
|
*
|
|
* @param {DragEvent} event the drag event.
|
|
*/
|
|
export function onDragExit(event) {
|
|
event.preventDefault();
|
|
event.currentTarget
|
|
.closest('.drop-area')
|
|
.classList.remove('drop-area--active');
|
|
}
|
|
|
|
/**
|
|
* Handler for when image upload fails.
|
|
*
|
|
* @param {Error} error an error
|
|
* @param {string} error.message an error message
|
|
*/
|
|
export function handleImageFailure({ message }) {
|
|
addSnackbarItem({
|
|
message,
|
|
addCloseButton: true,
|
|
});
|
|
}
|