Skip to content
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

Merged
merged 8 commits into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 84 additions & 6 deletions src/components/ha-picture-upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,23 @@ import { mdiImagePlus } from "@mdi/js";
import type { TemplateResult } from "lit";
import { LitElement, css, html } from "lit";
import { customElement, property, state } from "lit/decorators";
import type { MediaPickedEvent } from "../data/media-player";
import { fireEvent } from "../common/dom/fire_event";
import { haStyle } from "../resources/styles";
import { createImage, generateImageThumbnailUrl } from "../data/image_upload";
import {
MEDIA_PREFIX,
getIdFromUrl,
createImage,
generateImageThumbnailUrl,
} from "../data/image_upload";
import { showAlertDialog } from "../dialogs/generic/show-dialog-box";
import type { CropOptions } from "../dialogs/image-cropper-dialog/show-image-cropper-dialog";
import { showImageCropperDialog } from "../dialogs/image-cropper-dialog/show-image-cropper-dialog";
import type { HomeAssistant } from "../types";
import "./ha-button";
import "./ha-circular-progress";
import "./ha-file-upload";
import { showMediaBrowserDialog } from "./media-player/show-media-browser-dialog";

@customElement("ha-picture-upload")
export class HaPictureUpload extends LitElement {
Expand All @@ -29,6 +36,9 @@ export class HaPictureUpload extends LitElement {

@property({ type: Boolean }) public crop = false;

@property({ type: Boolean, attribute: "select-media" }) public selectMedia =
false;

@property({ attribute: false }) public cropOptions?: CropOptions;

@property({ type: Boolean }) public original = false;
Expand All @@ -39,13 +49,32 @@ export class HaPictureUpload extends LitElement {

public render(): TemplateResult {
if (!this.value) {
/* eslint-disable lit-a11y/anchor-is-valid */
Copy link
Contributor Author

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.

Copy link
Contributor

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?

const secondary =
this.secondary ||
(this.selectMedia
? html`${this.hass.localize(
"ui.components.picture-upload.secondary",
{
select_media: html`<a
href="javascript:"
@click=${this._chooseMedia}
>${this.hass.localize(
"ui.components.picture-upload.select_media"
)}</a
>`,
}
)}`
: undefined);
/* eslint-enable lit-a11y/anchor-is-valid */

return html`
<ha-file-upload
.hass=${this.hass}
.icon=${mdiImagePlus}
.label=${this.label ||
this.hass.localize("ui.components.picture-upload.label")}
.secondary=${this.secondary}
.secondary=${secondary}
.supports=${this.supports ||
this.hass.localize("ui.components.picture-upload.supported_formats")}
.uploading=${this._uploading}
Expand Down Expand Up @@ -93,7 +122,7 @@ export class HaPictureUpload extends LitElement {
this.value = null;
}

private async _cropFile(file: File) {
private async _cropFile(file: File, mediaId?: string) {
if (!["image/png", "image/jpeg", "image/gif"].includes(file.type)) {
showAlertDialog(this, {
text: this.hass.localize(
Expand All @@ -109,7 +138,16 @@ export class HaPictureUpload extends LitElement {
aspectRatio: NaN,
},
croppedCallback: (croppedFile) => {
this._uploadFile(croppedFile);
if (mediaId && croppedFile === file) {
this.value = generateImageThumbnailUrl(
mediaId,
this.size,
this.original
);
fireEvent(this, "change");
} else {
this._uploadFile(croppedFile);
}
},
});
}
Expand Down Expand Up @@ -141,16 +179,56 @@ export class HaPictureUpload extends LitElement {
}
}

private _chooseMedia = () => {
showMediaBrowserDialog(this, {
action: "pick",
entityId: "browser",
navigateIds: [
{ media_content_id: undefined, media_content_type: undefined },
{
media_content_id: MEDIA_PREFIX,
media_content_type: "app",
},
],
minimumNavigateLevel: 2,
mediaPickedCallback: async (pickedMedia: MediaPickedEvent) => {
const mediaId = getIdFromUrl(pickedMedia.item.media_content_id);
if (mediaId) {
if (this.crop) {
const url = generateImageThumbnailUrl(mediaId, undefined, true);
const response = await fetch(url);
const data = await response.blob();
Copy link
Contributor

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

Copy link
Contributor Author

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?

Copy link
Contributor

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.

const metadata = {
type: pickedMedia.item.media_content_type,
};
const file = new File([data], pickedMedia.item.title, metadata);
this._cropFile(file, mediaId);
} else {
this.value = generateImageThumbnailUrl(
mediaId,
this.size,
this.original
);
fireEvent(this, "change");
}
}
},
});
};

static get styles() {
return [
haStyle,
css`
:host {
display: block;
height: 240px;
}
ha-file-upload {
height: 100%;
height: 240px;
}
ha-button.center {
display: flex;
align-items: center;
}
.center-vertical {
display: flex;
Expand Down
1 change: 1 addition & 0 deletions src/components/ha-selector/ha-selector-image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ export class HaImageSelector extends LitElement {
.value=${this.value?.startsWith(URL_PREFIX) ? this.value : null}
.original=${this.selector.image?.original}
.cropOptions=${this.selector.image?.crop}
select-media
@change=${this._pictureChanged}
></ha-picture-upload>
`}
Expand Down
2 changes: 1 addition & 1 deletion src/components/media-player/dialog-media-player-browse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ class DialogMediaPlayerBrowse extends LitElement {
@opened=${this._dialogOpened}
>
<ha-dialog-header show-border slot="heading">
${this._navigateIds.length > 1
${this._navigateIds.length > (this._params.minimumNavigateLevel ?? 1)
? html`
<ha-icon-button
slot="navigationIcon"
Expand Down
1 change: 1 addition & 0 deletions src/components/media-player/show-media-browser-dialog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export interface MediaPlayerBrowseDialogParams {
entityId: string;
mediaPickedCallback: (pickedMedia: MediaPickedEvent) => void;
navigateIds?: MediaPlayerItemId[];
minimumNavigateLevel?: number;
}

export const showMediaBrowserDialog = (
Expand Down
4 changes: 2 additions & 2 deletions src/data/image_upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ interface Image {
}

export const URL_PREFIX = "/api/image/serve/";
export const MEDIA_PREFIX = "media-source://image_upload/";
export const MEDIA_PREFIX = "media-source://image_upload";

export interface ImageMutableParams {
name: string;
Expand All @@ -24,7 +24,7 @@ export const getIdFromUrl = (url: string): string | undefined => {
id = id.substring(0, idx);
}
} else if (url.startsWith(MEDIA_PREFIX)) {
id = url.substring(MEDIA_PREFIX.length);
id = url.substring(MEDIA_PREFIX.length + 1);
}
return id;
};
Expand Down
36 changes: 35 additions & 1 deletion src/dialogs/image-cropper-dialog/image-cropper-dialog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import Cropper from "cropperjs";
// @ts-ignore
import cropperCss from "cropperjs/dist/cropper.css";
import type { CSSResultGroup, PropertyValues, TemplateResult } from "lit";
import { css, html, LitElement, unsafeCSS } from "lit";
import { css, html, nothing, LitElement, unsafeCSS } from "lit";
import { customElement, property, state, query } from "lit/decorators";
import { classMap } from "lit/directives/class-map";
import "../../components/ha-dialog";
Expand All @@ -23,6 +23,8 @@ export class HaImagecropperDialog extends LitElement {

private _cropper?: Cropper;

@state() private _isTargetAspectRatio?: boolean;

public showDialog(params: HaImageCropperDialogParams): void {
this._params = params;
this._open = true;
Expand All @@ -33,6 +35,7 @@ export class HaImagecropperDialog extends LitElement {
this._params = undefined;
this._cropper?.destroy();
this._cropper = undefined;
this._isTargetAspectRatio = false;
}

protected updated(changedProperties: PropertyValues) {
Expand All @@ -47,6 +50,7 @@ export class HaImagecropperDialog extends LitElement {
dragMode: "move",
minCropBoxWidth: 50,
ready: () => {
this._isTargetAspectRatio = this._checkMatchAspectRatio();
URL.revokeObjectURL(this._image!.src);
},
});
Expand All @@ -55,6 +59,25 @@ export class HaImagecropperDialog extends LitElement {
}
}

private _checkMatchAspectRatio(): boolean {
const targetRatio = this._params?.options.aspectRatio;
if (!targetRatio) {
return true;
}
const imageData = this._cropper!.getImageData();
if (imageData.aspectRatio === targetRatio) {
return true;
}

// If the image is not exactly the aspect ratio see if it is within a pixel.
if (imageData.naturalWidth > imageData.naturalHeight) {
const targetHeight = imageData.naturalWidth / targetRatio;
return Math.abs(targetHeight - imageData.naturalHeight) <= 1;
}
const targetWidth = imageData.naturalHeight * targetRatio;
return Math.abs(targetWidth - imageData.naturalWidth) <= 1;
}

protected render(): TemplateResult {
return html`<ha-dialog
@closed=${this.closeDialog}
Expand All @@ -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}>
${this.hass.localize("ui.dialogs.image_cropper.use_original")}
</mwc-button>`
: nothing}

<mwc-button slot="primaryAction" @click=${this._cropImage}>
${this.hass.localize("ui.dialogs.image_cropper.crop")}
</mwc-button>
Expand All @@ -95,6 +124,11 @@ export class HaImagecropperDialog extends LitElement {
);
}

private _useOriginal() {
this._params!.croppedCallback(this._params!.file);
this.closeDialog();
}

static get styles(): CSSResultGroup {
return [
haStyleDialog,
Expand Down
1 change: 1 addition & 0 deletions src/panels/config/areas/dialog-area-registry-detail.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ class DialogAreaDetail extends LitElement {
.hass=${this.hass}
.value=${this._picture}
crop
select-media
.cropOptions=${cropOptions}
@change=${this._pictureChanged}
></ha-picture-upload>
Expand Down
1 change: 1 addition & 0 deletions src/panels/config/person/dialog-person-detail.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ class DialogPersonDetail extends LitElement implements HassDialog {
.hass=${this.hass}
.value=${this._picture}
crop
select-media
.cropOptions=${cropOptions}
@change=${this._pictureChanged}
></ha-picture-upload>
Expand Down
7 changes: 5 additions & 2 deletions src/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -757,7 +757,9 @@
"change_picture": "Change picture",
"current_image_alt": "Current picture",
"supported_formats": "Supports JPEG, PNG, or GIF image.",
"unsupported_format": "Unsupported format, please choose a JPEG, PNG, or GIF image."
"unsupported_format": "Unsupported format, please choose a JPEG, PNG, or GIF image.",
"secondary": "Drop your file here or {select_media}",
"select_media": "select from media"
},
"color-picker": {
"default": "default",
Expand Down Expand Up @@ -1226,7 +1228,8 @@
},
"image_cropper": {
"crop": "Crop",
"crop_image": "Picture to crop"
"crop_image": "Picture to crop",
"use_original": "Use original"
},
"date-picker": {
"today": "Today",
Expand Down
Loading