diff --git a/frontend/src/app/main/data/workspace.cljs b/frontend/src/app/main/data/workspace.cljs index 9fb3873862..3465990351 100644 --- a/frontend/src/app/main/data/workspace.cljs +++ b/frontend/src/app/main/data/workspace.cljs @@ -1529,26 +1529,6 @@ (let [orphans (set (into [] (keys (find-orphan-shapes state))))] (rx/of (dwsh/relocate-shapes orphans uuid/zero 0 true)))))) -;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; -;; Sitemap -;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; - -(defn start-rename-page-item - [id] - (ptk/reify ::start-rename-page-item - ptk/UpdateEvent - (update [_ state] - (assoc-in state [:workspace-local :page-item] id)))) - -(defn stop-rename-page-item - [] - (ptk/reify ::stop-rename-page-item - ptk/UpdateEvent - (update [_ state] - (let [local (-> (:workspace-local state) - (dissoc :page-item))] - (assoc state :workspace-local local))))) - ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; Components annotations ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; @@ -1825,6 +1805,9 @@ (dm/export dwpg/initialize-page) (dm/export dwpg/finalize-page) (dm/export dwpg/create-page) +(dm/export dwpg/create-page-and-focus) +(dm/export dwpg/start-rename-page-item) +(dm/export dwpg/stop-rename-page-item) (dm/export dwpg/duplicate-page) (dm/export dwpg/rename-page) (dm/export dwpg/delete-page) diff --git a/frontend/src/app/main/data/workspace/pages.cljs b/frontend/src/app/main/data/workspace/pages.cljs index 04e9205a7f..53588d253a 100644 --- a/frontend/src/app/main/data/workspace/pages.cljs +++ b/frontend/src/app/main/data/workspace/pages.cljs @@ -190,6 +190,44 @@ (rx/of (update-page-root file-id id)) (rx/empty)))))))) +(defn start-rename-page-item + [id] + (ptk/reify ::start-rename-page-item + ptk/UpdateEvent + (update [_ state] + (assoc-in state [:workspace-local :page-item] id)))) + +(defn stop-rename-page-item + [] + (ptk/reify ::stop-rename-page-item + ptk/UpdateEvent + (update [_ state] + (let [local (-> (:workspace-local state) + (dissoc :page-item))] + (assoc state :workspace-local local))))) + +(defn create-page-and-focus + "Create a page from the sitemap and move the focus to it: open the new + page and start renaming it. Only the user who adds the page gets the + focus; a page added by someone else arrives as a change and leaves + their sitemap alone." + [{:keys [file-id]}] + (let [id (uuid/next)] + (ptk/reify ::create-page-and-focus + ptk/WatchEvent + (watch [_ _ stream] + ;; The rename waits for the new page to initialize, because + ;; initializing a page replaces `:workspace-local`. + (rx/merge + (->> stream + (rx/filter (ptk/type? ::initialized)) + (rx/filter #(= id (deref %))) + (rx/take 1) + (rx/map #(start-rename-page-item id))) + (rx/of (create-page {:page-id id :file-id file-id}) + :interrupt + (dcm/go-to-workspace :file-id file-id :page-id id))))))) + (defn duplicate-page [page-id] (ptk/reify ::duplicate-page @@ -336,31 +374,35 @@ (ptk/reify ::rename-page ptk/WatchEvent (watch [it state _] - (let [page (dsh/lookup-page state id) - objects (:objects page) - empty-page? (and (= 1 (count objects)) - (= uuid/zero (first (keys objects)))) - changes (-> (pcb/empty-changes it) - (pcb/with-page page) - (pcb/mod-page page {:name name})) - pages (-> (dsh/lookup-file-data state) :pages) - index (d/index-of pages id) - prev-id (when (and (some? index) (pos? index)) - (nth pages (dec index) nil)) - next-id (when (some? index) - (nth pages (inc index) nil)) - fallback-page-id (or prev-id next-id) - separator? (= "---" (str/trim name))] - (rx/concat - (rx/of (dch/commit-changes changes)) - ;; Go to other page only if page is empty (only has the root shape) - ;; and the separator page is being renamed, otherwise user can rename - ;; any page to separator and be forced to go to another page - (when (and separator? - empty-page? - (= id (:current-page-id state)) - (some? fallback-page-id)) - (rx/of (dcm/go-to-workspace :page-id fallback-page-id)))))))) + (let [page (dsh/lookup-page state id)] + ;; Keeping the same name commits nothing, so the next undo still + ;; reverts the last visible change (for example, the page creation). + (if (= name (:name page)) + (rx/empty) + (let [objects (:objects page) + empty-page? (and (= 1 (count objects)) + (= uuid/zero (first (keys objects)))) + changes (-> (pcb/empty-changes it) + (pcb/with-page page) + (pcb/mod-page page {:name name})) + pages (-> (dsh/lookup-file-data state) :pages) + index (d/index-of pages id) + prev-id (when (and (some? index) (pos? index)) + (nth pages (dec index) nil)) + next-id (when (some? index) + (nth pages (inc index) nil)) + fallback-page-id (or prev-id next-id) + separator? (= "---" (str/trim name))] + (rx/concat + (rx/of (dch/commit-changes changes)) + ;; Go to other page only if page is empty (only has the root shape) + ;; and the separator page is being renamed, otherwise user can rename + ;; any page to separator and be forced to go to another page + (when (and separator? + empty-page? + (= id (:current-page-id state)) + (some? fallback-page-id)) + (rx/of (dcm/go-to-workspace :page-id fallback-page-id)))))))))) (defn- delete-page-components [changes page] diff --git a/frontend/src/app/main/ui/workspace/sidebar/sitemap.cljs b/frontend/src/app/main/ui/workspace/sidebar/sitemap.cljs index 818d002dd8..56326d0bb6 100644 --- a/frontend/src/app/main/ui/workspace/sidebar/sitemap.cljs +++ b/frontend/src/app/main/ui/workspace/sidebar/sitemap.cljs @@ -295,7 +295,6 @@ [{:keys [height collapsed on-toggle-collapsed scroll-store]}] (let [file (mf/deref ref:file-with-pages) file-id (get file :id) - project-id (get file :project-id) content-ref (mf/use-ref nil) on-scroll (mf/use-fn @@ -304,10 +303,11 @@ (sc/save-scroll! scroll-store [:sitemap file-id] event))) on-create (mf/use-fn - (mf/deps file-id project-id) + (mf/deps file-id collapsed on-toggle-collapsed) (fn [event] - (st/emit! (dw/create-page {:file-id file-id - :project-id project-id})) + (when ^boolean collapsed + (on-toggle-collapsed)) + (st/emit! (dw/create-page-and-focus {:file-id file-id})) (-> event dom/get-current-target dom/blur!))) read-only? (mf/use-ctx ctx/workspace-read-only?) diff --git a/frontend/test/frontend_tests/data/workspace_pages_test.cljs b/frontend/test/frontend_tests/data/workspace_pages_test.cljs index 8ec11e41b4..96a8fcefd2 100644 --- a/frontend/test/frontend_tests/data/workspace_pages_test.cljs +++ b/frontend/test/frontend_tests/data/workspace_pages_test.cljs @@ -10,9 +10,15 @@ [app.common.files.changes-builder :as pcb] [app.common.test-helpers.files :as cthf] [app.common.test-helpers.ids-map :as cthi] + [app.common.uuid :as uuid] + [app.main.data.common :as dcm] + [app.main.data.event :as ev] [app.main.data.workspace.pages :as dwpg] + [beicon.v2.core :as rx] [cljs.test :as t :include-macros true] - [frontend-tests.helpers.state :as ths])) + [frontend-tests.helpers.async :as tha] + [frontend-tests.helpers.state :as ths] + [potok.v2.core :as ptk])) (t/use-fixtures :each {:before cthi/reset-idmap!}) @@ -127,3 +133,55 @@ "at least one page remains") (t/is (= page1-id (first pgs)) "the first page (in page order) is kept"))))))) + +;; --------------------------------------------------------------------------- +;; Adding a page from the sitemap opens it and starts renaming it, once +;; the new page has initialized. +;; --------------------------------------------------------------------------- + +(t/deftest ^:async add-page-opens-and-renames-the-new-page + (let [file-id (uuid/next) + stream (rx/subject) + event (dwpg/create-page-and-focus {:file-id file-id}) + events (atom []) + done (tha/observe (ptk/watch event {} stream) + :on-next #(swap! events conj %))] + + (await (tha/settle)) + (let [[create interrupt go-to] @events + page-id (:id (ev/-data create))] + (t/is (= ::dwpg/create-page (ptk/type create))) + (t/is (= :interrupt interrupt)) + (t/is (= ::dcm/go-to-workspace (ptk/type go-to))) + + ;; A page added by someone else initializes first: no rename. + (rx/push! stream (ptk/data-event ::dwpg/initialized (uuid/next))) + (rx/push! stream (ptk/data-event ::dwpg/initialized page-id)) + (await done) + + (let [rename (last @events) + state (ptk/update rename {:workspace-local {}})] + (t/is (= 4 (count @events))) + (t/is (= ::dwpg/start-rename-page-item (ptk/type rename))) + (t/is (= page-id (get-in state [:workspace-local :page-item]))))))) + +;; --------------------------------------------------------------------------- +;; Renaming a page to its current name commits nothing, so the next undo +;; reverts the last visible change. +;; --------------------------------------------------------------------------- + +(defn- ^:async rename-page-events + [store page-id name] + (let [events (atom [])] + (await (tha/observe (ptk/watch (dwpg/rename-page page-id name) @store (rx/subject)) + :on-next #(swap! events conj %))) + @events)) + +(t/deftest ^:async rename-page-to-the-same-name-commits-nothing + (let [file (cthf/sample-file :file1 :page-label :page1) + page-id (cthi/id :page1) + name (:name (cthf/get-page file :page1)) + store (ths/setup-store file)] + (t/is (= [] (await (rename-page-events store page-id name)))) + (t/is (= [:app.main.data.changes/commit-changes] + (map ptk/type (await (rename-page-events store page-id "Renamed")))))))