Show warning when applying token with non-matching font variant

This commit is contained in:
Florian Schroedl 2025-08-26 12:09:20 +02:00 committed by Andrés Moya
parent 09b9383a0b
commit 6670b76ccc
3 changed files with 35 additions and 8 deletions

View File

@ -18,6 +18,7 @@
[app.common.uuid :as uuid] [app.common.uuid :as uuid]
[app.main.data.event :as ev] [app.main.data.event :as ev]
[app.main.data.helpers :as dsh] [app.main.data.helpers :as dsh]
[app.main.data.notifications :as ntf]
[app.main.data.style-dictionary :as sd] [app.main.data.style-dictionary :as sd]
[app.main.data.tinycolor :as tinycolor] [app.main.data.tinycolor :as tinycolor]
[app.main.data.workspace :as udw] [app.main.data.workspace :as udw]
@ -28,6 +29,7 @@
[app.main.data.workspace.undo :as dwu] [app.main.data.workspace.undo :as dwu]
[app.main.fonts :as fonts] [app.main.fonts :as fonts]
[app.main.store :as st] [app.main.store :as st]
[app.util.i18n :refer [tr]]
[beicon.v2.core :as rx] [beicon.v2.core :as rx]
[clojure.set :as set] [clojure.set :as set]
[cuerdas.core :as str] [cuerdas.core :as str]
@ -327,17 +329,24 @@
(update-text-decoration value shape-ids attributes page-id)))) (update-text-decoration value shape-ids attributes page-id))))
(defn- generate-font-weight-text-shape-update (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] (let [update-node? (fn [node]
(or (txt/is-text-node? node) (or (txt/is-text-node? node)
(txt/is-paragraph-node? node))) (txt/is-paragraph-node? node)))
update-fn (fn [node _] update-fn (fn [node _]
(let [font (fonts/get-font-data (:font-id node)) (let [font (fonts/get-font-data (:font-id node))
font-variant-id (fonts/find-closest-variant font (:weight font-variant) (:style font-variant))] variant (fonts/find-closest-variant font (:weight font-variant) (:style font-variant))
(if font-variant-id call-on-mismatch? (when (fn? on-mismatch)
(-> node (or
(d/txt-merge (assoc font-variant :font-variant-id (:id font-variant-id))) (not= (:weight font-variant) (:weight variant))
(cty/remove-typography-from-node)) (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)))] node)))]
(dwsh/update-shapes shape-ids (dwsh/update-shapes shape-ids
#(txt/update-text-content % update-node? update-fn nil) #(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] (update-font-weight value shape-ids attributes nil))
([value shape-ids _attributes page-id] ([value shape-ids _attributes page-id]
(when-let [font-variant (ctt/valid-font-weight-variant value)] (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 ------------------------------------------------------------ ;; Events to apply / unapply tokens to shapes ------------------------------------------------------------
@ -530,7 +549,7 @@
:font-weight :font-weight
{:title "Font Weight" {:title "Font Weight"
:attributes ctt/font-weight-keys :attributes ctt/font-weight-keys
:on-update-shape update-font-weight :on-update-shape update-font-weight-interactive
:modal {:key :tokens/font-weight :modal {:key :tokens/font-weight
:fields [{:label "Font Weight" :fields [{:label "Font Weight"
:key :font-weight}]}} :key :font-weight}]}}

View File

@ -7624,6 +7624,10 @@ msgstr "Import was successful. Some tokens were not included."
msgid "workspace.tokens.unknown-token-type-section" msgid "workspace.tokens.unknown-token-type-section"
msgstr "Type '%s' is not supported (%s)\n" 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 #: src/app/main/ui/workspace/tokens/token_pill.cljs:133
msgid "workspace.tokens.value-not-valid" msgid "workspace.tokens.value-not-valid"
msgstr "The value is not valid" msgstr "The value is not valid"

View File

@ -7517,6 +7517,10 @@ msgstr "La importación se ha realizado correctamente. Algunos tokens no se incl
msgid "workspace.tokens.unknown-token-type-section" msgid "workspace.tokens.unknown-token-type-section"
msgstr "El tipo '%s' no está soportado (%s)\n" 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 #: src/app/main/ui/workspace/tokens/token_pill.cljs:133
msgid "workspace.tokens.value-not-valid" msgid "workspace.tokens.value-not-valid"
msgstr "El valor no es válido" msgstr "El valor no es válido"