From 96c9e8d7d5ab6ccb82bd54b6654f2474796054f5 Mon Sep 17 00:00:00 2001 From: Tienson Qin Date: Thu, 11 Jan 2024 20:17:11 +0800 Subject: [PATCH] simplify pipeline --- src/main/frontend/commands.cljs | 6 ++-- src/main/frontend/components/editor.cljs | 3 +- src/main/frontend/handler/block.cljs | 6 +--- src/main/frontend/handler/editor.cljs | 3 +- .../frontend/handler/editor/lifecycle.cljs | 19 +++++++---- .../handler/file_based/property/util.cljs | 11 ++++++- .../frontend/modules/outliner/pipeline.cljs | 32 +------------------ 7 files changed, 31 insertions(+), 49 deletions(-) diff --git a/src/main/frontend/commands.cljs b/src/main/frontend/commands.cljs index 6dda9da9f..147384bcf 100644 --- a/src/main/frontend/commands.cljs +++ b/src/main/frontend/commands.cljs @@ -354,7 +354,7 @@ (defn insert! [id value {:keys [last-pattern postfix-fn backward-pos end-pattern backward-truncate-number - command only-breakline? skip-blank-value-check?] + command only-breakline?] :as _option}] (when-let [input (gdom/getElement id)] (let [last-pattern (when-not (= last-pattern :skip-check) @@ -420,8 +420,8 @@ (str prefix postfix)) new-pos (- (count prefix) (or backward-pos 0))] - (when (or (not (string/blank? new-value)) - skip-blank-value-check?) + (when-not (and (not (string/blank? value)) + (string/blank? new-value)) (state/set-block-content-and-last-pos! id new-value new-pos) (cursor/move-cursor-to input new-pos))))) diff --git a/src/main/frontend/components/editor.cljs b/src/main/frontend/components/editor.cljs index bf6830b05..32bfd9bf1 100644 --- a/src/main/frontend/components/editor.cljs +++ b/src/main/frontend/components/editor.cljs @@ -718,8 +718,7 @@ (rum/defcs box < rum/reactive {:init (fn [state] - (assoc state - ::id (str (random-uuid)))) + (assoc state ::id (str (random-uuid)))) :did-mount (fn [state] (state/set-editor-args! (:rum/args state)) state)} diff --git a/src/main/frontend/handler/block.cljs b/src/main/frontend/handler/block.cljs index 1177dca81..b0f404948 100644 --- a/src/main/frontend/handler/block.cljs +++ b/src/main/frontend/handler/block.cljs @@ -13,7 +13,6 @@ [goog.dom :as gdom] [logseq.graph-parser.block :as gp-block] [frontend.config :as config] - [frontend.util.drawer :as drawer] [frontend.handler.file-based.property.util :as property-util] [frontend.handler.property.util :as pu] [dommy.core :as dom] @@ -247,10 +246,7 @@ :else (subs content 0 pos)) - content (if db-graph? - content - (-> (property-util/remove-built-in-properties (:block/format block) content) - (drawer/remove-logbook)))] + content (property-util/sanity-block-content repo (:block/format block) content)] (state/clear-selection!) (edit-block-aux repo block content block-node text-range opts))))) diff --git a/src/main/frontend/handler/editor.cljs b/src/main/frontend/handler/editor.cljs index 3b01863f3..e6d2de726 100644 --- a/src/main/frontend/handler/editor.cljs +++ b/src/main/frontend/handler/editor.cljs @@ -2222,7 +2222,8 @@ (ui-outliner-tx/transact! {:outliner-op :insert-blocks :created-from-journal-template? journal?} - (save-current-block!) + (when-not (string/blank? (state/get-edit-content)) + (save-current-block!)) (let [result (outliner-core/insert-blocks! repo (db/get-db false) blocks' target (assoc opts :sibling? sibling?'))] diff --git a/src/main/frontend/handler/editor/lifecycle.cljs b/src/main/frontend/handler/editor/lifecycle.cljs index 46e6b14a1..c2ba05d2f 100644 --- a/src/main/frontend/handler/editor/lifecycle.cljs +++ b/src/main/frontend/handler/editor/lifecycle.cljs @@ -5,7 +5,8 @@ [frontend.util :as util] [frontend.util.cursor :as cursor] [goog.dom :as gdom] - [frontend.db :as db])) + [frontend.db :as db] + [frontend.handler.file-based.property.util :as property-util])) (defn did-mount! [state] @@ -30,16 +31,22 @@ state) (defn will-remount! - [_old-state state] + [old-state state] (keyboards-handler/esc-save! state) - state) + (let [old-content (:block/content (:block (first (:rum/args old-state)))) + new-content (:block/content (:block (first (:rum/args state)))) + input (state/get-input)] + (when (and input (not= old-content new-content)) + (set! (.-new-value input) new-content)) + state)) (defn will-unmount [state] - (let [{:keys [value] :as state} (editor-handler/get-state)] + (let [{:keys [value block node] :as state} (editor-handler/get-state) + new-value (or (and node (.-new-value node)) value)] (editor-handler/clear-when-saved!) - (when (db/entity [:block/uuid (:block/uuid (:block state))]) ; block still exists - (editor-handler/save-block! state value))) + (when (db/entity [:block/uuid (:block/uuid block)]) ; block still exists + (editor-handler/save-block! state new-value))) state) (def lifecycle diff --git a/src/main/frontend/handler/file_based/property/util.cljs b/src/main/frontend/handler/file_based/property/util.cljs index 9a2ba5e14..a9394cd0e 100644 --- a/src/main/frontend/handler/file_based/property/util.cljs +++ b/src/main/frontend/handler/file_based/property/util.cljs @@ -8,7 +8,9 @@ [frontend.format.mldoc :as mldoc] [frontend.db :as db] [frontend.state :as state] - [frontend.util.cursor :as cursor])) + [frontend.util.cursor :as cursor] + [logseq.db.sqlite.util :as sqlite-util] + [frontend.util.drawer :as drawer])) (defn hidden-properties "These are properties hidden from user including built-in ones and ones @@ -236,3 +238,10 @@ (keep (fn [k] (when (contains? properties k) [k (get properties k)])) (distinct properties-order)) properties*))) + +(defn sanity-block-content + [repo format content] + (if (sqlite-util/db-based-graph? repo) + content + (-> (remove-built-in-properties format content) + (drawer/remove-logbook)))) diff --git a/src/main/frontend/modules/outliner/pipeline.cljs b/src/main/frontend/modules/outliner/pipeline.cljs index 817c2c90f..16f1d0691 100644 --- a/src/main/frontend/modules/outliner/pipeline.cljs +++ b/src/main/frontend/modules/outliner/pipeline.cljs @@ -1,11 +1,8 @@ (ns frontend.modules.outliner.pipeline - (:require [clojure.string :as string] - [frontend.config :as config] + (:require [frontend.config :as config] [frontend.db :as db] [frontend.db.react :as react] - [frontend.handler.file-based.property.util :as property-util] [frontend.state :as state] - [frontend.util.drawer :as drawer] [frontend.modules.editor.undo-redo :as undo-redo] [datascript.core :as d] [frontend.handler.ui :as ui-handler] @@ -13,27 +10,6 @@ [logseq.db :as ldb] [promesa.core :as p])) -(defn- reset-editing-block-content! - [tx-data] - (let [repo (state/get-current-repo) - db? (config/db-based-graph? repo)] - (when-let [edit-block (state/get-edit-block)] - (when-let [last-datom (-> (filter (fn [datom] - (and (= :block/content (:a datom)) - (= (:e datom) (:db/id edit-block)))) tx-data) - last)] - (when-let [input (state/get-input)] - (when (:added last-datom) - (let [entity (db/entity (:e last-datom)) - db-content (:block/content entity) - content (if db? db-content - (->> db-content - (property-util/remove-built-in-properties (or (:block/format entity) :markdown)) - drawer/remove-logbook))] - (when (not= (string/trim content) - (string/trim (.-value input))) - (state/set-edit-content! input content))))))))) - (defn store-undo-data! [{:keys [tx-meta] :as opts}] (when-not config/test? @@ -94,12 +70,6 @@ (react/clear-query-state!) (ui-handler/re-render-root!)) (do - (try - (reset-editing-block-content! tx-data) - (catch :default e - (prn :reset-editing-block-content) - (js/console.error e))) - (when-not (:graph/importing @state/state) (react/refresh! repo tx-report affected-keys)