Browse Source

fix(editor): page blocks duplicated when multiple same page name files

charlie 4 years ago
parent
commit
0fda94a7aa

+ 2 - 0
resources/css/common.css

@@ -38,6 +38,7 @@ html[data-theme=dark] {
   --ls-block-ref-link-text-color: #1a6376;
   --ls-search-background-color: var(--ls-primary-background-color);
   --ls-border-color: #0e5263;
+  --ls-secondary-border-color: #126277;
   --ls-guideline-color: #0b4a5a;
   --ls-menu-hover-color: var(--ls-secondary-background-color);
   --ls-primary-text-color: #a4b5b6;
@@ -90,6 +91,7 @@ html[data-theme=light] {
   --ls-block-ref-link-text-color: #D8E1E8;
   --ls-search-background-color: var(--ls-primary-background-color);
   --ls-border-color: #ccc;
+  --ls-secondary-border-color: #e2e2e2;
   --ls-guideline-color: var(--ls-border-color);
   --ls-menu-hover-color: var(--ls-a-chosen-bg);
   --ls-primary-text-color: #24292e;

+ 34 - 10
src/main/frontend/components/page.cljs

@@ -22,6 +22,7 @@
             [frontend.components.project :as project]
             [frontend.config :as config]
             [frontend.db :as db]
+            [frontend.db.utils :as db-utils]
             [frontend.mixins :as mixins]
             [frontend.db-mixins :as db-mixins]
             [goog.dom :as gdom]
@@ -51,10 +52,22 @@
         (page-handler/add-page-to-recent! repo page-original-name)
         (db/get-page-blocks repo page-name)))))
 
+(rum/defc page-conflict-file-warning
+  [file-path blocks]
+  [:div.conflict-files-warning-it
+   [:a {:title "Go to change the page title"
+        :key file-path
+        :href (rfe/href :file {:path file-path})} file-path]])
+
 (rum/defc page-blocks-cp < rum/reactive
   db-mixins/query
   [repo page file-path page-name page-original-name encoded-page-name sidebar? journal? block? block-id format]
   (let [raw-page-blocks (get-blocks repo page-name page-original-name block? block-id)
+        grouped-blocks-by-file (into {} (for [[k v] (db-utils/group-by-file raw-page-blocks)]
+                                          [(:file/path (db-utils/entity (:db/id k))) v]))
+        raw-page-blocks (if (contains? grouped-blocks-by-file file-path)
+                          (get grouped-blocks-by-file file-path)
+                          raw-page-blocks)
         page-blocks (block-handler/with-dummy-block raw-page-blocks format
                       (if (empty? raw-page-blocks)
                         (let [content (db/get-file repo file-path)]
@@ -74,11 +87,21 @@
                        :editor-box editor/box}
         hiccup-config (common-handler/config-with-document-mode hiccup-config)
         hiccup (block/->hiccup page-blocks hiccup-config {})]
-    (rum/with-key
-      (content/content page-name
-                       {:hiccup hiccup
-                        :sidebar? sidebar?})
-      (str encoded-page-name "-hiccup"))))
+
+    [:div.page-blocks-inner
+
+     ;; more than one file conflict
+     (when (seq grouped-blocks-by-file)
+       [:div.conflict-files-warning-wrap
+        [:h3 "⚠️ Those pages have the same title, you might want to only keep one file: "]
+        (for [[file-path blocks] (into (sorted-map) grouped-blocks-by-file)]
+          (page-conflict-file-warning file-path blocks))])
+
+     (rum/with-key
+       (content/content page-name
+                        {:hiccup   hiccup
+                         :sidebar? sidebar?})
+       (str encoded-page-name "-hiccup"))]))
 
 (defn contents-page
   [{:page/keys [name original-name file] :as contents}]
@@ -311,14 +334,14 @@
                                                       {:title   (t :page/publish)
                                                        :options {:on-click (fn []
                                                                              (page-handler/publish-page!
-                                                                               page-name project/add-project
-                                                                               html-export/export-page))}})
+                                                                              page-name project/add-project
+                                                                              html-export/export-page))}})
                                                     (when-not published?
                                                       {:title   (t :page/publish-as-slide)
                                                        :options {:on-click (fn []
                                                                              (page-handler/publish-page-as-slide!
-                                                                               page-name project/add-project
-                                                                               html-export/export-page))}})
+                                                                              page-name project/add-project
+                                                                              html-export/export-page))}})
                                                     {:title   (t (if public? :page/make-private :page/make-public))
                                                      :options {:background (if public? "gray" "indigo")
                                                                :on-click (fn []
@@ -410,7 +433,8 @@
                   (block/block-parents config repo block-id format)]))
 
              ;; blocks
-             (page-blocks-cp repo page file-path page-name page-original-name encoded-page-name sidebar? journal? block? block-id format)]]
+             [:div.page-blocks-root
+              (page-blocks-cp repo page file-path page-name page-original-name encoded-page-name sidebar? journal? block? block-id format)]]]
 
            (when-not block?
              (today-queries repo today? sidebar?))

+ 41 - 0
src/main/frontend/components/page.css

@@ -2,6 +2,47 @@
   color: var(--ls-title-text-color);
 }
 
+.page-blocks {
+  &-inner {
+    .conflict-files-warning {
+      &-wrap {
+        font-size: 13px;
+        padding: 4px 15px;
+        padding-bottom: 8px;
+        line-height: 1;
+        margin: 5px;
+        margin-bottom: 20px;
+        border-width: 1px;
+        border-style: dotted;
+        border-radius: var(--ls-border-radius-low);
+        border-color: var(--ls-secondary-border-color);
+
+        > h3 {
+          font-weight: bold;
+          padding-top: 8px;
+          padding-bottom: 5px;
+          font-size: 14px;
+          line-height: 1.2em;
+          color: var(--ls-secondary-text-color);
+        }
+      }
+
+      &-it {
+        a {
+          display: flex;
+          align-items: center;
+          padding: 3px 0;
+        }
+
+        svg {
+          display: inline;
+          transform: scale(.6);
+        }
+      }
+    }
+  }
+}
+
 .cp__page {
   &-publish-actions {
     background-color: var(--ls-primary-background-color);

+ 5 - 0
src/main/frontend/db/utils.cljs

@@ -37,6 +37,11 @@
   (some->> blocks
            (group-by :block/page)))
 
+(defn group-by-file
+  [blocks]
+  (some->> blocks
+           (group-by :block/file)))
+
 (defn get-tx-id [tx-report]
   (get-in tx-report [:tempids :db/current-tx]))
 

+ 1 - 1
src/main/frontend/fs/watcher_handler.cljs

@@ -31,7 +31,7 @@
 
         (and (= "change" type)
              (nil? (db/get-file path)))
-        (println "Can't get file in the db: " path)
+        (js/console.warn "Can't get file in the db: " path)
 
         (and (= "change" type)
              (not= content (db/get-file path))