Skip to content

Commit

Permalink
fix: checking if paste was an image and return false if its not.
Browse files Browse the repository at this point in the history
  • Loading branch information
khakimov committed Nov 12, 2023
1 parent a161a57 commit e4bd6d3
Showing 1 changed file with 17 additions and 18 deletions.
35 changes: 17 additions & 18 deletions src/renderer/pages/Pile/Editor/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -74,20 +74,7 @@ export default function Editor({
},
});

const handleFile = (fileOrItem) => {
let file;
if (fileOrItem instanceof DataTransferItem) {
file = fileOrItem.getAsFile();

if (!file) {
return;
}
} else if (fileOrItem instanceof File) {
file = fileOrItem;
} else {
throw new Error('Invalid argument: must be a File or DataTransferItem');
}

const handleFile = (file) => {
if (file && file.type.indexOf('image') === 0) {
const fileName = file.name; // Retrieve the filename
const fileExtension = fileName.split('.').pop(); // Extract the file extension
Expand All @@ -101,6 +88,13 @@ export default function Editor({
}
};

const handleDataTransferItem = (item) => {
const file = item.getAsFile();
if (file) {
handleFile(file);
}
};

const editor = useEditor({
extensions: [
StarterKit,
Expand All @@ -120,8 +114,13 @@ export default function Editor({
let imageHandled = false; // flag to track if an image was handled

if (items) {
items.forEach(handleFile);
imageHandled = true;
items.forEach((item) => {
// Check if the item type is an image
if (item.type && item.type.indexOf('image') === 0) {
handleDataTransferItem(item);
imageHandled = true;
}
});
}
return imageHandled;
},
Expand All @@ -134,8 +133,8 @@ export default function Editor({
event.dataTransfer.files[0]
) {
// if dropping external files
const items = Array.from(event.dataTransfer.files);
items.forEach(handleFile);
const files = Array.from(event.dataTransfer.files);
files.forEach(handleFile);
return imageHandled; // handled
}
return imageHandled; // not handled use default behaviour
Expand Down

0 comments on commit e4bd6d3

Please sign in to comment.