frontend
frontend copied to clipboard
Add a picture uploader to picture-card-editor
Proposed change
Add a picture-upload form to the picture-card editor. I think this makes working with images much more accessible, otherwise it can be a source of a lot of confusion how to add pictures to the server and figure out the path.
Maybe this should be wrapped up in some kind of image-selector element that can be reused?
That would allow for either entering an external URL, uploading, or maybe opening a file picker and allowing to select from the list of current uploaded images either in /image/ or /www/?
Type of change
- [ ] Dependency upgrade
- [ ] Bugfix (non-breaking change which fixes an issue)
- [x] New feature (thank you!)
- [ ] Breaking change (fix/feature causing existing functionality to break)
- [ ] Code quality improvements to existing code or addition of tests
Example configuration
Additional information
- This PR fixes or closes issue: fixes #
- This PR is related to issue or discussion: https://github.com/home-assistant/frontend/discussions/18669
- Link to documentation pull request:
Checklist
- [x] The code change is tested and works locally.
- [x] There is no commented out code in this PR.
- [ ] Tests have been added to verify that the new code works.
If user exposed functionality or configuration variables are added/changed:
- [ ] Documentation added/updated for www.home-assistant.io
Oh I do like this!
Maybe this should be wrapped up in some kind of image-selector element that can be reused?
Yes, that would be a good idea, similar to the file selector
That would allow for either entering an external URL, uploading, or maybe opening a file picker and allowing to select from the list of current uploaded images either in /image/ or /www/?
That also sounds good, but should be options on the selector then probably? The last option would need backend support first. (We do need something similar for uploaded pictures though probably, as there is no way to manage or reuse already uploaded images)
If we had image-selector, what do you think of how the current UI looks?
OK to just have a <ha-picture-upload> stacked on top of a textbox for the path. Or is that take up too much space? Maybe have to click a button to show the uploader?
Image browser/picker could come later if/when that ever gets implemented.
I like this one too! I'm seeing 3 options to select an image, correct me if I'm wrong:
- Select a local file using image URL
- Upload image from device, that converts to a local image URL
- Upload image from web, that converts to a local image URL
In my opinion, the input-select with image URL isn't user friendly and pretty advanced. You have to know the path to a file, that requires access to your system. Can we drop this one? That means there should be an option to select an image from the system, like we're doing with the Media panel.
I like this one too! I'm seeing 3 options to select an image, correct me if I'm wrong:
- Select a local file using image URL
The URL input can also be remote URL on the web, it doesn't have to be local.
I think I would hide the input by default, but allow the user to switch to it with a button
I like this one too! I'm seeing 3 options to select an image, correct me if I'm wrong:
- Select a local file using image URL
- Upload image from device, that converts to a local image URL
- Upload image from web, that converts to a local image URL
The URL input can also be remote URL on the web, it doesn't have to be local.
That is the third option in my mind. So it would be:
- Upload image from web, that converts web URL to a local image URL
I think I would hide the input by default, but allow the user to switch to it with a button
I agree. There should be a way to select between selecting a local (already uploaded) file, upload new image or upload image from the web. This last option only have an input. The other doesn't need one in my mind.
I think I would hide the input by default
Can you clarify, what is "the input" you're referring to hide, the text selector, or the picture-upload?
I was kind of thinking to have the button toggle the larger upload element, though I'm wondering if Matthias is suggesting the opposite; to hide the URL. I'm not sure if that makes sense to me yet.
This is kind of what I was going to go for:
Matthias, you seem to also be suggesting that remote URLs should be replaced with "upload to a local image from web", but I think people might not necessarily want to upload remote URLs. The behavior now is just to show the live image from the remote domain. Maybe that is desired, in case the remote image is dynamic, or user just don't want to store it on his server?
@matthiasdebaat can you check above questions?
Thanks for your feedback. Based on this I've got something like this in my mind. More details can be found in the Figma design.
My suggestions are:
- Let the user to choose between upload or URL. When the user chooses upload, the URL input is hidden and vice versa.
- Add a delete button when an image is uploaded.
- URL is as we currently have it.
Updated from feedback.
I've for now dropped the crop feature from this as I don't really like how it works. When crop is enabled you are forced to go through the crop dialog, and there's no easy way to just use the entire image unaltered (you would have to try to drag the crop bubbles to exactly the corner of your image, and probably get it off by 1 pixel), so that seems potentially frustrating.
A better flow might be to have first the entire picture uploaded without crop, and then have a CROP button on the line below the picture to run it through the crop flow? Maybe that could be a different change.
We should add the camera entity and image entity to that radio list, as that will override the image (the entity will too btw on a picture-entity card, if it is a camera entity... 😓 )
Walkthrough
Walkthrough
The updates introduce a new image selector component (ha-selector-image) and enhance image handling across various parts of the application. Changes include adding style support, updating image selection mechanisms in different card editors, and modifying image-related data handling functions. Additionally, new translations and styles for warning buttons have been incorporated.
Changes
| Files/Groups | Change Summary |
|---|---|
src/components/ha-picture-upload.ts |
Added haStyle import, wrapped ha-button in a div, modified styles method to include haStyle. |
src/components/ha-selector/ha-selector-image.ts |
Introduced ha-selector-image component with image upload and URL selection features. |
src/components/ha-selector/ha-selector.ts |
Added ha-selector-image to LOAD_ELEMENTS for image selection functionality. |
src/data/image_upload.ts |
Added URL prefix constant, function to extract ID from URL, renamed parameter in deleteImage function. |
src/data/selector.ts |
Introduced ImageSelector interface in Selector type. |
src/panels/lovelace/editor/config-elements/... |
Updated image property selector from text to image in various card editors (picture, entity, glance). |
src/resources/styles.ts |
Added new style rule for warning buttons, setting primary theme color to error color. |
src/translations/en.json |
Added new "image" section with keys for selecting, uploading, and specifying image URLs. |
Sequence Diagram(s) (Beta)
sequenceDiagram
participant User
participant HaImageSelector
participant HaPictureUpload
participant ImageUploadAPI
User->>HaImageSelector: Select image option
HaImageSelector->>User: Show upload/URL input
User->>HaImageSelector: Upload image / Enter URL
HaImageSelector->>HaPictureUpload: Upload image
HaPictureUpload->>ImageUploadAPI: Send image data
ImageUploadAPI->>HaPictureUpload: Confirm upload
HaPictureUpload->>HaImageSelector: Return image URL
HaImageSelector->>User: Display selected image
Recent Review Details
Configuration used: CodeRabbit UI Review profile: ASSERTIVE
Commits
Files that changed from the base of the PR and between 5fab1969a8d85b5ac4fe9dcc34d66d7f77fb6482 and aaaf3aff8ce0a030870ae803f7a85fd29a197493.Files selected for processing (10)
- src/components/ha-picture-upload.ts (3 hunks)
- src/components/ha-selector/ha-selector-image.ts (1 hunks)
- src/components/ha-selector/ha-selector.ts (1 hunks)
- src/data/image_upload.ts (2 hunks)
- src/data/selector.ts (2 hunks)
- src/panels/lovelace/editor/config-elements/hui-picture-card-editor.ts (1 hunks)
- src/panels/lovelace/editor/config-elements/hui-picture-entity-card-editor.ts (1 hunks)
- src/panels/lovelace/editor/config-elements/hui-picture-glance-card-editor.ts (1 hunks)
- src/resources/styles.ts (1 hunks)
- src/translations/en.json (2 hunks)
Additional Context Used
Biome (58)
src/components/ha-picture-upload.ts (5)
136-136: Unexpected any. Specify a different type.
1-2: Some named imports are only used as types.
7-11: Some named imports are only used as types.
11-12: All these imports are only used as types.
110-110: Use Number.NaN instead of the equivalent global.
src/components/ha-selector/ha-selector-image.ts (4)
18-18: Unexpected any. Specify a different type.
1-1: Some named imports are only used as types.
3-4: All these imports are only used as types.
4-5: All these imports are only used as types.
src/components/ha-selector/ha-selector.ts (5)
72-72: Unexpected any. Specify a different type.
81-81: Unexpected any. Specify a different type.
87-87: Unexpected any. Specify a different type.
1-1: Some named imports are only used as types.
4-9: Some named imports are only used as types.
src/data/image_upload.ts (4)
18-18: This variable implicitly has the any type.
32-32: This type annotation is trivially inferred from its initialization.
58-60: This else clause can be omitted because previous branches break early.
1-1: All these imports are only used as types.
src/data/selector.ts (20)
339-339: Unexpected any. Specify a different type.
363-363: Unexpected any. Specify a different type.
470-484: Prefer for...of instead of forEach.
486-499: Prefer for...of instead of forEach.
501-512: Prefer for...of instead of forEach.
525-539: Prefer for...of instead of forEach.
553-566: Prefer for...of instead of forEach.
567-578: Prefer for...of instead of forEach.
590-601: Prefer for...of instead of forEach.
688-688: Forbidden non-null assertion.
783-785: Forbidden non-null assertion.
811-813: Forbidden non-null assertion.
4-5: All these imports are only used as types.
5-6: All these imports are only used as types.
6-10: Some named imports are only used as types.
10-14: All these imports are only used as types.
14-15: All these imports are only used as types.
91-91: Don't use '{}' as a type.
103-103: Don't use '{}' as a type.
108-108: Don't use '{}' as a type.
src/panels/lovelace/editor/config-elements/hui-picture-card-editor.ts (8)
78-78: Forbidden non-null assertion.
80-80: Forbidden non-null assertion.
85-85: Forbidden non-null assertion.
88-88: Forbidden non-null assertion.
4-5: All these imports are only used as types.
6-7: All these imports are only used as types.
7-8: All these imports are only used as types.
9-10: All these imports are only used as types.
src/panels/lovelace/editor/config-elements/hui-picture-entity-card-editor.ts (4)
120-120: Forbidden non-null assertion.
122-122: Forbidden non-null assertion.
126-126: Forbidden non-null assertion.
1-1: Some named imports are only used as types.
src/panels/lovelace/editor/config-elements/hui-picture-glance-card-editor.ts (8)
82-82: Forbidden non-null assertion.
86-86: Forbidden non-null assertion.
122-122: Change to an optional chain.
135-135: Forbidden non-null assertion.
137-137: Forbidden non-null assertion.
141-141: Forbidden non-null assertion.
145-145: Forbidden non-null assertion.
1-1: Some named imports are only used as types.
Additional comments not posted (10)
src/data/image_upload.ts (2)
11-11: The addition ofURL_PREFIXis a good practice for centralizing API endpoints, making them easier to manage and modify.
78-78: Renaming the parameter frommedia_idtoimage_idin thedeleteImagefunction improves clarity, aligning the terminology with the rest of the image handling functions.src/panels/lovelace/editor/config-elements/hui-picture-card-editor.ts (1)
27-27: Updating the schema to use the newimageselector aligns with the PR's goal to enhance image handling. This change facilitates a more intuitive UI for image selection.src/panels/lovelace/editor/config-elements/hui-picture-entity-card-editor.ts (1)
35-35: The addition of theimageselector to the schema is consistent with the PR's enhancements to image handling, providing a unified approach across different card editors.src/components/ha-picture-upload.ts (1)
Line range hint
5-174: The updates toHaPictureUploadinclude the integration ofhaStyleand improvements to the UI for handling image uploads and changes. The implementation of the image change and upload functionalities is clear and aligns with the PR's objectives to enhance image management.src/panels/lovelace/editor/config-elements/hui-picture-glance-card-editor.ts (1)
38-38: The update to include theimageselector in the schema for the picture glance card editor is consistent with the changes across other card editors, enhancing the user's ability to manage images directly within the UI.src/components/ha-selector/ha-selector.ts (1)
35-35: Addition ofimageselector type to support new image selection functionality.This change integrates the new
ha-selector-imagecomponent into the selector system, enabling image selection capabilities across the platform.src/resources/styles.ts (1)
85-85: Styling for warning buttons to use error color.This change ensures that warning buttons are visually consistent with error indicators, enhancing UI consistency and user experience.
src/data/selector.ts (1)
43-43: Introduction ofImageSelectortype to support new image selection functionality.This addition is crucial for enabling the new image selector functionality within the system, allowing for a structured way to handle image selections.
src/translations/en.json (1)
380-384: Translations for the new image handling features are clear and well-structured.
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?
Tips
Chat
There are 3 ways to chat with CodeRabbit:
- Review comments: Directly reply to a review comment made by CodeRabbit. Example:
I pushed a fix in commit <commit_id>.Generate unit testing code for this file.Open a follow-up GitHub issue for this discussion.
- Files and specific lines of code (under the "Files changed" tab): Tag
@coderabbitaiin a new review comment at the desired location with your query. Examples:@coderabbitai generate unit testing code for this file.@coderabbitai modularize this function.
- PR comments: Tag
@coderabbitaiin a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:@coderabbitai generate interesting stats about this repository and render them as a table.@coderabbitai show all the console.log statements in this repository.@coderabbitai read src/utils.ts and generate unit testing code.@coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.@coderabbitai help me debug CodeRabbit configuration file.
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.
CodeRabbit Commands (invoked as PR comments)
@coderabbitai pauseto pause the reviews on a PR.@coderabbitai resumeto resume the paused reviews.@coderabbitai reviewto trigger an incremental review. This is useful when automatic reviews are disabled for the repository.@coderabbitai full reviewto do a full review from scratch and review all the files again.@coderabbitai summaryto regenerate the summary of the PR.@coderabbitai resolveresolve all the CodeRabbit review comments.@coderabbitai configurationto show the current CodeRabbit configuration for the repository.@coderabbitai helpto get help.
Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
CodeRabbit Configration File (.coderabbit.yaml)
- You can programmatically configure CodeRabbit by adding a
.coderabbit.yamlfile to the root of your repository. - Please see the configuration documentation for more information.
- If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation:
# yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json
Documentation and Community
- Visit our Documentation for detailed information on how to use CodeRabbit.
- Join our Discord Community to get help, request features, and share feedback.
- Follow us on X/Twitter for updates and announcements.
I would still really like to combine the image entity/camera entity/upload image/image url options for improved clarity that they can not be used together. But this is already a nice addition that we should merge. Thanks @karwosts!