Selaa lähdekoodia

fix: relax same block id check only when deleting a source block

Tienson Qin 2 vuotta sitten
vanhempi
sitoutus
9cfc2f2d9e
1 muutettua tiedostoa jossa 29 lisäystä ja 12 poistoa
  1. 29 12
      src/main/frontend/handler/editor.cljs

+ 29 - 12
src/main/frontend/handler/editor.cljs

@@ -233,6 +233,13 @@
     (doseq [block blocks]
     (doseq [block blocks]
       (gdom-classes/remove block "block-highlight"))))
       (gdom-classes/remove block "block-highlight"))))
 
 
+;; id: block dom id, "ls-block-counter-uuid"
+(defn- another-block-with-same-id-exists?
+  [current-id block-id]
+  (when-let [id (and (string? block-id) (parse-uuid block-id))]
+    (and (not= current-id id)
+         (db/entity [:block/uuid id]))))
+
 (defn- remove-non-existed-refs!
 (defn- remove-non-existed-refs!
   [refs]
   [refs]
   (remove (fn [x] (or
   (remove (fn [x] (or
@@ -376,14 +383,25 @@
   ([block value
   ([block value
     {:keys [force?]
     {:keys [force?]
      :as opts}]
      :as opts}]
-   (let [{:block/keys [page format repo content]} block
+   (let [{:block/keys [uuid page format repo content properties]} block
          repo (or repo (state/get-current-repo))
          repo (or repo (state/get-current-repo))
          format (or format (state/get-preferred-format))
          format (or format (state/get-preferred-format))
          page (db/entity repo (:db/id page))
          page (db/entity repo (:db/id page))
+         block-id (when (map? properties) (get properties :id))
          content (-> (property/remove-built-in-properties format content)
          content (-> (property/remove-built-in-properties format content)
                      (drawer/remove-logbook))]
                      (drawer/remove-logbook))]
-     (if force?
+     (cond
+       (and (another-block-with-same-id-exists? uuid block-id)
+            (not (= :delete (:editor/op opts))))
+       (notification/show!
+        [:p.content
+         (util/format "Block with the id %s already exists!" block-id)]
+        :error)
+
+       force?
        (save-block-inner! block value opts)
        (save-block-inner! block value opts)
+
+       :else
        (let [content-changed? (not= (string/trim content) (string/trim value))]
        (let [content-changed? (not= (string/trim content) (string/trim value))]
          (when (and content-changed? page)
          (when (and content-changed? page)
            (save-block-inner! block value opts)))))))
            (save-block-inner! block value opts)))))))
@@ -811,7 +829,7 @@
                                                   :block/additional-properties (:block/properties block))
                                                   :block/additional-properties (:block/properties block))
                                            prev-block)]
                                            prev-block)]
                          (delete-block-aux! block delete-children?)
                          (delete-block-aux! block delete-children?)
-                         (save-block! repo prev-block' new-content))
+                         (save-block! repo prev-block' new-content {:editor/op :delete}))
                        (delete-block-aux! block delete-children?)))
                        (delete-block-aux! block delete-children?)))
                    (move-fn)))))))))
                    (move-fn)))))))))
    (state/set-editor-op! nil)))
    (state/set-editor-op! nil)))
@@ -1231,20 +1249,19 @@
 (defn save-block!
 (defn save-block!
   ([repo block-or-uuid content]
   ([repo block-or-uuid content]
     (save-block! repo block-or-uuid content {}))
     (save-block! repo block-or-uuid content {}))
-  ([repo block-or-uuid content {:keys [properties] :or {}}]
+  ([repo block-or-uuid content {:keys [properties] :as opts}]
    (let [block (if (or (uuid? block-or-uuid)
    (let [block (if (or (uuid? block-or-uuid)
                        (string? block-or-uuid))
                        (string? block-or-uuid))
                  (db-model/query-block-by-uuid block-or-uuid) block-or-uuid)]
                  (db-model/query-block-by-uuid block-or-uuid) block-or-uuid)]
      (save-block!
      (save-block!
-       {:block block :repo repo}
-       (if (seq properties)
-          (property/insert-properties (:block/format block) content properties)
-        content)
-     )))
-  ([{:keys [block repo] :as _state} value]
+      {:block block :repo repo :opts (dissoc opts :properties)}
+      (if (seq properties)
+        (property/insert-properties (:block/format block) content properties)
+        content))))
+  ([{:keys [block repo opts] :as _state} value]
    (let [repo (or repo (state/get-current-repo))]
    (let [repo (or repo (state/get-current-repo))]
      (when (db/entity repo [:block/uuid (:block/uuid block)])
      (when (db/entity repo [:block/uuid (:block/uuid block)])
-       (save-block-aux! block value {})))))
+       (save-block-aux! block value opts)))))
 
 
 (defn save-blocks!
 (defn save-blocks!
   [blocks]
   [blocks]
@@ -2626,7 +2643,7 @@
                           edit-block)]
                           edit-block)]
         (outliner-tx/transact! transact-opts
         (outliner-tx/transact! transact-opts
           (delete-block-aux! next-block false)
           (delete-block-aux! next-block false)
-          (save-block! repo edit-block' new-content))
+          (save-block! repo edit-block' new-content {:editor/op :delete}))
         (let [block (if next-block-has-refs? next-block edit-block)]
         (let [block (if next-block-has-refs? next-block edit-block)]
           (edit-block! block current-pos (:block/uuid block)))))))
           (edit-block! block current-pos (:block/uuid block)))))))