From 9cba125abe0540cbdfc5c9effb59a07d7d3f5585 Mon Sep 17 00:00:00 2001 From: Andrey Antukh Date: Fri, 22 Dec 2023 16:09:31 +0100 Subject: [PATCH] :sparkles: Modularize js-beautify library related code --- frontend/src/app/main/data/preview.cljs | 15 +++-------- .../src/app/main/ui/viewer/inspect/code.cljs | 15 +++-------- frontend/src/app/util/code_beautify.cljs | 26 +++++++++++++++++++ 3 files changed, 32 insertions(+), 24 deletions(-) create mode 100644 frontend/src/app/util/code_beautify.cljs diff --git a/frontend/src/app/main/data/preview.cljs b/frontend/src/app/main/data/preview.cljs index 452929385..519ed96d3 100644 --- a/frontend/src/app/main/data/preview.cljs +++ b/frontend/src/app/main/data/preview.cljs @@ -6,7 +6,6 @@ (ns app.main.data.preview (:require - ["js-beautify" :as beautify] [app.common.data :as d] [app.common.data.macros :as dm] [app.common.files.helpers :as cfh] @@ -14,6 +13,7 @@ [app.main.data.workspace.state-helpers :as wsh] [app.main.fonts :as fonts] [app.main.refs :as refs] + [app.util.code-beautify :as cb] [app.util.code-gen :as cg] [app.util.timers :as ts] [beicon.v2.core :as rx] @@ -38,15 +38,6 @@ ") -(defn format-code [code type] - (cond-> code - (= type "svg") - (-> (str/replace "" "") - (str/replace "><" ">\n<")) - - (or (= type "svg") (= type "html")) - (beautify/html #js {"indent_size" 2}))) - (defn update-preview-window [preview code width height] (when preview @@ -86,11 +77,11 @@ (dm/str fontfaces-css "\n" (-> (cg/generate-style-code objects style-type all-children) - (format-code style-type))) + (cb/format-code style-type))) markup-code (-> (cg/generate-markup-code objects markup-type [shape]) - (format-code markup-type))] + (cb/format-code markup-type))] (update-preview-window preview diff --git a/frontend/src/app/main/ui/viewer/inspect/code.cljs b/frontend/src/app/main/ui/viewer/inspect/code.cljs index b91b7df99..b80df099d 100644 --- a/frontend/src/app/main/ui/viewer/inspect/code.cljs +++ b/frontend/src/app/main/ui/viewer/inspect/code.cljs @@ -7,7 +7,6 @@ (ns app.main.ui.viewer.inspect.code (:require-macros [app.main.style :as stl]) (:require - ["js-beautify" :as beautify] [app.common.data :as d] [app.common.data.macros :as dm] [app.common.files.helpers :as cfh] @@ -26,6 +25,7 @@ [app.main.ui.hooks.resize :refer [use-resize-hook]] [app.main.ui.icons :as i] [app.main.ui.shapes.text.fontfaces :refer [shapes->fonts]] + [app.util.code-beautify :as cb] [app.util.code-gen :as cg] [app.util.dom :as dom] [app.util.http :as http] @@ -51,15 +51,6 @@ ") -(defn format-code [code type] - (cond-> code - (= type "svg") - (-> (str/replace "" "") - (str/replace "><" ">\n<")) - - (or (= type "svg") (= type "html")) - (beautify/html #js {"indent_size" 2}))) - (defn get-flex-elements [page-id shapes from] (let [ids (mapv :id shapes) ids (hooks/use-equal-memo ids) @@ -151,14 +142,14 @@ (dm/str fontfaces-css "\n" (-> (cg/generate-style-code objects style-type all-children) - (format-code style-type))))) + (cb/format-code style-type))))) markup-code (mf/use-memo (mf/deps markup-type shapes images-data) (fn [] (-> (cg/generate-markup-code objects markup-type shapes) - (format-code markup-type)))) + (cb/format-code markup-type)))) on-markup-copied (mf/use-callback diff --git a/frontend/src/app/util/code_beautify.cljs b/frontend/src/app/util/code_beautify.cljs new file mode 100644 index 000000000..c18ff885c --- /dev/null +++ b/frontend/src/app/util/code_beautify.cljs @@ -0,0 +1,26 @@ +;; This Source Code Form is subject to the terms of the Mozilla Public +;; License, v. 2.0. If a copy of the MPL was not distributed with this +;; file, You can obtain one at http://mozilla.org/MPL/2.0/. +;; +;; Copyright (c) KALEIDOS INC + +(ns app.util.code-beautify + (:require + ["js-beautify" :as beautify] + [cuerdas.core :as str])) + +(defn format-html + [data] + (beautify/html data #js {:indent_size 2})) + +(defn format-code + [code type] + (let [type (if (keyword? type) (name type) type)] + (cond-> code + (= type "svg") + (-> (str/replace "" "") + (str/replace "><" ">\n<")) + + (or (= type "svg") (= type "html")) + (format-html)))) +