Add workspace initialization fix for broken shape references

Is the code that executes at workspace initialization that checks all
the shape children for broken references and proceed to emit a special
event that fixes the shape children references.
This commit is contained in:
Andrey Antukh 2023-08-01 17:37:55 +02:00
parent 2ec5a3ba6a
commit 7efeeec9b1
4 changed files with 71 additions and 8 deletions

View file

@ -44,6 +44,7 @@
[app.main.data.workspace.drawing.common :as dwdc]
[app.main.data.workspace.edition :as dwe]
[app.main.data.workspace.fix-bool-contents :as fbc]
[app.main.data.workspace.fix-broken-shape-links :as fbs]
[app.main.data.workspace.fix-deleted-fonts :as fdf]
[app.main.data.workspace.groups :as dwg]
[app.main.data.workspace.guides :as dwgu]
@ -130,8 +131,10 @@
has-graphics? (-> file :media seq)
components-v2 (features/active-feature? state :components-v2)]
(rx/merge
(rx/of (fbc/fix-bool-contents))
(rx/of (fdf/fix-deleted-fonts))
(rx/of (fbc/fix-bool-contents)
(fdf/fix-deleted-fonts)
(fbs/fix-broken-shapes))
(if (and has-graphics? components-v2)
(rx/of (remove-graphics (:id file) (:name file)))
(rx/empty)))))))

View file

@ -0,0 +1,38 @@
;; 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 INC
(ns app.main.data.workspace.fix-broken-shape-links
(:require
[app.main.data.workspace.changes :as dch]
[beicon.core :as rx]
[potok.core :as ptk]))
(defn- generate-changes
[attr {:keys [objects id]}]
(let [base {:type :fix-obj attr id}
contains? (partial contains? objects)
xform (comp
(remove #(every? contains? (:shapes %)))
(map #(assoc base :id (:id %))))]
(sequence xform (vals objects))))
(defn fix-broken-shapes
[]
(ptk/reify ::fix-broken-shape-links
ptk/WatchEvent
(watch [it state _]
(let [data (get state :workspace-data)
changes (concat
(mapcat (partial generate-changes :page-id)
(vals (:pages-index data)))
(mapcat (partial generate-changes :component-id)
(vals (:components data))))]
(rx/of (dch/commit-changes
{:origin it
:redo-changes (vec changes)
:undo-changes []
:save-undo? false}))))))