alonso.torres 237e15827c 🐛 Fix the component watcher and library updates
Fixes in when the workspace syncs copies and propagates tokens, and in what
a layout wait covers (Phase 3c).

- Sync the copies when a shape is dragged inside a main.

  The component watcher dropped every commit flagged :translation? (the WASM
  drag commits only moves) so dragging whole mains around the canvas stays
  fast. A drag of a shape inside a main is flagged the same, so its copies
  never followed until some other edit of that main made them jump (F70).

  The watcher now drops a translation commit only when it moves shapes
  outside mains or whole mains: every shape it moves inside a main moves
  with the main's root (decision (q)). Otherwise the commit goes through
  component sync like any edit.

  workspace-sync-barrier-test: a translation of a whole main is dropped; one
  of a main's child syncs the copy. fill-commit in the interpreter takes
  several labels.

- Propagate the file's tokens after any library update.

  Updating a library from the notification synced the file and then
  propagated its tokens, so the copies take the token values of the file's
  token status, not the library's. The update button of the libraries
  dialog's Updates tab only synced the file (F83).

  dwl/update-libraries syncs the file from the given libraries and then
  propagates its tokens; the notification and the Updates tab both use it.

  workspace-library-updates-test: updating a library syncs it and then
  propagates the tokens.

- Sync copies whether or not component thumbnails are on.

  The component watcher's whole pipeline, the sync of the copies included,
  ran only when the :component-thumbnails flag was on or the WASM renderer
  was active. The flag is on by default, but an install that turned it off
  never synced copies on the SVG renderer: editing a main left its copies as
  they were (F8). The flag now gates only the thumbnail streams.

  workspace-sync-barrier-test: with the flag off, a main edit reaches the
  copy.

- Hold a token's shapes pending until the token is applied.

  A plugin's waitForLayoutUpdate waits until no shape has layout work
  pending. Applying a token resolves the tokens first, asynchronously, and
  opened no pending work meanwhile, so the wait found nothing pending and
  returned before the token reached the shapes or their copies (F54). The
  shapes are now pending, as a :token task, from the start of the resolution
  until the update is emitted, and the layout, text and sync work it starts
  takes over from there.

  The composable interpreter no longer waits on the token itself: case AY
  applies a token and checks its copies after the same wait a plugin uses,
  and fails without this change.

AI-assisted-by: claude-opus-5-5
2026-10-15 15:11:31 +02:00

1307 lines
55 KiB
Clojure

;; 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 app.main.ui.workspace.libraries
(:require-macros [app.main.style :as stl])
(:require
[app.common.data :as d]
[app.common.data.macros :as dm]
[app.common.files.tokens :as cfo]
[app.common.files.variant :as cfv]
[app.common.types.components-list :as ctkl]
[app.common.types.file :as ctf]
[app.common.types.library :as ctl]
[app.common.types.tokens-lib :as ctob]
[app.common.types.typographies-list :as ctyl]
[app.common.uuid :as uuid]
[app.config :as cf]
[app.main.data.dashboard :as dd]
[app.main.data.modal :as modal]
[app.main.data.notifications :as ntf]
[app.main.data.team :as dtm]
[app.main.data.workspace.colors :as mdc]
[app.main.data.workspace.libraries :as dwl]
[app.main.data.workspace.tokens.library-edit :as dwtl]
[app.main.refs :as refs]
[app.main.render :refer [component-svg]]
[app.main.store :as st]
[app.main.ui.components.color-bullet :as cb]
[app.main.ui.components.link-button :as lb]
[app.main.ui.components.search-bar :refer [search-bar*]]
[app.main.ui.components.title-bar :refer [title-bar*]]
[app.main.ui.context :as ctx]
[app.main.ui.ds.buttons.button :refer [button*]]
[app.main.ui.ds.buttons.icon-button :refer [icon-button*]]
[app.main.ui.ds.foundations.assets.icon :as i :refer [icon*]]
[app.main.ui.ds.foundations.typography.text :refer [text*]]
[app.main.ui.ds.layout.tab-switcher :refer [tab-switcher*]]
[app.main.ui.ds.product.empty-state :refer [empty-state*]]
[app.main.ui.ds.tooltip.tooltip :refer [tooltip*]]
[app.main.ui.hooks :as h]
[app.main.ui.workspace.tokens.import-from-library]
[app.util.color :as uc]
[app.util.dom :as dom]
[app.util.i18n :refer [c tr]]
[app.util.strings :refer [matches-search]]
[beicon.v2.core :as rx]
[cuerdas.core :as str]
[rumext.v2 :as mf]))
(defn- get-library-summary
"Given a library data return a summary representation of this library"
[data]
(let [colors (count (:colors data))
graphics 0
typographies (count (:typographies data))
components (count (->> (ctkl/components-seq data)
(remove #(cfv/is-secondary-variant? % data))))
tokens-lib (cfo/get-tokens-lib data)
tokens (if (some? tokens-lib) (count (ctob/get-all-tokens tokens-lib)) 0)
token-sets (if (some? tokens-lib) (count (ctob/get-sets tokens-lib)) 0)
token-themes (if (some? tokens-lib) (count (ctob/get-themes-no-hidden tokens-lib)) 0)
empty? (and (zero? components)
(zero? graphics)
(zero? colors)
(zero? typographies)
(zero? tokens)
(zero? token-sets)
(zero? token-themes))]
{:is-empty empty?
:colors colors
:graphics graphics
:typographies typographies
:components components
:tokens tokens
:token-sets token-sets
:token-themes token-themes}))
(defn- adapt-backend-summary
[summary]
(let [components (or (-> summary :components :count) 0)
graphics (or (-> summary :media :count) 0)
typographies (or (-> summary :typographies :count) 0)
colors (or (-> summary :colors :count) 0)
tokens (or (-> summary :tokens-count) 0)
token-sets (or (-> summary :token-sets-count) 0)
token-themes (or (-> summary :token-themes-count) 0)
empty? (and (zero? components)
(zero? graphics)
(zero? colors)
(zero? typographies)
(zero? tokens)
(zero? token-sets)
(zero? token-themes))]
{:is-empty empty?
:components components
:graphics graphics
:typographies typographies
:colors colors
:tokens tokens
:token-sets token-sets
:token-themes token-themes}))
(defn sort-linked-libraries
"Given the linked libraries of a file, each with a `:connected-to`
collection of the ids of the files that use it, return the libraries
ordered as a tree: each library is followed by the libraries nested
under it, marked with `:nested?` and `:parent-name`.
Direct libraries (`:is-indirect` false) and libraries not used by
any other linked library are roots. Every library is returned
exactly once, at any depth and even when the dependencies form a
cycle."
[libraries]
(let [by-id (d/index-by :id libraries)
sort-by-name (partial sort-by (comp str/lower :name))
parents
(d/update-vals by-id
(fn [{:keys [id connected-to]}]
(into #{}
(filter #(and (not= % id)
(contains? by-id %)))
connected-to)))
root?
(fn [{:keys [id is-indirect]}]
(or (false? is-indirect)
(empty? (get parents id))))
children
(fn [parent-id]
(->> libraries
(remove root?)
(filter #(contains? (get parents (:id %)) parent-id))
(sort-by-name)))
visit
(fn visit [[result visited :as acc] library parent]
(if (contains? visited (:id library))
acc
(reduce #(visit %1 %2 library)
[(conj result (assoc library
:nested? (some? parent)
:parent-name (:name parent)))
(conj visited (:id library))]
(children (:id library)))))]
;; The second pass picks up libraries only reachable through a
;; cycle, which have no root to hang from.
(as-> [[] #{}] $
(reduce #(visit %1 %2 nil) $ (sort-by-name (filter root? libraries)))
(reduce #(visit %1 %2 nil) $ (sort-by-name libraries))
(first $))))
(defn- linked-users
[{:keys [connected-to]} libraries-by-id]
(into [] (keep libraries-by-id) connected-to))
(defn- removable-users
"The linked libraries to disconnect to release `library`: those that use
it, directly or through other libraries, and that either are linked to
the file directly or are used by no linked library. A directly linked
library stays until it is disconnected, even when another one uses it.
Empty when the libraries only use each other in a cycle."
[library libraries-by-id]
(loop [pending (linked-users library libraries-by-id)
seen #{(:id library)}
removable []]
(if-let [user (first pending)]
(if (contains? seen (:id user))
(recur (rest pending) seen removable)
(let [users (linked-users user libraries-by-id)]
(recur (into (rest pending) users)
(conj seen (:id user))
(cond-> removable
(or (empty? users) (false? (:is-indirect user)))
(conj user)))))
removable)))
(defn library-disconnect-hint
"Why a linked library cannot be disconnected from this file, or nil when
it can. A library that another linked library uses stays connected as
long as that one does. Returns `{:reason :inherited}` for a nested
library, with the name of its parent, and `{:reason :used-by}` for a
direct one, with the name of the first library by name that uses it.
Both carry `:removable`, the names of the libraries to disconnect to
release this one, sorted by name. The parent can be nested itself, a
library can have several parents, and a parent can be linked directly
as well. In a cycle nothing is removable, and `:removable` holds the
name given in `:library-name`. `libraries-by-id` indexes the linked
libraries by id."
[{:keys [nested? parent-name] :as library} libraries-by-id]
(let [users (linked-users library libraries-by-id)]
(when (seq users)
(let [library-name (if nested?
parent-name
(->> users (sort-by (comp str/lower :name)) first :name))
removable (->> (removable-users library libraries-by-id)
(map :name)
(sort-by str/lower)
(vec))]
{:reason (if nested? :inherited :used-by)
:library-name library-name
:removable (if (seq removable) removable [library-name])}))))
(defn disconnect-hint-segments
"Splits a translated disconnect hint template, which holds two `%s`
(the library that keeps this one, then the libraries to disconnect),
into `[:text s]` and `[:name s]` segments in reading order, so the UI
can highlight the library names. Each of `removable` is its own name
segment, separated by `[:text \", \"]`."
[template library-name removable]
(let [[before between after] (vec (.split ^string template "%s"))
names (into []
(comp (map (fn [n] [:name n]))
(interpose [:text ", "]))
removable)]
(-> [[:text before] [:name library-name] [:text between]]
(into names)
(conj [:text after]))))
(mf/defc disconnect-hint-text*
{::mf/private true}
[{:keys [template library-name removable]}]
[:*
(for [[idx [kind text]] (map-indexed vector (disconnect-hint-segments template library-name removable))]
(if (= kind :name)
[:span {:key idx :class (stl/css :library-name-highlight)} text]
[:span {:key idx} text]))])
(defn- describe-library
[components-count graphics-count colors-count typography-count]
(let [all-zero? (and (zero? components-count)
(zero? graphics-count)
(zero? colors-count)
(zero? typography-count))]
(str
(str/join " · "
(cond-> []
(or all-zero? (pos? components-count))
(conj (tr "workspace.libraries.components" (c components-count)))
(or all-zero? (pos? graphics-count))
(conj (tr "workspace.libraries.graphics" (c graphics-count)))
(or all-zero? (pos? colors-count))
(conj (tr "workspace.libraries.colors" (c colors-count)))
(or all-zero? (pos? typography-count))
(conj (tr "workspace.libraries.typography" (c typography-count)))))
"\u00A0")))
(mf/defc library-description*
{::mf/private true}
[{:keys [summary hint-name]}]
(let [components-count (get summary :components)
graphics-count (get summary :graphics)
typography-count (get summary :typographies)
colors-count (get summary :colors)
tokens-count (get summary :tokens)
token-sets-count (get summary :token-sets)
token-themes-count (get summary :token-themes)
token-lib-sync? (contains? cf/flags :token-lib-sync)
elements-line
(str/join " · "
(cond-> []
(pos? components-count)
(conj (tr "workspace.libraries.components" (c components-count)))
(pos? graphics-count)
(conj (tr "workspace.libraries.graphics" (c graphics-count)))
(pos? colors-count)
(conj (tr "workspace.libraries.colors" (c colors-count)))
(pos? typography-count)
(conj (tr "workspace.libraries.typography" (c typography-count)))))
tokens-line
(str/join " · "
(cond-> []
(pos? tokens-count)
(conj (tr "workspace.libraries.tokens" (c tokens-count)))
(pos? token-sets-count)
(conj (tr "workspace.libraries.token-sets" (c token-sets-count)))
(pos? token-themes-count)
(conj (tr "workspace.libraries.token-themes" (c token-themes-count)))))]
(if token-lib-sync?
;; New format: show elements and tokens in separate lines
[:*
(when-not (str/empty? elements-line)
[:li {:class (stl/css :element-count-line)} elements-line])
(when-not (str/empty? tokens-line)
[:li {:class (stl/css :element-count-line)} tokens-line])
(when hint-name
[:li {:class (stl/css :hint-line)}
"(" (tr "workspace.libraries.connected-through") " "
[:span {:class (stl/css :hint-library-name)} hint-name]
")"])]
;; Old format: show each item individually
[:*
(when (pos? components-count)
[:li {:class (stl/css :element-count-legacy)}
(tr "workspace.libraries.components" (c components-count))])
(when (pos? graphics-count)
[:li {:class (stl/css :element-count-legacy)}
(tr "workspace.libraries.graphics" (c graphics-count))])
(when (pos? colors-count)
[:li {:class (stl/css :element-count-legacy)}
(tr "workspace.libraries.colors" (c colors-count))])
(when (pos? typography-count)
[:li {:class (stl/css :element-count-legacy)}
(tr "workspace.libraries.typography" (c typography-count))])
(when (pos? tokens-count)
[:li {:class (stl/css :element-count-legacy)}
(tr "workspace.libraries.tokens" (c tokens-count))])
(when (pos? token-sets-count)
[:li {:class (stl/css :element-count-legacy)}
(tr "workspace.libraries.token-sets" (c token-sets-count))])
(when (pos? token-themes-count)
[:li {:class (stl/css :element-count-legacy)}
(tr "workspace.libraries.token-themes" (c token-themes-count))])])))
(mf/defc sample-library-entry*
{::mf/private true}
[{:keys [library importing]}]
(let [id (:id library)
importing? (deref importing)
team-id (mf/use-ctx ctx/current-team-id)
on-error
(mf/use-fn
(fn [_]
(reset! importing nil)
(rx/of (ntf/error (tr "dashboard.libraries-and-templates.import-error")))))
on-success
(mf/use-fn
(mf/deps team-id)
(fn [_]
(st/emit! (dtm/fetch-shared-files team-id))))
import-library
(mf/use-fn
(mf/deps on-success on-error)
(fn [_]
(reset! importing id)
(st/emit! (dd/clone-template
(with-meta {:template-id id}
{:on-success on-success
:on-error on-error})))))]
[:div {:class (stl/css :sample-library-item)
:key (dm/str id)}
[:div {:class (stl/css :sample-library-item-name)} (:name library)]
[:> button* {:variant "secondary"
:disabled (some? importing?)
:on-click import-library
:class (stl/css :sample-library-button)}
(if (= importing? id) (tr "labels.adding") (tr "labels.add"))]]))
(defn- empty-library?
"Check if currentt library summary has elements or not"
[summary]
(boolean (:is-empty summary)))
(defn- has-tokens?
"Check if library has tokens to be imported"
[{:keys [data]}]
(when-let [tokens-lib (get data :tokens-lib)]
(not (ctob/empty-lib? tokens-lib))))
(mf/defc libraries-tab-legacy*
{::mf/private true}
[{:keys [is-shared linked-libraries shared-libraries]}]
(let [file-id (mf/use-ctx ctx/current-file-id)
search-term* (mf/use-state "")
search-term (deref search-term*)
;; The summary of the current/local library
;; NOTE: we only need a snapshot of current library
local-library (deref refs/workspace-data)
summary (get-library-summary local-library)
empty-library? (empty-library? summary)
selected (h/use-shared-state mdc/colorpalette-selected-broadcast-key :recent)
dependencies (mf/with-memo [shared-libraries]
(into {} (map (juxt :id :library-file-ids) (vals shared-libraries))))
library-names (mf/with-memo [shared-libraries]
(into {} (map (fn [{:keys [id name]}]
[id name])
(vals shared-libraries))))
find-connected-to
(mf/use-fn
(mf/deps dependencies)
(fn [library-id]
(->> dependencies
(keep (fn [[k v]] (when (contains? v library-id) k))))))
shared-libraries
(mf/with-memo [shared-libraries linked-libraries file-id search-term]
(when shared-libraries
(->> (vals shared-libraries)
(remove #(= (:id %) file-id))
(remove #(contains? linked-libraries (:id %)))
(filter #(matches-search (:name %) search-term))
(map #(assoc % :connected-to (find-connected-to (:id %))))
(map #(assoc % :connected-to-names (->> (:connected-to %)
(keep library-names))))
(sort-by (comp str/lower :name)))))
linked-libraries
(mf/with-memo [linked-libraries find-connected-to library-names]
(->> (vals linked-libraries)
(map #(assoc % :connected-to (find-connected-to (:id %))))
(map #(assoc % :connected-to-names (->> (:connected-to %)
(keep library-names))))
(sort-by (comp str/lower :name))))
linked-libraries-ids
(mf/with-memo [linked-libraries]
(into #{} d/xf:map-id linked-libraries))
importing*
(mf/use-state nil)
sample-libraries
(mf/with-memo []
[{:id "penpot-design-system", :name "Design system example"}
{:id "wireframing-kit", :name "Wireframe library"}
{:id "whiteboarding-kit", :name "Whiteboarding Kit"}])
change-search-term
(mf/use-fn
(fn [event]
(reset! search-term* event)))
link-library
(mf/use-fn
(mf/deps file-id)
(fn [event]
(let [library-id (some-> (dom/get-current-target event)
(dom/get-data "library-id")
(uuid/parse))]
(reset! selected library-id)
(st/emit! (dwl/link-file-to-library file-id library-id)))))
unlink-library
(mf/use-fn
(mf/deps file-id local-library)
(fn [event]
(let [library-id (some-> (dom/get-current-target event)
(dom/get-data "library-id")
(uuid/parse))]
(when (= library-id @selected)
(reset! selected :file))
(st/emit! (dwl/unlink-file-from-library file-id library-id)
(dwl/sync-file file-id library-id))
;; When the unlinked library is the current tokens source,
;; we must reset it to the local library.
(when (cfo/effective-tokens-source? local-library library-id)
(st/emit! (dwtl/set-tokens-source (:id local-library)))))))
import-tokens
(mf/use-fn
(mf/deps file-id)
(fn [event]
(let [library-id (some-> (dom/get-current-target event)
(dom/get-data "library-id")
(uuid/parse))]
(st/emit! (modal/show
:tokens/import-from-library {:file-id file-id
:library-id library-id})))))
on-delete-accept
(mf/use-fn
(mf/deps file-id)
#(st/emit! (dwl/set-file-shared file-id false)
(modal/show :libraries-dialog {:file-id file-id})))
on-delete-cancel
(mf/use-fn
(mf/deps file-id)
#(st/emit! (modal/show :libraries-dialog {:file-id file-id})))
publish
(mf/use-fn
(mf/deps file-id)
(fn [event]
(let [input-node (dom/get-target event)
publish-library #(st/emit! (dwl/set-file-shared file-id true))
cancel-publish #(st/emit! (modal/show :libraries-dialog {:file-id file-id}))]
(if empty-library?
(st/emit! (modal/show
{:type :confirm
:title (tr "modals.publish-empty-library.title")
:message (tr "modals.publish-empty-library.message")
:accept-label (tr "modals.publish-empty-library.accept")
:on-accept publish-library
:on-cancel cancel-publish}))
(publish-library))
(dom/blur! input-node))))
unpublish
(mf/use-fn
(mf/deps file-id)
(fn [_]
(st/emit! (modal/show
{:type :delete-shared-libraries
:ids #{file-id}
:origin :unpublish
:on-accept on-delete-accept
:on-cancel on-delete-cancel
:count-libraries 1}))))]
[:div {:class (stl/css :libraries-content-legacy)}
[:div {:class (stl/css :lib-section)}
[:> title-bar* {:collapsable false
:title (tr "workspace.libraries.in-this-file")
:class (stl/css :title-spacing-lib)}]
[:div {:class (stl/css :section-list)}
[:div {:class (stl/css :section-list-publish)}
[:div {:class (stl/css :item-content)}
[:div {:class (stl/css :item-title)} (tr "workspace.libraries.file-library")]
[:ul {:class (stl/css :item-contents-legacy)}
[:> library-description* {:summary summary}]]]
(if ^boolean is-shared
[:> button* {:variant "secondary"
:type "button"
:on-click unpublish}
(tr "common.unpublish")]
[:> button* {:variant "primary"
:type "button"
:on-click publish}
(tr "common.publish")])]
(for [{:keys [id name data connected-to connected-to-names] :as library} linked-libraries]
(let [disabled? (some #(contains? linked-libraries-ids %) connected-to)
can-import-tokens? (and (has-tokens? library)
(cfo/editable-tokens? local-library)
(contains? cf/flags :token-import-from-library))]
[:div {:class (stl/css :section-list-item-legacy)
:key (dm/str id)
:data-testid "library-item"}
[:div {:class (stl/css :item-content)}
[:div {:class (stl/css :item-name)} name]
[:ul {:class (stl/css :item-contents-legacy)}
(let [summary (get-library-summary data)]
[:*
[:> library-description* {:summary summary}]
(when (seq connected-to)
[:div {:class (stl/css :connected-to-wrapper-legacy)}
[:span "(" (tr "workspace.libraries.connected-to") " "]
[:span {:class (stl/css :connected-to-values)} (str/join ", " connected-to-names)]
[:span ")"]])])]]
[:div {:class (stl/css :library-actions)}
(when ^boolean can-import-tokens?
[:> icon-button*
{:type "button"
:aria-label (tr "workspace.tokens.import-tokens")
:icon i/import-export
:data-library-id (dm/str id)
:variant "secondary"
:on-click import-tokens}])
[:> icon-button* {:type "button"
:aria-label (tr "workspace.libraries.unlink-library-btn")
:icon i/detach
:data-library-id (dm/str id)
:variant "secondary"
:disabled disabled?
:on-click unlink-library}]]]))]]
[:div {:class (stl/css :shared-section)}
[:> title-bar* {:collapsable false
:title (tr "workspace.libraries.shared-libraries")
:class (stl/css :title-spacing-lib)}]
[:> search-bar* {:on-change change-search-term
:value search-term
:placeholder (tr "workspace.libraries.search-shared-libraries")
:icon-id i/search}]
(if (seq shared-libraries)
[:div {:class (stl/css :section-list-shared)}
(for [{:keys [id name] :as library} shared-libraries]
[:div {:class (stl/css :section-list-item-legacy)
:key (dm/str id)
:data-testid "library-item"}
[:div {:class (stl/css :item-content)}
[:div {:class (stl/css :item-name)} name]
[:ul {:class (stl/css :item-contents-legacy)}
(let [summary (-> (:library-summary library)
(adapt-backend-summary))]
[:> library-description* {:summary summary}])]]
[:> icon-button* {:class (stl/css :item-button-shared)
:variant "secondary"
:data-library-id (dm/str id)
:icon "add"
:aria-label (tr "workspace.libraries.shared-library-btn")
:on-click link-library}]])]
(when (empty? shared-libraries)
[:div {:class (stl/css :section-list-empty)}
(cond
(nil? shared-libraries)
(tr "workspace.libraries.loading")
(str/empty? search-term)
[:*
[:div {:class (stl/css :sample-libraries-info)}
(tr "workspace.libraries.empty.no-libraries")
[:a {:target "_blank"
:class (stl/css :sample-libraries-link)
:href "https://penpot.app/libraries-templates"}
(tr "workspace.libraries.empty.some-templates")]]
[:div {:class (stl/css :sample-libraries-container)}
(tr "workspace.libraries.empty.add-some")
(for [library sample-libraries]
[:> sample-library-entry*
{:library library
:key (dm/str (:id library))
:importing importing*}])]]
:else
(tr "workspace.libraries.no-matches-for" search-term))]))]]))
(mf/defc libraries-tab*
{::mf/private true}
[{:keys [linked-libraries shared-libraries]}]
(let [file-id (mf/use-ctx ctx/current-file-id)
search-term* (mf/use-state "")
search-term (deref search-term*)
selected (h/use-shared-state mdc/colorpalette-selected-broadcast-key :recent)
dependencies (mf/with-memo [shared-libraries]
(into {} (map (juxt :id :library-file-ids) (vals shared-libraries))))
library-names (mf/with-memo [shared-libraries]
(into {} (map (fn [{:keys [id name]}]
[id name])
(vals shared-libraries))))
find-connected-to
(mf/use-fn
(mf/deps dependencies)
(fn [library-id]
(->> dependencies
(keep (fn [[k v]] (when (contains? v library-id) k))))))
shared-libraries
(mf/with-memo [shared-libraries linked-libraries file-id search-term]
(when shared-libraries
(->> (vals shared-libraries)
(remove #(= (:id %) file-id))
(filter #(matches-search (:name %) search-term))
(map #(assoc % :connected-to (find-connected-to (:id %))))
(map #(assoc % :connected-to-names (->> (:connected-to %)
(keep library-names))))
(map #(assoc % :linked? (contains? linked-libraries (:id %))))
(sort-by (comp str/lower :name)))))
importing*
(mf/use-state nil)
sample-libraries
(mf/with-memo []
[{:id "penpot-design-system", :name "Design system example"}
{:id "wireframing-kit", :name "Wireframe library"}
{:id "whiteboarding-kit", :name "Whiteboarding Kit"}])
change-search-term
(mf/use-fn
(fn [event]
(reset! search-term* event)))
link-library
(mf/use-fn
(mf/deps file-id)
(fn [event]
(let [library-id (some-> (dom/get-current-target event)
(dom/get-data "library-id")
(uuid/parse))]
(reset! selected library-id)
(st/emit! (dwl/link-file-to-library file-id library-id)))))]
[:div {:class (stl/css :libraries-content)}
[:div {:class (stl/css :shared-section)}
[:> title-bar* {:collapsable false
:title (tr "workspace.libraries.shared-libraries")
:class (stl/css :title-spacing-lib)}]
[:> search-bar* {:on-change change-search-term
:value search-term
:placeholder (tr "workspace.libraries.search-shared-libraries")
:icon-id i/search}]
(if (seq shared-libraries)
[:div {:class (stl/css :section-list-shared)}
(for [{:keys [id name linked?] :as library} shared-libraries]
[:div {:class (stl/css :section-list-item)
:key (dm/str id)
:data-testid "library-item"}
[:div {:class (stl/css :item-content)}
[:div {:class (stl/css :item-name)} name]
[:ul {:class (stl/css :item-contents)}
(let [summary (-> (:library-summary library)
(adapt-backend-summary))]
[:> library-description* {:summary summary}])]]
(if ^boolean linked?
[:> text* {:class (stl/css :shared-library-added)
:as "span"
:typography "body-medium"}
[:> icon* {:icon-id i/tick
:class (stl/css :shared-library-added-icon)}]
(tr "workspace.libraries.shared-library-added")]
[:> icon-button* {:class (stl/css :item-button-shared)
:variant "secondary"
:data-library-id (dm/str id)
:icon "add"
:aria-label (tr "workspace.libraries.shared-library-btn")
:on-click link-library}])])]
(when (empty? shared-libraries)
[:div {:class (stl/css :section-list-empty)}
(cond
(nil? shared-libraries)
(tr "workspace.libraries.loading")
(str/empty? search-term)
[:*
[:div {:class (stl/css :sample-libraries-info)}
(tr "workspace.libraries.empty.no-libraries")
[:a {:target "_blank"
:class (stl/css :sample-libraries-link)
:href "https://penpot.app/libraries-templates"}
(tr "workspace.libraries.empty.some-templates")]]
[:div {:class (stl/css :sample-libraries-container)}
(tr "workspace.libraries.empty.add-some")
(for [library sample-libraries]
[:> sample-library-entry*
{:library library
:key (dm/str (:id library))
:importing importing*}])]]
:else
(tr "workspace.libraries.no-matches-for" search-term))]))]]))
(mf/defc file-tab*
{::mf/private true}
[{:keys [is-shared linked-libraries shared-libraries on-change-tab]}]
(let [file-id (mf/use-ctx ctx/current-file-id)
;; The summary of the current/local library
;; NOTE: we only need a snapshot of current library
local-library (deref refs/workspace-data)
summary (get-library-summary local-library)
empty-library? (empty-library? summary)
selected (h/use-shared-state mdc/colorpalette-selected-broadcast-key :recent)
dependencies (mf/with-memo [shared-libraries]
(into {} (map (juxt :id :library-file-ids) (vals shared-libraries))))
find-connected-to
(mf/use-fn
(mf/deps dependencies)
(fn [library-id]
(->> dependencies
(keep (fn [[k v]] (when (contains? v library-id) k))))))
linked-libraries
(mf/with-memo [linked-libraries find-connected-to]
(->> (vals linked-libraries)
(map #(assoc % :connected-to (find-connected-to (:id %))))
(sort-linked-libraries)))
libraries-by-id
(mf/with-memo [linked-libraries]
(d/index-by :id linked-libraries))
unlink-library
(mf/use-fn
(mf/deps file-id local-library)
(fn [event]
(let [library-id (some-> (dom/get-current-target event)
(dom/get-data "library-id")
(uuid/parse))]
(when (= library-id @selected)
(reset! selected :file))
(st/emit! (dwl/unlink-file-from-library file-id library-id)
(dwl/sync-file file-id library-id))
;; When the unlinked library is the current tokens source,
;; we must reset it to the local library.
(when (cfo/effective-tokens-source? local-library library-id)
(st/emit! (dwtl/set-tokens-source (:id local-library)))))))
import-tokens
(mf/use-fn
(mf/deps file-id)
(fn [event]
(let [library-id (some-> (dom/get-current-target event)
(dom/get-data "library-id")
(uuid/parse))]
(st/emit! (modal/show
:tokens/import-from-library {:file-id file-id
:library-id library-id})))))
set-as-tokens-source
(mf/use-fn
(fn [event]
(let [library-id (some-> (dom/get-current-target event)
(dom/get-data "library-id")
(uuid/parse))]
(st/emit! (dwtl/set-tokens-source library-id)))))
on-delete-accept
(mf/use-fn
(mf/deps file-id)
#(st/emit! (dwl/set-file-shared file-id false)
(modal/show :libraries-dialog {:file-id file-id})))
on-delete-cancel
(mf/use-fn
(mf/deps file-id)
#(st/emit! (modal/show :libraries-dialog {:file-id file-id})))
publish
(mf/use-fn
(mf/deps file-id)
(fn [event]
(let [input-node (dom/get-target event)
publish-library #(st/emit! (dwl/set-file-shared file-id true))
cancel-publish #(st/emit! (modal/show :libraries-dialog {:file-id file-id}))]
(if empty-library?
(st/emit! (modal/show
{:type :confirm
:title (tr "modals.publish-empty-library.title")
:message (tr "modals.publish-empty-library.message")
:accept-label (tr "modals.publish-empty-library.accept")
:on-accept publish-library
:on-cancel cancel-publish}))
(publish-library))
(dom/blur! input-node))))
unpublish
(mf/use-fn
(mf/deps file-id)
(fn [_]
(st/emit! (modal/show
{:type :delete-shared-libraries
:ids #{file-id}
:origin :unpublish
:on-accept on-delete-accept
:on-cancel on-delete-cancel
:count-libraries 1}))))
go-to-shared
(mf/use-fn
(mf/deps on-change-tab)
(fn [_]
(on-change-tab "libraries")))]
[:div {:class (stl/css :lib-section)}
[:> title-bar* {:collapsable false
:title (tr "workspace.libraries.assets-in-this-file")
:class (stl/css :title-spacing-lib)}]
[:div {:class (stl/css :section-list)}
[:div {:class (stl/css :section-list-publish)}
[:div {:class (stl/css :item-content)}
[:div {:class (stl/css :item-title)} (tr "workspace.libraries.file-library")]
[:ul {:class (stl/css :item-contents)}
(if empty-library?
[:> text* {:class (stl/css :empty-library-text)
:as "span"
:typography "body-small"}
(tr "workspace.libraries.empty.no-assets")]
[:> library-description* {:summary summary}])]]
(when (and (contains? cf/flags :token-lib-sync)
(cfo/effective-tokens-source? local-library (:id local-library))
(cfo/has-own-tokens? local-library))
[:> text* {:class (stl/css :tokens-source-label)
:as "span"
:typography "body-medium"}
(tr "workspace.libraries.tokens-source")])
(when (and (contains? cf/flags :token-lib-sync)
(not (cfo/effective-tokens-source? local-library (:id local-library))))
[:> button* {:variant "secondary"
:type "button"
:data-library-id (dm/str (:id local-library))
:on-click set-as-tokens-source}
(tr "workspace.libraries.set-as-tokens-source")])
(if ^boolean is-shared
[:> button* {:variant "secondary"
:type "button"
:on-click unpublish}
(tr "common.unpublish")]
[:> button* {:variant "primary"
:type "button"
:on-click publish}
(tr "common.publish")])]
[:div {:class (stl/css :section-list-linked)}
[:> title-bar* {:collapsable false
:title (tr "workspace.libraries.connected-libraries")
:class (stl/css :title-spacing-lib)
:title-class (stl/css :connected-libraries-title)}]
(if (seq linked-libraries)
(for [{:keys [id name data nested? parent-name] :as library} linked-libraries]
(let [hint (library-disconnect-hint library libraries-by-id)
hint-library (:library-name hint)
hint-removable (str/join ", " (:removable hint))
can-import-tokens? (and (has-tokens? library)
(cfo/editable-tokens? local-library)
(contains? cf/flags :token-import-from-library))]
[:div {:class (stl/css-case :section-list-item true
:section-list-item-nested nested?)
:key (dm/str id)
:data-testid "library-item"}
[:div {:class (stl/css :item-content)}
[:div {:class (stl/css :item-name)} name]
[:ul {:class (stl/css :item-contents)}
(let [summary (get-library-summary data)]
[:> library-description* {:summary summary :hint-name parent-name}])]]
[:div {:class (stl/css :library-actions)}
(when (and (contains? cf/flags :token-lib-sync)
(cfo/effective-tokens-source? local-library id))
[:> text* {:class (stl/css :tokens-source-label)
:as "span"
:typography "body-medium"}
(tr "workspace.libraries.tokens-source")])
(if (contains? cf/flags :token-lib-sync)
(when (and (cfo/tokens-provider? (:data library))
(not (cfo/effective-tokens-source? local-library id)))
[:> button* {:variant "secondary"
:type "button"
:data-library-id (dm/str id)
:on-click set-as-tokens-source}
(tr "workspace.libraries.set-as-tokens-source")])
(when ^boolean can-import-tokens?
[:> icon-button*
{:type "button"
:aria-label (tr "workspace.tokens.import-tokens")
:icon i/import-export
:data-library-id (dm/str id)
:variant "secondary"
:on-click import-tokens}]))
(if (some? hint)
[:> tooltip* {:class (stl/css :library-locked)
:tab-index 0
:data-testid "library-locked"
:aria-label (case (:reason hint)
:inherited (tr "workspace.libraries.connected-through-hint"
hint-library hint-removable)
:used-by (tr "workspace.libraries.used-by"
hint-library hint-removable))
:content #(mf/html
[:> disconnect-hint-text*
{:template (case (:reason hint)
:inherited (tr "workspace.libraries.connected-through-hint")
:used-by (tr "workspace.libraries.used-by"))
:library-name hint-library
:removable (:removable hint)}])}
[:> icon* {:icon-id (case (:reason hint)
:inherited i/arrow-up
:used-by i/info)}]]
[:> icon-button* {:type "button"
:aria-label (tr "workspace.libraries.unlink-library-btn")
:icon i/detach
:data-library-id (dm/str id)
:variant "secondary"
:on-click unlink-library}])]]))
[:div {:class (stl/css :shared-libraries-empty)}
[:> text* {:class (stl/css :empty-libraries-text)
:as "span"
:typography "body-small"}
(tr "workspace.libraries.empty.no-connected-libraries")]
[:button {:class (stl/css :go-to-shared-button)
:on-click go-to-shared}
(tr "workspace.libraries.add-shared-library")]])]]]))
(defn- extract-assets
[file-data library summary?]
(let [exceeded (volatile! {:components false
:colors false
:typographies false})
truncate (fn [asset-type items]
(if (and summary? (> (count items) 5))
(do
(vswap! exceeded assoc asset-type true)
(take 5 items))
items))
assets (dwl/assets-need-sync library file-data)
component-ids (into #{} (->> assets
(filter #(= (:asset-type %) :component))
(map :asset-id)))
color-ids (into #{} (->> assets
(filter #(= (:asset-type %) :color))
(map :asset-id)))
typography-ids (into #{} (->> assets
(filter #(= (:asset-type %) :typography))
(map :asset-id)))
components (->> component-ids
(map #(ctkl/get-component (:data library) %))
(sort-by #(str/lower (:name %)))
(truncate :components))
colors (->> color-ids
(map #(ctl/get-color (:data library) %))
(sort-by #(str/lower (:name %)))
(truncate :colors))
typographies (->> typography-ids
(map #(ctyl/get-typography (:data library) %))
(sort-by #(str/lower (:name %)))
(truncate :typographies))]
[library @exceeded {:components components
:colors colors
:typographies typographies}]))
(mf/defc updates-tab*
{::mf/private true}
[{:keys [file-id libraries is-legacy]}]
;; FIXME: naming
(let [summary?* (mf/use-state true)
summary? (deref summary?*)
updating? (mf/deref refs/updating-library)
;; NOTE: we don't want to react on file changes, we just want
;; a snapshot of file on the momento of open the dialog
file-data (deref refs/workspace-data)
see-all-assets
(mf/use-fn
(fn []
(reset! summary?* false)))
libs-assets (mf/with-memo [file-data libraries summary?*]
(->> (vals libraries)
(map #(extract-assets file-data % summary?))
(filter (fn [[_ _ {:keys [components colors typographies]}]]
(or (seq components)
(seq colors)
(seq typographies))))))
update (mf/use-fn
(mf/deps file-id)
(fn [event]
(when-not updating?
(let [library-id (some-> (dom/get-current-target event)
(dom/get-data "library-id")
(uuid/parse))]
(when library-id
(st/emit!
(dwl/set-updating-library true)
(dwl/update-libraries file-id [library-id])))))))]
[:div {:class (stl/css-case :updates-content (not is-legacy)
:updates-content-legacy is-legacy)}
[:div {:class (stl/css :update-section)}
(if (empty? libs-assets)
[:div {:class (stl/css :section-list-empty)}
[:> empty-state* {:icon i/library
:text (tr "workspace.libraries.no-libraries-need-sync")}]]
[:*
[:> title-bar* {:collapsable false
:title (tr "workspace.libraries.library-updates")
:class (stl/css :title-spacing-lib)}]
[:div {:class (stl/css :section-list)}
(for [[{:keys [id name] :as library}
exceeded
{:keys [components colors typographies]}] libs-assets]
[:div {:class (stl/css :section-list-item)
:key (dm/str id)}
[:div {:class (stl/css :item-content)}
[:div {:class (stl/css :item-name)} name]
[:ul {:class (stl/css :item-contents)}
(describe-library
(count components)
0
(count colors)
(count typographies))]]
[:> button* {:class (stl/css :item-update)
:disabled updating?
:variant "primary"
:data-library-id (dm/str id)
:on-click update}
(tr "workspace.libraries.update")]
[:div {:class (stl/css :libraries-updates)}
(when-not (empty? components)
[:div {:class (stl/css :libraries-updates-column)}
(for [component components]
[:div {:class (stl/css :libraries-updates-item)
:key (dm/str (:id component))}
(let [component (ctf/load-component-objects (:data library) component)
root-shape (ctf/get-component-root (:data library) component)]
[:*
[:& component-svg {:root-shape root-shape
:class (stl/css :component-svg)
:objects (:objects component)}]
[:div {:class (stl/css :name-block)}
[:span {:class (stl/css :item-name)
:title (:name component)}
(:name component)]]])])
(when (:components exceeded)
[:div {:class (stl/css :libraries-updates-item)
:key (uuid/next)}
[:div {:class (stl/css :name-block :ellipsis)}
[:span {:class (stl/css :item-name)} "(...)"]]])])
(when-not (empty? colors)
[:div {:class (stl/css :libraries-updates-column)
:style #js {"--bullet-size" "24px"}}
(for [color colors]
(let [default-name (cond
(:gradient color) (uc/gradient-type->string (get-in color [:gradient :type]))
(:color color) (:color color)
:else (:value color))]
[:div {:class (stl/css :libraries-updates-item)
:key (dm/str (:id color))}
[:*
[:> cb/color-bullet* {:color {:color (:color color)
:id (:id color)
:opacity (:opacity color)}}]
[:div {:class (stl/css :name-block)}
[:span {:class (stl/css :item-name)
:title (:name color)}
(:name color)]
(when-not (= (:name color) default-name)
[:span.color-value (:color color)])]]]))
(when (:colors exceeded)
[:div {:class (stl/css :libraries-updates-item)
:key (uuid/next)}
[:div {:class (stl/css :name-block.ellipsis)}
[:span {:class (stl/css :item-name)} "(...)"]]])])
(when-not (empty? typographies)
[:div {:class (stl/css :libraries-updates-column)}
(for [typography typographies]
[:div {:class (stl/css :libraries-updates-item)
:key (dm/str (:id typography))}
[:*
[:div {:class (stl/css :typography-sample)
:style {:font-family (:font-family typography)
:font-weight (:font-weight typography)
:font-style (:font-style typography)}}
(tr "workspace.assets.typography.sample")]
[:div {:class (stl/css :name-block)}
[:span {:class (stl/css :item-name)
:title (:name typography)}
(:name typography)]]]])
(when (:typographies exceeded)
[:div {:class (stl/css :libraries-updates-item)
:key (uuid/next)}
[:div {:class (stl/css :name-block.ellipsis)}
[:span {:class (stl/css :item-name)} "(...)"]]])])]
(when (or (pos? (:components exceeded))
(pos? (:colors exceeded))
(pos? (:typographies exceeded)))
[:> lb/link-button*
{:on-click see-all-assets
:class (stl/css :libraries-updates-see-all)
:value (str "(" (tr "workspace.libraries.update.see-all-changes") ")")}])])]])]]))
(mf/defc libraries-dialog
{::mf/register modal/components
::mf/register-as :libraries-dialog}
[{:keys [starting-tab file-id]}]
(let [files (mf/deref refs/files)
file (get files file-id)
shared? (:is-shared file)
linked-libraries
(mf/with-memo [files file-id]
(refs/select-libraries files file-id))
linked-libraries
(mf/with-memo [linked-libraries file-id]
(d/removem (fn [[_ lib]]
(= (:id lib) file-id))
linked-libraries))
shared-libraries
(mf/deref refs/shared-files)
close-dialog-outside
(mf/use-fn
(fn [event]
(when (= (dom/get-target event) (dom/get-current-target event))
(modal/hide!))))
close-dialog
(mf/use-fn
(fn [_]
(modal/hide!)))
token-lib-sync? (contains? cf/flags :token-lib-sync)
selected-tab*
(mf/use-state #(d/nilv starting-tab (if token-lib-sync? "file" "libraries")))
selected-tab
(deref selected-tab*)
on-change-tab
(mf/use-fn #(reset! selected-tab* %))
tabs
(mf/with-memo [token-lib-sync?]
(if token-lib-sync?
[{:label (tr "workspace.libraries.this-file")
:id "file"}
{:label (tr "workspace.libraries.libraries")
:id "libraries"}
{:label (tr "workspace.libraries.updates")
:id "updates"}]
[{:label (tr "workspace.libraries.libraries")
:id "libraries"}
{:label (tr "workspace.libraries.updates")
:id "updates"}]))]
(mf/with-effect []
(st/emit! (dtm/fetch-shared-files)))
[:div {:class (stl/css :modal-overlay)
:on-click close-dialog-outside
:data-testid "libraries-modal"}
[:div {:class (stl/css :modal-dialog)}
[:> icon-button* {:class (stl/css :close-btn)
:on-click close-dialog
:aria-label (tr "labels.close")
:variant "ghost"
:icon i/close}]
[:div {:class (stl/css :modal-title)}
(tr "workspace.libraries.libraries")]
[:> tab-switcher* {:tabs tabs
:selected selected-tab
:on-change on-change-tab}
(case selected-tab
"file"
(when token-lib-sync?
[:> file-tab* {:is-shared shared?
:on-change-tab on-change-tab
:linked-libraries linked-libraries
:shared-libraries shared-libraries}])
"libraries"
(if token-lib-sync?
[:> libraries-tab*
{:linked-libraries linked-libraries
:shared-libraries shared-libraries}]
[:> libraries-tab-legacy*
{:is-shared shared?
:linked-libraries linked-libraries
:shared-libraries shared-libraries}])
"updates"
[:> updates-tab*
{:file-id file-id
:is-legacy (not token-lib-sync?)
:libraries linked-libraries}])]]]))