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; type Callback = (oldValue: any, newValue: any) => void;
class Storage { class Storage {
constructor(subscribe = true) { constructor(subscribe = true, storage = window.localStorage) {
this.storage = storage;
if (!subscribe) { if (!subscribe) {
return; return;
} }
@ -26,6 +27,8 @@ class Storage {
}); });
} }
public storage: globalThis.Storage;
private _storage: { [storageKey: string]: any } = {}; private _storage: { [storageKey: string]: any } = {};
private _listeners: { private _listeners: {
@ -34,7 +37,7 @@ class Storage {
public addFromStorage(storageKey: any): void { public addFromStorage(storageKey: any): void {
if (!this._storage[storageKey]) { if (!this._storage[storageKey]) {
const data = window.localStorage.getItem(storageKey); const data = this.storage.getItem(storageKey);
if (data) { if (data) {
this._storage[storageKey] = JSON.parse(data); this._storage[storageKey] = JSON.parse(data);
} }
@ -77,9 +80,9 @@ class Storage {
this._storage[storageKey] = value; this._storage[storageKey] = value;
try { try {
if (value === undefined) { if (value === undefined) {
window.localStorage.removeItem(storageKey); this.storage.removeItem(storageKey);
} else { } else {
window.localStorage.setItem(storageKey, JSON.stringify(value)); this.storage.setItem(storageKey, JSON.stringify(value));
} }
} catch (err: any) { } catch (err: any) {
// Safari in private mode doesn't allow localstorage // Safari in private mode doesn't allow localstorage
@ -94,10 +97,14 @@ export const LocalStorage =
storageKey?: string, storageKey?: string,
property?: boolean, property?: boolean,
subscribe = true, subscribe = true,
storageType?: globalThis.Storage,
propertyOptions?: PropertyDeclaration propertyOptions?: PropertyDeclaration
): any => ): any =>
(clsElement: ClassElement) => { (clsElement: ClassElement) => {
const storage = subscribe ? subscribeStorage : new Storage(false); const storage =
subscribe && !storageType
? subscribeStorage
: new Storage(subscribe, storageType);
const key = String(clsElement.key); const key = String(clsElement.key);
storageKey = storageKey || 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 { mdiHelpCircle } from "@mdi/js";
import { HassEntity } from "home-assistant-js-websocket"; import { HassEntity } from "home-assistant-js-websocket";
import { css, CSSResultGroup, html, LitElement } from "lit"; 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 deepClone from "deep-clone-simple";
import { fireEvent } from "../../../common/dom/fire_event"; import { fireEvent } from "../../../common/dom/fire_event";
import "../../../components/ha-card"; import "../../../components/ha-card";
@ -20,6 +20,7 @@ import { documentationUrl } from "../../../util/documentation-url";
import "./action/ha-automation-action"; import "./action/ha-automation-action";
import "./condition/ha-automation-condition"; import "./condition/ha-automation-condition";
import "./trigger/ha-automation-trigger"; import "./trigger/ha-automation-trigger";
import { LocalStorage } from "../../../common/decorators/local-storage";
@customElement("manual-automation-editor") @customElement("manual-automation-editor")
export class HaManualAutomationEditor extends LitElement { export class HaManualAutomationEditor extends LitElement {
@ -35,7 +36,8 @@ export class HaManualAutomationEditor extends LitElement {
@property({ attribute: false }) public stateObj?: HassEntity; @property({ attribute: false }) public stateObj?: HassEntity;
@state() private _clipboard: Clipboard = {}; @LocalStorage("automationClipboard", true, false, window.sessionStorage)
private _clipboard: Clipboard = {};
protected render() { protected render() {
return html` return html`