enhance(ux): same node render for both reference and embed

also use icon-component/get-node-icon-cp when possible
This commit is contained in:
Tienson Qin
2025-11-12 03:57:18 +08:00
parent 3b99f1344f
commit a7c8212df5
6 changed files with 58 additions and 113 deletions

View File

@@ -4,6 +4,7 @@
[electron.ipc :as ipc]
[frontend.components.block :as block]
[frontend.components.cmdk.list-item :as list-item]
[frontend.components.icon :as icon-component]
[frontend.config :as config]
[frontend.context.i18n :refer [t]]
[frontend.db :as db]
@@ -206,24 +207,12 @@
;; Each result group has it's own load-results function
(defmulti load-results (fn [group _state] group))
(defn get-page-icon
[entity]
(cond
(ldb/class? entity)
"hash"
(ldb/property? entity)
"letter-p"
(ldb/whiteboard? entity)
"writing"
:else
"file"))
(defmethod load-results :initial [_ state]
(when-let [db (db/get-db)]
(let [!results (::results state)
recent-pages (map (fn [block]
(let [text (block-handler/block-unique-title block)
icon (get-page-icon block)]
icon (icon-component/get-node-icon-cp block {})]
{:icon icon
:icon-theme :gray
:text text
@@ -261,7 +250,7 @@
(->> search-results
(map (fn [block]
(let [text (block-handler/block-unique-title block)
icon (get-page-icon block)]
icon (icon-component/get-node-icon-cp block {})]
{:icon icon
:icon-theme :gray
:text text
@@ -289,7 +278,7 @@
(let [entity (db/entity [:block/uuid (:block/uuid page)])
source-page (or (model/get-alias-source-page repo (:db/id entity))
(:alias page))
icon (get-page-icon entity)
icon (icon-component/get-node-icon-cp entity {})
title (block-handler/block-unique-title (or entity page))
title' (if source-page (str title " -> alias: " (:block/title source-page)) title)]
(hash-map :icon icon
@@ -306,7 +295,7 @@
[repo block current-page input]
(let [id (:block/uuid block)
text (block-handler/block-unique-title block)
icon "letter-n"]
icon (icon-component/get-node-icon-cp block {})]
{:icon icon
:icon-theme :gray
:text (highlight-content-query text input)

View File

@@ -92,7 +92,9 @@
(if highlighted "bg-accent-07-alpha" "bg-gray-05")
" dark:text-white")
(= icon-theme :gray) (str " bg-gray-05 dark:text-white"))}
(shui/tabler-icon icon {:size "14" :class ""})]
(if (string? icon)
(shui/tabler-icon icon {:size "14" :class ""})
icon)]
[:div.flex.flex-1.flex-col
(when title
[:div.text-sm.pb-2.font-bold.text-gray-11 (highlight-query title)])

View File

@@ -3,7 +3,7 @@
[dommy.core :as dom]
[frontend.commands :as commands :refer [*matched-commands]]
[frontend.components.file-based.datetime :as datetime-comp]
[frontend.components.search :as search]
[frontend.components.icon :as icon-component]
[frontend.components.svg :as svg]
[frontend.config :as config]
[frontend.context.i18n :refer [t]]
@@ -51,6 +51,48 @@
(contains? #{"TASK STATUS" "TASK DATE" "PRIORITY"} (last item))))) commands)
commands))
(defn node-render
[block q {:keys [db-tag? db-based?]}]
(let [block' (if-let [id (:block/uuid block)]
(if-let [e (db/entity [:block/uuid id])]
(assoc e
:block/title (or (:friendly-title block) (:block/title e) (:block/title block))
:alias (:alias block))
block)
block)]
(when-not (string/blank? (:block/title block'))
[:div.flex.flex-col
(when (and (:block/uuid block') (or (:block/parent block') (not (:page? block))))
(when-let [breadcrumb (state/get-component :block/breadcrumb)]
[:div.text-xs.opacity-70.mb-1 {:style {:margin-left 3}}
(breadcrumb {:search? true} (state/get-current-repo) (:block/uuid block')
{:disabled? true})]))
[:div.flex.flex-row.items-start
(when-not (or db-tag? (not db-based?))
[:div.flex.items-center.h-5.mr-1.opacity-50
(cond
(:nlp-date? block')
(ui/icon "calendar" {:size 14})
(or (string/starts-with? (str (:block/title block')) (t :new-tag))
(string/starts-with? (str (:block/title block')) (t :new-page)))
(ui/icon "plus" {:size 14})
:else
(icon-component/get-node-icon-cp block' {}))])
(let [title (let [alias (get-in block' [:alias :block/title])
title (if (and db-based? (not (ldb/built-in? block')))
(block-handler/block-unique-title block')
(:block/title block'))]
(if alias
(str title " -> alias: " alias)
title))]
(if (or (string/starts-with? title (t :new-tag))
(string/starts-with? title (t :new-page)))
title
(search-handler/highlight-exact-query title q)))]])))
(rum/defcs commands < rum/reactive
(rum/local [] ::matched-commands)
[s id format]
@@ -202,56 +244,8 @@
:on-enter (fn []
(page-handler/page-not-exists-handler input id q current-pos))
:item-render (fn [block _chosen?]
(let [block' (if-let [id (:block/uuid block)]
(if-let [e (db/entity [:block/uuid id])]
(assoc e
:block/title (or (:friendly-title block) (:block/title e) (:block/title block))
:alias (:alias block))
block)
block)]
[:div.flex.flex-col
(when (and (:block/uuid block') (or (:block/parent block') (not (:page? block))))
(when-let [breadcrumb (state/get-component :block/breadcrumb)]
[:div.text-xs.opacity-70.mb-1 {:style {:margin-left 3}}
(breadcrumb {:search? true} (state/get-current-repo) (:block/uuid block')
{:disabled? true})]))
[:div.flex.flex-row.items-start
(when-not (or db-tag? (not db-based?))
[:div.flex.items-center.h-5.mr-1.opacity-50
(cond
(:nlp-date? block')
(ui/icon "calendar" {:size 14})
(ldb/class? block')
(ui/icon "hash" {:size 14})
(ldb/property? block')
(ui/icon "letter-p" {:size 14})
(db-model/whiteboard-page? block')
(ui/icon "writing" {:size 14})
(or (ldb/page? block') (:page? block))
(ui/icon "file" {:size 14})
(or (string/starts-with? (str (:block/title block')) (t :new-tag))
(string/starts-with? (str (:block/title block')) (t :new-page)))
(ui/icon "plus" {:size 14})
:else
(ui/icon "letter-n" {:size 14}))])
(let [title (let [alias (get-in block' [:alias :block/title])
title (if (and db-based? (not (ldb/built-in? block')))
(block-handler/block-unique-title block')
(:block/title block'))]
(if alias
(str title " -> alias: " alias)
title))]
(if (or (string/starts-with? title (t :new-tag))
(string/starts-with? title (t :new-page)))
title
(search-handler/highlight-exact-query title q)))]]))
(node-render block q {:db-tag? db-tag?
:db-based? db-based?}))
:empty-placeholder [:div.text-gray-500.text-sm.px-4.py-2 (if db-tag?
"Search for a tag"
"Search for a node")]
@@ -339,12 +333,9 @@
{:on-chosen chosen-handler
:on-enter non-exist-block-handler
:empty-placeholder [:div.text-gray-500.text-sm.px-4.py-2 (t :editor/block-search)]
:item-render (fn [{:block/keys [page uuid title]}]
(let [page-entity (db/entity [:block/uuid page])
repo (state/sub :git/current-repo)
format (get page-entity :block/format :markdown)]
(when-not (string/blank? title)
[:.py-2 (search/block-search-result-item repo uuid format title q :block)])))
:item-render (fn [block]
(node-render block q {:db-tag? false
:db-based? db?}))
:class "ac-block-search"})))
(rum/defcs block-search < rum/reactive

View File

@@ -653,18 +653,6 @@
:close-modal? false
k f'))))
(defn- get-node-icon
[node]
(cond
(ldb/class? node)
"hash"
(ldb/property? node)
"letter-p"
(entity-util/page? node)
"page"
:else
"letter-n"))
(rum/defc ^:large-vars/cleanup-todo select-node < rum/static
[property
{:keys [block multiple-choices? dropdown? input-opts on-input add-new-choice! target] :as opts}
@@ -760,7 +748,7 @@
(block-handler/block-unique-title node))]
(let [title (subs node-title 0 256)
node (or (db/entity id) node)
icon (get-node-icon node)
icon (icon-component/get-node-icon-cp node {})
header (when-not (db/page? node)
(when-let [breadcrumb (state/get-component :block/breadcrumb)]
[:div.text-xs.opacity-70
@@ -769,7 +757,7 @@
label [:div.flex.flex-row.items-center.gap-1
(when-not (or (:logseq.property/classes property)
(contains? #{:class :property} (:logseq.property/type property)))
(ui/icon icon {:size 14}))
icon)
[:div title]]]
[header label]))
[nil (:block/title node)])]

View File

@@ -1,19 +0,0 @@
(ns frontend.components.search
(:require [rum.core :as rum]
[frontend.handler.search :as search-handler]
[frontend.components.block :as block]))
(rum/defc block-search-result-item
[repo uuid format content q search-mode]
(let [content (search-handler/sanity-search-content format content)]
[:div
(when (not= search-mode :page)
[:div {:class "mb-1" :key "parents"}
(block/breadcrumb {:id "block-search-block-parent"
:block? true
:search? true}
repo
uuid
{:indent? false})])
[:div {:class "font-medium" :key "content"}
(search-handler/highlight-exact-query content q)]]))

View File

@@ -13,15 +13,9 @@
[frontend.storage :as storage]
[frontend.util :as util]
[logseq.db :as ldb]
[logseq.graph-parser.text :as text]
[missionary.core :as m]
[promesa.core :as p]))
(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)))
(defn search
"The aggretation of search results"
([q]