-
Notifications
You must be signed in to change notification settings - Fork 2.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow selecting previously uploaded image for picture upload #23072
base: dev
Are you sure you want to change the base?
Allow selecting previously uploaded image for picture upload #23072
Conversation
Love this! ❤️ We need to think what to do with images sizes, currently we upload and cut an image to size, so we know what size (or ratio) image we will get. If we allow people to select previously uploaded images these may not have the requested ratio/size.
1 use-case where this will happen is the user profile picture. |
A sequence like the following might work, for image uploads with cropOptions:
Kind of hacked my way through this and I think all the pieces work. |
Updated cropping as proposed. |
Thank you. We wait on a design for the select prev image button and come back to you |
Hi @karwosts, We would like to integrate the "select media" button into the Please make it optional, because we have file uploads that don't need the select option. If you have questions just ask |
I did hesitate in my original proposal to use the term "select from media" as we are not allowing (currently) selecting any media image, it can only be a file previously uploaded via image_upload, not anything from any other media integration. Just want to be clear you're aware of that limitation. |
Ok good point, but I think we can risk it. |
As for the design of the picker, I've done a very similar one for choosing local image via the Local path or web URL: If the first option now gets the ability to choose a local image, the second option right now is getting redundant. It only has the ability to paste in a web URL which is not aligned with HA principles of being local only. We'll discuss this internally, and get back as soon as possible. |
Second option can also be used for local only images, for anything that anyone has manually loaded to their /www/ folder, which would have been how this has always been done up until the upload dialog was added last year. Any files loaded manually to the /www/ folder are not available to the picker. Or maybe for a URL which is still local but not hosted by HA. |
No because local path is already in the UI, removing it would be a breaking change. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome, just some details left.
src/components/ha-picture-upload.ts
Outdated
navigateIds: [ | ||
{ media_content_id: undefined, media_content_type: undefined }, | ||
{ | ||
media_content_id: "media-source://image_upload", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please add a static constant for "media-source://image_upload"
src/components/ha-picture-upload.ts
Outdated
const id = pickedMedia.item.media_content_id; | ||
const stringToRemove = "media-source://image_upload/"; | ||
if (id.startsWith(stringToRemove)) { | ||
const mediaId = id.substr(stringToRemove.length); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
substr
is depricated: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substr
Please use substring
src/components/ha-picture-upload.ts
Outdated
const response = await fetch(url); | ||
const data = await response.blob(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can fail, please add error handling
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you elaborate a bit what you want to see here?
So HA gives us the list of images, we click on one, and then... it's not available? What should I do with that, a popup dialog that just says "unknown error"? I would have thought just whatever default logging exception to the console would be sufficient, and how this would usually be handled.
Or what is the specific mode of failure you were thinking of?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds good.
If it fails to load use showAlertDialog
with a text: "Load image preview in crop editor failed" (this is just an example).
I know this is an edge case but http requests can always fail and showing it only in console won't help the user in the situation.
@@ -72,6 +95,12 @@ export class HaImagecropperDialog extends LitElement { | |||
<mwc-button slot="secondaryAction" @click=${this.closeDialog}> | |||
${this.hass.localize("ui.common.cancel")} | |||
</mwc-button> | |||
${this._isTargetAspectRatio | |||
? html` <mwc-button slot="primaryAction" @click=${this._useOriginal}> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
? html` <mwc-button slot="primaryAction" @click=${this._useOriginal}> | |
? html`<mwc-button slot="primaryAction" @click=${this._useOriginal}> |
src/components/ha-picture-upload.ts
Outdated
constructor() { | ||
super(); | ||
this._chooseMedia = this._chooseMedia.bind(this); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
constructor() { | |
super(); | |
this._chooseMedia = this._chooseMedia.bind(this); | |
} |
use arrow function instead
src/components/ha-picture-upload.ts
Outdated
@@ -141,16 +175,58 @@ export class HaPictureUpload extends LitElement { | |||
} | |||
} | |||
|
|||
private _chooseMedia(): void { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
private _chooseMedia(): void { | |
private _chooseMedia = () => { |
Please take a look at the requested changes, and use the Ready for review button when you are done, thanks 👍 |
… select-previous-upload-image
src/components/ha-picture-upload.ts
Outdated
@@ -39,13 +49,32 @@ export class HaPictureUpload extends LitElement { | |||
|
|||
public render(): TemplateResult { | |||
if (!this.value) { | |||
/* eslint-disable lit-a11y/anchor-is-valid */ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not too sure about this check. Due I think to recent changes to dialogs/navigation, the href=# didn't work anymore, so I had to change to href="javascript:".
Eslint however doesn't seem to like the new "fake" anchor. I'm not sure how else to open a dialog from a link like was requested though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mhh yeah not the nicest solution.
What about using a button
tag and style it like a link?
Proposed change
In the image upload form, allow selecting a previously uploaded image via the browse media dialog.
Requires home-assistant/core#131468
Type of change
Example configuration
Additional information
Checklist
If user exposed functionality or configuration variables are added/changed: