-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix: Delete and backspace deleting block ids and breaking references #8974
Merged
Merged
Changes from 10 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
04f128f
press delete at the end of a block, if no refs in current block,
megayu 747d8d0
enhance backspace behavior to avoid broke ref
megayu 2f73aa6
fix press backspace at the end of the block of the end of the page issue
megayu f5ef095
fix pass wrong parameter type
megayu 7aa9b90
add e2e-test for delete and backspace across blocks
megayu d8ab6e0
add more wait time to ensure passing e2e-test
megayu fe772c7
add more wait time to ensure passing e2e-test
megayu 75db5f8
Merge branch 'master' into enhance/delete-backspace
cnrpman 7e9b2a9
Merge branch 'master' into enhance/delete-backspace
tiensonqin b7d01b1
Merge branch 'master' into enhance/delete-backspace
logseq-cldwalker e187640
Merge branch 'master' into enhance/delete-backspace
tiensonqin 6073078
make block-has-no-ref? simpler
megayu File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -362,6 +362,7 @@ | |
block | ||
(dissoc block :block/pre-block?)) | ||
block (update block :block/refs remove-non-existed-refs!) | ||
block (if (and left (not= (:block/left block) left)) (assoc block :block/left left) block) | ||
new-properties (merge | ||
(select-keys properties (property/hidden-properties)) | ||
(:block/properties block))] | ||
|
@@ -778,41 +779,49 @@ | |
(outliner-core/delete-blocks! [block] {:children? children?}))))) | ||
|
||
(defn- move-to-prev-block | ||
[repo sibling-block format id value] | ||
(when (and repo sibling-block) | ||
(when-let [sibling-block-id (dom/attr sibling-block "blockid")] | ||
(when-let [block (db/pull repo '[*] [:block/uuid (uuid sibling-block-id)])] | ||
(let [original-content (util/trim-safe (:block/content block)) | ||
value' (-> (property/remove-built-in-properties format original-content) | ||
(drawer/remove-logbook)) | ||
new-value (str value' value) | ||
tail-len (count value) | ||
pos (max | ||
(if original-content | ||
(gobj/get (utf8/encode original-content) "length") | ||
0) | ||
0)] | ||
(edit-block! block pos id | ||
{:custom-content new-value | ||
:tail-len tail-len | ||
:move-cursor? false}) | ||
{:prev-block block | ||
:new-content new-value}))))) | ||
([repo sibling-block format id value] | ||
(move-to-prev-block repo sibling-block format id value true)) | ||
([repo sibling-block format id value edit?] | ||
(when (and repo sibling-block) | ||
(when-let [sibling-block-id (dom/attr sibling-block "blockid")] | ||
(when-let [block (db/pull repo '[*] [:block/uuid (uuid sibling-block-id)])] | ||
(let [original-content (util/trim-safe (:block/content block)) | ||
value' (-> (property/remove-built-in-properties format original-content) | ||
(drawer/remove-logbook)) | ||
new-value (str value' value) | ||
tail-len (count value) | ||
pos (max | ||
(if original-content | ||
(gobj/get (utf8/encode original-content) "length") | ||
0) | ||
0)] | ||
(when edit? | ||
(edit-block! block pos id | ||
{:custom-content new-value | ||
:tail-len tail-len | ||
:move-cursor? false})) | ||
{:prev-block block | ||
:new-content new-value | ||
:pos pos})))))) | ||
|
||
(declare save-block!) | ||
|
||
(defn- block-has-no-ref? | ||
[block-or-uuid] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Optional: It could be nice to keep this fn simpler and have it just take an entity id (eid) like db/pull or https://cljdoc.org/d/datascript/datascript/1.4.2/api/datascript.core#entity. This would mean passing in the |
||
(empty? (:block/_refs (db/entity (if (uuid? block-or-uuid) [:block/uuid block-or-uuid] (:db/id block-or-uuid)))))) | ||
|
||
(defn delete-block! | ||
([repo] | ||
(delete-block! repo true)) | ||
([repo delete-children?] | ||
(state/set-editor-op! :delete) | ||
(let [{:keys [id block-id block-parent-id value format]} (get-state)] | ||
(when block-id | ||
(let [page-id (:db/id (:block/page (db/entity [:block/uuid block-id]))) | ||
(let [block (db/entity [:block/uuid block-id]) | ||
page-id (:db/id (:block/page block)) | ||
page-blocks-count (and page-id (db/get-page-blocks-count repo page-id))] | ||
(when (> page-blocks-count 1) | ||
(let [block (db/entity [:block/uuid block-id]) | ||
has-children? (seq (:block/_parent block)) | ||
(let [has-children? (seq (:block/_parent block)) | ||
block (db/pull (:db/id block)) | ||
left (tree/-get-left (outliner-core/block block)) | ||
left-has-children? (and left | ||
|
@@ -822,18 +831,30 @@ | |
(when-not (and has-children? left-has-children?) | ||
(when block-parent-id | ||
(let [block-parent (gdom/getElement block-parent-id) | ||
;; it's possible to find a block not belong to the same page of current editing block | ||
sibling-block (util/get-prev-block-non-collapsed-non-embed block-parent) | ||
{:keys [prev-block new-content]} (move-to-prev-block repo sibling-block format id value) | ||
delete_prev? (and (not (block-has-no-ref? block)) (block-has-no-ref? (uuid (dom/attr sibling-block "blockid")))) | ||
{:keys [prev-block new-content pos]} (move-to-prev-block repo sibling-block format id value (not delete_prev?)) | ||
concat-prev-block? (boolean (and prev-block new-content)) | ||
save-page? (= (:db/id (:block/page prev-block)) page-id) | ||
transact-opts (cond-> | ||
{:outliner-op :delete-block} | ||
concat-prev-block? | ||
(assoc :concat-data | ||
{:last-edit-block (:block/uuid block)}))] | ||
(outliner-tx/transact! transact-opts | ||
(when concat-prev-block? | ||
(save-block! repo prev-block new-content)) | ||
(delete-block-aux! block delete-children?)))))))))) | ||
(if (and delete_prev? save-page?) | ||
(let [block (assoc block :block/left (:block/left prev-block)) | ||
input (gdom/getElement id)] | ||
(outliner-tx/transact! transact-opts | ||
(delete-block-aux! prev-block false) | ||
(save-block! repo block new-content)) | ||
(state/set-edit-content! id new-content) | ||
(cursor/move-cursor-to input pos)) | ||
(outliner-tx/transact! transact-opts | ||
(when concat-prev-block? | ||
(save-block! repo prev-block new-content)) | ||
(delete-block-aux! block delete-children?))) | ||
)))))))) | ||
(state/set-editor-op! nil))) | ||
|
||
(defn delete-blocks! | ||
|
@@ -2645,11 +2666,9 @@ | |
(util/safe-set-range-text! input "" start end) | ||
(state/set-edit-content! (state/get-edit-input-id) (.-value input))) | ||
|
||
(defn- delete-concat [current-block] | ||
(defn- delete-concat | ||
[current-block input current-pos value] | ||
(let [input-id (state/get-edit-input-id) | ||
^js input (state/get-input) | ||
current-pos (cursor/pos input) | ||
value (gobj/get input "value") | ||
right (outliner-core/get-right-node (outliner-core/block current-block)) | ||
current-block-has-children? (db/has-children? (:block/uuid current-block)) | ||
collapsed? (util/collapsed? current-block) | ||
|
@@ -2664,17 +2683,29 @@ | |
(and (not collapsed?) first-child (db/has-children? (:block/uuid first-child))) | ||
nil | ||
|
||
:else | ||
(and next-block (block-has-no-ref? current-block)) | ||
(let [edit-block (state/get-edit-block) | ||
transact-opts {:outliner-op :delete-block | ||
:concat-data {:last-edit-block (:block/uuid edit-block) | ||
:end? true}} | ||
new-content (str value "" (:block/content next-block)) | ||
next-block (assoc next-block :block/left (:block/left current-block)) | ||
repo (state/get-current-repo)] | ||
(outliner-tx/transact! transact-opts | ||
(save-block! repo edit-block new-content) | ||
(delete-block-aux! next-block false)) | ||
(delete-block-aux! edit-block false) | ||
(save-block! repo next-block new-content)) | ||
(edit-block! next-block current-pos (:block/uuid next-block))) | ||
|
||
:else | ||
(let [edit-block (state/get-edit-block) | ||
transact-opts {:outliner-op :delete-block | ||
:concat-data {:last-edit-block (:block/uuid edit-block) | ||
:end? true}} | ||
new-content (str value "" (:block/content next-block)) | ||
repo (state/get-current-repo)] | ||
(outliner-tx/transact! transact-opts | ||
(save-block! repo edit-block new-content) | ||
(delete-block-aux! next-block false)) | ||
(state/set-edit-content! input-id new-content) | ||
(cursor/move-cursor-to input current-pos))))) | ||
|
||
|
@@ -2696,8 +2727,8 @@ | |
(let [editor-state (get-state) | ||
custom-query? (get-in editor-state [:config :custom-query?])] | ||
(when-not custom-query? | ||
(delete-concat current-block))) | ||
|
||
(delete-concat current-block input current-pos value))) | ||
:else | ||
(delete-and-update input current-pos (inc current-pos)))))) | ||
|
||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
❤️