Browse Source

fix: restore collapse behaviour

related to #4583
Tienson Qin 3 years ago
parent
commit
96b1c6788b

+ 6 - 11
src/main/frontend/components/block.cljs

@@ -1420,10 +1420,6 @@
 (rum/defcs block-control < rum/reactive
   [state config block uuid block-id children collapsed? *control-show? edit?]
   (let [doc-mode? (state/sub :document/mode?)
-        has-children-blocks? (and (coll? children) (seq children))
-        has-child? (and
-                    (not (:pre-block? block))
-                    has-children-blocks?)
         control-show? (util/react *control-show?)
         ref? (:ref? config)
         empty-content? (block-content-empty? block)]
@@ -1436,12 +1432,11 @@
       {:id (str "control-" uuid)
        :on-click (fn [event]
                    (util/stop event)
-                   (when-not (and (not collapsed?) (not has-child?))
-                     (if ref?
-                       (state/toggle-collapsed-block! uuid)
-                       (if collapsed?
-                         (editor-handler/expand-block! uuid)
-                         (editor-handler/collapse-block! uuid)))))}
+                   (if ref?
+                     (state/toggle-collapsed-block! uuid)
+                     (if collapsed?
+                       (editor-handler/expand-block! uuid)
+                       (editor-handler/collapse-block! uuid))))}
       [:span {:class (if control-show? "control-show cursor-pointer" "control-hide")}
        (ui/rotating-arrow collapsed?)]]
      (let [bullet [:a {:on-click (fn [event]
@@ -2116,7 +2111,7 @@
   (util/stop e)
   (when (or
          (model/block-collapsed? uuid)
-         (editor-handler/collapsable? uuid))
+         (editor-handler/collapsable? uuid true))
     (reset! *control-show? true))
   (when-let [parent (gdom/getElement block-id)]
     (let [node (.querySelector parent ".bullet-container")]

+ 2 - 6
src/main/frontend/db/model.cljs

@@ -424,12 +424,8 @@
               next-sibling (get ids->blocks [(:db/id (:block/parent node)) id])
               next-siblings (if (and next-sibling child-block)
                               (cons next-sibling next-siblings)
-                              next-siblings)
-              collapsed? (:block/collapsed? node)]
-          (if-let [node (and
-                         (or (not collapsed?)
-                             (= (:db/id node) (:db/id parent)))
-                         (or child-block next-sibling))]
+                              next-siblings)]
+          (if-let [node (or child-block next-sibling)]
             (recur node next-siblings (conj result node))
             (if-let [sibling (first next-siblings)]
               (recur sibling (rest next-siblings) (conj result sibling))

+ 22 - 7
src/main/frontend/handler/editor.cljs

@@ -3367,13 +3367,28 @@
     (util/forward-kill-word input)
     (state/set-edit-content! (state/get-edit-input-id) (.-value input))))
 
-(defn collapsable? [block-id]
-  (when block-id
-    (if-let [block (db-model/query-block-by-uuid block-id)]
-      (and
-       (not (util/collapsed? block))
-       (db-model/has-children? block-id))
-      false)))
+(defn block-with-title?
+  [format content semantic?]
+  (and (string/includes? content "\n")
+       (if semantic?
+         (let [ast (mldoc/->edn content (mldoc/default-config format))
+               first-elem-type (first (ffirst ast))]
+           (mldoc/block-with-title? first-elem-type))
+         true)))
+
+(defn collapsable?
+  ([block-id]
+   (collapsable? block-id false))
+  ([block-id semantic?]
+   (when block-id
+     (if-let [block (db-model/query-block-by-uuid block-id)]
+       (and
+        (not (util/collapsed? block))
+        (or (db-model/has-children? block-id)
+            (block-with-title? (:block/format block)
+                               (:block/content block)
+                               semantic?)))
+       false))))
 
 (defn all-blocks-with-level
   "Return all blocks associated with correct level