diff --git a/frontend/src/app/main/data/workspace/tokens/application.cljs b/frontend/src/app/main/data/workspace/tokens/application.cljs index 3c8dfe7b99..cc76192a18 100644 --- a/frontend/src/app/main/data/workspace/tokens/application.cljs +++ b/frontend/src/app/main/data/workspace/tokens/application.cljs @@ -775,7 +775,7 @@ Splits out `shape-ids` into separate default actions: - Layouts take the `default` update function - Shapes inside layout will only take margin" - [{:keys [token shapes attr]}] + [{:keys [token shapes]}] (ptk/reify ::apply-spacing-token-separated ptk/WatchEvent (watch [it state _] @@ -788,7 +788,7 @@ (group-by #(if (ctsl/any-layout-immediate-child? objects %) :frame-children :other) shapes)] (rx/of - (-> (apply-token {:attributes (or attr attributes) + (-> (apply-token {:attributes attributes :token token :shape-ids (map :id other) :on-update-shape on-update-shape}) @@ -829,113 +829,87 @@ (update shape :applied-tokens #(when % (apply dissoc % attributes))))))))) +(defn- target-shapes + "The selected shapes a token goes on. A selected group can stand for + its children, since groups take no tokens." + [objects shape-ids expand-with-children] + (cond->> (into [] (keep (d/getf objects)) shape-ids) + expand-with-children + (into [] (mapcat (fn [shape] + (if (= (:type shape) :group) + (keep objects (:shapes shape)) + [shape])))))) + +(defn- apply-action + "Works out which attributes the token goes on: the chosen ones, or its + type's defaults. A spacing token without chosen attributes goes on + layouts, and only on the margins of the shapes inside them." + [token attrs] + (let [{:keys [attributes on-update-shape]} (get token-properties (:type token))] + (if (and (= (:type token) :spacing) (nil? attrs)) + {:action :apply-spacing} + {:action :apply + :attributes (if (empty? attrs) attributes attrs) + :on-update-shape (or (get attr->shape-update (first attrs)) on-update-shape)}))) + +(defn- toggle-action + "Works out whether toggling the token removes it from the selection + or applies it, and on which attributes. From the token pill it comes + off when any shape has it; on chosen attributes (a design tab input, + a plugin) only when every shape has it on all of them, so a partial + application is completed instead." + [token attrs shapes] + (let [{:keys [attributes all-attributes]} (get token-properties (:type token)) + target-attrs (or attrs all-attributes attributes) + unapply? (if (seq attrs) + (cfo/shapes-applied-all? + (cfo/shapes-ids-by-applied-attributes token shapes target-attrs) + (into #{} (map :id) shapes) + target-attrs) + (cfo/shapes-token-applied? token shapes target-attrs))] + (if unapply? + {:action :unapply :attributes target-attrs} + (apply-action token attrs)))) + +(defn- action->event + [{:keys [action attributes on-update-shape]} token shapes shape-ids] + (case action + :unapply + (unapply-token {:attributes attributes + :token-name (:name token) + :shape-ids shape-ids}) + + :apply-spacing + (apply-spacing-token-separated {:token token + :shapes shapes}) + + :apply + (apply-token {:attributes attributes + :token token + :shape-ids shape-ids + :on-update-shape on-update-shape}))) + (defn toggle-token [{:keys [token attrs shape-ids expand-with-children]}] (ptk/reify ::on-toggle-token ptk/WatchEvent (watch [it state _] - (let [objects (dsh/lookup-page-objects state) - shapes (into [] (keep (d/getf objects)) shape-ids) - - shapes - (if expand-with-children - (into [] - (mapcat (fn [shape] - (if (= (:type shape) :group) - (keep objects (:shapes shape)) - [shape]))) - shapes) - shapes) - - {:keys [attributes all-attributes on-update-shape]} - (get token-properties (:type token)) - - on-update-shape - (if (seq attrs) - (or (get attr->shape-update (first attrs)) on-update-shape) - on-update-shape) - - target-attrs - (or attrs all-attributes attributes) - - unapply-tokens? - (if (seq attrs) - ;; Explicit attributes come from an input or a plugin apply - ;; call. Only toggle off when the token already covers every - ;; target attribute on every selected shape; a partial - ;; application is completed instead of removed. - (and (seq target-attrs) - (cfo/shapes-applied-all? - (cfo/shapes-ids-by-applied-attributes token shapes target-attrs) - (into #{} (map :id) shapes) - target-attrs)) - ;; No explicit attributes (token pill): toggle off when any - ;; selected shape has the token on any attribute. - (cfo/shapes-token-applied? token shapes target-attrs)) - - shape-ids - (map :id shapes)] - - (if unapply-tokens? - (rx/of - (unapply-token {:attributes target-attrs - :token-name (:name token) - :shape-ids shape-ids})) - (rx/of - (cond - (and (= (:type token) :spacing) - (nil? attrs)) - (-> (apply-spacing-token-separated {:token token - :attr attrs - :shapes shapes}) - (with-meta (meta it))) - - :else - (-> (apply-token {:attributes (if (empty? attrs) attributes attrs) - :token token - :shape-ids shape-ids - :on-update-shape on-update-shape}) - (with-meta (meta it)))))))))) + (let [shapes (target-shapes (dsh/lookup-page-objects state) shape-ids expand-with-children) + action (toggle-action token attrs shapes) + event (action->event action token shapes (map :id shapes))] + (rx/of + (if (= :unapply (:action action)) + event + (with-meta event (meta it)))))))) (defn apply-token-from-input [{:keys [token attrs shape-ids expand-with-children]}] (ptk/reify ::apply-token-from-input ptk/WatchEvent (watch [_ state _] - (let [objects (dsh/lookup-page-objects state) - shapes (into [] (keep (d/getf objects)) shape-ids) - - shapes - (if expand-with-children - (into [] - (mapcat (fn [shape] - (if (= (:type shape) :group) - (keep objects (:shapes shape)) - [shape]))) - shapes) - shapes) - - {:keys [attributes _ on-update-shape]} - (get token-properties (:type token)) - - on-update-shape - (if (seq attrs) - (or (get attr->shape-update (first attrs)) on-update-shape) - on-update-shape)] - + (let [shapes (target-shapes (dsh/lookup-page-objects state) shape-ids expand-with-children)] (rx/of - (cond - (and (= (:type token) :spacing) - (nil? attrs)) - (apply-spacing-token-separated {:token token - :attr attrs - :shapes shapes}) - - :else - (apply-token {:attributes (if (empty? attrs) attributes attrs) - :token token - :shape-ids shape-ids - :on-update-shape on-update-shape}))))))) + (action->event (apply-action token attrs) token shapes shape-ids)))))) (defn apply-token-on-color-selected diff --git a/frontend/test/frontend_tests/runner.cljs b/frontend/test/frontend_tests/runner.cljs index c0d928c680..ef4e8475e4 100644 --- a/frontend/test/frontend_tests/runner.cljs +++ b/frontend/test/frontend_tests/runner.cljs @@ -139,6 +139,7 @@ [frontend-tests.tokens.referenced-token-value-test] [frontend-tests.tokens.shadow-form-test] [frontend-tests.tokens.style-dictionary-test] + [frontend-tests.tokens.token-application-test] [frontend-tests.tokens.token-errors-test] [frontend-tests.tokens.token-group-test] [frontend-tests.tokens.token-parsing-test] @@ -356,6 +357,7 @@ 'frontend-tests.tokens.referenced-token-value-test 'frontend-tests.tokens.shadow-form-test 'frontend-tests.tokens.style-dictionary-test + 'frontend-tests.tokens.token-application-test 'frontend-tests.tokens.token-errors-test 'frontend-tests.tokens.token-group-test 'frontend-tests.tokens.token-parsing-test diff --git a/frontend/test/frontend_tests/tokens/logic/token_actions_test.cljs b/frontend/test/frontend_tests/tokens/logic/token_actions_test.cljs index fdd14413f2..e23c51b7ff 100644 --- a/frontend/test/frontend_tests/tokens/logic/token_actions_test.cljs +++ b/frontend/test/frontend_tests/tokens/logic/token_actions_test.cljs @@ -1541,6 +1541,177 @@ (t/testing "regular shape doesn't get spacing attributes" (t/is (nil? (:applied-tokens rect-regular'))))))))))) +(def ^:private design-tab-tokens + [{:name "color.primary" :value "red" :type :color} + {:name "opacity.half" :value "0.5" :type :opacity} + {:name "dimensions.lg" :value "100" :type :dimensions} + {:name "sizing.min" :value "50" :type :sizing}]) + +(defn- setup-file-with-design-tab-tokens + [& {:as shapes}] + (update-in (setup-file-with-tokens shapes) + [:data :tokens-lib] + #(reduce (fn [lib token] + (ctob/add-token lib (cthi/id :set-a) (ctob/make-token token))) + % + design-tab-tokens))) + +(t/deftest test-toggle-token-expands-group + (t/testing "toggling a token on a group with expand-with-children applies it to the children" + (t/async + done + (let [file (-> (setup-file-with-design-tab-tokens) + (ctho/add-group :group-1) + (cths/add-sample-shape :rect-in-group-1 :parent-label :group-1) + (cths/add-sample-shape :rect-in-group-2 :parent-label :group-1)) + store (ths/setup-store file) + events [(dwta/toggle-token {:token (toht/get-token file "color.primary") + :shape-ids [(cthi/id :group-1)] + :expand-with-children true})]] + (tohs/run-store-async + store done events + (fn [new-state] + (let [file' (ths/get-file-from-state new-state) + group-1' (cths/get-shape file' :group-1) + rect-in-group-1' (cths/get-shape file' :rect-in-group-1) + rect-in-group-2' (cths/get-shape file' :rect-in-group-2)] + (t/is (= "color.primary" (:fill (:applied-tokens rect-in-group-1')))) + (t/is (= "color.primary" (:fill (:applied-tokens rect-in-group-2')))) + (t/is (= "#ff0000" (get-in rect-in-group-1' [:fills 0 :fill-color]))) + (t/is (nil? (:applied-tokens group-1')))))))))) + +(t/deftest test-apply-token-from-input-fill-and-stroke + (t/testing "the fill and stroke rows apply a color token to their attribute only" + (t/async + done + (let [file (setup-file-with-design-tab-tokens) + store (ths/setup-store file) + token (toht/get-token file "color.primary") + events [(dwta/apply-token-from-input {:token token + :attrs #{:fill} + :shape-ids [(cthi/id :rect-1)] + :expand-with-children true}) + (dwta/apply-token-from-input {:token token + :attrs #{:stroke-color} + :shape-ids [(cthi/id :rect-2)] + :expand-with-children true})]] + (tohs/run-store-async + store done events + (fn [new-state] + (let [file' (ths/get-file-from-state new-state) + rect-1' (cths/get-shape file' :rect-1) + rect-2' (cths/get-shape file' :rect-2)] + (t/testing "fill row" + (t/is (= {:fill "color.primary"} (:applied-tokens rect-1'))) + (t/is (= "#ff0000" (get-in rect-1' [:fills 0 :fill-color])))) + (t/testing "stroke row" + (t/is (= {:stroke-color "color.primary"} (:applied-tokens rect-2'))) + (t/is (= "#ff0000" (get-in rect-2' [:strokes 0 :stroke-color]))))))))))) + +(t/deftest test-apply-token-from-input-keeps-applied-token + (t/testing "choosing the token a shape already has keeps it applied, unlike toggling" + (t/async + done + (let [file (-> (setup-file-with-design-tab-tokens) + (toht/apply-token-to-shape :rect-1 "color.primary" #{:fill})) + store (ths/setup-store file) + events [(dwta/apply-token-from-input {:token (toht/get-token file "color.primary") + :attrs #{:fill} + :shape-ids [(cthi/id :rect-1)] + :expand-with-children true})]] + (tohs/run-store-async + store done events + (fn [new-state] + (let [file' (ths/get-file-from-state new-state) + rect-1' (cths/get-shape file' :rect-1)] + (t/is (= "color.primary" (:fill (:applied-tokens rect-1')))) + (t/is (= "#ff0000" (get-in rect-1' [:fills 0 :fill-color])))))))))) + +(t/deftest test-apply-token-from-input-measures-and-opacity + (t/testing "the opacity, position and size fields apply the token to their attribute only" + (t/async + done + (let [file (setup-file-with-design-tab-tokens) + store (ths/setup-store file) + rect-3 (cths/get-shape file :rect-3) + events [(dwta/apply-token-from-input {:token (toht/get-token file "opacity.half") + :attrs #{:opacity} + :shape-ids [(cthi/id :rect-1)]}) + (dwta/apply-token-from-input {:token (toht/get-token file "dimensions.lg") + :attrs #{:x} + :shape-ids [(cthi/id :rect-2)]}) + (dwta/apply-token-from-input {:token (toht/get-token file "dimensions.lg") + :attrs #{:width} + :shape-ids [(cthi/id :rect-3)]})]] + (tohs/run-store-async + store done events + (fn [new-state] + (let [file' (ths/get-file-from-state new-state) + rect-1' (cths/get-shape file' :rect-1) + rect-2' (cths/get-shape file' :rect-2) + rect-3' (cths/get-shape file' :rect-3)] + (t/testing "opacity" + (t/is (= {:opacity "opacity.half"} (:applied-tokens rect-1'))) + (t/is (= 0.5 (:opacity rect-1')))) + (t/testing "position" + (t/is (= {:x "dimensions.lg"} (:applied-tokens rect-2'))) + (t/is (= 100 (:x rect-2')))) + (t/testing "size" + (t/is (= {:width "dimensions.lg"} (:applied-tokens rect-3'))) + (t/is (= 100 (:width rect-3'))) + (t/is (= (:height rect-3) (:height rect-3'))))))))))) + +(t/deftest test-apply-token-from-input-layout-sizing-limit + (t/testing "the min width field of a flex child sets that limit only" + (t/async + done + (let [file (-> (setup-file-with-design-tab-tokens) + (ctho/add-frame-with-child :frame-layout :rect-in-layout + {:frame-params {:layout :flex}})) + store (ths/setup-store file) + events [(dwta/apply-token-from-input {:token (toht/get-token file "sizing.min") + :attrs #{:layout-item-min-w} + :shape-ids [(cthi/id :rect-in-layout)]})]] + (tohs/run-store-async + store done events + (fn [new-state] + (let [file' (ths/get-file-from-state new-state) + rect-in-layout' (cths/get-shape file' :rect-in-layout)] + (t/is (= {:layout-item-min-w "sizing.min"} (:applied-tokens rect-in-layout'))) + (t/is (= 50 (:layout-item-min-w rect-in-layout'))) + (t/is (nil? (:layout-item-max-w rect-in-layout')))))))))) + +(t/deftest test-apply-token-on-color-selected + (t/testing "a color token chosen in the selection colors goes on each shape's fill or stroke" + (t/async + done + (let [file (setup-file-with-design-tab-tokens) + store (ths/setup-store file) + operations [{:prop :fill :shape-id (cthi/id :rect-1) :index 0} + {:prop :stroke :shape-id (cthi/id :rect-2) :index 0} + {:prop :content :shape-id (cthi/id :text-1) :index 0} + {:prop :shadow :shape-id (cthi/id :rect-3) :index 0}] + events [(dwta/apply-token-on-color-selected operations + (toht/get-token file "color.primary"))]] + (tohs/run-store-async + store done events + (fn [new-state] + (let [file' (ths/get-file-from-state new-state) + rect-1' (cths/get-shape file' :rect-1) + rect-2' (cths/get-shape file' :rect-2) + rect-3' (cths/get-shape file' :rect-3) + text-1' (cths/get-shape file' :text-1)] + (t/testing "fill" + (t/is (= {:fill "color.primary"} (:applied-tokens rect-1'))) + (t/is (= "#ff0000" (get-in rect-1' [:fills 0 :fill-color])))) + (t/testing "stroke" + (t/is (= {:stroke-color "color.primary"} (:applied-tokens rect-2'))) + (t/is (= "#ff0000" (get-in rect-2' [:strokes 0 :stroke-color])))) + (t/testing "text goes on the fill" + (t/is (= {:fill "color.primary"} (:applied-tokens text-1')))) + (t/testing "shadows take no color token" + (t/is (nil? (:applied-tokens rect-3'))))))))))) + (t/deftest test-detach-styles-color (t/testing "applying a color token to a shape with color styles should detach the styles" (t/async diff --git a/frontend/test/frontend_tests/tokens/token_application_test.cljs b/frontend/test/frontend_tests/tokens/token_application_test.cljs new file mode 100644 index 0000000000..1e23f373af --- /dev/null +++ b/frontend/test/frontend_tests/tokens/token_application_test.cljs @@ -0,0 +1,114 @@ +;; 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-application-test + (:require + [app.common.test-helpers.compositions :as tho] + [app.common.test-helpers.files :as thf] + [app.common.test-helpers.ids-map :as thi] + [app.common.test-helpers.shapes :as ths] + [app.common.types.token :as cto] + [app.common.types.tokens-lib :as ctob] + [app.main.data.workspace.tokens.application :as dwta] + [cljs.test :as t :include-macros true])) + +(t/use-fixtures :each thi/test-fixture) + +(def ^:private target-shapes @#'dwta/target-shapes) +(def ^:private toggle-action @#'dwta/toggle-action) + +(def ^:private radius + (ctob/make-token :name "radius.md" :type :border-radius :value "8")) + +(def ^:private color + (ctob/make-token :name "brand" :type :color :value "#ff0000")) + +(def ^:private spacing + (ctob/make-token :name "spacing.md" :type :spacing :value "16")) + +(def ^:private stroke-width + (ctob/make-token :name "stroke.md" :type :stroke-width :value "2")) + +(defn- with-token + [shape token attributes] + (cto/apply-token-to-shape {:shape shape :token token :attributes attributes})) + +(defn- toggle + "What toggling the token decides: remove it or apply it, and on which + attributes. How the shapes then change is covered by + `token-actions-test`." + [token attrs shapes] + (select-keys (toggle-action token attrs shapes) [:action :attributes])) + +(t/deftest toggle-from-the-pill + (t/testing "applies the token when no selected shape has it" + (t/is (= {:action :apply :attributes cto/border-radius-keys} + (toggle radius nil [(ths/sample-shape :rect1) + (ths/sample-shape :rect2)])))) + + (t/testing "removes the token when any selected shape has it on any attribute" + (t/is (= {:action :unapply :attributes cto/border-radius-keys} + (toggle radius nil [(ths/sample-shape :rect1) + (with-token (ths/sample-shape :rect2) radius #{:r1})])))) + + (t/testing "another token on the same attributes does not count" + (let [other (ctob/make-token :name "radius.lg" :type :border-radius :value "16") + shape (with-token (ths/sample-shape :rect1) other cto/border-radius-keys)] + (t/is (= :apply (:action (toggle radius nil [shape])))))) + + (t/testing "a color token goes on the fill but comes off the stroke too" + (t/is (= {:action :apply :attributes #{:fill}} + (toggle color nil [(ths/sample-shape :rect1)]))) + (t/is (= {:action :unapply :attributes cto/color-keys} + (toggle color nil [(with-token (ths/sample-shape :rect1) color #{:stroke-color})]))))) + +(t/deftest toggle-on-chosen-attributes + (t/testing "a partial application is completed, not removed" + (let [shape (with-token (ths/sample-shape :rect1) stroke-width + #{:stroke-width-right :stroke-width-bottom})] + (t/is (= {:action :apply :attributes cto/per-side-stroke-width-keys} + (toggle stroke-width cto/per-side-stroke-width-keys [shape]))))) + + (t/testing "a shape without the token completes the others" + (let [shapes [(with-token (ths/sample-shape :rect1) radius cto/border-radius-keys) + (ths/sample-shape :rect2)]] + (t/is (= :apply (:action (toggle radius cto/border-radius-keys shapes)))))) + + (t/testing "removes the token once every shape has it on every attribute" + (t/is (= {:action :unapply :attributes cto/border-radius-keys} + (toggle radius + cto/border-radius-keys + [(with-token (ths/sample-shape :rect1) radius cto/border-radius-keys) + (with-token (ths/sample-shape :rect2) radius cto/border-radius-keys)]))))) + +(t/deftest toggle-spacing + (t/testing "without attributes a spacing token is split between layouts and their children" + (t/is (= {:action :apply-spacing} + (toggle spacing nil [(ths/sample-shape :rect1)])))) + + (t/testing "on chosen attributes it goes on those" + (t/is (= {:action :apply :attributes #{:m1}} + (toggle spacing #{:m1} [(ths/sample-shape :rect1)]))))) + +(defn- objects-with-group + [] + (-> (thf/sample-file :file1 :page-label :page1) + (tho/add-group :group1) + (ths/add-sample-shape :rect1 :parent-label :group1) + (ths/add-sample-shape :rect2 :parent-label :group1) + (tho/add-rect :rect3) + (thf/current-page) + (:objects))) + +(t/deftest target-shapes-of-the-selection + (let [objects (objects-with-group) + ids [(thi/id :group1) (thi/id :rect3)]] + (t/testing "a selected group stands for its children" + (t/is (= [(thi/id :rect1) (thi/id :rect2) (thi/id :rect3)] + (map :id (target-shapes objects ids true))))) + + (t/testing "without expanding, the group stays selected" + (t/is (= ids (map :id (target-shapes objects ids false)))))))