Browse Source

fix: refactor blocks container id and fix #1965

https://github.com/logseq/logseq/issues/1965
Tienson Qin 4 years ago
parent
commit
a080c3e948

+ 26 - 28
src/main/frontend/components/block.cljs

@@ -85,8 +85,7 @@
 (defonce virtual-list-scroll-step 450)
 (defonce virtual-list-previous 50)
 
-(defonce container-ids (atom {}))
-(defonce container-idx (atom 0))
+(defonce *blocks-container-id (atom 0))
 
 ;; TODO:
 ;; add `key`
@@ -397,7 +396,7 @@
                                  :padding-bottom 64}}
                         [:h2.font-bold.text-lg page-name]
                         (let [page (db/entity [:block/name (string/lower-case page-name)])]
-                          ((state/get-page-blocks-cp) (state/get-current-repo) page (:sidebar? config)))]
+                          ((state/get-page-blocks-cp) (state/get-current-repo) page {:sidebar? (:sidebar? config)}))]
                  :interactive true
                  :delay 1000}
                 inner))))
@@ -476,6 +475,7 @@
      [:div.px-3.pt-1.pb-2
       (blocks-container blocks (assoc config
                                       :id (str id)
+                                      :embed-id id
                                       :embed? true
                                       :ref? false))]]))
 
@@ -548,7 +548,7 @@
                              {:style {:width 735
                                       :text-align "left"
                                       :max-height 600}}
-                             (block-container config block)]
+                             (blocks-container [block] config)]
                       :interactive true
                       :delay 1000}
                      (if label
@@ -1115,18 +1115,6 @@
                       "hide-inner-bullet"))}
        [:span.bullet {:blockid (str uuid)}]]]]))
 
-(defn- build-id
-  [config]
-  (let [ref? (:ref? config)
-        sidebar? (:sidebar? config)
-        k (pr-str config)
-        n (or
-           (get @container-ids k)
-           (let [n' (if (and ref? (not sidebar?)) @container-idx (swap! container-idx inc))]
-             (swap! container-ids assoc k n')
-             n'))]
-    (str n "-")))
-
 (rum/defc dnd-separator
   [block margin-left bottom top? nested?]
   (let [id (str (:block/uuid block)
@@ -1534,9 +1522,10 @@
 (rum/defc block-content-or-editor < rum/reactive
   [config {:block/keys [uuid title body meta content dummy? page format repo children marker properties block-refs-count pre-block? idx] :as block} edit-input-id block-id slide? heading-level]
   (let [editor-box (get config :editor-box)
-        edit? (state/sub [:editor/editing? edit-input-id])]
+        edit? (state/sub [:editor/editing? edit-input-id])
+        editor-id (str "editor-" edit-input-id)]
     (if (and edit? editor-box)
-      [:div.editor-wrapper {:id (str "editor-" edit-input-id)}
+      [:div.editor-wrapper {:id editor-id}
        (editor-box {:block block
                     :block-id uuid
                     :block-parent-id block-id
@@ -1552,7 +1541,9 @@
        [:div.flex.flex-1
         (block-content config block edit-input-id block-id slide?)]
        [:div.flex.flex-row
-        (when (and (:embed? config) (not (:page-embed? config)))
+        (when (and (:embed? config)
+                   (not (:page-embed? config))
+                   (= (:embed-id config) uuid))
           [:a.opacity-30.hover:opacity-100.svg-small.inline
            {:on-mouse-down (fn [e]
                              (util/stop e)
@@ -1757,7 +1748,8 @@
                     (not= (:block/content (second (:rum/args old-state)))
                           (:block/content (second (:rum/args new-state)))))}
   [state config {:block/keys [uuid title body meta content dummy? page format repo children pre-block? top? properties refs-with-children heading-level level type] :as block}]
-  (let [heading? (and (= type :heading) heading-level (<= heading-level 6))
+  (let [blocks-container-id (:blocks-container-id config)
+        heading? (and (= type :heading) heading-level (<= heading-level 6))
         *control-show? (get state ::control-show?)
         *ref-collapsed? (get state ::ref-collapsed?)
         collapsed? (or @*ref-collapsed? (get properties :collapsed))
@@ -1768,8 +1760,7 @@
         doc-mode? (:document/mode? config)
         embed? (:embed? config)
         reference? (:reference? config)
-        unique-dom-id (build-id (dissoc config :block/uuid))
-        block-id (str "ls-block-" unique-dom-id uuid)
+        block-id (str "ls-block-" blocks-container-id "-" uuid)
         has-child? (boolean
                     (and
                      (not pre-block?)
@@ -1807,7 +1798,7 @@
       (when (not slide?)
         (block-control config block uuid block-id body children dummy? collapsed? *ref-collapsed? *control-show?))
 
-      (let [edit-input-id (str "edit-block-" unique-dom-id uuid)]
+      (let [edit-input-id (str "edit-block-" blocks-container-id "-" uuid)]
         (block-content-or-editor config block edit-input-id block-id slide? heading-level))]
 
      (block-children config children collapsed? *ref-collapsed?)
@@ -2308,16 +2299,23 @@
                      blocks)]
        sections))))
 
-(defn blocks-container
-  [blocks config]
-  (let [
-        ;; blocks (map #(dissoc % :block/children) blocks)
+(rum/defcs blocks-container <
+  {:init (fn [state]
+           (assoc state ::init-blocks-container-id (atom nil)))}
+  [state blocks config]
+  (let [*init-blocks-container-id (::init-blocks-container-id state)
+        blocks-container-id (if @*init-blocks-container-id
+                              @*init-blocks-container-id
+                              (let [id' (swap! *blocks-container-id inc)]
+                                (reset! *init-blocks-container-id id')
+                                id'))
         sidebar? (:sidebar? config)
         ref? (:ref? config)
         custom-query? (:custom-query? config)
         blocks->vec-tree #(if (or custom-query? ref?) % (tree/blocks->vec-tree % (:id config)))
         blocks' (blocks->vec-tree blocks)
-        blocks (if (seq blocks') blocks' blocks)]
+        blocks (if (seq blocks') blocks' blocks)
+        config (assoc config :blocks-container-id blocks-container-id)]
     (when (seq blocks)
       [:div.blocks-container.flex-1
        {:style {:margin-left (cond

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

@@ -88,7 +88,7 @@
 
 (rum/defc page-blocks-cp < rum/reactive
   db-mixins/query
-  [repo page-e sidebar?]
+  [repo page-e {:keys [sidebar? preview?] :as config}]
   (when page-e
     (let [page-name (or (:block/name page-e)
                         (str (:block/uuid page-e)))
@@ -104,11 +104,12 @@
                            :block/file {:db/id (:db/id (:block/file page-e))}})
                         {:journal? journal?
                          :page-name page-name})
-          hiccup-config {:id (if block? (str block-id) page-name)
-                         :sidebar? sidebar?
-                         :block? block?
-                         :editor-box editor/box
-                         :page page}
+          hiccup-config (merge
+                         {:id (if block? (str block-id) page-name)
+                          :block? block?
+                          :editor-box editor/box
+                          :page page}
+                         config)
           hiccup-config (common-handler/config-with-document-mode hiccup-config)
           hiccup (block/->hiccup page-blocks hiccup-config {})]
       (page-blocks-inner page-name page-blocks hiccup sidebar?))))
@@ -116,7 +117,7 @@
 (defn contents-page
   [page]
   (when-let [repo (state/get-current-repo)]
-    (page-blocks-cp repo page true)))
+    (page-blocks-cp repo page {:sidebar? true})))
 
 (rum/defc today-queries < rum/reactive
   [repo today? sidebar?]
@@ -381,7 +382,7 @@
             (let [page (if block?
                          (db/entity repo [:block/uuid block-id])
                          page)]
-              (page-blocks-cp repo page sidebar?))]]
+              (page-blocks-cp repo page {:sidebar? sidebar?}))]]
 
           (when-not block?
             (today-queries repo today? sidebar?))

+ 4 - 15
src/main/frontend/modules/shortcut/core.cljs

@@ -81,26 +81,16 @@
     (swap! *installed dissoc install-id)))
 
 
-;; Sometimes backspace removes two chars instead of one
-;; https://github.com/logseq/logseq/issues/1965
-;; A workaround to ensure each shortcut group is only registered once
-
-(defonce *registered-shortcuts-state (atom nil))
-
 (defn- uninstall-shortcut-aux!
   [state handler-id]
   (some-> (get state :shortcut-key)
-          uninstall-shortcut!)
-  (swap! *registered-shortcuts-state dissoc handler-id))
+          uninstall-shortcut!))
 
 (defn- install-shortcut-aux!
   [state handler-id]
-  (if (get @*registered-shortcuts-state handler-id)
-    state
-    (let [install-id (-> handler-id
-                        (install-shortcut! {:state state}))]
-     (swap! *registered-shortcuts-state assoc handler-id install-id)
-     (assoc state :shortcut-key install-id))))
+  (let [install-id (-> handler-id
+                       (install-shortcut! {:state state}))]
+    (assoc state :shortcut-key install-id)))
 
 (defn mixin [handler-id]
   {:did-mount
@@ -108,7 +98,6 @@
      (install-shortcut-aux! state handler-id))
 
    :did-remount (fn [old-state new-state]
-
                   ;; uninstall
                   (uninstall-shortcut-aux! old-state handler-id)