mirror of https://github.com/logseq/logseq
Merge branch 'master' into enhance/mobile-ux-2
commit
79d0766b04
|
@ -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>$', '$<pfts_2lqh$', '...', 32)"
|
||||
snippet-aux "snippet(pages_fts, 1, ' $pfts_2lqh>$ ', ' $<pfts_2lqh$ ', '...', 32)"
|
||||
select (str "select rowid, uuid, content, " snippet-aux " from pages_fts where ")
|
||||
match-sql (str select
|
||||
" content match ? order by rank limit ?")
|
||||
|
@ -313,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))))))
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -26,18 +26,16 @@
|
|||
(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]
|
||||
(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)
|
||||
|
|
|
@ -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
|
||||
(->
|
||||
|
|
|
@ -267,9 +267,28 @@
|
|||
(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 replace-empty-target? idx]
|
||||
(cond
|
||||
;; replace existing block
|
||||
(and (= outliner-op :paste)
|
||||
replace-empty-target?
|
||||
(string/blank? (:block/content target-block))
|
||||
(zero? idx))
|
||||
(get-id (:block/parent target-block))
|
||||
|
||||
prev-hop
|
||||
(:db/id (:block/parent prev-hop))
|
||||
|
||||
|
@ -455,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)
|
||||
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
|
||||
|
|
|
@ -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]))
|
||||
|
||||
)
|
||||
|
|
Loading…
Reference in New Issue