🐛 Fix missing flex props on code generation

This commit is contained in:
Eva 2023-01-24 10:44:14 +01:00
parent bc88e30efa
commit ea1b3bd058
5 changed files with 121 additions and 68 deletions

View file

@ -55,7 +55,7 @@
{:p1 p1 :p2 p2 :p3 p3 :p4 p4} {:p1 p1 :p2 p2 :p3 p3 :p4 p4}
(and (= p1 p3) (= p2 p4)) (and (= p1 p3) (= p2 p4))
{:p1 p1 :p3 p3} {:p1 p1 :p2 p2}
(and (not= p1 p3) (= p2 p4)) (and (not= p1 p3) (= p2 p4))
{:p1 p1 :p2 p2 :p3 p3} {:p1 p1 :p2 p2 :p3 p3}
@ -67,9 +67,11 @@
(let [sizing (if (= type :width) (let [sizing (if (= type :width)
(:layout-item-h-sizing shape) (:layout-item-h-sizing shape)
(:layout-item-v-sizing shape))] (:layout-item-v-sizing shape))]
(if (= sizing :fill) (if (string? value)
"100%" value
(str (format-pixels value))))) (if (= sizing :fill)
"100%"
(str (format-pixels value))))))
(defn format-padding (defn format-padding
[padding-values type] [padding-values type]

View file

@ -27,7 +27,7 @@
:group [:layout :svg :layout-flex-item] :group [:layout :svg :layout-flex-item]
:rect [:layout :fill :stroke :shadow :blur :svg :layout-flex-item] :rect [:layout :fill :stroke :shadow :blur :svg :layout-flex-item]
:circle [:layout :fill :stroke :shadow :blur :svg :layout-flex-item] :circle [:layout :fill :stroke :shadow :blur :svg :layout-flex-item]
:path [:layout :fill :stroke :shadow :blur :svg] :path [:layout :fill :stroke :shadow :blur :svg :layout-flex-item]
:image [:image :layout :fill :stroke :shadow :blur :svg :layout-flex-item] :image [:image :layout :fill :stroke :shadow :blur :svg :layout-flex-item]
:text [:layout :text :shadow :blur :stroke :layout-flex-item]}) :text [:layout :text :shadow :blur :stroke :layout-flex-item]})

View file

@ -23,31 +23,31 @@
:rx "border-radius" :rx "border-radius"
:r1 "border-radius"} :r1 "border-radius"}
:format {:rotation #(str/fmt "rotate(%sdeg)" %) :format {:rotation #(str/fmt "rotate(%sdeg)" %)
:r1 #(apply str/fmt "%spx, %spx, %spx, %spx" %) :r1 #(apply str/fmt "%spx, %spx, %spx, %spx" %)
:width (partial fmt/format-size :width) :width #(cg/get-size :width %)
:height (partial fmt/format-size :height)} :height #(cg/get-size :height %)}
:multi {:r1 [:r1 :r2 :r3 :r4]}}) :multi {:r1 [:r1 :r2 :r3 :r4]}})
(defn copy-data (defn copy-data
([shape] ([shape]
(apply copy-data shape properties)) (apply copy-data shape properties))
([shape & properties] ([shape & properties]
(cg/generate-css-props shape properties params))) (cg/generate-css-props shape properties params)))
(mf/defc layout-block (mf/defc layout-block
[{:keys [shape]}] [{:keys [shape]}]
(let [selrect (:selrect shape) (let [selrect (:selrect shape)
{:keys [x y]} selrect] {:keys [x y width height]} selrect]
[:* [:*
[:div.attributes-unit-row [:div.attributes-unit-row
[:div.attributes-label (tr "inspect.attributes.layout.width")] [:div.attributes-label (tr "inspect.attributes.layout.width")]
[:div.attributes-value (fmt/format-size :width (:width shape) shape)] [:div.attributes-value (fmt/format-size :width width shape)]
[:& copy-button {:data (copy-data shape :width)}]] [:& copy-button {:data (copy-data selrect :width)}]]
[:div.attributes-unit-row [:div.attributes-unit-row
[:div.attributes-label (tr "inspect.attributes.layout.height")] [:div.attributes-label (tr "inspect.attributes.layout.height")]
[:div.attributes-value (fmt/format-size :height (:height shape) shape)] [:div.attributes-value (fmt/format-size :height height shape)]
[:& copy-button {:data (copy-data shape :height)}]] [:& copy-button {:data (copy-data selrect :height)}]]
(when (not= (:x shape) 0) (when (not= (:x shape) 0)
[:div.attributes-unit-row [:div.attributes-unit-row
@ -73,7 +73,7 @@
[:div.attributes-value [:div.attributes-value
(fmt/format-number (:r1 shape)) ", " (fmt/format-number (:r1 shape)) ", "
(fmt/format-number (:r2 shape)) ", " (fmt/format-number (:r2 shape)) ", "
(fmt/format-number (:r3 shape))", " (fmt/format-number (:r3 shape)) ", "
(fmt/format-pixels (:r4 shape))] (fmt/format-pixels (:r4 shape))]
[:& copy-button {:data (copy-data shape :r1)}]]) [:& copy-button {:data (copy-data shape :r1)}]])

View file

@ -39,10 +39,10 @@
:layout-gap "gap" :layout-gap "gap"
:layout-padding "padding"} :layout-padding "padding"}
:format {:layout name :format {:layout name
:layout-flex-dir name :layout-flex-dir cg/get-layout-direction
:layout-align-items name :layout-align-items name
:layout-justify-content name :layout-justify-content name
:layout-wrap-type name :layout-wrap-type cg/get-layout-orientation
:layout-gap fm/format-gap :layout-gap fm/format-gap
:layout-padding fm/format-padding}}) :layout-padding fm/format-padding}})
@ -72,7 +72,7 @@
(for [[k v] values] (for [[k v] values]
[:span.items {:key (str type "-" k "-" v)} v "px"])])) [:span.items {:key (str type "-" k "-" v)} v "px"])]))
(mf/defc layout-block (mf/defc layout-flex-block
[{:keys [shape]}] [{:keys [shape]}]
[:* [:*
[:div.attributes-unit-row [:div.attributes-unit-row
@ -82,7 +82,7 @@
[:div.attributes-unit-row [:div.attributes-unit-row
[:div.attributes-label "Direction"] [:div.attributes-label "Direction"]
[:div.attributes-value (str/capital (d/name (:layout-flex-dir shape)))] [:div.attributes-value (str/capital (cg/get-layout-direction (:layout-flex-dir shape)))]
[:& copy-button {:data (copy-data shape :layout-flex-dir)}]] [:& copy-button {:data (copy-data shape :layout-flex-dir)}]]
[:div.attributes-unit-row [:div.attributes-unit-row
@ -97,7 +97,7 @@
[:div.attributes-unit-row [:div.attributes-unit-row
[:div.attributes-label "Flex wrap"] [:div.attributes-label "Flex wrap"]
[:div.attributes-value (str/capital (d/name (:layout-wrap-type shape)))] [:div.attributes-value (str/capital (cg/get-layout-orientation (:layout-wrap-type shape)))]
[:& copy-button {:data (copy-data shape :layout-wrap-type)}]] [:& copy-button {:data (copy-data shape :layout-wrap-type)}]]
(when (= :wrap (:layout-wrap-type shape)) (when (= :wrap (:layout-wrap-type shape))
@ -129,11 +129,11 @@
(let [shapes (->> shapes (filter has-flex?))] (let [shapes (->> shapes (filter has-flex?))]
(when (seq shapes) (when (seq shapes)
[:div.attributes-block [:div.attributes-block
[:div.attributes-block-title [:div.attributes-block-title
[:div.attributes-block-title-text "Layout"] [:div.attributes-block-title-text "Layout"]
(when (= (count shapes) 1) (when (= (count shapes) 1)
[:& copy-button {:data (copy-data (first shapes))}])] [:& copy-button {:data (copy-data (first shapes))}])]
(for [shape shapes] (for [shape shapes]
[:& layout-block {:shape shape [:& layout-flex-block {:shape shape
:key (:id shape)}])]))) :key (:id shape)}])])))

View file

@ -41,6 +41,33 @@
(when-not (= :none (:stroke-style shape)) (when-not (= :none (:stroke-style shape))
(str/format "%spx %s %s" width style (uc/color->background color))))) (str/format "%spx %s %s" width style (uc/color->background color)))))
(defn get-size
[type values]
(let [value (cond
(number? values) values
(string? values) values
(type values) (type values)
:else (type (:selrect values)))]
(if (= :width type)
(fmt/format-size :width value values)
(fmt/format-size :heigth value values))))
(defn get-layout-orientation
[value]
(if (= :wrap value)
"wrap"
"nowrap"))
(defn get-layout-direction
[value]
(case value
:row "row"
:reverse-row "row-reverse"
:column "column"
:reverse-column "column-reverse"
"row"))
(def styles-data (def styles-data
{:layout {:props [:width :height :x :y :radius :rx :r1] {:layout {:props [:width :height :x :y :radius :rx :r1]
:to-prop {:x "left" :to-prop {:x "left"
@ -49,9 +76,9 @@
:rx "border-radius" :rx "border-radius"
:r1 "border-radius"} :r1 "border-radius"}
:format {:rotation #(str/fmt "rotate(%sdeg)" %) :format {:rotation #(str/fmt "rotate(%sdeg)" %)
:r1 #(apply str/fmt "%spx, %spx, %spx, %spx" %) :r1 #(apply str/fmt "%spx, %spx, %spx, %spx" %)
:width (partial fmt/format-size :width) :width #(get-size :width %)
:height (partial fmt/format-size :height)} :height #(get-size :height %)}
:multi {:r1 [:r1 :r2 :r3 :r4]}} :multi {:r1 [:r1 :r2 :r3 :r4]}}
:fill {:props [:fill-color :fill-color-gradient] :fill {:props [:fill-color :fill-color-gradient]
:to-prop {:fill-color "background" :fill-color-gradient "background"} :to-prop {:fill-color "background" :fill-color-gradient "background"}
@ -66,8 +93,8 @@
:to-prop {:blur "filter"} :to-prop {:blur "filter"}
:format {:blur #(str/fmt "blur(%spx)" (:value %))}} :format {:blur #(str/fmt "blur(%spx)" (:value %))}}
:layout-flex {:props [:layout :layout-flex {:props [:layout
:layout-align-items
:layout-flex-dir :layout-flex-dir
:layout-align-items
:layout-justify-content :layout-justify-content
:layout-gap :layout-gap
:layout-padding :layout-padding
@ -80,10 +107,10 @@
:layout-gap "gap" :layout-gap "gap"
:layout-padding "padding"} :layout-padding "padding"}
:format {:layout name :format {:layout name
:layout-flex-dir name :layout-flex-dir get-layout-direction
:layout-align-items name :layout-align-items name
:layout-justify-content name :layout-justify-content name
:layout-wrap-type name :layout-wrap-type get-layout-orientation
:layout-gap format-gap :layout-gap format-gap
:layout-padding fmt/format-padding}}}) :layout-padding fmt/format-padding}}})
@ -98,9 +125,9 @@
:text-transform] :text-transform]
:to-prop {:fill-color "color"} :to-prop {:fill-color "color"}
:format {:font-family #(str "'" % "'") :format {:font-family #(str "'" % "'")
:font-style #(str "'" % "'") :font-style #(str %)
:font-size #(str % "px") :font-size #(str % "px")
:line-height #(str % "px") :line-height #(str %)
:letter-spacing #(str % "px") :letter-spacing #(str % "px")
:text-decoration name :text-decoration name
:text-transform name :text-transform name
@ -126,6 +153,25 @@
:layout-item-min-w #(str % "px") :layout-item-min-w #(str % "px")
:layout-item-align-self name}}) :layout-item-align-self name}})
(def layout-align-content
{:props [:layout-align-content]
:to-prop {:layout-align-content "align-content"}
:format {:layout-align-content name}})
(defn get-specific-value
[values prop]
(let [result (if (get values prop)
(get values prop)
(get (:selrect values) prop))
result (if (= :width prop)
(get-size :width values)
result)
result (if (= :height prop)
(get-size :height values)
result)]
result))
(defn generate-css-props (defn generate-css-props
([values properties] ([values properties]
(generate-css-props values properties nil)) (generate-css-props values properties nil))
@ -150,7 +196,7 @@
get-value (fn [prop] get-value (fn [prop]
(if-let [props (prop multi)] (if-let [props (prop multi)]
(map #(get values %) props) (map #(get values %) props)
(get values prop))) (get-specific-value values prop)))
null? (fn [value] null? (fn [value]
(if (coll? value) (if (coll? value)
@ -178,20 +224,19 @@
;; it will come with a vector of flex-items if any. ;; it will come with a vector of flex-items if any.
;; If there are none it will continue as usual. ;; If there are none it will continue as usual.
flex-items (:flex-items shape) flex-items (:flex-items shape)
props (->> styles-data vals (mapcat :props)) props (->> styles-data vals (mapcat :props))
to-prop (->> styles-data vals (map :to-prop) (reduce merge)) to-prop (->> styles-data vals (map :to-prop) (reduce merge))
format (->> styles-data vals (map :format) (reduce merge)) format (->> styles-data vals (map :format) (reduce merge))
multi (->> styles-data vals (map :multi) (reduce merge)) multi (->> styles-data vals (map :multi) (reduce merge))
props (if (seq flex-items) props (cond-> props
(concat props (:props layout-flex-item-params)) (seq flex-items) (concat (:props layout-flex-item-params))
props) (= :wrap (:layout-wrap-type shape)) (concat (:props layout-align-content)))
to-prop (if (seq flex-items) to-prop (cond-> to-prop
(merge to-prop (:to-prop layout-flex-item-params)) (seq flex-items) (merge (:to-prop layout-flex-item-params))
to-prop) (= :wrap (:layout-wrap-type shape)) (merge (:to-prop layout-align-content)))
format (if (seq flex-items) format (cond-> format
(merge format (:format layout-flex-item-params)) (seq flex-items) (merge (:format layout-flex-item-params))
format)] (= :wrap (:layout-wrap-type shape)) (merge (:format layout-align-content)))]
(generate-css-props shape props {:to-prop to-prop (generate-css-props shape props {:to-prop to-prop
:format format :format format
:multi multi :multi multi
@ -208,40 +253,48 @@
(defn parse-style-text-blocks (defn parse-style-text-blocks
[node attrs] [node attrs]
(letfn (letfn
[(rec-style-text-map [acc node style] [(rec-style-text-map [acc node style]
(let [node-style (merge style (select-keys node attrs)) (let [node-style (merge style (select-keys node attrs))
head (or (-> acc first) [{} ""]) head (or (-> acc first) [{} ""])
[head-style head-text] head [head-style head-text] head
new-acc new-acc
(cond (cond
(:children node) (:children node)
(reduce #(rec-style-text-map %1 %2 node-style) acc (:children node)) (reduce #(rec-style-text-map %1 %2 node-style) acc (:children node))
(not= head-style node-style) (not= head-style node-style)
(cons [node-style (:text node "")] acc) (cons [node-style (:text node "")] acc)
:else :else
(cons [node-style (str head-text "" (:text node))] (rest acc))) (cons [node-style (str head-text "" (:text node))] (rest acc)))
;; We add an end-of-line when finish a paragraph ;; We add an end-of-line when finish a paragraph
new-acc new-acc
(if (= (:type node) "paragraph") (if (= (:type node) "paragraph")
(let [[hs ht] (first new-acc)] (let [[hs ht] (first new-acc)]
(cons [hs (str ht "\n")] (rest new-acc))) (cons [hs (str ht "\n")] (rest new-acc)))
new-acc)] new-acc)]
new-acc))] new-acc))]
(-> (rec-style-text-map [] node {}) (-> (rec-style-text-map [] node {})
reverse))) reverse)))
(defn text->properties [shape] (defn text->properties [shape]
(let [text-shape-style (select-keys styles-data [:layout :shadow :blur]) (let [flex-items (:flex-items shape)
text-shape-style (select-keys styles-data [:layout :shadow :blur])
shape-props (->> text-shape-style vals (mapcat :props)) shape-props (->> text-shape-style vals (mapcat :props))
shape-to-prop (->> text-shape-style vals (map :to-prop) (reduce merge)) shape-to-prop (->> text-shape-style vals (map :to-prop) (reduce merge))
shape-format (->> text-shape-style vals (map :format) (reduce merge)) shape-format (->> text-shape-style vals (map :format) (reduce merge))
shape-props (cond-> shape-props
(seq flex-items) (concat (:props layout-flex-item-params)))
shape-to-prop (cond-> shape-to-prop
(seq flex-items) (merge (:to-prop layout-flex-item-params)))
shape-format (cond-> shape-format
(seq flex-items) (merge (:format layout-flex-item-params)))
text-values (->> (search-text-attrs text-values (->> (search-text-attrs
(:content shape) (:content shape)
(conj (:props style-text) :fill-color-gradient :fill-opacity)) (conj (:props style-text) :fill-color-gradient :fill-opacity))
@ -257,9 +310,7 @@
(:props style-text) (:props style-text)
{:to-prop (:to-prop style-text) {:to-prop (:to-prop style-text)
:format (:format style-text) :format (:format style-text)
:tab-size 2})])) :tab-size 2})])))
)
(defn generate-css [shape] (defn generate-css [shape]
(let [name (:name shape) (let [name (:name shape)