Skip to content

add drop-to-replace functionality for image prompt drop zone#1341

Open
GlenCarpenter wants to merge 5 commits intomcmonkeyprojects:masterfrom
GlenCarpenter:master
Open

add drop-to-replace functionality for image prompt drop zone#1341
GlenCarpenter wants to merge 5 commits intomcmonkeyprojects:masterfrom
GlenCarpenter:master

Conversation

@GlenCarpenter
Copy link
Copy Markdown
Contributor

@GlenCarpenter GlenCarpenter commented Apr 10, 2026

add drop-to-replace functionality for image prompt drop zone

  • adds drag event listeners to image prompt dropzone to watch for file replacements
  • shows a 2px white outline: 4px solid var(--emphasis) to indicate file will be replaced
  • adds a 2px margin to images in drop zone to prevent outline overlap

@GlenCarpenter
Copy link
Copy Markdown
Contributor Author

this is related to #1269

let type = img.src.substring(img.src.lastIndexOf('.') + 1);
let file = new File([blob], imagePathClean, { type: `image/${type.length > 0 && type.length < 20 ? type : 'png'}` });
imagePromptAddImage(file);
imagePromptAddOrReplaceImage(file);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this function rename makes the commit messy, keep the original function name

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I originally renamed the old version of imagePromptAddImage to imagePromptAddImageCore and called it in a new imagePromptAddImage, but this feels more descriptive to me. "adding" and "replacing" are two separate actions and this is a function that figures out which action to take. That being said it's not a hill I am going to die on so I can change it back if you feel it makes more sense.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

got rid of the unnecessary functions

}
}

function isImageFileDragEvent(e) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this function is... odd. There's existing handling for drag/drop stuff

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will move this to the other event handling logic.

}

function getPromptImageDropReplaceTarget(e) {
if (!isImageFileDragEvent(e) || !e.target || !e.target.closest) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why would element.closest ever not exist

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

old habit I guess. It has full browser support now so this check isn't needed.

return;
}
let reader = new FileReader();
reader.onload = (e) => {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there should not be new file reading code, there already is code for this stuff

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will refactor to use the existing file reader code.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

consolidated a bunch of code

cursor: pointer;
}
.alt-prompt-image-container.image-drop-replace-target .alt-prompt-image {
outline: 2px solid white;
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mcmonkey4eva I am going to update this to match the :hover state for thumbnails

outline: 2px solid var(--box-selected-border-stronger);
border-radius: 5px;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ended up with

.alt-prompt-image-container.image-drop-replace-target .alt-prompt-image {
    outline: 4px solid var(--emphasis);
    border-radius: 5px;
}

This makes it more obvious the image is being targeted.

}

/** Reads the given file as a data URL and passes the result to the given handler. Ignores null file inputs. */
function readFileAsDataURL(file, handler) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

where did this come from now? again, there's already code that handles this part, you have no reason to be committing new file reading

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought this was the existing code you were referencing. What is the point of this module if we are going to create inline FileReader?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The key rule I think you're missing here is: a PR should always seek to create the minimum possible changes needed to achieve its stated goal. Refactoring and reorganizing should not happen in a PR unless that is the focused purpose of a PR, which this is not.
There's already code that handles reading a file in the correct place, so you should not be modifying it at all here, just redirecting where the resultant image goes.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense, I see what you're saying. I'll restore the old FileReader and keep all the new code localized to the existing imagePromptAddImage

@mcmonkey4eva
Copy link
Copy Markdown
Member

don't do the forcepushes btw, just do regular commits, it'll be squashed on final pull

}

function getPromptImageDropReplaceTarget(e) {
if (!e || !e.dataTransfer || !e.target || !e.target.closest || uiImprover.getFileList(e.dataTransfer, e).length == 0) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

again these are strange checks that don't make sense

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mean just the e.target.closest or all of the event checks?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The closest one is the obvious problem, but I imagine most of the checks here can never fail, and so don't need to be checked

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants