diff --git a/frontend/src/app/main/data/exports/assets.cljs b/frontend/src/app/main/data/exports/assets.cljs index f041557921..b10250e833 100644 --- a/frontend/src/app/main/data/exports/assets.cljs +++ b/frontend/src/app/main/data/exports/assets.cljs @@ -28,6 +28,8 @@ [cuerdas.core :as str] [potok.v2.core :as ptk])) +(def raster-export-types #{:png :jpeg :webp}) + (def default-timeout 5000) ;; Longest wait for the pending changes of the exported files to be saved. diff --git a/frontend/src/app/main/render.cljs b/frontend/src/app/main/render.cljs index 4a45630454..c2c95c468c 100644 --- a/frontend/src/app/main/render.cljs +++ b/frontend/src/app/main/render.cljs @@ -460,7 +460,7 @@ (mf/defc object-svg {::mf/wrap [mf/memo]} - [{:keys [objects object-id embed skip-children] + [{:keys [objects object-id embed skip-children class] :or {embed false} :as props}] (let [object (get objects object-id) @@ -482,6 +482,7 @@ [:& (mf/provider export/include-metadata-ctx) {:value false} [:& (mf/provider embed/context) {:value embed} [:svg {:id (dm/str "screenshot-" object-id) + :class class :view-box vbox :width (ust/format-precision width viewbox-decimal-precision) :height (ust/format-precision height viewbox-decimal-precision) diff --git a/frontend/src/app/main/ui/exports/preview.cljs b/frontend/src/app/main/ui/exports/preview.cljs new file mode 100644 index 0000000000..493726c8df --- /dev/null +++ b/frontend/src/app/main/ui/exports/preview.cljs @@ -0,0 +1,178 @@ +;; 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.exports.preview + (:require-macros [app.main.style :as stl]) + (:require + [app.common.files.helpers :as cfh] + [app.common.geom.shapes.bounds :as gsb] + [app.common.logging :as log] + [app.main.data.exports.assets :as de] + [app.main.data.exports.wasm :as wasm.exports] + [app.main.features :as features] + [app.main.refs :as refs] + [app.main.render :as render] + [app.main.ui.components.title-bar :refer [title-bar*]] + [app.main.ui.ds.buttons.button :refer [button*]] + [app.main.ui.ds.controls.select :refer [select*]] + [app.main.ui.hooks :as hooks] + [app.render-wasm.api :as wasm.api] + [app.util.i18n :refer [tr]] + [app.util.strings :as ust] + [app.util.timers :as timers] + [app.util.webapi :as wapi] + [cuerdas.core :as str] + [okulary.core :as l] + [rumext.v2 :as mf])) + +;; Keep in sync with --export-preview-size in preview.scss. +(def ^:private preview-size 240) + +(defn preview-settings + "Keep raster previews small without changing the displayed export dimensions. + SVG and PDF use a raster approximation at their natural size." + [{:keys [width height]} {:keys [type scale]}] + (let [raster? (contains? de/raster-export-types type) + scale (if raster? (or scale 1) 1)] + {:type (if raster? type :png) + :scale (min scale (/ preview-size (max 1 width height))) + :width (js/Math.ceil (* width scale)) + :height (js/Math.ceil (* height scale))})) + +(defn preview-bounds + "Use the same bounds as the active export renderer." + [objects object wasm?] + (or (when (and wasm? (wasm.api/initialized?)) + (wasm.api/get-shape-extrect (:id object))) + (let [bounds (gsb/get-object-bounds objects object {:ignore-margin? false}) + rounded-size #(js/parseFloat (ust/format-precision % render/viewbox-decimal-precision))] + (-> bounds + (update :width rounded-size) + (update :height rounded-size))))) + +(mf/defc wasm-preview* + {::mf/private true} + [{:keys [objects object-id export bounds]}] + (let [image* (mf/use-state nil) + image @image* + retry* (mf/use-state 0) + retry @retry* + width (:width bounds) + height (:height bounds) + retry-fn (mf/use-fn #(swap! retry* inc))] + ;; Keep the current image alive while the next render is debounced. + ;; Release its URI only after replacement or when the preview unmounts. + (mf/use-effect + (mf/deps (:uri image)) + (fn [] + (when-let [uri (:uri image)] + #(wapi/revoke-uri uri)))) + (mf/use-effect + (mf/deps objects object-id export width height retry) + (fn [] + (let [task (timers/schedule + 200 + (fn [] + (try + (if (wasm.api/initialized?) + (let [settings (preview-settings {:width width :height height} export) + uri (wasm.exports/export-image-uri + (assoc settings :object-id object-id))] + (reset! image* {:uri uri})) + (reset! image* {:error true})) + (catch :default cause + (log/error :hint "export preview failed" :cause cause) + (reset! image* {:error true})))))] + #(timers/dispose! task)))) + (cond + (:uri image) + [:img {:class (stl/css :image) + :src (:uri image) + :alt (tr "workspace.options.export.preview-image")}] + + (:error image) + [:div {:class (stl/css :status) :role "status"} + [:span (tr "workspace.options.export.preview-error")] + [:> button* {:variant "secondary" :on-click retry-fn} (tr "labels.retry")]] + + :else + [:span {:class (stl/css :status) :role "status"} (tr "labels.loading")]))) + +(mf/defc preview-content* + {::mf/private true} + [{:keys [object-id exports from page-id]}] + (let [index* (mf/use-state 0) + index (min @index* (dec (count exports))) + export (nth exports index) + options (mf/with-memo [exports] + (mapv (fn [index export] + {:id (str index) + :label (str (inc index) ". " (str/upper (name (:type export))) + (when (contains? de/raster-export-types (:type export)) + (str " · " (:scale export) "x")) + (when (seq (:suffix export)) (str " · " (:suffix export))))}) + (range (count exports)) exports)) + wasm-enabled (features/use-feature "render-wasm/v1") + wasm? (and (= from :workspace) wasm-enabled) + objects-ref (mf/with-memo [object-id from page-id] + (l/derived + (fn [source] + (let [objects (if (= from :viewer) + (get-in source [:pages page-id :objects]) + source)] + (select-keys objects + (cons object-id (cfh/get-children-ids objects object-id))))) + (if (= from :viewer) refs/viewer-data refs/workspace-page-objects) + =)) + objects (mf/deref objects-ref) + object (get objects object-id) + bounds (mf/with-memo [objects object wasm?] + (when object + (preview-bounds objects object wasm?))) + on-change (mf/use-fn + (fn [value] + (reset! index* (js/parseInt value 10))))] + (when object + (let [settings (preview-settings bounds export)] + [:div {:class (stl/css :content)} + (when (> (count exports) 1) + [:> select* {:aria-label (tr "workspace.options.export.preview-preset") + :default-selected (str index) + :options options + :on-change on-change}]) + [:div {:class (stl/css-case :canvas true :opaque (= :jpeg (:type export))) + :style {"--export-preview-ratio" (/ (max 1 (:width bounds)) + (max 1 (:height bounds)))}} + (if wasm? + [:> wasm-preview* {:objects objects + :object-id object-id + :export export + :bounds bounds}] + [:div {:class (stl/css :svg) + :role "img" + :aria-label (tr "workspace.options.export.preview-image")} + [:& render/object-svg {:objects objects + :object-id object-id + :class (stl/css :svg-image)}]])] + [:div {:class (stl/css :dimensions)} + (str (:width settings) " × " (:height settings) " · " (str/upper (name (:type export))))]])))) + +(mf/defc export-preview* + [{:keys [object-id exports from page-id] :or {from :workspace}}] + (let [open* (hooks/use-persisted-state ::open false) + open @open* + panel-id (str "export-preview-" object-id) + toggle-fn (mf/use-fn #(swap! open* not))] + [:div {:class (stl/css :preview)} + [:> title-bar* {:title (tr "workspace.options.export.preview") + :collapsable true + :collapsed (not open) + :on-collapsed toggle-fn + :aria-expanded open + :aria-controls panel-id}] + (when open + [:div {:id panel-id} + [:> preview-content* {:object-id object-id :exports exports :from from :page-id page-id}]])])) diff --git a/frontend/src/app/main/ui/exports/preview.scss b/frontend/src/app/main/ui/exports/preview.scss new file mode 100644 index 0000000000..f5c27b4410 --- /dev/null +++ b/frontend/src/app/main/ui/exports/preview.scss @@ -0,0 +1,82 @@ +// 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/typography.scss" as t; +@use "ds/_borders.scss" as *; +@use "ds/_utils.scss" as *; + +.preview { + // Keep in sync with preview-size in preview.cljs. + --export-preview-size: #{px2rem(240)}; + + grid-column: 1 / -1; + min-inline-size: 0; +} + +.content { + display: flex; + flex-direction: column; + gap: var(--sp-s); + padding: var(--sp-s); +} + +.canvas { + display: flex; + justify-content: center; + align-items: center; + align-self: center; + inline-size: min(100%, calc(var(--export-preview-size) * var(--export-preview-ratio))); + aspect-ratio: var(--export-preview-ratio); + overflow: hidden; + background: repeating-conic-gradient(rgb(212 212 212) 0% 25%, rgb(255 255 255) 0% 50%); + background-size: var(--sp-s) var(--sp-s); + border: $b-1 solid var(--color-background-quaternary); + border-radius: $br-4; +} + +.opaque { + background: rgb(255 255 255); +} + +.image { + display: block; + inline-size: 100%; + block-size: 100%; + object-fit: contain; +} + +.svg { + display: flex; + justify-content: center; + inline-size: 100%; + block-size: 100%; + pointer-events: none; +} + +.svg-image { + inline-size: 100%; + block-size: 100%; +} + +.dimensions, +.status { + @include t.use-typography("body-small"); + + color: var(--color-foreground-secondary); +} + +.dimensions { + text-align: center; +} + +.status { + display: flex; + flex-direction: column; + gap: var(--sp-s); + padding: var(--sp-m); + text-align: center; + background: var(--color-background-primary); +} diff --git a/frontend/src/app/main/ui/inspect/attributes.cljs b/frontend/src/app/main/ui/inspect/attributes.cljs index 2818edf9c1..4c96828d8a 100644 --- a/frontend/src/app/main/ui/inspect/attributes.cljs +++ b/frontend/src/app/main/ui/inspect/attributes.cljs @@ -87,4 +87,5 @@ :type type :page-id page-id :file-id file-id + :from from :share-id share-id}]])) diff --git a/frontend/src/app/main/ui/inspect/exports.cljs b/frontend/src/app/main/ui/inspect/exports.cljs index c70e37ff05..fa8a54162c 100644 --- a/frontend/src/app/main/ui/inspect/exports.cljs +++ b/frontend/src/app/main/ui/inspect/exports.cljs @@ -17,6 +17,7 @@ [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] + [app.main.ui.exports.preview :refer [export-preview*]] [app.util.dom :as dom] [app.util.i18n :refer [tr c]] [app.util.keyboard :as kbd] @@ -28,7 +29,7 @@ (mf/defc exports {::mf/wrap [#(mf/memo % =)]} - [{:keys [shapes page-id file-id share-id type] :as props}] + [{:keys [shapes page-id file-id share-id type from] :or {from :viewer} :as props}] (let [exports (mf/use-state []) xstate (mf/deref refs/export) vstate (mf/deref refs/viewer-data) @@ -44,7 +45,7 @@ scale-enabled? (mf/use-callback (fn [export] - (#{:png :jpeg :webp} (:type export)))) + (contains? de/raster-export-types (:type export)))) in-progress? (:in-progress xstate) @@ -218,5 +219,10 @@ :disabled in-progress?} (if in-progress? (tr "workspace.options.exporting-object") - (tr "workspace.options.export-object" (c (count shapes))))])])) - + (tr "workspace.options.export-object" (c (count shapes))))]) + (when (and (= 1 (count shapes)) (seq @exports)) + [:> export-preview* {:key (-> shapes first :id) + :object-id (-> shapes first :id) + :exports @exports + :from from + :page-id page-id}])])) diff --git a/frontend/src/app/main/ui/inspect/styles.cljs b/frontend/src/app/main/ui/inspect/styles.cljs index 8eddf353e8..96a54a7b47 100644 --- a/frontend/src/app/main/ui/inspect/styles.cljs +++ b/frontend/src/app/main/ui/inspect/styles.cljs @@ -285,4 +285,5 @@ :type type :page-id page-id :file-id file-id + :from from :share-id share-id}]]])) diff --git a/frontend/src/app/main/ui/workspace/sidebar/options/menus/exports.cljs b/frontend/src/app/main/ui/workspace/sidebar/options/menus/exports.cljs index cd48557514..4dd5a2c761 100644 --- a/frontend/src/app/main/ui/workspace/sidebar/options/menus/exports.cljs +++ b/frontend/src/app/main/ui/workspace/sidebar/options/menus/exports.cljs @@ -17,6 +17,7 @@ [app.main.ui.ds.buttons.icon-button :refer [icon-button*]] [app.main.ui.ds.foundations.assets.icon :as i] [app.main.ui.exports.assets] + [app.main.ui.exports.preview :refer [export-preview*]] [app.util.dom :as dom] [app.util.i18n :refer [c tr]] [app.util.keyboard :as kbd] @@ -79,7 +80,7 @@ scale-enabled? (mf/use-fn (fn [export] - (#{:png :jpeg :webp} (:type export)))) + (contains? de/raster-export-types (:type export)))) on-download (mf/use-fn @@ -274,4 +275,9 @@ :disabled in-progress?} (if in-progress? (tr "workspace.options.exporting-object") - (tr "workspace.options.export-object" (c (count shapes-with-exports))))])])])) + (tr "workspace.options.export-object" (c (count shapes-with-exports))))]) + + (when (and (= 1 (count ids)) (vector? exports) (seq exports)) + [:> export-preview* {:key (first ids) + :object-id (first ids) + :exports exports}])])])) diff --git a/frontend/test/frontend_tests/runner.cljs b/frontend/test/frontend_tests/runner.cljs index e1e0bab50a..6448015968 100644 --- a/frontend/test/frontend_tests/runner.cljs +++ b/frontend/test/frontend_tests/runner.cljs @@ -149,6 +149,7 @@ [frontend-tests.ui.drawarea-test] [frontend-tests.ui.ds-controls-numeric-input-test] [frontend-tests.ui.error-boundary-test] + [frontend-tests.ui.export-preview-test] [frontend-tests.ui.file-restored-modal-test] [frontend-tests.ui.gradient-handlers-test] [frontend-tests.ui.history-test] @@ -353,6 +354,7 @@ 'frontend-tests.ui.drawarea-test 'frontend-tests.ui.ds-controls-numeric-input-test 'frontend-tests.ui.error-boundary-test + 'frontend-tests.ui.export-preview-test 'frontend-tests.ui.file-restored-modal-test 'frontend-tests.ui.gradient-handlers-test 'frontend-tests.ui.history-test diff --git a/frontend/test/frontend_tests/ui/export_preview_test.cljs b/frontend/test/frontend_tests/ui/export_preview_test.cljs new file mode 100644 index 0000000000..3fdfe65652 --- /dev/null +++ b/frontend/test/frontend_tests/ui/export_preview_test.cljs @@ -0,0 +1,296 @@ +;; 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.ui.export-preview-test + (:require + ["jsdom" :refer [JSDOM]] + ["react" :as react] + ["react-dom/client" :as rdc] + ["react-dom/server" :as rds] + [app.common.types.shape :as cts] + [app.common.uuid :as uuid] + [app.main.data.exports.wasm :as wasm.exports] + [app.main.store :as st] + [app.main.ui.exports.preview :as preview] + [app.main.ui.hooks :as hooks] + [app.main.ui.inspect.exports :as inspect-exports] + [app.main.ui.workspace.sidebar.options.menus.exports :as exports] + [app.render-wasm.api :as wasm.api] + [app.util.globals :as globals] + [app.util.storage :as storage] + [app.util.webapi :as wapi] + [cljs.test :as t :include-macros true] + [cuerdas.core :as str] + [frontend-tests.helpers.mock :as mock] + [rumext.v2 :as mf])) + +(defn- render-menu + [shapes presets] + (with-redefs [hooks/use-portal-container (fn ([] nil) ([_] nil))] + (rds/renderToStaticMarkup + (mf/element exports/exports-menu* + #js {:ids (mapv :id shapes) + :shapes shapes + :type (if (= 1 (count shapes)) :rect :multiple) + :values {:exports presets} + :fileId (uuid/next) + :pageId (uuid/next)})))) + +(defn- ^:async with-mounted-menu + [presets test-fn] + (let [dom (JSDOM. "
") + window (.-window dom) + document (.-document window) + old-window (.-window js/globalThis) + old-document (.-document js/globalThis) + old-act (.-IS_REACT_ACT_ENVIRONMENT js/globalThis) + old-state @st/state + file-id (uuid/next) + page-id (uuid/next) + shape (cts/setup-shape {:type :rect :width 100 :height 50 :exports presets}) + container (.getElementById document "root")] + (set! (.-window js/globalThis) window) + (set! (.-document js/globalThis) document) + (set! (.-IS_REACT_ACT_ENVIRONMENT js/globalThis) true) + (reset! st/state {:current-file-id file-id :current-page-id page-id + :files {file-id {:data {:pages-index {page-id {:objects {(:id shape) shape}}}}}} + :viewer {:pages {page-id {:objects {(:id shape) shape}}}}}) + (try + (await + (mock/with-mocks* {globals/document document globals/window window + storage/user (storage/create-storage nil "export-preview-test")} + (let [root (rdc/createRoot container) + render-selection + (fn [section] + (.render root + (case section + :design (mf/element exports/exports-menu* + #js {:ids [(:id shape)] :shapes [shape] :type :rect + :values {:exports presets} :fileId file-id :pageId page-id}) + :preview (mf/element preview/export-preview* + #js {:objectId (:id shape) :exports presets}) + (:inspect :viewer) (mf/html + [:& inspect-exports/exports + {:shapes [shape] :type :rect :file-id file-id :page-id page-id + :from (if (= section :viewer) :viewer :workspace)}]) + nil)))] + (try + (await (react/act #(render-selection :design))) + (await (test-fn container render-selection)) + (finally + (await (react/act #(.unmount root)))))))) + (finally + (reset! st/state old-state) + (.close window) + (set! (.-window js/globalThis) old-window) + (set! (.-document js/globalThis) old-document) + (set! (.-IS_REACT_ACT_ENVIRONMENT js/globalThis) old-act))))) + +(t/deftest ^:async reopening-export-keeps-the-last-preview-expansion + (await + (with-mounted-menu + [{:type :png :scale 1 :suffix ""}] + (^:async fn [container _render-selection] + (let [preview-button #(.querySelector container "button[aria-controls^='export-preview-']") + export-button (.querySelector container "button")] + (await (react/act #(.click (preview-button)))) + (t/is (= "true" (.getAttribute (preview-button) "aria-expanded"))) + (await (react/act #(.click export-button))) + (t/is (nil? (preview-button))) + (await (react/act #(.click export-button))) + (t/is (= "true" (.getAttribute (preview-button) "aria-expanded"))) + (await (react/act #(.click (preview-button)))) + (await (react/act #(.click export-button))) + (await (react/act #(.click export-button))) + (t/is (= "false" (.getAttribute (preview-button) "aria-expanded")))))))) + +(t/deftest ^:async preview-preset-uses-the-penpot-select + (await + (with-mounted-menu + [{:type :png :scale 1 :suffix ""} {:type :pdf :scale 1 :suffix ""}] + (^:async fn [container _render-selection] + (await (react/act #(.click (.querySelector container "button[aria-controls^='export-preview-']")))) + (let [panel (.querySelector container "div[id^='export-preview-']") + select (.querySelector panel "button[role='combobox']")] + (t/is (some? select)) + (t/is (nil? (.querySelector panel "select"))) + (when select + (await (react/act #(.click select))) + (let [option (aget (.querySelectorAll panel "[role='option']") 1)] + (await (react/act #(.click option))) + (t/is (str/includes? (.-textContent panel) "100 × 50 · PDF"))))))))) + +(t/deftest ^:async reselecting-a-layer-keeps-the-preview-expansion + (await + (with-mounted-menu + [{:type :png :scale 1 :suffix ""}] + (^:async fn [container render-selection] + (let [preview-button #(.querySelector container "button[aria-controls^='export-preview-']")] + (await (react/act #(.click (preview-button)))) + (await (react/act #(render-selection nil))) + (t/is (nil? (preview-button))) + (await (react/act #(render-selection :design))) + (t/is (= "true" (.getAttribute (preview-button) "aria-expanded"))) + (await (react/act #(.click (preview-button)))) + (await (react/act #(render-selection nil))) + (await (react/act #(render-selection :design))) + (t/is (= "false" (.getAttribute (preview-button) "aria-expanded")))))))) + +(t/deftest ^:async inspect-shows-the-preview-and-keeps-its-expansion-between-tabs + (await + (with-mounted-menu + [{:type :png :scale 1 :suffix ""}] + (^:async fn [container render-selection] + (let [preview-button #(.querySelector container "button[aria-controls^='export-preview-']")] + (await (react/act #(.click (preview-button)))) + (await (react/act #(render-selection :inspect))) + (t/is (some? (preview-button))) + (when (preview-button) + (t/is (= "true" (.getAttribute (preview-button) "aria-expanded"))) + (t/is (some? (.querySelector container "svg[id^='screenshot-']"))) + (await (react/act #(.click (preview-button)))) + (await (react/act #(render-selection :design))) + (t/is (= "false" (.getAttribute (preview-button) "aria-expanded"))))))))) + +(t/deftest ^:async viewer-inspect-previews-the-viewer-page-without-workspace-objects + (await + (with-mounted-menu + [{:type :png :scale 1 :suffix ""}] + (^:async fn [container render-selection] + (await (react/act #(swap! st/state assoc :files {} :features #{"render-wasm/v1"}))) + (await (react/act #(render-selection :viewer))) + (let [button (.querySelector container "button[aria-controls^='export-preview-']")] + (t/is (some? button)) + (when button + (await (react/act #(.click button))) + (t/is (some? (.querySelector container "svg[id^='screenshot-']"))) + (t/is (str/includes? (.-textContent container) "100 × 50 · PNG")))))))) + +(t/deftest single-shape-with-export-offers-a-preview + (let [presets [{:type :png :scale 1 :suffix ""}] + shape (cts/setup-shape {:type :rect :exports presets}) + markup (render-menu [shape] presets)] + (t/is (str/includes? markup "aria-controls=\"export-preview-")) + (t/is (str/includes? markup "aria-expanded=\"false\"")))) + +(t/deftest preview-requires-an-export-preset + (let [shape (cts/setup-shape {:type :rect})] + (t/is (not (str/includes? (render-menu [shape] []) + "aria-controls=\"export-preview-"))))) + +(t/deftest multiple-selection-does-not-imply-a-combined-export-preview + (let [presets [{:type :png :scale 1 :suffix ""}] + shapes [(cts/setup-shape {:type :rect :exports presets}) + (cts/setup-shape {:type :rect :exports presets})]] + (t/is (not (str/includes? (render-menu shapes presets) + "aria-controls=\"export-preview-"))))) + +(t/deftest preview-keeps-the-export-size-while-bounding-the-render-size + (doseq [type [:png :jpeg :webp]] + (let [settings (preview/preview-settings {:width 1200 :height 800} + {:type type :scale 4})] + (t/is (= {:type type :scale 0.2 :width 4800 :height 3200} settings))))) + +(t/deftest preview-respects-small-raster-export-scales + (t/is (= {:type :png :scale 0.5 :width 50 :height 25} + (preview/preview-settings {:width 100 :height 50} {:type :png :scale 0.5})))) + +(t/deftest vector-previews-ignore-a-raster-preset-scale + (doseq [type [:svg :pdf]] + (t/is (= {:type :png :scale 1 :width 100 :height 50} + (preview/preview-settings {:width 100 :height 50} {:type type :scale 4}))))) + +(t/deftest empty-export-bounds-do-not-create-an-infinite-render-scale + (t/is (= {:type :png :scale 1 :width 0 :height 0} + (preview/preview-settings {:width 0 :height 0} {:type :png :scale 1})))) + +(t/deftest classic-preview-matches-the-rounded-svg-export-dimensions + (let [shape (cts/setup-shape {:type :frame :x 282 :y 300 + :width 400.000015 :height 260.000015}) + bounds (preview/preview-bounds {(:id shape) shape} shape false) + settings (preview/preview-settings bounds {:type :png :scale 1})] + (t/is (= 400 (:width settings))) + (t/is (= 260 (:height settings))))) + +(t/deftest wasm-preview-uses-native-bounds-without-rounding-away-real-fractions + (let [shape (cts/setup-shape {:type :frame :width 400 :height 260})] + (with-redefs [wasm.api/initialized? (constantly true) + wasm.api/get-shape-extrect (fn [_] {:width 400.5 :height 260.25})] + (let [bounds (preview/preview-bounds {(:id shape) shape} shape true) + settings (preview/preview-settings bounds {:type :png :scale 2})] + (t/is (= 801 (:width settings))) + (t/is (= 521 (:height settings))))))) + + +(defn- wait-for-preview + [] + (react/act #(js/Promise. (fn [resolve] (js/setTimeout resolve 250))))) + +(defn- ^:async with-native-preview + [test-fn] + (let [next-uri* (atom 0) + revoked* (atom [])] + (await + (mock/with-mocks* {wasm.api/initialized? (constantly true) + wasm.api/get-shape-extrect (fn [_] (hash-map :width 100 :height 50)) + wasm.exports/export-image-uri (fn [_] (str "blob:preview-" (swap! next-uri* inc))) + wapi/revoke-uri #(swap! revoked* conj %)} + (await + (with-mounted-menu + [{:type :png :scale 1 :suffix ""}] + (^:async fn [container render-selection] + (await (react/act #(swap! st/state assoc :features #{"render-wasm/v1"}))) + (await (react/act #(render-selection :preview))) + (await (react/act #(.click (.querySelector container "button[aria-controls^='export-preview-']")))) + (await (wait-for-preview)) + (await (test-fn container render-selection revoked*))))))))) + +(t/deftest ^:async native-preview-keeps-the-image-until-the-refresh-is-ready + (await + (with-native-preview + (^:async fn [container render-selection revoked*] + (let [image-src #(.getAttribute (.querySelector container "img") "src") + original (image-src) + file-id (:current-file-id @st/state) + page-id (:current-page-id @st/state) + path [:files file-id :data :pages-index page-id :objects] + shape-id (-> (get-in @st/state path) keys first)] + (await (react/act #(swap! st/state update-in (conj path shape-id :x) inc))) + (t/is (some? (.querySelector container "img"))) + (t/is (empty? @revoked*)) + (when (.querySelector container "img") + (t/is (= original (image-src)))) + (await (wait-for-preview)) + (t/is (not= original (image-src))) + (t/is (= [original] @revoked*)) + (let [replacement (image-src)] + (await (react/act #(render-selection nil))) + (t/is (= [original replacement] @revoked*)))))))) + +(t/deftest ^:async unrelated-parent-renders-keep-the-native-preview + (await + (with-native-preview + (^:async fn [container render-selection revoked*] + (let [original (.getAttribute (.querySelector container "img") "src")] + (await (react/act #(render-selection :preview))) + (await (wait-for-preview)) + (t/is (= original (.getAttribute (.querySelector container "img") "src"))) + (t/is (empty? @revoked*))))))) + +(t/deftest ^:async closing-native-preview-cancels-a-pending-refresh + (await + (with-native-preview + (^:async fn [container _render-selection revoked*] + (let [original (.getAttribute (.querySelector container "img") "src") + file-id (:current-file-id @st/state) + page-id (:current-page-id @st/state) + path [:files file-id :data :pages-index page-id :objects] + shape-id (-> (get-in @st/state path) keys first)] + (await (react/act #(swap! st/state update-in (conj path shape-id :x) inc))) + (await (react/act #(.click (.querySelector container "button[aria-controls^='export-preview-']")))) + (await (wait-for-preview)) + (t/is (nil? (.querySelector container "img"))) + (t/is (= [original] @revoked*))))))) diff --git a/frontend/translations/en.po b/frontend/translations/en.po index 3bfa4d75c7..dffa87786c 100644 --- a/frontend/translations/en.po +++ b/frontend/translations/en.po @@ -7603,6 +7603,22 @@ msgstr[1] "Export %s elements" msgid "workspace.options.export.add-export" msgstr "Add export" +#: src/app/main/ui/exports/preview.cljs:170 +msgid "workspace.options.export.preview" +msgstr "Preview" + +#: src/app/main/ui/exports/preview.cljs:98 +msgid "workspace.options.export.preview-error" +msgstr "Could not load the preview" + +#: src/app/main/ui/exports/preview.cljs:94, src/app/main/ui/exports/preview.cljs:156 +msgid "workspace.options.export.preview-image" +msgstr "Export preview" + +#: src/app/main/ui/exports/preview.cljs:142 +msgid "workspace.options.export.preview-preset" +msgstr "Preview export preset" + #: src/app/main/ui/inspect/exports.cljs:177, src/app/main/ui/inspect/exports.cljs:211, src/app/main/ui/workspace/sidebar/options/menus/exports.cljs:228, src/app/main/ui/workspace/sidebar/options/menus/exports.cljs:263 msgid "workspace.options.export.remove-export" msgstr "Remove export" diff --git a/frontend/translations/es.po b/frontend/translations/es.po index fa83708f7d..a75cb5565a 100644 --- a/frontend/translations/es.po +++ b/frontend/translations/es.po @@ -7678,6 +7678,22 @@ msgstr[1] "Exportar %s elementos" msgid "workspace.options.export.add-export" msgstr "Añadir exportación" +#: src/app/main/ui/exports/preview.cljs:170 +msgid "workspace.options.export.preview" +msgstr "Vista previa" + +#: src/app/main/ui/exports/preview.cljs:98 +msgid "workspace.options.export.preview-error" +msgstr "No se ha podido cargar la vista previa" + +#: src/app/main/ui/exports/preview.cljs:94, src/app/main/ui/exports/preview.cljs:156 +msgid "workspace.options.export.preview-image" +msgstr "Resultado de la exportación" + +#: src/app/main/ui/exports/preview.cljs:142 +msgid "workspace.options.export.preview-preset" +msgstr "Exportación mostrada" + #: src/app/main/ui/inspect/exports.cljs:177, src/app/main/ui/inspect/exports.cljs:211, src/app/main/ui/workspace/sidebar/options/menus/exports.cljs:228, src/app/main/ui/workspace/sidebar/options/menus/exports.cljs:263 msgid "workspace.options.export.remove-export" msgstr "Eliminar exportación"