diff --git a/src/common/string/slugify.ts b/src/common/string/slugify.ts new file mode 100644 index 0000000000..74a21b5c8f --- /dev/null +++ b/src/common/string/slugify.ts @@ -0,0 +1,19 @@ +// https://gist.github.com/hagemann/382adfc57adbd5af078dc93feef01fe1 +export const slugify = (value: string) => { + const a = + "àáäâãåăæąçćčđďèéěėëêęğǵḧìíïîįłḿǹńňñòóöôœøṕŕřßşśšșťțùúüûǘůűūųẃẍÿýźžż·/_,:;"; + const b = + "aaaaaaaaacccddeeeeeeegghiiiiilmnnnnooooooprrsssssttuuuuuuuuuwxyyzzz------"; + const p = new RegExp(a.split("").join("|"), "g"); + + return value + .toString() + .toLowerCase() + .replace(/\s+/g, "-") // Replace spaces with - + .replace(p, (c) => b.charAt(a.indexOf(c))) // Replace special characters + .replace(/&/g, "-and-") // Replace & with 'and' + .replace(/[^\w\-]+/g, "") // Remove all non-word characters + .replace(/\-\-+/g, "-") // Replace multiple - with single - + .replace(/^-+/, "") // Trim - from start of text + .replace(/-+$/, ""); // Trim - from end of text +}; diff --git a/src/panels/lovelace/editor/view-editor/hui-edit-view.ts b/src/panels/lovelace/editor/view-editor/hui-edit-view.ts index 4af79771f3..55d68a1702 100644 --- a/src/panels/lovelace/editor/view-editor/hui-edit-view.ts +++ b/src/panels/lovelace/editor/view-editor/hui-edit-view.ts @@ -93,6 +93,7 @@ export class HuiEditView extends LitElement { case "tab-settings": content = html` Panel Mode? @@ -112,10 +115,6 @@ export class HuiViewEditor extends LitElement { } private _valueChanged(ev: Event): void { - if (!this._config || !this.hass) { - return; - } - const target = ev.currentTarget! as EditorTarget; if (this[`_${target.configValue}`] === target.value) { @@ -134,6 +133,20 @@ export class HuiViewEditor extends LitElement { fireEvent(this, "view-config-changed", { config: newConfig }); } + + private _handleTitleBlur(ev) { + if ( + !this.isNew || + this._suggestedPath || + this._config.path || + !ev.currentTarget.value + ) { + return; + } + + const config = { ...this._config, path: slugify(ev.currentTarget.value) }; + fireEvent(this, "view-config-changed", { config }); + } } declare global {