Explorar o código

Merge branch 'master' into improve/plugin-register-ui-actions

charlie %!s(int64=4) %!d(string=hai) anos
pai
achega
d4509cd7de

+ 1 - 1
resources/package.json

@@ -1,6 +1,6 @@
 {
   "name": "Logseq",
-  "version": "0.2.1",
+  "version": "0.2.2",
   "main": "electron.js",
   "author": "Logseq",
   "description": "A privacy-first, open-source platform for knowledge management and collaboration.",

+ 8 - 9
src/main/frontend/components/block.cljs

@@ -644,23 +644,21 @@
 (defn- show-link?
   [config metadata s full-text]
   (let [img-formats (set (map name (config/img-formats)))
-        metadata-show (:show (safe-read-string metadata))]
+        metadata-show (:show (safe-read-string metadata))
+        format (get-in config [:block :block/format])]
     (or
-     ;; markdown
-     (and
-      (= :markdown (get-in config [:block :block/format]))
-      (string/starts-with? (string/triml full-text) "!"))
-
-     ;; org
      (and
-      (= :org (get-in config [:block :block/format]))
+      (= :org format)
       (or
        (and
         (nil? metadata-show)
         (or
          (config/local-asset? s)
          (text/image-link? img-formats s)))
-       (true? (boolean metadata-show)))))))
+       (true? (boolean metadata-show))))
+
+     ;; markdown
+     (string/starts-with? (string/triml full-text) "!"))))
 
 (defn inline
   [{:keys [html-export?] :as config} item]
@@ -1788,6 +1786,7 @@
                           (:block/content (second (:rum/args new-state)))))}
   [state config {:block/keys [uuid title body meta content page format repo children pre-block? top? properties refs path-refs heading-level level type] :as block}]
   (let [blocks-container-id (:blocks-container-id config)
+        config (update config :block merge block)
         heading? (and (= type :heading) heading-level (<= heading-level 6))
         *control-show? (get state ::control-show?)
         *ref-collapsed? (get state ::ref-collapsed?)

+ 12 - 12
src/main/frontend/components/hierarchy.cljs

@@ -4,20 +4,20 @@
             [frontend.components.block :as block]
             [rum.core :as rum]
             [frontend.ui :as ui]
-            [medley.core :as medley]))
+            [medley.core :as medley]
+            [frontend.db :as db]
+            [frontend.state :as state]
+            [frontend.text :as text]))
 
 (defn get-relation
-  ([page]
-   (get-relation page 100))
-  ([page limit]
-   (->> (search/page-search page limit)
-        (filter #(or
-                  (= page %)
-                  (string/starts-with? % (str page "/"))
-                  (string/includes? % (str "/" page "/"))
-                  (string/ends-with? % (str "/" page))))
-        (map #(string/split % #"/"))
-        (remove #(= % [page])))))
+  [page]
+  (when (text/namespace-page? page)
+    (->> (db/get-namespace-pages (state/get-current-repo) page)
+         (map (fn [page]
+                (or (:block/original-name page) (:block/name page))))
+         (map #(string/split % #"/"))
+         (remove #(= % [page]))
+         (sort))))
 
 (rum/defc structures
   [page]

+ 2 - 1
src/main/frontend/components/page.cljs

@@ -417,7 +417,8 @@
               (reference/references route-page-name false)
               (str route-page-name "-refs"))]
 
-           (hierarchy/structures route-page-name)
+           (when (text/namespace-page? route-page-name)
+             (hierarchy/structures route-page-name))
 
            ;; TODO: or we can lazy load them
            (when-not sidebar?

+ 13 - 2
src/main/frontend/components/sidebar.cljs

@@ -31,7 +31,8 @@
             [frontend.context.i18n :as i18n]
             [reitit.frontend.easy :as rfe]
             [goog.dom :as gdom]
-            [frontend.handler.web.nfs :as nfs-handler]))
+            [frontend.handler.web.nfs :as nfs-handler]
+            [cljs-drag-n-drop.core :as dnd]))
 
 (defn nav-item
   [title href svg-d active? close-modal-fn]
@@ -105,7 +106,17 @@
     [:div.flex-1.h-0.overflow-y-auto
      (sidebar-nav route-match close-fn)]]])
 
-(rum/defc main
+(rum/defc main <
+  {:did-mount (fn [state]
+                (when-let [element (gdom/getElement "main-content")]
+                  (dnd/subscribe!
+                   element
+                   :upload-files
+                   {:drop (fn [e files]
+                            (when-let [id (state/get-edit-input-id)]
+                              (let [format (:block/format (state/get-edit-block))]
+                                (editor-handler/upload-asset id files format editor-handler/*asset-uploading? true))))}))
+                state)}
   [{:keys [route-match global-graph-pages? logged? home? route-name indexeddb-support? white? db-restoring? main-content]}]
   (ui/catch-error
    [:div#main-content-container.w-full.flex.justify-center

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

@@ -50,7 +50,7 @@
   get-pages get-pages-relation get-pages-that-mentioned-page get-public-pages get-tag-pages
   journal-page? local-native-fs? mark-repo-as-cloned! page-alias-set page-blocks-transform pull-block
   set-file-last-modified-at! transact-files-db! with-block-refs-count get-modified-pages page-empty? page-empty-or-dummy? get-alias-source-page
-  set-file-content! has-children? get-namespace-files]
+  set-file-content! has-children? get-namespace-pages]
 
  [frontend.db.react
   get-current-marker get-current-page get-current-priority set-key-value

+ 15 - 10
src/main/frontend/db/model.cljs

@@ -1252,16 +1252,21 @@
            page-id)
       ffirst))
 
-(defn get-namespace-files
+(defn get-namespace-pages
   [repo namespace]
   (assert (string? namespace))
-  (let [db (conn/get-conn repo)
-        namespace (string/replace namespace "/" ".")]
+  (let [db (conn/get-conn repo)]
     (when-not (string/blank? namespace)
-     (let [namespace (string/trim namespace)
-           pattern-1 (re-pattern (util/format "[\\.|/]%s\\." namespace))
-           pattern-2 (re-pattern (util/format "^%s\\." namespace))]
-       (->> (d/datoms db :aevt :file/path)
-            (filter (fn [datom]
-                      (or (re-find pattern-1 (:v datom))
-                          (re-find pattern-2 (:v datom))))))))))
+      (let [namespace (string/lower-case (string/trim namespace))
+            ids (->> (d/datoms db :aevt :block/name)
+                     (filter (fn [datom]
+                               (let [page (:v datom)]
+                                 (or
+                                  (= page namespace)
+                                  (string/starts-with? page (str namespace "/"))))))
+                     (map :e))]
+        (when (seq ids)
+          (db-utils/pull-many repo
+                              '[:db/id :block/name :block/original-name
+                                {:block/file [:db/id :file/path]}]
+                              ids))))))

+ 20 - 22
src/main/frontend/db/react.cljs

@@ -293,28 +293,26 @@
         (when-let [cache (get @query-state related-key)]
           (let [{:keys [query inputs transform-fn query-fn inputs-fn]} cache]
             (when (or query query-fn)
-              (let [new-result (profile
-                                "takes"
-                                (->
-                                 (cond
-                                   query-fn
-                                   (profile
-                                    "Query:"
-                                    (doall (query-fn db)))
-
-                                   inputs-fn
-                                   (let [inputs (inputs-fn)]
-                                     (apply d/q query db inputs))
-
-                                   (keyword? query)
-                                   (db-utils/get-key-value repo-url query)
-
-                                   (seq inputs)
-                                   (apply d/q query db inputs)
-
-                                   :else
-                                   (d/q query db))
-                                 transform-fn))]
+              (let [new-result (->
+                                (cond
+                                  query-fn
+                                  (profile
+                                   "Query:"
+                                   (doall (query-fn db)))
+
+                                  inputs-fn
+                                  (let [inputs (inputs-fn)]
+                                    (apply d/q query db inputs))
+
+                                  (keyword? query)
+                                  (db-utils/get-key-value repo-url query)
+
+                                  (seq inputs)
+                                  (apply d/q query db inputs)
+
+                                  :else
+                                  (d/q query db))
+                                transform-fn)]
                 (set-new-result! related-key new-result)))))))))
 
 (defn transact-react!

+ 5 - 5
src/main/frontend/extensions/code.cljs

@@ -155,11 +155,11 @@
                                  (save-file-or-block-when-blur-or-esc! editor textarea config state))))
           (.addEventListener element "mousedown"
                              (fn [e]
-                               (let [block (into {} (db/get-block-by-uuid (:block/uuid config)))]
-                                 (state/clear-selection!)
-                                 (state/set-editing! id (.getValue editor) block nil false)
-                                 (util/stop e)
-                                 (state/set-block-component-editing-mode! true))))
+                               (state/clear-selection!)
+                               (when-let [block (and (:block/uuid config) (into {} (db/get-block-by-uuid (:block/uuid config))))]
+                                 (state/set-editing! id (.getValue editor) block nil false))
+                               (util/stop e)
+                               (state/set-block-component-editing-mode! true)))
           (.save editor)
           (.refresh editor)))
       editor)))

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

@@ -274,10 +274,7 @@
      (concat title body))
     (let [refs (remove string/blank? @refs)
           children-pages (->> (mapcat (fn [p]
-                                        (when (and (string/includes? p "/")
-                                                   (not (string/starts-with? p "../"))
-                                                   (not (string/starts-with? p "./"))
-                                                   (not (string/starts-with? p "http")))
+                                        (when (text/namespace-page? p)
                                           (util/split-namespace-pages p)))
                                       refs)
                               (remove string/blank?))

+ 14 - 12
src/main/frontend/handler/editor.cljs

@@ -2889,8 +2889,16 @@
 
      (map (fn [x] (dissoc x :block/children))))))
 
+(defn collapsable? [block-id]
+  (if-let [block (db-model/get-block-by-uuid block-id)]
+    (and
+     (nil? (-> block :block/properties :collapsed))
+     (or (not-empty (:block/body block))
+         (db-model/has-children? block-id)))
+    false))
+
 (defn collapse-block! [block-id]
-  (when (db-model/has-children? block-id)
+  (when (collapsable? block-id)
     (set-block-property! block-id :collapsed true)))
 
 (defn expand-block! [block-id]
@@ -2951,15 +2959,13 @@
     (let [blocks-with-level
           (all-blocks-with-level {:collapse? true})
           max-level (apply max (map :block/level blocks-with-level))]
-      (loop [level (dec max-level)]
+      (loop [level max-level]
         (if (zero? level)
           nil
           (let [blocks-to-collapse
                 (->> blocks-with-level
                      (filter (fn [b] (= (:block/level b) level)))
-                     (remove (fn [b]
-                               (or (not (db-model/has-children? (:block/uuid b)))
-                                   (-> b :block/properties :collapsed)))))]
+                     (filter (fn [b] (collapsable? (:block/uuid b)))))]
             (if (empty? blocks-to-collapse)
               (recur (dec level))
               (doseq [{:block/keys [uuid]} blocks-to-collapse]
@@ -2969,8 +2975,7 @@
   []
   (let [blocks-to-collapse
         (->> (all-blocks-with-level {:collapse? true})
-             (filter (fn [b] (= (:block/level b) 1)))
-             (remove (fn [b] (-> b :block/properties :collapsed))))]
+             (filter (fn [b] (collapsable? (:block/uuid b)))))]
     (when (seq blocks-to-collapse)
       (doseq [{:block/keys [uuid]} blocks-to-collapse]
         (collapse-block! uuid)))))
@@ -2985,11 +2990,8 @@
 (defn toggle-open! []
   (let [all-collapsed?
         (->> (all-blocks-with-level {:collapse? true})
-             (filter (fn [b] (= (:block/level b) 1)))
-             (every? (fn [{:block/keys [uuid properties]}]
-                       (or
-                        (not (db-model/has-children? uuid))
-                        (some? (-> properties :collapsed))))))]
+             (filter (fn [b] (collapsable? (:block/uuid b))))
+             (empty?))]
     (if all-collapsed?
       (expand-all!)
       (collapse-all!))))

+ 1 - 20
src/main/frontend/handler/editor/lifecycle.cljs

@@ -3,7 +3,6 @@
             [goog.dom :as gdom]
             [goog.object :as gobj]
             [clojure.string :as string]
-            [cljs-drag-n-drop.core :as dnd]
             [frontend.handler.editor.keyboards :as keyboards-handler]
             [frontend.handler.page :as page-handler]
             [frontend.handler.editor :as editor-handler :refer [get-state]]
@@ -15,20 +14,11 @@
 (defn did-mount!
   [state]
   (let [[{:keys [format block-parent-id]} id] (:rum/args state)
-        content (get-in @state/state [:editor/content id])
-        input (gdom/getElement id)
-        element (gdom/getElement "main-content")]
+        content (get-in @state/state [:editor/content id])]
     (when block-parent-id
       (state/set-editing-block-dom-id! block-parent-id))
     (editor-handler/restore-cursor-pos! id content)
 
-    (when (and input element)
-      (dnd/subscribe!
-       element
-       :upload-files
-       {:drop (fn [e files]
-                (editor-handler/upload-asset id files format editor-handler/*asset-uploading? true))}))
-
     ;; Here we delay this listener, otherwise the click to edit event will trigger a outside click event,
     ;; which will hide the editor so no way for editing.
     (js/setTimeout #(keyboards-handler/esc-save! state) 100)
@@ -46,15 +36,6 @@
   [state]
   (let [{:keys [id value format block repo config]} (get-state)
         file? (:file? config)]
-    (when-let [input (gdom/getElement id)]
-      ;; (.removeEventListener input "paste" (fn [event]
-      ;;                                       (append-paste-doc! format event)))
-      (let [s (str "cljs-drag-n-drop." :upload-images)
-            element (gdom/getElement "main-content")
-            a (gobj/get element s)
-            timer (:timer a)]
-        (when (and element timer)
-          (dnd/unsubscribe! element :upload-files))))
     (editor-handler/clear-when-saved!)
     ;; TODO: ugly
     (when-not (contains? #{:insert :indent-outdent :auto-save :undo :redo :delete} (state/get-editor-op))

+ 43 - 35
src/main/frontend/handler/page.cljs

@@ -234,13 +234,19 @@
 ;; FIXME: not safe
 ;; 1. normal pages [[foo]]
 ;; 2. namespace pages [[foo/bar]]
+;; 3. what if there's a tag `#foobar` and we want to replace `#foo` with `#something`?
 (defn- replace-old-page!
   [s old-name new-name]
-  (let [pattern "[[%s/"]
-    (-> s
-        (string/replace (util/format "[[%s]]" old-name) (util/format "[[%s]]" new-name))
-        (string/replace (util/format pattern old-name) (util/format pattern new-name))
-        (string/replace (str "#" old-name) (str "#" new-name)))))
+  (let [get-tag-pattern (fn [s] (if (string/includes? s " ")
+                                 (str "#[[" s "]]")
+                                 (str "#" s)))
+        old-tag-pattern (get-tag-pattern old-name)
+        new-tag-pattern (get-tag-pattern new-name)]
+    (let [pattern "[[%s/"]
+     (-> s
+         (string/replace (util/format "[[%s]]" old-name) (util/format "[[%s]]" new-name))
+         (string/replace (util/format pattern old-name) (util/format pattern new-name))
+         (string/replace old-tag-pattern new-tag-pattern)))))
 
 (defn- walk-replace-old-page!
   [form old-name new-name]
@@ -266,38 +272,40 @@
   [old-page-title old-name new-name]
   (string/replace-first old-page-title old-name new-name))
 
-;; FIXME: get namespace pages instead of files for compatibility with the
-;; database-only version.
+(defn- get-new-file-path
+  [old-path old-name new-name]
+  (let [path-old-name (string/replace old-name "/" ".")
+        path-new-name (string/replace new-name "/" ".")
+        [search replace] (cond
+                           (string/includes? old-path (str "." path-old-name "."))
+                           [(str "." path-old-name ".") (str "." path-new-name ".")]
+
+                           (string/includes? old-path (str "/" path-old-name "."))
+                           [(str "/" path-old-name ".") (str "/" path-new-name ".")]
+
+                           :else
+                           [(str path-old-name ".") (str path-new-name ".")])]
+    (string/replace-first old-path search replace)))
+
 (defn- rename-namespace-pages!
   [repo old-name new-name]
-  (doseq [datom (db/get-namespace-files repo old-name)]
-    (let [old-path (:v datom)
-          path-old-name (string/replace old-name "/" ".")
-          path-new-name (string/replace new-name "/" ".")
-          [search replace] (cond
-                             (string/includes? old-path (str "." path-old-name "."))
-                             [(str "." path-old-name ".") (str "." path-new-name ".")]
-
-                             (string/includes? old-path (str "/" path-old-name "."))
-                             [(str "/" path-old-name ".") (str "/" path-new-name ".")]
-
-                             :else
-                             [(str path-old-name ".") (str path-new-name ".")])
-          new-path (string/replace-first old-path search replace)
-          tx {:db/id (:e datom)
-              :file/path new-path}
-          page (db/entity (db/get-file-page-id old-path))
-          page-tx (when page
-                    (let [old-page-title (or (:block/original-name page)
-                                             (:block/name page))
-                          new-page-title (build-new-namespace-page-title old-page-title old-name new-name)]
-                      {:db/id (:db/id page)
-                       :block/original-name new-page-title
-                       :block/name (string/lower-case new-page-title)}))
-          txs (->> [tx page-tx] (remove nil?))]
-      (db/transact! repo txs)
-      (p/let [_ (rename-file-aux! repo old-path new-path)]
-        (println "Renamed " old-path " to " new-path ".")))))
+  (let [pages (db/get-namespace-pages repo old-name)]
+    (doseq [{:block/keys [name original-name file] :as page} pages]
+      (let [old-page-title (or original-name name)
+            new-page-title (build-new-namespace-page-title old-page-title old-name new-name)
+            page-tx {:db/id (:db/id page)
+                     :block/original-name new-page-title
+                     :block/name (string/lower-case new-page-title)}
+            old-path (:file/path file)
+            new-path (when old-path
+                       (get-new-file-path old-path old-name new-name))
+            file-tx (when file
+                      {:db/id (:db/id file)
+                       :file/path new-path})
+            txs (->> [file-tx page-tx] (remove nil?))]
+        (db/transact! repo txs)
+        (p/let [_ (rename-file-aux! repo old-path new-path)]
+          (println "Renamed " old-path " to " new-path))))))
 
 (defn rename!
   [old-name new-name]

+ 10 - 0
src/main/frontend/text.cljs

@@ -185,3 +185,13 @@
                (if remove-first-line? lines r))
         content (if remove-first-line? body (cons f body))]
     (string/join "\n" content)))
+
+(defn namespace-page?
+  [p]
+  (and (not (string/starts-with? p "../"))
+       (not (string/starts-with? p "./"))
+       (not (string/starts-with? p "http"))
+       (not
+        (when-let [last-part (last (string/split p #"/"))]
+          ;; a file
+          (string/includes? last-part ".")))))

+ 1 - 1
src/main/frontend/version.cljs

@@ -1,3 +1,3 @@
 (ns frontend.version)
 
-(defonce version "0.2.1")
+(defonce version "0.2.2")