diff --git a/frontend/src/app/plugins/api.cljs b/frontend/src/app/plugins/api.cljs index c43191eda..adf18684a 100644 --- a/frontend/src/app/plugins/api.cljs +++ b/frontend/src/app/plugins/api.cljs @@ -26,7 +26,7 @@ [app.plugins.page :as page] [app.plugins.shape :as shape] [app.plugins.user :as user] - [app.plugins.utils :as utils] + [app.plugins.utils :as u] [app.plugins.viewport :as viewport] [app.util.object :as obj] [beicon.v2.core :as rx] @@ -61,7 +61,7 @@ (getViewport [_] - (viewport/create-proxy $plugin)) + (viewport/viewport-proxy $plugin)) (getFile [_] @@ -108,28 +108,50 @@ (uploadMediaUrl [_ name url] - (let [file-id (:current-file-id @st/state)] - (p/create - (fn [resolve reject] - (->> (dwm/upload-media-url name file-id url) - (rx/map utils/to-js) - (rx/take 1) - (rx/subs! resolve reject)))))) + (cond + (not (string? name)) + (u/display-not-valid :uploadMedia-name name) + + (not (string? url)) + (u/display-not-valid :uploadMedia-url url) + + :else + (let [file-id (:current-file-id @st/state)] + (p/create + (fn [resolve reject] + (->> (dwm/upload-media-url name file-id url) + (rx/map u/to-js) + (rx/take 1) + (rx/subs! resolve reject))))))) (group [_ shapes] - (let [file-id (:current-file-id @st/state) - page-id (:current-page-id @st/state) - id (uuid/next) - ids (into #{} (map #(obj/get % "$id")) shapes)] - (st/emit! (dwg/group-shapes id ids)) - (shape/shape-proxy $plugin file-id page-id id))) + (cond + (or (not (array? shapes)) (not (every? shape/shape-proxy? shapes))) + (u/display-not-valid :group-shapes shapes) + + :else + (let [file-id (:current-file-id @st/state) + page-id (:current-page-id @st/state) + id (uuid/next) + ids (into #{} (map #(obj/get % "$id")) shapes)] + (st/emit! (dwg/group-shapes id ids)) + (shape/shape-proxy $plugin file-id page-id id)))) (ungroup [_ group & rest] - (let [shapes (concat [group] rest) - ids (into #{} (map #(obj/get % "$id")) shapes)] - (st/emit! (dwg/ungroup-shapes ids)))) + + (cond + (not (shape/shape-proxy? group)) + (u/display-not-valid :ungroup group) + + (and (some? rest) (not (every? shape/shape-proxy? rest))) + (u/display-not-valid :ungroup rest) + + :else + (let [shapes (concat [group] rest) + ids (into #{} (map #(obj/get % "$id")) shapes)] + (st/emit! (dwg/ungroup-shapes ids))))) (createFrame [_] @@ -161,23 +183,32 @@ (createText [_ text] - (let [file-id (:current-file-id @st/state) - page-id (:current-page-id @st/state) - page (dm/get-in @st/state [:workspace-data :pages-index page-id]) - shape (-> (cts/setup-shape {:type :text :x 0 :y 0 :grow-type :auto-width}) - (txt/change-text text) - (assoc :position-data nil)) - changes - (-> (cb/empty-changes) - (cb/with-page page) - (cb/with-objects (:objects page)) - (cb/add-object shape))] - (st/emit! (ch/commit-changes changes)) - (shape/shape-proxy $plugin file-id page-id (:id shape)))) + (cond + (or (not (string? text)) (empty? text)) + (u/display-not-valid :createText text) + + :else + (let [file-id (:current-file-id @st/state) + page-id (:current-page-id @st/state) + page (dm/get-in @st/state [:workspace-data :pages-index page-id]) + shape (-> (cts/setup-shape {:type :text :x 0 :y 0 :grow-type :auto-width}) + (txt/change-text text) + (assoc :position-data nil)) + changes + (-> (cb/empty-changes) + (cb/with-page page) + (cb/with-objects (:objects page)) + (cb/add-object shape))] + (st/emit! (ch/commit-changes changes)) + (shape/shape-proxy $plugin file-id page-id (:id shape))))) (createShapeFromSvg [_ svg-string] - (when (some? svg-string) + (cond + (not (string? svg-string)) + (u/display-not-valid :createShapeFromSvg svg-string) + + :else (let [id (uuid/next) file-id (:current-file-id @st/state) page-id (:current-page-id @st/state)] @@ -185,15 +216,19 @@ (shape/shape-proxy $plugin file-id page-id id)))) (createBoolean [_ bool-type shapes] - (let [ids (into #{} (map #(obj/get % "$id")) shapes) - bool-type (keyword bool-type)] + (let [bool-type (keyword bool-type)] + (cond + (not (contains? cts/bool-types bool-type)) + (u/display-not-valid :createBoolean-boolType bool-type) - (if (contains? cts/bool-types bool-type) - (let [id-ret (atom nil)] + (or (not (array? shapes)) (empty? shapes) (not (every? shape/shape-proxy? shapes))) + (u/display-not-valid :createBoolean-shapes shapes) + + :else + (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)) - - (utils/display-not-valid :bool-shape bool-type))))) + (shape/shape-proxy $plugin @id-ret)))))) (defn create-context [plugin-id] diff --git a/frontend/src/app/plugins/file.cljs b/frontend/src/app/plugins/file.cljs index 1fd74e29e..9f8ca7380 100644 --- a/frontend/src/app/plugins/file.cljs +++ b/frontend/src/app/plugins/file.cljs @@ -93,6 +93,9 @@ {:name js/Symbol.toStringTag :get (fn [] (str "FileProxy"))}) +(defn file-proxy? [p] + (instance? FileProxy p)) + (defn file-proxy [plugin-id id] (crc/add-properties! diff --git a/frontend/src/app/plugins/flex.cljs b/frontend/src/app/plugins/flex.cljs index e379fcba4..2f7b50bb7 100644 --- a/frontend/src/app/plugins/flex.cljs +++ b/frontend/src/app/plugins/flex.cljs @@ -29,6 +29,9 @@ (st/emit! (dwt/move-shapes-to-frame #{child-id} $id nil nil) (ptk/data-event :layout/update {:ids [$id]}))))) +(defn flex-layout-proxy? [p] + (instance? FlexLayout p)) + (defn flex-layout-proxy [plugin-id file-id page-id id] (-> (FlexLayout. plugin-id file-id page-id id) @@ -151,6 +154,9 @@ (deftype LayoutChildProxy [$plugin $file $page $id]) +(defn layout-child-proxy? [p] + (instance? LayoutChildProxy p)) + (defn layout-child-proxy [plugin-id file-id page-id id] (-> (LayoutChildProxy. plugin-id file-id page-id id) diff --git a/frontend/src/app/plugins/fonts.cljs b/frontend/src/app/plugins/fonts.cljs index 8455ec72b..fec42342d 100644 --- a/frontend/src/app/plugins/fonts.cljs +++ b/frontend/src/app/plugins/fonts.cljs @@ -39,6 +39,9 @@ :font-weight (d/nilv (obj/get variant "fontWeight") fontWeight)}] (st/emit! (dwt/update-text-range id start end values))))) +(defn font-proxy? [p] + (instance? PenpotFont p)) + (defn font-proxy [{:keys [id name variants] :as font}] (when (some? font) diff --git a/frontend/src/app/plugins/grid.cljs b/frontend/src/app/plugins/grid.cljs index 8b0f49ab9..1d9d4799f 100644 --- a/frontend/src/app/plugins/grid.cljs +++ b/frontend/src/app/plugins/grid.cljs @@ -74,6 +74,9 @@ (st/emit! (dwt/move-shapes-to-frame #{child-id} $id nil [row column]) (ptk/data-event :layout/update {:ids [$id]}))))) +(defn grid-layout-proxy? [p] + (instance? GridLayout p)) + (defn grid-layout-proxy [plugin-id file-id page-id id] (-> (GridLayout. plugin-id file-id page-id id) @@ -199,6 +202,9 @@ (deftype GridCellProxy [$plugin $file $page $id]) +(defn layout-cell-proxy? [p] + (instance? GridCellProxy p)) + (defn layout-cell-proxy [plugin-id file-id page-id id] (letfn [(locate-cell [_] diff --git a/frontend/src/app/plugins/library.cljs b/frontend/src/app/plugins/library.cljs index 380ff68c3..8107ca375 100644 --- a/frontend/src/app/plugins/library.cljs +++ b/frontend/src/app/plugins/library.cljs @@ -137,6 +137,9 @@ (let [color (u/proxy->library-color self)] (apply array (keys (dm/get-in color [:plugin-data (keyword "shared" namespace)]))))))) +(defn lib-color-proxy? [p] + (instance? LibraryColorProxy p)) + (defn lib-color-proxy [plugin-id file-id id] (assert (uuid? file-id)) @@ -316,6 +319,9 @@ (let [typography (u/proxy->library-typography self)] (apply array (keys (dm/get-in typography [:plugin-data (keyword "shared" namespace)]))))))) +(defn lib-typography-proxy? [p] + (instance? LibraryTypographyProxy p)) + (defn lib-typography-proxy [plugin-id file-id id] (assert (uuid? file-id)) @@ -522,6 +528,9 @@ (let [component (u/proxy->library-component self)] (apply array (keys (dm/get-in component [:plugin-data (keyword "shared" namespace)]))))))) +(defn lib-component-proxy? [p] + (instance? LibraryComponentProxy p)) + (defn lib-component-proxy [plugin-id file-id id] (assert (uuid? file-id)) @@ -643,6 +652,9 @@ (let [file (u/proxy->file self)] (apply array (keys (dm/get-in file [:data :plugin-data (keyword "shared" namespace)]))))))) +(defn library-proxy? [p] + (instance? Library p)) + (defn library-proxy [plugin-id file-id] (assert (uuid? file-id) "File id not valid") diff --git a/frontend/src/app/plugins/page.cljs b/frontend/src/app/plugins/page.cljs index 35c1a9dd5..f63cfdd9d 100644 --- a/frontend/src/app/plugins/page.cljs +++ b/frontend/src/app/plugins/page.cljs @@ -107,6 +107,9 @@ {:name js/Symbol.toStringTag :get (fn [] (str "PageProxy"))}) +(defn page-proxy? [p] + (instance? PageProxy p)) + (defn page-proxy [plugin-id file-id id] (crc/add-properties! diff --git a/frontend/src/app/plugins/shape.cljs b/frontend/src/app/plugins/shape.cljs index 2048e8afe..5fcf8ce28 100644 --- a/frontend/src/app/plugins/shape.cljs +++ b/frontend/src/app/plugins/shape.cljs @@ -380,6 +380,9 @@ {:name js/Symbol.toStringTag :get (fn [] (str "ShapeProxy"))}) +(defn shape-proxy? [p] + (instance? ShapeProxy p)) + (defn shape-proxy ([plugin-id id] (shape-proxy plugin-id (:current-file-id @st/state) (:current-page-id @st/state) id)) diff --git a/frontend/src/app/plugins/user.cljs b/frontend/src/app/plugins/user.cljs index eb199f5a6..42cd32f6e 100644 --- a/frontend/src/app/plugins/user.cljs +++ b/frontend/src/app/plugins/user.cljs @@ -38,11 +38,17 @@ {:name "sessionId" :get (fn [_] (str session-id))}))) +(defn current-user-proxy? [p] + (instance? CurrentUserProxy p)) + (defn current-user-proxy [plugin-id session-id] (-> (CurrentUserProxy. plugin-id session-id) (add-user-properties))) +(defn active-user-proxy? [p] + (instance? ActiveUserProxy p)) + (defn active-user-proxy [plugin-id session-id] (-> (ActiveUserProxy. plugin-id session-id) diff --git a/frontend/src/app/plugins/viewport.cljs b/frontend/src/app/plugins/viewport.cljs index 8859f6197..65e4423fb 100644 --- a/frontend/src/app/plugins/viewport.cljs +++ b/frontend/src/app/plugins/viewport.cljs @@ -32,7 +32,10 @@ {:name js/Symbol.toStringTag :get (fn [] (str "ViewportProxy"))}) -(defn create-proxy +(defn viewport-proxy? [p] + (instance? ViewportProxy p)) + +(defn viewport-proxy [plugin-id] (crc/add-properties! (ViewportProxy. plugin-id)