diff --git a/deps/db/src/logseq/db.cljs b/deps/db/src/logseq/db.cljs index d64c470dde..0e7a08c1d1 100644 --- a/deps/db/src/logseq/db.cljs +++ b/deps/db/src/logseq/db.cljs @@ -73,7 +73,7 @@ (when (seq tx-data) ;; (prn :debug :transact :sync? (= d/transact! (or @*transact-fn d/transact!)) :tx-meta tx-meta) - ;; (cljs.pprint/pprint tx-data) + ;; (cljs.pprint/pprint tx-data) (let [f (or @*transact-fn d/transact!)] (try diff --git a/deps/graph-parser/src/logseq/graph_parser.cljs b/deps/graph-parser/src/logseq/graph_parser.cljs index cfa5503c69..873430b790 100644 --- a/deps/graph-parser/src/logseq/graph_parser.cljs +++ b/deps/graph-parser/src/logseq/graph_parser.cljs @@ -113,7 +113,9 @@ Options available: (assoc :file/created-at (common-util/time-ms)))]) result (if skip-db-transact? tx - (ldb/transact! conn tx (select-keys options [:new-graph? :from-disk?])))] + (do + (ldb/transact! conn tx (select-keys options [:new-graph? :from-disk?])) + nil))] {:tx result :ast ast}))) diff --git a/deps/graph-parser/src/logseq/graph_parser/block.cljs b/deps/graph-parser/src/logseq/graph_parser/block.cljs index 8f698596b0..dbb2a13d08 100644 --- a/deps/graph-parser/src/logseq/graph_parser/block.cljs +++ b/deps/graph-parser/src/logseq/graph_parser/block.cljs @@ -299,16 +299,12 @@ (defn page-name->map "Create a page's map structure given a original page name (string). map as input is supported for legacy compatibility. - `with-id?`: if true, assign uuid to the map structure. - if the page entity already exists, no-op. - else, if with-id? is a uuid, the uuid is used. - otherwise, generate a uuid. `with-timestamp?`: assign timestampes to the map structure. Useful when creating new pages from references or namespaces, as there's no chance to introduce timestamps via editing in page `skip-existing-page-check?`: if true, allows pages to have the same name" - [original-page-name with-id? db with-timestamp? date-formatter - & {:keys [from-page class? skip-existing-page-check?]}] + [original-page-name db with-timestamp? date-formatter + & {:keys [page-uuid from-page class? skip-existing-page-check?]}] (let [db-based? (ldb/db-based-graph? db)] (cond (and original-page-name (string? original-page-name)) @@ -317,7 +313,7 @@ namespace? (and (not db-based?) (not (boolean (text/get-nested-page-name original-page-name))) (text/namespace-page? original-page-name)) - page-entity (when (and db (not skip-existing-page-check?)) + page-entity (when db (if class? (ldb/get-case-page db original-page-name) (ldb/get-page db original-page-name))) @@ -325,17 +321,18 @@ (merge {:block/name page-name :block/original-name original-page-name} - (when with-id? - (let [new-uuid (or - (cond page-entity (:block/uuid page-entity) - (uuid? with-id?) with-id?) - (d/squuid))] - {:block/uuid new-uuid})) + (let [new-uuid (if skip-existing-page-check? + (d/squuid) + (or + (cond page-entity (:block/uuid page-entity) + (uuid? page-uuid) page-uuid) + (d/squuid)))] + {:block/uuid new-uuid}) (when namespace? (let [namespace (first (common-util/split-last "/" original-page-name))] (when-not (string/blank? namespace) {:block/namespace {:block/name (common-util/page-name-sanity-lc namespace)}}))) - (when (and with-timestamp? (not page-entity)) ;; Only assign timestamp on creating new entity + (when (and with-timestamp? (or skip-existing-page-check? (not page-entity))) ;; Only assign timestamp on creating new entity (let [current-ms (common-util/time-ms)] {:block/created-at current-ms :block/updated-at current-ms})) @@ -347,14 +344,14 @@ (and (map? original-page-name) (:block/uuid original-page-name)) original-page-name - (and (map? original-page-name) with-id?) - (assoc original-page-name :block/uuid (d/squuid)) + (map? original-page-name) + (assoc original-page-name :block/uuid (or page-uuid (d/squuid))) :else nil))) (defn- with-page-refs-and-tags - [{:keys [title body tags refs marker priority] :as block} with-id? db date-formatter] + [{:keys [title body tags refs marker priority] :as block} db date-formatter] (let [db-based? (ldb/db-based-graph? db) refs (->> (concat tags refs (when-not db-based? [marker priority])) (remove string/blank?) @@ -399,10 +396,10 @@ (let [macro? (and (map? item) (= "macro" (:type item)))] (when-not macro? - (let [result (page-name->map item with-id? db true date-formatter) + (let [result (page-name->map item db true date-formatter) page-name (:block/name result) id (get @*name->id page-name)] - (when (and with-id? (nil? id)) + (when (nil? id) (swap! *name->id assoc page-name (:block/uuid result))) (if id (assoc result :block/uuid id) @@ -466,12 +463,12 @@ (defn get-page-refs-from-properties [properties db date-formatter user-config] (let [page-refs (get-page-ref-names-from-properties properties user-config)] - (map (fn [page] (page-name->map page true db true date-formatter)) page-refs))) + (map (fn [page] (page-name->map page db true date-formatter)) page-refs))) (defn- with-page-block-refs - [block with-id? db date-formatter] + [block db date-formatter] (some-> block - (with-page-refs-and-tags with-id? db date-formatter) + (with-page-refs-and-tags db date-formatter) with-block-refs (update :refs (fn [col] (remove nil? col))))) @@ -525,7 +522,7 @@ :block/macros (extract-macros-from-ast body) :block/body body} {:keys [tags refs]} - (with-page-block-refs {:body body :refs property-refs} true db date-formatter)] + (with-page-block-refs {:body body :refs property-refs} db date-formatter)] (cond-> block tags (assoc :block/tags tags) @@ -543,7 +540,7 @@ properties)) (defn- construct-block - [block properties timestamps body encoded-content format pos-meta with-id? {:keys [block-pattern db date-formatter]}] + [block properties timestamps body encoded-content format pos-meta {:keys [block-pattern db date-formatter]}] (let [id (get-custom-id-or-new-id properties) ref-pages-in-properties (->> (:page-refs properties) (remove string/blank?)) @@ -581,7 +578,7 @@ block) block (-> block (assoc :body body) - (with-page-block-refs with-id? db date-formatter) + (with-page-block-refs db date-formatter) (update :tags (fn [tags] (map #(assoc % :block/format format) tags))) (update :refs (fn [refs] (map #(if (map? %) (assoc % :block/format format) %) refs)))) block (update block :refs concat (:block-refs properties)) @@ -635,12 +632,11 @@ Args: `blocks`: mldoc ast. `content`: markdown or org-mode text. - `with-id?`: If `with-id?` equals to true, all the referenced pages will have new db ids. `format`: content's format, it could be either :markdown or :org-mode. `options`: Options supported are :user-config, :block-pattern, :extract-macros, :date-formatter, :page-name, :db-graph-mode? and :db" - [blocks content with-id? format {:keys [user-config db-graph-mode?] :as options}] - {:pre [(seq blocks) (string? content) (boolean? with-id?) (contains? #{:markdown :org} format)]} + [blocks content format {:keys [user-config db-graph-mode?] :as options}] + {:pre [(seq blocks) (string? content) (contains? #{:markdown :org} format)]} (let [encoded-content (utf8/encode content) [blocks body pre-block-properties] (loop [headings [] @@ -668,7 +664,7 @@ (recur headings (rest blocks) timestamps properties body)) (heading-block? block) - (let [block' (construct-block block properties timestamps body encoded-content format pos-meta with-id? options) + (let [block' (construct-block block properties timestamps body encoded-content format pos-meta options) block'' (if db-graph-mode? block' (assoc block' :macros (extract-macros-from-ast (cons block body))))] @@ -806,6 +802,6 @@ (common-util/uuid-string? %) {:block/uuid (uuid %)} :else - (page-name->map % true db true date-formatter)) + (page-name->map % db true date-formatter)) refs') set)))) diff --git a/deps/graph-parser/src/logseq/graph_parser/extract.cljc b/deps/graph-parser/src/logseq/graph_parser/extract.cljc index ff555d8122..ac397a07b0 100644 --- a/deps/graph-parser/src/logseq/graph_parser/extract.cljc +++ b/deps/graph-parser/src/logseq/graph_parser/extract.cljc @@ -161,7 +161,7 @@ page-m (-> (common-util/remove-nils-non-nested (assoc - (gp-block/page-name->map page false db true date-formatter + (gp-block/page-name->map page db true date-formatter :from-page from-page) :block/file {:file/path (common-util/path-normalize file)})) (extract-page-alias-and-tags page-name properties))] @@ -212,7 +212,7 @@ options' (assoc options :page-name page-name) ;; In case of diff-merge (2way) triggered, use the uuids to override the ones extracted from the AST override-uuids (resolve-uuid-fn format ast content options') - blocks (->> (gp-block/extract-blocks ast content false format options') + blocks (->> (gp-block/extract-blocks ast content format options') (attach-block-ids-if-match override-uuids) (mapv #(gp-block/fix-block-id-if-duplicated! db page-name extracted-block-ids %)) ;; FIXME: use page uuid @@ -243,7 +243,7 @@ (when (text/namespace-page? page) (->> (common-util/split-namespace-pages page) (map (fn [page] - (-> (gp-block/page-name->map page true db true date-formatter) + (-> (gp-block/page-name->map page db true date-formatter) (assoc :block/format format)))))))) pages (->> (concat [page-map] @@ -254,7 +254,12 @@ (remove nil?)) pages (common-util/distinct-by :block/name pages) pages (remove nil? pages) - pages (map (fn [page] (assoc page :block/uuid (d/squuid))) pages) + pages (map (fn [page] + (let [page-id (or (when db + (:block/uuid (ldb/get-page db (:block/name page)))) + (d/squuid))] + (assoc page :block/uuid page-id))) + pages) blocks (->> (remove nil? blocks) (map (fn [b] (dissoc b :block/title :block/body :block/level :block/children :block/meta))))] [pages blocks]) diff --git a/deps/graph-parser/test/logseq/graph_parser/block_test.cljs b/deps/graph-parser/test/logseq/graph_parser/block_test.cljs index 05c8ac611b..723ff3d1d0 100644 --- a/deps/graph-parser/test/logseq/graph_parser/block_test.cljs +++ b/deps/graph-parser/test/logseq/graph_parser/block_test.cljs @@ -102,7 +102,7 @@ {}))) "Default to enabled when :property-pages/enabled? is not in config") - (is (= ["tags" "foo" "bar"] + (is (= ["foo" "bar" "tags"] (:page-refs (extract-properties ;; tags is linkable and background-color is not diff --git a/src/main/frontend/components/diff.cljs b/src/main/frontend/components/diff.cljs index 17c3db2cf2..9a1380f32f 100644 --- a/src/main/frontend/components/diff.cljs +++ b/src/main/frontend/components/diff.cljs @@ -36,7 +36,7 @@ [:div.cp__diff-file [:div.cp__diff-file-header [:span.cp__diff-file-header-content.pl-1.font-medium - (str "File " path " has been modified on the disk.")]] + (str "File " path "has been modified on the disk.")]] [:div.p-4 (when (not= (string/trim disk-content) (string/trim db-content)) (ui/foldable diff --git a/src/main/frontend/db_worker.cljs b/src/main/frontend/db_worker.cljs index 0f176352fd..975f50a312 100644 --- a/src/main/frontend/db_worker.cljs +++ b/src/main/frontend/db_worker.cljs @@ -552,7 +552,6 @@ [this repo] (let [conn (worker-state/get-datascript-conn repo) writes @file/*writes] - ;; Clean pages that have been deleted (when conn (swap! file/*writes (fn [writes] diff --git a/src/main/frontend/format/block.cljs b/src/main/frontend/format/block.cljs index 3d17bbc69e..2d3c04fee3 100644 --- a/src/main/frontend/format/block.cljs +++ b/src/main/frontend/format/block.cljs @@ -16,11 +16,10 @@ (defn extract-blocks "Wrapper around logseq.graph-parser.block/extract-blocks that adds in system state and handles unexpected failure." - [blocks content format {:keys [with-id? page-name] - :or {with-id? true}}] + [blocks content format {:keys [page-name]}] (let [repo (state/get-current-repo)] (try - (gp-block/extract-blocks blocks content with-id? format + (gp-block/extract-blocks blocks content format {:user-config (state/get-config) :block-pattern (config/get-block-pattern format) :db (db/get-db repo) @@ -36,10 +35,10 @@ and handles unexpected failure." (defn page-name->map "Wrapper around logseq.graph-parser.block/page-name->map that adds in db" - ([original-page-name with-id?] - (page-name->map original-page-name with-id? true)) - ([original-page-name with-id? with-timestamp?] - (gp-block/page-name->map original-page-name with-id? (db/get-db (state/get-current-repo)) with-timestamp? (state/get-date-formatter)))) + ([original-page-name] + (page-name->map original-page-name true)) + ([original-page-name with-timestamp?] + (gp-block/page-name->map original-page-name (db/get-db (state/get-current-repo)) with-timestamp? (state/get-date-formatter)))) (defn- normalize-as-percentage [block] @@ -69,23 +68,20 @@ and handles unexpected failure." (first))) (defn parse-block - ([block] - (parse-block block nil)) - ([{:block/keys [uuid content format] :as block} {:keys [with-id?] - :or {with-id? true}}] - (when-not (string/blank? content) - (let [block (dissoc block :block/pre-block?) - format (or format :markdown) - parse-config (mldoc/get-default-config format) - ast (format/to-edn content format parse-config) - blocks (extract-blocks ast content format {:with-id? with-id?}) - new-block (first blocks) - block (cond-> - (merge block new-block) - (> (count blocks) 1) - (assoc :block/warning :multiple-blocks)) - block (dissoc block :block/title :block/body :block/level)] - (if uuid (assoc block :block/uuid uuid) block))))) + [{:block/keys [uuid content format] :as block}] + (when-not (string/blank? content) + (let [block (dissoc block :block/pre-block?) + format (or format :markdown) + parse-config (mldoc/get-default-config format) + ast (format/to-edn content format parse-config) + blocks (extract-blocks ast content format {}) + new-block (first blocks) + block (cond-> + (merge block new-block) + (> (count blocks) 1) + (assoc :block/warning :multiple-blocks)) + block (dissoc block :block/title :block/body :block/level)] + (if uuid (assoc block :block/uuid uuid) block)))) (defn parse-title-and-body ([block] diff --git a/src/main/frontend/fs/watcher_handler.cljs b/src/main/frontend/fs/watcher_handler.cljs index 7377e98e39..b7be265e8b 100644 --- a/src/main/frontend/fs/watcher_handler.cljs +++ b/src/main/frontend/fs/watcher_handler.cljs @@ -19,7 +19,8 @@ [logseq.common.config :as common-config] [logseq.common.util.block-ref :as block-ref] [promesa.core :as p] - [frontend.db.async :as db-async])) + [frontend.db.async :as db-async] + [frontend.db.transact :as db-transact])) ;; all IPC paths must be normalized! (via common-util/path-normalize) @@ -59,84 +60,87 @@ (defn handle-changed! [type {:keys [dir path content stat global-dir] :as payload}] - (when dir - (let [;; Global directory events don't know their originating repo so we rely + (let [repo (state/get-current-repo) + ^js sqlite @state/*db-worker] + (p/let [writes-finished? (when sqlite (.file-writes-finished? sqlite repo))] + (when (and dir writes-finished? (db-transact/request-finished?)) + (let [;; Global directory events don't know their originating repo so we rely ;; on the client to correctly identify it - repo (cond - global-dir (state/get-current-repo) + repo (cond + global-dir (state/get-current-repo) ;; FIXME(andelf): hack for demo graph, demo graph does not bind to local directory - (string/starts-with? dir "memory://") "Logseq demo" - :else (config/get-local-repo dir)) - repo-dir (config/get-local-dir repo) - {:keys [mtime]} stat - ext (keyword (path/file-ext path))] - (when (contains? #{:org :md :markdown :css :js :edn :excalidraw :tldr} ext) - (p/let [db-content (db-async/ (gp-block/page-name->map title uuid @conn true date-formatter - {:skip-existing-page-check? true}) + page (-> (gp-block/page-name->map title @conn true date-formatter + {:page-uuid uuid + :skip-existing-page-check? true}) (assoc :block/format :markdown)) result (ldb/transact! conn [page] {:persist-op? false :outliner-op :create-page})] diff --git a/src/main/frontend/worker/handler/page/db_based/page.cljs b/src/main/frontend/worker/handler/page/db_based/page.cljs index 2d772f6eb2..c2fcad529e 100644 --- a/src/main/frontend/worker/handler/page/db_based/page.cljs +++ b/src/main/frontend/worker/handler/page/db_based/page.cljs @@ -68,11 +68,12 @@ persist-op? true} :as options}] (let [date-formatter (common-config/get-date-formatter config) - [title page-name] (get-title-and-pagename title) - with-uuid? (if (uuid? uuid) uuid true)] + [title page-name] (get-title-and-pagename title)] (when-not (ldb/get-case-page @conn page-name) (let [format :markdown - page (-> (gp-block/page-name->map title with-uuid? @conn true date-formatter :class? class?) + page (-> (gp-block/page-name->map title @conn true date-formatter + {:class? class? + :page-uuid (when (uuid? uuid) uuid)}) (assoc :block/format format)) page-uuid (:block/uuid page) page-txs (build-page-tx conn properties page (select-keys options [:whiteboard? :class? :tags])) @@ -90,4 +91,4 @@ today-journal? (assoc :create-today-journal? true :today-journal-name page-name)))])] - [result page-name page-uuid])))) \ No newline at end of file + [result page-name page-uuid])))) diff --git a/src/main/frontend/worker/handler/page/file_based/page.cljs b/src/main/frontend/worker/handler/page/file_based/page.cljs index 35c392778c..afdaac0959 100644 --- a/src/main/frontend/worker/handler/page/file_based/page.cljs +++ b/src/main/frontend/worker/handler/page/file_based/page.cljs @@ -75,8 +75,7 @@ (let [date-formatter (common-config/get-date-formatter config) split-namespace? (not (or (string/starts-with? title "hls__") (date/valid-journal-title? date-formatter title))) - [title page-name] (get-title-and-pagename title) - with-uuid? (if (uuid? uuid) uuid true)] + [title page-name] (get-title-and-pagename title)] (when-not (ldb/get-page @conn page-name) (let [pages (if split-namespace? (common-util/split-namespace-pages title) @@ -84,7 +83,8 @@ format (or format (common-config/get-preferred-format config)) pages (map (fn [page] ;; only apply uuid to the deepest hierarchy of page to create if provided. - (-> (gp-block/page-name->map page (if (= page title) with-uuid? true) @conn true date-formatter) + (-> (gp-block/page-name->map page @conn true date-formatter + {:page-uuid (when (uuid? uuid) uuid)}) (assoc :block/format format))) pages) txs (->> pages diff --git a/src/test/frontend/db/model_test.cljs b/src/test/frontend/db/model_test.cljs index 32a112fca9..a543cb0ed8 100644 --- a/src/test/frontend/db/model_test.cljs +++ b/src/test/frontend/db/model_test.cljs @@ -79,7 +79,7 @@ - link to ns [[one]] - link to page one [[page ONE]]"}]) - (is (= '("one/two/tree" "page one") + (is (= '("one/two/tree" "tags" "page one") (map second (model/get-pages-relation test-helper/test-db true))) "(get-pages-relation) Must be only ns one/two/tree")