From 6670b76cccb9166e0ed78ea047e07cc7ff4be402 Mon Sep 17 00:00:00 2001 From: Florian Schroedl Date: Tue, 26 Aug 2025 12:09:20 +0200 Subject: [PATCH] :sparkles: Show warning when applying token with non-matching font variant --- .../data/workspace/tokens/application.cljs | 35 ++++++++++++++----- frontend/translations/en.po | 4 +++ frontend/translations/es.po | 4 +++ 3 files changed, 35 insertions(+), 8 deletions(-) diff --git a/frontend/src/app/main/data/workspace/tokens/application.cljs b/frontend/src/app/main/data/workspace/tokens/application.cljs index 6eb4474b24..b21ef41576 100644 --- a/frontend/src/app/main/data/workspace/tokens/application.cljs +++ b/frontend/src/app/main/data/workspace/tokens/application.cljs @@ -18,6 +18,7 @@ [app.common.uuid :as uuid] [app.main.data.event :as ev] [app.main.data.helpers :as dsh] + [app.main.data.notifications :as ntf] [app.main.data.style-dictionary :as sd] [app.main.data.tinycolor :as tinycolor] [app.main.data.workspace :as udw] @@ -28,6 +29,7 @@ [app.main.data.workspace.undo :as dwu] [app.main.fonts :as fonts] [app.main.store :as st] + [app.util.i18n :refer [tr]] [beicon.v2.core :as rx] [clojure.set :as set] [cuerdas.core :as str] @@ -327,17 +329,24 @@ (update-text-decoration value shape-ids attributes page-id)))) (defn- generate-font-weight-text-shape-update - [font-variant shape-ids page-id] + [font-variant shape-ids page-id on-mismatch] (let [update-node? (fn [node] (or (txt/is-text-node? node) (txt/is-paragraph-node? node))) update-fn (fn [node _] (let [font (fonts/get-font-data (:font-id node)) - font-variant-id (fonts/find-closest-variant font (:weight font-variant) (:style font-variant))] - (if font-variant-id - (-> node - (d/txt-merge (assoc font-variant :font-variant-id (:id font-variant-id))) - (cty/remove-typography-from-node)) + variant (fonts/find-closest-variant font (:weight font-variant) (:style font-variant)) + call-on-mismatch? (when (fn? on-mismatch) + (or + (not= (:weight font-variant) (:weight variant)) + (when (:style font-variant) + (not= (:style font-variant) (:style variant)))))] + (if variant + (do + (when call-on-mismatch? (on-mismatch variant)) + (-> node + (d/txt-merge (assoc variant :font-variant-id (:id variant))) + (cty/remove-typography-from-node))) node)))] (dwsh/update-shapes shape-ids #(txt/update-text-content % update-node? update-fn nil) @@ -348,7 +357,17 @@ ([value shape-ids attributes] (update-font-weight value shape-ids attributes nil)) ([value shape-ids _attributes page-id] (when-let [font-variant (ctt/valid-font-weight-variant value)] - (generate-font-weight-text-shape-update font-variant shape-ids page-id)))) + (generate-font-weight-text-shape-update font-variant shape-ids page-id nil)))) + +(defn update-font-weight-interactive + ([value shape-ids attributes] (update-font-weight-interactive value shape-ids attributes nil)) + ([value shape-ids _attributes page-id] + (when-let [font-variant (ctt/valid-font-weight-variant value)] + (let [on-mismatch #(st/emit! (ntf/show {:content (tr "workspace.tokens.font-variant-not-found") + :type :toast + :level :warning + :timeout 7000}))] + (generate-font-weight-text-shape-update font-variant shape-ids page-id on-mismatch))))) ;; Events to apply / unapply tokens to shapes ------------------------------------------------------------ @@ -530,7 +549,7 @@ :font-weight {:title "Font Weight" :attributes ctt/font-weight-keys - :on-update-shape update-font-weight + :on-update-shape update-font-weight-interactive :modal {:key :tokens/font-weight :fields [{:label "Font Weight" :key :font-weight}]}} diff --git a/frontend/translations/en.po b/frontend/translations/en.po index e371d5a12c..bffb2269d1 100644 --- a/frontend/translations/en.po +++ b/frontend/translations/en.po @@ -7624,6 +7624,10 @@ msgstr "Import was successful. Some tokens were not included." msgid "workspace.tokens.unknown-token-type-section" msgstr "Type '%s' is not supported (%s)\n" +#: frontend/src/app/main/data/workspace/tokens/application.cljs:364 +msgid "workspace.tokens.font-variant-not-found" +msgstr "Error setting font weight/style. This font style does not exist in the current font" + #: src/app/main/ui/workspace/tokens/token_pill.cljs:133 msgid "workspace.tokens.value-not-valid" msgstr "The value is not valid" diff --git a/frontend/translations/es.po b/frontend/translations/es.po index 6161352f70..df51cb83cc 100644 --- a/frontend/translations/es.po +++ b/frontend/translations/es.po @@ -7517,6 +7517,10 @@ msgstr "La importación se ha realizado correctamente. Algunos tokens no se incl msgid "workspace.tokens.unknown-token-type-section" msgstr "El tipo '%s' no está soportado (%s)\n" +#: frontend/src/app/main/data/workspace/tokens/application.cljs:364 +msgid "workspace.tokens.font-variant-not-found" +msgstr "Error al configurar el font-weight/style de la fuente. Este estilo de fuente no existe en la fuente actual." + #: src/app/main/ui/workspace/tokens/token_pill.cljs:133 msgid "workspace.tokens.value-not-valid" msgstr "El valor no es válido"