diff --git a/frontend/src/app/plugins/api.cljs b/frontend/src/app/plugins/api.cljs index 4de6700f8a..6ebdfa441b 100644 --- a/frontend/src/app/plugins/api.cljs +++ b/frontend/src/app/plugins/api.cljs @@ -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] diff --git a/frontend/src/app/plugins/utils.cljs b/frontend/src/app/plugins/utils.cljs index da600d4476..ab3015a0b5 100644 --- a/frontend/src/app/plugins/utils.cljs +++ b/frontend/src/app/plugins/utils.cljs @@ -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] diff --git a/frontend/src/app/util/code_gen.cljs b/frontend/src/app/util/code_gen.cljs index 1c8e255b9c..99c5a4e89c 100644 --- a/frontend/src/app/util/code_gen.cljs +++ b/frontend/src/app/util/code_gen.cljs @@ -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)))) diff --git a/frontend/src/app/util/code_gen/style_css.cljs b/frontend/src/app/util/code_gen/style_css.cljs index 3a1ace59f6..8ebc10be21 100644 --- a/frontend/src/app/util/code_gen/style_css.cljs +++ b/frontend/src/app/util/code_gen/style_css.cljs @@ -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"))))))