Pārlūkot izejas kodu

wip: rtc worker

Tienson Qin 2 gadi atpakaļ
vecāks
revīzija
7f21ff7193

+ 1 - 39
src/main/frontend/handler/file_based/property/util.cljs

@@ -107,45 +107,7 @@
   (let [from (cursor/pos input)]
     (cursor/move-cursor-to-thing input properties-end from)))
 
-(defn remove-properties
-  [format content]
-  (cond
-    (gp-property/contains-properties? content)
-    (let [lines (string/split-lines content)
-          [title-lines properties&body] (split-with #(-> (string/triml %)
-                                                         string/upper-case
-                                                         (string/starts-with? properties-start)
-                                                         not)
-                                                    lines)
-          body (drop-while #(-> (string/trim %)
-                                string/upper-case
-                                (string/starts-with? properties-end)
-                                not
-                                (or (string/blank? %)))
-                           properties&body)
-          body (if (and (seq body)
-                        (-> (first body)
-                            string/triml
-                            string/upper-case
-                            (string/starts-with? properties-end)))
-                 (let [line (string/replace (first body) #"(?i):END:\s?" "")]
-                   (if (string/blank? line)
-                     (rest body)
-                     (cons line (rest body))))
-                 body)]
-      (->> (concat title-lines body)
-           (string/join "\n")))
-
-    (not= format :org)
-    (let [lines (string/split-lines content)
-          lines (if (simplified-property? (first lines))
-                  (drop-while simplified-property? lines)
-                  (cons (first lines)
-                        (drop-while simplified-property? (rest lines))))]
-      (string/join "\n" lines))
-
-    :else
-    content))
+(def remove-properties wpu/remove-properties)
 
 ;; title properties body
 (defn with-built-in-properties

+ 20 - 37
src/main/frontend/modules/outliner/datascript.cljs

@@ -1,15 +1,15 @@
 (ns frontend.modules.outliner.datascript
-  (:require [frontend.db :as db]
-            [frontend.modules.outliner.pipeline :as pipelines]
-            [frontend.modules.editor.undo-redo :as undo-redo]
-            [frontend.state :as state]
-            [frontend.config :as config]
-            [logseq.graph-parser.util :as gp-util]
+  (:require [logseq.graph-parser.util :as gp-util]
+            [logseq.graph-parser.util.block-ref :as block-ref]
             [lambdaisland.glogi :as log]
             [clojure.string :as string]
-            [frontend.util :as util]
-            [logseq.graph-parser.util.block-ref :as block-ref]
-            [frontend.handler.file-based.property.util :as property-util]))
+            [frontend.worker.util :as worker-util]
+            [logseq.db.sqlite.util :as sqlite-util]
+            [frontend.worker.file.property-util :as wpu]
+            [datascript.core :as d]
+            [frontend.state :as state]
+            [frontend.config :as config]
+            [frontend.db :as db]))
 
 (defn new-outliner-txs-state [] (atom []))
 
@@ -19,19 +19,6 @@
    (instance? cljs.core/Atom state)
    (coll? @state)))
 
-(defn after-transact-pipelines
-  [{:keys [tx-meta] :as opts}]
-  (when-not config/test?
-    (when-let [replace-tx-data (:replace-tx-data opts)]
-      (db/transact! (:repo opts) replace-tx-data (:replace-tx-meta opts)))
-
-    (pipelines/invoke-hooks opts)
-
-    (when (or (:outliner/transact? tx-meta)
-              (:outliner-op tx-meta)
-              (:whiteboard/transact? tx-meta))
-      (undo-redo/listen-db-changes! opts))))
-
 (defn- remove-nil-from-transaction
   [txs]
   (some->> (gp-util/remove-nils txs)
@@ -49,21 +36,21 @@
 
 (defn update-refs-and-macros
   "When a block is deleted, refs are updated and macros associated with the block are deleted"
-  [txs repo opts]
+  [txs db repo opts]
   (if (= :delete-blocks (:outliner-op opts))
     (let [retracted-block-ids (->> (keep (fn [tx]
                                            (when (and (vector? tx)
                                                       (= :db.fn/retractEntity (first tx)))
                                              (second tx))) txs))
-          retracted-blocks (map db/entity retracted-block-ids)
+          retracted-blocks (map #(d/entity db %) retracted-block-ids)
           retracted-tx (->> (for [block retracted-blocks]
                               (let [refs (:block/_refs block)]
                                 (map (fn [ref]
                                        (let [id (:db/id ref)
-                                             block-content (property-util/remove-properties
+                                             block-content (wpu/remove-properties
                                                             (:block/format block) (:block/content block))
                                              new-content (some-> (:block/content ref)
-                                                                 (string/replace (re-pattern (util/format "(?i){{embed \\(\\(%s\\)\\)\\s?}}" (str (:block/uuid block))))
+                                                                 (string/replace (re-pattern (worker-util/format "(?i){{embed \\(\\(%s\\)\\)\\s?}}" (str (:block/uuid block))))
                                                                                  block-content)
                                                                  (string/replace (block-ref/->block-ref (str (:block/uuid block)))
                                                                                  block-content))
@@ -83,13 +70,13 @@
           revert-tx (mapcat :revert-tx retracted-tx)
           macros-tx (mapcat (fn [b]
                               ;; Only delete if last reference
-                              (keep #(when (<= (count (:block/_macros (db/entity repo (:db/id %))))
+                              (keep #(when (<= (count (:block/_macros (d/entity db (:db/id %))))
                                                1)
                                        (vector :db.fn/retractEntity (:db/id %)))
                                     (:block/macros b)))
                             retracted-blocks)]
       (when (seq retracted-tx')
-        (state/set-state! [:editor/last-replace-ref-content-tx (state/get-current-repo)]
+        (state/set-state! [:editor/last-replace-ref-content-tx repo]
                           {:retracted-block-ids retracted-block-ids
                            :revert-tx revert-tx}))
       (concat txs retracted-tx' macros-tx))
@@ -98,7 +85,8 @@
 (defn transact!
   [txs opts before-editor-cursor]
   (let [repo (state/get-current-repo)
-        db-based? (config/db-based-graph? repo)
+        conn (db/get-db repo false)
+        db-based? (sqlite-util/db-based-graph? repo)
         txs (map (fn [m]
                    (if (map? m)
                      (dissoc m :block/children :block/meta :block/top? :block/bottom? :block/anchor
@@ -108,7 +96,7 @@
         txs (remove-nil-from-transaction txs)
         txs (cond-> txs
               (= :delete-blocks (:outliner-op opts))
-              (update-refs-and-macros repo opts)
+              (update-refs-and-macros @conn repo opts)
 
               true
               (distinct))]
@@ -116,13 +104,8 @@
                (or db-based?
                    (not (contains? (:file/unlinked-dirs @state/state)
                                    (config/get-repo-dir repo)))))
-
-      ;; (prn :debug "Outliner transact:")
-      ;; (frontend.util/pprint {:txs txs :opts opts})
-
       (try
-        (let [repo (get opts :repo (state/get-current-repo))
-              rs (db/transact! repo txs (assoc opts :outliner/transact? true))
+        (let [rs (d/transact! conn txs (assoc opts :outliner/transact? true))
               tx-id (get-tx-id rs)]
 
           (state/update-state! :history/tx->editor-cursor
@@ -131,7 +114,7 @@
           ;; update the current edit block to include full information
           (when-let [block (state/get-edit-block)]
             (when (and (:block/uuid block) (not (:db/id block)))
-              (state/set-state! :editor/block (db/pull [:block/uuid (:block/uuid block)]))))
+              (state/set-state! :editor/block (d/pull @conn '[*] [:block/uuid (:block/uuid block)]))))
           rs)
         (catch :default e
           (log/error :exception e)

+ 15 - 2
src/main/frontend/modules/outliner/pipeline.cljs

@@ -6,7 +6,8 @@
             [frontend.handler.file-based.property.util :as property-util]
             [frontend.state :as state]
             [frontend.util.cursor :as cursor]
-            [frontend.util.drawer :as drawer]))
+            [frontend.util.drawer :as drawer]
+            [frontend.modules.editor.undo-redo :as undo-redo]))
 
 (defn- reset-editing-block-content!
   [tx-data tx-meta]
@@ -32,8 +33,20 @@
                   (state/set-edit-content! input content)
                   (when pos (cursor/move-cursor-to input pos)))))))))))
 
+(defn store-undo-data!
+  [{:keys [tx-meta] :as opts}]
+  (when-not config/test?
+    (when-let [replace-tx-data (:replace-tx-data opts)]
+      (db/transact! (state/get-current-repo) replace-tx-data (:replace-tx-meta opts)))
+
+    (when (or (:outliner/transact? tx-meta)
+              (:outliner-op tx-meta)
+              (:whiteboard/transact? tx-meta))
+      (undo-redo/listen-db-changes! opts))))
+
 (defn invoke-hooks
-  [{:keys [tx-meta tx-data deleted-block-uuids affected-keys blocks]}]
+  [{:keys [tx-meta tx-data deleted-block-uuids affected-keys blocks] :as opts}]
+  (store-undo-data! opts)
   (let [{:keys [from-disk? new-graph?]} tx-meta
         repo (state/get-current-repo)
         tx-report {:tx-meta tx-meta

+ 4 - 3
src/main/frontend/persist_db/browser.cljs

@@ -11,9 +11,10 @@
             [cljs-bean.core :as bean]
             [frontend.state :as state]
             [electron.ipc :as ipc]
-            [frontend.modules.outliner.datascript :as outliner-db]
+            [frontend.modules.outliner.pipeline :as pipeline]
             [clojure.edn :as edn]
-            [frontend.handler.worker :as worker-handler]))
+            [frontend.handler.worker :as worker-handler]
+            [frontend.db :as db]))
 
 (defonce *sqlite (atom nil))
 
@@ -100,7 +101,7 @@
                                       (pr-str context))
                     data (edn/read-string result)]
               (state/pub-event! [:search/transact-data repo (:search-indice data)])
-              (outliner-db/after-transact-pipelines data)
+              (pipeline/invoke-hooks data)
               (ipc/ipc :db-transact repo (:tx-data data) (:tx-meta data))
               nil)
             (notification/show! "Latest change was not saved! Please restart the application." :error))))))

+ 40 - 0
src/main/frontend/worker/file/property_util.cljs

@@ -136,3 +136,43 @@
                                         (or (string/starts-with? s (str ":" key ":"))
                                             (string/starts-with? s (str key gp-property/colons " ")))))))]
            (string/join "\n" lines)))))))
+
+(defn remove-properties
+  [format content]
+  (cond
+    (gp-property/contains-properties? content)
+    (let [lines (string/split-lines content)
+          [title-lines properties&body] (split-with #(-> (string/triml %)
+                                                         string/upper-case
+                                                         (string/starts-with? properties-start)
+                                                         not)
+                                                    lines)
+          body (drop-while #(-> (string/trim %)
+                                string/upper-case
+                                (string/starts-with? properties-end)
+                                not
+                                (or (string/blank? %)))
+                           properties&body)
+          body (if (and (seq body)
+                        (-> (first body)
+                            string/triml
+                            string/upper-case
+                            (string/starts-with? properties-end)))
+                 (let [line (string/replace (first body) #"(?i):END:\s?" "")]
+                   (if (string/blank? line)
+                     (rest body)
+                     (cons line (rest body))))
+                 body)]
+      (->> (concat title-lines body)
+           (string/join "\n")))
+
+    (not= format :org)
+    (let [lines (string/split-lines content)
+          lines (if (simplified-property? (first lines))
+                  (drop-while simplified-property? lines)
+                  (cons (first lines)
+                        (drop-while simplified-property? (rest lines))))]
+      (string/join "\n" lines))
+
+    :else
+    content))