From e1954b5dd77232ce1afcb75673143d464ee2502b Mon Sep 17 00:00:00 2001 From: Alejandro Alonso Date: Wed, 16 Aug 2023 09:49:07 +0200 Subject: [PATCH] :bug: Fix old files with invalid refs for texts and fills --- common/src/app/common/pages/common.cljc | 2 +- common/src/app/common/pages/migrations.cljc | 36 +++++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/common/src/app/common/pages/common.cljc b/common/src/app/common/pages/common.cljc index e0443616b4..5aa655067b 100644 --- a/common/src/app/common/pages/common.cljc +++ b/common/src/app/common/pages/common.cljc @@ -12,7 +12,7 @@ [app.common.schema :as sm] [app.common.uuid :as uuid])) -(def file-version 21) +(def file-version 22) (def default-color clr/gray-20) (def root uuid/zero) diff --git a/common/src/app/common/pages/migrations.cljc b/common/src/app/common/pages/migrations.cljc index c9e1e80fb1..14492b32e8 100644 --- a/common/src/app/common/pages/migrations.cljc +++ b/common/src/app/common/pages/migrations.cljc @@ -16,6 +16,7 @@ [app.common.math :as mth] [app.common.pages :as cp] [app.common.pages.helpers :as cph] + [app.common.text :as txt] [app.common.types.shape :as cts] [app.common.uuid :as uuid] [cuerdas.core :as str])) @@ -465,3 +466,38 @@ ;; TODO: pending to do a migration for delete already not used fill ;; and stroke props. This should be done for >1.14.x version. + +(defmethod migrate 22 + [data] + (letfn [(valid-ref? [ref] + (or (uuid? ref) + (nil? ref))) + + (valid-node? [node] + (and (valid-ref? (:typography-ref-file node)) + (valid-ref? (:typography-ref-id node)) + (valid-ref? (:fill-color-ref-file node)) + (valid-ref? (:fill-color-ref-id node)))) + + (fix-ref [ref] + (if (valid-ref? ref) ref nil)) + + (fix-node [node] + (-> node + (d/update-when :typography-ref-file fix-ref) + (d/update-when :typography-ref-id fix-ref) + (d/update-when :fill-color-ref-file fix-ref) + (d/update-when :fill-color-ref-id fix-ref))) + + (update-object [object] + (let [invalid-node? (complement valid-node?)] + (cond-> object + (cph/text-shape? object) + (update :content #(txt/transform-nodes invalid-node? fix-node %))))) + + (update-container [container] + (update container :objects update-vals update-object))] + + (-> data + (update :pages-index update-vals update-container) + (update :components update-vals update-container))))