diff --git a/frontend/src/lib/file_builder.cljs b/frontend/src/lib/file_builder.cljs index cf09d7ee2..2121fb807 100644 --- a/frontend/src/lib/file_builder.cljs +++ b/frontend/src/lib/file_builder.cljs @@ -58,193 +58,198 @@ (json/->js params) params)) -(defn- create-file* +(defn- create-file-api [file] - (let [state* (volatile! file)] - (obj/reify {:name "File"} - :id - {:get #(dm/str (:id @state*))} + (let [state* (volatile! file) + api (obj/reify {:name "File"} + :id + {:get #(dm/str (:id @state*))} - :currentFrameId - {:get #(dm/str (::fb/current-frame-id @state*))} + :currentFrameId + {:get #(dm/str (::fb/current-frame-id @state*))} - :currentPageId - {:get #(dm/str (::fb/current-page-id @state*))} + :currentPageId + {:get #(dm/str (::fb/current-page-id @state*))} - :lastId - {:get #(dm/str (::fb/last-id @state*))} + :lastId + {:get #(dm/str (::fb/last-id @state*))} - :addPage - (fn [params] - (try - (let [params (-> params - (decode-params) - (fb/decode-page))] - (vswap! state* fb/add-page params) - (dm/str (::fb/current-page-id @state*))) - (catch :default cause - (handle-exception cause)))) + :addPage + (fn [params] + (try + (let [params (-> params + (decode-params) + (fb/decode-page))] + (vswap! state* fb/add-page params) + (dm/str (::fb/current-page-id @state*))) + (catch :default cause + (handle-exception cause)))) - :closePage - (fn [] - (vswap! state* fb/close-page)) + :closePage + (fn [] + (vswap! state* fb/close-page)) - :addArtboard - (fn [params] - (try - (let [params (-> params - (json/->clj) - (assoc :type :frame) - (fb/decode-shape))] - (vswap! state* fb/add-artboard params) - (dm/str (::fb/last-id @state*))) - (catch :default cause - (handle-exception cause)))) + :addArtboard + (fn [params] + (try + (let [params (-> params + (json/->clj) + (assoc :type :frame) + (fb/decode-shape))] + (vswap! state* fb/add-artboard params) + (dm/str (::fb/last-id @state*))) + (catch :default cause + (handle-exception cause)))) - :closeArtboard - (fn [] - (vswap! state* fb/close-artboard)) + :closeArtboard + (fn [] + (vswap! state* fb/close-artboard)) - :addGroup - (fn [params] - (try - (let [params (-> params - (json/->clj) - (assoc :type :group) - (fb/decode-shape))] - (vswap! state* fb/add-group params) - (dm/str (::fb/last-id @state*))) - (catch :default cause - (handle-exception cause)))) + :addGroup + (fn [params] + (try + (let [params (-> params + (json/->clj) + (assoc :type :group) + (fb/decode-shape))] + (vswap! state* fb/add-group params) + (dm/str (::fb/last-id @state*))) + (catch :default cause + (handle-exception cause)))) - :closeGroup - (fn [] - (vswap! state* fb/close-group)) + :closeGroup + (fn [] + (vswap! state* fb/close-group)) - :addBool - (fn [params] - (try - (let [params (-> params - (json/->clj) - (fb/decode-add-bool))] - (vswap! state* fb/add-bool params) - (dm/str (::fb/last-id @state*))) - (catch :default cause - (handle-exception cause)))) + :addBool + (fn [params] + (try + (let [params (-> params + (json/->clj) + (fb/decode-add-bool))] + (vswap! state* fb/add-bool params) + (dm/str (::fb/last-id @state*))) + (catch :default cause + (handle-exception cause)))) - :addRect - (fn [params] - (try - (let [params (-> params - (json/->clj) - (assoc :type :rect) - (fb/decode-shape))] - (vswap! state* fb/add-shape params) - (dm/str (::fb/last-id @state*))) - (catch :default cause - (handle-exception cause)))) + :addRect + (fn [params] + (try + (let [params (-> params + (json/->clj) + (assoc :type :rect) + (fb/decode-shape))] + (vswap! state* fb/add-shape params) + (dm/str (::fb/last-id @state*))) + (catch :default cause + (handle-exception cause)))) - :addCircle - (fn [params] - (try - (let [params (-> params - (json/->clj) - (assoc :type :circle) - (fb/decode-shape))] - (vswap! state* fb/add-shape params) - (dm/str (::fb/last-id @state*))) - (catch :default cause - (handle-exception cause)))) + :addCircle + (fn [params] + (try + (let [params (-> params + (json/->clj) + (assoc :type :circle) + (fb/decode-shape))] + (vswap! state* fb/add-shape params) + (dm/str (::fb/last-id @state*))) + (catch :default cause + (handle-exception cause)))) - :addPath - (fn [params] - (try - (let [params (-> params - (json/->clj) - (assoc :type :path) - (fb/decode-shape))] - (vswap! state* fb/add-shape params) - (dm/str (::fb/last-id @state*))) - (catch :default cause - (handle-exception cause)))) + :addPath + (fn [params] + (try + (let [params (-> params + (json/->clj) + (assoc :type :path) + (fb/decode-shape))] + (vswap! state* fb/add-shape params) + (dm/str (::fb/last-id @state*))) + (catch :default cause + (handle-exception cause)))) - :addText - (fn [params] - (try - (let [params (-> params - (json/->clj) - (assoc :type :text) - (fb/decode-shape))] - (vswap! state* fb/add-shape params) - (dm/str (::fb/last-id @state*))) - (catch :default cause - (handle-exception cause)))) + :addText + (fn [params] + (try + (let [params (-> params + (json/->clj) + (assoc :type :text) + (fb/decode-shape))] + (vswap! state* fb/add-shape params) + (dm/str (::fb/last-id @state*))) + (catch :default cause + (handle-exception cause)))) - :addLibraryColor - (fn [params] - (try - (let [params (-> params - (json/->clj) - (fb/decode-library-color) - (d/without-nils))] - (vswap! state* fb/add-library-color params) - (dm/str (::fb/last-id @state*))) - (catch :default cause - (handle-exception cause)))) + :addLibraryColor + (fn [params] + (try + (let [params (-> params + (json/->clj) + (fb/decode-library-color) + (d/without-nils))] + (vswap! state* fb/add-library-color params) + (dm/str (::fb/last-id @state*))) + (catch :default cause + (handle-exception cause)))) - :addLibraryTypography - (fn [params] - (try - (let [params (-> params - (json/->clj) - (fb/decode-library-typography) - (d/without-nils))] - (vswap! state* fb/add-library-typography params) - (dm/str (::fb/last-id @state*))) - (catch :default cause - (handle-exception cause)))) + :addLibraryTypography + (fn [params] + (try + (let [params (-> params + (json/->clj) + (fb/decode-library-typography) + (d/without-nils))] + (vswap! state* fb/add-library-typography params) + (dm/str (::fb/last-id @state*))) + (catch :default cause + (handle-exception cause)))) - :addComponent - (fn [params] - (try - (let [params (-> params - (json/->clj) - (fb/decode-component) - (d/without-nils))] - (vswap! state* fb/add-component params) - (dm/str (::fb/last-id @state*))) - (catch :default cause - (handle-exception cause)))) + :addComponent + (fn [params] + (try + (let [params (-> params + (json/->clj) + (fb/decode-component) + (d/without-nils))] + (vswap! state* fb/add-component params) + (dm/str (::fb/last-id @state*))) + (catch :default cause + (handle-exception cause)))) - :addComponentInstance - (fn [params] - (try - (let [params (-> params - (json/->clj) - (fb/decode-add-component-instance) - (d/without-nils))] - (vswap! state* fb/add-component-instance params) - (dm/str (::fb/last-id @state*))) - (catch :default cause - (handle-exception cause)))) + :addComponentInstance + (fn [params] + (try + (let [params (-> params + (json/->clj) + (fb/decode-add-component-instance) + (d/without-nils))] + (vswap! state* fb/add-component-instance params) + (dm/str (::fb/last-id @state*))) + (catch :default cause + (handle-exception cause)))) - :getShape - (fn [shape-id] - (let [shape-id (uuid/parse shape-id)] - (some-> (fb/lookup-shape @state* shape-id) - (json/->js)))) + :getShape + (fn [shape-id] + (let [shape-id (uuid/parse shape-id)] + (some-> (fb/lookup-shape @state* shape-id) + (json/->js)))) - :toMap - (fn [] - (-> @state* - (d/without-qualified) - (json/->js)))))) + :toMap + (fn [] + (-> @state* + (d/without-qualified) + (json/->js))))] + + (specify! api + cljs.core/IDeref + (-deref [_] + (d/without-qualified @state*))))) (defn create-file [params] (try (let [params (-> params json/->clj fb/decode-file) file (fb/create-file params)] - (create-file* file)) + (create-file-api file)) (catch :default cause (handle-exception cause))))