Browse Source

enhance: page/block embed using :block/link instead of {{embed }}

Tienson Qin 2 years ago
parent
commit
cdae6e2ff0

+ 28 - 13
src/main/frontend/commands.cljs

@@ -100,19 +100,30 @@
     [[:editor/input template {:last-pattern command-trigger
                               :backward-pos 2}]]))
 
-(defn embed-page
+(defn file-based-embed-page
   []
-  (conj
-   [[:editor/input "{{embed [[]]}}" {:last-pattern command-trigger
-                                     :backward-pos 4}]]
-   [:editor/search-page :embed]))
+  [[:editor/input "{{embed [[]]}}" {:last-pattern command-trigger
+                                    :backward-pos 4}]
+   [:editor/search-page :embed]])
 
-(defn embed-block
+(defn file-based-embed-block
   []
   [[:editor/input "{{embed (())}}" {:last-pattern command-trigger
                                     :backward-pos 4}]
    [:editor/search-block :embed]])
 
+(defn db-based-embed-page
+  []
+  [[:editor/input "[[]]" {:last-pattern command-trigger
+                          :backward-pos 2}]
+   [:editor/search-page :embed]])
+
+(defn db-based-embed-block
+  []
+  [[:editor/input "(())" {:last-pattern command-trigger
+                          :backward-pos 2}]
+   [:editor/search-block :embed]])
+
 (defn get-preferred-workflow
   []
   (let [workflow (state/get-preferred-workflow)]
@@ -217,7 +228,9 @@
 
 (defn commands-map
   [get-page-ref-text]
-  (let [db? (config/db-based-graph? (state/get-current-repo))]
+  (let [db? (config/db-based-graph? (state/get-current-repo))
+        embed-page (if db? db-based-embed-page file-based-embed-page)
+        embed-block (if db? db-based-embed-block file-based-embed-block)]
     (->>
      (concat
     ;; basic
@@ -339,7 +352,8 @@
 
 (defn insert!
   [id value
-   {:keys [last-pattern postfix-fn backward-pos end-pattern backward-truncate-number command only-breakline?]
+   {:keys [last-pattern postfix-fn backward-pos end-pattern backward-truncate-number
+           command only-breakline? skip-blank-value-check?]
     :as _option}]
   (when-let [input (gdom/getElement id)]
     (let [last-pattern (when-not (= last-pattern :skip-check)
@@ -391,9 +405,9 @@
                    :else
                    (util/replace-last last-pattern orig-prefix value space?))
           postfix (cond-> postfix
-                          (and only-breakline? postfix
-                               (= (get postfix 0) "\n"))
-                          (string/replace-first "\n" ""))
+                    (and only-breakline? postfix
+                         (= (get postfix 0) "\n"))
+                    (string/replace-first "\n" ""))
           new-value (cond
                       (string/blank? postfix)
                       prefix
@@ -405,7 +419,8 @@
                       (str prefix postfix))
           new-pos (- (count prefix)
                      (or backward-pos 0))]
-      (when-not (string/blank? new-value)
+      (when (or (not (string/blank? new-value))
+                skip-blank-value-check?)
         (state/set-block-content-and-last-pos! id new-value new-pos)
         (cursor/move-cursor-to input new-pos)))))
 
@@ -651,7 +666,7 @@
             (state/set-edit-content! input-id new-content))
           (state/pub-event! [:editor/set-org-mode-heading current-block heading]))))))
 
-(defmethod handle-step :editor/search-page [[_]]
+(defmethod handle-step :editor/search-page [_]
   (state/set-editor-action! :page-search))
 
 (defmethod handle-step :editor/search-page-hashtag [[_]]

+ 55 - 4
src/main/frontend/components/editor.cljs

@@ -30,7 +30,8 @@
             [logseq.graph-parser.util :as gp-util]
             [promesa.core :as p]
             [react-draggable]
-            [rum.core :as rum]))
+            [rum.core :as rum]
+            [frontend.config :as config]))
 
 (rum/defc commands < rum/reactive
   [id format]
@@ -103,10 +104,36 @@
 (defn- in-sidebar? [el]
   (not (.contains (.getElementById js/document "left-container") el)))
 
+(defn- page-on-chosen-handler
+  [embed? input id q pos format]
+  (if embed?
+    (fn [chosen-item]
+      (let [value (.-value input)
+            value' (str (gp-util/safe-subs value 0 q)
+                        (gp-util/safe-subs value (+ (count q) 4 pos)))]
+        (state/set-edit-content! (.-id input) value')
+        (state/clear-editor-action!)
+        (let [page-name (util/page-name-sanity-lc chosen-item)
+              page (db/entity [:block/name page-name])
+              _ (when-not page (page-handler/create! chosen-item {:redirect? false
+                                                                  :create-first-block? false}))
+              current-block (state/get-edit-block)]
+          (editor-handler/api-insert-new-block! chosen-item
+                                                {:block-uuid (:block/uuid current-block)
+                                                 :sibling? true
+                                                 :replace-empty-target? true
+                                                 :other-attrs {:block/link (:db/id (db/entity [:block/name page-name]))}}))))
+    (page-handler/on-chosen-handler input id q pos format)))
+
 (rum/defc page-search < rum/reactive
+  {:will-unmount (fn [state]
+                   (reset! commands/*current-command nil)
+                   state)}
   "Embedded page searching popup"
   [id format]
-  (let [action (state/sub :editor/action)]
+  (let [action (state/sub :editor/action)
+        db? (config/db-based-graph? (state/get-current-repo))
+        embed? (and db? (= @commands/*current-command "Page embed"))]
     (when (contains? #{:page-search :page-search-hashtag} action)
       (let [pos (state/get-editor-last-pos)
             input (gdom/getElement id)]
@@ -149,7 +176,7 @@
                                     (cons q matched-pages))))]
             (ui/auto-complete
              matched-pages
-             {:on-chosen   (page-handler/on-chosen-handler input id q pos format)
+             {:on-chosen   (page-on-chosen-handler embed? input id q pos format)
               :on-enter    #(page-handler/page-not-exists-handler input id q current-pos)
               :item-render (fn [page-name chosen?]
                              [:div.preview-trigger-wrapper
@@ -176,6 +203,27 @@
                              (editor-handler/get-matched-blocks q (:block/uuid edit-block)))]
       (reset! result matched-blocks))))
 
+(defn- block-on-chosen-handler
+  [embed? input id q format selected-text]
+  (if embed?
+    (fn [chosen-item]
+      (let [pos (state/get-editor-last-pos)
+            value (.-value input)
+            value' (str (gp-util/safe-subs value 0 q)
+                        (gp-util/safe-subs value (+ (count q) 4 pos)))]
+        (state/set-edit-content! (.-id input) value')
+        (state/clear-editor-action!)
+        (let [current-block (state/get-edit-block)
+              id (:block/uuid chosen-item)
+              id (if (string? id) (uuid id) id)]
+          (editor-handler/api-insert-new-block! ""
+                                                {:block-uuid (:block/uuid current-block)
+                                                 :sibling? true
+                                                 :replace-empty-target? true
+                                                 :other-attrs {:block/link (:db/id (db/entity [:block/uuid id]))}})
+          (state/clear-edit!))))
+    (editor-handler/block-on-chosen-handler id q format selected-text)))
+
 (rum/defcs block-search-auto-complete < rum/reactive
   {:init (fn [state]
            (let [result (atom nil)]
@@ -187,7 +235,9 @@
   [state _edit-block input id q format selected-text]
   (let [result (->> (rum/react (get state ::result))
                     (remove (fn [b] (string/blank? (:block/content (db-model/query-block-by-uuid (:block/uuid b)))))))
-        chosen-handler (editor-handler/block-on-chosen-handler id q format selected-text)
+        db? (config/db-based-graph? (state/get-current-repo))
+        embed? (and db? (= @commands/*current-command "Block embed"))
+        chosen-handler (block-on-chosen-handler embed? input id q format selected-text)
         non-exist-block-handler (editor-handler/block-non-exist-handler input)]
     (ui/auto-complete
      result
@@ -207,6 +257,7 @@
 
 (rum/defcs block-search < rum/reactive
   {:will-unmount (fn [state]
+                   (reset! commands/*current-command nil)
                    (state/clear-search-result!)
                    state)}
   [state id _format]

+ 3 - 1
src/main/frontend/handler/editor.cljs

@@ -470,7 +470,8 @@
 
 (defn api-insert-new-block!
   [content {:keys [page block-uuid sibling? before? properties
-                   custom-uuid replace-empty-target? edit-block?]
+                   custom-uuid replace-empty-target? edit-block?
+                   other-attrs]
             :or {sibling? false
                  before? false
                  edit-block? true}}]
@@ -511,6 +512,7 @@
               new-block (if (and db-based? (seq properties))
                           (assoc new-block :block/properties (db-property/replace-key-with-id! properties))
                           new-block)
+              new-block (merge new-block other-attrs)
               [block-m sibling?] (cond
                                    before?
                                    (let [first-child? (->> [:block/parent :block/left]

+ 1 - 2
src/main/frontend/handler/repo.cljs

@@ -541,8 +541,7 @@
   (ipc/ipc "graphReady" graph))
 
 (defn- create-db [full-graph-name]
-  (p/let [; _ (ipc/ipc :db-new full-graph-name)
-          _ (persist-db/<new full-graph-name)
+  (p/let [_ (persist-db/<new full-graph-name)
           _ (start-repo-db-if-not-exists! full-graph-name)
           _ (state/add-repo! {:url full-graph-name})
           _ (route-handler/redirect-to-home!)