♻️ Extract the token group's pill click and open sections (#12795)

Move what the token group works out into pure functions with tests:
whether a type's section is open, whether a text is being edited, and
what clicking a token pill does. Nothing changes in behavior.

Signed-off-by: Florian Schroedl <flo.schroedl@gmail.com>
This commit is contained in:
Florian Schrödl 2026-11-16 16:14:57 +01:00 committed by GitHub
parent d2dfc09495
commit 699f79c693
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 115 additions and 21 deletions

View File

@ -57,6 +57,31 @@
:shadow i/drop-shadow
"add"))
(defn- type-unfolded?
"A token type's section only stays open for the file and set it was
opened in, so switching sets doesn't carry open sections over."
[unfolded-token-types file-id set-id type]
(and (= (:file-id unfolded-token-types) file-id)
(= (:set-id unfolded-token-types) set-id)
(contains? (set (:types unfolded-token-types)) type)))
(defn- editing-text?
[editor-state edition objects]
(or (not (empty? editor-state))
(and (some? edition)
(= :text (:type (get objects edition))))))
(defn- pill-click-action
"Clicking a token pill applies or removes the token on the selection,
or warns while a text is being edited. Number tokens and an empty
selection do nothing."
[token selected-shapes is-editing-text]
(cond
(= :number (:type token)) nil
(empty? selected-shapes) nil
is-editing-text :warn-text-edition
:else :toggle))
(def ^:private schema:token-group
[:map
[:type :keyword]
@ -80,17 +105,13 @@
current-file (mf/deref refs/file)
is-same-file-set? (and (= (:file-id unfolded-token-types-state) (:id current-file))
(= (:set-id unfolded-token-types-state) selected-token-set-id))
is-type-unfolded (and is-same-file-set?
(contains? (set (:types unfolded-token-types-state)) type))
is-type-unfolded
(type-unfolded? unfolded-token-types-state (:id current-file) selected-token-set-id type)
editing-ref (mf/deref refs/workspace-editor-state)
edition (mf/deref refs/selected-edition)
objects (mf/deref refs/workspace-page-objects)
not-editing? (and (empty? editing-ref)
(not (and (some? edition)
(= :text (:type (get objects edition))))))
editing-ref (mf/deref refs/workspace-editor-state)
edition (mf/deref refs/selected-edition)
objects (mf/deref refs/workspace-page-objects)
is-editing-text (editing-text? editing-ref edition objects)
can-edit-tokens?
(mf/use-ctx ctx/can-edit-tokens?)
@ -151,20 +172,22 @@
on-token-pill-click
(mf/use-fn
(mf/deps not-editing? selected-ids tokens-lib selected-token-set-id)
(mf/deps is-editing-text selected-shapes selected-ids tokens-lib selected-token-set-id)
(fn [event token]
(let [token (ctob/get-token tokens-lib selected-token-set-id (:id token))]
(dom/stop-propagation event)
;; Number tokens can't be applied via button click
(when (not= (:type token) :number)
(if (and not-editing? (seq selected-shapes))
(st/emit! (dwta/toggle-token {:token token
:shape-ids selected-ids}))
(when (seq selected-shapes)
(st/emit! (ntf/show {:content (tr "workspace.tokens.error-text-edition")
:type :toast
:level :warning
:timeout 3000}))))))))]
(case (pill-click-action token selected-shapes is-editing-text)
:toggle
(st/emit! (dwta/toggle-token {:token token
:shape-ids selected-ids}))
:warn-text-edition
(st/emit! (ntf/show {:content (tr "workspace.tokens.error-text-edition")
:type :toast
:level :warning
:timeout 3000}))
nil))))]
(mf/use-effect
(fn []

View File

@ -140,6 +140,7 @@
[frontend-tests.tokens.shadow-form-test]
[frontend-tests.tokens.style-dictionary-test]
[frontend-tests.tokens.token-errors-test]
[frontend-tests.tokens.token-group-test]
[frontend-tests.tokens.token-parsing-test]
[frontend-tests.tokens.token-path-test]
[frontend-tests.tokens.token-pill-test]
@ -356,6 +357,7 @@
'frontend-tests.tokens.shadow-form-test
'frontend-tests.tokens.style-dictionary-test
'frontend-tests.tokens.token-errors-test
'frontend-tests.tokens.token-group-test
'frontend-tests.tokens.token-parsing-test
'frontend-tests.tokens.token-path-test
'frontend-tests.tokens.token-pill-test

View File

@ -0,0 +1,69 @@
;; 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 SUBSIDIARY SL
(ns frontend-tests.tokens.token-group-test
(:require
[app.common.test-helpers.ids-map :as thi]
[app.common.test-helpers.shapes :as ths]
[app.common.types.tokens-lib :as ctob]
[app.main.data.workspace.tokens.library-edit :as dwtl]
[app.main.ui.workspace.tokens.management.group :as wtg]
[cljs.test :as t :include-macros true]))
(t/use-fixtures :each thi/test-fixture)
(def ^:private type-unfolded? @#'wtg/type-unfolded?)
(def ^:private editing-text? @#'wtg/editing-text?)
(def ^:private pill-click-action @#'wtg/pill-click-action)
(def ^:private unfolded-token-types @#'dwtl/make-unfolded-token-types-state)
(def ^:private radius
(ctob/make-token :name "radius.md" :type :border-radius :value "8"))
(def ^:private number
(ctob/make-token :name "scale.md" :type :number :value "2"))
(t/deftest section-unfolded
(let [unfolded (unfolded-token-types (thi/new-id! :file1) (thi/new-id! :set1) #{:color})]
(t/testing "open in the file and set it was opened in"
(t/is (type-unfolded? unfolded (thi/id :file1) (thi/id :set1) :color)))
(t/testing "closed in another set or file"
(t/is (not (type-unfolded? unfolded (thi/id :file1) (thi/new-id! :set2) :color)))
(t/is (not (type-unfolded? unfolded (thi/new-id! :file2) (thi/id :set1) :color))))
(t/testing "other types stay closed"
(t/is (not (type-unfolded? unfolded (thi/id :file1) (thi/id :set1) :spacing))))))
(t/deftest text-edition
(let [text (ths/sample-shape :text1 :type :text)
rect (ths/sample-shape :rect1)
objects {(:id text) text (:id rect) rect}]
(t/testing "nothing is being edited"
(t/is (not (editing-text? nil nil objects))))
(t/testing "a text is being edited"
(t/is (editing-text? nil (:id text) objects))
(t/is (editing-text? {(:id text) :editor-state} nil objects)))
(t/testing "editing another shape isn't text edition"
(t/is (not (editing-text? nil (:id rect) objects))))))
(t/deftest pill-click
(let [selection [(ths/sample-shape :rect1)]]
(t/testing "applies or removes the token on the selection"
(t/is (= :toggle (pill-click-action radius selection false))))
(t/testing "warns while a text is being edited"
(t/is (= :warn-text-edition (pill-click-action radius selection true))))
(t/testing "does nothing without a selection"
(t/is (nil? (pill-click-action radius [] false)))
(t/is (nil? (pill-click-action radius [] true))))
(t/testing "number tokens can't be applied with a click"
(t/is (nil? (pill-click-action number selection false)))
(t/is (nil? (pill-click-action number selection true))))))