mirror of
https://github.com/penpot/penpot.git
synced 2025-03-13 16:21:57 -05:00
✨ Plugins support for code generation
This commit is contained in:
parent
8ff0015458
commit
f86156b619
4 changed files with 61 additions and 10 deletions
|
@ -244,7 +244,54 @@
|
|||
(let [ids (into #{} (map #(obj/get % "$id")) shapes)
|
||||
id-ret (atom nil)]
|
||||
(st/emit! (dwb/create-bool bool-type ids {:id-ret id-ret}))
|
||||
(shape/shape-proxy $plugin @id-ret))))))
|
||||
(shape/shape-proxy $plugin @id-ret)))))
|
||||
|
||||
(generateMarkup
|
||||
[_ shapes options]
|
||||
(let [type (d/nilv (obj/get options "type") "html")]
|
||||
(cond
|
||||
(or (not (array? shapes)) (not (every? shape/shape-proxy? shapes)))
|
||||
(u/display-not-valid :generateMarkup-shapes shapes)
|
||||
|
||||
(and (some? type) (not (contains? #{"html" "svg"} type)))
|
||||
(u/display-not-valid :generateMarkup-type type)
|
||||
|
||||
:else
|
||||
(let [objects (u/locate-objects)
|
||||
shapes (into [] (map u/proxy->shape) shapes)]
|
||||
(cg/generate-markup-code objects type shapes)))))
|
||||
|
||||
(generateStyle
|
||||
[_ shapes options]
|
||||
(let [type (d/nilv (obj/get options "type") "css")
|
||||
prelude? (d/nilv (obj/get options "withPrelude") false)
|
||||
children? (d/nilv (obj/get options "includeChildren") true)]
|
||||
(cond
|
||||
(or (not (array? shapes)) (not (every? shape/shape-proxy? shapes)))
|
||||
(u/display-not-valid :generateStyle-shapes shapes)
|
||||
|
||||
(and (some? type) (not (contains? #{"css"} type)))
|
||||
(u/display-not-valid :generateStyle-type type)
|
||||
|
||||
(and (some? prelude?) (not (boolean? prelude?)))
|
||||
(u/display-not-valid :generateStyle-withPrelude prelude?)
|
||||
|
||||
(and (some? children?) (not (boolean? children?)))
|
||||
(u/display-not-valid :generateStyle-includeChildren children?)
|
||||
|
||||
:else
|
||||
(let [objects (u/locate-objects)
|
||||
shapes
|
||||
(->> (into #{} (map u/proxy->shape) shapes)
|
||||
(cfh/clean-loops objects))
|
||||
|
||||
shapes-with-children
|
||||
(if children?
|
||||
(->> shapes
|
||||
(mapcat #(cfh/get-children-with-self objects (:id %))))
|
||||
shapes)]
|
||||
(cg/generate-style-code
|
||||
objects type shapes shapes-with-children {:with-prelude? prelude?}))))))
|
||||
|
||||
(defn create-context
|
||||
[plugin-id]
|
||||
|
|
|
@ -32,8 +32,10 @@
|
|||
(dm/get-in (locate-file file-id) [:data :pages-index id]))
|
||||
|
||||
(defn locate-objects
|
||||
[file-id page-id]
|
||||
(:objects (locate-page file-id page-id)))
|
||||
([]
|
||||
(locate-objects (:current-file-id @st/state) (:current-page-id @st/state)))
|
||||
([file-id page-id]
|
||||
(:objects (locate-page file-id page-id))))
|
||||
|
||||
(defn locate-shape
|
||||
[file-id page-id id]
|
||||
|
|
|
@ -19,8 +19,10 @@
|
|||
(generate-markup objects shapes)))
|
||||
|
||||
(defn generate-style-code
|
||||
[objects type root-shapes all-shapes]
|
||||
(let [generate-style
|
||||
(case type
|
||||
"css" css/generate-style)]
|
||||
(generate-style objects root-shapes all-shapes)))
|
||||
([objects type root-shapes all-shapes]
|
||||
(generate-style-code objects type root-shapes all-shapes nil))
|
||||
([objects type root-shapes all-shapes options]
|
||||
(let [generate-style
|
||||
(case type
|
||||
"css" css/generate-style)]
|
||||
(generate-style objects root-shapes all-shapes options))))
|
||||
|
|
|
@ -300,10 +300,10 @@ body {
|
|||
(defn generate-style
|
||||
([objects root-shapes all-shapes]
|
||||
(generate-style objects root-shapes all-shapes nil))
|
||||
([objects root-shapes all-shapes options]
|
||||
([objects root-shapes all-shapes {:keys [with-prelude?] :or {with-prelude? true} :as options}]
|
||||
(let [options (assoc options :root-shapes (into #{} (map :id) root-shapes))]
|
||||
(dm/str
|
||||
prelude
|
||||
(if with-prelude? prelude "")
|
||||
(->> all-shapes
|
||||
(keep #(get-shape-css-selector % objects options))
|
||||
(str/join "\n\n"))))))
|
||||
|
|
Loading…
Add table
Reference in a new issue