fix: use page-handler/<create! if possible

pull/10793/head
Tienson Qin 2024-01-11 00:54:19 +08:00
parent 7600a53929
commit baefeaad5b
15 changed files with 280 additions and 242 deletions

View File

@ -183,8 +183,9 @@
(reset! (:editor/create-page? @state/state) false))))
(defn rebuild-block-refs
[repo conn db date-formatter block new-properties & {:keys [skip-content-parsing?]}]
(let [property-key-refs (keys new-properties)
[repo conn date-formatter block new-properties & {:keys [skip-content-parsing?]}]
(let [db @conn
property-key-refs (keys new-properties)
property-value-refs (->> (vals new-properties)
(mapcat (fn [v]
(cond
@ -192,7 +193,7 @@
v
(uuid? v)
(when-let [entity (d/entity conn [:block/uuid v])]
(when-let [entity (d/entity db [:block/uuid v])]
(let [from-property? (get-in entity [:block/metadata :created-from-property])]
(if (and from-property? (not (contains? (:block/type entity) "closed value")))
;; don't reference hidden block property values except closed values
@ -216,9 +217,9 @@
(concat property-refs content-refs)))
(defn- rebuild-refs
[repo conn db date-formatter txs-state block m]
[repo conn date-formatter txs-state block m]
(when (sqlite-util/db-based-graph? repo)
(let [refs (->> (rebuild-block-refs repo conn db date-formatter block (:block/properties block)
(let [refs (->> (rebuild-block-refs repo conn date-formatter block (:block/properties block)
:skip-content-parsing? true)
(concat (:block/refs m))
(concat (:block/tags m)))]
@ -344,7 +345,7 @@
(swap! txs-state conj
(dissoc m :db/other-tx)))
(rebuild-refs repo conn db date-formatter txs-state block-entity m)
(rebuild-refs repo conn date-formatter txs-state block-entity m)
this))

View File

@ -13,7 +13,7 @@
(-get-left [this conn])
(-get-right [this conn])
(-get-down [this conn])
(-save [this db conn repo date-formatter])
(-save [this txs-state conn repo date-formatter])
(-del [this db children? conn])
(-get-children [this conn]))

View File

@ -498,16 +498,17 @@
create-whiteboard? (= :whiteboard (:source-create item))
create-page? (= :page (:source-create item))
class (when create-class? (get-class-from-input @!input))]
(p/do!
(cond
create-class? (page-handler/create! class
create-class? (page-handler/<create! class
{:redirect? false
:create-first-block? false
:class? true})
create-whiteboard? (whiteboard-handler/create-new-whiteboard-and-redirect! @!input)
create-page? (page-handler/create! @!input {:redirect? true}))
create-page? (page-handler/<create! @!input {:redirect? true}))
(if create-class?
(state/pub-event! [:class/configure (db/entity [:block/name (util/page-name-sanity-lc class)])])
(state/close-modal!))))
(state/close-modal!)))))
(defn- get-filter-user-input
[input]

View File

@ -109,9 +109,9 @@
(common-util/safe-subs value (+ (count q) 4 pos)))]
(state/set-edit-content! (.-id input) value')
(state/clear-editor-action!)
(let [page-name (util/page-name-sanity-lc chosen-item)
(p/let [page-name (util/page-name-sanity-lc chosen-item)
page (db/entity [:block/name page-name])
_ (when-not page (page-handler/create! chosen-item {:redirect? false
_ (when-not page (page-handler/<create! chosen-item {:redirect? false
:create-first-block? false}))
current-block (state/get-edit-block)]
(editor-handler/api-insert-new-block! chosen-item

View File

@ -32,15 +32,15 @@
[frontend.components.property.util :as components-pu]
[promesa.core :as p]))
(defn- create-class-if-not-exists!
(defn- <create-class-if-not-exists!
[value]
(when (string? value)
(let [page-name (string/trim value)]
(when-not (string/blank? page-name)
(page-handler/create! page-name {:redirect? false
(p/let [_ (page-handler/<create! page-name {:redirect? false
:create-first-block? false
:class? true})
(pu/get-page-uuid page-name)))))
:class? true})]
(pu/get-page-uuid page-name))))))
(rum/defc class-select
[*property-schema schema-classes {:keys [multiple-choices? save-property-fn]
@ -86,16 +86,19 @@
nil))}}
multiple-choices?
(assoc :on-apply (fn [choices]
(let [choices' (map (fn [value] (or (create-class-if-not-exists! value) value)) choices)]
(swap! *property-schema assoc :classes (set choices'))
(save-property-fn)
(p/let [choices' (p/all (map (fn [value]
(p/let [result (<create-class-if-not-exists! value)]
(or result value))) choices))
_ (swap! *property-schema assoc :classes (set choices'))
_ (save-property-fn)]
(toggle-fn))))
(not multiple-choices?)
(assoc :on-chosen (fn [value]
(let [value' (or (create-class-if-not-exists! value) value)]
(swap! *property-schema assoc :classes #{value'})
(save-property-fn)
(p/let [result (<create-class-if-not-exists! value)
value' (or result value)
_ (swap! *property-schema assoc :classes #{value'})
_ (save-property-fn)]
(toggle-fn)))))]
(select/select opts)))

View File

@ -19,7 +19,8 @@
[lambdaisland.glogi :as log]
[rum.core :as rum]
[frontend.handler.route :as route-handler]
[frontend.handler.property.util :as pu]))
[frontend.handler.property.util :as pu]
[promesa.core :as p]))
(defn- select-type?
[property type]
@ -96,15 +97,16 @@
(fn [{:keys [toggle-fn]}]
(ui/datepicker value' {:on-change (fn [_e date]
(let [journal (date/js-date->journal-title date)]
(p/do!
(when-not (db/entity [:block/name (util/page-name-sanity-lc journal)])
(page-handler/create! journal {:redirect? false
(page-handler/<create! journal {:redirect? false
:create-first-block? false}))
(when (fn? on-change)
(on-change (db/entity [:block/name (util/page-name-sanity-lc journal)])))
(exit-edit-property)
(toggle-fn)
(when-let [toggle (:toggle-fn opts)]
(toggle))))}))
(toggle)))))}))
{:modal-class (util/hiccup->class
"origin-top-right.absolute.left-0.rounded-md.shadow-lg.mt-2")})))
@ -122,7 +124,7 @@
(:block/uuid page))
(exit-edit-property)))}))))
(defn- create-page-if-not-exists!
(defn- <create-page-if-not-exists!
[property classes page]
(let [page* (string/trim page)
[_ page inline-class] (or (seq (map string/trim (re-find #"(.*)#(.*)$" page*)))
@ -135,15 +137,15 @@
(or (pu/get-page-uuid inline-class)
(do (log/error :msg "Given inline class does not exist" :inline-class inline-class)
nil)))]
(page-handler/create! page {:redirect? false
(p/let [_ (page-handler/<create! page {:redirect? false
:create-first-block? false
:tags (if inline-class-uuid
[inline-class-uuid]
;; Only 1st class b/c page normally has
;; one of and not all these classes
(take 1 classes))
:class? class?})))
[page id]))
:class? class?})]
[page id])))))
(defn- select-aux
[block property {:keys [items selected-choices multiple-choices?] :as opts}]
@ -241,16 +243,15 @@
:input-opts input-opts})
multiple-choices?
(assoc :on-apply (fn [choices]
(let [pages (->> choices
(map #(create-page-if-not-exists! property string-classes %))
(map first))
(p/let [result (p/all (map #(<create-page-if-not-exists! property string-classes %) choices))
pages (map first result)
values (set (map #(pu/get-page-uuid repo %) pages))]
(when on-chosen (on-chosen values)))))
(not multiple-choices?)
(assoc :on-chosen (fn [chosen]
(let [page* (string/trim (if (string? chosen) chosen (:value chosen)))]
(when-not (string/blank? page*)
(let [[page id] (create-page-if-not-exists! property string-classes page*)
(p/let [[page id] (<create-page-if-not-exists! property string-classes page*)
id' (or id (pu/get-page-uuid repo page))]
(when on-chosen (on-chosen id'))))))))]
(select-aux block property opts')))
@ -306,6 +307,7 @@
(when (= js/document.activeElement (gdom/getElement editor-id))
(exit-edit-property false)))))
;; FIXME:
(defn create-new-block!
[block property value]
(let [last-block-id (db-property-handler/create-property-text-block! block property value
@ -315,14 +317,15 @@
(exit-edit-property)
(editor-handler/edit-block! (db/entity [:block/uuid last-block-id]) :max last-block-id)))
;; FIXME:
(defn create-new-block-from-template!
"`template`: tag block"
[block property template]
(let [repo (state/get-current-repo)
{:keys [page blocks]} (db-property-handler/property-create-new-block-from-template block property template)]
(db/transact! repo (if page (cons page blocks) blocks) {:outliner-op :insert-blocks})
(add-property! block (:block/original-name property) (:block/uuid (last blocks)))
(last blocks)))
(p/let [_ (db/transact! repo (if page (cons page blocks) blocks) {:outliner-op :insert-blocks})
_ (add-property! block (:block/original-name property) (:block/uuid (last blocks)))]
(last blocks))))
(defn- new-text-editor-opts
[repo block property value editor-id]

View File

@ -161,7 +161,8 @@
file-path)]
(if-not page
(let [label (:filename pdf-current)]
(page-handler/create! page-name {:redirect? false :create-first-block? false
(p/do!
(page-handler/<create! page-name {:redirect? false :create-first-block? false
:split-namespace? false
:format format
;; FIXME: file and file-path properties for db version
@ -174,16 +175,18 @@
url)
:file-path url}})
(db-model/get-page page-name))
(db-model/get-page page-name)))
;; try to update file path
(property-handler/add-page-property! page-name :file-path url))
page)))
(do
(property-handler/add-page-property! page-name :file-path url)
page)))))
(defn ensure-ref-block!
([pdf hl] (ensure-ref-block! pdf hl nil))
([pdf-current {:keys [id content page properties]} insert-opts]
(when-let [ref-page (and pdf-current (ensure-ref-page! pdf-current))]
(p/let [ref-page (when pdf-current (ensure-ref-page! pdf-current))]
(when ref-page
(let [ref-block (db-model/query-block-by-uuid id)]
(if-not (nil? (:block/content ref-block))
(do
@ -210,7 +213,7 @@
text (merge {:page (:block/name ref-page)
:custom-uuid id
:properties properties}
insert-opts)))))))))
insert-opts))))))))))
(defn del-ref-block!
[{:keys [id]}]

View File

@ -70,6 +70,7 @@
(go
(let [{:keys [page-name properties abstract-note]} (extractor/extract item)]
(when-not (str/blank? page-name)
;; FIXME:
(if (db/page-exists? (str/lower-case page-name))
(if (setting/setting :overwrite-mode?)
(page-handler/delete!

View File

@ -35,14 +35,31 @@
:as options}]
(let [repo (state/get-current-repo)
conn (db/get-db repo false)
config (state/get-config repo)]
(p/let [_ (worker-page/create! repo conn config title options)]
(let [[_ page-name] (worker-page/get-title-and-pagename title)]
config (state/get-config repo)
_ (worker-page/create! repo conn config title options)
[_ page-name] (worker-page/get-title-and-pagename title)]
(when redirect?
(route-handler/redirect-to-page! page-name))
(when-let [first-block (first (:block/_left (db/entity [:block/name page-name])))]
(block-handler/edit-block! first-block :max nil))
page-name)))))
page-name)))
(defn <create!
([title]
(<create! title {}))
([title {:keys [redirect?]
:or {redirect? true}
:as options}]
(p/let [repo (state/get-current-repo)
conn (db/get-db repo false)
config (state/get-config repo)
_ (worker-page/create! repo conn config title options)
[_ page-name] (worker-page/get-title-and-pagename title)]
(when redirect?
(route-handler/redirect-to-page! page-name))
(when-let [first-block (first (:block/_left (db/entity [:block/name page-name])))]
(block-handler/edit-block! first-block :max nil))
page-name)))
;; favorite fns
;; ============

View File

@ -69,9 +69,9 @@
(defn- rebuild-block-refs
[repo block new-properties & opts]
(let [db (db/get-db repo)
(let [conn (db/get-db repo false)
date-formatter (state/get-date-formatter)]
(outliner-core/rebuild-block-refs repo db date-formatter block new-properties opts)))
(outliner-core/rebuild-block-refs repo conn date-formatter block new-properties opts)))
(defn convert-property-input-string
[schema-type v-str]

View File

@ -95,8 +95,9 @@
parsed-blocks (->>
(block/extract-blocks parsed-blocks "" :markdown {:page-name page-name})
(mapv editor/wrap-parse-block))]
(p/do!
(when (not (db/page-exists? page-name))
(page-handler/create! page-name {:redirect? false}))
(page-handler/<create! page-name {:redirect? false}))
(let [page-block (db/entity [:block/name (util/page-name-sanity-lc page-name)])
children (:block/_parent page-block)
blocks (db/sort-by-left children page-block)
@ -111,7 +112,7 @@
parsed-blocks
{:target-block target-block
:sibling? sibling?})
(finished-ok-handler [page-name])))))
(finished-ok-handler [page-name]))))))
(defn create-page-with-exported-tree!
"Create page from the per page object generated in `export-repo-as-edn-v2!`
@ -127,7 +128,8 @@
has-children? (seq children)
page-format (or (some-> tree (:children) (first) (:format)) :markdown)
whiteboard? (= type "whiteboard")]
(try (page-handler/create! title {:redirect? false
(p/do!
(try (page-handler/<create! title {:redirect? false
:format page-format
:uuid uuid
:create-first-block? false
@ -159,7 +161,7 @@
(prn {:tree tree})
(notification/show! (str "Error happens when creating block content of page " title "\n"
e
"\nSkipped and continue the remaining import.") :error))))))
"\nSkipped and continue the remaining import.") :error)))))))
title)
(defn- pre-transact-uuids!

View File

@ -18,6 +18,7 @@
[frontend.handler.property :as property-handler]
[frontend.handler.ui :as ui-handler]
[frontend.handler.web.nfs :as web-nfs]
[frontend.worker.handler.page :as worker-page]
[frontend.worker.handler.page.rename :as worker-page-rename]
[frontend.mobile.util :as mobile-util]
[frontend.state :as state]
@ -39,9 +40,11 @@
[frontend.context.i18n :refer [t]]))
(def create! page-common-handler/create!)
(def <create! page-common-handler/<create!)
(def delete! page-common-handler/delete!)
(def unfavorite-page! page-common-handler/unfavorite-page!)
(def favorite-page! page-common-handler/favorite-page!)
(def get-title-and-pagename worker-page/get-title-and-pagename)
;; FIXME: add whiteboard
(defn- get-directory
@ -232,22 +235,22 @@
(subs q 1)
q))
last-pattern (str "#" (when wrapped? page-ref/left-brackets) last-pattern)]
(p/do!
(when db-based?
(let [tag (string/trim chosen)
edit-block (state/get-edit-block)]
(when (and (not (string/blank? tag)) (:block/uuid edit-block))
(let [tag-entity (db/entity [:block/name (util/page-name-sanity-lc tag)])]
(when-not tag-entity
(create! tag {:redirect? false
(p/let [tag-entity (db/entity [:block/name (util/page-name-sanity-lc tag)])
_ (when-not tag-entity
(<create! tag {:redirect? false
:create-first-block? false
:class? class?}))
:class? class?}))]
(when class?
(let [repo (state/get-current-repo)
tag-entity (or tag-entity (db/entity [:block/name (util/page-name-sanity-lc tag)]))
tx-data [[:db/add [:block/uuid (:block/uuid edit-block)] :block/tags (:db/id tag-entity)]
[:db/add [:block/uuid (:block/uuid edit-block)] :block/refs (:db/id tag-entity)]]]
(db/transact! repo tx-data {:outliner-op :save-block})))))))
(editor-handler/insert-command! id
(str "#" wrapped-tag)
format
@ -255,7 +258,7 @@
:end-pattern (when wrapped? page-ref/right-brackets)
:command :page-ref})
(when input (.focus input))))
(when input (.focus input)))))
(fn [chosen e]
(util/stop e)
(state/clear-editor-action!)
@ -285,7 +288,7 @@
template (state/get-default-journal-template)
create-f (fn []
(p/do!
(create! title {:redirect? false
(<create! title {:redirect? false
:split-namespace? false
:create-first-block? (not template)
:journal? true

View File

@ -195,6 +195,7 @@
:updated-at (util/time-ms),
:created-at (util/time-ms)}]))
;; FIXME:
(defn create-new-whiteboard-page!
([]
(create-new-whiteboard-page! nil))

View File

@ -9,7 +9,8 @@
[frontend.handler.page :as page-handler]
[frontend.state :as state]
[frontend.util :as util]
[frontend.util.text :as text-util]))
[frontend.util.text :as text-util]
[promesa.core :as p]))
(defn- is-tweet-link
[url]
@ -85,10 +86,10 @@
(editor-handler/insert content)
(editor-handler/insert (str "\n" content)))
(do
(p/do!
(editor-handler/escape-editing)
(when (not= page (state/get-current-page))
(page-handler/create! page {:redirect? redirect-page?}))
(page-handler/<create! page {:redirect? redirect-page?}))
;; Or else this will clear the newly inserted content
(js/setTimeout #(editor-handler/api-insert-new-block! content {:page page
:edit-block? true

View File

@ -555,22 +555,24 @@
(def ^:export create_page
(fn [name ^js properties ^js opts]
(some-> (if-let [page (db-model/get-page name)]
page
(let [properties (bean/->clj properties)
{:keys [redirect createFirstBlock format journal]} (bean/->clj opts)
name (page-handler/create!
{:keys [redirect createFirstBlock format journal]} (bean/->clj opts)]
(p/let [page (db-model/get-page name)
new-page (when-not page
(p/let [_ (page-handler/<create!
name
{:redirect? (if (boolean? redirect) redirect true)
:journal? journal
:create-first-block? (if (boolean? createFirstBlock) createFirstBlock true)
:format format
:properties properties})]
(db-model/get-page name)))
(:db/id)
:properties properties})
name (second (page-handler/get-title-and-pagename))]
(db-model/get-page name)))]
(some-> (or page new-page)
:db/id
(db-utils/pull)
(sdk-utils/normalize-keyword-for-json)
(bean/->js))))
(bean/->js))))))
(def ^:export delete_page
(fn [name]
@ -605,13 +607,13 @@
(fn [block-uuid-or-page-name content ^js opts]
(when (string/blank? block-uuid-or-page-name)
(throw (js/Error. "Page title or block UUID shouldn't be empty.")))
(let [{:keys [before sibling focus customUUID properties]} (bean/->clj opts)
(p/let [{:keys [before sibling focus customUUID properties]} (bean/->clj opts)
[page-name block-uuid] (if (util/uuid-string? block-uuid-or-page-name)
[nil (uuid block-uuid-or-page-name)]
[block-uuid-or-page-name nil])
page-name (when page-name (util/page-name-sanity-lc page-name))
_ (when (and page-name (not (db/entity [:block/name page-name])))
(page-handler/create! block-uuid-or-page-name {:create-first-block? false}))
(page-handler/<create! block-uuid-or-page-name {:create-first-block? false}))
custom-uuid (or customUUID (:id properties))
custom-uuid (when custom-uuid (sdk-utils/uuid-or-throw-error custom-uuid))
edit-block? (if (nil? focus) true focus)
@ -818,9 +820,9 @@
(defn ^:export prepend_block_in_page
[uuid-or-page-name content ^js opts]
(let [page? (not (util/uuid-string? uuid-or-page-name))
(p/let [page? (not (util/uuid-string? uuid-or-page-name))
page-not-exist? (and page? (nil? (db-model/get-page uuid-or-page-name)))
_ (and page-not-exist? (page-handler/create! uuid-or-page-name
_ (and page-not-exist? (page-handler/<create! uuid-or-page-name
{:redirect? false
:create-first-block? true
:format (state/get-preferred-format)}))]
@ -834,9 +836,9 @@
(defn ^:export append_block_in_page
[uuid-or-page-name content ^js opts]
(let [page? (not (util/uuid-string? uuid-or-page-name))
(p/let [page? (not (util/uuid-string? uuid-or-page-name))
page-not-exist? (and page? (nil? (db-model/get-page uuid-or-page-name)))
_ (and page-not-exist? (page-handler/create! uuid-or-page-name
_ (and page-not-exist? (page-handler/<create! uuid-or-page-name
{:redirect? false
:create-first-block? true
:format (state/get-preferred-format)}))]