diff --git a/frontend/src/app/main/data/workspace/libraries.cljs b/frontend/src/app/main/data/workspace/libraries.cljs index 318c7ecba..db0038c0b 100644 --- a/frontend/src/app/main/data/workspace/libraries.cljs +++ b/frontend/src/app/main/data/workspace/libraries.cljs @@ -537,7 +537,7 @@ (update [_ state] (-> state (update-in [:workspace-libraries file-id] - #(assoc % :modified-at modified-at :revn revn)) + assoc :modified-at modified-at :revn revn) (d/update-in-when [:workspace-libraries file-id :data] cp/process-changes changes))))) @@ -571,7 +571,10 @@ (defn update-component "Modify the component linked to the shape with the given id, in the current page, so that all attributes of its shapes are equal to the shape and its children. Also set all attributes - of the shape untouched." + of the shape untouched. + + NOTE: It's possible that the component to update is defined in an external library file, so + this function may cause to modify a file different of that the one we are currently editing." [id] (us/assert ::us/uuid id) (ptk/reify ::update-component @@ -593,22 +596,19 @@ file-id (:component-file shape) file (dwlh/get-file state file-id) - local-rchanges (->> rchanges - (filter :local-change?) - (map #(dissoc % :local-change?)) - vec) - local-uchanges (->> uchanges - (filter :local-change?) - (map #(dissoc % :local-change?)) - vec) - rchanges (->> rchanges - (remove :local-change?) - (map #(dissoc % :local-change?)) - vec) - uchanges (->> uchanges - (remove :local-change?) - (map #(dissoc % :local-change?)) - vec)] + xf-filter (comp + (filter :local-change?) + (map #(dissoc % :local-change?))) + + local-rchanges (into [] xf-filter rchanges) + local-uchanges (into [] xf-filter uchanges) + + xf-remove (comp + (remove :local-change?) + (map #(dissoc % :local-change?))) + + rchanges (into [] xf-remove rchanges) + uchanges (into [] xf-remove uchanges)] (log/debug :msg "UPDATE-COMPONENT finished" :js/local-rchanges (log-changes @@ -656,12 +656,17 @@ file-changes [(dwlh/generate-sync-file file-id :components library-id state) (dwlh/generate-sync-file file-id :colors library-id state) (dwlh/generate-sync-file file-id :typographies library-id state)] + + xf-fcat (comp (remove nil?) (map first) (mapcat identity)) rchanges (d/concat [] - (->> library-changes (remove nil?) (map first) (flatten)) - (->> file-changes (remove nil?) (map first) (flatten))) + (sequence xf-fcat library-changes) + (sequence xf-fcat file-changes)) + + xf-scat (comp (remove nil?) (map second) (mapcat identity)) uchanges (d/concat [] - (->> library-changes (remove nil?) (map second) (flatten)) - (->> file-changes (remove nil?) (map second) (flatten)))] + (sequence xf-scat library-changes) + (sequence xf-scat file-changes))] + (log/debug :msg "SYNC-FILE finished" :js/rchanges (log-changes rchanges file)) @@ -674,6 +679,9 @@ ;; When we have just updated the library file, give some time for the ;; update to finish, before marking this file as synced. ;; TODO: look for a more precise way of syncing this. + ;; Maybe by using the stream (second argument passed to watch) + ;; to wait for the corresponding changes-commited and then proced + ;; with the :update-sync mutation. (rx/concat (rx/timer 3000) (rp/mutation :update-sync {:file-id file-id diff --git a/frontend/src/app/main/data/workspace/libraries_helpers.cljs b/frontend/src/app/main/data/workspace/libraries_helpers.cljs index e27329c31..621503a42 100644 --- a/frontend/src/app/main/data/workspace/libraries_helpers.cljs +++ b/frontend/src/app/main/data/workspace/libraries_helpers.cljs @@ -714,8 +714,8 @@ (= (:id change) (:id shape-inst)) (assoc :local-change? true))) - rchanges (vec (map check-local rchanges)) - uchanges (vec (map check-local uchanges))] + rchanges (mapv check-local rchanges) + uchanges (mapv check-local uchanges)] [(d/concat rchanges child-rchanges) (d/concat uchanges child-uchanges)])) diff --git a/frontend/src/app/main/data/workspace/persistence.cljs b/frontend/src/app/main/data/workspace/persistence.cljs index ad53f2eaa..0e4615f62 100644 --- a/frontend/src/app/main/data/workspace/persistence.cljs +++ b/frontend/src/app/main/data/workspace/persistence.cljs @@ -55,12 +55,12 @@ (rx/debounce 2000) (rx/merge stoper forcer)) - local-file? #(let [event-file-id (:file-id %)] + local-file? #(as-> (:file-id %) event-file-id (or (nil? event-file-id) (= event-file-id file-id))) - library-file? #(let [event-file-id (:file-id %)] - (and (some? event-file-id) - (not= event-file-id file-id))) + library-file? #(as-> (:file-id %) event-file-id + (and (some? event-file-id) + (not= event-file-id file-id))) on-dirty (fn [] @@ -189,12 +189,11 @@ (update [_ state] (if (= file-id (:current-file-id state)) (-> state - (update-in [:workspace-file :revn] #(max % revn)) + (update-in [:workspace-file :revn] max revn) (update :workspace-data cp/process-changes changes) (update-in [:workspace-file :data] cp/process-changes changes)) (-> state - (update-in state [:workspace-libraries file-id :revn] - #(max % revn)) + (update-in state [:workspace-libraries file-id :revn] max revn) (update-in [:workspace-libraries file-id :data] cp/process-changes changes)))))) diff --git a/frontend/src/app/main/ui/workspace/context_menu.cljs b/frontend/src/app/main/ui/workspace/context_menu.cljs index 9bc4d122c..ca2343698 100644 --- a/frontend/src/app/main/ui/workspace/context_menu.cljs +++ b/frontend/src/app/main/ui/workspace/context_menu.cljs @@ -54,38 +54,37 @@ current-file-id (mf/use-ctx ctx/current-file-id) - do-duplicate #(st/emit! dw/duplicate-selected) - do-delete #(st/emit! dw/delete-selected) - do-copy #(st/emit! (dw/copy-selected)) - do-cut #(st/emit! (dw/copy-selected) dw/delete-selected) - do-paste #(st/emit! dw/paste) - do-bring-forward #(st/emit! (dw/vertical-order-selected :up)) - do-bring-to-front #(st/emit! (dw/vertical-order-selected :top)) - do-send-backward #(st/emit! (dw/vertical-order-selected :down)) - do-send-to-back #(st/emit! (dw/vertical-order-selected :bottom)) - do-show-shape #(st/emit! (dw/update-shape-flags id {:hidden false})) - do-hide-shape #(st/emit! (dw/update-shape-flags id {:hidden true})) - do-lock-shape #(st/emit! (dw/update-shape-flags id {:blocked true})) - do-unlock-shape #(st/emit! (dw/update-shape-flags id {:blocked false})) - do-create-group #(st/emit! dw/group-selected) - do-remove-group #(st/emit! dw/ungroup-selected) - do-mask-group #(st/emit! dw/mask-group) - do-unmask-group #(st/emit! dw/unmask-group) - do-add-component #(st/emit! dwl/add-component) - do-detach-component #(st/emit! (dwl/detach-component id)) - do-reset-component #(st/emit! (dwl/reset-component id)) - do-update-component #(do - (st/emit! (dwc/start-undo-transaction)) - (st/emit! (dwl/update-component id)) - (st/emit! (dwl/sync-file current-file-id - (:component-file shape))) - (st/emit! (dwc/commit-undo-transaction))) - confirm-update-remote-component #(do - (st/emit! (dwl/update-component id)) - (st/emit! (dwl/sync-file current-file-id - (:component-file shape))) - (st/emit! (dwl/sync-file (:component-file shape) - (:component-file shape)))) + do-duplicate (st/emitf dw/duplicate-selected) + do-delete (st/emitf dw/delete-selected) + do-copy (st/emitf (dw/copy-selected)) + do-cut (st/emitf (dw/copy-selected) dw/delete-selected) + do-paste (st/emitf dw/paste) + do-bring-forward (st/emitf (dw/vertical-order-selected :up)) + do-bring-to-front (st/emitf (dw/vertical-order-selected :top)) + do-send-backward (st/emitf (dw/vertical-order-selected :down)) + do-send-to-back (st/emitf (dw/vertical-order-selected :bottom)) + do-show-shape (st/emitf (dw/update-shape-flags id {:hidden false})) + do-hide-shape (st/emitf (dw/update-shape-flags id {:hidden true})) + do-lock-shape (st/emitf (dw/update-shape-flags id {:blocked true})) + do-unlock-shape (st/emitf (dw/update-shape-flags id {:blocked false})) + do-create-group (st/emitf dw/group-selected) + do-remove-group (st/emitf dw/ungroup-selected) + do-mask-group (st/emitf dw/mask-group) + do-unmask-group (st/emitf dw/unmask-group) + do-add-component (st/emitf dwl/add-component) + do-detach-component (st/emitf (dwl/detach-component id)) + do-reset-component (st/emitf (dwl/reset-component id)) + do-update-component (st/emitf + (dwc/start-undo-transaction) + (dwl/update-component id) + (dwl/sync-file current-file-id (:component-file shape)) + (dwc/commit-undo-transaction)) + confirm-update-remote-component (st/emitf + (dwl/update-component id) + (dwl/sync-file current-file-id + (:component-file shape)) + (dwl/sync-file (:component-file shape) + (:component-file shape))) do-update-remote-component (st/emitf (modal/show {:type :confirm :message "" @@ -95,9 +94,9 @@ :accept-label (t locale "modals.update-remote-component.accept") :accept-style :primary :on-accept confirm-update-remote-component})) - do-show-component #(st/emit! (dw/go-to-layout :assets)) - do-navigate-component-file #(st/emit! (dwl/nav-to-component-file - (:component-file shape)))] + do-show-component (st/emitf (dw/go-to-layout :assets)) + do-navigate-component-file (st/emitf (dwl/nav-to-component-file + (:component-file shape)))] [:* [:& menu-entry {:title (t locale "workspace.shape.menu.copy") :shortcut "Ctrl + c" @@ -204,7 +203,7 @@ (mf/defc viewport-context-menu [{:keys [mdata] :as props}] (let [locale (mf/deref i18n/locale) - do-paste #(st/emit! dw/paste)] + do-paste (st/emitf dw/paste)] [:* [:& menu-entry {:title (t locale "workspace.shape.menu.paste") :shortcut "Ctrl + v" @@ -231,7 +230,7 @@ (.setAttribute ^js dropdown "style" new-style)))))) [:& dropdown {:show (boolean mdata) - :on-close #(st/emit! dw/hide-context-menu)} + :on-close (st/emitf dw/hide-context-menu)} [:ul.workspace-context-menu {:ref dropdown-ref :style {:top top :left left} diff --git a/frontend/src/app/main/ui/workspace/sidebar/options/component.cljs b/frontend/src/app/main/ui/workspace/sidebar/options/component.cljs index 4b957fb8e..b5f8455e0 100644 --- a/frontend/src/app/main/ui/workspace/sidebar/options/component.cljs +++ b/frontend/src/app/main/ui/workspace/sidebar/options/component.cljs @@ -50,19 +50,19 @@ on-menu-close (mf/use-callback #(swap! local assoc :menu-open false)) - do-detach-component #(st/emit! (dwl/detach-component id)) - do-reset-component #(st/emit! (dwl/reset-component id)) - do-update-component #(do - (st/emit! (dwc/start-undo-transaction)) - (st/emit! (dwl/update-component id)) - (st/emit! (dwl/sync-file current-file-id current-file-id)) - (st/emit! (dwc/commit-undo-transaction))) - confirm-update-remote-component #(do - (st/emit! (dwl/update-component id)) - (st/emit! (dwl/sync-file current-file-id - (:component-file values))) - (st/emit! (dwl/sync-file (:component-file values) - (:component-file values)))) + do-detach-component (st/emitf (dwl/detach-component id)) + do-reset-component (st/emitf (dwl/reset-component id)) + do-update-component (st/emitf + (dwc/start-undo-transaction) + (dwl/update-component id) + (dwl/sync-file current-file-id current-file-id) + (dwc/commit-undo-transaction)) + confirm-update-remote-component (st/emitf + (dwl/update-component id) + (dwl/sync-file current-file-id + (:component-file values)) + (dwl/sync-file (:component-file values) + (:component-file values))) do-update-remote-component (st/emitf (modal/show {:type :confirm :message "" @@ -72,8 +72,8 @@ :accept-label (t locale "modals.update-remote-component.accept") :accept-style :primary :on-accept confirm-update-remote-component})) - do-show-component #(st/emit! (dw/go-to-layout :assets)) - do-navigate-component-file #(st/emit! (dwl/nav-to-component-file + do-show-component (st/emitf (dw/go-to-layout :assets)) + do-navigate-component-file (st/emitf (dwl/nav-to-component-file (:component-file values)))] (when show? [:div.element-set