✨ Copy stable deep link to selected boards from context menu (#10170)

* ✨ Copy stable deep link to selected boards from context menu

The workspace "Copy link" action copied the raw current location, which
only carries file and page ids, so the link did not point at any specific
board. Build the link from the current route plus a `board-id` query param
derived from the selection: a selected board contributes its own id, and a
selected shape contributes the id of the board it belongs to.

`board-id` is an existing workspace route param (`zoom-to-frame` already
selects and zooms to it on load), and board ids are stable across renames
and reordering, so the link keeps resolving to the same board. When the
selection contains no board, it falls back to the previous behavior.

Relates to #4584

AI-assisted-by: claude-opus-5-5
Signed-off-by: Filip Sajdak <filip.sajdak@siili.com>

* 📎 Add tests for copying a stable board link

copy-link-to-clipboard now links to the boards of the selection through
their ids, and nothing covered it. The tests run the event on a state
with two boards, a rectangle in each and a rectangle on the root, with
the router and the clipboard replaced.

They check that a selected board, and a shape in a board, link to that
board; that shapes in two boards link to both and a board with its child
gives one id; that the route params are kept next to the board ids; and
that an empty selection, or a shape on the root, copies the current
location. The five board cases fail on the version of the function before
the change.

AI-assisted-by: claude-opus-5-5
AI-assisted-by: claude-sonnet-5-5
Signed-off-by: Filip Sajdak <filip.sajdak@siili.com>

* ♻️ Share how the selection maps to its boards

select-shape and copy-link-to-clipboard each worked out which boards
a selection belongs to, with slightly different rules for a shape on
the root. Both now call selected-board-ids.

A selection with a shape on the root has no board to point at, so the
URL and the copied link both leave out the board ids. Before, the
copied link kept the boards of the rest of the selection. The URL also
lists each board once now, and has no board-id for an empty selection.

AI-assisted-by: claude-opus-5-5
Signed-off-by: Filip Sajdak <filip.sajdak@siili.com>

---------

Signed-off-by: Filip Sajdak <filip.sajdak@siili.com>
Co-authored-by: Eva Marco <eva.marco@kaleidos.net>
This commit is contained in:
Filip Sajdak 2026-11-20 09:47:11 +01:00 committed by GitHub
parent 5c61273766
commit 454e461c1e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 174 additions and 13 deletions

View File

@ -1192,11 +1192,23 @@
(rx/of (dwm/upload-media-workspace params))))))
(defn copy-link-to-clipboard
"Copy a link to the clipboard. When one or more boards are part of the
current selection, the link deep-links to those boards through their
ids (`board-id` route param), which are stable across renames and
reordering. Otherwise it falls back to the current location."
[]
(ptk/reify ::copy-link-to-clipboard
ptk/WatchEvent
(watch [_ _ _]
(clipboard/to-clipboard (rt/get-current-href)))))
(watch [_ state _]
(let [board-ids (dws/selected-board-ids (dsh/lookup-page-objects state)
(dsh/lookup-selected state))
link (or (when (some? board-ids)
(rt/resolve-uri (:router state)
(rt/lookup-name state)
(-> (rt/get-params state)
(assoc :board-id (mapv str board-ids)))))
(rt/get-current-href))]
(clipboard/to-clipboard link)))))
(defn copy-id-to-clipboard
[id]

View File

@ -127,6 +127,19 @@
;; --- Toggle shape's selection status (selected or deselected)
(defn selected-board-ids
"The ids of the boards `ids` belong to: the id itself for a board, the
frame of any other shape. Returns them without repeats, in selection
order, or nil when `ids` is empty or has a shape on the root, which
is not a board to point at."
[objects ids]
(let [board-ids (into (d/ordered-set)
(keep #(:id (cfh/get-frame objects %)))
ids)]
(when (and (seq board-ids)
(not (contains? board-ids uuid/zero)))
(vec board-ids))))
(defn select-shape
([id]
(select-shape id false))
@ -146,21 +159,15 @@
(watch [_ state _]
(let [page-id (:current-page-id state)
objects (dsh/lookup-page-objects state page-id)
selected-id (dsh/lookup-selected state)
selected (dsh/lookup-shapes state selected-id)
frame-ids (map (fn [item] (let [parent (cfh/get-frame objects (:id item))]
(:id parent))) selected)
params-without-board (-> (rt/get-params state)
(dissoc :board-id))
params-board (-> (rt/get-params state)
(assoc :board-id frame-ids))]
board-ids (selected-board-ids objects (dsh/lookup-selected state))
params (rt/get-params state)]
(rx/of (dwc/expand-all-parents [id] objects)
:interrupt
::dwsp/interrupt
(if (some #(= % uuid/zero) frame-ids)
(rt/nav :workspace params-without-board {::rt/replace true})
(rt/nav :workspace params-board {::rt/replace true}))))))))
(if (some? board-ids)
(rt/nav :workspace (assoc params :board-id board-ids) {::rt/replace true})
(rt/nav :workspace (dissoc params :board-id) {::rt/replace true}))))))))
(defn select-prev-shape
([]

View File

@ -0,0 +1,140 @@
;; This Source Code Form is subject to the terms of the Mozilla Public
;; License, v. 2.0. If a copy of the MPL was not distributed with this
;; file, You can obtain one at http://mozilla.org/MPL/2.0/.
;;
;; Copyright (c) KALEIDOS SUBSIDIARY SL
(ns frontend-tests.data.workspace-board-link-test
"The link `copy-link-to-clipboard` puts on the clipboard: one that
deep-links to the boards of the selection, or the current location
when the selection has none."
(:require
[app.common.data :as d]
[app.common.test-helpers.compositions :as ctho]
[app.common.test-helpers.files :as cthf]
[app.common.test-helpers.ids-map :as cthi]
[app.common.test-helpers.shapes :as cths]
[app.main.data.helpers :as dsh]
[app.main.data.workspace.clipboard :as dwcl]
[app.main.data.workspace.selection :as dws]
[app.main.router :as rt]
[app.util.clipboard :as clipboard]
[cljs.test :as t :include-macros true]
[frontend-tests.helpers.pages :as thp]
[frontend-tests.helpers.state :as ths]
[potok.v2.core :as ptk]))
(t/use-fixtures :each
{:before thp/reset-idmap!})
(def ^:private current-href "https://penpot.test/#/workspace")
(def ^:private board-link "https://penpot.test/#/workspace?board-id")
(defn- setup-file
"Two boards with a rectangle in each, and a rectangle on the root."
[]
(-> (cthf/sample-file :file1)
(ctho/add-frame :board-1)
(ctho/add-frame :board-2)
(cths/add-sample-shape :rect-1 :parent-label :board-1)
(cths/add-sample-shape :rect-2 :parent-label :board-2)
(cths/add-sample-shape :rect-root)))
(defn- copy-link
"Runs `copy-link-to-clipboard` with the shapes labelled `selected`
selected. Returns the link that reached the clipboard and the route
params `resolve-uri` was asked for, which is nil when it was not."
[selected]
(let [store (ths/setup-store (setup-file))
state (assoc-in @store [:workspace-local :selected]
(into #{} (map cthi/id) selected))
resolved (volatile! nil)
copied (volatile! nil)]
(with-redefs [rt/lookup-name (constantly :workspace)
rt/get-params (constantly {:file-id "file"})
rt/get-current-href (constantly current-href)
rt/resolve-uri (fn [_router _name params]
(vreset! resolved params)
board-link)
clipboard/to-clipboard (fn [link] (vreset! copied link))]
(ptk/watch (dwcl/copy-link-to-clipboard) state nil))
{:link @copied
:params @resolved}))
(defn- linked-boards
[result]
(set (get-in result [:params :board-id])))
(t/deftest a-selected-board-links-to-that-board
(let [result (copy-link [:board-1])]
(t/is (= board-link (:link result)))
(t/is (= #{(str (cthi/id :board-1))} (linked-boards result)))))
(t/deftest a-shape-in-a-board-links-to-its-board
(let [result (copy-link [:rect-1])]
(t/is (= board-link (:link result)))
(t/is (= #{(str (cthi/id :board-1))} (linked-boards result)))))
(t/deftest shapes-in-two-boards-link-to-both-boards
(let [result (copy-link [:rect-1 :rect-2])]
(t/is (= #{(str (cthi/id :board-1)) (str (cthi/id :board-2))}
(linked-boards result)))))
(t/deftest a-board-and-its-child-give-one-board-id
(let [result (copy-link [:board-1 :rect-1])]
(t/is (= [(str (cthi/id :board-1))]
(get-in result [:params :board-id])))))
(t/deftest the-route-params-are-kept-next-to-the-board-ids
(t/is (= "file" (get-in (copy-link [:board-1]) [:params :file-id]))))
(t/deftest nothing-selected-copies-the-current-location
(let [result (copy-link [])]
(t/is (= current-href (:link result)))
(t/is (nil? (:params result)))))
(t/deftest a-shape-on-the-root-copies-the-current-location
;; Its frame is the root, which is not a board to link to
(let [result (copy-link [:rect-root])]
(t/is (= current-href (:link result)))
(t/is (nil? (:params result)))))
(t/deftest a-root-shape-beside-a-board-copies-the-current-location
;; The same rule as the URL of the selection: a shape on the root
;; means there is no board to point at, even with a board selected
(let [result (copy-link [:board-1 :rect-root])]
(t/is (= current-href (:link result)))
(t/is (nil? (:params result)))))
(defn- selected-board-ids
"`selected-board-ids` over the shapes labelled `selected`. Read the ids
of the labels after calling it, which is what creates them."
[selected]
(let [store (ths/setup-store (setup-file))
objects (dsh/lookup-page-objects @store)]
(dws/selected-board-ids objects (into (d/ordered-set) (map cthi/id) selected))))
(t/deftest selected-board-ids-of-a-board-is-the-board
(let [ids (selected-board-ids [:board-1])]
(t/is (= [(cthi/id :board-1)] ids))))
(t/deftest selected-board-ids-of-a-shape-is-its-board
(let [ids (selected-board-ids [:rect-2])]
(t/is (= [(cthi/id :board-2)] ids))))
(t/deftest selected-board-ids-lists-a-board-once
(let [ids (selected-board-ids [:rect-1 :board-1])]
(t/is (= [(cthi/id :board-1)] ids))))
(t/deftest selected-board-ids-keeps-the-selection-order
(let [ids (selected-board-ids [:rect-2 :rect-1])]
(t/is (= [(cthi/id :board-2) (cthi/id :board-1)] ids))))
(t/deftest selected-board-ids-is-nil-for-a-shape-on-the-root
(t/is (nil? (selected-board-ids [:rect-root]))))
(t/deftest selected-board-ids-is-nil-when-a-root-shape-is-among-boards
(t/is (nil? (selected-board-ids [:board-1 :rect-root]))))
(t/deftest selected-board-ids-is-nil-for-an-empty-selection
(t/is (nil? (selected-board-ids []))))

View File

@ -37,6 +37,7 @@
[frontend-tests.data.team-test]
[frontend-tests.data.uploads-test]
[frontend-tests.data.viewer-test]
[frontend-tests.data.workspace-board-link-test]
[frontend-tests.data.workspace-colors-test]
[frontend-tests.data.workspace-comments-test]
[frontend-tests.data.workspace-context-menu-test]
@ -268,6 +269,7 @@
'frontend-tests.data.team-test
'frontend-tests.data.uploads-test
'frontend-tests.data.viewer-test
'frontend-tests.data.workspace-board-link-test
'frontend-tests.data.workspace-colors-test
'frontend-tests.data.workspace-comments-test
'frontend-tests.data.workspace-context-menu-test