Просмотр исходного кода

fix: tons of issues on whiteboards

Mostly because we made the change that multiple pages can have the
same name now, also the UI db is not full anymore.
Tienson Qin 1 год назад
Родитель
Сommit
554926cb19

+ 14 - 19
packages/tldraw/apps/tldraw-logseq/src/components/QuickSearch/QuickSearch.tsx

@@ -103,8 +103,9 @@ export const LogseqQuickSearch = observer(
     const { handlers, renderers } = React.useContext(LogseqContext)
     const t = handlers.t
 
-    const finishSearching = React.useCallback((id: string) => {
-      setTimeout(() => onChange(id))
+    const finishSearching = React.useCallback((id: string, isPage: boolean) => {
+    console.log({id, isPage})
+      setTimeout(() => onChange(id, isPage))
       rInput.current?.blur()
       if (id) {
         LogseqPortalShape.defaultSearchQuery = ''
@@ -185,12 +186,13 @@ export const LogseqQuickSearch = observer(
       }
 
       // New page or whiteboard option when no exact match
-      if (!searchResult?.pages?.some(p => p.toLowerCase() === q.toLowerCase()) && q) {
+      if (!searchResult?.pages?.some(p => p.title.toLowerCase() === q.toLowerCase()) && q) {
         options.push(
           {
             actionIcon: 'circle-plus',
-            onChosen: () => {
-              finishSearching(q)
+            onChosen: async () => {
+              let result = await handlers?.addNewPage(q)
+              finishSearching(result, true)
               return true
             },
             element: (
@@ -204,8 +206,8 @@ export const LogseqQuickSearch = observer(
           {
             actionIcon: 'circle-plus',
             onChosen: async () => {
-              await handlers?.addNewWhiteboard(q)
-              finishSearching(q)
+              let result = await handlers?.addNewWhiteboard(q)
+              finishSearching(result, true)
               return true
             },
             element: (
@@ -258,13 +260,13 @@ export const LogseqQuickSearch = observer(
             return {
               actionIcon: 'search' as 'search',
               onChosen: () => {
-                finishSearching(page)
+                finishSearching(page.id, true)
                 return true
               },
               element: (
                 <div className="tl-quick-search-option-row">
-                  <LogseqTypeTag type={handlers.isWhiteboardPage(page) ? 'WP' : 'P'} />
-                  {highlightedJSX(page, q)}
+                  <LogseqTypeTag type={handlers.isWhiteboardPage(page.id) ? 'WP' : 'P'} />
+                  {highlightedJSX(page.title, q)}
                 </div>
               ),
             }
@@ -289,21 +291,14 @@ export const LogseqQuickSearch = observer(
                   }
                   return false
                 },
-                element: block ? (
+                // FIXME: breadcrumb not works here because of async loading
+                element: (
                   <>
                     <div className="tl-quick-search-option-row">
                       <LogseqTypeTag type="B" />
-                      <div className="tl-quick-search-option-breadcrumb">
-                        <Breadcrumb blockId={uuid} />
-                      </div>
-                    </div>
-                    <div className="tl-quick-search-option-row">
-                      <div className="tl-quick-search-option-placeholder" />
                       {highlightedJSX(content, q)}
                     </div>
                   </>
-                ) : (
-                  <div className="tl-quick-search-option-row">{t('whiteboard/cache-outdated')}</div>
                 ),
               }
             })

+ 4 - 3
packages/tldraw/apps/tldraw-logseq/src/lib/shapes/LogseqPortalShape.tsx

@@ -420,9 +420,9 @@ export class LogseqPortalShape extends TLBoxShape<LogseqPortalShapeProps> {
       }
     }, [app.viewport.bounds.height.toFixed(2)])
 
-    const onPageNameChanged = React.useCallback((id: string) => {
+    const onPageNameChanged = React.useCallback((id: string, isPage: boolean) => {
       this.initialHeightCalculated = false
-      const blockType = validUUID(id) ? 'B' : 'P'
+      const blockType = isPage ? 'P' : 'B'
       this.update({
         pageId: id,
         size: [400, 320],
@@ -550,6 +550,7 @@ export class LogseqPortalShape extends TLBoxShape<LogseqPortalShapeProps> {
   getShapeSVGJsx({ preview }: any) {
     // Do not need to consider the original point here
     const bounds = this.getBounds()
+
     return (
       <>
         <rect
@@ -595,7 +596,7 @@ export class LogseqPortalShape extends TLBoxShape<LogseqPortalShapeProps> {
           fill="var(--ls-secondary-text-color)"
           stroke="var(--ls-secondary-text-color)"
         >
-          {this.props.blockType === 'P' ? this.props.pageId : ''}
+          {this.props.blockType === 'P' ? this.props.pageName : ''}
         </text>
       </>
     )

+ 2 - 3
src/main/frontend/components/block.cljs

@@ -553,7 +553,7 @@
         page-name (:block/name page-entity)
         breadcrumb? (:breadcrumb? config)
         config (assoc config :whiteboard-page? whiteboard-page?)
-        untitled? (when page-name (model/untitled-page? page-name))
+        untitled? (when page-name (model/untitled-page? (:block/original-name page-entity)))
         display-close-button? (and (not (:hide-close-button? config))
                                    (not config/publishing?))
         hide-icon? (:hide-icon? config)]
@@ -900,12 +900,11 @@
 (declare block-content)
 (declare breadcrumb)
 
-(rum/defc block-reference < rum/reactive
+(rum/defc block-reference < rum/reactive db-mixins/query
   {:init (fn [state]
            (let [block-id (second (:rum/args state))]
              (db-async/<get-block (state/get-current-repo) block-id :children? false))
            state)}
-  db-mixins/query
   [config id label]
   (if-let [block-id (if (uuid? id) id (parse-uuid id))]
     (if (state/sub-async-query-loading (str block-id))

+ 12 - 13
src/main/frontend/components/cmdk/core.cljs

@@ -215,19 +215,18 @@
     (swap! !results assoc-in [group :status] :loading)
     (p/let [pages (search/page-search @!input)
             items (->> pages
-                    (remove nil?)
-                    (map
-                      (fn [page]
-                        (let [entity (db/get-page page)
-                              whiteboard? (= (:block/type entity) "whiteboard")
-                              source-page (model/get-alias-source-page repo (:db/id entity))]
-                          (hash-map :icon (if whiteboard? "whiteboard" "page")
-                            :icon-theme :gray
-                            :text page
-                            :source-page (if source-page
-                                           (:block/original-name source-page)
-                                           page))))))]
-      (swap! !results update group        merge {:status :success :items items}))))
+                       (map
+                        (fn [page]
+                          (let [entity (db/entity [:block/uuid (uuid (:id page))])
+                                whiteboard? (= (:block/type entity) "whiteboard")
+                                source-page (model/get-alias-source-page repo (:db/id entity))]
+                            (hash-map :icon (if whiteboard? "whiteboard" "page")
+                                      :icon-theme :gray
+                                      :text (:title page)
+                                      :source-page (if source-page
+                                                     (:block/original-name source-page)
+                                                     (:title page)))))))]
+      (swap! !results update group merge {:status :success :items items}))))
 
 (defmethod load-results :whiteboards [group state]
   (let [!input (::input state)

+ 7 - 7
src/main/frontend/components/page.cljs

@@ -183,8 +183,8 @@
     (let [page-name (or (:block/name page-e)
                         (str (:block/uuid page-e)))
           block-id (parse-uuid page-name)
-          block? (boolean block-id)
           block (get-block (or (:block/uuid page-e) (:block/name page-e)))
+          block? (not (db/page? block))
           children (:block/_parent block)
           *loading? (:*loading? config)
           loading? (when *loading? (rum/react *loading?))]
@@ -198,12 +198,12 @@
         :else
         (let [document-mode? (state/sub :document/mode?)
               hiccup-config (merge
-                              {:id (if block? (str block-id) page-name)
-                               :db/id (:db/id block)
-                               :block? block?
-                               :editor-box editor/box
-                               :document/mode? document-mode?}
-                              config)
+                             {:id (if block? (str block-id) page-name)
+                              :db/id (:db/id block)
+                              :block? block?
+                              :editor-box editor/box
+                              :document/mode? document-mode?}
+                             config)
               config (common-handler/config-with-document-mode hiccup-config)
               blocks (if block? [block] (db/sort-by-order children block))]
           [:div

+ 4 - 2
src/main/frontend/db_worker.cljs

@@ -492,12 +492,14 @@
   (search-build-blocks-indice
    [this repo]
    (when-let [conn (worker-state/get-datascript-conn repo)]
-     (search/build-blocks-indice repo @conn)))
+     (search/build-blocks-indice repo @conn)
+     nil))
 
   (search-build-pages-indice
    [this repo]
    (when-let [conn (worker-state/get-datascript-conn repo)]
-     (search/build-blocks-indice repo @conn)))
+     (search/build-page-indice repo @conn)
+     nil))
 
   ;; page ops
   (page-search

+ 31 - 20
src/main/frontend/extensions/tldraw.cljs

@@ -27,13 +27,13 @@
             [frontend.components.whiteboard :as whiteboard]
             [cljs-bean.core :as bean]
             [frontend.db.async :as db-async]
-            [logseq.common.util :as common-util]))
+            [logseq.common.util :as common-util]
+            [frontend.util.text :as text-util]))
 
 (def tldraw (r/adapt-class (gobj/get TldrawLogseq "App")))
 
 (def generate-preview (gobj/get TldrawLogseq "generateJSXFromModel"))
 
-;; FIXME: use page uuid instead or creating a ref
 (rum/defc page-cp
   [props]
   (page/page {:page-name (gobj/get props "pageName") :whiteboard? true}))
@@ -63,18 +63,23 @@
 
 (rum/defc page-name-link
   [props]
-  (block/page-cp {:preview? true} {:block/name (gobj/get props "pageName")}))
+  (when-let [page-name (gobj/get props "pageName")]
+    (when-let [page (db/get-page page-name)]
+      (block/page-cp {:preview? true} page))))
 
 (defn search-handler
   [q filters]
-  (let [{:keys [pages? blocks? files?]} (js->clj filters {:keywordize-keys true})
+  (let [{:keys [pages? blocks?]} (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}))
-            blocks (map (fn [b] (update b :block/uuid str)) blocks)
-            pages (when pages? (search/page-search q))
-            files (when files? (search/file-search q limit))]
-      (clj->js {:pages pages :blocks blocks :files files}))))
+            blocks (map (fn [b]
+                          (-> b
+                              (update :block/uuid str)
+                              (update :block/content #(->> (text-util/cut-by % "$pfts_2lqh>$" "$<pfts_2lqh$")
+                                                           (apply str))))) blocks)
+            pages (when pages? (search/page-search q))]
+      (clj->js {:pages pages :blocks blocks}))))
 
 (defn save-asset-handler
   [file]
@@ -118,8 +123,8 @@
                         (model/query-block-by-uuid block-uuid)))
    :getBlockPageName #(let [block-id-str %]
                         (if (util/uuid-string? block-id-str)
-                          (:block/name (model/get-block-page (state/get-current-repo) (parse-uuid block-id-str)))
-                          (:block/name (db/get-page block-id-str))))
+                          (str (:block/uuid (model/get-block-page (state/get-current-repo) (parse-uuid block-id-str))))
+                          (str (:block/uuid (db/get-page block-id-str)))))
    :exportToImage (fn [page-uuid-str options]
                     (assert (common-util/uuid-string? page-uuid-str))
                     (state/set-modal! #(export/export-blocks (uuid page-uuid-str) (merge (js->clj options :keywordize-keys true) {:whiteboard? true}))))
@@ -133,9 +138,14 @@
    :setCurrentPdf (fn [src] (state/set-current-pdf! (if src (pdf-assets/inflate-asset src) nil)))
    :copyToClipboard (fn [text, html] (util/copy-to-clipboard! text :html html))
    :getRedirectPageName (fn [page-name-or-uuid] (model/get-redirect-page-name page-name-or-uuid))
-   :insertFirstPageBlock (fn [page-name] (editor-handler/insert-first-page-block-if-not-exists! page-name {:redirect? false}))
+   :insertFirstPageBlock (fn [page-name]
+                           (editor-handler/insert-first-page-block-if-not-exists! page-name {:redirect? false}))
+   :addNewPage (fn [page-name]
+                 (p/let [result (page-handler/<create! page-name {:redirect? false})]
+                   (str (:block/uuid result))))
    :addNewWhiteboard (fn [page-name]
-                       (whiteboard-handler/<create-new-whiteboard-page! page-name))
+                       (p/let [result (whiteboard-handler/<create-new-whiteboard-page! page-name)]
+                         (str result)))
    :addNewBlock (fn [content]
                   (p/let [new-block-id (whiteboard-handler/<add-new-block! current-whiteboard-uuid content)]
                     (str new-block-id)))
@@ -143,15 +153,16 @@
                       (state/sidebar-add-block! (state/get-current-repo)
                                                 (:db/id (model/get-page uuid))
                                                 (keyword type)))
-   :redirectToPage (fn [page-name-or-uuid] ; FIXME whiteboard link refs should store UUIDs instead of page names
-                     (when page-name-or-uuid
-                       (let [block-id (parse-uuid page-name-or-uuid)
-                             page (if block-id
-                                    (model/get-block-page (state/get-current-repo) block-id)
-                                    (db/get-page page-name-or-uuid))
-                             whiteboard? (model/whiteboard-page? page)]
+   :redirectToPage (fn [page-uuid-str]
+                     (when page-uuid-str
+                       (p/let [block-id (parse-uuid page-uuid-str)
+                               _ (when block-id (db-async/<get-block (state/get-current-repo) block-id))
+                               page (or
+                                     (when block-id (model/get-block-page (state/get-current-repo) block-id))
+                                     (db/get-page page-uuid-str))
+                               whiteboard? (model/whiteboard-page? page)]
                          (p/let [new-page (when (nil? page)
-                                            (page-handler/<create! page-name-or-uuid {:redirect? false}))
+                                            (page-handler/<create! page-uuid-str {:redirect? false}))
                                  page' (or new-page page)]
                            (route-handler/redirect-to-page! (if whiteboard?
                                                               (:block/uuid page')

+ 6 - 4
src/main/frontend/handler/editor.cljs

@@ -1656,10 +1656,12 @@
                             (when-let [page-id (:db/id (:block/page block))]
                               (:block/name (db/entity page-id))))
           pages (search/page-search q {:built-in? false})]
-    (if editing-page
-      ;; To prevent self references
-      (remove (fn [p] (= (util/page-name-sanity-lc p) editing-page)) pages)
-      pages)))
+    (->> (if editing-page
+           ;; To prevent self references
+           (remove (fn [p] (= (util/safe-page-name-sanity-lc (:title p))
+                              (util/safe-page-name-sanity-lc editing-page))) pages)
+           pages)
+         (map :title))))
 
 (defn get-matched-classes
   "Return matched class names"

+ 7 - 1
src/main/frontend/handler/whiteboard.cljs

@@ -38,7 +38,13 @@
                 (assoc block :index (get shape-id->index (str (:block/uuid block)) 0))))
          (filter pu/shape-block?)
          (map pu/block->shape)
-         (sort-by :index))))
+         (sort-by :index)
+         (map (fn [shape]
+                (if-let [page-id (:pageId shape)]
+                  (let [page (db/get-page page-id)]
+                    ;; Used in page preview
+                    (assoc shape :pageName (:block/original-name page)))
+                  shape))))))
 
 (defn- whiteboard-clj->tldr [page-block blocks]
   (let [id (str (:block/uuid page-block))

+ 8 - 11
src/main/frontend/worker/search.cljs

@@ -316,7 +316,7 @@
                    (map original-page-name->index)
                    (bean/->js))
         indice (fuse. pages
-                      (clj->js {:keys ["name"]
+                      (clj->js {:keys ["original-name"]
                                 :shouldSort true
                                 :tokenize true
                                 :distance 1024
@@ -372,7 +372,7 @@
                (fn [indice]
                  (when indice
                    (doseq [page-entity pages-to-remove]
-                     (.remove indice (fn [page] (= (:block/name page-entity) (gobj/get page "name")))))
+                     (.remove indice (fn [page] (= (str (:block/uuid page-entity)) (gobj/get page "id")))))
                    (doseq [page pages-to-add]
                      (.remove indice (fn [p] (= (str (:block/uuid page)) (gobj/get p "id"))))
                      (.add indice (bean/->js (original-page-name->index page))))
@@ -416,19 +416,16 @@
                          (build-page-indice repo db))
               result (cond->>
                       (->> (.search indice q (clj->js {:limit limit}))
-                           (bean/->clj)
-                           (remove #(nil? (get-in % [:item :original-name]))))
+                           (bean/->clj))
 
                        (and (sqlite-util/db-based-graph? repo) (= false built-in?))
                        (remove #(get-in % [:item :built-in?])))]
           (->> result
-               (common-util/distinct-by (fn [i] (string/trim (get-in i [:item :original-name]))))
-               (map
+               (keep
                 (fn [{:keys [item]}]
-                  (:original-name item)))
-               (remove nil?)
-               (map string/trim)
+                  {:id (:id item)
+                   :title (:original-name item)}))
                (distinct)
-               (filter (fn [original-name]
-                         (exact-matched? q original-name)))
+               (filter (fn [{:keys [title]}]
+                         (exact-matched? q title)))
                bean/->js))))))