Browse Source

enhance: no need to create empty value

The editor state will be restored after setting a property or exiting
the property editing.
Tienson Qin 1 year ago
parent
commit
18a36f856d

+ 25 - 32
src/main/frontend/components/property.cljs

@@ -175,27 +175,17 @@
             (when *property-schema
               (swap! *property-schema update-schema-fn))
             (let [schema (or (and *property-schema @*property-schema)
-                             (update-schema-fn property-schema))
-                  repo (state/get-current-repo)]
+                             (update-schema-fn property-schema))]
               (p/let [property' (when block (<add-property-from-dropdown block property-name schema opts))
                       property (or property' property)
                       add-class-property? (and (contains? (:block/type block) "class") page-configure? class-schema?)]
                 (p/do!
                  (when *show-new-property-config? (reset! *show-new-property-config? false))
                  (components-pu/update-property! property property-name schema)
-                 (when block
-                   (if (= type :default)
-                     (when (and (not add-class-property?)
-
-                                (not (seq (:property/closed-values property))))
-                       (pv/<create-new-block! block property ""))
-                     (p/do!
-                      (property-handler/set-block-property! repo (:block/uuid block)
-                                                            (:db/ident property)
-                                                            (if (= (get-in property [:block/schema :type]) :checkbox)
-                                                              false
-                                                              :logseq.property/empty-placeholder))
-                      (shui/dialog-close!)))))))))}
+                 (when (and block (= type :default)
+                            (not add-class-property?)
+                            (not (seq (:property/closed-values property))))
+                   (pv/<create-new-block! block property "")))))))}
 
 ;; only set when in property configure modal
         (and *property-name (:type property-schema))
@@ -431,8 +421,13 @@
   (rum/local false ::show-new-property-config?)
   (rum/local {} ::property-schema)
   {:will-unmount (fn [state]
-                   (when-let [*property-key (nth (:rum/args state) 1)]
-                     (reset! *property-key nil))
+                   (let [args (:rum/args state)
+                         *property-key (second args)
+                         {:keys [original-block edit-original-block]} (last args)]
+                     (when *property-key (reset! *property-key nil))
+                     (when (and original-block edit-original-block
+                                (not= (:db/id original-block) (:db/id (state/get-edit-block)))) ; new block created
+                       (edit-original-block)))
                    state)}
   shortcut/disable-all-shortcuts
   [state block *property-key {:keys [class-schema? page? page-configure?]
@@ -475,26 +470,24 @@
                            (when property
                              (let [add-class-property? (and (contains? (:block/type block) "class") class-schema?)
                                    type (get-in property [:block/schema :type])]
-                               (p/do!
-                                (when property
-                                  (cond
-                                    add-class-property?
+                               (when property
+                                 (cond
+                                   add-class-property?
+                                   (p/do!
                                     (pv/<add-property! block (:db/ident property) "" {:class-schema? class-schema?
                                                                                       :exit-edit? page-configure?})
+                                    (shui/dialog-close!))
 
-                                    (and (= :default type)
-                                         (not (seq (:property/closed-values property))))
+                                   (and (= :default type)
+                                        (not (seq (:property/closed-values property))))
+                                   (p/do!
                                     (pv/<create-new-block! block property "")
+                                    (shui/dialog-close!))
 
-                                    (or (not= :default type)
-                                        (and (= :default type) (seq (:property/closed-values property))))
-                                    (property-handler/set-block-property! (state/get-current-repo) (:block/uuid block)
-                                                                          (:db/ident property)
-                                                                          (if (= (get-in property [:block/schema :type]) :checkbox)
-                                                                            false
-                                                                            :logseq.property/empty-placeholder))))
-                                (shui/dialog-close!))))))
-
+                                   (or (not= :default type)
+                                       (and (= :default type) (seq (:property/closed-values property))))
+                                   (p/do!
+                                    (reset! *show-new-property-config? false))))))))
              input-opts {}]
          (property-select exclude-properties on-chosen input-opts)))]))
 

+ 6 - 5
src/main/frontend/handler/db_based/property.cljs

@@ -41,11 +41,12 @@
    {:outliner-op :create-property-text-block}
     (outliner-op/create-property-text-block! block-id property-id value opts)))
 
-(defn collapse-expand-block-property!
-  [block-id property-id collapse?]
-  (ui-outliner-tx/transact!
-   {:outliner-op :collapse-expand-block-property}
-   (outliner-op/collapse-expand-block-property! block-id property-id collapse?)))
+(comment
+  (defn collapse-expand-block-property!
+    [block-id property-id collapse?]
+    (ui-outliner-tx/transact!
+     {:outliner-op :collapse-expand-block-property}
+     (outliner-op/collapse-expand-block-property! block-id property-id collapse?))))
 
 (defn batch-set-property!
   [block-id property-id value]

+ 11 - 9
src/main/frontend/handler/events.cljs

@@ -970,18 +970,20 @@
                            (db/entity [:block/uuid (uuid s)])))
          blocks (or (when block [block])
                     edit-block-or-selected
-                    (when current-block [current-block]))]
+                    (when current-block [current-block]))
+         opts' (cond-> opts
+                 editing-block
+                 (assoc :original-block editing-block
+                        :edit-original-block
+                        (fn [{:keys [editing-default-property?]}]
+                          (when (and (not (state/editing?)) editing-block
+                                     (not editing-default-property?))
+                            (editor-handler/edit-block! editing-block (or pos :max))))))]
      (when (seq blocks)
-       (shui/dialog-open! #(property-dialog/dialog blocks opts)
+       (shui/dialog-open! #(property-dialog/dialog blocks opts')
                           {:id :property-dialog
                            :align "start"
-                           :content-props {:onOpenAutoFocus #(.preventDefault %)}
-                           :on-close (fn [_id]
-                                       (when (and (not (state/editing?)) editing-block
-                                                  ;; block not changed
-                                                  (= (:block/tx-id (first blocks))
-                                                     (:block/tx-id (db/entity (:db/id blocks)))))
-                                         (editor-handler/edit-block! editing-block (or pos :max))))})))))
+                           :content-props {:onOpenAutoFocus #(.preventDefault %)}})))))
 
 (rum/defc multi-tabs-dialog
   []

+ 5 - 4
src/main/frontend/modules/outliner/op.cljs

@@ -77,10 +77,11 @@
   (op-transact!
    [:create-property-text-block [block-id property-id value opts]]))
 
-(defn collapse-expand-block-property!
-  [block-id property-id collapse?]
-  (op-transact!
-   [:collapse-expand-block-property [block-id property-id collapse?]]))
+(comment
+  (defn collapse-expand-block-property!
+    [block-id property-id collapse?]
+    (op-transact!
+     [:collapse-expand-block-property [block-id property-id collapse?]])))
 
 (defn batch-set-property!
   [block-ids property-id value]