From a9c27ad8dd4dedc13e41143a13355ee51f1263e3 Mon Sep 17 00:00:00 2001 From: Bram Kragten Date: Thu, 25 May 2023 17:58:05 +0200 Subject: [PATCH] Save automation clipboard in session storage (#16624) --- src/common/decorators/local-storage.ts | 17 ++++++++++++----- .../automation/manual-automation-editor.ts | 6 ++++-- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/src/common/decorators/local-storage.ts b/src/common/decorators/local-storage.ts index 8794af4d23..752b95933e 100644 --- a/src/common/decorators/local-storage.ts +++ b/src/common/decorators/local-storage.ts @@ -5,7 +5,8 @@ import type { ClassElement } from "../../types"; type Callback = (oldValue: any, newValue: any) => void; class Storage { - constructor(subscribe = true) { + constructor(subscribe = true, storage = window.localStorage) { + this.storage = storage; if (!subscribe) { return; } @@ -26,6 +27,8 @@ class Storage { }); } + public storage: globalThis.Storage; + private _storage: { [storageKey: string]: any } = {}; private _listeners: { @@ -34,7 +37,7 @@ class Storage { public addFromStorage(storageKey: any): void { if (!this._storage[storageKey]) { - const data = window.localStorage.getItem(storageKey); + const data = this.storage.getItem(storageKey); if (data) { this._storage[storageKey] = JSON.parse(data); } @@ -77,9 +80,9 @@ class Storage { this._storage[storageKey] = value; try { if (value === undefined) { - window.localStorage.removeItem(storageKey); + this.storage.removeItem(storageKey); } else { - window.localStorage.setItem(storageKey, JSON.stringify(value)); + this.storage.setItem(storageKey, JSON.stringify(value)); } } catch (err: any) { // Safari in private mode doesn't allow localstorage @@ -94,10 +97,14 @@ export const LocalStorage = storageKey?: string, property?: boolean, subscribe = true, + storageType?: globalThis.Storage, propertyOptions?: PropertyDeclaration ): any => (clsElement: ClassElement) => { - const storage = subscribe ? subscribeStorage : new Storage(false); + const storage = + subscribe && !storageType + ? subscribeStorage + : new Storage(subscribe, storageType); const key = String(clsElement.key); storageKey = storageKey || String(clsElement.key); diff --git a/src/panels/config/automation/manual-automation-editor.ts b/src/panels/config/automation/manual-automation-editor.ts index c2e768c725..7324f8d5a4 100644 --- a/src/panels/config/automation/manual-automation-editor.ts +++ b/src/panels/config/automation/manual-automation-editor.ts @@ -2,7 +2,7 @@ import "@material/mwc-button/mwc-button"; import { mdiHelpCircle } from "@mdi/js"; import { HassEntity } from "home-assistant-js-websocket"; import { css, CSSResultGroup, html, LitElement } from "lit"; -import { customElement, property, state } from "lit/decorators"; +import { customElement, property } from "lit/decorators"; import deepClone from "deep-clone-simple"; import { fireEvent } from "../../../common/dom/fire_event"; import "../../../components/ha-card"; @@ -20,6 +20,7 @@ import { documentationUrl } from "../../../util/documentation-url"; import "./action/ha-automation-action"; import "./condition/ha-automation-condition"; import "./trigger/ha-automation-trigger"; +import { LocalStorage } from "../../../common/decorators/local-storage"; @customElement("manual-automation-editor") export class HaManualAutomationEditor extends LitElement { @@ -35,7 +36,8 @@ export class HaManualAutomationEditor extends LitElement { @property({ attribute: false }) public stateObj?: HassEntity; - @state() private _clipboard: Clipboard = {}; + @LocalStorage("automationClipboard", true, false, window.sessionStorage) + private _clipboard: Clipboard = {}; protected render() { return html`