🔧 Move token lib edit to workspace.data and remove unused code

This commit is contained in:
Andrés Moya 2025-04-25 12:49:31 +02:00 committed by Andrés Moya
parent 213c04bc8a
commit a1921bb767
12 changed files with 81 additions and 185 deletions

View file

@ -4,7 +4,7 @@
;;
;; Copyright (c) KALEIDOS INC
(ns app.main.data.tokens
(ns app.main.data.workspace.tokens.library-edit
(:require
[app.common.data.macros :as dm]
[app.common.files.changes-builder :as pcb]

View file

@ -1,70 +0,0 @@
;; 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.main.ui.workspace.tokens.common
(:require
[app.common.data :as d]
[app.main.data.shortcuts :as dsc]
[app.main.store :as st]
[app.util.dom :as dom]
[app.util.globals :as globals]
[app.util.keyboard :as kbd]
[cuerdas.core :as str]
[goog.events :as events]
[rumext.v2 :as mf])
(:import goog.events.EventType))
;; Helpers ---------------------------------------------------------------------
(defn camel-keys [m]
(->> m
(d/deep-mapm
(fn [[k v]]
(if (or (keyword? k) (string? k))
[(keyword (str/camel (name k))) v]
[k v])))))
(defn direction-select
"Returns next `n` in `direction` while wrapping around at the last item at the count of `coll`.
`direction` accepts `:up` or `:down`."
[direction n coll]
(let [last-n (dec (count coll))
next-n (case direction
:up (dec n)
:down (inc n))
wrap-around-n (cond
(neg? next-n) last-n
(> next-n last-n) 0
:else next-n)]
wrap-around-n))
(defn use-arrow-highlight [{:keys [shortcuts-key options on-select]}]
(let [highlighted* (mf/use-state nil)
highlighted (deref highlighted*)
on-dehighlight #(reset! highlighted* nil)
on-keyup (fn [event]
(cond
(and (kbd/enter? event) highlighted) (on-select (nth options highlighted))
(kbd/up-arrow? event) (do
(dom/prevent-default event)
(->> (direction-select :up (or highlighted 0) options)
(reset! highlighted*)))
(kbd/down-arrow? event) (do
(dom/prevent-default event)
(->> (direction-select :down (or highlighted -1) options)
(reset! highlighted*)))))]
(mf/with-effect [highlighted]
(let [shortcuts-key shortcuts-key
keys [(events/listen globals/document EventType.KEYUP on-keyup)
(events/listen globals/document EventType.KEYDOWN dom/prevent-default)]]
(st/emit! (dsc/push-shortcuts shortcuts-key {}))
(fn []
(doseq [key keys]
(events/unlistenByKey key))
(st/emit! (dsc/pop-shortcuts shortcuts-key)))))
{:highlighted highlighted
:on-dehighlight on-dehighlight}))

View file

@ -11,9 +11,9 @@
[app.common.data.macros :as dm]
[app.common.types.tokens-lib :as ctob]
[app.main.data.modal :as modal]
[app.main.data.tokens :as dt]
[app.main.data.workspace.shape-layout :as dwsl]
[app.main.data.workspace.tokens.application :as dwta]
[app.main.data.workspace.tokens.library-edit :as dwtl]
[app.main.refs :as refs]
[app.main.store :as st]
[app.main.ui.components.dropdown :refer [dropdown]]
@ -264,7 +264,7 @@
:action (fn [event]
(let [{:keys [key fields]} modal]
(dom/stop-propagation event)
(st/emit! (dt/assign-token-context-menu nil)
(st/emit! (dwtl/assign-token-context-menu nil)
(modal/show key {:x (.-clientX ^js event)
:y (.-clientY ^js event)
:position :right
@ -274,10 +274,10 @@
:token token}))))}
{:title (tr "workspace.token.duplicate")
:no-selectable true
:action #(st/emit! (dt/duplicate-token (:name token)))}
:action #(st/emit! (dwtl/duplicate-token (:name token)))}
{:title (tr "workspace.token.delete")
:no-selectable true
:action #(st/emit! (dt/delete-token
:action #(st/emit! (dwtl/delete-token
(ctob/prefixed-set-path-string->set-name-string selected-token-set-name)
(:name token)))}]))
@ -446,7 +446,7 @@
(mf/portal
(mf/html
[:& dropdown {:show is-open?
:on-close #(st/emit! (dt/assign-token-context-menu nil))}
:on-close #(st/emit! (dwtl/assign-token-context-menu nil))}
[:div {:class (stl/css :token-context-menu)
:data-testid "tokens-context-menu-for-token"
:ref dropdown-ref

View file

@ -1,34 +0,0 @@
;; 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.main.ui.workspace.tokens.core
(:require
[app.common.data :as d]
[app.main.ui.workspace.tokens.token :as wtt]))
;; Helpers ---------------------------------------------------------------------
(defn resolve-token-value [{:keys [value resolved-value] :as _token}]
(or
resolved-value
(d/parse-double value)))
(defn maybe-resolve-token-value [{:keys [value] :as token}]
(when value (resolve-token-value token)))
(defn tokens->select-options [{:keys [shape tokens attributes selected-attributes]}]
(map
(fn [{:keys [name] :as token}]
(cond-> (assoc token :label name)
(wtt/token-applied? token shape (or selected-attributes attributes)) (assoc :selected? true)))
tokens))
(defn tokens-name-map->select-options [{:keys [shape tokens attributes selected-attributes]}]
(map
(fn [[_k {:keys [name] :as token}]]
(cond-> (assoc token :label name)
(wtt/token-applied? token shape (or selected-attributes attributes)) (assoc :selected? true)))
tokens))

View file

@ -12,8 +12,8 @@
[app.common.data.macros :as dm]
[app.common.types.tokens-lib :as ctob]
[app.main.data.modal :as modal]
[app.main.data.tokens :as dt]
[app.main.data.workspace.tokens.application :as dwta]
[app.main.data.workspace.tokens.library-edit :as dwtl]
[app.main.refs :as refs]
[app.main.store :as st]
[app.main.ui.ds.buttons.button :refer [button*]]
@ -452,15 +452,15 @@
(when (and (seq result) (not err))
(st/emit!
(if (ctob/token? token)
(dt/update-token (:name token)
{:name final-name
:value final-value
:description final-description})
(dwtl/update-token (:name token)
{:name final-name
:value final-value
:description final-description})
(dt/create-token {:name final-name
:type token-type
:value final-value
:description final-description}))
(dwtl/create-token {:name final-name
:type token-type
:value final-value
:description final-description}))
(wtu/update-workspace-tokens)
(modal/hide)))))))))
@ -470,7 +470,7 @@
(fn [e]
(dom/prevent-default e)
(modal/hide!)
(st/emit! (dt/delete-token (ctob/prefixed-set-path-string->set-name-string selected-token-set-name) (:name token)))))
(st/emit! (dwtl/delete-token (ctob/prefixed-set-path-string->set-name-string selected-token-set-name) (:name token)))))
on-cancel
(mf/use-fn

View file

@ -12,7 +12,7 @@
[app.common.types.tokens-lib :as ctob]
[app.main.data.event :as ev]
[app.main.data.modal :as modal]
[app.main.data.tokens :as wdt]
[app.main.data.workspace.tokens.library-edit :as dwtl]
[app.main.refs :as refs]
[app.main.store :as st]
[app.main.ui.components.radio-buttons :refer [radio-button radio-buttons]]
@ -117,7 +117,7 @@
(fn [e]
(dom/prevent-default e)
(dom/stop-propagation e)
(st/emit! (wdt/delete-token-theme group name)))
(st/emit! (dwtl/delete-token-theme group name)))
on-edit-theme
(fn [e]
(dom/prevent-default e)
@ -131,7 +131,7 @@
[:div {:on-click (fn [e]
(dom/prevent-default e)
(dom/stop-propagation e)
(st/emit! (wdt/toggle-token-theme-active? group name)))}
(st/emit! (dwtl/toggle-token-theme-active? group name)))}
[:& switch {:name (tr "workspace.token.theme-name" name)
:on-change (constantly nil)
:selected? selected?}]]]
@ -292,7 +292,7 @@
(mf/use-fn
(mf/deps current-theme on-back)
(fn []
(st/emit! (wdt/delete-token-theme (:group current-theme) (:name current-theme)))
(st/emit! (dwtl/delete-token-theme (:group current-theme) (:name current-theme)))
(on-back)))
;; Sets tree handlers
@ -386,7 +386,7 @@
(mf/use-fn
(mf/deps theme)
(fn [theme']
(st/emit! (wdt/update-token-theme [(:group theme) (:name theme)] theme'))))]
(st/emit! (dwtl/update-token-theme [(:group theme) (:name theme)] theme'))))]
[:> edit-create-theme*
{:change-view change-view
@ -402,7 +402,7 @@
(mf/use-fn
(fn [theme]
(st/emit! (ptk/event ::ev/event {::ev/name "create-tokens-theme"})
(wdt/create-token-theme theme))))
(dwtl/create-token-theme theme))))
has-prev-view (has-prev-view (:prev-type state))]
[:> edit-create-theme*

View file

@ -10,7 +10,7 @@
[app.common.data.macros :as dm]
[app.common.types.tokens-lib :as ctob]
[app.main.data.event :as ev]
[app.main.data.tokens :as dt]
[app.main.data.workspace.tokens.library-edit :as dwtl]
[app.main.refs :as refs]
[app.main.store :as st]
[app.main.ui.context :as ctx]
@ -31,26 +31,26 @@
(defn- on-start-creation
[]
(st/emit! (dt/start-token-set-creation [])))
(st/emit! (dwtl/start-token-set-creation [])))
(defn- on-toggle-token-set-click [name]
(st/emit! (dt/toggle-token-set name)))
(st/emit! (dwtl/toggle-token-set name)))
(defn- on-toggle-token-set-group-click [path]
(st/emit! (dt/toggle-token-set-group path)))
(st/emit! (dwtl/toggle-token-set-group path)))
(defn- on-select-token-set-click [name]
(st/emit! (dt/set-selected-token-set-name name)))
(st/emit! (dwtl/set-selected-token-set-name name)))
(defn on-update-token-set
[token-set name]
(st/emit! (dt/clear-token-set-edition)
(dt/update-token-set token-set name)))
(st/emit! (dwtl/clear-token-set-edition)
(dwtl/update-token-set token-set name)))
(defn- on-update-token-set-group
[path name]
(st/emit! (dt/clear-token-set-edition)
(dt/rename-token-set-group path name)))
(st/emit! (dwtl/clear-token-set-edition)
(dwtl/rename-token-set-group path name)))
(defn- on-create-token-set
[parent-set name]
@ -63,7 +63,7 @@
(ctob/normalize-set-name name))]
(st/emit! (ptk/data-event ::ev/event {::ev/name "create-token-set" :name name})
(dt/create-token-set name))))
(dwtl/create-token-set name))))
(defn group-edition-id
"Prefix editing groups `edition-id` so it can be differentiated from sets with the same id."
@ -167,7 +167,7 @@
(dom/prevent-default event)
(dom/stop-propagation event)
(when (and can-edit? (not is-editing))
(st/emit! (dt/assign-token-set-context-menu
(st/emit! (dwtl/assign-token-set-context-menu
{:position (dom/get-client-position event)
:is-group true
:id id
@ -270,7 +270,7 @@
(dom/prevent-default event)
(dom/stop-propagation event)
(when (and can-edit? (not is-editing))
(st/emit! (dt/assign-token-set-context-menu
(st/emit! (dwtl/assign-token-set-context-menu
{:position (dom/get-client-position event)
:is-group false
:id id
@ -383,8 +383,8 @@
:position position
:collapsed-paths collapsed-paths}]
(if (:is-group data)
(st/emit! (dt/drop-token-set-group params))
(st/emit! (dt/drop-token-set params))))))
(st/emit! (dwtl/drop-token-set-group params))
(st/emit! (dwtl/drop-token-set params))))))
on-toggle-collapse
(mf/use-fn
@ -560,15 +560,15 @@
(mf/deps can-edit?)
(fn [_]
(when can-edit?
(st/emit! (dt/clear-token-set-edition)
(dt/clear-token-set-creation)))))
(st/emit! (dwtl/clear-token-set-edition)
(dwtl/clear-token-set-creation)))))
on-start-edition
(mf/use-fn
(mf/deps can-edit?)
(fn [id]
(when can-edit?
(st/emit! (dt/start-token-set-edition id)))))]
(st/emit! (dwtl/start-token-set-edition id)))))]
[:> controlled-sets-list*
{:token-sets token-sets

View file

@ -8,7 +8,7 @@
(:require-macros [app.main.style :as stl])
(:require
[app.common.data.macros :as dm]
[app.main.data.tokens :as dt]
[app.main.data.workspace.tokens.library-edit :as dwtl]
[app.main.refs :as refs]
[app.main.store :as st]
[app.main.ui.components.dropdown :refer [dropdown]]
@ -36,24 +36,24 @@
{::mf/private true}
[{:keys [is-group id edition-id path]}]
(let [create-set-at-path
(mf/use-fn (mf/deps path) #(st/emit! (dt/start-token-set-creation path)))
(mf/use-fn (mf/deps path) #(st/emit! (dwtl/start-token-set-creation path)))
on-edit
(mf/use-fn
(mf/deps id)
(fn []
(st/emit! (dt/start-token-set-edition edition-id))))
(st/emit! (dwtl/start-token-set-edition edition-id))))
on-duplicate
(mf/use-fn
(mf/deps is-group id)
(fn []
(st/emit! (dt/duplicate-token-set id is-group))))
(st/emit! (dwtl/duplicate-token-set id is-group))))
on-delete
(mf/use-fn
(mf/deps is-group path)
#(st/emit! (dt/delete-token-set-path is-group path)))]
#(st/emit! (dwtl/delete-token-set-path is-group path)))]
[:ul {:class (stl/css :context-list)}
(when is-group
@ -75,7 +75,7 @@
(+ (dm/get-prop position :x) 5)
on-close
(mf/use-fn #(st/emit! (dt/assign-token-set-context-menu nil)))]
(mf/use-fn #(st/emit! (dwtl/assign-token-set-context-menu nil)))]
[:& dropdown {:show (some? position)
:on-close on-close}

View file

@ -13,8 +13,8 @@
[app.main.data.event :as ev]
[app.main.data.modal :as modal]
[app.main.data.notifications :as ntf]
[app.main.data.tokens :as dt]
[app.main.data.workspace.tokens.application :as dwta]
[app.main.data.workspace.tokens.library-edit :as dwtl]
[app.main.refs :as refs]
[app.main.store :as st]
[app.main.ui.components.dropdown-menu :refer [dropdown-menu dropdown-menu-item*]]
@ -83,7 +83,7 @@
(mf/use-fn
(fn [event token]
(dom/prevent-default event)
(st/emit! (dt/assign-token-context-menu
(st/emit! (dwtl/assign-token-context-menu
{:type :token
:position (dom/get-client-position event)
:errors (:errors token)
@ -92,14 +92,14 @@
on-toggle-open-click
(mf/use-fn
(mf/deps is-open type)
#(st/emit! (dt/set-token-type-section-open type (not is-open))))
#(st/emit! (dwtl/set-token-type-section-open type (not is-open))))
on-popover-open-click
(mf/use-fn
(mf/deps type title modal)
(fn [event]
(dom/stop-propagation event)
(st/emit! (dt/set-token-type-section-open type true)
(st/emit! (dwtl/set-token-type-section-open type true)
;; FIXME: use dom/get-client-position
(modal/show (:key modal)
{:x (.-clientX ^js event)
@ -325,7 +325,7 @@
(let [match (->> (ctob/get-sets tokens-lib)
(first)
(:name))]
(st/emit! (dt/set-selected-token-set-name match)))))
(st/emit! (dwtl/set-selected-token-set-name match)))))
[:*
[:& token-context-menu]
@ -394,7 +394,7 @@
(sd/process-json-stream {:file-name file-name})
(rx/subs! (fn [lib]
(st/emit! (ptk/data-event ::ev/event {::ev/name "import-tokens"})
(dt/import-tokens-lib lib)))
(dwtl/import-tokens-lib lib)))
(fn [err]
(js/console.error err)
(st/emit! (ntf/show {:content (wte/humanize-errors [(ex-data err)])

View file

@ -11,7 +11,7 @@
[app.common.types.tokens-lib :as ctob]
[app.common.uuid :as uuid]
[app.main.data.modal :as modal]
[app.main.data.tokens :as wdt]
[app.main.data.workspace.tokens.library-edit :as dwtl]
[app.main.refs :as refs]
[app.main.store :as st]
[app.main.ui.components.dropdown :refer [dropdown]]
@ -31,7 +31,7 @@
selected? (get active-theme-paths theme-id)
select-theme (fn [e]
(dom/stop-propagation e)
(st/emit! (wdt/toggle-token-theme-active? group name))
(st/emit! (dwtl/toggle-token-theme-active? group name))
(on-close))]]
[:li {:key theme-id
:role "option"

View file

@ -15,10 +15,10 @@
[app.common.test-helpers.tokens :as ctht]
[app.common.types.tokens-lib :as ctob]
[app.main.data.helpers :as dsh]
[app.main.data.tokens :as dt]
[app.main.data.workspace.libraries :as dwl]
[app.main.data.workspace.selection :as dws]
[app.main.data.workspace.tokens.application :as dwta]
[app.main.data.workspace.tokens.library-edit :as dwtl]
[app.main.ui.workspace.tokens.update :as wtu]
[cljs.test :as t :include-macros true]
[frontend-tests.helpers.pages :as thp]
@ -203,11 +203,11 @@
store (ths/setup-store file)
;; ==== Action
events [(dt/set-selected-token-set-name "test-token-set")
(dt/update-token "test-token-1"
{:name "test-token-1"
:type :border-radius
:value 66})]
events [(dwtl/set-selected-token-set-name "test-token-set")
(dwtl/update-token "test-token-1"
{:name "test-token-1"
:type :border-radius
:value 66})]
step2 (fn [_]
(let [events2 [(wtu/update-workspace-tokens)
@ -359,25 +359,25 @@
store (ths/setup-store file)
;; ==== Action
events [(dt/set-selected-token-set-name "test-token-set")
(dt/update-token "token-radius"
{:name "token-radius"
:value 30})
(dt/update-token "token-rotation"
{:name "token-rotation"
:value 45})
(dt/update-token "token-opacity"
{:name "token-opacity"
:value 0.9})
(dt/update-token "token-stroke-width"
{:name "token-stroke-width"
:value 8})
(dt/update-token "token-color"
{:name "token-color"
:value "#ff0000"})
(dt/update-token "token-dimensions"
{:name "token-dimensions"
:value 200})]
events [(dwtl/set-selected-token-set-name "test-token-set")
(dwtl/update-token "token-radius"
{:name "token-radius"
:value 30})
(dwtl/update-token "token-rotation"
{:name "token-rotation"
:value 45})
(dwtl/update-token "token-opacity"
{:name "token-opacity"
:value 0.9})
(dwtl/update-token "token-stroke-width"
{:name "token-stroke-width"
:value 8})
(dwtl/update-token "token-color"
{:name "token-color"
:value "#ff0000"})
(dwtl/update-token "token-dimensions"
{:name "token-dimensions"
:value 200})]
step2 (fn [_]
(let [events2 [(wtu/update-workspace-tokens)

View file

@ -2,7 +2,7 @@
(:require
[app.common.test-helpers.files :as cthf]
[app.common.types.tokens-lib :as ctob]
[app.main.data.tokens :as dt]
[app.main.data.workspace.tokens.library-edit :as dwtl]
[cljs.test :as t :include-macros true]
[frontend-tests.helpers.pages :as thp]
[frontend-tests.helpers.state :as ths]
@ -27,7 +27,7 @@
done
(let [file (setup-file-with-token-lib)
store (ths/setup-store file)
events [(dt/duplicate-token-set "Set A" false)]]
events [(dwtl/duplicate-token-set "Set A" false)]]
(tohs/run-store-async
store done events
@ -46,7 +46,7 @@
done
(let [file (setup-file-with-token-lib)
store (ths/setup-store file)
events [(dt/duplicate-token-set "Set B" false)]]
events [(dwtl/duplicate-token-set "Set B" false)]]
(tohs/run-store-async
store done events