Browse Source

fix: auto save on editing properties

Junyi Du 3 years ago
parent
commit
cf430963af

+ 5 - 0
src/main/frontend/db/model.cljs

@@ -453,6 +453,11 @@
      (when-let [block (d/entity db [:block/uuid block-id])]
        (:block/parent block)))))
 
+(defn top-block?
+  [block]
+  (= (:db/id (:block/parent block))
+     (:db/id (:block/page block))))
+
 ;; non recursive query
 (defn get-block-parents
   ([repo block-id]

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

@@ -1270,7 +1270,7 @@
 (defn save-current-block!
   ([]
    (save-current-block! {}))
-  ([{:keys [force?] :as opts}]
+  ([{:keys [force? skip-properties?] :as opts}]
    ;; non English input method
    (when-not (state/editor-in-composition?)
      (when (state/get-current-repo)
@@ -1296,6 +1296,11 @@
                force?
                (save-block-aux! db-block value opts)
 
+               (and skip-properties?
+                    (db-model/top-block? block)
+                    (when elem (thingatpt/properties-at-point elem)))
+               nil
+
                (and block value db-content-without-heading
                     (not= (string/trim db-content-without-heading)
                           (string/trim value)))
@@ -1801,17 +1806,14 @@
     (when @*auto-save-timeout
       (js/clearTimeout @*auto-save-timeout))
     (mark-last-input-time! repo)
-    (when-not
-     (and
-      (= (:db/id (:block/parent block))
-         (:db/id (:block/page block)))            ; don't auto-save for page's properties block
-      (get-in block [:block/properties :title]))
+    (when-not (and (db-model/top-block? block)            ; don't auto-save for page's properties block
+                   (not-empty (:block/properties block)))
       (reset! *auto-save-timeout
               (js/setTimeout
                (fn []
                  (when (state/input-idle? repo)
                    (state/set-editor-op! :auto-save)
-                   (save-current-block! {})
+                   (save-current-block! {:skip-properties? true})
                    (state/set-editor-op! nil)))
                500)))))
 

+ 7 - 3
src/main/frontend/util/thingatpt.cljs

@@ -63,9 +63,11 @@
   (when-let [macro (thing-at-point ["{{embed" "}}"] input)]
     (assoc macro :type "macro")))
 
+;; TODO support markdown YAML front matter
+;; TODO support using org style properties in markdown
 (defn properties-at-point [& [input]]
   (when-let [properties
-             (case (state/get-preferred-format)
+             (case (state/get-preferred-format) ;; TODO fix me to block's format
                :org (thing-at-point
                      [property-util/properties-start
                       property-util/properties-end]
@@ -75,10 +77,12 @@
                    line)))]
     (assoc properties :type "properties-drawer")))
 
+;; TODO support markdown YAML front matter
+;; TODO support using org style properties in markdown
 (defn property-key-at-point [& [input]]
   (when (properties-at-point input)
     (let [property
-          (case (state/get-preferred-format)
+          (case (state/get-preferred-format) ;; TODO fix me to block's format
             :org (thing-at-point ":" input "\n")
             (when-let [line (:raw-content (line-at-point input))]
               (let [key (first (string/split line "::"))
@@ -109,7 +113,7 @@
                :ordered (int? bullet))))))
 
 (defn- get-markup-at-point [& [input]]
-  (let [format (state/get-preferred-format)]
+  (let [format (state/get-preferred-format)] ;; TODO fix me to block's format
    (or (thing-at-point (config/get-hr format) input)
        (thing-at-point (config/get-bold format) input)
        (thing-at-point (config/get-italic format) input)