Jelajahi Sumber

enhance: zoom in shouldn't update the collapse status in db/files

Tienson Qin 4 tahun lalu
induk
melakukan
cd9bb9190f

+ 79 - 99
src/main/frontend/components/block.cljs

@@ -185,23 +185,23 @@
       (ui/resize-provider
        (ui/resize-consumer
         (cond->
-         {:className "resize image-resize"
-          :onSizeChanged (fn [value]
-                           (when (and (not @*resizing-image?)
-                                      (some? @size)
-                                      (not= value @size))
-                             (reset! *resizing-image? true))
-                           (reset! size value))
-          :onMouseUp (fn []
-                       (when (and @size @*resizing-image?)
-                         (when-let [block-id (:block/uuid config)]
-                           (let [size (bean/->clj @size)]
-                             (editor-handler/resize-image! block-id metadata full_text size))))
-                       (when @*resizing-image?
+          {:className "resize image-resize"
+           :onSizeChanged (fn [value]
+                            (when (and (not @*resizing-image?)
+                                       (some? @size)
+                                       (not= value @size))
+                              (reset! *resizing-image? true))
+                            (reset! size value))
+           :onMouseUp (fn []
+                        (when (and @size @*resizing-image?)
+                          (when-let [block-id (:block/uuid config)]
+                            (let [size (bean/->clj @size)]
+                              (editor-handler/resize-image! block-id metadata full_text size))))
+                        (when @*resizing-image?
                           ;; TODO: need a better way to prevent the clicking to edit current block
-                         (js/setTimeout #(reset! *resizing-image? false) 200)))
-          :onClick (fn [e]
-                     (when @*resizing-image? (util/stop e)))}
+                          (js/setTimeout #(reset! *resizing-image? false) 200)))
+           :onClick (fn [e]
+                      (when @*resizing-image? (util/stop e)))}
           (and (:width metadata) (not (util/mobile?)))
           (assoc :style {:width (:width metadata)}))
         [:div.asset-container
@@ -476,7 +476,7 @@
           redirect-page-name (or (and (= :org (state/get-preferred-format))
                                       (:org-mode/insert-file-link? (state/get-config))
                                       redirect-page-name)
-                              (model/get-redirect-page-name page-name (:block/alias? config)))
+                                 (model/get-redirect-page-name page-name (:block/alias? config)))
           inner (page-inner config
                             page-name-in-block
                             page-name
@@ -924,9 +924,9 @@
             (->elem
              :a
              (cond->
-              {:href      (str "file://" path)
-               :data-href path
-               :target    "_blank"}
+               {:href      (str "file://" path)
+                :data-href path
+                :target    "_blank"}
                title
                (assoc :title title))
              (map-inline config label)))
@@ -983,9 +983,9 @@
                     (->elem
                      :a
                      (cond->
-                      {:href      (str "file://" href*)
-                       :data-href href*
-                       :target    "_blank"}
+                       {:href      (str "file://" href*)
+                        :data-href href*
+                        :target    "_blank"}
                        title
                        (assoc :title title))
                      (map-inline config label))))))
@@ -1009,20 +1009,20 @@
 
             (= protocol "ar")
             (->elem
-              :a.external-link
-              (cond->
-                {:href (ar-url->http-url href)
-                 :target "_blank"}
-                title
-                (assoc :title title))
-              (map-inline config label))
+             :a.external-link
+             (cond->
+               {:href (ar-url->http-url href)
+                :target "_blank"}
+               title
+               (assoc :title title))
+             (map-inline config label))
 
             :else
             (->elem
              :a.external-link
              (cond->
-              {:href href
-               :target "_blank"}
+               {:href href
+                :target "_blank"}
                title
                (assoc :title title))
              (map-inline config label))))))
@@ -1357,9 +1357,9 @@
        (map editor-handler/expand-block! block-ids)))))
 
 (rum/defc block-children < rum/reactive
-  [config children collapsed? *ref-collapsed?]
+  [config children collapsed? *default-collapsed?]
   (let [ref? (:ref? config)
-        collapsed? (if ref? (rum/react *ref-collapsed?) collapsed?)
+        collapsed? (if ref? (rum/react *default-collapsed?) collapsed?)
         children (and (coll? children) (filter map? children))]
     (when (and (coll? children)
                (seq children)
@@ -1396,7 +1396,7 @@
    (every? #(= % ["Horizontal_Rule"]) body)))
 
 (rum/defcs block-control < rum/reactive
-  [state config block uuid block-id body children collapsed? *ref-collapsed? *control-show? edit?]
+  [state config block uuid block-id body children collapsed? *control-show? edit?]
   (let [doc-mode? (state/sub :document/mode?)
         has-children-blocks? (and (coll? children) (seq children))
         has-child? (and
@@ -1408,9 +1408,7 @@
                                 (seq body))
                            has-children-blocks?)
                        (util/react *control-show?))
-        ref-collapsed? (util/react *ref-collapsed?)
         ref? (:ref? config)
-        collapsed? (if ref? ref-collapsed? collapsed?)
         empty-content? (block-content-empty? block)]
     [:div.mr-1.flex.flex-row.items-center.sm:mr-2
      {:style {:height 24
@@ -1422,11 +1420,9 @@
        :on-click (fn [event]
                    (util/stop event)
                    (when-not (and (not collapsed?) (not has-child?))
-                     (if ref?
-                       (swap! *ref-collapsed? not)
-                       (if collapsed?
-                         (editor-handler/expand-block! uuid)
-                         (editor-handler/collapse-block! uuid)))))}
+                     (if collapsed?
+                       (editor-handler/expand-block! uuid)
+                       (editor-handler/collapse-block! uuid))))}
       [:span {:class (if control-show? "control-show" "control-hide")}
        (ui/rotating-arrow collapsed?)]]
      (let [bullet [:a {:on-click (fn [event]
@@ -1570,7 +1566,7 @@
 
 (defn build-block-title
   [config {:block/keys [title marker pre-block? properties level heading-level]
-                                :as t}]
+           :as t}]
   (let [config (assoc config :block t)
         slide? (boolean (:slide? config))
         block-ref? (:block-ref? config)
@@ -1622,7 +1618,7 @@
           (when (and (util/electron?) (not= block-type :default))
             [:a.prefix-link
              {:on-click #(case block-type
-                            ;; pdf annotation
+                           ;; pdf annotation
                            :annotation (pdf-assets/open-block-ref! t)
                            (.preventDefault %))}
 
@@ -1795,12 +1791,12 @@
            (not (:block/pre-block? block)))
       (let [move-to (rum/react *move-to)]
         (when-not
-         (or (and top? (not= move-to :top))
-             (and (not top?) (= move-to :top))
-             (and block-content? (not= move-to :nested))
-             (and (not block-content?)
-                  (seq (:block/children block))
-                  (= move-to :nested)))
+            (or (and top? (not= move-to :top))
+                (and (not top?) (= move-to :top))
+                (and block-content? (not= move-to :nested))
+                (and (not block-content?)
+                     (seq (:block/children block))
+                     (= move-to :nested)))
           (dnd-separator move-to block-content?))))))
 
 (defn clock-summary-cp
@@ -1843,12 +1839,12 @@
                          :on-mouse-down ; TODO: it seems that Safari doesn't work well with on-mouse-down
                          )
         attrs (cond->
-                  {:blockid       (str uuid)
-                   :data-type (name block-type)
-                   :style {:width "100%"}}
-                  (not block-ref?)
-                  (assoc mouse-down-key (fn [e]
-                                          (block-content-on-mouse-down e block block-id content edit-input-id))))]
+                {:blockid       (str uuid)
+                 :data-type (name block-type)
+                 :style {:width "100%"}}
+                (not block-ref?)
+                (assoc mouse-down-key (fn [e]
+                                        (block-content-on-mouse-down e block block-id content edit-input-id))))]
     [:div.block-content.inline
      (cond-> {:id (str "block-content-" uuid)
               :on-mouse-up (fn [_e]
@@ -2023,16 +2019,16 @@
                                                                  (:block/pre-block? block)
                                                                  content)]
                                  [block
-                                 (if (seq title)
-                                   (->elem :span (map-inline config title))
-                                   (->elem :div (markup-elements-cp config body)))]))))
+                                  (if (seq title)
+                                    (->elem :span (map-inline config title))
+                                    (->elem :div (markup-elements-cp config body)))]))))
             breadcrumb (->> (into [] parents-props)
                             (concat [page-name-props] (when more? [:more]))
                             (filterv identity)
                             (map (fn [x] (if (vector? x)
-                                          (let [[block label] x]
-                                            (breadcrumb-fragment config block label))
-                                          [:span.opacity-70 "⋯"])))
+                                           (let [[block label] x]
+                                             (breadcrumb-fragment config block label))
+                                           [:span.opacity-70 "⋯"])))
                             (interpose (breadcrumb-separator)))]
         [:div.block-parents.flex-row.flex-1
          {:class (when (seq breadcrumb)
@@ -2144,31 +2140,13 @@
      children)
     (distinct @refs)))
 
-;; (rum/defc block-immediate-children < rum/reactive
-;;   [repo config uuid ref? collapsed?]
-;;   (when (and ref? (not collapsed?))
-;;     (let [children (db/get-block-immediate-children repo uuid)
-;;           children (block-handler/filter-blocks repo children (:filters config) false)]
-;;       (when (seq children)
-;;         [:div.ref-children {:style {:margin-left "1.8rem"}}
-;;          (blocks-container children (assoc config
-;;                                            :breadcrumb-show? false
-;;                                            :ref? true
-;;                                            :ref-child? true))]))))
-
 (rum/defcs block-container < rum/reactive
   {:init (fn [state]
-           (let [[config block] (:rum/args state)
-                 ref-collpased? (boolean
-                                 (and
-                                  (seq (:block/children block))
-                                  (or (:custom-query? config)
-                                      (and (:ref? config)
-                                           (>= (:ref/level block)
-                                               (state/get-ref-open-blocks-level))))))]
+           (let [[config block] (:rum/args state)]
              (assoc state
+                    ::init-collapsed? (get-in block [:block/properties :collapsed])
                     ::control-show? (atom false)
-                    ::ref-collapsed? (atom ref-collpased?))))
+                    ::default-collapsed? (atom (editor-handler/block-default-collapsed? block config)))))
    :should-update (fn [old-state new-state]
                     (let [compare-keys [:block/uuid :block/properties
                                         :block/parent :block/left
@@ -2181,7 +2159,8 @@
                        (not= (select-keys (first (:rum/args old-state)) config-compare-keys)
                              (select-keys (first (:rum/args new-state)) config-compare-keys)))))}
   [state config {:block/keys [uuid repo children pre-block? top? properties refs heading-level level type format content] :as block}]
-  (let [block (merge block (block/parse-title-and-body uuid format pre-block? content))
+  (let [init-collapsed? (::init-collapsed? state)
+        block (merge block (block/parse-title-and-body uuid format pre-block? content))
         body (:block/body block)
         blocks-container-id (:blocks-container-id config)
         config (update config :block merge block)
@@ -2191,9 +2170,10 @@
                  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))
+        *default-collapsed? (get state ::default-collapsed?)
+        collapsed? (if (not= init-collapsed? (:collapsed properties))
+                     (:collapsed properties)
+                     @*default-collapsed?)
         ref? (boolean (:ref? config))
         breadcrumb-show? (:breadcrumb-show? config)
         slide? (boolean (:slide? config))
@@ -2217,15 +2197,15 @@
         review-cards? (:review-cards? config)]
     [:div.ls-block
      (cond->
-      {:id block-id
-       :data-refs data-refs
-       :data-refs-self data-refs-self
-       :data-collapsed (and collapsed? has-child?)
-       :class (str uuid
-                   (when pre-block? " pre-block")
-                   (when (and card? (not review-cards?)) " shadow-xl"))
-       :blockid (str uuid)
-       :haschild (str has-child?)}
+       {:id block-id
+        :data-refs data-refs
+        :data-refs-self data-refs-self
+        :data-collapsed (and collapsed? has-child?)
+        :class (str uuid
+                    (when pre-block? " pre-block")
+                    (when (and card? (not review-cards?)) " shadow-xl"))
+        :blockid (str uuid)
+        :haschild (str has-child?)}
 
        level
        (assoc :level level)
@@ -2257,11 +2237,11 @@
        :on-mouse-leave (fn [e]
                          (block-mouse-leave e *control-show? block-id doc-mode?))}
       (when (not slide?)
-        (block-control config block uuid block-id body children collapsed? *ref-collapsed? *control-show? edit?))
+        (block-control config block uuid block-id body children collapsed? *control-show? edit?))
 
       (block-content-or-editor config block edit-input-id block-id heading-level edit?)]
 
-     (block-children config children collapsed? *ref-collapsed?)
+     (block-children config children collapsed? *default-collapsed?)
 
      (dnd-separator-wrapper block block-id slide? false false)]))
 
@@ -2328,7 +2308,7 @@
         (->elem
          :li
          (cond->
-          {:checked checked?}
+           {:checked checked?}
            number
            (assoc :value number))
          (vec-cat

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

@@ -67,16 +67,8 @@
           (editor-handler/edit-block! block :max (:block/uuid block))))))
   state)
 
-(defn- expand-top-block!
-  [state]
-  (when-let [block-id (last (:rum/args state))]
-    (when-let [block (db/get-block-by-uuid block-id)]
-      (when (get-in block [:block/properties :collapsed])
-        (editor-handler/expand-block! block-id))))
-  state)
-
 (rum/defc page-blocks-inner <
-  {:did-mount  #(-> % open-first-block! expand-top-block!)
+  {:did-mount  open-first-block!
    :did-update open-first-block!}
   [page-name page-blocks hiccup sidebar? preview? block-uuid]
   [:div.page-blocks-inner {:style {:margin-left (if sidebar? 0 -20)}}

+ 28 - 0
src/main/frontend/handler/editor.cljs

@@ -3566,3 +3566,31 @@
       (save-block! (state/get-current-repo)
                    (:block/uuid block)
                    content))))
+
+(defn block-default-collapsed?
+  "Whether a block should be collapsed by default.
+  Currently, this handles several cases:
+  1. Zoom in mode, it will open the current block if it's collapsed.
+  2. Queries.
+  3. References."
+  [block config]
+  (let [collapsed? (cond
+                     (and (:block? config)
+                          (= (:id config) (str (:block/uuid block))))
+                     false
+
+                     (and (:block? config)
+                          (get-in block [:block/properties :collapsed]))
+                     true
+
+                     :else
+                     (boolean
+                      (and
+                       (seq (:block/children block))
+                       (or (:custom-query? config)
+                           (and (:ref? config)
+                                (>= (:ref/level block)
+                                    (state/get-ref-open-blocks-level)))))))]
+    (if (or (:ref? config) (:block? config))
+      collapsed?
+      (get-in block [:block/properties :collapsed]))))