diff --git a/frontend/src/app/main/data/workspace.cljs b/frontend/src/app/main/data/workspace.cljs index 266db1193..5ac1b297f 100644 --- a/frontend/src/app/main/data/workspace.cljs +++ b/frontend/src/app/main/data/workspace.cljs @@ -267,12 +267,10 @@ (ptk/reify ::finalize-page ptk/UpdateEvent (update [_ state] - (let [page-id (or page-id (get-in state [:workspace-data :pages 0])) - local (-> (:workspace-local state) - (dissoc - :edition - :edit-path - :selected))] + (let [local (-> (:workspace-local state) + (dissoc :edition + :edit-path + :selected))] (-> state (assoc-in [:workspace-cache page-id] local) (dissoc :current-page-id :workspace-local :trimmed-page :workspace-drawing)))))) @@ -349,8 +347,6 @@ (declare purge-page) (declare go-to-file) -;; TODO: properly handle positioning on undo. - ;; TODO: for some reason, the page-id here in some circumstances is `nil` (defn delete-page [id] @@ -670,6 +666,7 @@ (watch [_ _ _] (rx/of (dch/update-shapes [id] #(merge % attrs)))))) + (defn start-rename-shape [id] (us/verify ::us/uuid id) diff --git a/frontend/src/app/main/data/workspace/changes.cljs b/frontend/src/app/main/data/workspace/changes.cljs index a56579c5e..2b5a57c7d 100644 --- a/frontend/src/app/main/data/workspace/changes.cljs +++ b/frontend/src/app/main/data/workspace/changes.cljs @@ -114,7 +114,9 @@ :changes changes})))) (defn commit-changes - [{:keys [redo-changes undo-changes origin save-undo? file-id] :or {save-undo? true}}] + [{:keys [redo-changes undo-changes + origin save-undo? file-id] + :or {save-undo? true}}] (log/debug :msg "commit-changes" :js/redo-changes redo-changes :js/undo-changes undo-changes) diff --git a/frontend/src/app/main/data/workspace/common.cljs b/frontend/src/app/main/data/workspace/common.cljs index cba5c0600..1fb8fc85b 100644 --- a/frontend/src/app/main/data/workspace/common.cljs +++ b/frontend/src/app/main/data/workspace/common.cljs @@ -31,6 +31,12 @@ (s/def ::set-of-string (s/every string? :kind set?)) (s/def ::ordered-set-of-uuid (s/every uuid? :kind d/ordered-set?)) +(defn initialized? + "Check if the state is properly intialized in a workspace. This means + it has the `:current-page-id` and `:current-file-id` properly set." + [state] + (and (uuid? (:current-file-id state)) + (uuid? (:current-page-id state)))) ;; --- Helpers diff --git a/frontend/src/app/main/data/workspace/groups.cljs b/frontend/src/app/main/data/workspace/groups.cljs index 9de51f22a..c5f527359 100644 --- a/frontend/src/app/main/data/workspace/groups.cljs +++ b/frontend/src/app/main/data/workspace/groups.cljs @@ -137,7 +137,7 @@ (defn prepare-remove-group [page-id group objects] - (let [shapes (:shapes group) + (let [shapes (into [] (:shapes group)) ; ensure we always have vector parent-id (cp/get-parent (:id group) objects) parent (get objects parent-id) diff --git a/frontend/src/app/main/data/workspace/persistence.cljs b/frontend/src/app/main/data/workspace/persistence.cljs index 402c227fc..cff4864e7 100644 --- a/frontend/src/app/main/data/workspace/persistence.cljs +++ b/frontend/src/app/main/data/workspace/persistence.cljs @@ -56,13 +56,14 @@ (rx/filter dch/commit-changes?) (rx/debounce 2000) (rx/merge stoper forcer)) - - local-file? #(as-> (:file-id %) event-file-id - (or (nil? event-file-id) - (= event-file-id file-id))) - library-file? #(as-> (:file-id %) event-file-id - (and (some? event-file-id) - (not= 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? + #(as-> (:file-id %) event-file-id + (and (some? event-file-id) + (not= event-file-id file-id))) on-dirty (fn [] @@ -565,6 +566,20 @@ [frame-id] (ptk/event ::update-frame-thumbnail {:frame-id frame-id})) +(defn update-shape-thumbnail + "An event that is succeptible to be executed out of the main flow, so + it need to correctly handle the situation that there are no page-id + or file-is loaded." + [shape-id thumbnail-data] + (ptk/reify ::update-shape-thumbnail + ptk/WatchEvent + (watch [_ state _] + (when (and (dwc/initialized? state) + (uuid? shape-id)) + (rx/of (dch/update-shapes [shape-id] + #(assoc % :thumbnail thumbnail-data) + {:save-undo? false})))))) + (defn- extract-frame-changes "Process a changes set in a commit to extract the frames that are changing" [[event [old-objects new-objects]]] diff --git a/frontend/src/app/main/data/workspace/texts.cljs b/frontend/src/app/main/data/workspace/texts.cljs index 8bc844b4a..04d6b0eb9 100644 --- a/frontend/src/app/main/data/workspace/texts.cljs +++ b/frontend/src/app/main/data/workspace/texts.cljs @@ -54,23 +54,23 @@ (ptk/reify ::finalize-editor-state ptk/WatchEvent (watch [_ state _] - (let [content (-> (get-in state [:workspace-editor-state id]) - (ted/get-editor-current-content))] + (when (dwc/initialized? state) + (let [content (-> (get-in state [:workspace-editor-state id]) + (ted/get-editor-current-content))] + (if (ted/content-has-text? content) + (let [content (d/merge (ted/export-content content) + (dissoc (:content shape) :children))] + (rx/merge + (rx/of (update-editor-state shape nil)) + (when (and (not= content (:content shape)) + (some? (:current-page-id state))) + (rx/of + (dch/update-shapes [id] #(assoc % :content content)) + (dwu/commit-undo-transaction))))) - (if (ted/content-has-text? content) - (let [content (d/merge (ted/export-content content) - (dissoc (:content shape) :children))] - (rx/merge - (rx/of (update-editor-state shape nil)) - (when (and (not= content (:content shape)) - (some? (:current-page-id state))) - (rx/of - (dch/update-shapes [id] #(assoc % :content content)) - (dwu/commit-undo-transaction))))) - - (when (some? id) - (rx/of (dws/deselect-shape id) - (dwc/delete-shapes #{id})))))))) + (when (some? id) + (rx/of (dws/deselect-shape id) + (dwc/delete-shapes #{id}))))))))) (defn initialize-editor-state [{:keys [id content] :as shape} decorator] diff --git a/frontend/src/app/main/ui/workspace/viewport/thumbnail_renderer.cljs b/frontend/src/app/main/ui/workspace/viewport/thumbnail_renderer.cljs index 95f27e5a9..85a9ab316 100644 --- a/frontend/src/app/main/ui/workspace/viewport/thumbnail_renderer.cljs +++ b/frontend/src/app/main/ui/workspace/viewport/thumbnail_renderer.cljs @@ -6,7 +6,6 @@ (ns app.main.ui.workspace.viewport.thumbnail-renderer (:require - [app.main.data.workspace.changes :as dwc] [app.main.data.workspace.persistence :as dwp] [app.main.store :as st] [app.util.dom :as dom] @@ -80,7 +79,7 @@ "Component in charge of creating thumbnails and storing them" {::mf/wrap-props false} [props] - (let [objects (obj/get props "objects") + (let [objects (obj/get props "objects") background (obj/get props "background") ;; Id of the current frame being rendered @@ -97,12 +96,9 @@ updates-stream (mf/use-memo - (fn [] - (let [update-events - (->> st/stream - (rx/filter dwp/update-frame-thumbnail?))] - (->> (rx/zip update-events next) - (rx/map first))))) + #(let [update-events (rx/filter dwp/update-frame-thumbnail? st/stream)] + (->> (rx/zip update-events next) + (rx/map first)))) on-thumbnail-data (mf/use-callback @@ -111,9 +107,7 @@ (reset! shape-id nil) (timers/schedule (fn [] - (st/emit! (dwc/update-shapes [@shape-id] - #(assoc % :thumbnail data) - {:save-undo? false})) + (st/emit! (dwp/update-shape-thumbnail @shape-id data)) (rx/push! next :next))))) on-frame-not-found