Skip to content
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: editor freeze when cut and paste blocks at the bottom #5014

Merged
merged 3 commits into from Apr 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
62 changes: 32 additions & 30 deletions src/main/frontend/handler/editor.cljs
Expand Up @@ -885,23 +885,20 @@
(state/set-editor-op! nil)))

(defn delete-blocks!
[repo dom-blocks]
(let [block-uuids (distinct (map #(uuid (dom/attr % "blockid")) dom-blocks))]
(when (seq block-uuids)
(let [uuid->dom-block (zipmap block-uuids dom-blocks)
lookup-refs (map (fn [id] [:block/uuid id]) block-uuids)
blocks (db/pull-many repo '[*] lookup-refs)
block (first blocks)
block-parent (get uuid->dom-block (:block/uuid block))
sibling-block (when block-parent (util/get-prev-block-non-collapsed-non-embed block-parent))]
(outliner-tx/transact!
{:outliner-op :delete-blocks}
(outliner-core/delete-blocks! blocks {}))
(when sibling-block
(move-to-prev-block repo sibling-block
(:block/format block)
(dom/attr sibling-block "id")
""))))))
[repo block-uuids blocks dom-blocks]
(when (seq block-uuids)
(let [uuid->dom-block (zipmap block-uuids dom-blocks)
block (first blocks)
block-parent (get uuid->dom-block (:block/uuid block))
sibling-block (when block-parent (util/get-prev-block-non-collapsed-non-embed block-parent))]
(outliner-tx/transact!
{:outliner-op :delete-blocks}
(outliner-core/delete-blocks! blocks {}))
(when sibling-block
(move-to-prev-block repo sibling-block
(:block/format block)
(dom/attr sibling-block "id")
"")))))

(defn- batch-set-block-property!
"col: a collection of [block-id property-key property-value]."
Expand Down Expand Up @@ -1019,9 +1016,8 @@
[]
(when-let [blocks (seq (state/get-selection-blocks))]
(let [repo (state/get-current-repo)
ids (->> (distinct (map #(when-let [id (dom/attr % "blockid")]
(uuid id)) blocks))
(remove nil?))
ids (distinct (keep #(when-let [id (dom/attr % "blockid")]
(uuid id)) blocks))
content (compose-copied-blocks-contents repo ids)
block (db/entity [:block/uuid (first ids)])]
(when block
Expand Down Expand Up @@ -1097,12 +1093,16 @@
(when copy? (copy-selection-blocks))
(when-let [blocks (seq (get-selected-blocks))]
;; remove embeds, references and queries
(let [blocks (remove (fn [block]
(let [dom-blocks (remove (fn [block]
(or (= "true" (dom/attr block "data-transclude"))
(= "true" (dom/attr block "data-query")))) blocks)]
(when (seq blocks)
(let [repo (state/get-current-repo)]
(delete-blocks! repo blocks))))))
(when (seq dom-blocks)
(let [repo (state/get-current-repo)
block-uuids (distinct (map #(uuid (dom/attr % "blockid")) dom-blocks))
lookup-refs (map (fn [id] [:block/uuid id]) block-uuids)
blocks (db/pull-many repo '[*] lookup-refs)]
(state/set-copied-full-blocks nil blocks)
(delete-blocks! repo block-uuids blocks dom-blocks))))))

(def url-regex
"Didn't use link/plain-link as it is incorrectly detects words as urls."
Expand Down Expand Up @@ -1236,6 +1236,7 @@
(let [repo (state/get-current-repo)
;; TODO: support org mode
md-content (compose-copied-blocks-contents repo [block-id])]
(state/set-copied-full-blocks md-content [block])
(common-handler/copy-to-clipboard-without-id-property! (:block/format block) md-content)
(delete-block-aux! block true))))

Expand Down Expand Up @@ -2867,7 +2868,7 @@
(if (seq ids)
(let [blocks (db/get-block-and-children repo (first ids))
result (vec (concat result blocks))]
(recur (remove (set (map :block/uuid result)) ids) result))
(recur (remove (set (map :block/uuid result)) (rest ids)) result))
result)))

(defn- paste-text
Expand All @@ -2877,11 +2878,12 @@
input (state/get-input)
*stop-event? (atom true)]
(cond
(and (seq copied-block-ids)
(:copy/content copied-blocks)
(not (string/blank? text))
(= (string/replace (string/trim text) "\r" "")
(string/replace (string/trim (:copy/content copied-blocks)) "\r" "")))
(or (seq copied-block-ids)
(seq (:copy/full-blocks copied-blocks))
(and text
(:copy/content copied-blocks)
(= (string/replace (string/trim text) "\r" "")
(string/replace (string/trim (:copy/content copied-blocks)) "\r" ""))))
(let [blocks (or
(:copy/full-blocks copied-blocks)
(get-all-blocks-by-ids (state/get-current-repo) copied-block-ids))]
Expand Down
5 changes: 5 additions & 0 deletions src/main/frontend/state.cljs
Expand Up @@ -1477,6 +1477,11 @@
:copy/block-ids ids
:copy/full-blocks nil}))

(defn set-copied-full-blocks
[content blocks]
(set-state! :copy/blocks {:copy/content content
:copy/full-blocks blocks}))

(defn set-copied-full-blocks!
[blocks]
(set-state! [:copy/blocks :copy/full-blocks] blocks))
Expand Down