浏览代码

fix: return more filter values

Peng Xiao 3 年之前
父节点
当前提交
482799eb4f

+ 9 - 4
src/main/frontend/extensions/tldraw.cljs

@@ -5,9 +5,9 @@
             [frontend.db.model :as model]
             [frontend.handler.editor :as editor-handler]
             [frontend.handler.route :as route-handler]
-            [frontend.handler.search :as search]
             [frontend.handler.whiteboard :as whiteboard-handler]
             [frontend.rum :as r]
+            [frontend.search :as search]
             [frontend.state :as state]
             [frontend.util :as util]
             [goog.object :as gobj]
@@ -43,9 +43,14 @@
       (whiteboard-handler/add-new-block-portal-shape! uuid client-x client-y))))
 
 (defn search-handler
-  [q]
-  (p/let [results (search/search q)]
-    (clj->js results)))
+  [q filters]
+  (let [{:keys [pages? blocks? files?]} (js->clj filters {:keywordize-keys true})
+        repo (state/get-current-repo)
+        limit 100]
+    (p/let [blocks (when blocks? (search/block-search repo q {:limit limit}))
+            pages (when pages? (search/page-search q))
+            files (when files? (search/file-search q limit))]
+      (clj->js {:pages pages :blocks blocks :files files}))))
 
 (defn save-asset-handler
   [file]

+ 6 - 4
tldraw/apps/tldraw-logseq/src/lib/logseq-context.ts

@@ -1,9 +1,8 @@
 import React from 'react'
 
 export interface SearchResult {
-  pages: string[]
-  blocks: { content: string; page: number; uuid: string }[]
-  'has-more?': boolean
+  pages?: string[]
+  blocks?: { content: string; page: number; uuid: string }[]
   files?: string[]
 }
 
@@ -23,7 +22,10 @@ export interface LogseqContextValue {
     }>
   }
   handlers: {
-    search: (query: string) => Promise<SearchResult>
+    search: (
+      query: string,
+      filters: { 'pages?': boolean; 'blocks?': boolean; 'files?': boolean }
+    ) => Promise<SearchResult>
     addNewBlock: (content: string) => string // returns the new block uuid
     queryBlockByUUID: (uuid: string) => any
     isWhiteboardPage: (pageName: string) => boolean

+ 11 - 5
tldraw/apps/tldraw-logseq/src/lib/shapes/LogseqPortalShape.tsx

@@ -99,7 +99,7 @@ const highlightedJSX = (input: string, keyword: string) => {
   )
 }
 
-const useSearch = (q: string) => {
+const useSearch = (q: string, searchFilter: 'B' | 'P' | null) => {
   const { handlers } = React.useContext(LogseqContext)
   const [results, setResults] = React.useState<SearchResult | null>(null)
 
@@ -107,7 +107,13 @@ const useSearch = (q: string) => {
     let canceled = false
     const searchHandler = handlers?.search
     if (q.length > 0 && searchHandler) {
-      handlers.search(q).then(_results => {
+      const filter = { 'pages?': true, 'blocks?': true, 'files?': false }
+      if (searchFilter === 'B') {
+        filter['pages?'] = false
+      } else if (searchFilter === 'P') {
+        filter['blocks?'] = false
+      }
+      handlers.search(q, filter).then(_results => {
         if (!canceled) {
           setResults(_results)
         }
@@ -324,10 +330,10 @@ export class LogseqPortalShape extends TLBoxShape<LogseqPortalShapeProps> {
 
     const [focusedOptionIdx, setFocusedOptionIdx] = React.useState<number>(0)
 
-    const searchResult = useSearch(q)
+    const [searchFilter, setSearchFilter] = React.useState<'B' | 'P' | null>(null)
+    const searchResult = useSearch(q, searchFilter)
 
     const [prefixIcon, setPrefixIcon] = React.useState<string>('circle-plus')
-    const [searchFilter, setSearchFilter] = React.useState<'B' | 'P' | null>(null)
 
     React.useEffect(() => {
       // autofocus seems not to be working
@@ -373,7 +379,7 @@ export class LogseqPortalShape extends TLBoxShape<LogseqPortalShapeProps> {
       })
 
       // New page option when no exact match
-      if (!searchResult?.pages.some(p => p.toLowerCase() === q.toLowerCase()) && q) {
+      if (!searchResult?.pages?.some(p => p.toLowerCase() === q.toLowerCase()) && q) {
         options.push({
           actionIcon: 'circle-plus',
           onChosen: () => {