Bläddra i källkod

feat: add a created-at slider for graph view

Gabriel Horner 2 år sedan
förälder
incheckning
658918b2b9

+ 21 - 0
src/main/frontend/components/page.cljs

@@ -789,6 +789,7 @@
 (defonce *builtin-pages? (atom nil))
 (defonce *excluded-pages? (atom true))
 (defonce *show-journals-in-page-graph? (atom nil))
+(defonce *created-at-filter (atom nil))
 
 (rum/defc ^:large-vars/cleanup-todo graph-filters < rum/reactive
   [graph settings n-hops]
@@ -802,6 +803,7 @@
         orphan-pages? (if (nil? orphan-pages?') orphan-pages? orphan-pages?')
         builtin-pages? (if (nil? builtin-pages?') builtin-pages? builtin-pages?')
         excluded-pages? (if (nil? excluded-pages?') excluded-pages? excluded-pages?')
+        created-at-filter (or (rum/react *created-at-filter) (:created-at-filter settings))
         set-setting! (fn [key value]
                        (let [new-settings (assoc settings key value)]
                          (config-handler/set-config! :graph/settings new-settings)))
@@ -876,6 +878,21 @@
                                (reset! *excluded-pages? value)
                                (set-setting! :excluded-pages? value)))
                            true)]]
+              (when (config/db-based-graph? (state/get-current-repo))
+               [:div.flex.flex-col.mb-2
+                [:p "Created before"]
+                (when created-at-filter
+                  [:div (.toDateString (js/Date. (+ created-at-filter (get-in graph [:all-pages :created-at-min]))))])
+                (ui/tippy {:html [:div.pr-3 (str (js/Date. (+ created-at-filter (get-in graph [:all-pages :created-at-min]))))]}
+                          ;; Slider keeps track off the range from min created-at to max created-at
+                          ;; because there were bugs with setting min and max directly
+                          (ui/slider created-at-filter
+                                     {:min 0
+                                      :max (- (get-in graph [:all-pages :created-at-max])
+                                              (get-in graph [:all-pages :created-at-min]))
+                                      :on-change #(do
+                                                    (reset! *created-at-filter (int %))
+                                                    (set-setting! :created-at-filter (int %)))}))])
               (when (seq focus-nodes)
                 [:div.flex.flex-col.mb-2
                  [:p {:title "N hops from selected nodes"}
@@ -890,6 +907,8 @@
                                                       (swap! *graph-reset? not)
                                                       (reset! *focus-nodes [])
                                                       (reset! *n-hops nil)
+                                                      (reset! *created-at-filter nil)
+                                                      (set-setting! :created-at-filter nil)
                                                       (state/clear-search-filters!))}
                "Reset Graph"]]]))
          {})
@@ -994,6 +1013,8 @@
   [state]
   (let [settings (state/graph-settings)
         theme (state/sub :ui/theme)
+        ;; Needed for query to retrigger after reset
+        _reset? (rum/react *graph-reset?)
         graph (graph-handler/build-global-graph theme settings)
         search-graph-filters (state/sub :search/graph-filters)
         graph (update graph :nodes #(filter-graph-nodes % search-graph-filters))]

+ 1 - 1
src/main/frontend/db/rtc/db_listener.cljs

@@ -43,7 +43,7 @@
              [_e _a _v _t _add6?] :block/type
              [_e _a _v _t _add7?] :block/schema
              [_e _a _v _t _add8?] :block/content} attr->datom
-            _ (prn ::x attr->datom)
+            ;; _ (prn ::x attr->datom)
             ops (cond
                   (and (not add1?) block-uuid
                        (not add2?) (contains? updated-key-set :block/name))

+ 21 - 13
src/main/frontend/handler/graph.cljs

@@ -84,7 +84,7 @@
      :links links}))
 
 (defn build-global-graph
-  [theme {:keys [journal? orphan-pages? builtin-pages? excluded-pages?]}]
+  [theme {:keys [journal? orphan-pages? builtin-pages? excluded-pages? created-at-filter]}]
   (let [dark? (= "dark" theme)
         current-page (or (:block/name (db/get-current-page)) "")]
     (when-let [repo (state/get-current-repo)]
@@ -93,34 +93,42 @@
             namespaces (db/get-all-namespace-relation repo)
             tags (set (map second tagged-pages))
             full-pages (db/get-all-pages repo)
+            full-pages-map (into {} (map (juxt :block/name identity) full-pages))
             all-pages (map db/get-original-name full-pages)
             page-name->original-name (zipmap (map :block/name full-pages) all-pages)
-            pages-after-journal-filter (if-not journal?
-                                         (remove :block/journal? full-pages)
-                                         full-pages)
-
-           pages-after-exclude-filter (cond->> pages-after-journal-filter
-                                        (not excluded-pages?)
-                                        (remove (fn [p] (true? (pu/get-property p :exclude-from-graph-view)))))
+            created-ats (map :block/created-at full-pages)
 
+            ;; build up nodes
+            full-pages'
+            (cond->> full-pages
+              created-at-filter
+              (filter #(<= (:block/created-at %) (+ (apply min created-ats) created-at-filter)))
+              (not journal?)
+              (remove :block/journal?)
+              (not excluded-pages?)
+              (remove (fn [p] (true? (pu/get-property p :exclude-from-graph-view)))))
             links (concat (seq relation)
                           (seq tagged-pages)
                           (seq namespaces))
             linked (set (flatten links))
             build-in-pages (set (map string/lower-case default-db/built-in-pages-names))
-            nodes (cond->> (map :block/name pages-after-exclude-filter)
+            nodes (cond->> (map :block/name full-pages')
                     (not builtin-pages?)
                     (remove (fn [p] (contains? build-in-pages (string/lower-case p))))
                     (not orphan-pages?)
                     (filter #(contains? linked (string/lower-case %))))
+
             page-links (reduce (fn [m [k v]] (-> (update m k inc)
                                                  (update v inc))) {} links)
             links (build-links (remove (fn [[_ to]] (nil? to)) links))
             nodes (build-nodes dark? (string/lower-case current-page) page-links tags nodes namespaces)]
-        (normalize-page-name
-         {:nodes nodes
-          :links links
-          :page-name->original-name page-name->original-name})))))
+        (-> {:nodes (map #(assoc % :block/created-at (get-in full-pages-map [(:id %) :block/created-at])) nodes)
+             :links links
+             :page-name->original-name page-name->original-name}
+            normalize-page-name
+            (assoc :all-pages
+                   {:created-at-min (apply min created-ats)
+                    :created-at-max (apply max created-ats)}))))))
 
 (defn build-page-graph
   [page theme show-journal]

+ 1 - 1
src/main/frontend/schema/handler/common_config.cljc

@@ -61,7 +61,7 @@
               :string]]
     [:ref/default-open-blocks-level :int]
     [:ref/linked-references-collapsed-threshold :int]
-    [:graph/settings [:map-of :keyword :boolean]]
+    [:graph/settings [:map-of :keyword [:or :boolean :int :nil]]]
     [:favorites [:vector :string]]
     ;; There isn't a :float yet
     [:srs/learning-fraction float?]