0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-02-01 11:59:17 -05:00

Adding themes

This commit is contained in:
Florian Schroedl 2024-09-19 15:44:56 +02:00
parent 7758e48c48
commit 743f61f2cd
8 changed files with 40 additions and 35 deletions

View file

@ -16,6 +16,10 @@
[cljs.analyzer.api :as aapi] [cljs.analyzer.api :as aapi]
[cuerdas.core :as str])) [cuerdas.core :as str]))
(defmacro legacy
"Purely annotational macro to find instances later to remove when the refactor to tokens-lib is done."
[& body] `(do ~@body))
(defmacro select-keys (defmacro select-keys
"A macro version of `select-keys`. Useful when keys vector is known "A macro version of `select-keys`. Useful when keys vector is known
at compile time (aprox 600% performance boost). at compile time (aprox 600% performance boost).

View file

@ -274,7 +274,7 @@
[:mod-token-theme [:mod-token-theme
[:map {:title "ModTokenThemeChange"} [:map {:title "ModTokenThemeChange"}
[:type [:= :mod-token-theme]] [:type [:= :mod-token-theme]]
[:id ::sm/uuid] (dm/legacy [:id {:optional true} [:maybe ::sm/uuid]])
[:name :string] [:name :string]
[:token-theme ::ctot/token-theme]]] [:token-theme ::ctot/token-theme]]]
@ -860,7 +860,6 @@
(defmethod process-change :add-token-theme (defmethod process-change :add-token-theme
[data {:keys [token-theme]}] [data {:keys [token-theme]}]
(-> data (-> data
(ctotl/add-token-theme token-theme)
(update :tokens-lib (update :tokens-lib
#(-> % #(-> %
(ctob/ensure-tokens-lib) (ctob/ensure-tokens-lib)
@ -871,7 +870,8 @@
(defmethod process-change :mod-token-theme (defmethod process-change :mod-token-theme
[data {:keys [id name group token-theme]}] [data {:keys [id name group token-theme]}]
(-> data (-> data
(ctotl/update-token-theme id merge token-theme) (dm/legacy (#(when id
(ctotl/update-token-theme % (random-uuid) merge token-theme))))
(update :tokens-lib (update :tokens-lib
#(-> % #(-> %
(ctob/ensure-tokens-lib) (ctob/ensure-tokens-lib)

View file

@ -713,14 +713,14 @@
[changes token-theme] [changes token-theme]
(-> changes (-> changes
(update :redo-changes conj {:type :add-token-theme :token-theme token-theme}) (update :redo-changes conj {:type :add-token-theme :token-theme token-theme})
(update :undo-changes conj {:type :del-token-theme :id (:id token-theme) :name (:name token-theme)}) ;; (legacy (update :undo-changes conj {:type :del-token-theme :name (:name token-theme)}))
(apply-changes-local))) (apply-changes-local)))
(defn update-token-theme (defn update-token-theme
[changes token-theme prev-token-theme] [changes token-theme prev-token-theme]
(-> changes (-> changes
(update :redo-changes conj {:type :mod-token-theme :id (:id token-theme) :name (:name prev-token-theme) :token-theme token-theme}) (update :redo-changes conj {:type :mod-token-theme :name (:name prev-token-theme) :token-theme token-theme})
(update :undo-changes conj {:type :mod-token-theme :id (:id token-theme) :name (:name token-theme) :token-theme (or prev-token-theme token-theme)}) (update :undo-changes conj {:type :mod-token-theme :name (:name token-theme) :token-theme (or prev-token-theme token-theme)})
(apply-changes-local))) (apply-changes-local)))
(defn delete-token-theme (defn delete-token-theme

View file

View file

@ -6,11 +6,12 @@
(ns app.common.types.token-theme (ns app.common.types.token-theme
(:require (:require
[app.common.data.macros :as dm]
[app.common.schema :as sm])) [app.common.schema :as sm]))
(sm/register! ::token-theme (sm/register! ::token-theme
[:map {:title "TokenTheme"} [:map {:title "TokenTheme"}
[:id ::sm/uuid] (dm/legacy [:id {:optional true} [:maybe ::sm/uuid]])
[:name :string] [:name :string]
[:group {:optional true} :string] [:group {:optional true} :string]
[:source? {:optional true} :boolean] [:source? {:optional true} :boolean]

View file

@ -11,6 +11,7 @@
[app.common.files.changes-builder :as pcb] [app.common.files.changes-builder :as pcb]
[app.common.geom.point :as gpt] [app.common.geom.point :as gpt]
[app.common.types.shape :as cts] [app.common.types.shape :as cts]
[app.common.types.tokens-lib :as ctob]
[app.common.uuid :as uuid] [app.common.uuid :as uuid]
[app.main.data.changes :as dch] [app.main.data.changes :as dch]
[app.main.data.workspace.shapes :as dwsh] [app.main.data.workspace.shapes :as dwsh]
@ -40,6 +41,13 @@
(watch [_ _ _] (watch [_ _ _]
(rx/of (dwsh/update-shapes [id] #(merge % attrs)))))) (rx/of (dwsh/update-shapes [id] #(merge % attrs))))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; TOKENS Getters
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defn get-tokens-lib [state]
(get-in state [:workspace-data :tokens-lib]))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; TOKENS Actions ;; TOKENS Actions
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
@ -98,11 +106,7 @@
(map #(get-in file [:tokens %]) (:tokens token-set))) (map #(get-in file [:tokens %]) (:tokens token-set)))
(defn create-token-theme [token-theme] (defn create-token-theme [token-theme]
(let [new-token-theme (merge (let [new-token-theme token-theme]
{:id (uuid/next)
:sets #{}
:selected :enabled}
token-theme)]
(ptk/reify ::create-token-theme (ptk/reify ::create-token-theme
ptk/WatchEvent ptk/WatchEvent
(watch [it _ _] (watch [it _ _]
@ -111,13 +115,13 @@
(rx/of (rx/of
(dch/commit-changes changes))))))) (dch/commit-changes changes)))))))
(defn update-token-theme [token-theme] (defn update-token-theme [[group name] token-theme]
(ptk/reify ::update-token-theme (ptk/reify ::update-token-theme
ptk/WatchEvent ptk/WatchEvent
(watch [it state _] (watch [it state _]
(let [prev-token-theme (wtts/get-workspace-token-theme (:id token-theme) state) (let [tokens-lib (get-tokens-lib state)
changes (-> (pcb/empty-changes it) prev-token-theme (some-> tokens-lib (ctob/get-theme group name))
(pcb/update-token-theme token-theme prev-token-theme))] changes (pcb/update-token-theme (pcb/empty-changes it) token-theme prev-token-theme)]
(rx/of (rx/of
(dch/commit-changes changes)))))) (dch/commit-changes changes))))))

View file

@ -1,5 +0,0 @@
(ns app.main.ui.workspace.tokens.macros)
(defmacro legacy
"Purely annotational macro to find instances later to remove when the refactor to tokens-lib is done."
[& body] `(do ~@body))

View file

@ -15,12 +15,11 @@
[app.main.ui.icons :as i] [app.main.ui.icons :as i]
[app.main.ui.workspace.tokens.common :refer [labeled-input] :as wtco] [app.main.ui.workspace.tokens.common :refer [labeled-input] :as wtco]
[app.main.ui.workspace.tokens.sets :as wts] [app.main.ui.workspace.tokens.sets :as wts]
[app.main.ui.workspace.tokens.sets-context :as sets-context]
[app.main.ui.workspace.tokens.token-set :as wtts] [app.main.ui.workspace.tokens.token-set :as wtts]
[app.util.dom :as dom] [app.util.dom :as dom]
[rumext.v2 :as mf]
[cuerdas.core :as str] [cuerdas.core :as str]
[app.main.ui.workspace.tokens.sets-context :as sets-context] [rumext.v2 :as mf]))
[app.main.ui.shapes.group :as group]))
(def ^:private chevron-icon (def ^:private chevron-icon
(i/icon-xref :arrow (stl/css :chevron-icon))) (i/icon-xref :arrow (stl/css :chevron-icon)))
@ -110,10 +109,8 @@
"Create theme"]]])) "Create theme"]]]))
(mf/defc edit-theme (mf/defc edit-theme
[{:keys [token-sets theme theme-groups on-back on-submit]}] [{:keys [edit? token-sets theme theme-groups on-back on-submit]}]
(let [{:keys [dropdown-open? on-open-dropdown on-close-dropdown on-toggle-dropdown]} (wtco/use-dropdown-open-state) (let [{:keys [dropdown-open? on-open-dropdown on-close-dropdown on-toggle-dropdown]} (wtco/use-dropdown-open-state)
edit? (some? (:id theme))
theme-state (mf/use-state {:token-sets token-sets theme-state (mf/use-state {:token-sets token-sets
:theme theme}) :theme theme})
disabled? (-> (get-in @theme-state [:theme :name]) disabled? (-> (get-in @theme-state [:theme :name])
@ -139,13 +136,15 @@
(fn [e] (fn [e]
(dom/prevent-default e) (dom/prevent-default e)
(let [theme (:theme @theme-state) (let [theme (:theme @theme-state)
final-name (str/trim (:name theme)) final-name (-> (:name theme)
final-group (-> (:group theme) (str/trim))
(str/trim) empty-description? (-> (:description theme)
(str/lower))] (str/trim)
(str/empty?))]
(when-not (str/empty? final-name) (when-not (str/empty? final-name)
(cond-> theme (cond-> theme
(empty final-group) (dissoc :group) empty-description? (assoc :description "")
:always (doto js/console.log)
:always on-submit))) :always on-submit)))
(on-back)))] (on-back)))]
[:form {:on-submit on-save-form} [:form {:on-submit on-save-form}
@ -219,11 +218,12 @@
theme (mf/deref (refs/workspace-token-theme theme-group theme-name)) theme (mf/deref (refs/workspace-token-theme theme-group theme-name))
theme-groups (mf/deref refs/workspace-token-theme-groups)] theme-groups (mf/deref refs/workspace-token-theme-groups)]
[:& edit-theme [:& edit-theme
{:token-sets token-sets {:edit? true
:token-sets token-sets
:theme theme :theme theme
:theme-groups theme-groups :theme-groups theme-groups
:on-back #(set-state (constantly {:type :themes-overview})) :on-back #(set-state (constantly {:type :themes-overview}))
:on-submit #(st/emit! (wdt/update-token-theme %))}])) :on-submit #(st/emit! (wdt/update-token-theme [(:group theme) (:name theme)] %))}]))
(mf/defc create-theme (mf/defc create-theme
[{:keys [set-state]}] [{:keys [set-state]}]
@ -231,7 +231,8 @@
theme {:name "" :sets #{}} theme {:name "" :sets #{}}
theme-groups (mf/deref refs/workspace-token-theme-groups)] theme-groups (mf/deref refs/workspace-token-theme-groups)]
[:& edit-theme [:& edit-theme
{:token-sets token-sets {:edit? false
:token-sets token-sets
:theme theme :theme theme
:theme-groups theme-groups :theme-groups theme-groups
:on-back #(set-state (constantly {:type :themes-overview})) :on-back #(set-state (constantly {:type :themes-overview}))