diff --git a/deps/graph-parser/src/logseq/graph_parser.cljs b/deps/graph-parser/src/logseq/graph_parser.cljs index 3edb1afb9..28d38b6c1 100644 --- a/deps/graph-parser/src/logseq/graph_parser.cljs +++ b/deps/graph-parser/src/logseq/graph_parser.cljs @@ -23,35 +23,41 @@ :as options}] (db-set-file-content! conn file content) (let [format (gp-util/get-format file) - file-content [{:file/path file}] - {:keys [tx ast]} - (if (contains? gp-config/mldoc-support-formats format) - (let [extract-options' (merge {:block-pattern (gp-config/get-block-pattern format) - :date-formatter "MMM do, yyyy" - :supported-formats (gp-config/supported-formats)} - extract-options - {:db @conn}) - {:keys [pages blocks ast]} - (extract/extract file content extract-options') - delete-blocks (delete-blocks-fn (first pages) file) - block-ids (map (fn [block] {:block/uuid (:block/uuid block)}) blocks) - block-refs-ids (->> (mapcat :block/refs blocks) - (filter (fn [ref] (and (vector? ref) - (= :block/uuid (first ref))))) - (map (fn [ref] {:block/uuid (second ref)})) - (seq)) - ;; To prevent "unique constraint" on datascript - block-ids (set/union (set block-ids) (set block-refs-ids)) - pages (extract/with-ref-pages pages blocks) - pages-index (map #(select-keys % [:block/name]) pages)] - ;; does order matter? - {:tx (concat file-content pages-index delete-blocks pages block-ids blocks) - :ast ast}) - {:tx file-content}) - tx (concat tx [(cond-> {:file/path file} - new? - ;; TODO: use file system timestamp? - (assoc :file/created-at (date-time-util/time-ms)))])] + file-content [(cond-> {:file/path file} + new? + ;; TODO: use file system timestamp? + (assoc :file/created-at (date-time-util/time-ms)))] + extract-options' (merge {:block-pattern (gp-config/get-block-pattern format) + :date-formatter "MMM do, yyyy" + :supported-formats (gp-config/supported-formats)} + extract-options + {:db @conn}) + {:keys [pages blocks ast] + :or {pages [] + blocks [] + ast []}} + (cond (contains? gp-config/mldoc-support-formats format) + (extract/extract file content extract-options') + + ;; File can be saved as plain edn (mostly, the whiteboard files) + (= format :edn) + (gp-util/safe-read-string content) + + :else nil) + + delete-blocks (delete-blocks-fn (first pages) file) + block-ids (map (fn [block] {:block/uuid (:block/uuid block)}) blocks) + block-refs-ids (->> (mapcat :block/refs blocks) + (filter (fn [ref] (and (vector? ref) + (= :block/uuid (first ref))))) + (map (fn [ref] {:block/uuid (second ref)})) + (seq)) + ;; To prevent "unique constraint" on datascript + block-ids (set/union (set block-ids) (set block-refs-ids)) + pages (extract/with-ref-pages pages blocks) + pages-index (map #(select-keys % [:block/name]) pages) + + tx (concat file-content pages-index delete-blocks pages block-ids blocks)] {:tx (d/transact! conn (gp-util/remove-nils tx) (select-keys options [:new-graph? :from-disk?])) :ast ast})) diff --git a/deps/graph-parser/src/logseq/graph_parser/util.cljs b/deps/graph-parser/src/logseq/graph_parser/util.cljs index 8fc1a56d0..139e72317 100644 --- a/deps/graph-parser/src/logseq/graph_parser/util.cljs +++ b/deps/graph-parser/src/logseq/graph_parser/util.cljs @@ -2,7 +2,9 @@ "Util fns shared between graph-parser and rest of app. Util fns only rely on clojure standard libraries." (:require [clojure.walk :as walk] - [clojure.string :as string])) + [clojure.string :as string] + [logseq.graph-parser.log :as log] + [clojure.reader :as reader])) (defn safe-re-find "Copy of frontend.util/safe-re-find. Too basic to couple to main app" @@ -153,3 +155,11 @@ [file] (when file (normalize-format (keyword (string/lower-case (last (string/split file #"\."))))))) + +(defn safe-read-string + [content] + (try + (reader/read-string content) + (catch :default e + (log/error :parse/read-string-failed e) + {}))) diff --git a/src/main/frontend/modules/file/core.cljs b/src/main/frontend/modules/file/core.cljs index cee86762d..44566b25f 100644 --- a/src/main/frontend/modules/file/core.cljs +++ b/src/main/frontend/modules/file/core.cljs @@ -153,7 +153,7 @@ files [[file-path new-content]]] (push-to-write-chan files))) -(defn save-tree +(defn save-tree! [page-block tree] {:pre [(map? page-block)]} (let [ok-handler #(save-tree-aux! page-block tree) diff --git a/src/main/frontend/modules/outliner/file.cljs b/src/main/frontend/modules/outliner/file.cljs index 1fefd00de..02dff5f23 100644 --- a/src/main/frontend/modules/outliner/file.cljs +++ b/src/main/frontend/modules/outliner/file.cljs @@ -31,7 +31,7 @@ (nil? (:block/file page-block))) (let [tree (tree/blocks->vec-tree repo blocks (:block/name page-block))] (if page-block - (file/save-tree page-block tree) + (file/save-tree! page-block tree) (js/console.error (str "can't find page id: " page-db-id))))))) (defn write-files! diff --git a/src/main/frontend/modules/outliner/whiteboard.cljs b/src/main/frontend/modules/outliner/whiteboard.cljs new file mode 100644 index 000000000..a6243dad8 --- /dev/null +++ b/src/main/frontend/modules/outliner/whiteboard.cljs @@ -0,0 +1 @@ +(ns frontend.modules.outliner.whiteboard) diff --git a/src/test/frontend/modules/file/core_test.cljs b/src/test/frontend/modules/file/core_test.cljs new file mode 100644 index 000000000..d3b77f8a6 --- /dev/null +++ b/src/test/frontend/modules/file/core_test.cljs @@ -0,0 +1,12 @@ +(ns frontend.modules.file.core-test + (:require [cljs.test :refer [use-fixtures] :as test] + [frontend.test.fixtures :as fixtures] + [frontend.test.helper :as helper])) + + +(def test-db helper/test-db) + +(use-fixtures :each + fixtures/load-test-env + fixtures/react-components + fixtures/reset-db)