From b34c161fc35f95d9a95b893e80d7f49a3f42df9d Mon Sep 17 00:00:00 2001 From: "alonso.torres" Date: Mon, 9 Jun 2025 12:39:26 +0200 Subject: [PATCH] :sparkles: Adds local store proxy in plugins --- .../src/app/main/ui/workspace/plugins.cljs | 9 ++- frontend/src/app/plugins/api.cljs | 6 ++ frontend/src/app/plugins/local_storage.cljs | 71 +++++++++++++++++++ frontend/translations/en.po | 3 + frontend/translations/es.po | 3 + 5 files changed, 91 insertions(+), 1 deletion(-) create mode 100644 frontend/src/app/plugins/local_storage.cljs diff --git a/frontend/src/app/main/ui/workspace/plugins.cljs b/frontend/src/app/main/ui/workspace/plugins.cljs index 8179af0ce..8ea1e7649 100644 --- a/frontend/src/app/main/ui/workspace/plugins.cljs +++ b/frontend/src/app/main/ui/workspace/plugins.cljs @@ -275,7 +275,14 @@ [:div {:class (stl/css :permissions-list-entry)} i/oauth-1 [:p {:class (stl/css :permissions-list-text)} - (tr "workspace.plugins.permissions.allow-download")]])]) + (tr "workspace.plugins.permissions.allow-download")]]) + + (cond + (contains? permissions "allow:localstorage") + [:div {:class (stl/css :permissions-list-entry)} + i/oauth-1 + [:p {:class (stl/css :permissions-list-text)} + (tr "workspace.plugins.permissions.allow-localstorage")]])]) (mf/defc plugins-permissions-dialog {::mf/register modal/components diff --git a/frontend/src/app/plugins/api.cljs b/frontend/src/app/plugins/api.cljs index 5845994af..079677999 100644 --- a/frontend/src/app/plugins/api.cljs +++ b/frontend/src/app/plugins/api.cljs @@ -33,6 +33,7 @@ [app.plugins.format :as format] [app.plugins.history :as history] [app.plugins.library :as library] + [app.plugins.local-storage :as local-storage] [app.plugins.page :as page] [app.plugins.parser :as parser] [app.plugins.shape :as shape] @@ -85,6 +86,11 @@ {:this true :get #(.getTheme ^js %)} + :localStorage + {:this true + :get + (fn [_] (local-storage/local-storage-proxy plugin-id))} + :selection {:this true :get #(.getSelectedShapes ^js %) diff --git a/frontend/src/app/plugins/local_storage.cljs b/frontend/src/app/plugins/local_storage.cljs new file mode 100644 index 000000000..cac6529be --- /dev/null +++ b/frontend/src/app/plugins/local_storage.cljs @@ -0,0 +1,71 @@ +;; This Source Code Form is subject to the terms of the Mozilla Public +;; License, v. 2.0. If a copy of the MPL was not distributed with this +;; file, You can obtain one at http://mozilla.org/MPL/2.0/. +;; +;; Copyright (c) KALEIDOS INC + +(ns app.plugins.local-storage + (:require + [app.common.data.macros :as dm] + [app.common.exceptions :as ex] + [app.plugins.register :as r] + [app.plugins.utils :as u] + [app.util.globals :as g] + [app.util.object :as obj] + [cuerdas.core :as str])) + +(defonce ^:private local-storage + (ex/ignoring (unchecked-get g/global "localStorage"))) + +(defn prefix-key + [plugin-id key] + (dm/str "penpot-plugins:" plugin-id "/" key)) + +(defn local-storage-proxy + [plugin-id] + (obj/reify {:name "LocalStorageProxy"} + :$plugin {:enumerable false :get (fn [] plugin-id)} + + :getItem + (fn [key] + (cond + (not (r/check-permission plugin-id "allow:localstorage")) + (u/display-not-valid :getItem "Plugin doesn't have 'allow:localstorage' permission") + + (not (string? key)) + (u/display-not-valid :getItem "The key must be a string") + + :else + (.getItem ^js local-storage (prefix-key plugin-id key)))) + + :setItem + (fn [key value] + (cond + (not (r/check-permission plugin-id "allow:localstorage")) + (u/display-not-valid :setItem "Plugin doesn't have 'allow:localstorage' permission") + + (not (string? key)) + (u/display-not-valid :setItem "The key must be a string") + + :else + (.setItem ^js local-storage (prefix-key plugin-id key) value))) + + :removeItem + (fn [key] + (cond + (not (r/check-permission plugin-id "allow:localstorage")) + (u/display-not-valid :removeItem "Plugin doesn't have 'allow:localstorage' permission") + + (not (string? key)) + (u/display-not-valid :removeItem "The key must be a string") + + :else + (.getItem ^js local-storage (prefix-key plugin-id key)))) + + :getKeys + (fn [] + (->> (.keys js/Object local-storage) + (filter #(str/starts-with? % (prefix-key plugin-id ""))) + (map #(str/replace % (prefix-key plugin-id "") "")) + (apply array))))) + diff --git a/frontend/translations/en.po b/frontend/translations/en.po index 9aaffe059..45126a98d 100644 --- a/frontend/translations/en.po +++ b/frontend/translations/en.po @@ -6670,6 +6670,9 @@ msgstr "" msgid "workspace.plugins.permissions.allow-download" msgstr "Start file downloads." +msgid "workspace.plugins.permissions.allow-localstorage" +msgstr "Store data in the browser." + #: src/app/main/ui/workspace/plugins.cljs:271 msgid "workspace.plugins.permissions.comment-read" msgstr "Read your comments and replies." diff --git a/frontend/translations/es.po b/frontend/translations/es.po index 06a5b77d1..fbe76c8a4 100644 --- a/frontend/translations/es.po +++ b/frontend/translations/es.po @@ -6696,6 +6696,9 @@ msgstr "" msgid "workspace.plugins.permissions.allow-download" msgstr "Comenzar descargas de ficheros." +msgid "workspace.plugins.permissions.allow-localstorage" +msgstr "Guardar datos en el navegador." + #: src/app/main/ui/workspace/plugins.cljs:271 msgid "workspace.plugins.permissions.comment-read" msgstr "Leer tus comentarios y respuestas."