🐛 Skip spurious save when missing font cannot be remapped (#12164)

fix-deleted-fonts committed text and typography changes even when no
replacement font existed, so opening a page with a missing custom font
rewrote identical content and cleared position-data on every visit.

Only emit changes when the remapped content or typography actually
differs. Add focused unit coverage for the no-replacement and remapped
paths.

Closes #12151
This commit is contained in:
Alejandro Alonso 2026-10-07 18:03:51 +02:00 committed by GitHub
parent ddce3c2c95
commit 30127e57a0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 94 additions and 11 deletions

View File

@ -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)))

View File

@ -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)))))

View File

@ -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