2022-06-23 17:43:43 +02:00
|
|
|
;; 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/.
|
|
|
|
;;
|
2022-11-02 18:11:50 +01:00
|
|
|
;; Copyright (c) KALEIDOS INC
|
2022-06-23 17:43:43 +02:00
|
|
|
|
2022-11-08 10:40:19 +01:00
|
|
|
(ns common-tests.helpers.files
|
2022-06-23 17:43:43 +02:00
|
|
|
(:require
|
2024-05-07 12:50:32 +02:00
|
|
|
[app.common.data :as d]
|
2023-10-11 13:39:56 +02:00
|
|
|
[app.common.features :as ffeat]
|
2024-04-18 18:10:00 +02:00
|
|
|
[app.common.files.changes :as cfc]
|
|
|
|
[app.common.files.validate :as cfv]
|
|
|
|
[app.common.pprint :refer [pprint]]
|
2022-10-06 18:47:16 +02:00
|
|
|
[app.common.types.file :as ctf]
|
2024-04-18 18:10:00 +02:00
|
|
|
[app.common.types.page :as ctp]
|
2022-10-06 18:47:16 +02:00
|
|
|
[app.common.types.pages-list :as ctpl]
|
2024-05-07 12:50:32 +02:00
|
|
|
[app.common.uuid :as uuid]
|
|
|
|
[common-tests.helpers.ids-map :as thi]
|
|
|
|
[cuerdas.core :as str]))
|
2022-10-06 18:47:16 +02:00
|
|
|
|
2024-04-18 18:10:00 +02:00
|
|
|
;; ----- Files
|
|
|
|
|
|
|
|
(defn sample-file
|
|
|
|
[label & {:keys [page-label name] :as params}]
|
2022-10-06 18:47:16 +02:00
|
|
|
(binding [ffeat/*current* #{"components/v2"}]
|
2024-04-18 18:10:00 +02:00
|
|
|
(let [params (cond-> params
|
|
|
|
label
|
|
|
|
(assoc :id (thi/new-id! label))
|
2022-06-23 17:43:43 +02:00
|
|
|
|
2024-04-18 18:10:00 +02:00
|
|
|
page-label
|
|
|
|
(assoc :page-id (thi/new-id! page-label))
|
2022-06-23 17:43:43 +02:00
|
|
|
|
2024-04-18 18:10:00 +02:00
|
|
|
(nil? name)
|
|
|
|
(assoc :name "Test file"))
|
2022-06-23 17:43:43 +02:00
|
|
|
|
2024-04-18 18:10:00 +02:00
|
|
|
file (-> (ctf/make-file (dissoc params :page-label))
|
|
|
|
(assoc :features #{"components/v2"}))
|
2022-06-23 17:43:43 +02:00
|
|
|
|
2024-04-18 18:10:00 +02:00
|
|
|
page (-> file
|
|
|
|
:data
|
|
|
|
(ctpl/pages-seq)
|
|
|
|
(first))]
|
|
|
|
|
|
|
|
(with-meta file
|
|
|
|
{:current-page-id (:id page)}))))
|
|
|
|
|
|
|
|
(defn validate-file!
|
|
|
|
([file] (validate-file! file {}))
|
|
|
|
([file libraries]
|
|
|
|
(cfv/validate-file-schema! file)
|
|
|
|
(cfv/validate-file! file libraries)))
|
|
|
|
|
|
|
|
(defn apply-changes
|
|
|
|
[file changes]
|
|
|
|
(let [file' (ctf/update-file-data file #(cfc/process-changes % (:redo-changes changes) true))]
|
|
|
|
(validate-file! file')
|
|
|
|
file'))
|
|
|
|
|
|
|
|
;; ----- Pages
|
|
|
|
|
|
|
|
(defn sample-page
|
|
|
|
[label & {:keys [] :as params}]
|
|
|
|
(ctp/make-empty-page (assoc params :id (thi/new-id! label))))
|
|
|
|
|
|
|
|
(defn add-sample-page
|
|
|
|
[file label & {:keys [] :as params}]
|
|
|
|
(let [page (sample-page label params)]
|
|
|
|
(-> file
|
|
|
|
(ctf/update-file-data #(ctpl/add-page % page))
|
|
|
|
(vary-meta assoc :current-page-id (:id page)))))
|
|
|
|
|
|
|
|
(defn get-page
|
|
|
|
[file label]
|
|
|
|
(ctpl/get-page (:data file) (thi/id label)))
|
|
|
|
|
|
|
|
(defn current-page-id
|
|
|
|
[file]
|
|
|
|
(:current-page-id (meta file)))
|
|
|
|
|
|
|
|
(defn current-page
|
|
|
|
[file]
|
|
|
|
(ctpl/get-page (:data file) (current-page-id file)))
|
|
|
|
|
|
|
|
(defn switch-to-page
|
|
|
|
[file label]
|
|
|
|
(vary-meta file assoc :current-page-id (thi/id label)))
|
|
|
|
|
2024-05-07 12:50:32 +02:00
|
|
|
;; ----- Debug
|
2024-04-29 16:37:21 +02:00
|
|
|
|
2024-05-07 12:50:32 +02:00
|
|
|
(defn dump-file-type
|
|
|
|
"Dump a file using dump-tree function in common.types.file."
|
|
|
|
[file & {:keys [page-label libraries] :as params}]
|
|
|
|
(let [params (-> params
|
|
|
|
(or {:show-ids true :show-touched true})
|
|
|
|
(dissoc page-label libraries))
|
|
|
|
page (if (some? page-label)
|
|
|
|
(:id (get-page file page-label))
|
|
|
|
(current-page-id file))
|
|
|
|
libraries (or libraries {})]
|
2024-04-29 15:15:10 +02:00
|
|
|
|
2024-05-07 12:50:32 +02:00
|
|
|
(ctf/dump-tree file page libraries params)))
|
2024-04-29 15:15:10 +02:00
|
|
|
|
2024-05-07 12:50:32 +02:00
|
|
|
(defn pprint-file
|
|
|
|
"Pretry print a file trying to limit the quantity of info shown."
|
|
|
|
[file & {:keys [level length] :or {level 10 length 1000}}]
|
|
|
|
(pprint file {:level level :length length}))
|
2022-07-01 16:51:49 +02:00
|
|
|
|
2024-05-07 12:50:32 +02:00
|
|
|
(defn dump-shape
|
|
|
|
"Dump a shape, with each attribute in a line."
|
|
|
|
[shape]
|
|
|
|
(println "{")
|
|
|
|
(doseq [[k v] (sort shape)]
|
|
|
|
(when (some? v)
|
|
|
|
(println (str " " k " : " v))))
|
|
|
|
(println "}"))
|
|
|
|
|
|
|
|
(defn- stringify-keys [m keys]
|
|
|
|
(apply str (interpose ", " (map #(str % ": " (get m %)) keys))))
|
|
|
|
|
|
|
|
(defn- dump-page-shape
|
|
|
|
[shape keys padding]
|
|
|
|
(println (str/pad (str padding
|
|
|
|
(when (:main-instance shape) "{")
|
|
|
|
(or (thi/label (:id shape)) "<no-label>")
|
|
|
|
(when (:main-instance shape) "}")
|
|
|
|
(when keys
|
|
|
|
(str " [" (stringify-keys shape keys) "]")))
|
|
|
|
{:length 40 :type :right})
|
|
|
|
(if (nil? (:shape-ref shape))
|
|
|
|
(if (:component-root shape)
|
|
|
|
(str "# [Component " (or (thi/label (:component-id shape)) "<no-label>") "]")
|
|
|
|
"")
|
|
|
|
(str/format "%s--> %s%s"
|
|
|
|
(cond (:component-root shape) "#"
|
|
|
|
(:component-id shape) "@"
|
|
|
|
:else "-")
|
|
|
|
(if (:component-root shape)
|
|
|
|
(str "[Component " (or (thi/label (:component-id shape)) "<no-label>") "] ")
|
|
|
|
"")
|
|
|
|
(or (thi/label (:shape-ref shape)) "<no-label>")))))
|
|
|
|
|
|
|
|
(defn dump-page
|
|
|
|
"Dump the layer tree of the page. Print the label of each shape, and the specified keys."
|
|
|
|
([page keys]
|
|
|
|
(dump-page page uuid/zero "" keys))
|
|
|
|
([page root-id padding keys]
|
|
|
|
(let [lookupf (d/getf (:objects page))
|
|
|
|
root-shape (lookupf root-id)
|
|
|
|
shapes (map lookupf (:shapes root-shape))]
|
|
|
|
(doseq [shape shapes]
|
|
|
|
(dump-page-shape shape keys padding)
|
|
|
|
(dump-page page (:id shape) (str padding " ") keys)))))
|
2022-07-01 16:51:49 +02:00
|
|
|
|
2024-05-07 12:50:32 +02:00
|
|
|
(defn dump-file
|
|
|
|
"Dump the current page of the file, using dump-page above.
|
|
|
|
Example: (thf/dump-file file [:id :touched])"
|
|
|
|
([file] (dump-file file []))
|
|
|
|
([file keys] (dump-page (current-page file) keys)))
|