Merge remote-tracking branch 'origin/feat/db' into fix/multiple-tabs

This commit is contained in:
rcmerci
2025-04-18 18:18:36 +08:00
16 changed files with 150 additions and 137 deletions

View File

@@ -465,11 +465,29 @@
(when (seq ref-ids)
(d/pull-many db '[*] ref-ids))))
(defn hidden-ref?
"Whether ref-block (for block with the `id`) should be hidden."
[db ref-block id]
(let [db-based? (entity-plus/db-based-graph? db)]
(if db-based?
(let [entity (d/entity db id)]
(or
(= (:db/id ref-block) id)
(= id (:db/id (:block/page ref-block)))
(hidden? (:block/page ref-block))
(hidden? ref-block)
(contains? (set (map :db/id (:block/tags ref-block))) (:db/id entity))
(some? (get ref-block (:db/ident entity)))))
(or
(= (:db/id ref-block) id)
(= id (:db/id (:block/page ref-block)))))))
(defn get-block-refs-count
[db id]
(some-> (d/entity db id)
:block/_refs
count))
(some->> (d/entity db id)
:block/_refs
(remove (fn [ref-block] (hidden-ref? db ref-block id)))
count))
(defn hidden-or-internal-tag?
[e]

View File

@@ -299,24 +299,12 @@
(defn- get-linked-references
[db id]
(let [db-based? (ldb/db-based-graph? db)
entity (d/entity db id)
(let [entity (d/entity db id)
ids (set (cons id (ldb/get-block-alias db id)))
refs (mapcat (fn [id] (:block/_refs (d/entity db id))) ids)
page-filters (get-filters db entity)
full-ref-blocks (->> refs
(remove (fn [block]
(if db-based?
(or
(= (:db/id block) id)
(= id (:db/id (:block/page block)))
(ldb/hidden? (:block/page block))
(ldb/hidden? block)
(contains? (set (map :db/id (:block/tags block))) (:db/id entity))
(some? (get block (:db/ident entity))))
(or
(= (:db/id block) id)
(= id (:db/id (:block/page block)))))))
(remove (fn [block] (ldb/hidden-ref? db block id)))
(common-util/distinct-by :db/id))
ref-blocks (cond->> full-ref-blocks
(seq page-filters)

View File

@@ -296,6 +296,7 @@
(let [heading (str "Heading " level)]
[heading (->heading level) heading (str "h-" level) "Heading"])) (range 1 7)))
(defonce *latest-matched-command (atom ""))
(defonce *matched-commands (atom nil))
(defonce *initial-commands (atom nil))
@@ -471,12 +472,18 @@
(defn init-commands!
[get-page-ref-text]
(let [commands (commands-map get-page-ref-text)]
(reset! *latest-matched-command "")
(reset! *initial-commands commands)
(reset! *matched-commands commands)))
(defn set-matched-commands!
[command matched-commands]
(reset! *latest-matched-command command)
(reset! *matched-commands matched-commands))
(defn reinit-matched-commands!
[]
(reset! *matched-commands @*initial-commands))
(set-matched-commands! "" @*initial-commands))
(defn restore-state
[]

View File

@@ -2977,28 +2977,10 @@
(rum/defcs ^:large-vars/cleanup-todo block-content-or-editor < rum/reactive
(rum/local false ::hover?)
{:init (fn [state]
(let [block (second (:rum/args state))
config (first (:rum/args state))
current-block-page? (= (str (:block/uuid block)) (state/get-current-page))
embed-self? (and (:embed? config)
(= (:block/uuid block) (:block/uuid (:block config))))
default-hide? (or (not (and current-block-page? (not embed-self?) (state/auto-expand-block-refs?)))
(= (str (:id config)) (str (:block/uuid block))))
*refs-count (atom nil)]
(when-let [id (:db/id block)]
(p/let [count (db-async/<get-block-refs-count (state/get-current-repo) id)]
(reset! *refs-count count)))
(assoc state
::hide-block-refs? (atom default-hide?)
::refs-count *refs-count)))}
[state config {:block/keys [uuid] :as block} {:keys [edit-input-id block-id edit? hide-block-refs-count?]}]
[state config {:block/keys [uuid] :as block} {:keys [edit-input-id block-id edit? hide-block-refs-count? refs-count *hide-block-refs?]}]
(let [format (if (config/db-based-graph? (state/get-current-repo))
:markdown
(or (:block/format block) :markdown))
*hide-block-refs? (get state ::hide-block-refs?)
*refs-count (get state ::refs-count)
hide-block-refs? (rum/react *hide-block-refs?)
editor-box (state/get-component :editor/box)
editor-id (str "editor-" edit-input-id)
slide? (:slide? config)
@@ -3009,9 +2991,6 @@
named? (some? (:block/name block))
repo (state/get-current-repo)
db-based? (config/db-based-graph? repo)
refs-count (if (seq (:block/_refs block))
(count (remove :logseq.property/view-for (:block/_refs block)))
(rum/react *refs-count))
table? (:table? config)
raw-mode-block (state/sub :editor/raw-mode-block)
type-block-editor? (and (contains? #{:code} (:logseq.property.node/display-type block))
@@ -3076,14 +3055,7 @@
(when-not (or (:block-ref? config) (:table? config) (:gallery-view? config)
(:property? config))
(when (and db-based? (seq (:block/tags block)))
(tags-cp (assoc config :block/uuid (:block/uuid block)) block)))]]
(when (and (not (or (:table? config) (:property? config)))
(not hide-block-refs?)
(> refs-count 0)
(not (:page-title? config)))
(when-let [refs-cp (state/get-component :block/linked-references)]
(refs-cp uuid)))]]))
(tags-cp (assoc config :block/uuid (:block/uuid block)) block)))]]]]))
(rum/defcs single-block-cp < mixins/container-id
[state _config block-uuid]
@@ -3432,10 +3404,30 @@
(rum/defcs ^:large-vars/cleanup-todo block-container-inner-aux < rum/reactive db-mixins/query
{:init (fn [state]
(let [*ref (atom nil)]
(assoc state ::ref *ref)))}
(let [*ref (atom nil)
[_container-state _repo config block] (:rum/args state)
current-block-page? (= (str (:block/uuid block)) (state/get-current-page))
embed-self? (and (:embed? config)
(= (:block/uuid block) (:block/uuid (:block config))))
default-hide? (or (not (and current-block-page? (not embed-self?) (state/auto-expand-block-refs?)))
(= (str (:id config)) (str (:block/uuid block))))
*refs-count (atom nil)]
(when-not (:view? config)
(when-let [id (:db/id block)]
(p/let [count (db-async/<get-block-refs-count (state/get-current-repo) id)]
(reset! *refs-count count))))
(assoc state
::ref *ref
::hide-block-refs? (atom default-hide?)
::refs-count *refs-count)))}
[state container-state repo config* block {:keys [navigating-block navigated? editing? selected?] :as opts}]
(let [*ref (::ref state)
*hide-block-refs? (get state ::hide-block-refs?)
*refs-count (get state ::refs-count)
hide-block-refs? (rum/react *hide-block-refs?)
refs-count (if (seq (:block/_refs block))
(count (remove :logseq.property/view-for (:block/_refs block)))
(rum/react *refs-count))
[original-block block] (build-block config* block {:navigating-block navigating-block :navigated? navigated?})
config* (if original-block
(assoc config* :original-block original-block)
@@ -3613,6 +3605,8 @@
{:edit-input-id edit-input-id
:block-id block-id
:edit? editing?
:refs-count refs-count
:*hide-block-refs? *hide-block-refs?
:hide-block-refs-count? hide-block-refs-count?}))])]
(when (and db-based? (not collapsed?) (not (or table? property?)))
@@ -3630,6 +3624,14 @@
[:div (when-not (:page-title? config) {:style {:padding-left 45}})
(db-properties-cp config block {:in-block-container? true})])
(when (and (not (or (:table? config) (:property? config)))
(not hide-block-refs?)
(> refs-count 0)
(not (:page-title? config)))
(when-let [refs-cp (state/get-component :block/linked-references)]
[:div.px-4.py-2.border.rounded.my-2.shadow-xs {:style {:margin-left 42}}
(refs-cp block {})]))
(when (and db-based? (not collapsed?) (not (or table? property?))
(ldb/class-instance? (entity-plus/entity-memoized (db/get-db) :logseq.class/Query) block))
(let [query-block (:logseq.property/query (db/entity (:db/id block)))

View File

@@ -38,6 +38,8 @@
[react-draggable]
[rum.core :as rum]))
(defonce no-matched-commands [["No matched commands" [[:editor/move-cursor-to-end]]]])
(defn filter-commands
[page? commands]
(if page?
@@ -56,7 +58,7 @@
_ (when (state/get-editor-action)
(reset! *matched matched'))
page? (db/page? (db/entity (:db/id (state/get-edit-block))))
matched (filter-commands page? @*matched)
matched (or (filter-commands page? @*matched) no-matched-commands)
filtered? (not= matched @commands/*initial-commands)]
(ui/auto-complete
matched

View File

@@ -185,7 +185,7 @@
.property-value-inner {
.select-item {
@apply flex items-center shrink;
@apply flex items-center shrink whitespace-nowrap;
}
&[data-type] {

View File

@@ -1,55 +1,18 @@
(ns frontend.components.reference
(:require [frontend.common.missionary :as c.m]
[frontend.components.block :as block]
[frontend.components.content :as content]
[frontend.components.editor :as editor]
[frontend.components.reference-filters :as filters]
[frontend.components.views :as views]
[frontend.config :as config]
[frontend.db :as db]
[frontend.db-mixins :as db-mixins]
[frontend.db.async :as db-async]
[frontend.db.utils :as db-utils]
[frontend.state :as state]
[frontend.ui :as ui]
[logseq.db.common.view :as db-view]
[logseq.shui.hooks :as hooks]
[logseq.shui.ui :as shui]
[missionary.core :as m]
[promesa.core :as p]
[rum.core :as rum]))
;; TODO: merge both page and block linked refs
(rum/defc block-linked-references-aux < rum/reactive db-mixins/query
[e]
(let [block-id (:block/uuid e)
ref-blocks (-> (db/get-referenced-blocks (:db/id e))
db-utils/group-by-page)]
(when (> (count ref-blocks) 0)
(let [ref-hiccup (block/->hiccup ref-blocks
{:id (str block-id)
:ref? true
:breadcrumb-show? true
:group-by-page? true
:editor-box editor/box}
{})]
[:div.references-blocks
(content/content block-id
{:hiccup ref-hiccup})]))))
(rum/defc block-linked-references
[block-id]
(when-let [e (db/entity [:block/uuid block-id])]
(let [[loading? set-loading!] (hooks/use-state true)]
(hooks/use-effect!
(fn []
(p/do!
(db-async/<get-block-refs (state/get-current-repo) (:db/id e))
(set-loading! false)))
[])
(when-not loading?
(block-linked-references-aux e)))))
(rum/defc references-aux
[page-entity config]
(let [filters (db-view/get-filters (db/get-db) page-entity)
@@ -85,9 +48,9 @@
:config config})))
(rum/defc references-cp < rum/reactive db-mixins/query
[page-entity config]
(let [page (db/sub-block (:db/id page-entity))]
(references-aux page config)))
[entity config]
(let [block (db/sub-block (:db/id entity))]
(references-aux block config)))
(rum/defc references
[entity config]

View File

@@ -1492,8 +1492,8 @@
(db/entity [:block/uuid (:block/uuid result)]))))
(rum/defc views-tab < rum/reactive db-mixins/query
[view-parent current-view {:keys [views data items-count set-view-entity! set-data! set-views! view-feature-type show-items-count?]} hover?]
[:div.views.flex.flex-row.items-center.flex-wrap.gap-2
[view-parent current-view {:keys [views data items-count set-view-entity! set-data! set-views! view-feature-type show-items-count? references? opacity]}]
[:div.views
(for [view* views]
(let [view (db/sub-block (:db/id view*))
current-view? (= (:db/id current-view) (:db/id view))]
@@ -1529,11 +1529,12 @@
(do
(set-view-entity! view)
(set-data! nil))))}
(let [display-type (or (:db/ident (get view :logseq.property.view/type))
:logseq.property.view/type.table)]
(when-let [icon (:logseq.property/icon (db/entity display-type))]
(icon-component/icon icon {:color? true
:size 15})))
(when-not references?
(let [display-type (or (:db/ident (get view :logseq.property.view/type))
:logseq.property.view/type.table)]
(when-let [icon (:logseq.property/icon (db/entity display-type))]
(icon-component/icon icon {:color? true
:size 15}))))
(let [title (:block/title view)]
(if (= title "")
"New view"
@@ -1546,8 +1547,7 @@
{:variant :text
:size :sm
:title "Add new view"
:class (str "!px-1 -ml-1 text-muted-foreground hover:text-foreground transition-opacity ease-in duration-300 "
(if hover? "opacity-100" "opacity-75"))
:class (str "!px-1 -ml-1 text-muted-foreground hover:text-foreground transition-opacity ease-in duration-300 " opacity)
:on-click (fn []
(p/let [view (create-view! view-parent view-feature-type)]
(set-views! (concat views [view]))))}
@@ -1559,8 +1559,13 @@
{:keys [view-feature-type title-key additional-actions]
:as option}]
(let [[hover? set-hover?] (hooks/use-state nil)
db-based? (config/db-based-graph? (state/get-current-repo))]
[:div.flex.flex-1.flex-wrap.items-center.justify-between.gap-1
db-based? (config/db-based-graph? (state/get-current-repo))
references? (contains? #{:linked-references :unlinked-references} view-feature-type)
opacity (cond
(and references? (not hover?)) "opacity-0"
hover? "opacity-100"
:else "opacity-75")]
[:div.flex.flex-1.flex-nowrap.items-center.justify-between.gap-1.overflow-hidden
{:on-mouse-over #(set-hover? true)
:on-mouse-out #(set-hover? false)}
[:div.flex.flex-row.items-center.gap-2
@@ -1569,7 +1574,10 @@
[:div.font-medium.opacity-50.text-sm
(t (or title-key :views.table/default-title)
(count (:rows table)))]
(views-tab view-parent view-entity option hover?))
(views-tab view-parent view-entity (assoc option
:hover? hover?
:opacity opacity
:references? references?)))
[:div.font-medium.text-sm
[:span
(case view-feature-type
@@ -1579,7 +1587,7 @@
"Nodes")]
[:span.ml-1 (count (:rows table))]])]
[:div.view-actions.flex.items-center.gap-1.transition-opacity.ease-in.duration-300
{:class (if hover? "opacity-100" "opacity-75")}
{:class opacity}
(when (seq additional-actions)
[:<> (for [action additional-actions]

View File

@@ -1,3 +1,7 @@
.views {
@apply flex flex-row items-center flex-nowrap gap-2 overflow-hidden;
}
.ls-view-order-setting {
button.order-button {
width: 107px;

View File

@@ -66,7 +66,6 @@
(let [*open? (::open? state)
page-entity (model/get-page page-name-or-uuid)
page (model/sub-block (:db/id page-entity))
block-uuid (:block/uuid page-entity)
refs-count (count (:block/_refs page))]
(when (> refs-count 0)
(shui/popover
@@ -81,7 +80,7 @@
{:on-open-auto-focus #(.preventDefault %)}
(shui/popover-arrow {:class-name "popper-arrow"})
[:div {:class classname}
(reference/block-linked-references block-uuid)])))))))
(reference/references page {})])))))))
;; This is not accurate yet
(defn- get-page-human-update-time

View File

@@ -120,6 +120,7 @@
(cond
(and (:block.temp/fully-loaded? e) ; children may not be fully loaded
(not children-only?)
(not children?)
(not nested-children?)
(not (some #{:block.temp/refs-count} properties)))
(p/promise e)

View File

@@ -118,7 +118,7 @@
[]
(state/set-page-blocks-cp! page/page-cp)
(state/set-component! :block/->hiccup block/->hiccup)
(state/set-component! :block/linked-references reference/block-linked-references)
(state/set-component! :block/linked-references reference/references)
(state/set-component! :whiteboard/tldraw-preview whiteboard/tldraw-preview)
(state/set-component! :block/single-block block/single-block-cp)
(state/set-component! :block/container block/block-container)

View File

@@ -1773,23 +1773,25 @@
[property q]
(search/property-value-search property q))
(defn get-matched-commands
(defn get-last-command
[input]
(try
(let [edit-content (or (gobj/get input "value") "")
pos (cursor/pos input)
last-slash-caret-pos (:pos (:pos (state/get-editor-action-data)))
last-command (and last-slash-caret-pos (subs edit-content last-slash-caret-pos pos))]
(when (> pos 0)
(or
(and (= commands/command-trigger (util/nth-safe edit-content (dec pos)))
@commands/*initial-commands)
(and last-command
(commands/get-matched-commands last-command)))))
(when (> pos 0) last-command))
(catch :default e
(js/console.error e)
nil)))
(defn get-matched-commands
[command]
(condp = command
nil nil
"" @commands/*initial-commands
(commands/get-matched-commands command)))
(defn auto-complete?
[]
(or @*asset-uploading?
@@ -1929,9 +1931,9 @@
(defn resize-image!
[config block-id metadata full_text size]
(let [asset (:asset-block config)]
(if (and asset (config/db-based-graph?))
(if (config/db-based-graph?)
(property-handler/set-block-property! (state/get-current-repo)
(:db/id asset)
(if asset (:db/id asset) block-id)
:logseq.property.asset/resize-metadata
size)
(let [new-meta (merge metadata size)
@@ -3206,10 +3208,13 @@
(and (= :commands (state/get-editor-action)) (not= k commands/command-trigger))
(if (= commands/command-trigger (second (re-find #"(\S+)\s+$" value)))
(state/clear-editor-action!)
(let [matched-commands (get-matched-commands input)]
(let [command (get-last-command input)
matched-commands (get-matched-commands command)]
(if (seq matched-commands)
(reset! commands/*matched-commands matched-commands)
(state/clear-editor-action!))))
(commands/set-matched-commands! command matched-commands)
(if (> (- (count command) (count @commands/*latest-matched-command)) 2)
(state/clear-editor-action!)
(reset! commands/*matched-commands nil)))))
:else
(default-case-for-keyup-handler input current-pos k code is-processed?))

View File

@@ -1,21 +1,22 @@
(ns frontend.handler.file-sync
"Provides util handler fns for file sync"
(:require ["path" :as node-path]
[cljs-time.coerce :as tc]
[cljs-time.core :as t]
[cljs-time.format :as tf]
[cljs.core.async :as async :refer [go <!]]
[cljs.core.async.interop :refer [p->c]]
[clojure.string :as string]
[frontend.config :as config]
[frontend.db :as db]
[frontend.fs :as fs]
[frontend.fs.sync :as sync]
[frontend.handler.notification :as notification]
[frontend.state :as state]
[frontend.handler.user :as user]
[frontend.fs :as fs]
[frontend.pubsub :as pubsub]
[cljs-time.coerce :as tc]
[cljs-time.core :as t]
[frontend.state :as state]
[frontend.storage :as storage]
[frontend.util :as util]
[lambdaisland.glogi :as log]
[logseq.common.path :as path]))
@@ -23,7 +24,6 @@
(def refresh-file-sync-component (atom false))
(defn get-current-graph-uuid []
(state/get-current-file-sync-graph-uuid))
@@ -100,10 +100,12 @@
(defn load-session-graphs
[]
(when-not (state/sub [:file-sync/remote-graphs :loading])
(go (state/set-state! [:file-sync/remote-graphs :loading] true)
(go
(when-not util/web-platform?
(state/set-state! [:file-sync/remote-graphs :loading] true)
(let [graphs-or-exp (<! (<list-graphs))]
(when-not (instance? ExceptionInfo graphs-or-exp)
(state/set-state! :file-sync/remote-graphs {:loading false :graphs graphs-or-exp}))))))
(state/set-state! :file-sync/remote-graphs {:loading false :graphs graphs-or-exp})))))))
(defn reset-session-graphs
[]
@@ -181,7 +183,6 @@
>))]
all-version-list)))))
(defn init-remote-graph
[local-graph-dir graph]
(when (and local-graph-dir graph)

View File

@@ -84,7 +84,7 @@
[]
(state/set-page-blocks-cp! page-component/page-cp)
(state/set-component! :block/->hiccup block/->hiccup)
(state/set-component! :block/linked-references reference/block-linked-references)
(state/set-component! :block/linked-references reference/references)
(state/set-component! :whiteboard/tldraw-preview whiteboard/tldraw-preview)
(state/set-component! :block/single-block block/single-block-cp)
(state/set-component! :block/container block/block-container)

View File

@@ -82,8 +82,10 @@
(state/set-editor-action! action)
;; Default cursor pos to end of line
(let [pos (or cursor-pos (count value))
input #js {:value value}]
(with-redefs [editor/get-matched-commands (constantly commands)
input #js {:value value}
command (subs value 1)]
(with-redefs [editor/get-last-command (constantly command)
editor/get-matched-commands (constantly commands)
;; Ignore as none of its behaviors are tested
editor/default-case-for-keyup-handler (constantly nil)
cursor/pos (constantly pos)]
@@ -93,18 +95,31 @@
(deftest keyup-handler-test
(testing "Command autocompletion"
;; default last matching command is ""
(keyup-handler {:value "/z"
:action :commands
:commands []})
(is (= :commands (state/get-editor-action))
"Completion stays open if no matches but differs from last success by <= 2 chars")
(keyup-handler {:value "/zz"
:action :commands
:commands []})
(is (= :commands (state/get-editor-action))
"Completion stays open if no matches but differs from last success by <= 2 chars")
(keyup-handler {:value "/zzz"
:action :commands
:commands []})
(is (= nil (state/get-editor-action))
"Completion closed if no matches and > 2 chars form last success")
(keyup-handler {:value "/b"
:action :commands
:commands [:fake-command]})
(is (= :commands (state/get-editor-action))
"Completion stays open if there is a matching command")
(keyup-handler {:value "/zz"
:action :commands
:commands []})
(is (= nil (state/get-editor-action))
"Completion closed if there no matching commands")
(keyup-handler {:value "/ " :action :commands})
(is (= nil (state/get-editor-action))
"Completion closed after a space follows /")