Fix: creating new pages from bracketing text is broken #4971

close #4971
This commit is contained in:
Tienson Qin
2022-04-21 22:55:50 +08:00
parent 067f25f4bc
commit 403bb10b14
13 changed files with 70 additions and 71 deletions

View File

@@ -82,8 +82,7 @@
(let [db-page-name (db-model/get-redirect-page-name page-name)]
;; No error handling required, as a page name is always valid
;; Open new page if the page does not exist
(editor-handler/insert-first-page-block-if-not-exists! db-page-name)
(route-handler/redirect-to-page! db-page-name))
(editor-handler/insert-first-page-block-if-not-exists! db-page-name))
block-id
(if (db-model/get-block-by-uuid block-id)

View File

@@ -387,21 +387,14 @@
:on-mouse-down
(fn [e]
(util/stop e)
(let [create-first-block! (fn []
(when-not (editor-handler/add-default-title-property-if-needed! redirect-page-name)
(editor-handler/insert-first-page-block-if-not-exists! redirect-page-name)))]
(if (gobj/get e "shiftKey")
(do
(js/setTimeout create-first-block! 310)
(when-let [page-entity (db/entity [:block/name redirect-page-name])]
(state/sidebar-add-block!
(state/get-current-repo)
(:db/id page-entity)
:page
{:page page-entity})))
(do
(create-first-block!)
(route-handler/redirect-to-page! redirect-page-name))))
(if (gobj/get e "shiftKey")
(when-let [page-entity (db/entity [:block/name redirect-page-name])]
(state/sidebar-add-block!
(state/get-current-repo)
(:db/id page-entity)
:page
{:page page-entity}))
(state/pub-event! [:page/create page-name]))
(when (and contents-page?
(util/mobile?)
(state/get-left-sidebar-open?))
@@ -459,7 +452,7 @@
[:span.text-sm.mr-2 "Alias:"]
page-original-name])])
(let [page (db/entity [:block/name (util/page-name-sanity-lc redirect-page-name)])]
(editor-handler/insert-first-page-block-if-not-exists! redirect-page-name)
(editor-handler/insert-first-page-block-if-not-exists! redirect-page-name {:redirect? false})
(when-let [f (state/get-page-blocks-cp)]
(f (state/get-current-repo) page {:sidebar? sidebar? :preview? true})))]))]
(if (or (not manual?) open?)

View File

@@ -82,7 +82,7 @@
(rum/defc dummy-block
[page-name]
(let [handler-fn (fn []
(let [block (editor-handler/insert-first-page-block-if-not-exists! page-name :check-empty-page? false)]
(let [block (editor-handler/insert-first-page-block-if-not-exists! page-name {:redirect? false})]
(js/setTimeout #(editor-handler/edit-block! block :max (:block/uuid block)) 0)))]
[:div.ls-block.flex-1.flex-col.rounded-sm {:style {:width "100%"}}
[:div.flex.flex-row
@@ -194,8 +194,8 @@
(let [new-page-name (string/trim @*title-value)
merge? (and (not= (util/page-name-sanity-lc page-name)
(util/page-name-sanity-lc @*title-value))
(page-handler/page-exists? page-name)
(page-handler/page-exists? @*title-value))]
(db/page-exists? page-name)
(db/page-exists? @*title-value))]
(ui/make-confirm-modal
{:title (if merge?
(str "Page “" @*title-value "” already exists, merge to it?")

View File

@@ -9,7 +9,6 @@
[frontend.handler :as handler]
[frontend.handler.config :as config-handler]
[frontend.handler.notification :as notification]
[frontend.handler.page :as page-handler]
[frontend.handler.route :as route-handler]
[frontend.handler.ui :as ui-handler]
[frontend.handler.user :as user-handler]
@@ -25,7 +24,8 @@
[goog.object :as gobj]
[reitit.frontend.easy :as rfe]
[rum.core :as rum]
[frontend.mobile.util :as mobile-util]))
[frontend.mobile.util :as mobile-util]
[frontend.db :as db]))
(defn toggle
[label-for name state on-toggle & [detail-text]]
@@ -357,7 +357,7 @@
(config-handler/set-config! :default-home new-home)
(notification/show! "Home default page updated successfully!" :success))
(page-handler/page-exists? (string/lower-case value))
(db/page-exists? value)
(let [home (get (state/get-config) :default-home {})
new-home (assoc home :page value)]
(config-handler/set-config! :default-home new-home)

View File

@@ -53,7 +53,7 @@
get-page-referenced-blocks get-page-referenced-pages get-page-unlinked-references get-page-referenced-blocks-no-cache
get-all-pages get-pages get-pages-relation get-pages-that-mentioned-page get-public-pages get-tag-pages
journal-page? mark-repo-as-cloned! page-alias-set pull-block
set-file-last-modified-at! transact-files-db! page-empty? page-empty-or-dummy? get-alias-source-page
set-file-last-modified-at! transact-files-db! page-empty? page-exists? page-empty-or-dummy? get-alias-source-page
set-file-content! has-children? get-namespace-pages get-all-namespace-relation get-pages-by-name-partition]
[frontend.db.react

View File

@@ -716,7 +716,15 @@
(when-let [db (conn/get-db repo)]
(count (d/datoms db :avet :block/page page-id))))
(defn page-exists?
"Whether a page exists."
[page-name]
(when page-name
(db-utils/entity [:block/name (util/page-name-sanity-lc page-name)])))
(defn page-empty?
"Whether a page is empty. Does it has a non-page block?
`page-id` could be either a string or a db/id."
[repo page-id]
(when-let [db (conn/get-db repo)]
(let [page-id (if (string? page-id)
@@ -886,25 +894,29 @@
(db-utils/entity [:block/name (util/page-name-sanity-lc page-name)])))
(defn get-redirect-page-name
"Given any readable page-name, return the exact page-name in db. Accepts both
"Given any readable page-name, return the exact page-name in db. If page
doesn't exists yet, will return the passed `page-name`. Accepts both
sanitized or unsanitized names.
alias?: if true, alias is allowed to be returned; otherwise, it would be deref."
([page-name] (get-redirect-page-name page-name false))
([page-name alias?]
(when page-name
(let [page-name (util/page-name-sanity-lc page-name)
(let [page-name' (util/page-name-sanity-lc page-name)
page-entity (db-utils/entity [:block/name page-name])]
(cond
alias?
page-name'
(nil? page-entity)
page-name
(page-empty-or-dummy? (state/get-current-repo) (:db/id page-entity))
(let [source-page (get-alias-source-page (state/get-current-repo) page-name)]
(or (when source-page (:block/name source-page))
page-name))
page-name'))
:else
page-name)))))
page-name')))))
(defn get-page-original-name
[page-name]

View File

@@ -7,7 +7,8 @@
[frontend.handler.notification :as notification]
[frontend.state :as state]
[frontend.handler.editor :as editor-handler]
[frontend.handler.page :as page-handler]))
[frontend.handler.page :as page-handler]
[frontend.db :as db]))
(defn add [page-name type item]
(go
@@ -68,7 +69,7 @@
(go
(let [{:keys [page-name properties abstract-note]} (extractor/extract item)]
(if (page-handler/page-exists? (str/lower-case page-name))
(if (db/page-exists? (str/lower-case page-name))
(if (setting/setting :overwrite-mode?)
(page-handler/delete!
page-name

View File

@@ -589,9 +589,11 @@
(state/set-editor-op! nil)))
(defn api-insert-new-block!
[content {:keys [page block-uuid sibling? before? properties custom-uuid replace-empty-target?]
[content {:keys [page block-uuid sibling? before? properties
custom-uuid replace-empty-target? edit-block?]
:or {sibling? false
before? false}}]
before? false
edit-block? true}}]
(when (or page block-uuid)
(let [before? (if page false before?)
sibling? (if before? true (if page false sibling?))
@@ -650,18 +652,17 @@
(outliner-insert-block! {} block-m new-block {:sibling? sibling?
:keep-uuid? true
:replace-empty-target? replace-empty-target?})
(js/setTimeout #(edit-block! new-block :max (:block/uuid new-block)) 10)
(when edit-block?
(js/setTimeout #(edit-block! new-block :max (:block/uuid new-block)) 10))
new-block))))))
(defn insert-first-page-block-if-not-exists!
[page-name & {:keys [check-empty-page?]
:or {check-empty-page? true}}]
(when (string? page-name)
(when-let [page (db/entity [:block/name (util/page-name-sanity-lc page-name)])]
(when (or
(and check-empty-page? (db/page-empty? (state/get-current-repo) (:db/id page)))
(false? check-empty-page?))
(api-insert-new-block! "" {:page page-name})))))
([page-name]
(insert-first-page-block-if-not-exists! page-name {}))
([page-name opts]
(when (and (string? page-name)
(not (string/blank? page-name)))
(state/pub-event! [:page/create page-name opts]))))
(defn properties-block
[properties format page]
@@ -1172,8 +1173,7 @@
(js/window.open page)
(let [page-name (db-model/get-redirect-page-name page)]
(state/clear-edit!)
(insert-first-page-block-if-not-exists! page-name)
(route-handler/redirect-to-page! page-name))))))
(insert-first-page-block-if-not-exists! page-name))))))
(defn open-link-in-sidebar!
[]
@@ -2311,8 +2311,7 @@
"page-ref" (when-not (string/blank? (:link thing-at-point))
(let [page (:link thing-at-point)
page-name (db-model/get-redirect-page-name page)]
(insert-first-page-block-if-not-exists! page-name)
(route-handler/redirect-to-page! page-name)))
(insert-first-page-block-if-not-exists! page-name)))
"list-item" (dwim-in-list)
"properties-drawer" (dwim-in-properties state))

View File

@@ -249,6 +249,9 @@
(defmethod handle :page/title-property-changed [[_ old-title new-title]]
(page-handler/rename! old-title new-title))
(defmethod handle :page/create [[_ page-name opts]]
(page-handler/create! page-name opts))
(defmethod handle :page/create-today-journal [[_ _repo]]
(p/let [_ (page-handler/create-today-journal!)]
(ui-handler/re-render-root!)))

View File

@@ -78,7 +78,7 @@
(block/extract-blocks parsed-blocks "" true :markdown)
(mapv editor/wrap-parse-block))
page-name (:title headers)]
(when (not (page/page-exists? page-name))
(when (not (db/page-exists? page-name))
(page/create! page-name {:redirect? false}))
(let [page-block (db/entity [:block/name (util/page-name-sanity-lc page-name)])
children (:block/_parent page-block)

View File

@@ -95,8 +95,9 @@
split-namespace? true}}]
(let [title (string/trim title)
title (util/remove-boundary-slashes title)
page-name (util/page-name-sanity-lc title)]
(when-not (db/entity [:block/name page-name])
page-name (util/page-name-sanity-lc title)
repo (state/get-current-repo)]
(when-not (db/page-exists? page-name)
(let [pages (if split-namespace?
(util/split-namespace-pages title)
[title])
@@ -104,7 +105,7 @@
pages (map (fn [page]
(-> (block/page-name->map page true)
(assoc :block/format format)))
pages)
pages)
txs (->> pages
;; for namespace pages, only last page need properties
drop-last
@@ -112,19 +113,15 @@
(remove nil?))
last-txs (build-page-tx format properties (last pages) journal?)
txs (concat txs last-txs)]
(db/transact! txs)))
;; (util/pprint txs)
(db/transact! txs)
(when create-first-block?
(when (db/page-empty? repo (:db/id (db/entity [:block/name page-name])))
(editor-handler/api-insert-new-block! "" {:page page-name})))
(when create-first-block?
(editor-handler/insert-first-page-block-if-not-exists! page-name))
(when-let [page (db/entity [:block/name page-name])]
(outliner-file/sync-to-file page))
(when redirect?
(route-handler/redirect-to-page! page-name))
page-name)))))
(when redirect?
(route-handler/redirect-to-page! page-name))
page-name)))
(defn delete-file!
[repo page-name]
@@ -453,16 +450,11 @@
(p/let [_ (rename-page-aux old-page-title new-page-title redirect?)]
(println "Renamed " old-page-title " to " new-page-title)))))))
(defn page-exists?
[page-name]
(when page-name
(db/entity [:block/name (util/page-name-sanity-lc page-name)])))
(defn merge-pages!
"Only accepts sanitized page names"
[from-page-name to-page-name]
(when (and (page-exists? from-page-name)
(page-exists? to-page-name)
(when (and (db/page-exists? from-page-name)
(db/page-exists? to-page-name)
(not= from-page-name to-page-name))
(let [to-page (db/entity [:block/name to-page-name])
to-id (:db/id to-page)

View File

@@ -43,5 +43,6 @@
event)})
(defn init []
(let [config (clj->js config)]
(Sentry/init config)))
(when-not cfg/dev?
(let [config (clj->js config)]
(Sentry/init config))))

View File

@@ -47,7 +47,6 @@
:block/children :block/meta :block/top? :block/bottom? :block/anchor
:block/title :block/body :block/level :block/container)
m)) txs)]
;; (util/pprint txs)
(when (and (seq txs)
(not (:skip-transact? opts)))
(try