From a06dc560814da064b2afa4331aa08275f2f1c53e Mon Sep 17 00:00:00 2001 From: Tienson Qin Date: Tue, 29 Nov 2022 15:33:00 +0800 Subject: [PATCH 1/6] fix: remove code that doesn't make sense --- src/main/frontend/handler/web/nfs.cljs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/frontend/handler/web/nfs.cljs b/src/main/frontend/handler/web/nfs.cljs index 930e1dcf16..12f089cf84 100644 --- a/src/main/frontend/handler/web/nfs.cljs +++ b/src/main/frontend/handler/web/nfs.cljs @@ -131,7 +131,6 @@ *repo (atom nil) dir (or dir nil) dir (some-> dir - (string/replace "file:///" "file://") (string/replace " " "%20"))] ;; TODO: add ext filter to avoid loading .git or other ignored file handlers (-> From ef287afb70831d55e351b8e44539a1268a0cd0f7 Mon Sep 17 00:00:00 2001 From: Tienson Qin Date: Tue, 29 Nov 2022 10:30:30 +0800 Subject: [PATCH 2/6] fix: can't paste in the first block if it's empty close #7500 --- src/main/frontend/modules/outliner/core.cljs | 27 +++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/src/main/frontend/modules/outliner/core.cljs b/src/main/frontend/modules/outliner/core.cljs index 3cf8030cac..31df65c3c2 100644 --- a/src/main/frontend/modules/outliner/core.cljs +++ b/src/main/frontend/modules/outliner/core.cljs @@ -267,9 +267,30 @@ (recur (rest blocks) (first blocks)) matched))))) -(defn- compute-block-parent - [block parent target-block prev-hop top-level? sibling? get-new-id] +(defn- get-id + [x] (cond + (map? x) + (:db/id x) + + (vector? x) + (second x) + + :else + x)) + +(defn- compute-block-parent + [block parent target-block prev-hop top-level? sibling? get-new-id outliner-op] + (cond + ;; replace existing block + (and (= outliner-op :paste) + (string/blank? (:block/content target-block)) + (= (get-id (:block/parent target-block)) + (get-id (:block/parent block))) + (= (get-id (:block/left target-block)) + (get-id (:block/left block)))) + (get-id (:block/parent target-block)) + prev-hop (:db/id (:block/parent prev-hop)) @@ -455,7 +476,7 @@ (not= (:block/parent block) (:block/parent target-block))) prev-hop (if outdented-block? (find-outdented-block-prev-hop block blocks) nil) left-exists-in-blocks? (contains? ids (:db/id (:block/left block))) - parent (compute-block-parent block parent target-block prev-hop top-level? sibling? get-new-id) + parent (compute-block-parent block parent target-block prev-hop top-level? sibling? get-new-id outliner-op) left (compute-block-left blocks block left target-block prev-hop idx replace-empty-target? left-exists-in-blocks? get-new-id)] (cond-> (merge block {:block/uuid uuid From 81f22da35d077bec38b7e300f4fb3760288b5a34 Mon Sep 17 00:00:00 2001 From: Tienson Qin Date: Tue, 29 Nov 2022 12:08:07 +0800 Subject: [PATCH 3/6] fix: paste nested blocks --- src/main/frontend/handler/editor.cljs | 34 ++++++++++++++----- src/main/frontend/modules/outliner/core.cljs | 10 +++--- .../frontend/modules/outliner/core_test.cljs | 33 +++++++++++++++++- 3 files changed, 62 insertions(+), 15 deletions(-) diff --git a/src/main/frontend/handler/editor.cljs b/src/main/frontend/handler/editor.cljs index 4213d2a5ed..2f953be870 100644 --- a/src/main/frontend/handler/editor.cljs +++ b/src/main/frontend/handler/editor.cljs @@ -1907,6 +1907,11 @@ (edit-block! last-block' :max (:block/uuid last-block'))))) 0)) +(defn- nested-blocks + [blocks] + (let [ids (set (map :db/id blocks))] + (some? (some #(ids (:db/id (:block/parent %))) blocks)))) + (defn paste-blocks "Given a vec of blocks, insert them into the target page. keep-uuid?: if true, keep the uuid provided in the block structure." @@ -1926,12 +1931,25 @@ block (db/entity (:db/id target-block)) page (if (:block/name block) block (when target-block (:block/page (db/entity (:db/id target-block))))) - target-block (or target-block editing-block) + empty-target? (string/blank? (:block/content target-block)) + paste-nested-blocks? (nested-blocks blocks) + target-block-has-children? (db/has-children? (:block/uuid target-block)) + replace-empty-target? (if (and paste-nested-blocks? empty-target? + target-block-has-children?) + false + true) + target-block' (if replace-empty-target? target-block + (db/pull (:db/id (:block/left target-block)))) sibling? (cond + (and paste-nested-blocks? empty-target?) + (if (= (:block/parent target-block') (:block/parent target-block)) + true + false) + (some? sibling?) sibling? - (db/has-children? (:block/uuid target-block)) + target-block-has-children? false :else @@ -1944,15 +1962,15 @@ (outliner-tx/transact! {:outliner-op :insert-blocks} - (when target-block - (let [format (or (:block/format target-block) (state/get-preferred-format)) + (when target-block' + (let [format (or (:block/format target-block') (state/get-preferred-format)) blocks' (map (fn [block] (paste-block-cleanup block page exclude-properties format content-update-fn)) blocks) - result (outliner-core/insert-blocks! blocks' target-block {:sibling? sibling? - :outliner-op :paste - :replace-empty-target? true - :keep-uuid? keep-uuid?})] + result (outliner-core/insert-blocks! blocks' target-block' {:sibling? sibling? + :outliner-op :paste + :replace-empty-target? replace-empty-target? + :keep-uuid? keep-uuid?})] (edit-last-block-after-inserted! result)))))) (defn- block-tree->blocks diff --git a/src/main/frontend/modules/outliner/core.cljs b/src/main/frontend/modules/outliner/core.cljs index 31df65c3c2..87a545beb8 100644 --- a/src/main/frontend/modules/outliner/core.cljs +++ b/src/main/frontend/modules/outliner/core.cljs @@ -280,15 +280,13 @@ x)) (defn- compute-block-parent - [block parent target-block prev-hop top-level? sibling? get-new-id outliner-op] + [block parent target-block prev-hop top-level? sibling? get-new-id outliner-op replace-empty-target? idx] (cond ;; replace existing block (and (= outliner-op :paste) + replace-empty-target? (string/blank? (:block/content target-block)) - (= (get-id (:block/parent target-block)) - (get-id (:block/parent block))) - (= (get-id (:block/left target-block)) - (get-id (:block/left block)))) + (zero? idx)) (get-id (:block/parent target-block)) prev-hop @@ -476,7 +474,7 @@ (not= (:block/parent block) (:block/parent target-block))) prev-hop (if outdented-block? (find-outdented-block-prev-hop block blocks) nil) left-exists-in-blocks? (contains? ids (:db/id (:block/left block))) - parent (compute-block-parent block parent target-block prev-hop top-level? sibling? get-new-id outliner-op) + parent (compute-block-parent block parent target-block prev-hop top-level? sibling? get-new-id outliner-op replace-empty-target? idx) left (compute-block-left blocks block left target-block prev-hop idx replace-empty-target? left-exists-in-blocks? get-new-id)] (cond-> (merge block {:block/uuid uuid diff --git a/src/test/frontend/modules/outliner/core_test.cljs b/src/test/frontend/modules/outliner/core_test.cljs index bbba14a236..269ecf71cd 100644 --- a/src/test/frontend/modules/outliner/core_test.cljs +++ b/src/test/frontend/modules/outliner/core_test.cljs @@ -330,6 +330,37 @@ (is (= [19 20] (get-children 18)))))) +(deftest test-paste-into-empty-block + (testing " + Paste a block into the first block (its content is empty) + [[22 [[2 [[3 [[4] + [5]]] + [6 [[7 [[8]]]]] + [9 [[10] + [11]]]]] + [12 [[13] + [14] + [15]]] + [16 [[17]]]]]] + " + (transact-tree! tree) + (db/transact! test-db [{:block/uuid 22 + :block/content ""}]) + (let [target-block (get-block 22)] + (outliner-tx/transact! + {:graph test-db} + (outliner-core/insert-blocks! [{:block/left [:block/uuid 1] + :block/content "test" + :block/parent [:block/uuid 1] + :block/page 1}] + target-block + {:sibling? false + :outliner-op :paste + :replace-empty-target? true})) + (is (= "test" (:block/content (get-block 22)))) + (is (= [22] (get-children 1))) + (is (= [2 12 16] (get-children 22)))))) + (deftest test-batch-transact (testing "add 4, 5 after 2 and delete 3" (let [tree [[1 [[2] [3]]]]] @@ -691,6 +722,6 @@ (do (frontend.test.fixtures/reset-datascript test-db) - (cljs.test/test-vars [#'random-deletes])) + (cljs.test/test-vars [#'test-paste-first-empty-block])) ) From b658fdb9e8c84ee859a33727d04d5c8349362bb8 Mon Sep 17 00:00:00 2001 From: Tienson Qin Date: Tue, 29 Nov 2022 17:08:02 +0800 Subject: [PATCH 4/6] fix: catch match search with escaped input Also, fixed the problem the highlights don't have spaces around. --- src/electron/electron/search.cljs | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/src/electron/electron/search.cljs b/src/electron/electron/search.cljs index fa1efce39e..16d0c77cd1 100644 --- a/src/electron/electron/search.cljs +++ b/src/electron/electron/search.cljs @@ -215,12 +215,15 @@ (defn- search-blocks-aux [database sql input page limit] - (let [stmt (prepare database sql)] - (js->clj - (if page - (.all ^object stmt (int page) input limit) - (.all ^object stmt input limit)) - :keywordize-keys true))) + (try + (let [stmt (prepare database sql)] + (js->clj + (if page + (.all ^object stmt (int page) input limit) + (.all ^object stmt input limit)) + :keywordize-keys true)) + (catch :default e + (logger/error "Search blocks failed: " (str e))))) (defn- get-match-inputs [q] @@ -285,9 +288,13 @@ (defn- search-pages-aux [database sql input limit] (let [stmt (prepare database sql)] - (map search-pages-res-unpack (-> (.raw ^object stmt) - (.all input limit) - (js->clj))))) + (try + (doall + (map search-pages-res-unpack (-> (.raw ^object stmt) + (.all input limit) + (js->clj)))) + (catch :default e + (logger/error "Search page failed: " (str e)))))) (defn search-pages [repo q {:keys [limit]}] @@ -300,7 +307,7 @@ ;; the 2nd column in pages_fts (content) ;; pfts_2lqh is a key for retrieval ;; highlight and snippet only works for some matching with high rank - snippet-aux "snippet(pages_fts, 1, '$pfts_2lqh>$', '$$ ', ' $ Date: Tue, 29 Nov 2022 17:29:08 +0800 Subject: [PATCH 5/6] fix: slow search --- src/electron/electron/search.cljs | 3 +-- src/main/frontend/handler/search.cljs | 4 +--- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/src/electron/electron/search.cljs b/src/electron/electron/search.cljs index 16d0c77cd1..d84585349c 100644 --- a/src/electron/electron/search.cljs +++ b/src/electron/electron/search.cljs @@ -320,8 +320,7 @@ match-inputs) (apply concat))] (->> - (concat matched-result - (search-pages-aux database non-match-sql non-match-input limit)) + matched-result (distinct-by :id) (take limit) (vec)))))) diff --git a/src/main/frontend/handler/search.cljs b/src/main/frontend/handler/search.cljs index 6da9f2717f..d4b37cf53c 100644 --- a/src/main/frontend/handler/search.cljs +++ b/src/main/frontend/handler/search.cljs @@ -26,9 +26,7 @@ (defn sanity-search-content "Convert a block to the display contents for searching" [format content] - (->> (text/remove-level-spaces content format (config/get-block-pattern format)) - (drawer/remove-logbook) - (property/remove-built-in-properties format))) + (text/remove-level-spaces content format (config/get-block-pattern format))) (defn search ([q] From 4167c7b5cbd20d2c84dffa27c9a08f08d07f23dd Mon Sep 17 00:00:00 2001 From: Tienson Qin Date: Tue, 29 Nov 2022 17:34:24 +0800 Subject: [PATCH 6/6] enhance: reduce search items --- src/main/frontend/handler/search.cljs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/frontend/handler/search.cljs b/src/main/frontend/handler/search.cljs index d4b37cf53c..0845dbe032 100644 --- a/src/main/frontend/handler/search.cljs +++ b/src/main/frontend/handler/search.cljs @@ -32,10 +32,10 @@ ([q] (search (state/get-current-repo) q)) ([repo q] - (search repo q {:limit 20})) + (search repo q {:limit 10})) ([repo q {:keys [page-db-id limit more?] :or {page-db-id nil - limit 20} + limit 10} :as opts}] (when-not (string/blank? q) (let [page-db-id (if (string? page-db-id)