Don't allow deleting the block when there's only two blocks

pull/645/head
Tienson Qin 2020-09-07 09:10:44 +08:00
parent 06addeb113
commit 07856027eb
3 changed files with 36 additions and 30 deletions

View File

@ -90,8 +90,8 @@
(fn [chosen _click?] (fn [chosen _click?]
(state/set-editor-show-page-search false) (state/set-editor-show-page-search false)
(editor-handler/insert-command! id (editor-handler/insert-command! id
(util/format "#%s" (if (string/includes? chosen " ") (util/format "#%s" (if (string/includes? chosen " ")
(str "[[" chosen "]]") (str "[[" chosen "]]")
chosen)) chosen))
format format
{:last-pattern (str "#" (if @editor-handler/*selected-text "" q))})) {:last-pattern (str "#" (if @editor-handler/*selected-text "" q))}))
@ -408,11 +408,10 @@
selected-start (gobj/get node "selectionStart") selected-start (gobj/get node "selectionStart")
selected-end (gobj/get node "selectionEnd")] selected-end (gobj/get node "selectionEnd")]
(cond (cond
(not= selected-start selected-end) (not= selected-start selected-end)
nil nil
(zero? current-pos) (and (zero? current-pos))
(editor-handler/delete-block! state repo e) (editor-handler/delete-block! state repo e)
(and (> current-pos 1) (and (> current-pos 1)
@ -449,7 +448,7 @@
:else :else
nil)) nil))
;; deleting hashtag ;; deleting hashtag
(and (= deleted "#") (state/get-editor-show-page-search-hashtag)) (and (= deleted "#") (state/get-editor-show-page-search-hashtag))
(state/set-editor-show-page-search-hashtag false) (state/set-editor-show-page-search-hashtag false)
@ -478,7 +477,7 @@
(commands/handle-step [:editor/search-page]) (commands/handle-step [:editor/search-page])
(reset! commands/*slash-caret-pos (util/get-caret-pos input))) (reset! commands/*slash-caret-pos (util/get-caret-pos input)))
(and (and
(not= key-code 8) ;; backspace (not= key-code 8) ;; backspace
(or (or
(editor-handler/surround-by? input "#" " ") (editor-handler/surround-by? input "#" " ")
@ -488,7 +487,7 @@
(commands/handle-step [:editor/search-page-hashtag]) (commands/handle-step [:editor/search-page-hashtag])
(state/set-last-pos! (:pos (util/get-caret-pos input))) (state/set-last-pos! (:pos (util/get-caret-pos input)))
(reset! commands/*slash-caret-pos (util/get-caret-pos input))) (reset! commands/*slash-caret-pos (util/get-caret-pos input)))
(and (and
(= key " ") (= key " ")
(state/get-editor-show-page-search-hashtag)) (state/get-editor-show-page-search-hashtag))

View File

@ -950,6 +950,11 @@
nil) nil)
react))))) react)))))
(defn get-page-blocks-count
[repo page-id]
(when-let [db (get-conn repo)]
(count (d/datoms db :avet :block/page page-id))))
(defn get-page-directives-content (defn get-page-directives-content
[page] [page]
(let [blocks (get-page-blocks page)] (let [blocks (get-page-blocks page)]

View File

@ -289,8 +289,8 @@
(reset! last-child-end-pos old-end-pos))) (reset! last-child-end-pos old-end-pos)))
(cond-> (cond->
{:block/uuid uuid {:block/uuid uuid
:block/meta new-meta} :block/meta new-meta}
(and (some? indent-left?) (not @next-leq-level?)) (and (some? indent-left?) (not @next-leq-level?))
(assoc :block/level (if indent-left? (dec level) (inc level))) (assoc :block/level (if indent-left? (dec level) (inc level)))
(and new-content (not @next-leq-level?)) (and new-content (not @next-leq-level?))
@ -713,27 +713,29 @@
[state repo e] [state repo e]
(let [{:keys [id block-id block-parent-id dummy? value pos format]} (get-state state)] (let [{:keys [id block-id block-parent-id dummy? value pos format]} (get-state state)]
(when block-id (when block-id
(do (when-let [page-id (:db/id (:block/page (db/entity [:block/uuid block-id])))]
(util/stop e) (let [page-blocks-count (db/get-page-blocks-count repo page-id)]
;; delete block, edit previous block (when (> page-blocks-count 2)
(let [block (db/pull [:block/uuid block-id]) (util/stop e)
block-parent (gdom/getElement block-parent-id) ;; delete block, edit previous block
sibling-block (get-prev-block-non-collapsed block-parent)] (let [block (db/pull [:block/uuid block-id])
(delete-block-aux! block dummy?) block-parent (gdom/getElement block-parent-id)
(when sibling-block sibling-block (get-prev-block-non-collapsed block-parent)]
(when-let [sibling-block-id (d/attr sibling-block "blockid")] (delete-block-aux! block dummy?)
(when repo (when sibling-block
(when-let [block (db/pull repo '[*] [:block/uuid (uuid sibling-block-id)])] (when-let [sibling-block-id (d/attr sibling-block "blockid")]
(let [original-content (util/trim-safe (:block/content block)) (when repo
new-value (str original-content value) (when-let [block (db/pull repo '[*] [:block/uuid (uuid sibling-block-id)])]
pos (max (let [original-content (util/trim-safe (:block/content block))
(if original-content new-value (str original-content value)
(utf8/length (utf8/encode (text/remove-level-spaces original-content format))) pos (max
0) (if original-content
0)] (utf8/length (utf8/encode (text/remove-level-spaces original-content format)))
(save-block-if-changed! block new-value) 0)
(edit-block! (uuid sibling-block-id) 0)]
pos format id))))))))))) (save-block-if-changed! block new-value)
(edit-block! (uuid sibling-block-id)
pos format id)))))))))))))
(defn delete-blocks! (defn delete-blocks!
[repo block-uuids] [repo block-uuids]