fix: can't restore page's name if it includes reserved characters

Also, fixed issues related to renaming pages
pull/1862/head
Tienson Qin 2021-05-14 02:48:20 +08:00
parent 085b8937e0
commit 662fe83b7f
9 changed files with 149 additions and 67 deletions

View File

@ -1282,27 +1282,32 @@
(rum/defc property-cp
[config block k v]
[:div.my-1
[:b (name k)]
[:span.mr-1 ":"]
(cond
(int? v)
v
(let [pre-block? (:block/pre-block? block)
date (and (= k :date) (date/get-locale-string (str v)))]
[:div
[:span.font-bold (name k)]
[:span.mr-1 ":"]
(cond
(int? v)
v
(coll? v)
(let [v (->> (remove string/blank? v)
(filter string?))
vals (for [v-item v]
(page-cp config {:block/name v-item}))
elems (interpose (span-comma) vals)]
(for [elem elems]
(rum/with-key elem (str (random-uuid)))))
date
date
:else
(let [page-name (string/lower-case (str v))]
(if (db/entity [:block/name page-name])
(page-cp config {:block/name page-name})
(inline-text (:block/format block) (str v)))))])
(coll? v)
(let [v (->> (remove string/blank? v)
(filter string?))
vals (for [v-item v]
(page-cp config {:block/name v-item}))
elems (interpose (span-comma) vals)]
(for [elem elems]
(rum/with-key elem (str (random-uuid)))))
:else
(let [page-name (string/lower-case (str v))]
(if (db/entity [:block/name page-name])
(page-cp config {:block/name page-name})
(inline-text (:block/format block) (str v)))))]))
(rum/defc properties-cp
[config block]
@ -1315,7 +1320,7 @@
properties (sort properties)]
(cond
(seq properties)
[:div.blocks-properties.text-sm.opacity-80.my-1.p-2
[:div
(for [[k v] properties]
(rum/with-key (property-cp config block k v)
(str (:block/uuid block) "-" k)))]

View File

@ -55,25 +55,18 @@
(page-handler/add-page-to-recent! repo page-original-name)
(db/get-page-blocks repo page-name)))))
(defn- open-first-block!
[state]
(let [blocks (nth (:rum/args state) 1)
block (first blocks)]
(when (:block/dummy? block)
(editor-handler/edit-block! block :max (:block/format block) (:block/uuid block))))
state)
(rum/defc page-blocks-inner <
{:did-mount (fn [state]
(let [block (first (nth (:rum/args state) 1))]
(when (:block/dummy? block)
(editor-handler/edit-block! block 0 (:block/format block) (:block/uuid block))))
state)}
{:did-mount open-first-block!
:did-update open-first-block!}
[page-name page-blocks hiccup sidebar?]
[:div.page-blocks-inner
;; (when (and (seq grouped-blocks-by-file)
;; (> (count grouped-blocks-by-file) 1))
;; (ui/admonition
;; :warning
;; [:div.text-sm
;; [:p.font-medium "Those pages have the same title, you might want to only keep one file."]
;; [:ol
;; (for [[file-path blocks] (into (sorted-map) grouped-blocks-by-file)]
;; [:li [:a {:key file-path
;; :href (rfe/href :file {:path file-path})} file-path]])]]))
(rum/with-key
(content/content page-name
{:hiccup hiccup
@ -86,9 +79,6 @@
db-mixins/query
[repo page-e file-path page-name page-original-name encoded-page-name sidebar? journal? block? block-id format]
(let [raw-page-blocks (get-blocks repo page-name page-original-name block? block-id)
;; grouped-blocks-by-file (into {} (for [[k v] (db-utils/group-by-file raw-page-blocks)]
;; [(:file/path (db-utils/entity (:db/id k))) v]))
;; raw-page-blocks (get grouped-blocks-by-file file-path raw-page-blocks)
page-blocks (block-handler/with-dummy-block raw-page-blocks format
(if (empty? raw-page-blocks)
{:block/page {:db/id (:db/id page-e)}

View File

@ -20,7 +20,7 @@
(when-let [formatter-string (state/get-date-formatter)]
(tf/unparse (tf/formatter formatter-string) date)))
(def custom-formatter (tf/formatter "yyyy-MM-dd HH:mm:ssZ"))
(def custom-formatter (tf/formatter "yyyy-MM-dd'T'HH:mm:ssZZ"))
(defn journal-title-formatters
[]
@ -44,8 +44,24 @@
"yyyy年MM月dd日"}
(state/get-date-formatter)))
(defn get-date-time-string [date-time]
(tf/unparse custom-formatter date-time))
(defn get-date-time-string
([]
(get-date-time-string (t/now)))
([date-time]
(tf/unparse custom-formatter date-time)))
(defn get-locale-string
[s]
(try
(->> (tf/parse (tf/formatters :date-time-no-ms) s)
(t/to-default-time-zone)
(tf/unparse (tf/formatter "MMM do, yyyy")))
(catch js/Error e
nil)))
(defn ISO-string
[]
(.toISOString (js/Date.)))
(defn get-local-date-time-string
[]

View File

@ -11,7 +11,9 @@
[medley.core :as medley]
[frontend.format.block :as block]
[frontend.debug :as debug]
[clojure.string :as string]))
[clojure.string :as string]
[frontend.text :as text]
[frontend.handler.common :as common-handler]))
(defn get-block-ids
[block]
@ -35,14 +37,21 @@
blocks (vec blocks)]
(if (seq blocks)
blocks
(let [page-block (when page-name (db/entity [:block/name (string/lower-case page-name)]))
(let [page-block (when page-name (db/pull [:block/name (string/lower-case page-name)]))
create-title-property? (util/include-windows-reserved-chars? page-name)
content (if create-title-property?
(let [title (or (:block/original-name page-block)
(:block/name page-block))
properties (common-handler/get-page-default-properties title)]
(text/build-properties-str format properties))
"")
page-id {:db/id (:db/id page-block)}
dummy (merge {:block/uuid (db/new-block-id)
:block/left page-id
:block/parent page-id
:block/page page-id
:block/title ""
:block/content ""
:block/content content
:block/format format
:block/dummy? true}
default-option)

View File

@ -15,7 +15,8 @@
[cljs-time.format :as tf]
[frontend.config :as config]
["ignore" :as Ignore]
["/frontend/utils" :as utils]))
["/frontend/utils" :as utils]
[frontend.date :as date]))
(defn get-ref
[repo-url]
@ -160,3 +161,9 @@
(do (log/error :token/failed-get-token token-m)
(reject)))))
nil))))))))
(defn get-page-default-properties
[page-name]
{:title page-name
;; :date (date/get-date-time-string)
})

View File

@ -48,18 +48,49 @@
([page-name] (when-let [page (db/entity [:block/name page-name])]
(:file/path (:block/file page)))))
(defn default-properties-block
[title format page]
(let [properties (common-handler/get-page-default-properties title)
content (text/build-properties-str format properties)]
{:block/pre-block? true
:block/uuid (db/new-block-id)
:block/properties properties
:block/left page
:block/format format
:block/content content
:block/parent page
:block/unordered true
:block/page page}))
(defn create!
([title]
(create! title {}))
([title {:keys [redirect?]
:or {redirect? true}}]
(let [title (string/trim title)
page (string/lower-case title)]
(let [tx (block/page-name->map title true)]
(db/transact! [tx]))
page (string/lower-case title)
tx (block/page-name->map title true)
format (state/get-preferred-format)
page-entity [:block/uuid (:block/uuid tx)]
create-title-property? (util/include-windows-reserved-chars? title)
default-properties (default-properties-block title format page-entity)
empty-block {:block/uuid (db/new-block-id)
:block/left [:block/uuid (:block/uuid default-properties)]
:block/format format
:block/content ""
:block/parent page-entity
:block/unordered true
:block/page page-entity}
txs (if create-title-property?
[tx default-properties empty-block]
[tx])]
(db/transact! txs)
(when redirect?
(route-handler/redirect! {:to :page
:path-params {:name page}})))))
:path-params {:name page}})
(when create-title-property?
(js/setTimeout (fn []
(editor-handler/edit-block! empty-block 0 format (:block/uuid empty-block))) 50))))))
(defn page-add-property!
[page-name key value]
@ -173,12 +204,12 @@
(defn rename-file!
[file new-name ok-handler]
(let [repo (state/get-current-repo)
file (db/pull (:db/id file))
old-path (:file/path file)
new-path (compute-new-file-path old-path new-name)]
;; update db
(db/transact! repo [{:db/id (:db/id file)
:file/path new-path}])
(->
(p/let [_ (fs/rename! repo
(if (util/electron?)
@ -217,15 +248,25 @@
(let [name-changed? (not= (string/lower-case (string/trim old-name))
(string/lower-case (string/trim new-name)))]
(when-let [repo (state/get-current-repo)]
(when-let [page (db/entity [:block/name (string/lower-case old-name)])]
(when-let [page (db/pull [:block/name (string/lower-case old-name)])]
(let [old-original-name (:block/original-name page)
file (:block/file page)
journal? (:block/journal? page)]
(d/transact! (db/get-conn repo false)
[{:db/id (:db/id page)
journal? (:block/journal? page)
properties-block (:data (outliner-core/get-right-node (outliner-core/block page)))
properties-block-tx (when (and properties-block
(string/includes? (string/lower-case (:block/content properties-block))
(string/lower-case old-name)))
{:db/id (:db/id properties-block)
:block/content (text/insert-property! (:block/format properties-block)
(:block/content properties-block)
:title
new-name)})
page-txs [{:db/id (:db/id page)
:block/uuid (:block/uuid page)
:block/name (string/lower-case new-name)
:block/original-name new-name}])
:block/original-name new-name}]
page-txs (if properties-block-tx (conj page-txs properties-block-tx) page-txs)]
(d/transact! (db/get-conn repo false) page-txs)
(when (and file (not journal?) name-changed?)
(rename-file! file new-name (fn [] nil)))

View File

@ -92,7 +92,8 @@
block-id))
(-get-parent-id [this]
(-> (get-in this [:data :block/parent])
(-> (or (get-in this [:data :block/parent])
(get-in this [:data :block/uuid]))
(outliner-u/->block-id)))
(-set-parent-id [this parent-id]
@ -108,7 +109,7 @@
(update this :data assoc :block/left [:block/uuid left-id]))
(-get-parent [this]
(when-let [parent-id (tree/-get-parent-id this)]
(when-let [parent-id (or (tree/-get-parent-id this) (get-in this [:data :db/id]))]
(get-block-by-id parent-id)))
(-get-left [this]

View File

@ -217,14 +217,21 @@
content)))
(defn build-properties-str
[format properties]
(when (seq properties)
(let [org? (= format :org)
kv-format (if org? ":%s: %s" "%s:: %s")
full-format (if org? ":PROPERTIES:\n%s\n:END:\n" "%s\n")
properties-content (->> (map (fn [[k v]] (util/format kv-format k v)) properties)
(string/join "\n"))]
(util/format full-format properties-content))))
([format properties]
(build-properties-str format properties false))
([format properties front-matter?]
(when (seq properties)
(let [org? (= format :org)
[kv-format wrapper] (cond
org?
[":%s: %s" ":PROPERTIES:\n%s\n:END:"]
front-matter?
["%s: %s" "---\n%s\n---"]
:else
["%s:: %s" "%s"])
properties-content (->> (map (fn [[k v]] (util/format kv-format (name k) v)) properties)
(string/join "\n"))]
(util/format wrapper properties-content)))))
;; title properties body
(defn with-built-in-properties

View File

@ -1081,12 +1081,18 @@
(when (uuid-string? block-id)
(first (array-seq (js/document.getElementsByClassName block-id))))))))
(defonce windows-reserved-chars #"[\\/:\\*\\?\"<>|]+")
(defn include-windows-reserved-chars?
[s]
(re-find windows-reserved-chars s))
(defn page-name-sanity
[page-name]
(-> page-name
(string/replace #"/" ".")
;; Windows reserved path characters
(string/replace #"[\\/:\\*\\?\"<>|]+" "_")))
(string/replace windows-reserved-chars "_")))
(defn lowercase-first
[s]