diff --git a/frontend/src/app/main/data/workspace/fix_deleted_fonts.cljs b/frontend/src/app/main/data/workspace/fix_deleted_fonts.cljs index 0f90250ca0..7a69602045 100644 --- a/frontend/src/app/main/data/workspace/fix_deleted_fonts.cljs +++ b/frontend/src/app/main/data/workspace/fix_deleted_fonts.cljs @@ -65,15 +65,18 @@ [{:keys [objects id]}] (reduce-kv (fn [changes shape-id shape] (if (shape-has-invalid-font-family?? shape) - (conj changes {:type :mod-obj - :id shape-id - :page-id id - :operations [{:type :set - :attr :content - :val (fix-shape-content shape)} - {:type :set - :attr :position-data - :val nil}]}) + (let [fixed-content (fix-shape-content shape)] + (if (not= fixed-content (:content shape)) + (conj changes {:type :mod-obj + :id shape-id + :page-id id + :operations [{:type :set + :attr :content + :val fixed-content} + {:type :set + :attr :position-data + :val nil}]}) + changes)) changes)) [] objects)) @@ -82,8 +85,11 @@ [fdata] (reduce-kv (fn [changes _ typography] (if (has-invalid-font-family? typography) - (conj changes {:type :mod-typography - :typography (fix-typography typography)}) + (let [fixed-typography (fix-typography typography)] + (if (not= fixed-typography typography) + (conj changes {:type :mod-typography + :typography fixed-typography}) + changes)) changes)) [] (:typographies fdata))) diff --git a/frontend/test/frontend_tests/data/fix_deleted_fonts_test.cljs b/frontend/test/frontend_tests/data/fix_deleted_fonts_test.cljs new file mode 100644 index 0000000000..4592c90ab3 --- /dev/null +++ b/frontend/test/frontend_tests/data/fix_deleted_fonts_test.cljs @@ -0,0 +1,75 @@ +;; This Source Code Form is subject to the terms of the Mozilla Public +;; License, v. 2.0. If a copy of the MPL was not distributed with this +;; file, You can obtain one at http://mozilla.org/MPL/2.0/. +;; +;; Copyright (c) KALEIDOS SUBSIDIARY SL + +(ns frontend-tests.data.fix-deleted-fonts-test + (:require + [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.shape :as cts] + [app.common.types.text :as txt] + [app.main.data.changes :as dwc] + [app.main.data.workspace.fix-deleted-fonts :as fdf] + [app.main.fonts :as fonts] + [beicon.v2.core :as rx] + [cljs.test :as t :include-macros true] + [frontend-tests.helpers.state :as ths] + [potok.v2.core :as ptk])) + +(t/use-fixtures :each + {:before cthi/reset-idmap!}) + +(def ^:private missing-font-id + "custom-missing-font-id") + +(defn- text-content-with-font + [text font-id font-family] + (-> (cts/setup-shape {:type :text :x 0 :y 0 :grow-type :auto-width}) + :content + (txt/change-text text :font-id font-id :font-family font-family))) + +(defn- file-with-text-shape + [font-id font-family] + (-> (cthf/sample-file :file1 :page-label :page1) + (cths/add-sample-shape :text + {:type :text + :content (text-content-with-font "TEST" + font-id + font-family)}))) + +(defn- watch-fix-deleted-fonts-events + [file] + (let [events (atom []) + store (ths/setup-store file) + state @store] + (when-let [result (ptk/watch (fdf/fix-deleted-fonts-for-page (:id file) + (cthf/current-page-id file)) + state + (rx/empty))] + (->> result (rx/subs! #(swap! events conj %)))) + @events)) + +(t/deftest fix-deleted-fonts-skips-unfixable-missing-font + (let [file (file-with-text-shape missing-font-id "Gibson") + events (watch-fix-deleted-fonts-events file)] + (t/is (empty? events) + "missing custom fonts with no replacement must not commit changes"))) + +(t/deftest fix-deleted-fonts-remaps-when-replacement-exists + (let [replacement-id "custom-gibson-replacement" + cleanup #(swap! fonts/fontsdb dissoc replacement-id)] + (try + (swap! fonts/fontsdb assoc replacement-id + {:id replacement-id + :family "Gibson" + :name "Gibson" + :variants []}) + (let [file (file-with-text-shape missing-font-id "Gibson") + events (watch-fix-deleted-fonts-events file)] + (t/is (= 1 (count events))) + (t/is (ptk/type? ::dwc/commit-changes (first events)))) + (finally + (cleanup))))) diff --git a/frontend/test/frontend_tests/runner.cljs b/frontend/test/frontend_tests/runner.cljs index adb408ceb4..493dddeb42 100644 --- a/frontend/test/frontend_tests/runner.cljs +++ b/frontend/test/frontend_tests/runner.cljs @@ -12,6 +12,7 @@ [frontend-tests.data.dashboard-test] [frontend-tests.data.event-test] [frontend-tests.data.exports-assets-test] + [frontend-tests.data.fix-deleted-fonts-test] [frontend-tests.data.nitrate-test] [frontend-tests.data.persistence-retry-test] [frontend-tests.data.persistence-test] @@ -150,6 +151,7 @@ 'frontend-tests.data.comments-filters-test 'frontend-tests.data.dashboard-test 'frontend-tests.data.event-test + 'frontend-tests.data.fix-deleted-fonts-test 'frontend-tests.data.nitrate-test 'frontend-tests.data.persistence-retry-test 'frontend-tests.data.persistence-test