Browse Source

fix: wrong blocks order in presentation mode

Tienson Qin 4 years ago
parent
commit
7de92478b5

+ 0 - 6
src/main/frontend/components/content.cljs

@@ -191,12 +191,6 @@
               :on-click #(srs/make-block-a-card! block-id)}
              "Make a Card"))
 
-          (ui/menu-link
-           {:key "Copy as JSON"
-            :on-click (fn [_e]
-                        (export-handler/copy-block-as-json! block-id))}
-           "Copy as JSON")
-
           (ui/menu-link
            {:key "Cut"
             :on-click (fn [_e]

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

@@ -441,6 +441,47 @@
   (when-let [page (db-utils/entity [:block/name page])]
     (: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]
+  (let [blocks (keep-only-one-file blocks)]
+    (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]
+  (walk/postwalk (fn [f]
+                   (if (and (map? f)
+                            (:block/_parent f))
+                     (let [children (:block/_parent f)]
+                       (-> f
+                           (dissoc :block/_parent)
+                           (assoc :block/children (sort-by-left children f))))
+                     f))
+                 form))
+
 (defn get-page-blocks
   ([page]
    (get-page-blocks (state/get-current-repo) page nil))
@@ -450,8 +491,9 @@
                    :or {use-cache? true
                         pull-keys '[*]}}]
    (let [page (string/lower-case page)
-         page-id (or (:db/id (db-utils/entity repo-url [:block/name page]))
-                     (:db/id (db-utils/entity repo-url [:block/original-name page])))
+         page-entity (or (db-utils/entity repo-url [:block/name page])
+                         (db-utils/entity repo-url [:block/original-name page]))
+         page-id (:db/id page-entity)
          db (conn/get-conn repo-url)]
      (when page-id
        (some->
@@ -463,7 +505,8 @@
                                     block-eids (mapv :e datoms)]
                                 (db-utils/pull-many repo-url pull-keys block-eids)))}
                  nil)
-        react)))))
+        react
+        (sort-by-left page-entity))))))
 
 (defn get-page-blocks-no-cache
   ([page]
@@ -569,47 +612,6 @@
             rules)
            (apply concat)))))
 
-;; 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]
-  (let [blocks (keep-only-one-file blocks)]
-    (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]
-  (walk/postwalk (fn [f]
-                   (if (and (map? f)
-                            (:block/_parent f))
-                     (let [children (:block/_parent f)]
-                       (-> f
-                           (dissoc :block/_parent)
-                           (assoc :block/children (sort-by-left children f))))
-                     f))
-                 form))
-
 (defn get-block-immediate-children
   "Doesn't include nested children."
   [repo block-uuid]

+ 0 - 17
src/main/frontend/handler/export.cljs

@@ -67,23 +67,6 @@
    (outliner-tree/blocks->vec-tree (str (:block/uuid block)))
    (outliner-file/tree->file-content {:init-level 1})))
 
-(defn copy-block-as-json!
-  [block-id]
-  (when-let [repo (state/get-current-repo)]
-    (let [block-children (db/get-block-and-children repo block-id)]
-      (util/copy-to-clipboard! (js/JSON.stringify (bean/->js block-children))))))
-
-(defn copy-page-as-json!
-  [page-name]
-  (when-let [repo (state/get-current-repo)]
-    (let [properties (db/get-page-properties page-name)
-          blocks (db/get-page-blocks repo page-name)]
-      (util/copy-to-clipboard!
-       (js/JSON.stringify
-        (bean/->js
-         {:properties properties
-          :blocks blocks}))))))
-
 (defn export-repo-as-json!
   [repo]
   (when-let [db (db/get-conn repo)]