From 81c240847052b13ecbbe3913c058a4c000adc3e9 Mon Sep 17 00:00:00 2001 From: Juan de la Cruz Date: Tue, 17 Nov 2026 12:33:51 +0100 Subject: [PATCH] :sparkles: Add scale options panel to the right sidebar (#12453) * :sparkles: Extend combobox icons and input handling Allow a combobox to show a fallback icon when no option provides one. Clear delivered input values so unmounting does not emit them again. AI-assisted-by: gpt-6-luna * :sparkles: Add scale options panel to the right sidebar Add scale controls for selection dimensions, presets, and anchor points. Scale shape content around the selected anchor and preview canvas resizes. Reset scale state when selection changes, and exit scale mode with Escape. Cover scaling and resize previews with frontend tests. AI-assisted-by: gpt-6-luna --------- Co-authored-by: Luis de Dios --- frontend/resources/images/icons/scale.svg | 3 + frontend/src/app/main/data/workspace.cljs | 1 + .../app/main/data/workspace/shortcuts.cljs | 10 +- .../app/main/data/workspace/transforms.cljs | 62 ++++ .../src/app/main/ui/ds/controls/combobox.cljs | 18 +- .../src/app/main/ui/ds/controls/combobox.mdx | 12 + .../main/ui/ds/controls/combobox.stories.jsx | 6 + .../main/ui/ds/foundations/assets/icon.cljs | 1 + .../src/app/main/ui/workspace/sidebar.cljs | 6 + .../app/main/ui/workspace/sidebar/scale.cljs | 331 ++++++++++++++++++ .../app/main/ui/workspace/sidebar/scale.scss | 156 +++++++++ .../logic/scale_selected_test.cljs | 212 +++++++++++ frontend/test/frontend_tests/runner.cljs | 2 + frontend/translations/en.po | 48 +++ frontend/translations/es.po | 62 +++- 15 files changed, 922 insertions(+), 8 deletions(-) create mode 100644 frontend/resources/images/icons/scale.svg create mode 100644 frontend/src/app/main/ui/workspace/sidebar/scale.cljs create mode 100644 frontend/src/app/main/ui/workspace/sidebar/scale.scss create mode 100644 frontend/test/frontend_tests/logic/scale_selected_test.cljs diff --git a/frontend/resources/images/icons/scale.svg b/frontend/resources/images/icons/scale.svg new file mode 100644 index 0000000000..93b7010809 --- /dev/null +++ b/frontend/resources/images/icons/scale.svg @@ -0,0 +1,3 @@ + + + diff --git a/frontend/src/app/main/data/workspace.cljs b/frontend/src/app/main/data/workspace.cljs index 3b7a9c40b5..03098b4569 100644 --- a/frontend/src/app/main/data/workspace.cljs +++ b/frontend/src/app/main/data/workspace.cljs @@ -1719,6 +1719,7 @@ (dm/export dwt/update-positions) (dm/export dwt/flip-horizontal-selected) (dm/export dwt/flip-vertical-selected) +(dm/export dwt/scale-selected) (dm/export dwly/set-opacity) ;; Common diff --git a/frontend/src/app/main/data/workspace/shortcuts.cljs b/frontend/src/app/main/data/workspace/shortcuts.cljs index 5d05d6d8ba..0a81071eb2 100644 --- a/frontend/src/app/main/data/workspace/shortcuts.cljs +++ b/frontend/src/app/main/data/workspace/shortcuts.cljs @@ -58,6 +58,14 @@ (.preventDefault event) (st/emit! (toggle-layout-flag :display-guides))))) +(defn- escape + "Leave the scale content mode when it is active, keeping the + selection; otherwise clear the current drawing and selection." + [] + (if (contains? (deref refs/workspace-layout) :scale-text) + (st/emit! :interrupt (dw/remove-layout-flag :scale-text)) + (st/emit! :interrupt (dwdc/clear-drawing) (dw/deselect-all true)))) + (defn- emit-when-no-readonly [& events] (let [can-edit? (:can-edit (deref refs/permissions)) @@ -181,7 +189,7 @@ :subsections [:edit] :section [:workspace] :customizable false - :fn #(st/emit! :interrupt (dwdc/clear-drawing) (dw/deselect-all true))} + :fn escape} :find {:tooltip (ds/meta "F") :command (ds/c-mod "f") :subsections [:edit] :label (fn [] (tr "shortcuts.find")) diff --git a/frontend/src/app/main/data/workspace/transforms.cljs b/frontend/src/app/main/data/workspace/transforms.cljs index bb3f100d87..b2372250f0 100644 --- a/frontend/src/app/main/data/workspace/transforms.cljs +++ b/frontend/src/app/main/data/workspace/transforms.cljs @@ -1630,6 +1630,68 @@ (rx/of (dwm/apply-wasm-modifiers modifiers {:ignore-snap-pixel true})) (rx/of (dwm/apply-modifiers {:modifiers modifiers :ignore-snap-pixel true})))))))) +;; -- Scale --------------------------------------------------------- + +(defn- anchor->point + "Returns the point of `rect` selected by `anchor`, a + `[horizontal vertical]` pair. Horizontal is one of `:left`, `:center` + or `:right`; vertical is one of `:top`, `:center` or `:bottom`." + [rect [h v]] + (let [{:keys [x y width height]} rect] + (gpt/point (case h + :left x + :right (+ x width) + (+ x (/ width 2))) + (case v + :top y + :bottom (+ y height) + (+ y (/ height 2)))))) + +(defn scale-selected + "Scales the selected shapes and their content (strokes, texts, + shadows...) by `factor`, keeping the `anchor` point of the selection + bounds in place. Behaves like a resize in the scale content mode: + constraints are ignored." + [factor anchor] + (dm/assert! + "expected a positive scale factor" + (and (number? factor) (pos? factor))) + + (ptk/reify ::scale-selected + ptk/WatchEvent + (watch [_ state _] + (let [objects (dsh/lookup-page-objects state) + shapes (->> (dsh/lookup-selected state {:omit-blocked? true}) + (map (d/getf objects)) + (remove ctk/is-variant-container?))] + (if (or (empty? shapes) (mth/close? factor 1)) + (rx/empty) + (let [ids (map :id shapes) + origin (anchor->point (gsh/shapes->rect shapes) anchor) + scalev (gpt/point factor factor) + + modifier + (fn [shape] + (let [grow-type (get shape :grow-type) + new-grow-type (when (cfh/text-shape? shape) + (dwm/next-grow-type grow-type scalev))] + (cond-> (ctm/resize-modifiers scalev origin) + :always + (ctm/scale-content factor) + + (and (some? new-grow-type) (not= new-grow-type grow-type)) + (ctm/change-property :grow-type new-grow-type)))) + + modif-tree (dwm/build-modif-tree ids objects modifier)] + + (if (features/active-feature? state "render-wasm/v1") + (rx/of (dwm/apply-wasm-modifiers modif-tree + :ignore-constraints true + :ignore-snap-pixel true)) + (rx/of (dwm/apply-modifiers {:modifiers modif-tree + :ignore-constraints true + :ignore-snap-pixel true}))))))))) + (defn fit-layout-modifiers [objects frame] ;; Set temporaly the auto flag and calculate a reflow to resize and position diff --git a/frontend/src/app/main/ui/ds/controls/combobox.cljs b/frontend/src/app/main/ui/ds/controls/combobox.cljs index 2174d84a78..8dfb5647ce 100644 --- a/frontend/src/app/main/ui/ds/controls/combobox.cljs +++ b/frontend/src/app/main/ui/ds/controls/combobox.cljs @@ -12,7 +12,7 @@ [app.main.constants :refer [max-input-length]] [app.main.ui.ds.controls.select :refer [handle-focus-change]] [app.main.ui.ds.controls.shared.options-dropdown :refer [options-dropdown* schema:option]] - [app.main.ui.ds.foundations.assets.icon :refer [icon*] :as i] + [app.main.ui.ds.foundations.assets.icon :refer [icon* icon-list] :as i] [app.util.dom :as dom] [app.util.globals :as globals] [app.util.keyboard :as kbd] @@ -23,6 +23,9 @@ [rumext.v2.util :as mfu]) (:import goog.events.EventType)) +(def ^:private schema:icon + [:and :string [:fn #(contains? icon-list %)]]) + (def ^:private schema:combobox [:map [:id {:optional true} :string] @@ -30,6 +33,7 @@ [:class {:optional true} :string] [:max-length {:optional true} :int] [:placeholder {:optional true} :string] + [:icon {:optional true} [:maybe schema:icon]] [:disabled {:optional true} :boolean] [:default-selected {:optional true} :string] [:select-only {:optional true} :boolean] @@ -39,7 +43,7 @@ (mf/defc combobox* {::mf/schema schema:combobox} - [{:keys [id options class placeholder disabled has-error default-selected select-only max-length empty-to-end on-change] :rest props}] + [{:keys [id options class placeholder icon disabled has-error default-selected select-only max-length empty-to-end on-change] :rest props}] (let [;; NOTE: we use mfu/bean here for transparently handle ;; options provide as clojure data structures or javascript ;; plain objects and lists. @@ -106,6 +110,7 @@ (reset! selected-id* id) (reset! is-open* false) (reset! focused-id* nil) + (mf/set-ref-val! value-ref nil) (when (fn? on-change) (on-change id)))))) @@ -139,6 +144,9 @@ (if (some? selected-option) selected-id input-value))] + ;; The value was delivered, so an unmount must not + ;; report it again. + (mf/set-ref-val! value-ref nil) (on-change value)))))))) on-input-click @@ -202,6 +210,7 @@ (reset! selected-id* focused-id) (reset! is-open* false) (reset! focused-id* nil) + (mf/set-ref-val! value-ref nil) (dom/blur! (mf/ref-val input-ref)) (when (and (fn? on-change) (some? focused-id)) @@ -239,8 +248,9 @@ (d/seek #(= selected-id (get % :id)) options))) icon - (when selected-option - (get selected-option :icon)) + (or (when selected-option + (get selected-option :icon)) + icon) avatar (when selected-option diff --git a/frontend/src/app/main/ui/ds/controls/combobox.mdx b/frontend/src/app/main/ui/ds/controls/combobox.mdx index 86b8e11119..c4b39840c1 100644 --- a/frontend/src/app/main/ui/ds/controls/combobox.mdx +++ b/frontend/src/app/main/ui/ds/controls/combobox.mdx @@ -53,6 +53,18 @@ These are available in the `app.main.ds.foundations.assets.icon` namespace. ]}] ``` +The `combobox*` itself also accepts an optional `icon`. It shows at the start +of the input when the selected option has no icon of its own, for example when +the user types a value that matches no option. + +```clj +[:> combobox* + {:icon i/expand + :default-selected "1" + :options [{ :label "1x" :id "1" } + { :label "2x" :id "2" }]}] +``` + ### Avatars diff --git a/frontend/src/app/main/ui/ds/controls/combobox.stories.jsx b/frontend/src/app/main/ui/ds/controls/combobox.stories.jsx index 63882556a9..3794d6e9f5 100644 --- a/frontend/src/app/main/ui/ds/controls/combobox.stories.jsx +++ b/frontend/src/app/main/ui/ds/controls/combobox.stories.jsx @@ -72,6 +72,12 @@ export const WithIcons = { }, }; +export const WithIcon = { + args: { + icon: "expand", + }, +}; + export const EmptyToEnd = { args: { emptyToEnd: true, diff --git a/frontend/src/app/main/ui/ds/foundations/assets/icon.cljs b/frontend/src/app/main/ui/ds/foundations/assets/icon.cljs index 126cb7b4bc..51f6dbb4b3 100644 --- a/frontend/src/app/main/ui/ds/foundations/assets/icon.cljs +++ b/frontend/src/app/main/ui/ds/foundations/assets/icon.cljs @@ -239,6 +239,7 @@ (def ^:icon-id rotation "rotation") (def ^:icon-id row "row") (def ^:icon-id row-reverse "row-reverse") +(def ^:icon-id scale "scale") (def ^:icon-id search "search") (def ^:icon-id separate-nodes "separate-nodes") (def ^:icon-id settings "settings") diff --git a/frontend/src/app/main/ui/workspace/sidebar.cljs b/frontend/src/app/main/ui/workspace/sidebar.cljs index aed6882aea..63d4aa53b6 100644 --- a/frontend/src/app/main/ui/workspace/sidebar.cljs +++ b/frontend/src/app/main/ui/workspace/sidebar.cljs @@ -37,6 +37,7 @@ [app.main.ui.workspace.sidebar.history :refer [history-toolbox*]] [app.main.ui.workspace.sidebar.layers :refer [layers-toolbox*]] [app.main.ui.workspace.sidebar.options :refer [options-toolbox*]] + [app.main.ui.workspace.sidebar.scale :refer [scale-sidebar*]] [app.main.ui.workspace.sidebar.shortcuts :refer [shortcuts-container*]] [app.main.ui.workspace.sidebar.sitemap :refer [sitemap*]] [app.main.ui.workspace.sidebar.versions :refer [versions-toolbox*]] @@ -286,6 +287,7 @@ [{:keys [layout section file-id page-id drawing-tool active-tokens] :as props}] (let [is-comments? (= drawing-tool :comments) is-history? (contains? layout :document-history) + is-scale? (contains? layout :scale-text) is-inspect? (= section :inspect) is-debug? (= section :debug) @@ -295,6 +297,7 @@ can-be-expanded? (and (not is-comments?) (not is-history?) + (not is-scale?) is-inspect? (= current-section :code)) @@ -355,6 +358,9 @@ is-history? [:> history-content* {}] + is-scale? + [:> scale-sidebar* {}] + :else (let [props (mf/spread-props props {:on-change-section on-change-section diff --git a/frontend/src/app/main/ui/workspace/sidebar/scale.cljs b/frontend/src/app/main/ui/workspace/sidebar/scale.cljs new file mode 100644 index 0000000000..a62fda8f9c --- /dev/null +++ b/frontend/src/app/main/ui/workspace/sidebar/scale.cljs @@ -0,0 +1,331 @@ +;; 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.sidebar.scale + "Right sidebar panel of the scale content mode (K shortcut)." + (:require-macros [app.main.style :as stl]) + (:require + [app.common.data :as d] + [app.common.geom.shapes :as gsh] + [app.common.math :as mth] + [app.common.types.component :as ctk] + [app.main.data.workspace :as dw] + [app.main.features :as features] + [app.main.refs :as refs] + [app.main.store :as st] + [app.main.ui.components.title-bar :refer [title-bar*]] + [app.main.ui.ds.controls.combobox :refer [combobox*]] + [app.main.ui.ds.controls.numeric-input :refer [numeric-input*]] + [app.main.ui.ds.foundations.assets.icon :as i] + [app.main.ui.ds.product.panel-title :refer [panel-title*]] + [app.main.ui.ds.tooltip :refer [tooltip*]] + [app.main.ui.formats :as fmt] + [app.util.i18n :refer [tr]] + [cuerdas.core :as str] + [rumext.v2 :as mf])) + +(def ^:private scale-presets + [0.5 0.75 1 1.5 2 3 4]) + +(def ^:private anchors + [{:value [:left :top] + :class (stl/css :anchor-top-left) + :label #(tr "workspace.scale.anchor.top-left")} + {:value [:center :top] + :class (stl/css :anchor-top-center) + :label #(tr "workspace.scale.anchor.top-center")} + {:value [:right :top] + :class (stl/css :anchor-top-right) + :label #(tr "workspace.scale.anchor.top-right")} + {:value [:left :center] + :class (stl/css :anchor-center-left) + :label #(tr "workspace.scale.anchor.center-left")} + {:value [:center :center] + :class (stl/css :anchor-center) + :label #(tr "workspace.scale.anchor.center")} + {:value [:right :center] + :class (stl/css :anchor-center-right) + :label #(tr "workspace.scale.anchor.center-right")} + {:value [:left :bottom] + :class (stl/css :anchor-bottom-left) + :label #(tr "workspace.scale.anchor.bottom-left")} + {:value [:center :bottom] + :class (stl/css :anchor-bottom-center) + :label #(tr "workspace.scale.anchor.bottom-center")} + {:value [:right :bottom] + :class (stl/css :anchor-bottom-right) + :label #(tr "workspace.scale.anchor.bottom-right")}]) + +(def ^:private default-anchor + [:center :center]) + +(def ^:private factor-pattern + #"^(?:[0-9]+(?:\.[0-9]*)?|\.[0-9]+)$") + +(defn- factor->string + [factor] + (let [precision (cond + (< factor 0.01) 4 + (< factor 0.1) 3 + :else 2)] + (or (fmt/format-number factor {:precision precision}) + "1"))) + +(defn- parse-factor + "Parses a scale typed by the user, like `2`, `1.5x` or `0,5x`. + Returns nil when the value is not a positive number, optionally + followed by an `x`." + [value] + (let [text (-> (str value) + (str/trim) + (str/lower) + (str/replace #"x$" "") + (str/trim) + (str/replace "," "."))] + (when (re-matches factor-pattern text) + (let [factor (d/parse-double text)] + (when (and (mth/finite? factor) (pos? factor)) + factor))))) + +(defn scale-state-for-selection + "Starts a scale session at 1x for the selected IDs." + [selected] + {:selection selected :factor 1}) + +(defn selection-scale-factor + "Returns 1x when the stored scale belongs to a different selection." + [scale-state selected] + (if (= selected (:selection scale-state)) + (:factor scale-state) + 1)) + +(defn selection-anchor + "Returns the saved anchor, or the center for a new non-empty selection." + [anchor-state selected] + (when (seq selected) + (if (= selected (:selection anchor-state)) + (:anchor anchor-state) + default-anchor))) + +(defn calculate-scale-after-dimension-change + "Updates the current scale factor by a width or height change ratio." + [factor dimension new-dimension] + (* factor (/ new-dimension dimension))) + +(defn- apply-preview-modifiers + [shapes modifiers wasm-modifiers wasm?] + (mapv (fn [shape] + (let [id (:id shape)] + (if wasm? + (gsh/apply-transform shape (get wasm-modifiers id)) + (gsh/transform-shape shape (get-in modifiers [id :modifiers]))))) + shapes)) + +(defn- scale-input-operation + [factor value] + (when-let [new-factor (parse-factor value)] + (when-not (mth/close? new-factor factor) + {:multiplier (/ new-factor factor) + :factor new-factor}))) + +(mf/defc anchor-point-selector* + {::mf/private true} + [{:keys [value disabled group-name on-change]}] + [:div {:class (stl/css :anchor-grid) + :role "radiogroup" + :aria-label (tr "workspace.scale.anchor-point")} + (for [{anchor-value :value + anchor-class :class + get-label :label} anchors] + (let [selected? (= anchor-value value) + label (get-label)] + [:> tooltip* {:key (str (first anchor-value) "-" (second anchor-value)) + :content label + :class (str anchor-class " " (stl/css :anchor-point-tooltip))} + [:label {:class [(stl/css :anchor-point) + (stl/css-case :center-area (= anchor-value default-anchor) + :disabled disabled)]} + [:input {:type "radio" + :class (stl/css :anchor-input) + :name group-name + :checked selected? + :disabled disabled + :aria-label label + :on-change #(on-change anchor-value)}] + [:span {:class (stl/css :anchor-mark)}]]]))]) + +(mf/defc scale-sidebar* + [] + (let [selected (mf/deref refs/selected-shapes) + shapes-ref (mf/with-memo [selected] + (refs/objects-by-id selected)) + shapes-data (mf/deref shapes-ref) + + workspace-modifiers (mf/deref refs/workspace-modifiers) + wasm-modifiers (mf/deref refs/workspace-wasm-modifiers) + wasm? (features/active-feature? @st/state "render-wasm/v1") + + selected-shapes (mf/with-memo [shapes-data] + (remove ctk/is-variant-container? shapes-data)) + + shapes (mf/with-memo [selected-shapes workspace-modifiers wasm-modifiers wasm?] + (apply-preview-modifiers selected-shapes + workspace-modifiers + (into {} wasm-modifiers) + wasm?)) + + rect (mf/with-memo [shapes] + (when (seq shapes) + (gsh/shapes->rect shapes))) + + width (get rect :width) + height (get rect :height) + size (when rect (max width height)) + disabled (or (nil? size) (mth/almost-zero? size)) + + width-disabled (or disabled (mth/almost-zero? (d/nilv width 0))) + height-disabled (or disabled (mth/almost-zero? (d/nilv height 0))) + + scale-state* (mf/use-state (scale-state-for-selection selected)) + scale-state (deref scale-state*) + factor (selection-scale-factor scale-state selected) + + factor-str (factor->string factor) + factor-id (str factor-str) + + anchor* (mf/use-state nil) + anchor-map (deref anchor*) + + anchor (selection-anchor anchor-map selected) + + width-id (mf/use-id) + height-id (mf/use-id) + scale-id (mf/use-id) + size-title-id (mf/use-id) + scale-title-id (mf/use-id) + group-id (mf/use-id) + + revision* (mf/use-state 0) + revision (deref revision*) + + scale-options + (mf/with-memo [] + (mapv (fn [factor] + (let [value (factor->string factor)] + {:id value + :label (str value "x")})) + scale-presets)) + + default-selected + (if (some #(= factor-id (:id %)) scale-options) + factor-id + (str factor-id "x")) + + reset-scale-input + (mf/use-fn + #(swap! revision* inc)) + + scale-by + (mf/use-fn + (mf/deps size anchor factor selected) + (fn [multiplier new-factor] + (when (and (some? size) + (pos? size) + (pos? multiplier)) + (st/emit! (dw/scale-selected multiplier + (or anchor default-anchor))) + (reset! scale-state* {:selection selected :factor new-factor})))) + + on-width-change + (mf/use-fn + (mf/deps width factor scale-by) + (fn [value] + (when (and (number? value) (some? width) (pos? width) (pos? value)) + (scale-by (/ value width) + (calculate-scale-after-dimension-change factor width value))))) + + on-height-change + (mf/use-fn + (mf/deps height factor scale-by) + (fn [value] + (when (and (number? value) (some? height) (pos? height) (pos? value)) + (scale-by (/ value height) + (calculate-scale-after-dimension-change factor height value))))) + + on-scale-change + (mf/use-fn + (mf/deps factor scale-by reset-scale-input) + (fn [value] + (if-let [{:keys [multiplier factor]} (scale-input-operation factor value)] + (scale-by multiplier factor) + ;; Normalize the display without scaling for invalid or unchanged input. + (reset-scale-input)))) + + on-anchor-change + (mf/use-fn + (mf/deps selected) + (fn [anchor] + (reset! anchor* {:selection selected :anchor anchor}))) + + on-close + (mf/use-fn + #(st/emit! (dw/remove-layout-flag :scale-text)))] + + (mf/with-layout-effect [selected] + (when-not (= selected (:selection (deref scale-state*))) + (reset! scale-state* (scale-state-for-selection selected)) + (reset! anchor* nil))) + + [:div {:class (stl/css :scale-sidebar)} + [:> panel-title* {:class (stl/css :scale-title) + :text (tr "workspace.scale.title") + :on-close on-close}] + + [:div {:class (stl/css :scale-content)} + [:section {:class (stl/css :scale-row) + :aria-labelledby size-title-id} + [:div {:id size-title-id} + [:> title-bar* {:collapsable false + :title (tr "workspace.options.size")}]] + [:div {:class (stl/css :scale-fields)} + [:div {:class (stl/css :scale-field)} + [:> numeric-input* {:id width-id + :icon i/character-w + :property (tr "workspace.options.width") + :min 0.01 + :disabled width-disabled + :placeholder "--" + :value width + :on-change on-width-change}]] + [:div {:class (stl/css :scale-field)} + [:> numeric-input* {:id height-id + :icon i/character-h + :property (tr "workspace.options.height") + :min 0.01 + :disabled height-disabled + :placeholder "--" + :value height + :on-change on-height-change}]]]] + + [:section {:class (stl/css :scale-row) + :aria-labelledby scale-title-id} + [:div {:id scale-title-id} + [:> title-bar* {:collapsable false + :title (tr "workspace.scale.scale-and-anchor")}]] + [:div {:class (stl/css :scale-fields)} + [:div {:class (stl/css :scale-field)} + [:> combobox* {:key (str revision "-" factor-id) + :id scale-id + :icon i/scale + :disabled disabled + :options scale-options + :default-selected default-selected + :on-change on-scale-change}]] + [:div {:class (stl/css :scale-field)} + [:> anchor-point-selector* {:value anchor + :disabled disabled + :group-name (str "scale-anchor-" group-id) + :on-change on-anchor-change}]]]]]])) diff --git a/frontend/src/app/main/ui/workspace/sidebar/scale.scss b/frontend/src/app/main/ui/workspace/sidebar/scale.scss new file mode 100644 index 0000000000..f9dc4abd64 --- /dev/null +++ b/frontend/src/app/main/ui/workspace/sidebar/scale.scss @@ -0,0 +1,156 @@ +// 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 + +@use "ds/_borders.scss" as *; +@use "ds/_sizes.scss" as *; +@use "ds/_utils.scss" as *; +@use "ds/typography.scss" as t; + +.scale-sidebar { + display: flex; + flex-direction: column; + block-size: 100%; + overflow-y: auto; + background-color: var(--color-background-primary); +} + +.scale-title { + margin: var(--sp-s) var(--sp-s) 0; +} + +.scale-content { + display: flex; + flex-direction: column; + gap: var(--sp-l); + padding: var(--sp-m) var(--sp-m) var(--sp-l); +} + +.scale-row { + display: flex; + flex-direction: column; + gap: var(--sp-xs); +} + +.scale-fields { + display: grid; + grid-template-columns: 1fr 1fr; + align-items: start; + gap: var(--sp-xs); +} + +.scale-field { + position: relative; + min-inline-size: 0; +} + +// --- Anchor point selector ---------------------------------------- + +.anchor-grid { + --anchor-side-size: #{$sz-24}; + --anchor-center-size: #{px2rem(60)}; + + display: grid; + grid-template-areas: + "top-left top-center top-right" + "center-left center center-right" + "bottom-left bottom-center bottom-right"; + grid-template-columns: var(--anchor-side-size) var(--anchor-center-size) var(--anchor-side-size); + grid-template-rows: var(--anchor-side-size) var(--anchor-center-size) var(--anchor-side-size); + inline-size: fit-content; + block-size: fit-content; + border-radius: $br-8; + background-color: var(--color-background-tertiary); +} + +.anchor-grid .anchor-point-tooltip { + inline-size: 100%; + block-size: 100%; +} + +.anchor-point { + position: relative; + display: grid; + place-items: center; +} + +.anchor-top-left { + grid-area: top-left; +} + +.anchor-top-center { + grid-area: top-center; +} + +.anchor-top-right { + grid-area: top-right; +} + +.anchor-center-left { + grid-area: center-left; +} + +.anchor-center { + grid-area: center; +} + +.anchor-center-right { + grid-area: center-right; +} + +.anchor-bottom-left { + grid-area: bottom-left; +} + +.anchor-bottom-center { + grid-area: bottom-center; +} + +.anchor-bottom-right { + grid-area: bottom-right; +} + +.center-area { + border-radius: $br-8; + background-color: var(--color-background-primary); +} + +.anchor-input { + position: absolute; + inline-size: $sz-1; + block-size: $sz-1; + margin: 0; + opacity: 0; + pointer-events: none; +} + +.anchor-mark { + inline-size: px2rem(10); + block-size: px2rem(10); + border-radius: px2rem(6); + outline: $b-2 solid transparent; + background-color: var(--color-foreground-secondary); +} + +.anchor-point:hover .anchor-mark { + background-color: var(--color-foreground-primary); +} + +.anchor-input:checked + .anchor-mark { + outline-color: var(--color-accent-primary-muted); + background-color: var(--color-accent-primary); +} + +.anchor-input:focus-visible + .anchor-mark { + outline-color: var(--color-accent-primary); +} + +.anchor-point.disabled .anchor-mark { + background-color: var(--color-foreground-disabled); +} + +.anchor-point.disabled .anchor-input:checked + .anchor-mark { + background-color: var(--color-accent-primary-muted); +} diff --git a/frontend/test/frontend_tests/logic/scale_selected_test.cljs b/frontend/test/frontend_tests/logic/scale_selected_test.cljs new file mode 100644 index 0000000000..4c05636ece --- /dev/null +++ b/frontend/test/frontend_tests/logic/scale_selected_test.cljs @@ -0,0 +1,212 @@ +;; 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.logic.scale-selected-test + "Tests for the scale-selected event used by the scale sidebar panel." + (:require + [app.common.data :as d] + [app.common.geom.point :as gpt] + [app.common.geom.rect :as grc] + [app.common.geom.shapes :as gsh] + [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.common.types.modifiers :as ctm] + [app.common.types.text :as txt] + [app.main.data.workspace.selection :as dws] + [app.main.data.workspace.transforms :as dwt] + [app.main.ui.workspace.sidebar.scale :as scale] + [cljs.test :as t :include-macros true] + [frontend-tests.helpers.state :as ths] + [frontend-tests.helpers.wasm :as thw])) + +(def ^:private parse-factor + @#'scale/parse-factor) + +(def ^:private scale-input-operation + @#'scale/scale-input-operation) + +(def ^:private apply-preview-modifiers + @#'scale/apply-preview-modifiers) + +(t/use-fixtures :each + {:before (fn [] + (cthi/reset-idmap!) + (thw/setup-wasm-mocks!)) + :after thw/teardown-wasm-mocks!}) + +(defn- shape-rect + [file label] + (-> (cths/get-shape file label) + :points + grc/points->rect)) + +(defn- assert-rect + [rect x y width height] + (t/is (= x (:x rect))) + (t/is (= y (:y rect))) + (t/is (= width (:width rect))) + (t/is (= height (:height rect)))) + +(t/deftest scale-sidebar-factor-starts-at-one-for-a-new-selection + (let [selection #{:shape} + state (scale/scale-state-for-selection selection)] + (t/is (= 1 (scale/selection-scale-factor state selection))))) + +(t/deftest scale-sidebar-factor-resets-after-deselecting-and-reselecting + (let [selection #{:shape} + scaled {:selection selection :factor 1.5} + deselected (scale/scale-state-for-selection #{}) + reselected (scale/scale-state-for-selection selection)] + (t/is (= 1 (scale/selection-scale-factor deselected #{}))) + (t/is (= 1 (scale/selection-scale-factor reselected selection))) + (t/is (= 1 (scale/selection-scale-factor scaled #{}))))) + +(t/deftest scale-sidebar-factor-is-independent-of-canvas-resize + (let [selection #{:shape} + state {:selection selection :factor 1.5}] + (t/is (= 1.5 (scale/selection-scale-factor state selection))))) + +(t/deftest scale-sidebar-factor-updates-after-dimension-input + (t/is (= 1.5 (scale/calculate-scale-after-dimension-change 1 100 150)))) + +(t/deftest scale-sidebar-size-uses-live-resize-modifiers + (let [file (-> (cthf/sample-file :file1) + (ctho/add-rect :rect1 :x 0 :y 0 :width 100 :height 50)) + shape (cths/get-shape file :rect1) + resize (ctm/resize-modifiers (gpt/point 2 2) (gpt/point 0 0)) + modifiers {(:id shape) {:modifiers resize}} + wasm-mtx (ctm/modifiers->transform resize) + normal (apply-preview-modifiers [shape] modifiers {} false) + wasm (apply-preview-modifiers [shape] {} {(:id shape) wasm-mtx} true)] + (t/is (= 200 (:width (gsh/shapes->rect normal)))) + (t/is (= 100 (:height (gsh/shapes->rect normal)))) + (t/is (= 200 (:width (gsh/shapes->rect wasm)))) + (t/is (= 100 (:height (gsh/shapes->rect wasm)))))) + +(t/deftest scale-sidebar-defaults-to-center-anchor-for-a-selection + (t/is (= [:center :center] + (scale/selection-anchor nil #{:shape}))) + (t/is (nil? (scale/selection-anchor nil #{})))) + +(t/deftest scale-sidebar-uses-the-user-selected-anchor + (let [selection #{:shape} + state {:selection selection :anchor [:left :top]}] + (t/is (= [:left :top] + (scale/selection-anchor state selection))))) + +(t/deftest scale-sidebar-does-not-keep-the-anchor-between-selections + (let [previous-selection #{:shape-1} + current-selection #{:shape-2} + state {:selection previous-selection :anchor [:left :top]}] + (t/is (= [:center :center] + (scale/selection-anchor state current-selection))))) + +(t/deftest scale-sidebar-parses-custom-scale-factors + (doseq [[value expected] [["1.5x" 1.5] + ["0,5x" 0.5] + [" 2 X " 2]]] + (t/is (= expected (parse-factor value)) + (str "parses " value)))) + +(t/deftest scale-sidebar-rejects-invalid-custom-scale-factors + (doseq [value ["0" "-1" "not a number"]] + (t/is (nil? (parse-factor value)) + (str "rejects " value)))) + +(t/deftest scale-sidebar-does-not-scale-invalid-or-unchanged-input + (t/is (nil? (scale-input-operation 1 "0"))) + (t/is (nil? (scale-input-operation 1 "not a number"))) + (t/is (nil? (scale-input-operation 1 "1x")))) + +(t/deftest scale-sidebar-builds-an-operation-for-a-new-scale-factor + (t/is (= {:multiplier 1.5 :factor 1.5} + (scale-input-operation 1 "1.5x")))) + +(t/deftest scale-selected-keeps-the-center-anchor-in-place + (t/async + done + (let [file (-> (cthf/sample-file :file1) + (ctho/add-rect :rect1 + :x 100 :y 100 :width 100 :height 50)) + rect (cths/get-shape file :rect1) + store (ths/setup-store file)] + + (ths/run-store + store done + [(dws/select-shape (:id rect)) + (dwt/scale-selected 2 [:center :center])] + (fn [state] + (let [file' (ths/get-file-from-state state)] + (assert-rect (shape-rect file' :rect1) 50 75 200 100))))))) + +(t/deftest scale-selected-keeps-the-top-left-anchor-in-place + (t/async + done + (let [file (-> (cthf/sample-file :file1) + (ctho/add-rect :rect1 + :x 100 :y 100 :width 100 :height 50)) + rect (cths/get-shape file :rect1) + store (ths/setup-store file)] + + (ths/run-store + store done + [(dws/select-shape (:id rect)) + (dwt/scale-selected 3 [:left :top])] + (fn [state] + (let [file' (ths/get-file-from-state state)] + (assert-rect (shape-rect file' :rect1) 100 100 300 150))))))) + +(t/deftest scale-selected-scales-strokes-and-text + (t/async + done + (let [file (-> (cthf/sample-file :file1) + (ctho/add-rect :rect1 + :x 0 :y 0 :width 100 :height 100 + :strokes [{:stroke-color "#000000" + :stroke-opacity 1 + :stroke-width 2 + :stroke-alignment :inner}]) + (ctho/add-text :text1 "Hello" :text-params {:x 200 :y 0})) + rect (cths/get-shape file :rect1) + text (cths/get-shape file :text1) + store (ths/setup-store file)] + + (ths/run-store + store done + [(dws/select-shapes (d/ordered-set (:id rect) (:id text))) + (dwt/scale-selected 2 [:left :top])] + (fn [state] + (let [file' (ths/get-file-from-state state) + rect' (cths/get-shape file' :rect1) + text' (cths/get-shape file' :text1) + text-node (first (txt/node-seq txt/is-text-node? (:content text')))] + + (assert-rect (shape-rect file' :rect1) 0 0 200 200) + (t/is (= 4 (get-in rect' [:strokes 0 :stroke-width])) + "stroke width scales with the selection") + (t/is (= "28" (:font-size text-node)) + "font size scales with the selection"))))))) + +(t/deftest scale-selected-ignores-constraints-in-the-legacy-renderer + (t/async + done + (let [file (ctho/add-frame-with-child + (cthf/sample-file :file1) :frame1 :child1 + :frame-params {:x 0 :y 0 :width 200 :height 200} + :child-params {:x 50 :y 50 :width 50 :height 50}) + frame (cths/get-shape file :frame1) + store (ths/setup-store file {:renderer :svg})] + + (ths/run-store + store done + [(dws/select-shape (:id frame)) + (dwt/scale-selected 2 [:left :top])] + (fn [state] + (let [file' (ths/get-file-from-state state)] + (assert-rect (shape-rect file' :frame1) 0 0 400 400) + (assert-rect (shape-rect file' :child1) 100 100 100 100))))))) diff --git a/frontend/test/frontend_tests/runner.cljs b/frontend/test/frontend_tests/runner.cljs index 57298f62c0..0ceb498519 100644 --- a/frontend/test/frontend_tests/runner.cljs +++ b/frontend/test/frontend_tests/runner.cljs @@ -84,6 +84,7 @@ [frontend-tests.logic.path-helpers-test] [frontend-tests.logic.path-lifecycle-test] [frontend-tests.logic.path-tools-test] + [frontend-tests.logic.scale-selected-test] [frontend-tests.logic.sidebar-transform-coalescing-test] [frontend-tests.logic.update-position-test] [frontend-tests.logic.wasm-modifiers-nil-id-test] @@ -307,6 +308,7 @@ 'frontend-tests.logic.path-lifecycle-test 'frontend-tests.logic.path-tools-test 'frontend-tests.logic.pasting-in-containers-test + 'frontend-tests.logic.scale-selected-test 'frontend-tests.main.refs-test 'frontend-tests.main-errors-test 'frontend-tests.logic.sidebar-transform-coalescing-test diff --git a/frontend/translations/en.po b/frontend/translations/en.po index 97b210489e..b25f177276 100644 --- a/frontend/translations/en.po +++ b/frontend/translations/en.po @@ -8750,6 +8750,54 @@ msgstr "**Editing is disabled** because changes can’t be saved." msgid "workspace.save-failure.stopped" msgstr "**Editing is disabled** because changes can’t be saved." +#: src/app/main/ui/workspace/sidebar/scale.cljs +msgid "workspace.scale.anchor-point" +msgstr "Anchor point" + +#: src/app/main/ui/workspace/sidebar/scale.cljs +msgid "workspace.scale.anchor.bottom-center" +msgstr "Bottom center" + +#: src/app/main/ui/workspace/sidebar/scale.cljs +msgid "workspace.scale.anchor.bottom-left" +msgstr "Bottom left" + +#: src/app/main/ui/workspace/sidebar/scale.cljs +msgid "workspace.scale.anchor.bottom-right" +msgstr "Bottom right" + +#: src/app/main/ui/workspace/sidebar/scale.cljs +msgid "workspace.scale.anchor.center" +msgstr "Center" + +#: src/app/main/ui/workspace/sidebar/scale.cljs +msgid "workspace.scale.anchor.center-left" +msgstr "Center left" + +#: src/app/main/ui/workspace/sidebar/scale.cljs +msgid "workspace.scale.anchor.center-right" +msgstr "Center right" + +#: src/app/main/ui/workspace/sidebar/scale.cljs +msgid "workspace.scale.anchor.top-center" +msgstr "Top center" + +#: src/app/main/ui/workspace/sidebar/scale.cljs +msgid "workspace.scale.anchor.top-left" +msgstr "Top left" + +#: src/app/main/ui/workspace/sidebar/scale.cljs +msgid "workspace.scale.anchor.top-right" +msgstr "Top right" + +#: src/app/main/ui/workspace/sidebar/scale.cljs +msgid "workspace.scale.scale-and-anchor" +msgstr "Scale and anchor point" + +#: src/app/main/ui/workspace/sidebar/scale.cljs +msgid "workspace.scale.title" +msgstr "Scale" + #: src/app/main/ui/workspace/context_menu.cljs:667 msgid "workspace.shape.menu.add-flex" msgstr "Add flex layout" diff --git a/frontend/translations/es.po b/frontend/translations/es.po index 08a7b56a21..9aed20a322 100644 --- a/frontend/translations/es.po +++ b/frontend/translations/es.po @@ -5872,7 +5872,9 @@ msgstr "Desbloquea Enterprise" #: src/app/main/ui/dashboard/subscription.cljs:219 msgid "subscription.dashboard.banner.unlock-features-description" -msgstr "Para organizaciones que necesitan una gobernanza avanzada, una administración centralizada y una colaboración segura." +msgstr "" +"Para organizaciones que necesitan una gobernanza avanzada, una " +"administración centralizada y una colaboración segura." #: src/app/main/ui/dashboard/subscription.cljs:224 msgid "subscription.dashboard.banner.upgrade-nitrate" @@ -7185,7 +7187,11 @@ msgstr "RECURSOS EN ESTE ARCHIVO" #: src/app/main/data/workspace/libraries.cljs:1963 msgid "workspace.libraries.broken-library-disconnected" -msgstr "No se ha podido cargar la biblioteca \"%s\" y se ha desconectado de este archivo. Los componentes y colores que ya usa el archivo no se ven afectados, pero quedarán huérfanos hasta que la biblioteca se arregle y se vuelva a conectar." +msgstr "" +"No se ha podido cargar la biblioteca \"%s\" y se ha desconectado de este " +"archivo. Los componentes y colores que ya usa el archivo no se ven " +"afectados, pero quedarán huérfanos hasta que la biblioteca se arregle y se " +"vuelva a conectar." #: src/app/main/ui/workspace/libraries.cljs:178, src/app/main/ui/workspace/libraries.cljs:207, src/app/main/ui/workspace/libraries.cljs:251 msgid "workspace.libraries.colors" @@ -8656,7 +8662,9 @@ msgstr "No se encuentran extensiones" #: src/app/main/data/plugins.cljs, src/app/main/ui/dashboard.cljs, src/app/main/ui/workspace/plugins.cljs msgid "workspace.plugins.error.global-not-allowed" -msgstr "Por ahora, las extensiones que funcionan fuera de un archivo solo se pueden instalar desde localhost." +msgstr "" +"Por ahora, las extensiones que funcionan fuera de un archivo solo se pueden " +"instalar desde localhost." #: src/app/main/ui/workspace/plugins.cljs:237 msgid "workspace.plugins.error.manifest" @@ -8829,6 +8837,54 @@ msgstr "**Edición desactivada** porque no se pueden guardar los cambios." msgid "workspace.save-failure.stopped" msgstr "**Edición desactivada** porque no se pueden guardar los cambios." +#: src/app/main/ui/workspace/sidebar/scale.cljs +msgid "workspace.scale.anchor-point" +msgstr "Punto de anclaje" + +#: src/app/main/ui/workspace/sidebar/scale.cljs +msgid "workspace.scale.anchor.bottom-center" +msgstr "Abajo en el centro" + +#: src/app/main/ui/workspace/sidebar/scale.cljs +msgid "workspace.scale.anchor.bottom-left" +msgstr "Abajo a la izquierda" + +#: src/app/main/ui/workspace/sidebar/scale.cljs +msgid "workspace.scale.anchor.bottom-right" +msgstr "Abajo a la derecha" + +#: src/app/main/ui/workspace/sidebar/scale.cljs +msgid "workspace.scale.anchor.center" +msgstr "Centro" + +#: src/app/main/ui/workspace/sidebar/scale.cljs +msgid "workspace.scale.anchor.center-left" +msgstr "Centro a la izquierda" + +#: src/app/main/ui/workspace/sidebar/scale.cljs +msgid "workspace.scale.anchor.center-right" +msgstr "Centro a la derecha" + +#: src/app/main/ui/workspace/sidebar/scale.cljs +msgid "workspace.scale.anchor.top-center" +msgstr "Arriba en el centro" + +#: src/app/main/ui/workspace/sidebar/scale.cljs +msgid "workspace.scale.anchor.top-left" +msgstr "Arriba a la izquierda" + +#: src/app/main/ui/workspace/sidebar/scale.cljs +msgid "workspace.scale.anchor.top-right" +msgstr "Arriba a la derecha" + +#: src/app/main/ui/workspace/sidebar/scale.cljs +msgid "workspace.scale.scale-and-anchor" +msgstr "Escala y punto de anclaje" + +#: src/app/main/ui/workspace/sidebar/scale.cljs +msgid "workspace.scale.title" +msgstr "Escalado" + #: src/app/main/ui/workspace/context_menu.cljs:667 msgid "workspace.shape.menu.add-flex" msgstr "Añadir flex layout"