Adds local store proxy in plugins

This commit is contained in:
alonso.torres 2025-06-09 12:39:26 +02:00
parent ed0c84a069
commit b34c161fc3
5 changed files with 91 additions and 1 deletions

View file

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

View file

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

View file

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

View file

@ -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."

View file

@ -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."