Quellcode durchsuchen

fix(editor): handle save editing block timestamp

Fix #8238
Andelf vor 3 Jahren
Ursprung
Commit
cfe28ca88a
2 geänderte Dateien mit 24 neuen und 6 gelöschten Zeilen
  1. 9 4
      src/main/frontend/components/datetime.cljs
  2. 15 2
      src/main/frontend/handler/editor.cljs

+ 9 - 4
src/main/frontend/components/datetime.cljs

@@ -97,12 +97,17 @@
         text (repeated/timestamp-map->text timestamp)
         block-data (state/get-timestamp-block)
         {:keys [block typ show?]} block-data
+        editing-block-id (:block/uuid (state/get-edit-block))
         block-id (or (:block/uuid block)
-                     (:block/uuid (state/get-edit-block)))
+                     editing-block-id)
         typ (or @commands/*current-command typ)]
-    (editor-handler/set-block-timestamp! block-id
-                                         typ
-                                         text)
+    (if (and (state/editing?) (= editing-block-id block-id))
+      (editor-handler/set-editing-block-timestamp! typ
+                                                   text)
+      (editor-handler/set-block-timestamp! block-id
+                                           typ
+                                           text))
+
     (when show?
       (reset! show? false)))
   (clear-timestamp!)

+ 15 - 2
src/main/frontend/handler/editor.cljs

@@ -887,9 +887,8 @@
 
 (defn set-block-timestamp!
   [block-id key value]
-  (let [key (string/lower-case key)
+  (let [key (string/lower-case (str key))
         block-id (if (string? block-id) (uuid block-id) block-id)
-        key (string/lower-case (str key))
         value (str value)]
     (when-let [block (db/pull [:block/uuid block-id])]
       (let [{:block/keys [content]} block
@@ -903,6 +902,20 @@
               (state/set-edit-content! input-id new-content)
               (save-block-if-changed! block new-content))))))))
 
+(defn set-editing-block-timestamp!
+  "Almost the same as set-block-timestamp! except for:
+   - it doesn't save the block
+   - it extracts current content from current input"
+  [key value]
+  (let [key (string/lower-case (str key))
+        value (str value)
+        content (state/get-edit-content)
+        new-content (-> (text-util/remove-timestamp content key)
+                        (text-util/add-timestamp key value))]
+    (when (not= content new-content)
+      (let [input-id (state/get-edit-input-id)]
+        (state/set-edit-content! input-id new-content)))))
+
 (defn set-blocks-id!
   "Persist block uuid to file if the uuid is valid, and it's not persisted in file.
    Accepts a list of uuids."