mirror of
https://github.com/penpot/penpot.git
synced 2025-01-10 17:00:36 -05:00
💄 Add mostly cosmetic improvements to text-svg-position ns
This commit is contained in:
parent
56aa751425
commit
956da67f84
2 changed files with 52 additions and 45 deletions
|
@ -420,6 +420,14 @@
|
||||||
{:width (.-innerWidth ^js js/window)
|
{:width (.-innerWidth ^js js/window)
|
||||||
:height (.-innerHeight ^js js/window)})
|
:height (.-innerHeight ^js js/window)})
|
||||||
|
|
||||||
|
(defn get-computed-styles
|
||||||
|
[node]
|
||||||
|
(js/getComputedStyle node))
|
||||||
|
|
||||||
|
(defn get-property-value
|
||||||
|
[o prop]
|
||||||
|
(.getPropertyValue ^js o prop))
|
||||||
|
|
||||||
(defn focus!
|
(defn focus!
|
||||||
[^js node]
|
[^js node]
|
||||||
(when (some? node)
|
(when (some? node)
|
||||||
|
|
|
@ -12,22 +12,21 @@
|
||||||
[app.main.fonts :as fonts]
|
[app.main.fonts :as fonts]
|
||||||
[app.util.dom :as dom]
|
[app.util.dom :as dom]
|
||||||
[app.util.text-position-data :as tpd]
|
[app.util.text-position-data :as tpd]
|
||||||
|
[cuerdas.core :as str]
|
||||||
[promesa.core :as p]))
|
[promesa.core :as p]))
|
||||||
|
|
||||||
(defn parse-text-nodes
|
(defn parse-text-nodes
|
||||||
"Given a text node retrieves the rectangles for everyone of its paragraphs and its text."
|
"Given a text node retrieves the rectangles for everyone of its paragraphs and its text."
|
||||||
[parent-node direction text-node text-align]
|
[parent-node direction text-node text-align]
|
||||||
|
|
||||||
(letfn [(parse-entry [^js entry]
|
(letfn [(parse-entry [^js entry]
|
||||||
(when (some? (.-position entry))
|
(when (some? (.-position entry))
|
||||||
{:node (.-node entry)
|
{:node (.-node entry)
|
||||||
:position (dom/bounding-rect->rect (.-position entry))
|
:position (dom/bounding-rect->rect (.-position entry))
|
||||||
:text (.-text entry)
|
:text (.-text entry)
|
||||||
:direction direction}))]
|
:direction direction}))]
|
||||||
(into
|
(into []
|
||||||
[]
|
(keep parse-entry)
|
||||||
(keep parse-entry)
|
(tpd/parse-text-nodes parent-node text-node text-align))))
|
||||||
(tpd/parse-text-nodes parent-node text-node text-align))))
|
|
||||||
|
|
||||||
(def load-promises (atom {}))
|
(def load-promises (atom {}))
|
||||||
|
|
||||||
|
@ -40,51 +39,51 @@
|
||||||
load-promise)))
|
load-promise)))
|
||||||
|
|
||||||
(defn resolve-font
|
(defn resolve-font
|
||||||
[^js node]
|
[node]
|
||||||
|
(let [styles (dom/get-computed-styles node)
|
||||||
|
font (dom/get-property-value styles "font")
|
||||||
|
font (if (or (not font) (empty? font))
|
||||||
|
;; Firefox 95 won't return the font correctly.
|
||||||
|
;; We can get the font shorthand with the font-size + font-family
|
||||||
|
(str/ffmt "% %"
|
||||||
|
(dom/get-property-value styles "font-size")
|
||||||
|
(dom/get-property-value styles "font-family"))
|
||||||
|
font)
|
||||||
|
|
||||||
(let [styles (js/getComputedStyle node)
|
font-id (dom/get-property-value styles "--font-id")]
|
||||||
font (.getPropertyValue styles "font")
|
|
||||||
font (if (or (not font) (empty? font))
|
|
||||||
;; Firefox 95 won't return the font correctly.
|
|
||||||
;; We can get the font shorthand with the font-size + font-family
|
|
||||||
(dm/str (.getPropertyValue styles "font-size")
|
|
||||||
" "
|
|
||||||
(.getPropertyValue styles "font-family"))
|
|
||||||
font)
|
|
||||||
|
|
||||||
font-id (.getPropertyValue styles "--font-id")]
|
(->> (fonts/ensure-loaded! font-id)
|
||||||
|
(p/fmap (fn []
|
||||||
|
(when-not ^boolean (dom/check-font? font)
|
||||||
|
(load-font font))))
|
||||||
|
(p/merr (fn [_cause]
|
||||||
|
(js/console.error (str/ffmt "Cannot load font %" font-id))
|
||||||
|
(p/resolved nil))))))
|
||||||
|
|
||||||
(-> (fonts/ensure-loaded! font-id)
|
|
||||||
(p/then #(when (not (dom/check-font? font))
|
(defn- process-text-node
|
||||||
(load-font font)))
|
[parent-node]
|
||||||
(p/catch #(.error js/console (dm/str "Cannot load font " font-id) %)))))
|
(let [root (dom/get-parent-with-selector parent-node ".text-node-html")
|
||||||
|
paragraph (dom/get-parent-with-selector parent-node ".paragraph")
|
||||||
|
shape-x (d/parse-double (dom/get-attribute root "data-x"))
|
||||||
|
shape-y (d/parse-double (dom/get-attribute root "data-y"))
|
||||||
|
direction (.-direction ^js (dom/get-computed-styles parent-node))
|
||||||
|
text-align (.-textAlign ^js (dom/get-computed-styles paragraph))]
|
||||||
|
|
||||||
|
(sequence
|
||||||
|
(comp
|
||||||
|
(mapcat #(parse-text-nodes parent-node direction % text-align))
|
||||||
|
(map #(-> %
|
||||||
|
(update-in [:position :x] + shape-x)
|
||||||
|
(update-in [:position :y] + shape-y))))
|
||||||
|
(seq (.-childNodes parent-node)))))
|
||||||
|
|
||||||
(defn- calc-text-node-positions
|
(defn- calc-text-node-positions
|
||||||
[shape-id]
|
[shape-id]
|
||||||
|
(let [text-nodes (-> (dom/query (dm/fmt "#html-text-node-%" shape-id))
|
||||||
(when (some? shape-id)
|
(dom/query-all ".text-node"))]
|
||||||
(let [text-nodes (-> (dom/query (dm/fmt "#html-text-node-%" shape-id))
|
(->> (p/all (map resolve-font text-nodes))
|
||||||
(dom/query-all ".text-node"))
|
(p/fmap #(mapcat process-text-node text-nodes)))))
|
||||||
load-fonts (->> text-nodes (map resolve-font))
|
|
||||||
|
|
||||||
process-text-node
|
|
||||||
(fn [parent-node]
|
|
||||||
(let [root (dom/get-parent-with-selector parent-node ".text-node-html")
|
|
||||||
paragraph (dom/get-parent-with-selector parent-node ".paragraph")
|
|
||||||
shape-x (-> (dom/get-attribute root "data-x") d/parse-double)
|
|
||||||
shape-y (-> (dom/get-attribute root "data-y") d/parse-double)
|
|
||||||
direction (.-direction (js/getComputedStyle parent-node))
|
|
||||||
text-align (.-textAlign (js/getComputedStyle paragraph))]
|
|
||||||
|
|
||||||
(->> (.-childNodes parent-node)
|
|
||||||
(mapcat #(parse-text-nodes parent-node direction % text-align))
|
|
||||||
(mapv #(-> %
|
|
||||||
(update-in [:position :x] + shape-x)
|
|
||||||
(update-in [:position :y] + shape-y))))))]
|
|
||||||
(-> (p/all load-fonts)
|
|
||||||
(p/then
|
|
||||||
(fn []
|
|
||||||
(->> text-nodes (mapcat process-text-node))))))))
|
|
||||||
|
|
||||||
(defn calc-position-data
|
(defn calc-position-data
|
||||||
[shape-id]
|
[shape-id]
|
||||||
|
@ -94,7 +93,7 @@
|
||||||
value)))
|
value)))
|
||||||
|
|
||||||
(transform-data [{:keys [node position text direction]}]
|
(transform-data [{:keys [node position text direction]}]
|
||||||
(let [styles (js/getComputedStyle ^js node)
|
(let [styles (dom/get-computed-styles node)
|
||||||
position (assoc position :y (+ (dm/get-prop position :y)
|
position (assoc position :y (+ (dm/get-prop position :y)
|
||||||
(dm/get-prop position :height)))]
|
(dm/get-prop position :height)))]
|
||||||
(into position (filter val)
|
(into position (filter val)
|
||||||
|
|
Loading…
Reference in a new issue