-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
115 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
src/panels/lovelace/editor/view-editor/hui-view-background-editor.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import "@material/mwc-list/mwc-list-item"; | ||
import { CSSResultGroup, LitElement, css, html, nothing } from "lit"; | ||
import { customElement, property, state } from "lit/decorators"; | ||
import { fireEvent } from "../../../../common/dom/fire_event"; | ||
import "../../../../components/user/ha-user-badge"; | ||
import { LovelaceViewConfig } from "../../../../data/lovelace/config/view"; | ||
import { HomeAssistant, ValueChangedEvent } from "../../../../types"; | ||
import "../../../../components/ha-picture-upload"; | ||
import type { HaPictureUpload } from "../../../../components/ha-picture-upload"; | ||
import { CropOptions } from "../../../../dialogs/image-cropper-dialog/show-image-cropper-dialog"; | ||
|
||
const cropOptions: CropOptions = { | ||
round: false, | ||
type: "image/jpeg", | ||
quality: 0.75, | ||
aspectRatio: 1.78, | ||
}; | ||
|
||
@customElement("hui-view-background-editor") | ||
export class HuiViewBackgroundEditor extends LitElement { | ||
@property({ attribute: false }) public hass!: HomeAssistant; | ||
|
||
@state() private _config!: LovelaceViewConfig; | ||
|
||
set config(config: LovelaceViewConfig) { | ||
this._config = config; | ||
} | ||
|
||
protected render() { | ||
if (!this.hass) { | ||
return nothing; | ||
} | ||
|
||
const backgroundUrlRegex = /url\(['"]?([^'"]+)['"]?\)/; | ||
const backgroundUrlMatch = backgroundUrlRegex.exec( | ||
this._config?.background || "" | ||
); | ||
const backgroundUrl = backgroundUrlMatch ? backgroundUrlMatch[1] : null; | ||
|
||
return html` | ||
<p> | ||
${this.hass.localize( | ||
"ui.panel.lovelace.editor.edit_view.background.title" | ||
)} | ||
</p> | ||
<ha-picture-upload | ||
.hass=${this.hass} | ||
.value=${backgroundUrl} | ||
crop | ||
.cropOptions=${cropOptions} | ||
original | ||
@change=${this._backgroundChanged} | ||
></ha-picture-upload> | ||
`; | ||
} | ||
|
||
private _backgroundChanged(ev: ValueChangedEvent<string | null>) { | ||
const backgroundUrl = (ev.target as HaPictureUpload).value; | ||
const config = { | ||
...this._config, | ||
background: backgroundUrl | ||
? `center / cover no-repeat url('${backgroundUrl}')` | ||
: undefined, | ||
}; | ||
fireEvent(this, "view-config-changed", { config }); | ||
} | ||
|
||
static get styles(): CSSResultGroup { | ||
return css` | ||
:host { | ||
display: block; | ||
} | ||
`; | ||
} | ||
} | ||
|
||
declare global { | ||
interface HTMLElementTagNameMap { | ||
"hui-view-background-editor": HuiViewBackgroundEditor; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters