import "@material/mwc-button/mwc-button"; import { mdiHelpCircle } from "@mdi/js"; import { CSSResultGroup, LitElement, css, html, nothing } from "lit"; import { customElement, property, query } from "lit/decorators"; import { fireEvent } from "../../../common/dom/fire_event"; import { nestedArrayMove } from "../../../common/util/array-move"; import "../../../components/ha-card"; import "../../../components/ha-icon-button"; import { Action, Fields, ScriptConfig } from "../../../data/script"; import { haStyle } from "../../../resources/styles"; import type { HomeAssistant } from "../../../types"; import { documentationUrl } from "../../../util/documentation-url"; import "../automation/action/ha-automation-action"; import "./ha-script-fields"; import type HaScriptFields from "./ha-script-fields"; @customElement("manual-script-editor") export class HaManualScriptEditor extends LitElement { @property({ attribute: false }) public hass!: HomeAssistant; @property({ type: Boolean }) public isWide = false; @property({ type: Boolean }) public narrow = false; @property({ type: Boolean }) public disabled = false; @property({ attribute: false }) public config!: ScriptConfig; @query("ha-script-fields") private _scriptFields?: HaScriptFields; private _openFields = false; public addFields() { this._openFields = true; fireEvent(this, "value-changed", { value: { ...this.config, fields: { [this.hass.localize("ui.panel.config.script.editor.field.field") || "field"]: { selector: { text: null, }, }, }, }, }); } protected updated(changedProps) { if (this._openFields && changedProps.has("config")) { this._openFields = false; this._scriptFields?.updateComplete.then(() => this._scriptFields?.focusLastField() ); } } protected render() { return html` ${this.disabled ? html` ${this.hass.localize("ui.panel.config.script.editor.read_only")} ${this.hass.localize("ui.panel.config.script.editor.migrate")} ` : ""} ${this.config.fields ? html`

${this.hass.localize( "ui.panel.config.script.editor.field.fields" )}

` : nothing}

${this.hass.localize("ui.panel.config.script.editor.sequence")}

`; } private _fieldsChanged(ev: CustomEvent): void { ev.stopPropagation(); fireEvent(this, "value-changed", { value: { ...this.config!, fields: ev.detail.value as Fields }, }); } private _sequenceChanged(ev: CustomEvent): void { ev.stopPropagation(); fireEvent(this, "value-changed", { value: { ...this.config!, sequence: ev.detail.value as Action[] }, }); } private _itemMoved(ev: CustomEvent): void { ev.stopPropagation(); const { oldIndex, newIndex, oldPath, newPath } = ev.detail; const updatedConfig = nestedArrayMove( this.config, oldIndex, newIndex, oldPath, newPath ); fireEvent(this, "value-changed", { value: updatedConfig, }); } private _duplicate() { fireEvent(this, "duplicate"); } static get styles(): CSSResultGroup { return [ haStyle, css` :host { display: block; } ha-card { overflow: hidden; } .description { margin: 0; } p { margin-bottom: 0; } .header { display: flex; align-items: center; } .header:first-child { margin-top: -16px; } .header .name { font-size: 20px; font-weight: 400; flex: 1; } .header a { color: var(--secondary-text-color); } ha-alert.re-order { display: block; margin-bottom: 16px; border-radius: var(--ha-card-border-radius, 12px); overflow: hidden; } `, ]; } } declare global { interface HTMLElementTagNameMap { "manual-script-editor": HaManualScriptEditor; } }