✨ Focus a new page when it is added from the Pages list (#13138)

* ✨ Focus a new page when it is added from the Pages list

The "+" button added the page at the end of the list, often out of view,
so nothing showed that it was created. It now opens the new page, scrolls
the list to it and starts renaming it with the default name selected. A
collapsed Pages section expands first.

The new add-page event waits for the page to initialize before it starts
the rename, because page initialization replaces :workspace-local. Only
the user who adds the page gets the focus; other users get the page as a
change. create-page stays as it was for the plugin API. The rename events
move to the pages namespace so add-page can use them.

Closes #12474

AI-assisted-by: claude-opus-5-5

* ✨ Skip the rename commit when a page keeps its name

From the review: a new page now starts in rename mode, so pressing Enter
without changes committed a rename to the same name, and the next undo
did nothing visible instead of removing the page. Renaming a page to its
current name commits nothing now.

Rename add-page to create-page-and-focus, which says what it does and
does not collide with pcb/add-page.

AI-assisted-by: claude-opus-5-5
This commit is contained in:
Eva Marco 2026-11-26 09:40:08 +01:00 committed by GitHub
parent 033a40ca1c
commit 284ffbc391
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 133 additions and 50 deletions

View File

@ -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)

View File

@ -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]

View File

@ -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?)

View File

@ -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")))))))