🐛 Fix stroke-width parsing on svg upload

And refactor a bit the stroke parsing function
This commit is contained in:
Andrey Antukh 2023-08-04 08:41:20 +02:00
parent f3bf04e1c9
commit 66e877ed40
3 changed files with 63 additions and 50 deletions

View file

@ -8,6 +8,7 @@
(:require (:require
[app.common.colors :as clr] [app.common.colors :as clr]
[app.common.data :as d] [app.common.data :as d]
[app.common.data.macros :as dm]
[app.common.exceptions :as ex] [app.common.exceptions :as ex]
[app.common.geom.matrix :as gmt] [app.common.geom.matrix :as gmt]
[app.common.geom.point :as gpt] [app.common.geom.point :as gpt]
@ -123,56 +124,65 @@
(assoc-in [:fills 0 :fill-opacity] (-> (get-in shape [:svg-attrs :style :fill-opacity]) (assoc-in [:fills 0 :fill-opacity] (-> (get-in shape [:svg-attrs :style :fill-opacity])
(d/parse-double 1))))))) (d/parse-double 1)))))))
(defn setup-stroke [shape]
(let [stroke-linecap (-> (or (get-in shape [:svg-attrs :stroke-linecap])
(get-in shape [:svg-attrs :style :stroke-linecap]))
((d/nilf str/trim))
((d/nilf keyword)))
color-attr (str/trim (get-in shape [:svg-attrs :stroke]))
color-attr (if (= color-attr "currentColor") clr/black color-attr)
color-style (str/trim (get-in shape [:svg-attrs :style :stroke]))
color-style (if (= color-style "currentColor") clr/black color-style)
shape (defn- setup-stroke
(cond-> shape [shape]
;; Color present as attribute (let [attrs (get shape :svg-attrs)
(uc/color? color-attr) style (get attrs :style)
(-> (update :svg-attrs dissoc :stroke)
(assoc-in [:strokes 0 :stroke-color] (uc/parse-color color-attr)))
;; Color present as style stroke (or (str/trim (:stroke attrs))
(uc/color? color-style) (str/trim (:stroke style)))
(-> (update-in [:svg-attrs :style] dissoc :stroke)
(assoc-in [:strokes 0 :stroke-color] (uc/parse-color color-style)))
(get-in shape [:svg-attrs :stroke-opacity]) color (cond
(-> (update :svg-attrs dissoc :stroke-opacity) (= stroke "currentColor") clr/black
(assoc-in [:strokes 0 :stroke-opacity] (-> (get-in shape [:svg-attrs :stroke-opacity]) (= stroke "none") nil
(d/parse-double 1)))) :else (uc/parse-color stroke))
(get-in shape [:svg-attrs :style :stroke-opacity]) opacity (when (some? color)
(-> (update-in [:svg-attrs :style] dissoc :stroke-opacity) (d/parse-double
(assoc-in [:strokes 0 :stroke-opacity] (-> (get-in shape [:svg-attrs :style :stroke-opacity]) (or (:stroke-opacity attrs)
(d/parse-double 1)))) (:stroke-opacity style))
1))
(get-in shape [:svg-attrs :stroke-width]) width (when (some? color)
(-> (update :svg-attrs dissoc :stroke-width) (d/parse-double
(assoc-in [:strokes 0 :stroke-width] (-> (get-in shape [:svg-attrs :stroke-width]) (or (:stroke-width attrs)
(d/parse-double)))) (:stroke-width style))
1))
(get-in shape [:svg-attrs :style :stroke-width]) linecap (or (get attrs :stroke-linecap)
(-> (update-in [:svg-attrs :style] dissoc :stroke-width) (get style :stroke-linecap))
(assoc-in [:strokes 0 :stroke-width] (-> (get-in shape [:svg-attrs :style :stroke-width]) linecap (some-> linecap str/trim keyword)
(d/parse-double))))
(and stroke-linecap (= (:type shape) :path)) attrs (-> attrs
(-> (update-in [:svg-attrs :style] dissoc :stroke-linecap) (dissoc :stroke)
(cond-> (#{:round :square} stroke-linecap) (dissoc :stroke-width)
(assoc :stroke-cap-start stroke-linecap (dissoc :stroke-opacity)
:stroke-cap-end stroke-linecap))))] (update :style (fn [style]
(-> style
(dissoc :stroke)
(dissoc :stroke-linecap)
(dissoc :stroke-width)
(dissoc :stroke-opacity)))))]
(cond-> shape (cond-> (assoc shape :svg-attrs attrs)
(d/any-key? (get-in shape [:strokes 0]) :stroke-color :stroke-opacity :stroke-width :stroke-cap-start :stroke-cap-end) (some? color)
(assoc-in [:strokes 0 :stroke-color] color)
(and (some? color) (some? opacity))
(assoc-in [:strokes 0 :stroke-opacity] opacity)
(and (some? color) (some? width))
(assoc-in [:strokes 0 :stroke-width] width)
(and (some? linecap) (= (:type shape) :path)
(or (= linecap :round) (= linecap :square)))
(assoc :stroke-cap-start linecap
:stroke-cap-end linecap)
(d/any-key? (dm/get-in shape [:strokes 0])
:stroke-color :stroke-opacity :stroke-width
:stroke-cap-start :stroke-cap-end)
(assoc-in [:strokes 0 :stroke-style] :svg)))) (assoc-in [:strokes 0 :stroke-style] :svg))))
(defn setup-opacity [shape] (defn setup-opacity [shape]

View file

@ -89,7 +89,7 @@
(obj/merge! attrs (clj->js fill-attrs))))) (obj/merge! attrs (clj->js fill-attrs)))))
(defn add-stroke [attrs stroke-data render-id index] (defn add-stroke [attrs stroke-data render-id index]
(let [stroke-style (:stroke-style stroke-data :none) (let [stroke-style (:stroke-style stroke-data :solid)
stroke-color-gradient-id (str "stroke-color-gradient_" render-id "_" index) stroke-color-gradient-id (str "stroke-color-gradient_" render-id "_" index)
stroke-width (:stroke-width stroke-data 1)] stroke-width (:stroke-width stroke-data 1)]
(if (not= stroke-style :none) (if (not= stroke-style :none)

View file

@ -8,6 +8,7 @@
"Color conversion utils." "Color conversion utils."
(:require (:require
[app.common.data :as d] [app.common.data :as d]
[app.common.data.macros :as dm]
[app.util.i18n :as i18n :refer [tr]] [app.util.i18n :as i18n :refer [tr]]
[app.util.object :as obj] [app.util.object :as obj]
[app.util.strings :as ust] [app.util.strings :as ust]
@ -176,14 +177,16 @@
(= id :multiple) (= id :multiple)
(= file-id :multiple))) (= file-id :multiple)))
(defn color? [^string color-str] (defn color?
(and (not (nil? color-str)) [color]
(seq color-str) (and (string? color)
(gcolor/isValidColor color-str))) (gcolor/isValidColor color)))
(defn parse-color [^string color-str] (defn parse-color
(let [result (gcolor/parse color-str)] [color]
(str (.-hex ^js result)))) (when (color? color)
(let [result (gcolor/parse color)]
(dm/str (.-hex ^js result)))))
(def color-names (def color-names
(obj/get-keys ^js gcolor/names)) (obj/get-keys ^js gcolor/names))