From bdbf4bad2a6f558b00e79276ef79b7eff8b34850 Mon Sep 17 00:00:00 2001 From: Tienson Qin Date: Tue, 23 Aug 2022 17:49:45 +0800 Subject: [PATCH] fix: search/editor frozen by base64 encoded image --- src/main/frontend/components/block.cljs | 93 +++++++++++++------------ src/main/frontend/search/db.cljs | 9 +-- src/main/frontend/search/node.cljs | 16 +++-- src/main/frontend/util.cljc | 5 ++ 4 files changed, 66 insertions(+), 57 deletions(-) diff --git a/src/main/frontend/components/block.cljs b/src/main/frontend/components/block.cljs index 8e5e5c883..4baa2170f 100644 --- a/src/main/frontend/components/block.cljs +++ b/src/main/frontend/components/block.cljs @@ -1967,55 +1967,56 @@ (dom/closest target ".query-table"))) (defn- block-content-on-mouse-down - [e block block-id _content edit-input-id] - (.stopPropagation e) - (let [target (gobj/get e "target") - button (gobj/get e "buttons") - shift? (gobj/get e "shiftKey") - meta? (util/meta-key? e)] - (if (and meta? (not (state/get-edit-input-id))) - (do - (util/stop e) - (state/conj-selection-block! (gdom/getElement block-id) :down) - (when (and block-id (not (state/get-selection-start-block))) - (state/set-selection-start-block! block-id))) - (when (contains? #{1 0} button) - (when-not (target-forbidden-edit? target) - (cond - (and shift? (state/get-selection-start-block-or-first)) - (do - (util/stop e) + [e block block-id content edit-input-id] + (when-not (util/base64-image-included? content) + (.stopPropagation e) + (let [target (gobj/get e "target") + button (gobj/get e "buttons") + shift? (gobj/get e "shiftKey") + meta? (util/meta-key? e)] + (if (and meta? (not (state/get-edit-input-id))) + (do + (util/stop e) + (state/conj-selection-block! (gdom/getElement block-id) :down) + (when (and block-id (not (state/get-selection-start-block))) + (state/set-selection-start-block! block-id))) + (when (contains? #{1 0} button) + (when-not (target-forbidden-edit? target) + (cond + (and shift? (state/get-selection-start-block-or-first)) + (do + (util/stop e) + (util/clear-selection!) + (editor-handler/highlight-selection-area! block-id)) + + shift? (util/clear-selection!) - (editor-handler/highlight-selection-area! block-id)) - shift? - (util/clear-selection!) + :else + (do + (editor-handler/clear-selection!) + (editor-handler/unhighlight-blocks!) + (let [f #(let [block (or (db/pull [:block/uuid (:block/uuid block)]) block) + cursor-range (util/caret-range (gdom/getElement block-id)) + {:block/keys [content format]} block + content (->> content + (property/remove-built-in-properties format) + (drawer/remove-logbook))] + ;; save current editing block + (let [{:keys [value] :as state} (editor-handler/get-state)] + (editor-handler/save-block! state value)) + (state/set-editing! + edit-input-id + content + block + cursor-range + false))] + ;; wait a while for the value of the caret range + (if (util/ios?) + (f) + (js/setTimeout f 5)) - :else - (do - (editor-handler/clear-selection!) - (editor-handler/unhighlight-blocks!) - (let [f #(let [block (or (db/pull [:block/uuid (:block/uuid block)]) block) - cursor-range (util/caret-range (gdom/getElement block-id)) - {:block/keys [content format]} block - content (->> content - (property/remove-built-in-properties format) - (drawer/remove-logbook))] - ;; save current editing block - (let [{:keys [value] :as state} (editor-handler/get-state)] - (editor-handler/save-block! state value)) - (state/set-editing! - edit-input-id - content - block - cursor-range - false))] - ;; wait a while for the value of the caret range - (if (util/ios?) - (f) - (js/setTimeout f 5)) - - (when block-id (state/set-selection-start-block! block-id)))))))))) + (when block-id (state/set-selection-start-block! block-id))))))))))) (rum/defc dnd-separator-wrapper < rum/reactive [block block-id slide? top? block-content?] diff --git a/src/main/frontend/search/db.cljs b/src/main/frontend/search/db.cljs index a9a32c0c0..9f0901828 100644 --- a/src/main/frontend/search/db.cljs +++ b/src/main/frontend/search/db.cljs @@ -14,10 +14,11 @@ "Convert a block to the index for searching" [{:block/keys [uuid page content] :as block}] (when-let [content (util/search-normalize content (state/enable-search-remove-accents?))] - {:id (:db/id block) - :uuid (str uuid) - :page page - :content content})) + (when-not (util/base64-image-included? content) + {:id (:db/id block) + :uuid (str uuid) + :page page + :content content}))) (defn build-blocks-indice ;; TODO: Remove repo effects fns further up the call stack. db fns need standardization on taking connection diff --git a/src/main/frontend/search/node.cljs b/src/main/frontend/search/node.cljs index 3a34a954b..c25d64952 100644 --- a/src/main/frontend/search/node.cljs +++ b/src/main/frontend/search/node.cljs @@ -3,20 +3,22 @@ [electron.ipc :as ipc] [frontend.search.db :as search-db] [frontend.search.protocol :as protocol] - [promesa.core :as p])) + [promesa.core :as p] + [frontend.util :as util])) (defrecord Node [repo] protocol/Engine (query [_this q opts] (p/let [result (ipc/ipc "search-blocks" repo q opts) result (bean/->clj result)] - (map (fn [{:keys [content uuid page]}] - {:block/uuid uuid - :block/content content - :block/page page}) result))) + (keep (fn [{:keys [content uuid page]}] + (when-not (util/base64-image-included? content) + {:block/uuid uuid + :block/content content + :block/page page})) result))) (cache-stale? [_this repo] - ;; only FTS require cache validating - (ipc/ipc "searchVersionChanged?" repo)) + ;; only FTS require cache validating + (ipc/ipc "searchVersionChanged?" repo)) (rebuild-blocks-indice! [_this] (let [indice (search-db/build-blocks-indice repo)] (ipc/ipc "rebuild-blocks-indice" repo indice))) diff --git a/src/main/frontend/util.cljc b/src/main/frontend/util.cljc index 57928a9bc..1a88f7ede 100644 --- a/src/main/frontend/util.cljc +++ b/src/main/frontend/util.cljc @@ -1343,3 +1343,8 @@ Math/floor int (#(str % " " (:name unit) (when (> % 1) "s") " ago")))))))) + +#?(:cljs + (defn base64-image-included? + [content] + (string/includes? content "data:image/png;base64,")))