Save automation clipboard in session storage (#16624)

This commit is contained in:
Bram Kragten 2023-05-25 17:58:05 +02:00 committed by GitHub
parent dacdc62672
commit a9c27ad8dd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 7 deletions

View File

@ -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);

View File

@ -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`