소스 검색

refactor: remove :block/file from non-page blocks

Tienson Qin 4 년 전
부모
커밋
63b672dcf8

+ 1 - 5
src/main/frontend/components/block.cljs

@@ -2788,11 +2788,7 @@
     (when (seq blocks)
       [:div.blocks-container.flex-1
        {:class (when doc-mode? "document-mode")
-        :style {:margin-left (cond
-                               sidebar?
-                               0
-                               :else
-                               -10)}}
+        :style {:margin-left (if sidebar? 0 -10)}}
        (lazy-blocks config blocks)])))
 
 ;; headers to hiccup

+ 32 - 46
src/main/frontend/db/model.cljs

@@ -183,32 +183,19 @@
                :where
                [?file :file/path ?path]
                [(?pred $ ?path)]
-               [?block :block/file ?file]]
+               [?p :block/file ?file]
+               [?block :block/page ?p]]
              (conn/get-conn repo-url) pred)
         db-utils/seq-flatten)))
 
-(defn get-files-non-page-blocks
-  [repo-url paths]
-  (let [paths (set paths)
-        pred (fn [_db e]
-               (contains? paths e))]
-    (-> (d/q '[:find ?block
-               :in $ ?pred
-               :where
-               [?file :file/path ?path]
-               [(?pred $ ?path)]
-               [?block :block/file ?file]
-               [(missing? $ ?block :block/name)]]
-          (conn/get-conn repo-url) pred)
-        db-utils/seq-flatten)))
-
 (defn get-file-blocks
   [repo-url path]
   (-> (d/q '[:find ?block
              :in $ ?path
              :where
              [?file :file/path ?path]
-             [?block :block/file ?file]]
+             [?p :block/file ?file]
+             [?block :block/page ?p]]
            (conn/get-conn repo-url) path)
       db-utils/seq-flatten))
 
@@ -403,37 +390,32 @@
     (:block/properties page)))
 
 ;; FIXME: alert
-(defn- keep-only-one-file
-  [blocks]
-  (filter (fn [b] (= (:block/file b) (:block/file (first blocks)))) blocks))
-
 (defn sort-by-left
   ([blocks parent]
    (sort-by-left blocks parent true))
   ([blocks parent check?]
-   (let [blocks (keep-only-one-file blocks)]
-     (when check?
-       (when (not= (count blocks) (count (set (map :block/left blocks))))
-         (let [duplicates (->> (map (comp :db/id :block/left) blocks)
-                               frequencies
-                               (filter (fn [[_k v]] (> v 1)))
-                               (map (fn [[k _v]]
-                                      (let [left (db-utils/pull k)]
-                                        {:left left
-                                         :duplicates (->>
-                                                      (filter (fn [block]
-                                                                (= k (:db/id (:block/left block))))
-                                                              blocks)
-                                                      (map #(select-keys % [:db/id :block/level :block/content :block/file])))}))))]
-           (util/pprint duplicates)))
-       (assert (= (count blocks) (count (set (map :block/left blocks)))) "Each block should have a different left node"))
-
-    (let [left->blocks (reduce (fn [acc b] (assoc acc (:db/id (:block/left b)) b)) {} blocks)]
-      (loop [block parent
-             result []]
-        (if-let [next (get left->blocks (:db/id block))]
-          (recur next (conj result next))
-          (vec result)))))))
+   (when check?
+     (when (not= (count blocks) (count (set (map :block/left blocks))))
+       (let [duplicates (->> (map (comp :db/id :block/left) blocks)
+                             frequencies
+                             (filter (fn [[_k v]] (> v 1)))
+                             (map (fn [[k _v]]
+                                    (let [left (db-utils/pull k)]
+                                      {:left left
+                                       :duplicates (->>
+                                                    (filter (fn [block]
+                                                              (= k (:db/id (:block/left block))))
+                                                            blocks)
+                                                    (map #(select-keys % [:db/id :block/level :block/content :block/file])))}))))]
+         (util/pprint duplicates)))
+     (assert (= (count blocks) (count (set (map :block/left blocks)))) "Each block should have a different left node"))
+
+   (let [left->blocks (reduce (fn [acc b] (assoc acc (:db/id (:block/left b)) b)) {} blocks)]
+     (loop [block parent
+            result []]
+       (if-let [next (get left->blocks (:db/id block))]
+         (recur next (conj result next))
+         (vec result))))))
 
 (defn- sort-by-left-recursive
   [form]
@@ -689,6 +671,11 @@
               (db-utils/entity [:block/original-name page-name]))
           :block/file))
 
+(defn get-block-file-path
+  [block]
+  (when-let [page-id (:db/id (:block/page block))]
+    (:file/path (:block/file (db-utils/entity page-id)))))
+
 (defn get-file-page-id
   [file-path]
   (when-let [repo (state/get-current-repo)]
@@ -1256,8 +1243,7 @@
 (defn delete-blocks
   [repo-url files delete-page?]
   (when (seq files)
-    (let [f (if delete-page? get-files-blocks get-files-non-page-blocks)
-          blocks (f repo-url files)]
+    (let [blocks (get-files-blocks repo-url files)]
       (mapv (fn [eid] [:db.fn/retractEntity eid]) blocks))))
 
 (defn delete-files

+ 1 - 1
src/main/frontend/format/block.cljs

@@ -624,7 +624,7 @@
                            :pre-block? true
                            :unordered true}
                           (block-keywordize)))
-                       (select-keys first-block [:block/file :block/format :block/page]))
+                       (select-keys first-block [:block/format :block/page]))
                       blocks)
                      blocks)
             blocks (map (fn [block] (dissoc block :block/anchor)) blocks)]

+ 9 - 21
src/main/frontend/handler/editor.cljs

@@ -542,7 +542,7 @@
         new-m {:block/uuid (db/new-block-id)
                :block/content fst-block-text}
         prev-block (-> (merge (select-keys block [:block/parent :block/left :block/format
-                                                  :block/page :block/file :block/journal?]) new-m)
+                                                  :block/page :block/journal?]) new-m)
                        (wrap-parse-block))
         left-block (db/pull (:db/id (:block/left block)))
         _ (outliner-core/save-node (outliner-core/block current-block))
@@ -575,7 +575,7 @@
         new-m {:block/uuid (db/new-block-id)
                :block/content snd-block-text}
         next-block (-> (merge (select-keys block [:block/parent :block/left :block/format
-                                                  :block/page :block/file :block/journal?]) new-m)
+                                                  :block/page :block/journal?]) new-m)
                        (wrap-parse-block))
         sibling? (when block-self? false)
         {:keys [sibling? blocks]} (profile
@@ -683,7 +683,7 @@
               content (if (seq properties)
                         (property/insert-properties format content properties)
                         content)
-              new-block (-> (select-keys block [:block/page :block/file :block/journal?
+              new-block (-> (select-keys block [:block/page :block/journal?
                                                 :block/journal-day])
                             (assoc :block/content content
                                    :block/format format))
@@ -693,10 +693,7 @@
                                  (:db/id (:block/page new-block))))
               new-block (-> new-block
                             (wrap-parse-block)
-                            (assoc :block/uuid (or custom-uuid (db/new-block-id))))
-              new-block (if-let [db-id (:db/id (:block/file block))]
-                          (assoc new-block :block/file db-id)
-                          new-block)]
+                            (assoc :block/uuid (or custom-uuid (db/new-block-id))))]
           (let [[block-m sibling?] (cond
                                      before?
                                      (let [block (db/pull (:db/id (:block/left block)))
@@ -1558,12 +1555,7 @@
 ;; assets/journals_2021_02_03_1612350230540_0.png
 (defn resolve-relative-path
   [file-path]
-  (if-let [current-file (or (some-> (state/get-edit-block)
-                                    :block/file
-                                    :db/id
-                                    (db/entity)
-                                    :file/path)
-
+  (if-let [current-file (or (db-model/get-block-file-path (state/get-edit-block))
                             ;; fix dummy file path of page
                             (and (util/electron?)
                                  (util/node-path.join
@@ -2095,7 +2087,7 @@
             editing-block))))
 
 (defn- paste-block-tree-at-point-edit-aux
-  [uuid file page exclude-properties format content-update-fn]
+  [uuid page exclude-properties format content-update-fn]
   (fn [block]
     (outliner-core/block
      (let [[new-content new-title]
@@ -2123,8 +2115,7 @@
                             :block/uuid
                             :db/id
                             :block/left
-                            :block/parent
-                            :block/file)
+                            :block/parent)
                     {:block/uuid uuid
                      :block/page (select-keys page [:db/id])
                      :block/format format
@@ -2136,9 +2127,7 @@
                      :block/title new-title
                      :block/path-refs (->> (cons (:db/id page) (:block/path-refs block))
                                            (remove nil?))})]
-       (if file
-         (assoc m :block/file (select-keys file [:db/id]))
-         m)))))
+       m))))
 
 (defn paste-block-vec-tree-at-target
   [tree exclude-properties {:keys [content-update-fn
@@ -2148,7 +2137,6 @@
   (let [repo (state/get-current-repo)
         page (or page-block
                  (:block/page (db/entity (:db/id (state/get-edit-block)))))
-        file (:block/file page)
         [target-block sibling? delete-editing-block? editing-block]
         ((or get-pos-fn get-block-tree-insert-pos-at-point))]
     (when target-block
@@ -2167,7 +2155,7 @@
                      (recur (zip/next (zip/edit
                                        loc
                                        (paste-block-tree-at-point-edit-aux
-                                        uuid file page exclude-properties format content-update-fn)))))))))
+                                        uuid page exclude-properties format content-update-fn)))))))))
             _ (when editing-block
                 (let [editing-block (outliner-core/block editing-block)]
                   (outliner-core/save-node editing-block)))

+ 16 - 15
src/main/frontend/handler/extract.cljs

@@ -72,8 +72,7 @@
                             (swap! ref-pages set/union (set block-ref-pages)))
                           (-> block
                               (dissoc :ref-pages)
-                              (assoc :block/file {:file/path file}
-                                     :block/format format
+                              (assoc :block/format format
                                      :block/page [:block/name (string/lower-case page)]
                                      :block/refs block-ref-pages
                                      :block/path-refs block-path-ref-pages))))
@@ -139,7 +138,6 @@
           pages (remove nil? pages)
           pages (map (fn [page] (assoc page :block/uuid (db/new-block-id))) pages)]
       [pages
-       block-ids
        (remove nil? blocks)])
     (catch js/Error e
       (log/error :exception e))))
@@ -211,20 +209,22 @@
     (-> (p/all (map
                  (fn [{:file/keys [path content]} contents]
                    (when content
-                     ;; TODO: remove `text/scheduled-deadline-dash->star` once migration is done
-                     (let [org? (= "org" (string/lower-case (util/get-file-ext path)))]
-                       (let [content (if org?
-                                       content
-                                       (text/scheduled-deadline-dash->star content))
-                             utf8-content (utf8/encode content)]
-                         (extract-blocks-pages repo-url path content utf8-content)))))
+                     (let [org? (= "org" (string/lower-case (util/get-file-ext path)))
+                           content (if org?
+                                     content
+                                     (text/scheduled-deadline-dash->star content))
+                           utf8-content (utf8/encode content)]
+                       (extract-blocks-pages repo-url path content utf8-content))))
                  files))
         (p/then (fn [result]
                   (let [result (remove empty? result)]
                     (when (seq result)
-                      (let [[pages block-ids blocks] (apply map concat result)
-                            block-ids (remove (fn [b] (or (nil? b)
-                                                         (nil? (:block/uuid b)))) block-ids)
+                      (let [result (util/distinct-by (fn [[pages blocks]]
+                                                       (let [page (first pages)]
+                                                         (:block/name page))) result)
+                            [pages blocks] (apply map concat result)
+                            block-ids (->> (map :block/uuid blocks)
+                                           (remove nil?))
                             pages (with-ref-pages pages blocks)
                             blocks (map (fn [block]
                                           (let [id (:block/uuid block)
@@ -233,8 +233,9 @@
                                      blocks)
                             ;; To prevent "unique constraint" on datascript
                             pages-index (map #(select-keys % [:block/name]) pages)
-                            block-ids-set (set (map (fn [{:block/keys [uuid]}] [:block/uuid uuid]) block-ids))
-                            blocks (map #(remove-illegal-refs % block-ids-set refresh?) blocks)]
+                            block-ids-set (set (map (fn [uuid] [:block/uuid uuid]) block-ids))
+                            blocks (map #(remove-illegal-refs % block-ids-set refresh?) blocks)
+                            block-ids (map (fn [uuid] {:block/uuid uuid}) block-ids)]
                         (apply concat [pages-index pages block-ids blocks])))))))))
 
 (defn extract-all-block-refs

+ 12 - 5
src/main/frontend/handler/file.cljs

@@ -123,6 +123,13 @@
            (update item :block/refs keep-block-ref-f))
       data)))
 
+(defn- page-exists-in-another-file
+  [page file]
+  (when-let [page-name (:block/name page)]
+    (let [current-file (:file/path (db/get-page-file page-name))]
+      (when (not= file current-file)
+       current-file))))
+
 (defn reset-file!
   [repo-url file content]
   (let [electron-local-repo? (and (util/electron?)
@@ -148,8 +155,11 @@
           file-content [{:file/path file}]]
       (p/let [tx (if (contains? config/mldoc-support-formats format)
                    (p/let [delete-blocks (db/delete-file-blocks! repo-url file)
-                           [pages block-ids blocks] (extract-handler/extract-blocks-pages repo-url file content utf8-content)
+                           [pages blocks] (extract-handler/extract-blocks-pages repo-url file content utf8-content)
+                           _ (when-let [current-file (page-exists-in-another-file (first pages) file)]
+                               (p/rejected (str "Page already exists with another file: " current-file)))
                            blocks (remove-non-exists-refs! blocks)
+                           block-ids (map (fn [block] {:block/uuid (:block/uuid block)}) blocks)
                            pages (extract-handler/with-ref-pages pages blocks)]
                      (concat file-content delete-blocks pages block-ids blocks))
                    file-content)]
@@ -190,10 +200,7 @@
                          (restore-config! repo true))
                        (when (= path (config/get-custom-css-path repo))
                          (ui-handler/add-style-if-exists!))
-                       (when re-render-root? (ui-handler/re-render-root!))
-                       ;; (when (and add-history? original-content)
-                       ;;   (history/add-history! repo [[path original-content content]]))
-                       )
+                       (when re-render-root? (ui-handler/re-render-root!)))
                      (fn [error]
                        (println "Write file failed, path: " path ", content: " content)
                        (log/error :write/failed error))))))

+ 6 - 7
src/main/frontend/handler/page.cljs

@@ -47,8 +47,11 @@
 
 (defn get-page-file-path
   ([] (get-page-file-path (state/get-current-page)))
-  ([page-name] (when-let [page (db/entity [:block/name page-name])]
-                 (:file/path (:block/file page)))))
+  ([page-name]
+   (when page-name
+     (let [page-name (string/lower-case page-name)]
+       (when-let [page (db/entity [:block/name page-name])]
+        (:file/path (:block/file page)))))))
 
 (defn- build-title [page]
   (let [original-name (:block/original-name page)]
@@ -155,7 +158,6 @@
                                       (str (name key) ":: " value))
                      :block/format format
                      :block/properties {key value}
-                     :block/file (:block/file page)
                      :block/pre-block? true}]
           (outliner-core/insert-node (outliner-core/block block)
                                      (outliner-core/block page)
@@ -505,10 +507,7 @@
 
 (defn get-page-ref-text
   [page]
-  (let [edit-block-file-path (some-> (state/get-edit-block)
-                                     (get-in [:block/file :db/id])
-                                     db/entity
-                                     :file/path)
+  (let [edit-block-file-path (model/get-block-file-path (state/get-edit-block))
         page-name (string/lower-case page)]
     (if (and edit-block-file-path
              (state/org-mode-file-link? (state/get-current-repo)))

+ 3 - 9
src/main/frontend/modules/file/core.cljs

@@ -134,15 +134,9 @@
       (let [file-path (config/get-file-path repo path)
             page-blocks (db/get-page-blocks-no-cache (:block/name page))
             file {:file/path file-path}
-            tx (->>
-                (concat
-                 [{:file/path file-path}
-                  {:block/name (:block/name page)
-                   :block/file file}]
-                 (map (fn [block] {:db/id (:db/id block)
-                                  :block/file file})
-                   page-blocks))
-                (remove nil?))]
+            tx [{:file/path file-path}
+                {:block/name (:block/name page)
+                 :block/file file}]]
         (db/transact! tx)
         (when ok-handler (ok-handler))))))
 

+ 20 - 19
src/main/frontend/modules/outliner/core.cljs

@@ -138,7 +138,7 @@
             "db should be satisfied outliner-tx-state?")
     (let [this (block (update-block-unordered (:data this)))
           m (-> (:data this)
-                (dissoc :block/children :block/meta)
+                (dissoc :block/children :block/meta :block/top? :block/bottom?)
                 (util/remove-nils))
           m (if (state/enable-block-timestamps?) (block-with-timestamps m) m)
           other-tx (:db/other-tx m)
@@ -564,19 +564,19 @@
                      (some-> (tree/-set-left-id last-node-right left-id)
                              (tree/-save txs-state)))))))))))))
 
-(defn- set-nodes-page&file-aux
-  [node page file txs-state]
-  (let [new-node (update node :data assoc :block/page page :block/file file)]
+(defn- set-nodes-page-aux
+  [node page txs-state]
+  (let [new-node (update node :data assoc :block/page page)]
     (tree/-save new-node txs-state)
     (doseq [n (tree/-get-children new-node)]
-      (set-nodes-page&file-aux n page file txs-state))))
+      (set-nodes-page-aux n page txs-state))))
 
-(defn- set-nodes-page&file
+(defn- set-nodes-page
   [node target-node txs-state]
   (let [page (or (get-in target-node [:data :block/page])
                  {:db/id (get-in target-node [:data :db/id])}) ; or page block
-        file (get-in target-node [:data :block/file])]
-    (set-nodes-page&file-aux node page file txs-state)))
+        ]
+    (set-nodes-page-aux node page txs-state)))
 
 (defn move-subtree
   "Move subtree to a destination position in the relation tree.
@@ -596,21 +596,22 @@
                        (= (tree/-get-parent-id root) target-node-id)))
       (let [root-page (:db/id (:block/page (:data root)))
             target-page (:db/id (:block/page (:data target-node)))
+            not-same-page? (not= root-page target-page)
             opts (cond-> {:outliner-op :move-subtree}
-                   (not= root-page target-page)
+                   not-same-page?
                    (assoc :from-page root-page
                           :target-page target-page))]
         (ds/auto-transact!
-        [txs-state (ds/new-outliner-txs-state)] opts
-        (let [left-node-id (tree/-get-left-id root)
-              right-node (tree/-get-right root)]
-          (when (tree/satisfied-inode? right-node)
-            (let [new-right-node (tree/-set-left-id right-node left-node-id)]
-              (tree/-save new-right-node txs-state)))
-          (let [new-root (first (if sibling?
-                                  (insert-node-as-sibling txs-state root target-node)
-                                  (insert-node-as-first-child txs-state root target-node)))]
-            (set-nodes-page&file new-root target-node txs-state))))))))
+         [txs-state (ds/new-outliner-txs-state)] opts
+         (let [left-node-id (tree/-get-left-id root)
+               right-node (tree/-get-right root)]
+           (when (tree/satisfied-inode? right-node)
+             (let [new-right-node (tree/-set-left-id right-node left-node-id)]
+               (tree/-save new-right-node txs-state)))
+           (let [new-root (first (if sibling?
+                                   (insert-node-as-sibling txs-state root target-node)
+                                   (insert-node-as-first-child txs-state root target-node)))]
+             (set-nodes-page new-root target-node txs-state))))))))
 
 (defn get-right-node
   [node]

+ 3 - 3
src/main/frontend/modules/outliner/datascript.cljc

@@ -7,8 +7,8 @@
                      [frontend.modules.editor.undo-redo :as undo-redo]
                      [frontend.state :as state]
                      [frontend.config :as config]
-                     [lambdaisland.glogi :as log])))
-
+                     [lambdaisland.glogi :as log]
+                     [frontend.util :as util])))
 
 #?(:cljs
    (defn new-outliner-txs-state [] (atom [])))
@@ -36,7 +36,7 @@
 #?(:cljs
    (defn transact!
      [txs opts]
-     ;; (util/pprint txs)
+     (util/pprint txs)
      (when (and (seq txs)
                 (not (:skip-transact? opts)))
        (try